4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/linux/linux-module.c
34 * Object Devices Class Driver
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
39 #define DEBUG_SUBSYSTEM S_CLASS
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/major.h>
45 #include <linux/sched.h>
47 #include <linux/slab.h>
48 #include <linux/ioport.h>
49 #include <linux/fcntl.h>
50 #include <linux/delay.h>
51 #include <linux/skbuff.h>
52 #include <linux/proc_fs.h>
54 #include <linux/poll.h>
55 #include <linux/init.h>
56 #include <linux/list.h>
57 #include <linux/highmem.h>
59 #include <asm/ioctls.h>
61 #include <asm/uaccess.h>
62 #include <linux/miscdevice.h>
63 #include <linux/seq_file.h>
64 #include <linux/kobject.h>
66 #include <libcfs/libcfs.h>
67 #include <obd_support.h>
68 #include <obd_class.h>
69 #include <lprocfs_status.h>
70 #include <uapi/linux/lnet/lnetctl.h>
71 #include <uapi/linux/lustre/lustre_ioctl.h>
72 #include <uapi/linux/lustre/lustre_ver.h>
74 #define OBD_MAX_IOCTL_BUFFER 8192
76 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
78 if (data->ioc_len > BIT(30)) {
79 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
83 if (data->ioc_inllen1 > BIT(30)) {
84 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
88 if (data->ioc_inllen2 > BIT(30)) {
89 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
93 if (data->ioc_inllen3 > BIT(30)) {
94 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
98 if (data->ioc_inllen4 > BIT(30)) {
99 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
103 if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
104 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
108 if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
109 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
113 if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
114 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
118 if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
119 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
123 if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
124 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
128 if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
129 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
133 if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
134 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
138 if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
139 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
143 if (obd_ioctl_packlen(data) > data->ioc_len) {
144 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
145 obd_ioctl_packlen(data), data->ioc_len);
152 /* buffer MUST be at least the size of obd_ioctl_hdr */
153 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
155 struct obd_ioctl_hdr hdr;
156 struct obd_ioctl_data *data;
160 if (copy_from_user(&hdr, arg, sizeof(hdr)))
163 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
164 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
165 OBD_IOCTL_VERSION, hdr.ioc_version);
169 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
170 CERROR("User buffer len %d exceeds %d max buffer\n",
171 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
175 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
176 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
180 /* When there are lots of processes calling vmalloc on multi-core
181 * system, the high lock contention will hurt performance badly,
182 * obdfilter-survey is an example, which relies on ioctl. So we'd
183 * better avoid vmalloc on ioctl path. LU-66 */
184 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
186 CERROR("Cannot allocate control buffer of len %d\n",
191 data = (struct obd_ioctl_data *)*buf;
193 if (copy_from_user(*buf, arg, hdr.ioc_len)) {
194 OBD_FREE_LARGE(*buf, hdr.ioc_len);
198 if (obd_ioctl_is_invalid(data)) {
199 CERROR("ioctl not correctly formatted\n");
200 OBD_FREE_LARGE(*buf, hdr.ioc_len);
204 if (data->ioc_inllen1) {
205 data->ioc_inlbuf1 = &data->ioc_bulk[0];
206 offset += cfs_size_round(data->ioc_inllen1);
209 if (data->ioc_inllen2) {
210 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
211 offset += cfs_size_round(data->ioc_inllen2);
214 if (data->ioc_inllen3) {
215 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
216 offset += cfs_size_round(data->ioc_inllen3);
219 if (data->ioc_inllen4)
220 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
224 EXPORT_SYMBOL(obd_ioctl_getdata);
226 /* opening /dev/obd */
227 static int obd_class_open(struct inode * inode, struct file * file)
231 try_module_get(THIS_MODULE);
235 /* closing /dev/obd */
236 static int obd_class_release(struct inode * inode, struct file * file)
240 module_put(THIS_MODULE);
244 /* to control /dev/obd */
245 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
251 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
252 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
253 RETURN(err = -EACCES);
254 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
255 RETURN(err = -ENOTTY);
257 err = class_handle_ioctl(cmd, (unsigned long)arg);
262 /* declare character device */
263 static struct file_operations obd_psdev_fops = {
264 .owner = THIS_MODULE,
265 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
266 .open = obd_class_open, /* open */
267 .release = obd_class_release, /* release */
271 struct miscdevice obd_psdev = {
272 .minor = OBD_DEV_MINOR,
273 .name = OBD_DEV_NAME,
274 .fops = &obd_psdev_fops,
277 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
280 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
283 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
287 const char *state = "on";
289 const char *state = "off";
291 return sprintf(buf, "%s\n", state);
295 * Check all obd devices health
300 * \retval number of characters printed if healthy
303 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
309 if (libcfs_catastrophe) {
310 len = sprintf(buf, "LBUG\n");
314 read_lock(&obd_dev_lock);
315 for (i = 0; i < class_devno_max(); i++) {
316 struct obd_device *obd;
318 obd = class_num2obd(i);
319 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
322 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
323 if (obd->obd_stopping)
326 class_incref(obd, __FUNCTION__, current);
327 read_unlock(&obd_dev_lock);
329 if (obd_health_check(NULL, obd)) {
330 len = sprintf(buf, "device %s reported unhealthy\n",
334 class_decref(obd, __FUNCTION__, current);
335 read_lock(&obd_dev_lock);
337 read_unlock(&obd_dev_lock);
340 len = sprintf(buf, "healthy\n");
342 len = sprintf(buf, "NOT HEALTHY\n");
347 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
352 if (strlen(obd_jobid_var))
353 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
357 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
358 const char *buffer, size_t count)
360 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
363 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
365 memcpy(obd_jobid_var, buffer, count);
367 /* Trim the trailing '\n' if any */
368 if (obd_jobid_var[count - 1] == '\n')
369 obd_jobid_var[count - 1] = 0;
374 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
379 if (strlen(obd_jobid_node))
380 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_node);
384 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
385 const char *buffer, size_t count)
387 if (!count || count > LUSTRE_JOBID_SIZE)
390 /* clear previous value */
391 memset(obd_jobid_node, 0, LUSTRE_JOBID_SIZE);
393 memcpy(obd_jobid_node, buffer, count);
395 /* Trim the trailing '\n' if any */
396 if (obd_jobid_node[count - 1] == '\n') {
397 /* Don't echo just a newline */
400 obd_jobid_node[count - 1] = 0;
406 /* Root for /sys/kernel/debug/lustre */
407 struct dentry *debugfs_lustre_root;
408 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
410 #ifdef CONFIG_PROC_FS
411 /* Root for /proc/fs/lustre */
412 struct proc_dir_entry *proc_lustre_root = NULL;
413 EXPORT_SYMBOL(proc_lustre_root);
415 #define lprocfs_base NULL
416 #endif /* CONFIG_PROC_FS */
418 LUSTRE_RO_ATTR(version);
419 LUSTRE_RO_ATTR(pinger);
420 LUSTRE_RO_ATTR(health_check);
421 LUSTRE_RW_ATTR(jobid_var);
422 LUSTRE_RW_ATTR(jobid_name);
424 static struct attribute *lustre_attrs[] = {
425 &lustre_attr_version.attr,
426 &lustre_attr_pinger.attr,
427 &lustre_attr_health_check.attr,
428 &lustre_attr_jobid_name.attr,
429 &lustre_attr_jobid_var.attr,
433 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
435 if (*pos >= class_devno_max())
441 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
445 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
448 if (*pos >= class_devno_max())
454 static int obd_device_list_seq_show(struct seq_file *p, void *v)
456 loff_t index = *(loff_t *)v;
457 struct obd_device *obd = class_num2obd((int)index);
463 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
464 if (obd->obd_stopping)
466 else if (obd->obd_inactive)
468 else if (obd->obd_set_up)
470 else if (obd->obd_attached)
475 seq_printf(p, "%3d %s %s %s %s %d\n",
476 (int)index, status, obd->obd_type->typ_name,
477 obd->obd_name, obd->obd_uuid.uuid,
478 atomic_read(&obd->obd_refcount));
482 static const struct seq_operations obd_device_list_sops = {
483 .start = obd_device_list_seq_start,
484 .stop = obd_device_list_seq_stop,
485 .next = obd_device_list_seq_next,
486 .show = obd_device_list_seq_show,
489 static int obd_device_list_open(struct inode *inode, struct file *file)
491 struct seq_file *seq;
492 int rc = seq_open(file, &obd_device_list_sops);
497 seq = file->private_data;
498 seq->private = inode->i_private;
502 static const struct file_operations obd_device_list_fops = {
503 .owner = THIS_MODULE,
504 .open = obd_device_list_open,
507 .release = seq_release,
510 struct kobject *lustre_kobj;
511 EXPORT_SYMBOL_GPL(lustre_kobj);
513 static struct attribute_group lustre_attr_group = {
514 .attrs = lustre_attrs,
517 int class_procfs_init(void)
519 struct proc_dir_entry *entry;
524 lustre_kobj = kobject_create_and_add("lustre", fs_kobj);
525 if (lustre_kobj == NULL)
528 /* Create the files associated with this kobject */
529 rc = sysfs_create_group(lustre_kobj, &lustre_attr_group);
531 kobject_put(lustre_kobj);
535 rc = obd_sysctl_init();
537 kobject_put(lustre_kobj);
541 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
542 if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
543 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
545 debugfs_lustre_root = NULL;
546 kobject_put(lustre_kobj);
550 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
551 &obd_device_list_fops);
552 if (IS_ERR_OR_NULL(file)) {
553 rc = file ? PTR_ERR(file) : -ENOMEM;
554 kobject_put(lustre_kobj);
558 entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
561 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
562 kobject_put(lustre_kobj);
566 proc_lustre_root = entry;
571 int class_procfs_clean(void)
575 debugfs_remove_recursive(debugfs_lustre_root);
577 debugfs_lustre_root = NULL;
579 if (proc_lustre_root)
580 lprocfs_remove(&proc_lustre_root);
582 kobject_put(lustre_kobj);