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/
32 #define DEBUG_SUBSYSTEM S_CLASS
34 #include <linux/miscdevice.h>
35 #include <linux/user_namespace.h>
36 #include <linux/uidgid.h>
37 #include <linux/atomic.h>
38 #include <linux/list.h>
39 #include <linux/oom.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <uapi/linux/lnet/lnetctl.h>
44 #include <lustre_kernelcomm.h>
45 #include <lprocfs_status.h>
46 #include <cl_object.h>
47 #ifdef HAVE_SERVER_SUPPORT
48 # include <dt_object.h>
49 # include <md_object.h>
50 #endif /* HAVE_SERVER_SUPPORT */
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include "llog_internal.h"
55 static __u64 obd_max_alloc;
60 static DEFINE_SPINLOCK(obd_updatemax_lock);
62 /* The following are visible and mutable through /proc/sys/lustre/. */
63 unsigned int obd_debug_peer_on_timeout;
64 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
65 unsigned int obd_dump_on_timeout;
66 EXPORT_SYMBOL(obd_dump_on_timeout);
67 unsigned int obd_dump_on_eviction;
68 EXPORT_SYMBOL(obd_dump_on_eviction);
69 unsigned int obd_lbug_on_eviction;
70 EXPORT_SYMBOL(obd_lbug_on_eviction);
71 unsigned long obd_max_dirty_pages;
72 EXPORT_SYMBOL(obd_max_dirty_pages);
73 atomic_long_t obd_dirty_pages;
74 EXPORT_SYMBOL(obd_dirty_pages);
75 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
76 EXPORT_SYMBOL(obd_timeout);
77 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
78 EXPORT_SYMBOL(ldlm_timeout);
79 unsigned int ping_interval = (OBD_TIMEOUT_DEFAULT > 4) ?
80 (OBD_TIMEOUT_DEFAULT / 4) : 1;
81 EXPORT_SYMBOL(ping_interval);
82 unsigned int ping_evict_timeout_multiplier = 6;
83 EXPORT_SYMBOL(ping_evict_timeout_multiplier);
84 unsigned int obd_timeout_set;
85 EXPORT_SYMBOL(obd_timeout_set);
86 unsigned int ldlm_timeout_set;
87 EXPORT_SYMBOL(ldlm_timeout_set);
88 /* bulk transfer timeout, give up after 100s by default */
89 unsigned int bulk_timeout = 100; /* seconds */
90 EXPORT_SYMBOL(bulk_timeout);
91 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
92 unsigned int at_min = 0;
93 EXPORT_SYMBOL(at_min);
94 unsigned int at_max = 600;
95 EXPORT_SYMBOL(at_max);
96 unsigned int at_history = 600;
97 EXPORT_SYMBOL(at_history);
98 int at_early_margin = 5;
99 EXPORT_SYMBOL(at_early_margin);
101 EXPORT_SYMBOL(at_extra);
103 #ifdef CONFIG_PROC_FS
104 struct lprocfs_stats *obd_memory = NULL;
105 EXPORT_SYMBOL(obd_memory);
108 static int obdclass_oom_handler(struct notifier_block *self,
109 unsigned long notused, void *nfreed)
111 #ifdef CONFIG_PROC_FS
113 pr_info("obd_memory max: %llu, obd_memory current: %llu\n",
114 obd_memory_max(), obd_memory_sum());
115 #endif /* CONFIG_PROC_FS */
120 static struct notifier_block obdclass_oom = {
121 .notifier_call = obdclass_oom_handler
124 static int class_resolve_dev_name(__u32 len, const char *name)
131 CERROR("No name passed,!\n");
132 GOTO(out, rc = -EINVAL);
134 if (name[len - 1] != 0) {
135 CERROR("Name not nul terminated!\n");
136 GOTO(out, rc = -EINVAL);
139 CDEBUG(D_IOCTL, "device name %s\n", name);
140 dev = class_name2dev(name);
142 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
143 GOTO(out, rc = -EINVAL);
146 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
153 #define OBD_MAX_IOCTL_BUFFER 8192
155 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
157 const int maxlen = 1 << 30;
158 if (data->ioc_len > maxlen) {
159 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
163 if (data->ioc_inllen1 > maxlen) {
164 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
168 if (data->ioc_inllen2 > maxlen) {
169 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
173 if (data->ioc_inllen3 > maxlen) {
174 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
178 if (data->ioc_inllen4 > maxlen) {
179 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
183 if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
184 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
188 if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
189 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
193 if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
194 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
198 if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
199 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
203 if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
204 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
208 if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
209 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
213 if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
214 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
218 if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
219 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
223 if (obd_ioctl_packlen(data) > data->ioc_len) {
224 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
225 obd_ioctl_packlen(data), data->ioc_len);
232 /* buffer MUST be at least the size of obd_ioctl_hdr */
233 int obd_ioctl_getdata(struct obd_ioctl_data **datap, int *len, void __user *arg)
235 struct obd_ioctl_hdr hdr;
236 struct obd_ioctl_data *data;
240 if (copy_from_user(&hdr, arg, sizeof(hdr)))
243 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
244 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
245 OBD_IOCTL_VERSION, hdr.ioc_version);
249 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
250 CERROR("User buffer len %d exceeds %d max buffer\n",
251 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
255 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
256 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
260 /* When there are lots of processes calling vmalloc on multi-core
261 * system, the high lock contention will hurt performance badly,
262 * obdfilter-survey is an example, which relies on ioctl. So we'd
263 * better avoid vmalloc on ioctl path. LU-66
265 OBD_ALLOC_LARGE(data, hdr.ioc_len);
267 CERROR("Cannot allocate control buffer of len %d\n",
273 if (copy_from_user(data, arg, hdr.ioc_len)) {
274 OBD_FREE_LARGE(data, hdr.ioc_len);
278 if (obd_ioctl_is_invalid(data)) {
279 CERROR("ioctl not correctly formatted\n");
280 OBD_FREE_LARGE(data, hdr.ioc_len);
284 if (data->ioc_inllen1) {
285 data->ioc_inlbuf1 = &data->ioc_bulk[0];
286 offset += cfs_size_round(data->ioc_inllen1);
289 if (data->ioc_inllen2) {
290 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
291 offset += cfs_size_round(data->ioc_inllen2);
294 if (data->ioc_inllen3) {
295 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
296 offset += cfs_size_round(data->ioc_inllen3);
299 if (data->ioc_inllen4)
300 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
306 EXPORT_SYMBOL(obd_ioctl_getdata);
308 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
310 struct obd_ioctl_data *data;
311 struct obd_device *obd = NULL;
312 int err = 0, len = 0;
315 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
316 if (obd_ioctl_getdata(&data, &len, (void __user *)arg)) {
317 CERROR("OBD ioctl: data error\n");
322 case OBD_IOC_PROCESS_CFG: {
323 struct lustre_cfg *lcfg;
325 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
326 CERROR("No config buffer passed!\n");
327 GOTO(out, err = -EINVAL);
329 OBD_ALLOC(lcfg, data->ioc_plen1);
331 GOTO(out, err = -ENOMEM);
332 err = copy_from_user(lcfg, data->ioc_pbuf1,
335 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
337 err = class_process_config(lcfg);
339 OBD_FREE(lcfg, data->ioc_plen1);
343 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
344 case OBD_GET_VERSION: {
347 if (!data->ioc_inlbuf1) {
348 CERROR("No buffer passed in ioctl\n");
349 GOTO(out, err = -EINVAL);
352 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
353 CERROR("ioctl buffer too small to hold version\n");
354 GOTO(out, err = -EINVAL);
359 CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
360 "use llapi_get_version_string() and/or relink\n",
363 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
364 strlen(LUSTRE_VERSION_STRING) + 1);
366 if (copy_to_user((void __user *)arg, data, len))
371 case OBD_IOC_NAME2DEV: {
372 /* Resolve a device name. This does not change the
373 * currently selected device.
377 dev = class_resolve_dev_name(data->ioc_inllen1,
381 GOTO(out, err = -EINVAL);
383 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
388 case OBD_IOC_UUID2DEV: {
389 /* Resolve a device uuid. This does not change the
390 * currently selected device.
393 struct obd_uuid uuid;
395 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
396 CERROR("No UUID passed!\n");
397 GOTO(out, err = -EINVAL);
399 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
400 CERROR("UUID not NUL terminated!\n");
401 GOTO(out, err = -EINVAL);
404 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
405 obd_str2uuid(&uuid, data->ioc_inlbuf1);
406 dev = class_uuid2dev(&uuid);
409 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
411 GOTO(out, err = -EINVAL);
414 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
416 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
421 case OBD_IOC_GETDEVICE: {
422 int index = data->ioc_count;
425 if (!data->ioc_inlbuf1) {
426 CERROR("No buffer passed in ioctl\n");
427 GOTO(out, err = -EINVAL);
429 if (data->ioc_inllen1 < 128) {
430 CERROR("ioctl buffer too small to hold version\n");
431 GOTO(out, err = -EINVAL);
434 obd = class_num2obd(index);
436 GOTO(out, err = -ENOENT);
438 if (obd->obd_stopping)
440 else if (obd->obd_inactive)
442 else if (obd->obd_set_up)
444 else if (obd->obd_attached)
449 str = (char *)data->ioc_bulk;
450 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
451 (int)index, status, obd->obd_type->typ_name,
452 obd->obd_name, obd->obd_uuid.uuid,
453 atomic_read(&obd->obd_refcount));
455 if (copy_to_user((void __user *)arg, data, len))
463 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
464 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
465 GOTO(out, err = -EINVAL);
466 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
467 GOTO(out, err = -EINVAL);
468 obd = class_name2obd(data->ioc_inlbuf4);
469 } else if (data->ioc_dev < class_devno_max()) {
470 obd = class_num2obd(data->ioc_dev);
472 CERROR("OBD ioctl: No device\n");
473 GOTO(out, err = -EINVAL);
477 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
478 GOTO(out, err = -EINVAL);
480 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
482 if (!obd->obd_set_up || obd->obd_stopping) {
483 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
484 GOTO(out, err = -EINVAL);
487 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
491 if (copy_to_user((void __user *)arg, data, len))
494 OBD_FREE_LARGE(data, len);
496 } /* class_handle_ioctl */
498 /* to control /dev/obd */
499 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
505 /* Allow non-root access for some limited ioctls */
506 if (!capable(CAP_SYS_ADMIN))
507 RETURN(err = -EACCES);
509 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
510 RETURN(err = -ENOTTY);
512 err = class_handle_ioctl(cmd, (unsigned long)arg);
517 /* declare character device */
518 static const struct file_operations obd_psdev_fops = {
519 .owner = THIS_MODULE,
520 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
524 static struct miscdevice obd_psdev = {
525 .minor = MISC_DYNAMIC_MINOR,
526 .name = OBD_DEV_NAME,
527 .fops = &obd_psdev_fops,
530 #define test_string_to_size_err(value, expect, def_unit, __rc) \
535 BUILD_BUG_ON(sizeof(value) >= 23); \
536 __ret = sysfs_memparse(value, sizeof(value) - 1, &__size, def_unit); \
538 CERROR("string_helper: parsing '%s' expect rc %d != got %d\n", \
539 value, __rc, __ret); \
540 else if (!__ret && (u64)expect != __size) \
541 CERROR("string_helper: parsing '%s' expect %llu != got %llu\n",\
542 value, (u64)expect, __size); \
545 #define test_string_to_size_one(value, expect, def_unit) \
546 test_string_to_size_err(value, expect, def_unit, 0)
548 static int __init obd_init_checks(void)
550 __u64 u64val, div64val;
554 CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
556 u64val = OBD_OBJECT_EOF;
557 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
558 if (u64val != OBD_OBJECT_EOF) {
559 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
560 u64val, (int)sizeof(u64val));
563 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
565 CERROR("u64 hex wrong length, strlen(%s)=%d != 18\n", buf, len);
569 div64val = OBD_OBJECT_EOF;
570 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
571 if (u64val != OBD_OBJECT_EOF) {
572 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
573 u64val, (int)sizeof(u64val));
576 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
577 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
578 u64val, (int)sizeof(u64val));
581 if (do_div(div64val, 256) != (u64val & 255)) {
582 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
585 if (u64val >> 8 != div64val) {
586 CERROR("do_div(%#llx,256) %llu != %llu\n",
587 u64val, div64val, u64val >> 8);
590 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
592 CERROR("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
595 len = snprintf(buf, sizeof(buf), "%llu", u64val);
597 CERROR("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
600 len = snprintf(buf, sizeof(buf), "%lld", u64val);
602 CERROR("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
605 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
606 CERROR("mask failed: u64val %llu >= %llu\n", u64val,
614 if (!test_string_to_size_err("256B34", 256, "B", -EINVAL)) {
615 CERROR("string_helpers: format should be number then units\n");
618 if (!test_string_to_size_err("132OpQ", 132, "B", -EINVAL)) {
619 CERROR("string_helpers: invalid units should be rejected\n");
622 if (!test_string_to_size_err("1.82B", 1, "B", -EINVAL)) {
623 CERROR("string_helpers: 'B' with '.' should be invalid\n");
626 if (test_string_to_size_one("343\n", 343, "B")) {
627 CERROR("string_helpers: should ignore newline\n");
633 /* memparse unit handling */
635 ret += test_string_to_size_one("0B", 0, "B");
636 ret += test_string_to_size_one("512B", 512, "B");
637 ret += test_string_to_size_one("1.067kB", 1067, "B");
638 ret += test_string_to_size_one("1.042KiB", 1067, "B");
639 ret += test_string_to_size_one("8", 8388608, "M");
640 ret += test_string_to_size_one("65536", 65536, "B");
641 ret += test_string_to_size_one("128", 131072, "K");
642 ret += test_string_to_size_one("1M", 1048576, "B");
643 ret += test_string_to_size_one("0.5T", 549755813888ULL, "T");
644 ret += test_string_to_size_one("256.5G", 275414777856ULL, "G");
648 /* string helper values */
649 ret += test_string_to_size_one("16", 16777216, "MiB");
650 ret += test_string_to_size_one("8.39MB", 8390000, "MiB");
651 ret += test_string_to_size_one("8.00MiB", 8388608, "MiB");
652 ret += test_string_to_size_one("256GB", 256000000000ULL, "GiB");
653 ret += test_string_to_size_one("238.731GiB", 256335459385ULL, "GiB");
658 ret += test_string_to_size_one("0.4TB", 400000000000ULL, "TiB");
659 ret += test_string_to_size_one("12.5TiB", 13743895347200ULL, "TiB");
660 ret += test_string_to_size_one("2PB", 2000000000000000ULL, "PiB");
661 ret += test_string_to_size_one("16PiB", 18014398509481984ULL, "PiB");
665 /* huge values should overflow */
666 if (!test_string_to_size_err("1000EiB", 0, "EiB", -EOVERFLOW)) {
667 CERROR("string_helpers: failed to detect binary overflow\n");
670 if (!test_string_to_size_err("1000EB", 0, "EiB", -EOVERFLOW)) {
671 CERROR("string_helpers: failed to detect decimal overflow\n");
678 static int __init obdclass_init(void)
682 LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
684 register_oom_notifier(&obdclass_oom);
686 err = obd_init_checks();
690 #ifdef CONFIG_PROC_FS
691 obd_memory = lprocfs_stats_alloc(OBD_STATS_NUM,
692 LPROCFS_STATS_FLAG_NONE |
693 LPROCFS_STATS_FLAG_IRQ_SAFE);
694 if (obd_memory == NULL) {
695 CERROR("kmalloc of 'obd_memory' failed\n");
699 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
700 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_BYTES,
703 err = libcfs_kkuc_init();
705 goto cleanup_obd_memory;
707 err = obd_zombie_impexp_init();
711 err = class_handle_init();
713 goto cleanup_zombie_impexp;
715 err = misc_register(&obd_psdev);
717 CERROR("cannot register OBD miscdevice: err = %d\n", err);
718 goto cleanup_class_handle;
721 /* Default the dirty page cache cap to 1/2 of system memory.
722 * For clients with less memory, a larger fraction is needed
723 * for other purposes (mostly for BGL). */
724 if (cfs_totalram_pages() <= 512 << (20 - PAGE_SHIFT))
725 obd_max_dirty_pages = cfs_totalram_pages() / 4;
727 obd_max_dirty_pages = cfs_totalram_pages() / 2;
729 err = obd_init_caches();
731 goto cleanup_deregister;
733 err = class_procfs_init();
737 err = lu_global_init();
739 goto cleanup_class_procfs;
741 err = cl_global_init();
743 goto cleanup_lu_global;
745 err = llog_info_init();
747 goto cleanup_cl_global;
749 #ifdef HAVE_SERVER_SUPPORT
750 err = dt_global_init();
752 goto cleanup_llog_info;
754 err = lu_ucred_global_init();
756 goto cleanup_dt_global;
757 #endif /* HAVE_SERVER_SUPPORT */
759 /* simulate a late OOM situation now to require all
760 * alloc'ed/initialized resources to be freed
762 if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
763 /* force error to ensure module will be unloaded/cleaned */
770 #ifdef HAVE_SERVER_SUPPORT
771 lu_ucred_global_fini();
777 #endif /* HAVE_SERVER_SUPPORT */
786 cleanup_class_procfs:
787 class_procfs_clean();
790 obd_cleanup_caches();
793 misc_deregister(&obd_psdev);
795 cleanup_class_handle:
796 class_handle_cleanup();
798 cleanup_zombie_impexp:
799 obd_zombie_impexp_stop();
805 #ifdef CONFIG_PROC_FS
806 lprocfs_stats_free(&obd_memory);
809 unregister_oom_notifier(&obdclass_oom);
813 void obd_update_maxusage(void)
817 max = obd_memory_sum();
819 spin_lock(&obd_updatemax_lock);
820 if (max > obd_max_alloc)
822 spin_unlock(&obd_updatemax_lock);
824 EXPORT_SYMBOL(obd_update_maxusage);
826 #ifdef CONFIG_PROC_FS
827 __u64 obd_memory_max(void)
831 obd_update_maxusage();
832 spin_lock(&obd_updatemax_lock);
834 spin_unlock(&obd_updatemax_lock);
838 #endif /* CONFIG_PROC_FS */
840 static void __exit obdclass_exit(void)
842 #ifdef CONFIG_PROC_FS
845 #endif /* CONFIG_PROC_FS */
848 misc_deregister(&obd_psdev);
849 #ifdef HAVE_SERVER_SUPPORT
850 lu_ucred_global_fini();
852 #endif /* HAVE_SERVER_SUPPORT */
857 obd_cleanup_caches();
859 class_procfs_clean();
861 class_handle_cleanup();
862 class_del_uuid(NULL); /* Delete all UUIDs. */
863 obd_zombie_impexp_stop();
866 #ifdef CONFIG_PROC_FS
867 memory_leaked = obd_memory_sum();
868 memory_max = obd_memory_max();
870 lprocfs_stats_free(&obd_memory);
871 /* the below message is checked in test-framework.sh check_mem_leak() */
872 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
873 "obd_memory max: %llu, leaked: %llu\n",
874 memory_max, memory_leaked);
875 #endif /* CONFIG_PROC_FS */
877 unregister_oom_notifier(&obdclass_oom);
882 void obd_heat_clear(struct obd_heat_instance *instance, int count)
886 memset(instance, 0, sizeof(*instance) * count);
889 EXPORT_SYMBOL(obd_heat_clear);
892 * The file heat is calculated for every time interval period I. The access
893 * frequency during each period is counted. The file heat is only recalculated
894 * at the end of a time period. And a percentage of the former file heat is
895 * lost when recalculated. The recursion formula to calculate the heat of the
896 * file f is as follow:
898 * Hi+1(f) = (1-P)*Hi(f)+ P*Ci
900 * Where Hi is the heat value in the period between time points i*I and
901 * (i+1)*I; Ci is the access count in the period; the symbol P refers to the
902 * weight of Ci. The larger the value the value of P is, the more influence Ci
903 * has on the file heat.
905 void obd_heat_decay(struct obd_heat_instance *instance, __u64 time_second,
906 unsigned int weight, unsigned int period_second)
912 if (instance->ohi_time_second > time_second) {
913 obd_heat_clear(instance, 1);
917 if (instance->ohi_time_second == 0)
920 for (second = instance->ohi_time_second + period_second;
921 second < time_second;
922 second += period_second) {
923 instance->ohi_heat = instance->ohi_heat *
924 (256 - weight) / 256 +
925 instance->ohi_count * weight / 256;
926 instance->ohi_count = 0;
927 instance->ohi_time_second = second;
931 EXPORT_SYMBOL(obd_heat_decay);
933 __u64 obd_heat_get(struct obd_heat_instance *instance, unsigned int time_second,
934 unsigned int weight, unsigned int period_second)
938 obd_heat_decay(instance, time_second, weight, period_second);
940 if (instance->ohi_count == 0)
941 RETURN(instance->ohi_heat);
943 RETURN(instance->ohi_heat * (256 - weight) / 256 +
944 instance->ohi_count * weight / 256);
946 EXPORT_SYMBOL(obd_heat_get);
948 void obd_heat_add(struct obd_heat_instance *instance,
949 unsigned int time_second, __u64 count,
950 unsigned int weight, unsigned int period_second)
954 obd_heat_decay(instance, time_second, weight, period_second);
955 if (instance->ohi_time_second == 0) {
956 instance->ohi_time_second = time_second;
957 instance->ohi_heat = 0;
958 instance->ohi_count = count;
960 instance->ohi_count += count;
964 EXPORT_SYMBOL(obd_heat_add);
966 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
967 MODULE_DESCRIPTION("Lustre Class Driver");
968 MODULE_VERSION(LUSTRE_VERSION_STRING);
969 MODULE_LICENSE("GPL");
971 module_init(obdclass_init);
972 module_exit(obdclass_exit);