linux-module.c revision 29aaf4962a3bce337d37176858ef1025b9f29cc4
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/init.h> 60#include <linux/list.h> 61#include <linux/highmem.h> 62#include <asm/io.h> 63#include <asm/ioctls.h> 64#include <asm/poll.h> 65#include <asm/uaccess.h> 66#include <linux/miscdevice.h> 67#include <linux/seq_file.h> 68 69#include <linux/libcfs/libcfs.h> 70#include <obd_support.h> 71#include <obd_class.h> 72#include <linux/lnet/lnetctl.h> 73#include <lprocfs_status.h> 74#include <lustre_ver.h> 75#include <lustre/lustre_build_version.h> 76 77int proc_version; 78 79/* buffer MUST be at least the size of obd_ioctl_hdr */ 80int obd_ioctl_getdata(char **buf, int *len, void *arg) 81{ 82 struct obd_ioctl_hdr hdr; 83 struct obd_ioctl_data *data; 84 int err; 85 int offset = 0; 86 87 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr)); 88 if ( err ) 89 RETURN(err); 90 91 if (hdr.ioc_version != OBD_IOCTL_VERSION) { 92 CERROR("Version mismatch kernel (%x) vs application (%x)\n", 93 OBD_IOCTL_VERSION, hdr.ioc_version); 94 RETURN(-EINVAL); 95 } 96 97 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) { 98 CERROR("User buffer len %d exceeds %d max buffer\n", 99 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER); 100 RETURN(-EINVAL); 101 } 102 103 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) { 104 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len); 105 RETURN(-EINVAL); 106 } 107 108 /* When there are lots of processes calling vmalloc on multi-core 109 * system, the high lock contention will hurt performance badly, 110 * obdfilter-survey is an example, which relies on ioctl. So we'd 111 * better avoid vmalloc on ioctl path. LU-66 */ 112 OBD_ALLOC_LARGE(*buf, hdr.ioc_len); 113 if (*buf == NULL) { 114 CERROR("Cannot allocate control buffer of len %d\n", 115 hdr.ioc_len); 116 RETURN(-EINVAL); 117 } 118 *len = hdr.ioc_len; 119 data = (struct obd_ioctl_data *)*buf; 120 121 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len); 122 if ( err ) { 123 OBD_FREE_LARGE(*buf, hdr.ioc_len); 124 RETURN(err); 125 } 126 127 if (obd_ioctl_is_invalid(data)) { 128 CERROR("ioctl not correctly formatted\n"); 129 OBD_FREE_LARGE(*buf, hdr.ioc_len); 130 RETURN(-EINVAL); 131 } 132 133 if (data->ioc_inllen1) { 134 data->ioc_inlbuf1 = &data->ioc_bulk[0]; 135 offset += cfs_size_round(data->ioc_inllen1); 136 } 137 138 if (data->ioc_inllen2) { 139 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset; 140 offset += cfs_size_round(data->ioc_inllen2); 141 } 142 143 if (data->ioc_inllen3) { 144 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset; 145 offset += cfs_size_round(data->ioc_inllen3); 146 } 147 148 if (data->ioc_inllen4) { 149 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset; 150 } 151 152 EXIT; 153 return 0; 154} 155EXPORT_SYMBOL(obd_ioctl_getdata); 156 157int obd_ioctl_popdata(void *arg, void *data, int len) 158{ 159 int err; 160 161 err = copy_to_user(arg, data, len); 162 if (err) 163 err = -EFAULT; 164 return err; 165} 166EXPORT_SYMBOL(obd_ioctl_popdata); 167 168/* opening /dev/obd */ 169static int obd_class_open(struct inode * inode, struct file * file) 170{ 171 try_module_get(THIS_MODULE); 172 RETURN(0); 173} 174 175/* closing /dev/obd */ 176static int obd_class_release(struct inode * inode, struct file * file) 177{ 178 module_put(THIS_MODULE); 179 RETURN(0); 180} 181 182/* to control /dev/obd */ 183static long obd_class_ioctl(struct file *filp, unsigned int cmd, 184 unsigned long arg) 185{ 186 int err = 0; 187 188 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */ 189 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET)) 190 RETURN(err = -EACCES); 191 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */ 192 RETURN(err = -ENOTTY); 193 194 err = class_handle_ioctl(cmd, (unsigned long)arg); 195 196 RETURN(err); 197} 198 199/* declare character device */ 200static struct file_operations obd_psdev_fops = { 201 .owner = THIS_MODULE, 202 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */ 203 .open = obd_class_open, /* open */ 204 .release = obd_class_release, /* release */ 205}; 206 207/* modules setup */ 208struct miscdevice obd_psdev = { 209 .minor = OBD_DEV_MINOR, 210 .name = OBD_DEV_NAME, 211 .fops = &obd_psdev_fops, 212}; 213 214 215#ifdef LPROCFS 216int obd_proc_version_seq_show(struct seq_file *m, void *v) 217{ 218 return seq_printf(m, "lustre: %s\nkernel: %s\nbuild: %s\n", 219 LUSTRE_VERSION_STRING, "patchless_client", 220 BUILD_VERSION); 221} 222LPROC_SEQ_FOPS_RO(obd_proc_version); 223 224int obd_proc_pinger_seq_show(struct seq_file *m, void *v) 225{ 226 return seq_printf(m, "%s\n", "on"); 227} 228LPROC_SEQ_FOPS_RO(obd_proc_pinger); 229 230static int obd_proc_health_seq_show(struct seq_file *m, void *v) 231{ 232 int rc = 0, i; 233 234 if (libcfs_catastrophe) 235 seq_printf(m, "LBUG\n"); 236 237 read_lock(&obd_dev_lock); 238 for (i = 0; i < class_devno_max(); i++) { 239 struct obd_device *obd; 240 241 obd = class_num2obd(i); 242 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up) 243 continue; 244 245 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); 246 if (obd->obd_stopping) 247 continue; 248 249 class_incref(obd, __FUNCTION__, current); 250 read_unlock(&obd_dev_lock); 251 252 if (obd_health_check(NULL, obd)) { 253 seq_printf(m, "device %s reported unhealthy\n", 254 obd->obd_name); 255 rc++; 256 } 257 class_decref(obd, __FUNCTION__, current); 258 read_lock(&obd_dev_lock); 259 } 260 read_unlock(&obd_dev_lock); 261 262 if (rc == 0) 263 return seq_printf(m, "healthy\n"); 264 265 seq_printf(m, "NOT HEALTHY\n"); 266 return 0; 267} 268LPROC_SEQ_FOPS_RO(obd_proc_health); 269 270static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v) 271{ 272 return seq_printf(m, "%s\n", obd_jobid_var); 273} 274 275static ssize_t obd_proc_jobid_var_seq_write(struct file *file, const char *buffer, 276 size_t count, loff_t *off) 277{ 278 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN) 279 return -EINVAL; 280 281 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1); 282 /* Trim the trailing '\n' if any */ 283 memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n')); 284 return count; 285} 286LPROC_SEQ_FOPS(obd_proc_jobid_var); 287 288/* Root for /proc/fs/lustre */ 289struct proc_dir_entry *proc_lustre_root = NULL; 290EXPORT_SYMBOL(proc_lustre_root); 291 292struct lprocfs_vars lprocfs_base[] = { 293 { "version", &obd_proc_version_fops }, 294 { "pinger", &obd_proc_pinger_fops }, 295 { "health_check", &obd_proc_health_fops }, 296 { "jobid_var", &obd_proc_jobid_var_fops }, 297 { 0 } 298}; 299#else 300#define lprocfs_base NULL 301#endif /* LPROCFS */ 302 303static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos) 304{ 305 if (*pos >= class_devno_max()) 306 return NULL; 307 308 return pos; 309} 310 311static void obd_device_list_seq_stop(struct seq_file *p, void *v) 312{ 313} 314 315static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos) 316{ 317 ++*pos; 318 if (*pos >= class_devno_max()) 319 return NULL; 320 321 return pos; 322} 323 324static int obd_device_list_seq_show(struct seq_file *p, void *v) 325{ 326 loff_t index = *(loff_t *)v; 327 struct obd_device *obd = class_num2obd((int)index); 328 char *status; 329 330 if (obd == NULL) 331 return 0; 332 333 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); 334 if (obd->obd_stopping) 335 status = "ST"; 336 else if (obd->obd_inactive) 337 status = "IN"; 338 else if (obd->obd_set_up) 339 status = "UP"; 340 else if (obd->obd_attached) 341 status = "AT"; 342 else 343 status = "--"; 344 345 return seq_printf(p, "%3d %s %s %s %s %d\n", 346 (int)index, status, obd->obd_type->typ_name, 347 obd->obd_name, obd->obd_uuid.uuid, 348 atomic_read(&obd->obd_refcount)); 349} 350 351struct seq_operations obd_device_list_sops = { 352 .start = obd_device_list_seq_start, 353 .stop = obd_device_list_seq_stop, 354 .next = obd_device_list_seq_next, 355 .show = obd_device_list_seq_show, 356}; 357 358static int obd_device_list_open(struct inode *inode, struct file *file) 359{ 360 struct seq_file *seq; 361 int rc = seq_open(file, &obd_device_list_sops); 362 363 if (rc) 364 return rc; 365 366 seq = file->private_data; 367 seq->private = PDE_DATA(inode); 368 369 return 0; 370} 371 372struct file_operations obd_device_list_fops = { 373 .owner = THIS_MODULE, 374 .open = obd_device_list_open, 375 .read = seq_read, 376 .llseek = seq_lseek, 377 .release = seq_release, 378}; 379 380int class_procfs_init(void) 381{ 382 int rc = 0; 383 384 obd_sysctl_init(); 385 proc_lustre_root = lprocfs_register("fs/lustre", NULL, 386 lprocfs_base, NULL); 387 if (IS_ERR(proc_lustre_root)) { 388 rc = PTR_ERR(proc_lustre_root); 389 proc_lustre_root = NULL; 390 goto out; 391 } 392 393 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444, 394 &obd_device_list_fops, NULL); 395out: 396 if (rc) 397 CERROR("error adding /proc/fs/lustre/devices file\n"); 398 RETURN(0); 399} 400 401int class_procfs_clean(void) 402{ 403 if (proc_lustre_root) { 404 lprocfs_remove(&proc_lustre_root); 405 } 406 RETURN(0); 407} 408