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