linux-module.c revision 885a947e5b08953ebd5fce88be89a0399a7ab918
1/* 2 * GPL HEADER START 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 only, 8 * as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License version 2 for more details (a copy is included 14 * in the LICENSE file that accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License 17 * version 2 along with this program; If not, see 18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf 19 * 20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 21 * CA 95054 USA or visit www.sun.com if you need additional information or 22 * have any questions. 23 * 24 * GPL HEADER END 25 */ 26/* 27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 28 * Use is subject to license terms. 29 * 30 * Copyright (c) 2011, 2012, Intel Corporation. 31 */ 32/* 33 * This file is part of Lustre, http://www.lustre.org/ 34 * Lustre is a trademark of Sun Microsystems, Inc. 35 * 36 * lustre/obdclass/linux/linux-module.c 37 * 38 * Object Devices Class Driver 39 * These are the only exported functions, they provide some generic 40 * infrastructure for managing object devices 41 */ 42 43#define DEBUG_SUBSYSTEM S_CLASS 44 45#include <linux/module.h> 46#include <linux/errno.h> 47#include <linux/kernel.h> 48#include <linux/major.h> 49#include <linux/sched.h> 50#include <linux/lp.h> 51#include <linux/slab.h> 52#include <linux/ioport.h> 53#include <linux/fcntl.h> 54#include <linux/delay.h> 55#include <linux/skbuff.h> 56#include <linux/proc_fs.h> 57#include <linux/fs.h> 58#include <linux/poll.h> 59#include <linux/list.h> 60#include <linux/highmem.h> 61#include <asm/io.h> 62#include <asm/ioctls.h> 63#include <asm/poll.h> 64#include <asm/uaccess.h> 65#include <linux/miscdevice.h> 66#include <linux/seq_file.h> 67 68#include <linux/libcfs/libcfs.h> 69#include <obd_support.h> 70#include <obd_class.h> 71#include <linux/lnet/lnetctl.h> 72#include <lprocfs_status.h> 73#include <lustre_ver.h> 74#include <lustre/lustre_build_version.h> 75 76int proc_version; 77 78/* buffer MUST be at least the size of obd_ioctl_hdr */ 79int obd_ioctl_getdata(char **buf, int *len, void *arg) 80{ 81 struct obd_ioctl_hdr hdr; 82 struct obd_ioctl_data *data; 83 int err; 84 int offset = 0; 85 86 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr)); 87 if ( err ) 88 return err; 89 90 if (hdr.ioc_version != OBD_IOCTL_VERSION) { 91 CERROR("Version mismatch kernel (%x) vs application (%x)\n", 92 OBD_IOCTL_VERSION, hdr.ioc_version); 93 return -EINVAL; 94 } 95 96 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) { 97 CERROR("User buffer len %d exceeds %d max buffer\n", 98 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER); 99 return -EINVAL; 100 } 101 102 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) { 103 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len); 104 return -EINVAL; 105 } 106 107 /* When there are lots of processes calling vmalloc on multi-core 108 * system, the high lock contention will hurt performance badly, 109 * obdfilter-survey is an example, which relies on ioctl. So we'd 110 * better avoid vmalloc on ioctl path. LU-66 */ 111 OBD_ALLOC_LARGE(*buf, hdr.ioc_len); 112 if (*buf == NULL) { 113 CERROR("Cannot allocate control buffer of len %d\n", 114 hdr.ioc_len); 115 return -EINVAL; 116 } 117 *len = hdr.ioc_len; 118 data = (struct obd_ioctl_data *)*buf; 119 120 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len); 121 if ( err ) { 122 OBD_FREE_LARGE(*buf, hdr.ioc_len); 123 return err; 124 } 125 126 if (obd_ioctl_is_invalid(data)) { 127 CERROR("ioctl not correctly formatted\n"); 128 OBD_FREE_LARGE(*buf, hdr.ioc_len); 129 return -EINVAL; 130 } 131 132 if (data->ioc_inllen1) { 133 data->ioc_inlbuf1 = &data->ioc_bulk[0]; 134 offset += cfs_size_round(data->ioc_inllen1); 135 } 136 137 if (data->ioc_inllen2) { 138 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset; 139 offset += cfs_size_round(data->ioc_inllen2); 140 } 141 142 if (data->ioc_inllen3) { 143 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset; 144 offset += cfs_size_round(data->ioc_inllen3); 145 } 146 147 if (data->ioc_inllen4) { 148 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset; 149 } 150 151 return 0; 152} 153EXPORT_SYMBOL(obd_ioctl_getdata); 154 155int obd_ioctl_popdata(void *arg, void *data, int len) 156{ 157 int err; 158 159 err = copy_to_user(arg, data, len); 160 if (err) 161 err = -EFAULT; 162 return err; 163} 164EXPORT_SYMBOL(obd_ioctl_popdata); 165 166/* opening /dev/obd */ 167static int obd_class_open(struct inode * inode, struct file * file) 168{ 169 try_module_get(THIS_MODULE); 170 return 0; 171} 172 173/* closing /dev/obd */ 174static int obd_class_release(struct inode * inode, struct file * file) 175{ 176 module_put(THIS_MODULE); 177 return 0; 178} 179 180/* to control /dev/obd */ 181static long obd_class_ioctl(struct file *filp, unsigned int cmd, 182 unsigned long arg) 183{ 184 int err = 0; 185 186 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */ 187 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET)) 188 return err = -EACCES; 189 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */ 190 return err = -ENOTTY; 191 192 err = class_handle_ioctl(cmd, (unsigned long)arg); 193 194 return err; 195} 196 197/* declare character device */ 198static struct file_operations obd_psdev_fops = { 199 .owner = THIS_MODULE, 200 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */ 201 .open = obd_class_open, /* open */ 202 .release = obd_class_release, /* release */ 203}; 204 205/* modules setup */ 206struct miscdevice obd_psdev = { 207 .minor = OBD_DEV_MINOR, 208 .name = OBD_DEV_NAME, 209 .fops = &obd_psdev_fops, 210}; 211 212 213#ifdef LPROCFS 214int obd_proc_version_seq_show(struct seq_file *m, void *v) 215{ 216 return seq_printf(m, "lustre: %s\nkernel: %s\nbuild: %s\n", 217 LUSTRE_VERSION_STRING, "patchless_client", 218 BUILD_VERSION); 219} 220LPROC_SEQ_FOPS_RO(obd_proc_version); 221 222int obd_proc_pinger_seq_show(struct seq_file *m, void *v) 223{ 224 return seq_printf(m, "%s\n", "on"); 225} 226LPROC_SEQ_FOPS_RO(obd_proc_pinger); 227 228static int obd_proc_health_seq_show(struct seq_file *m, void *v) 229{ 230 int rc = 0, i; 231 232 if (libcfs_catastrophe) 233 seq_printf(m, "LBUG\n"); 234 235 read_lock(&obd_dev_lock); 236 for (i = 0; i < class_devno_max(); i++) { 237 struct obd_device *obd; 238 239 obd = class_num2obd(i); 240 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up) 241 continue; 242 243 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); 244 if (obd->obd_stopping) 245 continue; 246 247 class_incref(obd, __FUNCTION__, current); 248 read_unlock(&obd_dev_lock); 249 250 if (obd_health_check(NULL, obd)) { 251 seq_printf(m, "device %s reported unhealthy\n", 252 obd->obd_name); 253 rc++; 254 } 255 class_decref(obd, __FUNCTION__, current); 256 read_lock(&obd_dev_lock); 257 } 258 read_unlock(&obd_dev_lock); 259 260 if (rc == 0) 261 return seq_printf(m, "healthy\n"); 262 263 seq_printf(m, "NOT HEALTHY\n"); 264 return 0; 265} 266LPROC_SEQ_FOPS_RO(obd_proc_health); 267 268static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v) 269{ 270 return seq_printf(m, "%s\n", obd_jobid_var); 271} 272 273static ssize_t obd_proc_jobid_var_seq_write(struct file *file, const char *buffer, 274 size_t count, loff_t *off) 275{ 276 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN) 277 return -EINVAL; 278 279 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1); 280 /* Trim the trailing '\n' if any */ 281 memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n')); 282 return count; 283} 284LPROC_SEQ_FOPS(obd_proc_jobid_var); 285 286/* Root for /proc/fs/lustre */ 287struct proc_dir_entry *proc_lustre_root = NULL; 288EXPORT_SYMBOL(proc_lustre_root); 289 290struct lprocfs_vars lprocfs_base[] = { 291 { "version", &obd_proc_version_fops }, 292 { "pinger", &obd_proc_pinger_fops }, 293 { "health_check", &obd_proc_health_fops }, 294 { "jobid_var", &obd_proc_jobid_var_fops }, 295 { 0 } 296}; 297 298static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos) 299{ 300 if (*pos >= class_devno_max()) 301 return NULL; 302 303 return pos; 304} 305 306static void obd_device_list_seq_stop(struct seq_file *p, void *v) 307{ 308} 309 310static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos) 311{ 312 ++*pos; 313 if (*pos >= class_devno_max()) 314 return NULL; 315 316 return pos; 317} 318 319static int obd_device_list_seq_show(struct seq_file *p, void *v) 320{ 321 loff_t index = *(loff_t *)v; 322 struct obd_device *obd = class_num2obd((int)index); 323 char *status; 324 325 if (obd == NULL) 326 return 0; 327 328 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); 329 if (obd->obd_stopping) 330 status = "ST"; 331 else if (obd->obd_inactive) 332 status = "IN"; 333 else if (obd->obd_set_up) 334 status = "UP"; 335 else if (obd->obd_attached) 336 status = "AT"; 337 else 338 status = "--"; 339 340 return seq_printf(p, "%3d %s %s %s %s %d\n", 341 (int)index, status, obd->obd_type->typ_name, 342 obd->obd_name, obd->obd_uuid.uuid, 343 atomic_read(&obd->obd_refcount)); 344} 345 346struct seq_operations obd_device_list_sops = { 347 .start = obd_device_list_seq_start, 348 .stop = obd_device_list_seq_stop, 349 .next = obd_device_list_seq_next, 350 .show = obd_device_list_seq_show, 351}; 352 353static int obd_device_list_open(struct inode *inode, struct file *file) 354{ 355 struct seq_file *seq; 356 int rc = seq_open(file, &obd_device_list_sops); 357 358 if (rc) 359 return rc; 360 361 seq = file->private_data; 362 seq->private = PDE_DATA(inode); 363 364 return 0; 365} 366 367struct file_operations obd_device_list_fops = { 368 .owner = THIS_MODULE, 369 .open = obd_device_list_open, 370 .read = seq_read, 371 .llseek = seq_lseek, 372 .release = seq_release, 373}; 374 375int class_procfs_init(void) 376{ 377 int rc = 0; 378 379 proc_lustre_root = lprocfs_register("fs/lustre", NULL, 380 lprocfs_base, NULL); 381 if (IS_ERR(proc_lustre_root)) { 382 rc = PTR_ERR(proc_lustre_root); 383 proc_lustre_root = NULL; 384 goto out; 385 } 386 387 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444, 388 &obd_device_list_fops, NULL); 389out: 390 if (rc) 391 CERROR("error adding /proc/fs/lustre/devices file\n"); 392 return 0; 393} 394 395int class_procfs_clean(void) 396{ 397 if (proc_lustre_root) { 398 lprocfs_remove(&proc_lustre_root); 399 } 400 return 0; 401} 402#endif /* LPROCFS */ 403