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) 1999, 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.
33 #define DEBUG_SUBSYSTEM S_CLASS
35 #include <linux/miscdevice.h>
36 #include <linux/user_namespace.h>
37 #ifdef HAVE_UIDGID_HEADER
38 # include <linux/uidgid.h>
40 #include <linux/atomic.h>
41 #include <linux/list.h>
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <uapi/linux/lnet/lnetctl.h>
46 #include <lustre_debug.h>
47 #include <lustre_kernelcomm.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #ifdef HAVE_SERVER_SUPPORT
51 # include <dt_object.h>
52 # include <md_object.h>
53 #endif /* HAVE_SERVER_SUPPORT */
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include "llog_internal.h"
58 static __u64 obd_max_alloc;
63 static DEFINE_SPINLOCK(obd_updatemax_lock);
65 /* The following are visible and mutable through /proc/sys/lustre/. */
66 unsigned int obd_debug_peer_on_timeout;
67 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
68 unsigned int obd_dump_on_timeout;
69 EXPORT_SYMBOL(obd_dump_on_timeout);
70 unsigned int obd_dump_on_eviction;
71 EXPORT_SYMBOL(obd_dump_on_eviction);
72 unsigned long obd_max_dirty_pages;
73 EXPORT_SYMBOL(obd_max_dirty_pages);
74 atomic_long_t obd_dirty_pages;
75 EXPORT_SYMBOL(obd_dirty_pages);
76 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
77 EXPORT_SYMBOL(obd_timeout);
78 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
79 EXPORT_SYMBOL(ldlm_timeout);
80 unsigned int obd_timeout_set;
81 EXPORT_SYMBOL(obd_timeout_set);
82 unsigned int ldlm_timeout_set;
83 EXPORT_SYMBOL(ldlm_timeout_set);
84 /* bulk transfer timeout, give up after 100s by default */
85 unsigned int bulk_timeout = 100; /* seconds */
86 EXPORT_SYMBOL(bulk_timeout);
87 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
88 unsigned int at_min = 0;
89 EXPORT_SYMBOL(at_min);
90 unsigned int at_max = 600;
91 EXPORT_SYMBOL(at_max);
92 unsigned int at_history = 600;
93 EXPORT_SYMBOL(at_history);
94 int at_early_margin = 5;
95 EXPORT_SYMBOL(at_early_margin);
97 EXPORT_SYMBOL(at_extra);
99 atomic_long_t obd_dirty_transit_pages;
100 EXPORT_SYMBOL(obd_dirty_transit_pages);
102 #ifdef CONFIG_PROC_FS
103 struct lprocfs_stats *obd_memory = NULL;
104 EXPORT_SYMBOL(obd_memory);
107 static int class_resolve_dev_name(__u32 len, const char *name)
114 CERROR("No name passed,!\n");
115 GOTO(out, rc = -EINVAL);
117 if (name[len - 1] != 0) {
118 CERROR("Name not nul terminated!\n");
119 GOTO(out, rc = -EINVAL);
122 CDEBUG(D_IOCTL, "device name %s\n", name);
123 dev = class_name2dev(name);
125 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
126 GOTO(out, rc = -EINVAL);
129 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
136 #define OBD_MAX_IOCTL_BUFFER 8192
138 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
140 if (data->ioc_len > BIT(30)) {
141 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
145 if (data->ioc_inllen1 > BIT(30)) {
146 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
150 if (data->ioc_inllen2 > BIT(30)) {
151 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
155 if (data->ioc_inllen3 > BIT(30)) {
156 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
160 if (data->ioc_inllen4 > BIT(30)) {
161 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
165 if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
166 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
170 if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
171 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
175 if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
176 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
180 if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
181 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
185 if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
186 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
190 if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
191 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
195 if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
196 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
200 if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
201 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
205 if (obd_ioctl_packlen(data) > data->ioc_len) {
206 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
207 obd_ioctl_packlen(data), data->ioc_len);
214 /* buffer MUST be at least the size of obd_ioctl_hdr */
215 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
217 struct obd_ioctl_hdr hdr;
218 struct obd_ioctl_data *data;
222 if (copy_from_user(&hdr, arg, sizeof(hdr)))
225 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
226 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
227 OBD_IOCTL_VERSION, hdr.ioc_version);
231 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
232 CERROR("User buffer len %d exceeds %d max buffer\n",
233 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
237 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
238 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
242 /* When there are lots of processes calling vmalloc on multi-core
243 * system, the high lock contention will hurt performance badly,
244 * obdfilter-survey is an example, which relies on ioctl. So we'd
245 * better avoid vmalloc on ioctl path. LU-66
247 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
249 CERROR("Cannot allocate control buffer of len %d\n",
254 data = (struct obd_ioctl_data *)*buf;
256 if (copy_from_user(*buf, arg, hdr.ioc_len)) {
257 OBD_FREE_LARGE(*buf, hdr.ioc_len);
261 if (obd_ioctl_is_invalid(data)) {
262 CERROR("ioctl not correctly formatted\n");
263 OBD_FREE_LARGE(*buf, hdr.ioc_len);
267 if (data->ioc_inllen1) {
268 data->ioc_inlbuf1 = &data->ioc_bulk[0];
269 offset += cfs_size_round(data->ioc_inllen1);
272 if (data->ioc_inllen2) {
273 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
274 offset += cfs_size_round(data->ioc_inllen2);
277 if (data->ioc_inllen3) {
278 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
279 offset += cfs_size_round(data->ioc_inllen3);
282 if (data->ioc_inllen4)
283 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
287 EXPORT_SYMBOL(obd_ioctl_getdata);
289 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
292 struct obd_ioctl_data *data;
293 struct libcfs_debug_ioctl_data *debug_data;
294 struct obd_device *obd = NULL;
295 int err = 0, len = 0;
298 /* only for debugging */
299 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
300 debug_data = (struct libcfs_debug_ioctl_data*)arg;
301 libcfs_subsystem_debug = debug_data->subs;
302 libcfs_debug = debug_data->debug;
306 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
307 if (obd_ioctl_getdata(&buf, &len, (void __user *)arg)) {
308 CERROR("OBD ioctl: data error\n");
311 data = (struct obd_ioctl_data *)buf;
314 case OBD_IOC_PROCESS_CFG: {
315 struct lustre_cfg *lcfg;
317 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
318 CERROR("No config buffer passed!\n");
319 GOTO(out, err = -EINVAL);
321 OBD_ALLOC(lcfg, data->ioc_plen1);
323 GOTO(out, err = -ENOMEM);
324 err = copy_from_user(lcfg, data->ioc_pbuf1,
327 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
329 err = class_process_config(lcfg);
331 OBD_FREE(lcfg, data->ioc_plen1);
335 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
336 case OBD_GET_VERSION: {
339 if (!data->ioc_inlbuf1) {
340 CERROR("No buffer passed in ioctl\n");
341 GOTO(out, err = -EINVAL);
344 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
345 CERROR("ioctl buffer too small to hold version\n");
346 GOTO(out, err = -EINVAL);
351 CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
352 "use llapi_get_version_string() and/or relink\n",
355 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
356 strlen(LUSTRE_VERSION_STRING) + 1);
358 if (copy_to_user((void __user *)arg, data, len))
363 case OBD_IOC_NAME2DEV: {
364 /* Resolve a device name. This does not change the
365 * currently selected device.
369 dev = class_resolve_dev_name(data->ioc_inllen1,
373 GOTO(out, err = -EINVAL);
375 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
380 case OBD_IOC_UUID2DEV: {
381 /* Resolve a device uuid. This does not change the
382 * currently selected device.
385 struct obd_uuid uuid;
387 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
388 CERROR("No UUID passed!\n");
389 GOTO(out, err = -EINVAL);
391 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
392 CERROR("UUID not NUL terminated!\n");
393 GOTO(out, err = -EINVAL);
396 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
397 obd_str2uuid(&uuid, data->ioc_inlbuf1);
398 dev = class_uuid2dev(&uuid);
401 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
403 GOTO(out, err = -EINVAL);
406 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
408 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
413 case OBD_IOC_GETDEVICE: {
414 int index = data->ioc_count;
417 if (!data->ioc_inlbuf1) {
418 CERROR("No buffer passed in ioctl\n");
419 GOTO(out, err = -EINVAL);
421 if (data->ioc_inllen1 < 128) {
422 CERROR("ioctl buffer too small to hold version\n");
423 GOTO(out, err = -EINVAL);
426 obd = class_num2obd(index);
428 GOTO(out, err = -ENOENT);
430 if (obd->obd_stopping)
432 else if (obd->obd_set_up)
434 else if (obd->obd_attached)
438 str = (char *)data->ioc_bulk;
439 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
440 (int)index, status, obd->obd_type->typ_name,
441 obd->obd_name, obd->obd_uuid.uuid,
442 atomic_read(&obd->obd_refcount));
444 if (copy_to_user((void __user *)arg, data, len))
452 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
453 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
454 GOTO(out, err = -EINVAL);
455 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
456 GOTO(out, err = -EINVAL);
457 obd = class_name2obd(data->ioc_inlbuf4);
458 } else if (data->ioc_dev < class_devno_max()) {
459 obd = class_num2obd(data->ioc_dev);
461 CERROR("OBD ioctl: No device\n");
462 GOTO(out, err = -EINVAL);
466 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
467 GOTO(out, err = -EINVAL);
469 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
471 if (!obd->obd_set_up || obd->obd_stopping) {
472 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
473 GOTO(out, err = -EINVAL);
477 case OBD_IOC_NO_TRANSNO: {
478 if (!obd->obd_attached) {
479 CERROR("Device %d not attached\n", obd->obd_minor);
480 GOTO(out, err = -ENODEV);
482 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
484 obd->obd_no_transno = 1;
489 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
493 if (copy_to_user((void __user *)arg, data, len))
500 OBD_FREE_LARGE(buf, len);
502 } /* class_handle_ioctl */
504 /* opening /dev/obd */
505 static int obd_class_open(struct inode * inode, struct file * file)
508 try_module_get(THIS_MODULE);
512 /* closing /dev/obd */
513 static int obd_class_release(struct inode * inode, struct file * file)
517 module_put(THIS_MODULE);
521 /* to control /dev/obd */
522 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
528 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
529 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
530 RETURN(err = -EACCES);
532 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
533 RETURN(err = -ENOTTY);
535 err = class_handle_ioctl(cmd, (unsigned long)arg);
540 /* declare character device */
541 static struct file_operations obd_psdev_fops = {
542 .owner = THIS_MODULE,
543 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
544 .open = obd_class_open, /* open */
545 .release = obd_class_release, /* release */
549 struct miscdevice obd_psdev = {
550 .minor = MISC_DYNAMIC_MINOR,
551 .name = OBD_DEV_NAME,
552 .fops = &obd_psdev_fops,
555 static int obd_init_checks(void)
557 __u64 u64val, div64val;
561 CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
563 u64val = OBD_OBJECT_EOF;
564 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
565 if (u64val != OBD_OBJECT_EOF) {
566 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
567 u64val, (int)sizeof(u64val));
570 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
572 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
576 div64val = OBD_OBJECT_EOF;
577 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
578 if (u64val != OBD_OBJECT_EOF) {
579 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
580 u64val, (int)sizeof(u64val));
583 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
584 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
585 u64val, (int)sizeof(u64val));
588 if (do_div(div64val, 256) != (u64val & 255)) {
589 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
592 if (u64val >> 8 != div64val) {
593 CERROR("do_div(%#llx,256) %llu != %llu\n",
594 u64val, div64val, u64val >> 8);
597 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
599 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
602 len = snprintf(buf, sizeof(buf), "%llu", u64val);
604 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
607 len = snprintf(buf, sizeof(buf), "%lld", u64val);
609 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
612 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
613 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
621 static int __init obdclass_init(void)
625 LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
629 err = obd_init_checks();
630 if (err == -EOVERFLOW)
633 #ifdef CONFIG_PROC_FS
634 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
635 LPROCFS_STATS_FLAG_NONE |
636 LPROCFS_STATS_FLAG_IRQ_SAFE);
637 if (obd_memory == NULL) {
638 CERROR("kmalloc of 'obd_memory' failed\n");
642 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
643 LPROCFS_CNTR_AVGMINMAX,
646 err = obd_zombie_impexp_init();
648 goto cleanup_obd_memory;
650 err = class_handle_init();
652 goto cleanup_zombie_impexp;
654 err = misc_register(&obd_psdev);
656 CERROR("cannot register OBD miscdevice: err = %d\n", err);
657 goto cleanup_class_handle;
660 /* Default the dirty page cache cap to 1/2 of system memory.
661 * For clients with less memory, a larger fraction is needed
662 * for other purposes (mostly for BGL). */
663 if (totalram_pages <= 512 << (20 - PAGE_SHIFT))
664 obd_max_dirty_pages = totalram_pages / 4;
666 obd_max_dirty_pages = totalram_pages / 2;
668 err = obd_init_caches();
670 goto cleanup_deregister;
672 err = class_procfs_init();
676 err = lu_global_init();
678 goto cleanup_class_procfs;
680 err = cl_global_init();
682 goto cleanup_lu_global;
684 #ifdef HAVE_SERVER_SUPPORT
685 err = dt_global_init();
687 goto cleanup_cl_global;
689 err = lu_ucred_global_init();
691 goto cleanup_dt_global;
692 #endif /* HAVE_SERVER_SUPPORT */
694 err = llog_info_init();
696 #ifdef HAVE_SERVER_SUPPORT
697 goto cleanup_lu_ucred_global;
698 #else /* !HAVE_SERVER_SUPPORT */
699 goto cleanup_cl_global;
700 #endif /* HAVE_SERVER_SUPPORT */
702 err = lustre_register_fs();
704 /* simulate a late OOM situation now to require all
705 * alloc'ed/initialized resources to be freed */
706 if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
707 /* fake error but filesystem has been registered */
708 lustre_unregister_fs();
709 /* force error to ensure module will be unloaded/cleaned */
714 goto cleanup_llog_info;
721 #ifdef HAVE_SERVER_SUPPORT
722 cleanup_lu_ucred_global:
723 lu_ucred_global_fini();
727 #endif /* HAVE_SERVER_SUPPORT */
735 cleanup_class_procfs:
736 class_procfs_clean();
739 obd_cleanup_caches();
742 misc_deregister(&obd_psdev);
744 cleanup_class_handle:
745 class_handle_cleanup();
747 cleanup_zombie_impexp:
748 obd_zombie_impexp_stop();
751 #ifdef CONFIG_PROC_FS
752 lprocfs_free_stats(&obd_memory);
758 void obd_update_maxusage(void)
762 max = obd_memory_sum();
764 spin_lock(&obd_updatemax_lock);
765 if (max > obd_max_alloc)
767 spin_unlock(&obd_updatemax_lock);
769 EXPORT_SYMBOL(obd_update_maxusage);
771 #ifdef CONFIG_PROC_FS
772 __u64 obd_memory_max(void)
776 obd_update_maxusage();
777 spin_lock(&obd_updatemax_lock);
779 spin_unlock(&obd_updatemax_lock);
783 #endif /* CONFIG_PROC_FS */
785 static void __exit obdclass_exit(void)
787 #ifdef CONFIG_PROC_FS
790 #endif /* CONFIG_PROC_FS */
793 lustre_unregister_fs();
795 misc_deregister(&obd_psdev);
797 #ifdef HAVE_SERVER_SUPPORT
798 lu_ucred_global_fini();
800 #endif /* HAVE_SERVER_SUPPORT */
804 obd_cleanup_caches();
806 class_procfs_clean();
808 class_handle_cleanup();
809 class_del_uuid(NULL); /* Delete all UUIDs. */
810 obd_zombie_impexp_stop();
812 #ifdef CONFIG_PROC_FS
813 memory_leaked = obd_memory_sum();
814 memory_max = obd_memory_max();
816 lprocfs_free_stats(&obd_memory);
817 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
818 "obd_memory max: %llu, leaked: %llu\n",
819 memory_max, memory_leaked);
820 #endif /* CONFIG_PROC_FS */
825 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
826 MODULE_DESCRIPTION("Lustre Class Driver");
827 MODULE_VERSION(LUSTRE_VERSION_STRING);
828 MODULE_LICENSE("GPL");
830 module_init(obdclass_init);
831 module_exit(obdclass_exit);