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"
53 #include <lustre_ioctl_old.h>
55 static __u64 obd_max_alloc;
57 static DEFINE_SPINLOCK(obd_updatemax_lock);
59 /* The following are visible and mutable through /proc/sys/lustre/. */
60 unsigned int obd_debug_peer_on_timeout;
61 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
62 unsigned int obd_dump_on_timeout;
63 EXPORT_SYMBOL(obd_dump_on_timeout);
64 unsigned int obd_dump_on_eviction;
65 EXPORT_SYMBOL(obd_dump_on_eviction);
66 unsigned int obd_lbug_on_eviction;
67 EXPORT_SYMBOL(obd_lbug_on_eviction);
68 unsigned long obd_max_dirty_pages;
69 EXPORT_SYMBOL(obd_max_dirty_pages);
70 atomic_long_t obd_dirty_pages;
71 EXPORT_SYMBOL(obd_dirty_pages);
72 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
73 EXPORT_SYMBOL(obd_timeout);
74 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
75 EXPORT_SYMBOL(ldlm_timeout);
76 unsigned int ping_interval = (OBD_TIMEOUT_DEFAULT > 4) ?
77 (OBD_TIMEOUT_DEFAULT / 4) : 1;
78 EXPORT_SYMBOL(ping_interval);
79 unsigned int ping_evict_timeout_multiplier = 6;
80 EXPORT_SYMBOL(ping_evict_timeout_multiplier);
81 unsigned int obd_timeout_set;
82 EXPORT_SYMBOL(obd_timeout_set);
83 unsigned int ldlm_timeout_set;
84 EXPORT_SYMBOL(ldlm_timeout_set);
85 /* bulk transfer timeout, give up after 100s by default */
86 unsigned int bulk_timeout = 100; /* seconds */
87 EXPORT_SYMBOL(bulk_timeout);
89 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
90 unsigned int at_min = 5;
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 struct percpu_counter obd_memory;
102 EXPORT_SYMBOL(obd_memory);
104 static int obdclass_oom_handler(struct notifier_block *self,
105 unsigned long notused, void *nfreed)
108 pr_info("obd_memory max: %llu, obd_memory current: %llu\n",
109 obd_memory_max(), obd_memory_sum());
114 static struct notifier_block obdclass_oom = {
115 .notifier_call = obdclass_oom_handler
118 int obd_ioctl_msg(const char *file, const char *func, int line, int level,
119 const char *name, unsigned int cmd, const char *msg, int rc)
121 static struct cfs_debug_limit_state cdls;
124 [_IOC_READ] = "_IOR",
125 [_IOC_WRITE] = "_IOW",
126 [_IOC_READ|_IOC_WRITE] = "_IOWR",
130 type = _IOC_TYPE(cmd);
131 __CDEBUG_WITH_LOC(file, func, line, level, &cdls,
132 "%s: iocontrol from '%s' cmd=%x %s('%c', %u, %u) %s: rc = %d\n",
133 name, current->comm, cmd,
134 dirs[_IOC_DIR(cmd)] ?: "_IO?",
135 isprint(type) ? type : '?', _IOC_NR(cmd),
136 _IOC_SIZE(cmd), msg, rc);
139 EXPORT_SYMBOL(obd_ioctl_msg);
141 static int class_resolve_dev_name(__u32 len, const char *name)
148 CERROR("No name passed,!\n");
149 GOTO(out, rc = -EINVAL);
151 if (name[len - 1] != 0) {
152 CERROR("Name not nul terminated!\n");
153 GOTO(out, rc = -EINVAL);
156 CDEBUG(D_IOCTL, "device name %s\n", name);
157 dev = class_name2dev(name);
159 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
160 GOTO(out, rc = -EINVAL);
163 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
170 #define OBD_MAX_IOCTL_BUFFER 8192
172 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
174 const int maxlen = 1 << 30;
175 if (data->ioc_len > maxlen) {
176 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
180 if (data->ioc_inllen1 > maxlen) {
181 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
185 if (data->ioc_inllen2 > maxlen) {
186 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
190 if (data->ioc_inllen3 > maxlen) {
191 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
195 if (data->ioc_inllen4 > maxlen) {
196 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
200 if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
201 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
205 if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
206 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
210 if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
211 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
215 if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
216 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
220 if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
221 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
225 if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
226 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
230 if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
231 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
235 if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
236 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
240 if (obd_ioctl_packlen(data) > data->ioc_len) {
241 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
242 obd_ioctl_packlen(data), data->ioc_len);
249 /* buffer MUST be at least the size of obd_ioctl_hdr */
250 int obd_ioctl_getdata(struct obd_ioctl_data **datap, int *len, void __user *arg)
252 struct obd_ioctl_hdr hdr;
253 struct obd_ioctl_data *data;
258 if (copy_from_user(&hdr, arg, sizeof(hdr)))
261 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
262 CERROR("%s: kernel/user version mismatch (%x != %x): rc = %d\n",
263 current->comm, OBD_IOCTL_VERSION, hdr.ioc_version, rc);
267 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
268 CERROR("%s: user buffer len %d exceeds %d max: rc = %d\n",
269 current->comm, hdr.ioc_len, OBD_MAX_IOCTL_BUFFER, rc);
273 if (hdr.ioc_len < sizeof(*data)) {
274 CERROR("%s: user buffer %d too small for ioctl %zu: rc = %d\n",
275 current->comm, hdr.ioc_len, sizeof(*data), rc);
279 /* When there are lots of processes calling vmalloc on multi-core
280 * system, the high lock contention will hurt performance badly,
281 * obdfilter-survey is an example, which relies on ioctl. So we'd
282 * better avoid vmalloc on ioctl path. LU-66
284 OBD_ALLOC_LARGE(data, hdr.ioc_len);
287 CERROR("%s: cannot allocate control buffer len %d: rc = %d\n",
288 current->comm, hdr.ioc_len, rc);
293 if (copy_from_user(data, arg, hdr.ioc_len))
294 GOTO(out_free, rc = -EFAULT);
296 if (obd_ioctl_is_invalid(data))
297 GOTO(out_free, rc = -EINVAL);
299 if (data->ioc_inllen1) {
300 data->ioc_inlbuf1 = &data->ioc_bulk[0];
301 offset += round_up(data->ioc_inllen1, 8);
304 if (data->ioc_inllen2) {
305 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
306 offset += round_up(data->ioc_inllen2, 8);
309 if (data->ioc_inllen3) {
310 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
311 offset += round_up(data->ioc_inllen3, 8);
314 if (data->ioc_inllen4)
315 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
322 OBD_FREE_LARGE(data, *len);
325 EXPORT_SYMBOL(obd_ioctl_getdata);
327 int class_handle_ioctl(unsigned int cmd, void __user *uarg)
329 struct obd_ioctl_data *data;
330 struct obd_device *obd = NULL;
334 CDEBUG(D_IOCTL, "obdclass: cmd=%x len=%u uarg=%pK\n", cmd, len, uarg);
335 if (unlikely(_IOC_TYPE(cmd) != 'f' && !OBD_IOC_BARRIER_ALLOW(cmd) &&
336 !IOC_OSC_SET_ACTIVE_ALLOW(cmd)))
337 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "unknown", -ENOTTY));
339 rc = obd_ioctl_getdata(&data, &len, uarg);
341 CERROR("%s: ioctl data error: rc = %d\n", current->comm, rc);
346 case OBD_IOC_PROCESS_CFG: {
347 struct lustre_cfg *lcfg;
349 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
350 rc = OBD_IOC_ERROR("obdclass", cmd, "no config buffer",
354 OBD_ALLOC(lcfg, data->ioc_plen1);
356 GOTO(out, rc = -ENOMEM);
357 rc = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
359 rc = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
361 rc = class_process_config(lcfg);
363 OBD_FREE(lcfg, data->ioc_plen1);
367 #ifdef OBD_GET_VERSION
368 case_OBD_IOC_DEPRECATED(OBD_GET_VERSION, "obdclass", 2, 15)
369 if (!data->ioc_inlbuf1) {
370 rc = OBD_IOC_ERROR("obdclass", cmd, "no buffer passed",
375 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
376 rc = OBD_IOC_ERROR("obdclass", cmd, "buffer too small",
381 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
382 strlen(LUSTRE_VERSION_STRING) + 1);
384 if (copy_to_user(uarg, data, len))
388 case OBD_IOC_NAME2DEV: {
389 /* Resolve device name, does not change current selected dev */
392 dev = class_resolve_dev_name(data->ioc_inllen1,
396 GOTO(out, rc = -EINVAL);
398 if (copy_to_user(uarg, data, sizeof(*data)))
403 case OBD_IOC_UUID2DEV: {
404 /* Resolve device uuid, does not change current selected dev */
405 struct obd_uuid uuid;
408 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
409 rc = OBD_IOC_ERROR("obdclass", cmd, "no UUID passed",
413 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
414 rc = OBD_IOC_ERROR("obdclass", cmd, "unterminated UUID",
419 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
420 obd_str2uuid(&uuid, data->ioc_inlbuf1);
421 dev = class_uuid2dev(&uuid);
424 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
426 GOTO(out, rc = -EINVAL);
429 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
431 if (copy_to_user(uarg, data, sizeof(*data)))
436 case OBD_IOC_GETDEVICE: {
437 int index = data->ioc_count;
440 if (!data->ioc_inlbuf1) {
441 rc = OBD_IOC_ERROR("obdclass", cmd, "no buffer passed",
445 if (data->ioc_inllen1 < 128) {
446 rc = OBD_IOC_ERROR("obdclass", cmd, "too small version",
451 obd = class_num2obd(index);
453 GOTO(out, rc = -ENOENT);
455 if (obd->obd_stopping)
457 else if (obd->obd_inactive)
459 else if (obd->obd_set_up)
461 else if (obd->obd_attached)
466 str = data->ioc_bulk;
467 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
468 index, status, obd->obd_type->typ_name,
469 obd->obd_name, obd->obd_uuid.uuid,
470 kref_read(&obd->obd_refcount));
472 if (copy_to_user(uarg, data, len))
479 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
480 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
481 GOTO(out, rc = -EINVAL);
482 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
483 GOTO(out, rc = -EINVAL);
484 obd = class_name2obd(data->ioc_inlbuf4);
486 obd = class_num2obd(data->ioc_dev);
490 rc = OBD_IOC_ERROR(data->ioc_inlbuf4, cmd, "no device found",
494 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
496 if (!obd->obd_set_up || obd->obd_stopping) {
498 CERROR("obdclass: device %d not set up: rc = %d\n",
503 rc = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
507 if (copy_to_user(uarg, data, len))
510 OBD_FREE_LARGE(data, len);
512 } /* class_handle_ioctl */
514 /* to control /dev/obd */
515 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
521 /* Allow non-root access for some limited ioctls */
522 if (!capable(CAP_SYS_ADMIN))
525 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
528 err = class_handle_ioctl(cmd, (void __user *)arg);
533 /* declare character device */
534 static const struct file_operations obd_psdev_fops = {
535 .owner = THIS_MODULE,
536 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
540 static struct miscdevice obd_psdev = {
541 .minor = MISC_DYNAMIC_MINOR,
542 .name = OBD_DEV_NAME,
543 .fops = &obd_psdev_fops,
546 #define test_string_to_size_err(value, expect, def_unit, __rc) \
551 BUILD_BUG_ON(sizeof(value) >= 23); \
552 __ret = sysfs_memparse(value, sizeof(value) - 1, &__size, def_unit); \
554 CERROR("string_helper: parsing '%s' expect rc %d != got %d\n", \
555 value, __rc, __ret); \
556 else if (!__ret && (u64)expect != __size) \
557 CERROR("string_helper: parsing '%s' expect %llu != got %llu\n",\
558 value, (u64)expect, __size); \
561 #define test_string_to_size_one(value, expect, def_unit) \
562 test_string_to_size_err(value, expect, def_unit, 0)
564 static int __init obd_init_checks(void)
566 __u64 u64val, div64val;
570 CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
572 u64val = OBD_OBJECT_EOF;
573 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
574 if (u64val != OBD_OBJECT_EOF) {
575 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
576 u64val, (int)sizeof(u64val));
579 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
581 CERROR("u64 hex wrong length, strlen(%s)=%d != 18\n", buf, len);
585 div64val = OBD_OBJECT_EOF;
586 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
587 if (u64val != OBD_OBJECT_EOF) {
588 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
589 u64val, (int)sizeof(u64val));
592 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
593 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
594 u64val, (int)sizeof(u64val));
597 if (do_div(div64val, 256) != (u64val & 255)) {
598 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
601 if (u64val >> 8 != div64val) {
602 CERROR("do_div(%#llx,256) %llu != %llu\n",
603 u64val, div64val, u64val >> 8);
606 len = snprintf(buf, sizeof(buf), "%#llx", u64val);
608 CERROR("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
611 len = snprintf(buf, sizeof(buf), "%llu", u64val);
613 CERROR("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
616 len = snprintf(buf, sizeof(buf), "%lld", u64val);
618 CERROR("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
621 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
622 CERROR("mask failed: u64val %llu >= %llu\n", u64val,
630 if (!test_string_to_size_err("256B34", 256, "B", -EINVAL)) {
631 CERROR("string_helpers: format should be number then units\n");
634 if (!test_string_to_size_err("132OpQ", 132, "B", -EINVAL)) {
635 CERROR("string_helpers: invalid units should be rejected\n");
638 if (!test_string_to_size_err("1.82B", 1, "B", -EINVAL)) {
639 CERROR("string_helpers: 'B' with '.' should be invalid\n");
642 if (test_string_to_size_one("343\n", 343, "B")) {
643 CERROR("string_helpers: should ignore newline\n");
649 /* memparse unit handling */
651 ret += test_string_to_size_one("0B", 0, "B");
652 ret += test_string_to_size_one("512B", 512, "B");
653 ret += test_string_to_size_one("1.067kB", 1067, "B");
654 ret += test_string_to_size_one("1.042KiB", 1067, "B");
655 ret += test_string_to_size_one("8", 8388608, "M");
656 ret += test_string_to_size_one("65536", 65536, "B");
657 ret += test_string_to_size_one("128", 131072, "K");
658 ret += test_string_to_size_one("1M", 1048576, "B");
659 ret += test_string_to_size_one("0.5T", 549755813888ULL, "T");
660 ret += test_string_to_size_one("256.5G", 275414777856ULL, "G");
664 /* string helper values */
665 ret += test_string_to_size_one("16", 16777216, "MiB");
666 ret += test_string_to_size_one("8.39MB", 8390000, "MiB");
667 ret += test_string_to_size_one("8.00MiB", 8388608, "MiB");
668 ret += test_string_to_size_one("256GB", 256000000000ULL, "GiB");
669 ret += test_string_to_size_one("238.731GiB", 256335459385ULL, "GiB");
674 ret += test_string_to_size_one("0.4TB", 400000000000ULL, "TiB");
675 ret += test_string_to_size_one("12.5TiB", 13743895347200ULL, "TiB");
676 ret += test_string_to_size_one("2PB", 2000000000000000ULL, "PiB");
677 ret += test_string_to_size_one("16PiB", 18014398509481984ULL, "PiB");
681 /* huge values should overflow */
682 if (!test_string_to_size_err("1000EiB", 0, "EiB", -EOVERFLOW)) {
683 CERROR("string_helpers: failed to detect binary overflow\n");
686 if (!test_string_to_size_err("1000EB", 0, "EiB", -EOVERFLOW)) {
687 CERROR("string_helpers: failed to detect decimal overflow\n");
694 static int __init obdclass_init(void)
698 LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
700 register_oom_notifier(&obdclass_oom);
702 err = libcfs_setup();
706 err = obd_init_checks();
710 err = percpu_counter_init(&obd_memory, 0, GFP_KERNEL);
712 CERROR("obdclass: initializing 'obd_memory' failed: rc = %d\n",
717 err = libcfs_kkuc_init();
719 goto cleanup_obd_memory;
721 err = obd_zombie_impexp_init();
725 err = class_handle_init();
727 goto cleanup_zombie_impexp;
729 err = misc_register(&obd_psdev);
731 CERROR("cannot register OBD miscdevice: rc = %d\n", err);
732 goto cleanup_class_handle;
735 /* Default the dirty page cache cap to 1/2 of system memory.
736 * For clients with less memory, a larger fraction is needed
737 * for other purposes (mostly for BGL). */
738 if (cfs_totalram_pages() <= 512 << (20 - PAGE_SHIFT))
739 obd_max_dirty_pages = cfs_totalram_pages() / 4;
741 obd_max_dirty_pages = cfs_totalram_pages() / 2;
743 err = obd_init_caches();
745 goto cleanup_deregister;
747 err = class_procfs_init();
751 err = lu_global_init();
753 goto cleanup_class_procfs;
755 err = cl_global_init();
757 goto cleanup_lu_global;
759 err = llog_info_init();
761 goto cleanup_cl_global;
763 #ifdef HAVE_SERVER_SUPPORT
764 err = dt_global_init();
766 goto cleanup_llog_info;
768 err = lu_ucred_global_init();
770 goto cleanup_dt_global;
771 #endif /* HAVE_SERVER_SUPPORT */
773 /* simulate a late OOM situation now to require all
774 * alloc'ed/initialized resources to be freed
776 if (CFS_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
777 /* force error to ensure module will be unloaded/cleaned */
784 #ifdef HAVE_SERVER_SUPPORT
785 lu_ucred_global_fini();
791 #endif /* HAVE_SERVER_SUPPORT */
800 cleanup_class_procfs:
801 class_procfs_clean();
804 obd_cleanup_caches();
807 misc_deregister(&obd_psdev);
809 cleanup_class_handle:
810 class_handle_cleanup();
812 cleanup_zombie_impexp:
813 obd_zombie_impexp_stop();
819 percpu_counter_destroy(&obd_memory);
821 unregister_oom_notifier(&obdclass_oom);
825 void obd_update_maxusage(void)
829 max = obd_memory_sum();
831 spin_lock(&obd_updatemax_lock);
832 if (max > obd_max_alloc)
834 spin_unlock(&obd_updatemax_lock);
836 EXPORT_SYMBOL(obd_update_maxusage);
838 __u64 obd_memory_max(void)
842 obd_update_maxusage();
843 spin_lock(&obd_updatemax_lock);
845 spin_unlock(&obd_updatemax_lock);
849 EXPORT_SYMBOL(obd_memory_max);
851 static void __exit obdclass_exit(void)
857 misc_deregister(&obd_psdev);
858 #ifdef HAVE_SERVER_SUPPORT
859 lu_ucred_global_fini();
861 #endif /* HAVE_SERVER_SUPPORT */
866 obd_cleanup_caches();
868 class_procfs_clean();
870 class_handle_cleanup();
871 class_del_uuid(NULL); /* Delete all UUIDs. */
872 obd_zombie_impexp_stop();
875 memory_leaked = obd_memory_sum();
876 memory_max = obd_memory_max();
878 percpu_counter_destroy(&obd_memory);
879 /* the below message is checked in test-framework.sh check_mem_leak() */
880 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
881 "obd_memory max: %llu, leaked: %llu\n",
882 memory_max, memory_leaked);
884 unregister_oom_notifier(&obdclass_oom);
889 void obd_heat_clear(struct obd_heat_instance *instance, int count)
893 memset(instance, 0, sizeof(*instance) * count);
896 EXPORT_SYMBOL(obd_heat_clear);
899 * The file heat is calculated for every time interval period I. The access
900 * frequency during each period is counted. The file heat is only recalculated
901 * at the end of a time period. And a percentage of the former file heat is
902 * lost when recalculated. The recursion formula to calculate the heat of the
903 * file f is as follow:
905 * Hi+1(f) = (1-P)*Hi(f)+ P*Ci
907 * Where Hi is the heat value in the period between time points i*I and
908 * (i+1)*I; Ci is the access count in the period; the symbol P refers to the
909 * weight of Ci. The larger the value the value of P is, the more influence Ci
910 * has on the file heat.
912 void obd_heat_decay(struct obd_heat_instance *instance, __u64 time_second,
913 unsigned int weight, unsigned int period_second)
919 if (instance->ohi_time_second > time_second) {
920 obd_heat_clear(instance, 1);
924 if (instance->ohi_time_second == 0)
927 for (second = instance->ohi_time_second + period_second;
928 second < time_second;
929 second += period_second) {
930 instance->ohi_heat = instance->ohi_heat *
931 (256 - weight) / 256 +
932 instance->ohi_count * weight / 256;
933 instance->ohi_count = 0;
934 instance->ohi_time_second = second;
938 EXPORT_SYMBOL(obd_heat_decay);
940 __u64 obd_heat_get(struct obd_heat_instance *instance, unsigned int time_second,
941 unsigned int weight, unsigned int period_second)
945 obd_heat_decay(instance, time_second, weight, period_second);
947 if (instance->ohi_count == 0)
948 RETURN(instance->ohi_heat);
950 RETURN(instance->ohi_heat * (256 - weight) / 256 +
951 instance->ohi_count * weight / 256);
953 EXPORT_SYMBOL(obd_heat_get);
955 void obd_heat_add(struct obd_heat_instance *instance,
956 unsigned int time_second, __u64 count,
957 unsigned int weight, unsigned int period_second)
961 obd_heat_decay(instance, time_second, weight, period_second);
962 if (instance->ohi_time_second == 0) {
963 instance->ohi_time_second = time_second;
964 instance->ohi_heat = 0;
965 instance->ohi_count = count;
967 instance->ohi_count += count;
971 EXPORT_SYMBOL(obd_heat_add);
973 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
974 MODULE_DESCRIPTION("Lustre Class Driver");
975 MODULE_VERSION(LUSTRE_VERSION_STRING);
976 MODULE_LICENSE("GPL");
978 module_init(obdclass_init);
979 module_exit(obdclass_exit);