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, 2015, 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/user_namespace.h>
36 #ifdef HAVE_UIDGID_HEADER
37 # include <linux/uidgid.h>
39 #include <linux/atomic.h>
40 #include <linux/list.h>
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <lnet/lnetctl.h>
45 #include <lustre_debug.h>
46 #include <lustre_kernelcomm.h>
47 #include <lprocfs_status.h>
48 #include <lustre_ver.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_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 char obd_jobid_var[JOBSTATS_JOBID_VAR_MAX_LEN + 1] = JOBSTATS_DISABLE;
104 #ifdef CONFIG_PROC_FS
105 struct lprocfs_stats *obd_memory = NULL;
106 EXPORT_SYMBOL(obd_memory);
109 char obd_jobid_node[LUSTRE_JOBID_SIZE + 1];
111 /* Get jobid of current process by reading the environment variable
112 * stored in between the "env_start" & "env_end" of task struct.
115 * It's better to cache the jobid for later use if there is any
116 * efficient way, the cl_env code probably could be reused for this
119 * If some job scheduler doesn't store jobid in the "env_start/end",
120 * then an upcall could be issued here to get the jobid by utilizing
121 * the userspace tools/api. Then, the jobid must be cached.
123 int lustre_get_jobid(char *jobid)
125 int jobid_len = LUSTRE_JOBID_SIZE;
126 char tmp_jobid[LUSTRE_JOBID_SIZE] = { 0 };
130 /* Jobstats isn't enabled */
131 if (strcmp(obd_jobid_var, JOBSTATS_DISABLE) == 0)
134 /* Whole node dedicated to single job */
135 if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) == 0) {
136 memcpy(tmp_jobid, obd_jobid_node, LUSTRE_JOBID_SIZE);
140 /* Use process name + fsuid as jobid */
141 if (strcmp(obd_jobid_var, JOBSTATS_PROCNAME_UID) == 0) {
142 snprintf(tmp_jobid, LUSTRE_JOBID_SIZE, "%s.%u",
144 from_kuid(&init_user_ns, current_fsuid()));
148 rc = cfs_get_environ(obd_jobid_var, tmp_jobid, &jobid_len);
150 if (rc == -EOVERFLOW) {
151 /* For the PBS_JOBID and LOADL_STEP_ID keys (which are
152 * variable length strings instead of just numbers), it
153 * might make sense to keep the unique parts for JobID,
154 * instead of just returning an error. That means a
155 * larger temp buffer for cfs_get_environ(), then
156 * truncating the string at some separator to fit into
157 * the specified jobid_len. Fix later if needed. */
159 if (unlikely(!printed)) {
160 LCONSOLE_ERROR_MSG(0x16b, "%s value too large "
161 "for JobID buffer (%d)\n",
162 obd_jobid_var, jobid_len);
166 CDEBUG((rc == -ENOENT || rc == -EINVAL ||
167 rc == -EDEADLK) ? D_INFO : D_ERROR,
168 "Get jobid for (%s) failed: rc = %d\n",
177 /* Only replace the job ID if it changed. */
178 if (strcmp(jobid, tmp_jobid) != 0)
179 memcpy(jobid, tmp_jobid, jobid_len);
183 EXPORT_SYMBOL(lustre_get_jobid);
185 static int class_resolve_dev_name(__u32 len, const char *name)
192 CERROR("No name passed,!\n");
193 GOTO(out, rc = -EINVAL);
195 if (name[len - 1] != 0) {
196 CERROR("Name not nul terminated!\n");
197 GOTO(out, rc = -EINVAL);
200 CDEBUG(D_IOCTL, "device name %s\n", name);
201 dev = class_name2dev(name);
203 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
204 GOTO(out, rc = -EINVAL);
207 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
214 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
217 struct obd_ioctl_data *data;
218 struct libcfs_debug_ioctl_data *debug_data;
219 struct obd_device *obd = NULL;
220 int err = 0, len = 0;
223 /* only for debugging */
224 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
225 debug_data = (struct libcfs_debug_ioctl_data*)arg;
226 libcfs_subsystem_debug = debug_data->subs;
227 libcfs_debug = debug_data->debug;
231 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
232 if (obd_ioctl_getdata(&buf, &len, (void __user *)arg)) {
233 CERROR("OBD ioctl: data error\n");
236 data = (struct obd_ioctl_data *)buf;
239 case OBD_IOC_PROCESS_CFG: {
240 struct lustre_cfg *lcfg;
242 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
243 CERROR("No config buffer passed!\n");
244 GOTO(out, err = -EINVAL);
246 OBD_ALLOC(lcfg, data->ioc_plen1);
248 GOTO(out, err = -ENOMEM);
249 err = copy_from_user(lcfg, data->ioc_pbuf1,
252 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
254 err = class_process_config(lcfg);
256 OBD_FREE(lcfg, data->ioc_plen1);
260 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
261 case OBD_GET_VERSION: {
264 if (!data->ioc_inlbuf1) {
265 CERROR("No buffer passed in ioctl\n");
266 GOTO(out, err = -EINVAL);
269 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
270 CERROR("ioctl buffer too small to hold version\n");
271 GOTO(out, err = -EINVAL);
276 CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
277 "use llapi_get_version_string() and/or relink\n",
280 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
281 strlen(LUSTRE_VERSION_STRING) + 1);
283 if (copy_to_user((void __user *)arg, data, len))
288 case OBD_IOC_NAME2DEV: {
289 /* Resolve a device name. This does not change the
290 * currently selected device.
294 dev = class_resolve_dev_name(data->ioc_inllen1,
298 GOTO(out, err = -EINVAL);
300 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
305 case OBD_IOC_UUID2DEV: {
306 /* Resolve a device uuid. This does not change the
307 * currently selected device.
310 struct obd_uuid uuid;
312 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
313 CERROR("No UUID passed!\n");
314 GOTO(out, err = -EINVAL);
316 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
317 CERROR("UUID not NUL terminated!\n");
318 GOTO(out, err = -EINVAL);
321 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
322 obd_str2uuid(&uuid, data->ioc_inlbuf1);
323 dev = class_uuid2dev(&uuid);
326 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
328 GOTO(out, err = -EINVAL);
331 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
333 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
338 case OBD_IOC_GETDEVICE: {
339 int index = data->ioc_count;
342 if (!data->ioc_inlbuf1) {
343 CERROR("No buffer passed in ioctl\n");
344 GOTO(out, err = -EINVAL);
346 if (data->ioc_inllen1 < 128) {
347 CERROR("ioctl buffer too small to hold version\n");
348 GOTO(out, err = -EINVAL);
351 obd = class_num2obd(index);
353 GOTO(out, err = -ENOENT);
355 if (obd->obd_stopping)
357 else if (obd->obd_set_up)
359 else if (obd->obd_attached)
363 str = (char *)data->ioc_bulk;
364 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
365 (int)index, status, obd->obd_type->typ_name,
366 obd->obd_name, obd->obd_uuid.uuid,
367 atomic_read(&obd->obd_refcount));
369 if (copy_to_user((void __user *)arg, data, len))
377 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
378 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
379 GOTO(out, err = -EINVAL);
380 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
381 GOTO(out, err = -EINVAL);
382 obd = class_name2obd(data->ioc_inlbuf4);
383 } else if (data->ioc_dev < class_devno_max()) {
384 obd = class_num2obd(data->ioc_dev);
386 CERROR("OBD ioctl: No device\n");
387 GOTO(out, err = -EINVAL);
391 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
392 GOTO(out, err = -EINVAL);
394 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
396 if (!obd->obd_set_up || obd->obd_stopping) {
397 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
398 GOTO(out, err = -EINVAL);
402 case OBD_IOC_NO_TRANSNO: {
403 if (!obd->obd_attached) {
404 CERROR("Device %d not attached\n", obd->obd_minor);
405 GOTO(out, err = -ENODEV);
407 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
409 obd->obd_no_transno = 1;
414 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
418 if (copy_to_user((void __user *)arg, data, len))
425 OBD_FREE_LARGE(buf, len);
427 } /* class_handle_ioctl */
429 #define OBD_INIT_CHECK
430 #ifdef OBD_INIT_CHECK
431 static int obd_init_checks(void)
433 __u64 u64val, div64val;
437 CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
439 u64val = OBD_OBJECT_EOF;
440 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
441 if (u64val != OBD_OBJECT_EOF) {
442 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
443 u64val, (int)sizeof(u64val));
446 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
448 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
452 div64val = OBD_OBJECT_EOF;
453 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
454 if (u64val != OBD_OBJECT_EOF) {
455 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
456 u64val, (int)sizeof(u64val));
459 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
460 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
461 u64val, (int)sizeof(u64val));
464 if (do_div(div64val, 256) != (u64val & 255)) {
465 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
468 if (u64val >> 8 != div64val) {
469 CERROR("do_div(%#llx,256) %llu != %llu\n",
470 u64val, div64val, u64val >> 8);
473 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
475 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
478 len = snprintf(buf, sizeof(buf), "%llu", u64val);
480 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
483 len = snprintf(buf, sizeof(buf), "%lld", u64val);
485 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
488 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
489 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
497 #define obd_init_checks() do {} while(0)
500 static int __init obdclass_init(void)
504 LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
508 err = obd_init_checks();
509 if (err == -EOVERFLOW)
512 #ifdef CONFIG_PROC_FS
513 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
514 LPROCFS_STATS_FLAG_NONE |
515 LPROCFS_STATS_FLAG_IRQ_SAFE);
516 if (obd_memory == NULL) {
517 CERROR("kmalloc of 'obd_memory' failed\n");
521 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
522 LPROCFS_CNTR_AVGMINMAX,
525 err = obd_zombie_impexp_init();
527 goto cleanup_obd_memory;
529 err = class_handle_init();
531 goto cleanup_zombie_impexp;
533 err = misc_register(&obd_psdev);
535 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
536 goto cleanup_class_handle;
539 /* Default the dirty page cache cap to 1/2 of system memory.
540 * For clients with less memory, a larger fraction is needed
541 * for other purposes (mostly for BGL). */
542 if (totalram_pages <= 512 << (20 - PAGE_SHIFT))
543 obd_max_dirty_pages = totalram_pages / 4;
545 obd_max_dirty_pages = totalram_pages / 2;
547 err = obd_init_caches();
549 goto cleanup_deregister;
551 err = class_procfs_init();
555 err = lu_global_init();
557 goto cleanup_class_procfs;
559 err = cl_global_init();
561 goto cleanup_lu_global;
563 #ifdef HAVE_SERVER_SUPPORT
564 err = dt_global_init();
566 goto cleanup_cl_global;
568 err = lu_ucred_global_init();
570 goto cleanup_dt_global;
571 #endif /* HAVE_SERVER_SUPPORT */
573 err = llog_info_init();
575 #ifdef HAVE_SERVER_SUPPORT
576 goto cleanup_lu_ucred_global;
577 #else /* !HAVE_SERVER_SUPPORT */
578 goto cleanup_cl_global;
579 #endif /* HAVE_SERVER_SUPPORT */
581 err = lustre_register_fs();
583 /* simulate a late OOM situation now to require all
584 * alloc'ed/initialized resources to be freed */
585 if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
586 /* fake error but filesystem has been registered */
587 lustre_unregister_fs();
588 /* force error to ensure module will be unloaded/cleaned */
593 goto cleanup_llog_info;
600 #ifdef HAVE_SERVER_SUPPORT
601 cleanup_lu_ucred_global:
602 lu_ucred_global_fini();
606 #endif /* HAVE_SERVER_SUPPORT */
614 cleanup_class_procfs:
616 class_procfs_clean();
619 obd_cleanup_caches();
622 misc_deregister(&obd_psdev);
624 cleanup_class_handle:
625 class_handle_cleanup();
627 cleanup_zombie_impexp:
628 obd_zombie_impexp_stop();
631 #ifdef CONFIG_PROC_FS
632 lprocfs_free_stats(&obd_memory);
638 void obd_update_maxusage(void)
642 max = obd_memory_sum();
644 spin_lock(&obd_updatemax_lock);
645 if (max > obd_max_alloc)
647 spin_unlock(&obd_updatemax_lock);
649 EXPORT_SYMBOL(obd_update_maxusage);
651 #ifdef CONFIG_PROC_FS
652 __u64 obd_memory_max(void)
656 obd_update_maxusage();
657 spin_lock(&obd_updatemax_lock);
659 spin_unlock(&obd_updatemax_lock);
663 #endif /* CONFIG_PROC_FS */
665 static void __exit obdclass_exit(void)
667 #ifdef CONFIG_PROC_FS
670 #endif /* CONFIG_PROC_FS */
673 lustre_unregister_fs();
675 misc_deregister(&obd_psdev);
677 #ifdef HAVE_SERVER_SUPPORT
678 lu_ucred_global_fini();
680 #endif /* HAVE_SERVER_SUPPORT */
684 obd_cleanup_caches();
687 class_procfs_clean();
689 class_handle_cleanup();
690 class_del_uuid(NULL); /* Delete all UUIDs. */
691 obd_zombie_impexp_stop();
693 #ifdef CONFIG_PROC_FS
694 memory_leaked = obd_memory_sum();
695 memory_max = obd_memory_max();
697 lprocfs_free_stats(&obd_memory);
698 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
699 "obd_memory max: %llu, leaked: %llu\n",
700 memory_max, memory_leaked);
701 #endif /* CONFIG_PROC_FS */
706 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
707 MODULE_DESCRIPTION("Lustre Class Driver");
708 MODULE_VERSION(LUSTRE_VERSION_STRING);
709 MODULE_LICENSE("GPL");
711 module_init(obdclass_init);
712 module_exit(obdclass_exit);