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, 2017, 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/sched.h>
46 #include <linux/slab.h>
47 #include <linux/ioport.h>
48 #include <linux/fcntl.h>
49 #include <linux/delay.h>
50 #include <linux/skbuff.h>
51 #include <linux/proc_fs.h>
53 #include <linux/poll.h>
54 #include <linux/init.h>
55 #include <linux/list.h>
56 #include <linux/highmem.h>
58 #include <asm/ioctls.h>
60 #include <asm/uaccess.h>
61 #include <linux/miscdevice.h>
62 #include <linux/seq_file.h>
63 #include <linux/kobject.h>
65 #include <libcfs/libcfs.h>
66 #include <obd_support.h>
67 #include <obd_class.h>
68 #include <lprocfs_status.h>
69 #include <uapi/linux/lnet/lnetctl.h>
70 #include <uapi/linux/lustre/lustre_ioctl.h>
71 #include <uapi/linux/lustre/lustre_ver.h>
73 #define OBD_MAX_IOCTL_BUFFER 8192
75 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
77 if (data->ioc_len > BIT(30)) {
78 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
82 if (data->ioc_inllen1 > BIT(30)) {
83 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
87 if (data->ioc_inllen2 > BIT(30)) {
88 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
92 if (data->ioc_inllen3 > BIT(30)) {
93 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
97 if (data->ioc_inllen4 > BIT(30)) {
98 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
102 if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
103 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
107 if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
108 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
112 if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
113 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
117 if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
118 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
122 if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
123 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
127 if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
128 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
132 if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
133 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
137 if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
138 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
142 if (obd_ioctl_packlen(data) > data->ioc_len) {
143 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
144 obd_ioctl_packlen(data), data->ioc_len);
151 /* buffer MUST be at least the size of obd_ioctl_hdr */
152 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
154 struct obd_ioctl_hdr hdr;
155 struct obd_ioctl_data *data;
159 if (copy_from_user(&hdr, arg, sizeof(hdr)))
162 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
163 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
164 OBD_IOCTL_VERSION, hdr.ioc_version);
168 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
169 CERROR("User buffer len %d exceeds %d max buffer\n",
170 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
174 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
175 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
179 /* When there are lots of processes calling vmalloc on multi-core
180 * system, the high lock contention will hurt performance badly,
181 * obdfilter-survey is an example, which relies on ioctl. So we'd
182 * better avoid vmalloc on ioctl path. LU-66 */
183 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
185 CERROR("Cannot allocate control buffer of len %d\n",
190 data = (struct obd_ioctl_data *)*buf;
192 if (copy_from_user(*buf, arg, hdr.ioc_len)) {
193 OBD_FREE_LARGE(*buf, hdr.ioc_len);
197 if (obd_ioctl_is_invalid(data)) {
198 CERROR("ioctl not correctly formatted\n");
199 OBD_FREE_LARGE(*buf, hdr.ioc_len);
203 if (data->ioc_inllen1) {
204 data->ioc_inlbuf1 = &data->ioc_bulk[0];
205 offset += cfs_size_round(data->ioc_inllen1);
208 if (data->ioc_inllen2) {
209 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
210 offset += cfs_size_round(data->ioc_inllen2);
213 if (data->ioc_inllen3) {
214 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
215 offset += cfs_size_round(data->ioc_inllen3);
218 if (data->ioc_inllen4)
219 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
223 EXPORT_SYMBOL(obd_ioctl_getdata);
225 /* opening /dev/obd */
226 static int obd_class_open(struct inode * inode, struct file * file)
230 try_module_get(THIS_MODULE);
234 /* closing /dev/obd */
235 static int obd_class_release(struct inode * inode, struct file * file)
239 module_put(THIS_MODULE);
243 /* to control /dev/obd */
244 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
250 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
251 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
252 RETURN(err = -EACCES);
253 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
254 RETURN(err = -ENOTTY);
256 err = class_handle_ioctl(cmd, (unsigned long)arg);
261 /* declare character device */
262 static struct file_operations obd_psdev_fops = {
263 .owner = THIS_MODULE,
264 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
265 .open = obd_class_open, /* open */
266 .release = obd_class_release, /* release */
270 struct miscdevice obd_psdev = {
271 .minor = MISC_DYNAMIC_MINOR,
272 .name = OBD_DEV_NAME,
273 .fops = &obd_psdev_fops,
276 struct static_lustre_uintvalue_attr {
278 struct attribute attr;
279 ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
281 ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
282 const char *buf, size_t len);
287 static ssize_t static_uintvalue_show(struct kobject *kobj,
288 struct attribute *attr,
291 struct static_lustre_uintvalue_attr *lattr = (void *)attr;
293 return sprintf(buf, "%d\n", *lattr->value);
296 static ssize_t static_uintvalue_store(struct kobject *kobj,
297 struct attribute *attr,
298 const char *buffer, size_t count)
300 struct static_lustre_uintvalue_attr *lattr = (void *)attr;
304 rc = kstrtouint(buffer, 10, &val);
313 #define LUSTRE_STATIC_UINT_ATTR(name, value) \
314 static struct static_lustre_uintvalue_attr lustre_sattr_##name = \
315 { __ATTR(name, 0644, static_uintvalue_show, \
316 static_uintvalue_store), value }
318 LUSTRE_STATIC_UINT_ATTR(timeout, &obd_timeout);
319 LUSTRE_STATIC_UINT_ATTR(debug_peer_on_timeout, &obd_debug_peer_on_timeout);
320 LUSTRE_STATIC_UINT_ATTR(dump_on_timeout, &obd_dump_on_timeout);
321 LUSTRE_STATIC_UINT_ATTR(dump_on_eviction, &obd_dump_on_eviction);
322 LUSTRE_STATIC_UINT_ATTR(at_min, &at_min);
323 LUSTRE_STATIC_UINT_ATTR(at_max, &at_max);
324 LUSTRE_STATIC_UINT_ATTR(at_extra, &at_extra);
325 LUSTRE_STATIC_UINT_ATTR(at_early_margin, &at_early_margin);
326 LUSTRE_STATIC_UINT_ATTR(at_history, &at_history);
328 #ifdef HAVE_SERVER_SUPPORT
329 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
330 LUSTRE_STATIC_UINT_ATTR(bulk_timeout, &bulk_timeout);
333 static ssize_t memused_show(struct kobject *kobj, struct attribute *attr,
336 return sprintf(buf, "%llu\n", obd_memory_sum());
338 LUSTRE_RO_ATTR(memused);
340 static ssize_t memused_max_show(struct kobject *kobj, struct attribute *attr,
343 return sprintf(buf, "%llu\n", obd_memory_max());
345 LUSTRE_RO_ATTR(memused_max);
347 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
350 return sprintf(buf, "%lu\n",
351 obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
354 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
355 const char *buffer, size_t count)
360 rc = kstrtoul(buffer, 10, &val);
364 val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
366 if (val > ((totalram_pages / 10) * 9)) {
367 /* Somebody wants to assign too much memory to dirty pages */
371 if (val < 4 << (20 - PAGE_SHIFT)) {
372 /* Less than 4 Mb for dirty cache is also bad */
376 obd_max_dirty_pages = val;
380 LUSTRE_RW_ATTR(max_dirty_mb);
382 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
385 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
388 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
392 const char *state = "on";
394 const char *state = "off";
396 return sprintf(buf, "%s\n", state);
400 * Check all obd devices health
405 * \retval number of characters printed if healthy
408 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
414 if (libcfs_catastrophe) {
415 len = sprintf(buf, "LBUG\n");
419 read_lock(&obd_dev_lock);
420 for (i = 0; i < class_devno_max(); i++) {
421 struct obd_device *obd;
423 obd = class_num2obd(i);
424 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
427 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
428 if (obd->obd_stopping)
431 class_incref(obd, __FUNCTION__, current);
432 read_unlock(&obd_dev_lock);
434 if (obd_health_check(NULL, obd)) {
435 len = sprintf(buf, "device %s reported unhealthy\n",
439 class_decref(obd, __FUNCTION__, current);
440 read_lock(&obd_dev_lock);
442 read_unlock(&obd_dev_lock);
445 len = sprintf(buf, "healthy\n");
447 len = sprintf(buf, "NOT HEALTHY\n");
452 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
457 if (strlen(obd_jobid_var))
458 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
462 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
463 const char *buffer, size_t count)
465 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
468 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
470 memcpy(obd_jobid_var, buffer, count);
472 /* Trim the trailing '\n' if any */
473 if (obd_jobid_var[count - 1] == '\n')
474 obd_jobid_var[count - 1] = 0;
479 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
484 if (strlen(obd_jobid_name))
485 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
489 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
490 const char *buffer, size_t count)
492 if (!count || count > LUSTRE_JOBID_SIZE)
495 if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
496 !strchr(buffer, '%')) {
497 lustre_jobid_clear(buffer);
501 /* clear previous value */
502 memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
504 memcpy(obd_jobid_name, buffer, count);
506 /* Trim the trailing '\n' if any */
507 if (obd_jobid_name[count - 1] == '\n') {
508 /* Don't echo just a newline */
511 obd_jobid_name[count - 1] = 0;
517 /* Root for /sys/kernel/debug/lustre */
518 struct dentry *debugfs_lustre_root;
519 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
521 #ifdef CONFIG_PROC_FS
522 /* Root for /proc/fs/lustre */
523 struct proc_dir_entry *proc_lustre_root = NULL;
524 EXPORT_SYMBOL(proc_lustre_root);
526 #define lprocfs_base NULL
527 #endif /* CONFIG_PROC_FS */
529 LUSTRE_RO_ATTR(version);
530 LUSTRE_RO_ATTR(pinger);
531 LUSTRE_RO_ATTR(health_check);
532 LUSTRE_RW_ATTR(jobid_var);
533 LUSTRE_RW_ATTR(jobid_name);
535 static struct attribute *lustre_attrs[] = {
536 &lustre_attr_version.attr,
537 &lustre_attr_pinger.attr,
538 &lustre_attr_health_check.attr,
539 &lustre_attr_jobid_name.attr,
540 &lustre_attr_jobid_var.attr,
541 &lustre_sattr_timeout.u.attr,
542 &lustre_attr_max_dirty_mb.attr,
543 &lustre_sattr_debug_peer_on_timeout.u.attr,
544 &lustre_sattr_dump_on_timeout.u.attr,
545 &lustre_sattr_dump_on_eviction.u.attr,
546 &lustre_sattr_at_min.u.attr,
547 &lustre_sattr_at_max.u.attr,
548 &lustre_sattr_at_extra.u.attr,
549 &lustre_sattr_at_early_margin.u.attr,
550 &lustre_sattr_at_history.u.attr,
551 &lustre_attr_memused_max.attr,
552 &lustre_attr_memused.attr,
553 #ifdef HAVE_SERVER_SUPPORT
554 &lustre_sattr_ldlm_timeout.u.attr,
555 &lustre_sattr_bulk_timeout.u.attr,
560 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
562 if (*pos >= class_devno_max())
568 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
572 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
575 if (*pos >= class_devno_max())
581 static int obd_device_list_seq_show(struct seq_file *p, void *v)
583 loff_t index = *(loff_t *)v;
584 struct obd_device *obd = class_num2obd((int)index);
590 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
591 if (obd->obd_stopping)
593 else if (obd->obd_inactive)
595 else if (obd->obd_set_up)
597 else if (obd->obd_attached)
602 seq_printf(p, "%3d %s %s %s %s %d\n",
603 (int)index, status, obd->obd_type->typ_name,
604 obd->obd_name, obd->obd_uuid.uuid,
605 atomic_read(&obd->obd_refcount));
609 static const struct seq_operations obd_device_list_sops = {
610 .start = obd_device_list_seq_start,
611 .stop = obd_device_list_seq_stop,
612 .next = obd_device_list_seq_next,
613 .show = obd_device_list_seq_show,
616 static int obd_device_list_open(struct inode *inode, struct file *file)
618 struct seq_file *seq;
619 int rc = seq_open(file, &obd_device_list_sops);
624 seq = file->private_data;
625 seq->private = inode->i_private;
629 static const struct file_operations obd_device_list_fops = {
630 .owner = THIS_MODULE,
631 .open = obd_device_list_open,
634 .release = seq_release,
637 struct kset *lustre_kset;
638 EXPORT_SYMBOL_GPL(lustre_kset);
640 static struct attribute_group lustre_attr_group = {
641 .attrs = lustre_attrs,
644 ssize_t class_set_global(const char *param)
646 const char *value = strchr(param, '=') + 1;
647 size_t off = value - param - 1;
648 ssize_t count = -ENOENT;
651 for (i = 0; lustre_attrs[i]; i++) {
652 if (!strncmp(lustre_attrs[i]->name, param, off)) {
653 count = lustre_attr_store(&lustre_kset->kobj,
654 lustre_attrs[i], value,
662 int class_procfs_init(void)
664 struct proc_dir_entry *entry;
669 lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
673 /* Create the files associated with this kobject */
674 rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
676 kset_unregister(lustre_kset);
680 rc = jobid_cache_init();
682 kset_unregister(lustre_kset);
686 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
687 if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
688 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
690 debugfs_lustre_root = NULL;
691 kset_unregister(lustre_kset);
695 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
696 &obd_device_list_fops);
697 if (IS_ERR_OR_NULL(file)) {
698 rc = file ? PTR_ERR(file) : -ENOMEM;
699 debugfs_remove(debugfs_lustre_root);
700 kset_unregister(lustre_kset);
704 entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
707 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
708 debugfs_remove_recursive(debugfs_lustre_root);
709 kset_unregister(lustre_kset);
713 proc_lustre_root = entry;
718 int class_procfs_clean(void)
722 debugfs_remove_recursive(debugfs_lustre_root);
724 debugfs_lustre_root = NULL;
727 if (proc_lustre_root)
728 lprocfs_remove(&proc_lustre_root);
730 sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
732 kset_unregister(lustre_kset);