linux-module.c revision d7e09d0397e84eefbabfd9cb353221f3c6448d83
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_read_version(char *page, char **start, off_t off, int count, 223 int *eof, void *data) 224{ 225 *eof = 1; 226 return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild: %s\n", 227 LUSTRE_VERSION_STRING, "patchless_client", 228 BUILD_VERSION); 229} 230 231int obd_proc_read_pinger(char *page, char **start, off_t off, int count, 232 int *eof, void *data) 233{ 234 *eof = 1; 235 return snprintf(page, count, "%s\n", 236 "on" 237 ); 238} 239 240/** 241 * Check all obd devices health 242 * 243 * \param page 244 * \param start 245 * \param off 246 * \param count 247 * \param eof 248 * \param data 249 * proc read function parameters, please refer to kernel 250 * code fs/proc/generic.c proc_file_read() 251 * \param data [in] unused 252 * 253 * \retval number of characters printed 254 */ 255static int obd_proc_read_health(char *page, char **start, off_t off, 256 int count, int *eof, void *data) 257{ 258 int rc = 0, i; 259 *eof = 1; 260 261 if (libcfs_catastrophe) 262 rc += snprintf(page + rc, count - rc, "LBUG\n"); 263 264 read_lock(&obd_dev_lock); 265 for (i = 0; i < class_devno_max(); i++) { 266 struct obd_device *obd; 267 268 obd = class_num2obd(i); 269 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up) 270 continue; 271 272 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); 273 if (obd->obd_stopping) 274 continue; 275 276 class_incref(obd, __FUNCTION__, current); 277 read_unlock(&obd_dev_lock); 278 279 if (obd_health_check(NULL, obd)) { 280 rc += snprintf(page + rc, count - rc, 281 "device %s reported unhealthy\n", 282 obd->obd_name); 283 } 284 class_decref(obd, __FUNCTION__, current); 285 read_lock(&obd_dev_lock); 286 } 287 read_unlock(&obd_dev_lock); 288 289 if (rc == 0) 290 return snprintf(page, count, "healthy\n"); 291 292 rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n"); 293 return rc; 294} 295 296static int obd_proc_rd_jobid_var(char *page, char **start, off_t off, 297 int count, int *eof, void *data) 298{ 299 return snprintf(page, count, "%s\n", obd_jobid_var); 300} 301 302static int obd_proc_wr_jobid_var(struct file *file, const char *buffer, 303 unsigned long count, void *data) 304{ 305 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN) 306 return -EINVAL; 307 308 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1); 309 /* Trim the trailing '\n' if any */ 310 memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n')); 311 return count; 312} 313 314/* Root for /proc/fs/lustre */ 315struct proc_dir_entry *proc_lustre_root = NULL; 316EXPORT_SYMBOL(proc_lustre_root); 317 318struct lprocfs_vars lprocfs_base[] = { 319 { "version", obd_proc_read_version, NULL, NULL }, 320 { "pinger", obd_proc_read_pinger, NULL, NULL }, 321 { "health_check", obd_proc_read_health, NULL, NULL }, 322 { "jobid_var", obd_proc_rd_jobid_var, 323 obd_proc_wr_jobid_var, NULL }, 324 { 0 } 325}; 326#else 327#define lprocfs_base NULL 328#endif /* LPROCFS */ 329 330static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos) 331{ 332 if (*pos >= class_devno_max()) 333 return NULL; 334 335 return pos; 336} 337 338static void obd_device_list_seq_stop(struct seq_file *p, void *v) 339{ 340} 341 342static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos) 343{ 344 ++*pos; 345 if (*pos >= class_devno_max()) 346 return NULL; 347 348 return pos; 349} 350 351static int obd_device_list_seq_show(struct seq_file *p, void *v) 352{ 353 loff_t index = *(loff_t *)v; 354 struct obd_device *obd = class_num2obd((int)index); 355 char *status; 356 357 if (obd == NULL) 358 return 0; 359 360 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); 361 if (obd->obd_stopping) 362 status = "ST"; 363 else if (obd->obd_inactive) 364 status = "IN"; 365 else if (obd->obd_set_up) 366 status = "UP"; 367 else if (obd->obd_attached) 368 status = "AT"; 369 else 370 status = "--"; 371 372 return seq_printf(p, "%3d %s %s %s %s %d\n", 373 (int)index, status, obd->obd_type->typ_name, 374 obd->obd_name, obd->obd_uuid.uuid, 375 atomic_read(&obd->obd_refcount)); 376} 377 378struct seq_operations obd_device_list_sops = { 379 .start = obd_device_list_seq_start, 380 .stop = obd_device_list_seq_stop, 381 .next = obd_device_list_seq_next, 382 .show = obd_device_list_seq_show, 383}; 384 385static int obd_device_list_open(struct inode *inode, struct file *file) 386{ 387 struct proc_dir_entry *dp = PDE(inode); 388 struct seq_file *seq; 389 int rc = seq_open(file, &obd_device_list_sops); 390 391 if (rc) 392 return rc; 393 394 seq = file->private_data; 395 seq->private = dp->data; 396 397 return 0; 398} 399 400struct file_operations obd_device_list_fops = { 401 .owner = THIS_MODULE, 402 .open = obd_device_list_open, 403 .read = seq_read, 404 .llseek = seq_lseek, 405 .release = seq_release, 406}; 407 408int class_procfs_init(void) 409{ 410 int rc; 411 ENTRY; 412 413 obd_sysctl_init(); 414 proc_lustre_root = lprocfs_register("fs/lustre", NULL, 415 lprocfs_base, NULL); 416 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444, 417 &obd_device_list_fops, NULL); 418 if (rc) 419 CERROR("error adding /proc/fs/lustre/devices file\n"); 420 RETURN(0); 421} 422 423int class_procfs_clean(void) 424{ 425 ENTRY; 426 if (proc_lustre_root) { 427 lprocfs_remove(&proc_lustre_root); 428 } 429 RETURN(0); 430} 431