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 case OBD_GET_VERSION:
261 if (!data->ioc_inlbuf1) {
262 CERROR("No buffer passed in ioctl\n");
263 GOTO(out, err = -EINVAL);
266 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
267 CERROR("ioctl buffer too small to hold version\n");
268 GOTO(out, err = -EINVAL);
271 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
272 strlen(LUSTRE_VERSION_STRING) + 1);
274 if (copy_to_user((void __user *)arg, data, len))
278 case OBD_IOC_NAME2DEV: {
279 /* Resolve a device name. This does not change the
280 * currently selected device.
284 dev = class_resolve_dev_name(data->ioc_inllen1,
288 GOTO(out, err = -EINVAL);
290 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
295 case OBD_IOC_UUID2DEV: {
296 /* Resolve a device uuid. This does not change the
297 * currently selected device.
300 struct obd_uuid uuid;
302 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
303 CERROR("No UUID passed!\n");
304 GOTO(out, err = -EINVAL);
306 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
307 CERROR("UUID not NUL terminated!\n");
308 GOTO(out, err = -EINVAL);
311 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
312 obd_str2uuid(&uuid, data->ioc_inlbuf1);
313 dev = class_uuid2dev(&uuid);
316 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
318 GOTO(out, err = -EINVAL);
321 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
323 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
328 case OBD_IOC_GETDEVICE: {
329 int index = data->ioc_count;
332 if (!data->ioc_inlbuf1) {
333 CERROR("No buffer passed in ioctl\n");
334 GOTO(out, err = -EINVAL);
336 if (data->ioc_inllen1 < 128) {
337 CERROR("ioctl buffer too small to hold version\n");
338 GOTO(out, err = -EINVAL);
341 obd = class_num2obd(index);
343 GOTO(out, err = -ENOENT);
345 if (obd->obd_stopping)
347 else if (obd->obd_set_up)
349 else if (obd->obd_attached)
353 str = (char *)data->ioc_bulk;
354 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
355 (int)index, status, obd->obd_type->typ_name,
356 obd->obd_name, obd->obd_uuid.uuid,
357 atomic_read(&obd->obd_refcount));
359 if (copy_to_user((void __user *)arg, data, len))
367 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
368 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
369 GOTO(out, err = -EINVAL);
370 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
371 GOTO(out, err = -EINVAL);
372 obd = class_name2obd(data->ioc_inlbuf4);
373 } else if (data->ioc_dev < class_devno_max()) {
374 obd = class_num2obd(data->ioc_dev);
376 CERROR("OBD ioctl: No device\n");
377 GOTO(out, err = -EINVAL);
381 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
382 GOTO(out, err = -EINVAL);
384 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
386 if (!obd->obd_set_up || obd->obd_stopping) {
387 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
388 GOTO(out, err = -EINVAL);
392 case OBD_IOC_NO_TRANSNO: {
393 if (!obd->obd_attached) {
394 CERROR("Device %d not attached\n", obd->obd_minor);
395 GOTO(out, err = -ENODEV);
397 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
399 obd->obd_no_transno = 1;
404 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
408 if (copy_to_user((void __user *)arg, data, len))
415 OBD_FREE_LARGE(buf, len);
417 } /* class_handle_ioctl */
419 #define OBD_INIT_CHECK
420 #ifdef OBD_INIT_CHECK
421 static int obd_init_checks(void)
423 __u64 u64val, div64val;
427 CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
429 u64val = OBD_OBJECT_EOF;
430 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
431 if (u64val != OBD_OBJECT_EOF) {
432 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
433 u64val, (int)sizeof(u64val));
436 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
438 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
442 div64val = OBD_OBJECT_EOF;
443 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
444 if (u64val != OBD_OBJECT_EOF) {
445 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
446 u64val, (int)sizeof(u64val));
449 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
450 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
451 u64val, (int)sizeof(u64val));
454 if (do_div(div64val, 256) != (u64val & 255)) {
455 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
458 if (u64val >> 8 != div64val) {
459 CERROR("do_div(%#llx,256) %llu != %llu\n",
460 u64val, div64val, u64val >> 8);
463 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
465 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
468 len = snprintf(buf, sizeof(buf), "%llu", u64val);
470 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
473 len = snprintf(buf, sizeof(buf), "%lld", u64val);
475 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
478 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
479 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
487 #define obd_init_checks() do {} while(0)
490 static int __init obdclass_init(void)
494 LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
498 err = obd_init_checks();
499 if (err == -EOVERFLOW)
502 #ifdef CONFIG_PROC_FS
503 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
504 LPROCFS_STATS_FLAG_NONE |
505 LPROCFS_STATS_FLAG_IRQ_SAFE);
506 if (obd_memory == NULL) {
507 CERROR("kmalloc of 'obd_memory' failed\n");
511 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
512 LPROCFS_CNTR_AVGMINMAX,
515 err = obd_zombie_impexp_init();
517 goto cleanup_obd_memory;
519 err = class_handle_init();
521 goto cleanup_zombie_impexp;
523 err = misc_register(&obd_psdev);
525 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
526 goto cleanup_class_handle;
529 /* Default the dirty page cache cap to 1/2 of system memory.
530 * For clients with less memory, a larger fraction is needed
531 * for other purposes (mostly for BGL). */
532 if (totalram_pages <= 512 << (20 - PAGE_SHIFT))
533 obd_max_dirty_pages = totalram_pages / 4;
535 obd_max_dirty_pages = totalram_pages / 2;
537 err = obd_init_caches();
539 goto cleanup_deregister;
541 err = class_procfs_init();
545 err = lu_global_init();
547 goto cleanup_class_procfs;
549 err = cl_global_init();
551 goto cleanup_lu_global;
553 #ifdef HAVE_SERVER_SUPPORT
554 err = dt_global_init();
556 goto cleanup_cl_global;
558 err = lu_ucred_global_init();
560 goto cleanup_dt_global;
561 #endif /* HAVE_SERVER_SUPPORT */
563 err = llog_info_init();
565 #ifdef HAVE_SERVER_SUPPORT
566 goto cleanup_lu_ucred_global;
567 #else /* !HAVE_SERVER_SUPPORT */
568 goto cleanup_cl_global;
569 #endif /* HAVE_SERVER_SUPPORT */
571 err = lustre_register_fs();
573 /* simulate a late OOM situation now to require all
574 * alloc'ed/initialized resources to be freed */
575 if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
576 /* fake error but filesystem has been registered */
577 lustre_unregister_fs();
578 /* force error to ensure module will be unloaded/cleaned */
583 goto cleanup_llog_info;
590 #ifdef HAVE_SERVER_SUPPORT
591 cleanup_lu_ucred_global:
592 lu_ucred_global_fini();
596 #endif /* HAVE_SERVER_SUPPORT */
604 cleanup_class_procfs:
606 class_procfs_clean();
609 obd_cleanup_caches();
612 misc_deregister(&obd_psdev);
614 cleanup_class_handle:
615 class_handle_cleanup();
617 cleanup_zombie_impexp:
618 obd_zombie_impexp_stop();
621 #ifdef CONFIG_PROC_FS
622 lprocfs_free_stats(&obd_memory);
628 void obd_update_maxusage(void)
632 max = obd_memory_sum();
634 spin_lock(&obd_updatemax_lock);
635 if (max > obd_max_alloc)
637 spin_unlock(&obd_updatemax_lock);
639 EXPORT_SYMBOL(obd_update_maxusage);
641 #ifdef CONFIG_PROC_FS
642 __u64 obd_memory_max(void)
646 obd_update_maxusage();
647 spin_lock(&obd_updatemax_lock);
649 spin_unlock(&obd_updatemax_lock);
653 #endif /* CONFIG_PROC_FS */
655 static void __exit obdclass_exit(void)
657 #ifdef CONFIG_PROC_FS
660 #endif /* CONFIG_PROC_FS */
663 lustre_unregister_fs();
665 misc_deregister(&obd_psdev);
667 #ifdef HAVE_SERVER_SUPPORT
668 lu_ucred_global_fini();
670 #endif /* HAVE_SERVER_SUPPORT */
674 obd_cleanup_caches();
677 class_procfs_clean();
679 class_handle_cleanup();
680 class_del_uuid(NULL); /* Delete all UUIDs. */
681 obd_zombie_impexp_stop();
683 #ifdef CONFIG_PROC_FS
684 memory_leaked = obd_memory_sum();
685 memory_max = obd_memory_max();
687 lprocfs_free_stats(&obd_memory);
688 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
689 "obd_memory max: %llu, leaked: %llu\n",
690 memory_max, memory_leaked);
691 #endif /* CONFIG_PROC_FS */
696 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
697 MODULE_DESCRIPTION("Lustre Class Driver");
698 MODULE_VERSION(LUSTRE_VERSION_STRING);
699 MODULE_LICENSE("GPL");
701 module_init(obdclass_init);
702 module_exit(obdclass_exit);