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 int obd_lbug_on_eviction;
73 EXPORT_SYMBOL(obd_lbug_on_eviction);
74 unsigned long obd_max_dirty_pages;
75 EXPORT_SYMBOL(obd_max_dirty_pages);
76 atomic_long_t obd_dirty_pages;
77 EXPORT_SYMBOL(obd_dirty_pages);
78 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
79 EXPORT_SYMBOL(obd_timeout);
80 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
81 EXPORT_SYMBOL(ldlm_timeout);
82 unsigned int obd_timeout_set;
83 EXPORT_SYMBOL(obd_timeout_set);
84 unsigned int ldlm_timeout_set;
85 EXPORT_SYMBOL(ldlm_timeout_set);
86 /* bulk transfer timeout, give up after 100s by default */
87 unsigned int bulk_timeout = 100; /* seconds */
88 EXPORT_SYMBOL(bulk_timeout);
89 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
90 unsigned int at_min = 0;
91 EXPORT_SYMBOL(at_min);
92 unsigned int at_max = 600;
93 EXPORT_SYMBOL(at_max);
94 unsigned int at_history = 600;
95 EXPORT_SYMBOL(at_history);
96 int at_early_margin = 5;
97 EXPORT_SYMBOL(at_early_margin);
99 EXPORT_SYMBOL(at_extra);
101 atomic_long_t obd_dirty_transit_pages;
102 EXPORT_SYMBOL(obd_dirty_transit_pages);
104 #ifdef CONFIG_PROC_FS
105 struct lprocfs_stats *obd_memory = NULL;
106 EXPORT_SYMBOL(obd_memory);
109 static int class_resolve_dev_name(__u32 len, const char *name)
116 CERROR("No name passed,!\n");
117 GOTO(out, rc = -EINVAL);
119 if (name[len - 1] != 0) {
120 CERROR("Name not nul terminated!\n");
121 GOTO(out, rc = -EINVAL);
124 CDEBUG(D_IOCTL, "device name %s\n", name);
125 dev = class_name2dev(name);
127 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
128 GOTO(out, rc = -EINVAL);
131 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
138 #define OBD_MAX_IOCTL_BUFFER 8192
140 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
142 if (data->ioc_len > BIT(30)) {
143 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
147 if (data->ioc_inllen1 > BIT(30)) {
148 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
152 if (data->ioc_inllen2 > BIT(30)) {
153 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
157 if (data->ioc_inllen3 > BIT(30)) {
158 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
162 if (data->ioc_inllen4 > BIT(30)) {
163 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
167 if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
168 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
172 if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
173 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
177 if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
178 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
182 if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
183 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
187 if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
188 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
192 if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
193 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
197 if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
198 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
202 if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
203 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
207 if (obd_ioctl_packlen(data) > data->ioc_len) {
208 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
209 obd_ioctl_packlen(data), data->ioc_len);
216 /* buffer MUST be at least the size of obd_ioctl_hdr */
217 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
219 struct obd_ioctl_hdr hdr;
220 struct obd_ioctl_data *data;
224 if (copy_from_user(&hdr, arg, sizeof(hdr)))
227 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
228 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
229 OBD_IOCTL_VERSION, hdr.ioc_version);
233 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
234 CERROR("User buffer len %d exceeds %d max buffer\n",
235 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
239 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
240 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
244 /* When there are lots of processes calling vmalloc on multi-core
245 * system, the high lock contention will hurt performance badly,
246 * obdfilter-survey is an example, which relies on ioctl. So we'd
247 * better avoid vmalloc on ioctl path. LU-66
249 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
251 CERROR("Cannot allocate control buffer of len %d\n",
256 data = (struct obd_ioctl_data *)*buf;
258 if (copy_from_user(*buf, arg, hdr.ioc_len)) {
259 OBD_FREE_LARGE(*buf, hdr.ioc_len);
263 if (obd_ioctl_is_invalid(data)) {
264 CERROR("ioctl not correctly formatted\n");
265 OBD_FREE_LARGE(*buf, hdr.ioc_len);
269 if (data->ioc_inllen1) {
270 data->ioc_inlbuf1 = &data->ioc_bulk[0];
271 offset += cfs_size_round(data->ioc_inllen1);
274 if (data->ioc_inllen2) {
275 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
276 offset += cfs_size_round(data->ioc_inllen2);
279 if (data->ioc_inllen3) {
280 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
281 offset += cfs_size_round(data->ioc_inllen3);
284 if (data->ioc_inllen4)
285 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
289 EXPORT_SYMBOL(obd_ioctl_getdata);
291 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
294 struct obd_ioctl_data *data;
295 struct libcfs_debug_ioctl_data *debug_data;
296 struct obd_device *obd = NULL;
297 int err = 0, len = 0;
300 /* only for debugging */
301 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
302 debug_data = (struct libcfs_debug_ioctl_data*)arg;
303 libcfs_subsystem_debug = debug_data->subs;
304 libcfs_debug = debug_data->debug;
308 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
309 if (obd_ioctl_getdata(&buf, &len, (void __user *)arg)) {
310 CERROR("OBD ioctl: data error\n");
313 data = (struct obd_ioctl_data *)buf;
316 case OBD_IOC_PROCESS_CFG: {
317 struct lustre_cfg *lcfg;
319 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
320 CERROR("No config buffer passed!\n");
321 GOTO(out, err = -EINVAL);
323 OBD_ALLOC(lcfg, data->ioc_plen1);
325 GOTO(out, err = -ENOMEM);
326 err = copy_from_user(lcfg, data->ioc_pbuf1,
329 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
331 err = class_process_config(lcfg);
333 OBD_FREE(lcfg, data->ioc_plen1);
337 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
338 case OBD_GET_VERSION: {
341 if (!data->ioc_inlbuf1) {
342 CERROR("No buffer passed in ioctl\n");
343 GOTO(out, err = -EINVAL);
346 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
347 CERROR("ioctl buffer too small to hold version\n");
348 GOTO(out, err = -EINVAL);
353 CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
354 "use llapi_get_version_string() and/or relink\n",
357 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
358 strlen(LUSTRE_VERSION_STRING) + 1);
360 if (copy_to_user((void __user *)arg, data, len))
365 case OBD_IOC_NAME2DEV: {
366 /* Resolve a device name. This does not change the
367 * currently selected device.
371 dev = class_resolve_dev_name(data->ioc_inllen1,
375 GOTO(out, err = -EINVAL);
377 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
382 case OBD_IOC_UUID2DEV: {
383 /* Resolve a device uuid. This does not change the
384 * currently selected device.
387 struct obd_uuid uuid;
389 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
390 CERROR("No UUID passed!\n");
391 GOTO(out, err = -EINVAL);
393 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
394 CERROR("UUID not NUL terminated!\n");
395 GOTO(out, err = -EINVAL);
398 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
399 obd_str2uuid(&uuid, data->ioc_inlbuf1);
400 dev = class_uuid2dev(&uuid);
403 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
405 GOTO(out, err = -EINVAL);
408 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
410 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
415 case OBD_IOC_GETDEVICE: {
416 int index = data->ioc_count;
419 if (!data->ioc_inlbuf1) {
420 CERROR("No buffer passed in ioctl\n");
421 GOTO(out, err = -EINVAL);
423 if (data->ioc_inllen1 < 128) {
424 CERROR("ioctl buffer too small to hold version\n");
425 GOTO(out, err = -EINVAL);
428 obd = class_num2obd(index);
430 GOTO(out, err = -ENOENT);
432 if (obd->obd_stopping)
434 else if (obd->obd_inactive)
436 else if (obd->obd_set_up)
438 else if (obd->obd_attached)
443 str = (char *)data->ioc_bulk;
444 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
445 (int)index, status, obd->obd_type->typ_name,
446 obd->obd_name, obd->obd_uuid.uuid,
447 atomic_read(&obd->obd_refcount));
449 if (copy_to_user((void __user *)arg, data, len))
457 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
458 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
459 GOTO(out, err = -EINVAL);
460 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
461 GOTO(out, err = -EINVAL);
462 obd = class_name2obd(data->ioc_inlbuf4);
463 } else if (data->ioc_dev < class_devno_max()) {
464 obd = class_num2obd(data->ioc_dev);
466 CERROR("OBD ioctl: No device\n");
467 GOTO(out, err = -EINVAL);
471 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
472 GOTO(out, err = -EINVAL);
474 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
476 if (!obd->obd_set_up || obd->obd_stopping) {
477 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
478 GOTO(out, err = -EINVAL);
482 case OBD_IOC_NO_TRANSNO: {
483 if (!obd->obd_attached) {
484 CERROR("Device %d not attached\n", obd->obd_minor);
485 GOTO(out, err = -ENODEV);
487 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
489 obd->obd_no_transno = 1;
494 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
498 if (copy_to_user((void __user *)arg, data, len))
505 OBD_FREE_LARGE(buf, len);
507 } /* class_handle_ioctl */
509 /* opening /dev/obd */
510 static int obd_class_open(struct inode * inode, struct file * file)
513 try_module_get(THIS_MODULE);
517 /* closing /dev/obd */
518 static int obd_class_release(struct inode * inode, struct file * file)
522 module_put(THIS_MODULE);
526 /* to control /dev/obd */
527 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
533 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
534 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
535 RETURN(err = -EACCES);
537 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
538 RETURN(err = -ENOTTY);
540 err = class_handle_ioctl(cmd, (unsigned long)arg);
545 /* declare character device */
546 static struct file_operations obd_psdev_fops = {
547 .owner = THIS_MODULE,
548 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
549 .open = obd_class_open, /* open */
550 .release = obd_class_release, /* release */
554 struct miscdevice obd_psdev = {
555 .minor = MISC_DYNAMIC_MINOR,
556 .name = OBD_DEV_NAME,
557 .fops = &obd_psdev_fops,
560 static int obd_init_checks(void)
562 __u64 u64val, div64val;
566 CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
568 u64val = OBD_OBJECT_EOF;
569 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
570 if (u64val != OBD_OBJECT_EOF) {
571 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
572 u64val, (int)sizeof(u64val));
575 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
577 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
581 div64val = OBD_OBJECT_EOF;
582 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
583 if (u64val != OBD_OBJECT_EOF) {
584 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
585 u64val, (int)sizeof(u64val));
588 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
589 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
590 u64val, (int)sizeof(u64val));
593 if (do_div(div64val, 256) != (u64val & 255)) {
594 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
597 if (u64val >> 8 != div64val) {
598 CERROR("do_div(%#llx,256) %llu != %llu\n",
599 u64val, div64val, u64val >> 8);
602 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
604 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
607 len = snprintf(buf, sizeof(buf), "%llu", u64val);
609 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
612 len = snprintf(buf, sizeof(buf), "%lld", u64val);
614 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
617 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
618 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
626 static int __init obdclass_init(void)
630 LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
634 err = obd_init_checks();
635 if (err == -EOVERFLOW)
638 #ifdef CONFIG_PROC_FS
639 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
640 LPROCFS_STATS_FLAG_NONE |
641 LPROCFS_STATS_FLAG_IRQ_SAFE);
642 if (obd_memory == NULL) {
643 CERROR("kmalloc of 'obd_memory' failed\n");
647 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
648 LPROCFS_CNTR_AVGMINMAX,
651 err = obd_zombie_impexp_init();
653 goto cleanup_obd_memory;
655 err = class_handle_init();
657 goto cleanup_zombie_impexp;
659 err = misc_register(&obd_psdev);
661 CERROR("cannot register OBD miscdevice: err = %d\n", err);
662 goto cleanup_class_handle;
665 /* Default the dirty page cache cap to 1/2 of system memory.
666 * For clients with less memory, a larger fraction is needed
667 * for other purposes (mostly for BGL). */
668 if (totalram_pages <= 512 << (20 - PAGE_SHIFT))
669 obd_max_dirty_pages = totalram_pages / 4;
671 obd_max_dirty_pages = totalram_pages / 2;
673 err = obd_init_caches();
675 goto cleanup_deregister;
677 err = class_procfs_init();
681 err = lu_global_init();
683 goto cleanup_class_procfs;
685 err = cl_global_init();
687 goto cleanup_lu_global;
689 #ifdef HAVE_SERVER_SUPPORT
690 err = dt_global_init();
692 goto cleanup_cl_global;
694 err = lu_ucred_global_init();
696 goto cleanup_dt_global;
697 #endif /* HAVE_SERVER_SUPPORT */
699 err = llog_info_init();
701 #ifdef HAVE_SERVER_SUPPORT
702 goto cleanup_lu_ucred_global;
703 #else /* !HAVE_SERVER_SUPPORT */
704 goto cleanup_cl_global;
705 #endif /* HAVE_SERVER_SUPPORT */
707 err = lustre_register_fs();
709 /* simulate a late OOM situation now to require all
710 * alloc'ed/initialized resources to be freed */
711 if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
712 /* fake error but filesystem has been registered */
713 lustre_unregister_fs();
714 /* force error to ensure module will be unloaded/cleaned */
719 goto cleanup_llog_info;
726 #ifdef HAVE_SERVER_SUPPORT
727 cleanup_lu_ucred_global:
728 lu_ucred_global_fini();
732 #endif /* HAVE_SERVER_SUPPORT */
740 cleanup_class_procfs:
741 class_procfs_clean();
744 obd_cleanup_caches();
747 misc_deregister(&obd_psdev);
749 cleanup_class_handle:
750 class_handle_cleanup();
752 cleanup_zombie_impexp:
753 obd_zombie_impexp_stop();
756 #ifdef CONFIG_PROC_FS
757 lprocfs_free_stats(&obd_memory);
763 void obd_update_maxusage(void)
767 max = obd_memory_sum();
769 spin_lock(&obd_updatemax_lock);
770 if (max > obd_max_alloc)
772 spin_unlock(&obd_updatemax_lock);
774 EXPORT_SYMBOL(obd_update_maxusage);
776 #ifdef CONFIG_PROC_FS
777 __u64 obd_memory_max(void)
781 obd_update_maxusage();
782 spin_lock(&obd_updatemax_lock);
784 spin_unlock(&obd_updatemax_lock);
788 #endif /* CONFIG_PROC_FS */
790 static void __exit obdclass_exit(void)
792 #ifdef CONFIG_PROC_FS
795 #endif /* CONFIG_PROC_FS */
798 lustre_unregister_fs();
800 misc_deregister(&obd_psdev);
802 #ifdef HAVE_SERVER_SUPPORT
803 lu_ucred_global_fini();
805 #endif /* HAVE_SERVER_SUPPORT */
809 obd_cleanup_caches();
811 class_procfs_clean();
813 class_handle_cleanup();
814 class_del_uuid(NULL); /* Delete all UUIDs. */
815 obd_zombie_impexp_stop();
817 #ifdef CONFIG_PROC_FS
818 memory_leaked = obd_memory_sum();
819 memory_max = obd_memory_max();
821 lprocfs_free_stats(&obd_memory);
822 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
823 "obd_memory max: %llu, leaked: %llu\n",
824 memory_max, memory_leaked);
825 #endif /* CONFIG_PROC_FS */
830 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
831 MODULE_DESCRIPTION("Lustre Class Driver");
832 MODULE_VERSION(LUSTRE_VERSION_STRING);
833 MODULE_LICENSE("GPL");
835 module_init(obdclass_init);
836 module_exit(obdclass_exit);