Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / obdclass / class_obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_CLASS
33
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>
40
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
54 #ifdef CONFIG_PROC_FS
55 static __u64 obd_max_alloc;
56 #else
57 __u64 obd_max_alloc;
58 #endif
59
60 static DEFINE_SPINLOCK(obd_updatemax_lock);
61
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 obd_timeout_set;
80 EXPORT_SYMBOL(obd_timeout_set);
81 unsigned int ldlm_timeout_set;
82 EXPORT_SYMBOL(ldlm_timeout_set);
83 /* bulk transfer timeout, give up after 100s by default */
84 unsigned int bulk_timeout = 100; /* seconds */
85 EXPORT_SYMBOL(bulk_timeout);
86 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
87 unsigned int at_min = 0;
88 EXPORT_SYMBOL(at_min);
89 unsigned int at_max = 600;
90 EXPORT_SYMBOL(at_max);
91 unsigned int at_history = 600;
92 EXPORT_SYMBOL(at_history);
93 int at_early_margin = 5;
94 EXPORT_SYMBOL(at_early_margin);
95 int at_extra = 30;
96 EXPORT_SYMBOL(at_extra);
97
98 #ifdef CONFIG_PROC_FS
99 struct lprocfs_stats *obd_memory = NULL;
100 EXPORT_SYMBOL(obd_memory);
101 #endif
102
103 static int obdclass_oom_handler(struct notifier_block *self,
104                                 unsigned long notused, void *nfreed)
105 {
106 #ifdef CONFIG_PROC_FS
107         /* in bytes */
108         pr_info("obd_memory max: %llu, obd_memory current: %llu\n",
109                 obd_memory_max(), obd_memory_sum());
110 #endif /* CONFIG_PROC_FS */
111
112         return NOTIFY_OK;
113 }
114
115 static struct notifier_block obdclass_oom = {
116         .notifier_call = obdclass_oom_handler
117 };
118
119 static int class_resolve_dev_name(__u32 len, const char *name)
120 {
121         int rc;
122         int dev;
123
124         ENTRY;
125         if (!len || !name) {
126                 CERROR("No name passed,!\n");
127                 GOTO(out, rc = -EINVAL);
128         }
129         if (name[len - 1] != 0) {
130                 CERROR("Name not nul terminated!\n");
131                 GOTO(out, rc = -EINVAL);
132         }
133
134         CDEBUG(D_IOCTL, "device name %s\n", name);
135         dev = class_name2dev(name);
136         if (dev == -1) {
137                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
138                 GOTO(out, rc = -EINVAL);
139         }
140
141         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
142         rc = dev;
143
144 out:
145         RETURN(rc);
146 }
147
148 #define OBD_MAX_IOCTL_BUFFER    8192
149
150 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
151 {
152         const int maxlen = 1 << 30;
153         if (data->ioc_len > maxlen) {
154                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
155                 return 1;
156         }
157
158         if (data->ioc_inllen1 > maxlen) {
159                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
160                 return 1;
161         }
162
163         if (data->ioc_inllen2 > maxlen) {
164                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
165                 return 1;
166         }
167
168         if (data->ioc_inllen3 > maxlen) {
169                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
170                 return 1;
171         }
172
173         if (data->ioc_inllen4 > maxlen) {
174                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
175                 return 1;
176         }
177
178         if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
179                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
180                 return 1;
181         }
182
183         if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
184                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
185                 return 1;
186         }
187
188         if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
189                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
190                 return 1;
191         }
192
193         if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
194                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
195                 return 1;
196         }
197
198         if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
199                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
200                 return 1;
201         }
202
203         if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
204                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
205                 return 1;
206         }
207
208         if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
209                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
210                 return 1;
211         }
212
213         if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
214                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
215                 return 1;
216         }
217
218         if (obd_ioctl_packlen(data) > data->ioc_len) {
219                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
220                        obd_ioctl_packlen(data), data->ioc_len);
221                 return 1;
222         }
223
224         return 0;
225 }
226
227 /* buffer MUST be at least the size of obd_ioctl_hdr */
228 int obd_ioctl_getdata(struct obd_ioctl_data **datap, int *len, void __user *arg)
229 {
230         struct obd_ioctl_hdr hdr;
231         struct obd_ioctl_data *data;
232         int offset = 0;
233
234         ENTRY;
235         if (copy_from_user(&hdr, arg, sizeof(hdr)))
236                 RETURN(-EFAULT);
237
238         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
239                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
240                        OBD_IOCTL_VERSION, hdr.ioc_version);
241                 RETURN(-EINVAL);
242         }
243
244         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
245                 CERROR("User buffer len %d exceeds %d max buffer\n",
246                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
247                 RETURN(-EINVAL);
248         }
249
250         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
251                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
252                 RETURN(-EINVAL);
253         }
254
255         /* When there are lots of processes calling vmalloc on multi-core
256          * system, the high lock contention will hurt performance badly,
257          * obdfilter-survey is an example, which relies on ioctl. So we'd
258          * better avoid vmalloc on ioctl path. LU-66
259          */
260         OBD_ALLOC_LARGE(data, hdr.ioc_len);
261         if (!data) {
262                 CERROR("Cannot allocate control buffer of len %d\n",
263                        hdr.ioc_len);
264                 RETURN(-EINVAL);
265         }
266         *len = hdr.ioc_len;
267
268         if (copy_from_user(data, arg, hdr.ioc_len)) {
269                 OBD_FREE_LARGE(data, hdr.ioc_len);
270                 RETURN(-EFAULT);
271         }
272
273         if (obd_ioctl_is_invalid(data)) {
274                 CERROR("ioctl not correctly formatted\n");
275                 OBD_FREE_LARGE(data, hdr.ioc_len);
276                 RETURN(-EINVAL);
277         }
278
279         if (data->ioc_inllen1) {
280                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
281                 offset += cfs_size_round(data->ioc_inllen1);
282         }
283
284         if (data->ioc_inllen2) {
285                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
286                 offset += cfs_size_round(data->ioc_inllen2);
287         }
288
289         if (data->ioc_inllen3) {
290                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
291                 offset += cfs_size_round(data->ioc_inllen3);
292         }
293
294         if (data->ioc_inllen4)
295                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
296
297         *datap = data;
298
299         RETURN(0);
300 }
301 EXPORT_SYMBOL(obd_ioctl_getdata);
302
303 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
304 {
305         struct obd_ioctl_data *data;
306         struct obd_device *obd = NULL;
307         int err = 0, len = 0;
308
309         ENTRY;
310         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
311         if (obd_ioctl_getdata(&data, &len, (void __user *)arg)) {
312                 CERROR("OBD ioctl: data error\n");
313                 RETURN(-EINVAL);
314         }
315
316         switch (cmd) {
317         case OBD_IOC_PROCESS_CFG: {
318                 struct lustre_cfg *lcfg;
319
320                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
321                         CERROR("No config buffer passed!\n");
322                         GOTO(out, err = -EINVAL);
323                 }
324                 OBD_ALLOC(lcfg, data->ioc_plen1);
325                 if (lcfg == NULL)
326                         GOTO(out, err = -ENOMEM);
327                 err = copy_from_user(lcfg, data->ioc_pbuf1,
328                                          data->ioc_plen1);
329                 if (!err)
330                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
331                 if (!err)
332                         err = class_process_config(lcfg);
333
334                 OBD_FREE(lcfg, data->ioc_plen1);
335                 GOTO(out, err);
336         }
337
338 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
339         case OBD_GET_VERSION: {
340                 static bool warned;
341
342                 if (!data->ioc_inlbuf1) {
343                         CERROR("No buffer passed in ioctl\n");
344                         GOTO(out, err = -EINVAL);
345                 }
346
347                 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
348                         CERROR("ioctl buffer too small to hold version\n");
349                         GOTO(out, err = -EINVAL);
350                 }
351
352                 if (!warned) {
353                         warned = true;
354                         CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
355                               "use llapi_get_version_string() and/or relink\n",
356                               current->comm);
357                 }
358                 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
359                        strlen(LUSTRE_VERSION_STRING) + 1);
360
361                 if (copy_to_user((void __user *)arg, data, len))
362                         err = -EFAULT;
363                 GOTO(out, err);
364         }
365 #endif
366         case OBD_IOC_NAME2DEV: {
367                 /* Resolve a device name.  This does not change the
368                  * currently selected device.
369                  */
370                 int dev;
371
372                 dev = class_resolve_dev_name(data->ioc_inllen1,
373                                              data->ioc_inlbuf1);
374                 data->ioc_dev = dev;
375                 if (dev < 0)
376                         GOTO(out, err = -EINVAL);
377
378                 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
379                         err = -EFAULT;
380                 GOTO(out, err);
381         }
382
383         case OBD_IOC_UUID2DEV: {
384                 /* Resolve a device uuid.  This does not change the
385                  * currently selected device.
386                  */
387                 int dev;
388                 struct obd_uuid uuid;
389
390                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
391                         CERROR("No UUID passed!\n");
392                         GOTO(out, err = -EINVAL);
393                 }
394                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
395                         CERROR("UUID not NUL terminated!\n");
396                         GOTO(out, err = -EINVAL);
397                 }
398
399                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
400                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
401                 dev = class_uuid2dev(&uuid);
402                 data->ioc_dev = dev;
403                 if (dev == -1) {
404                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
405                                data->ioc_inlbuf1);
406                         GOTO(out, err = -EINVAL);
407                 }
408
409                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
410                        dev);
411                 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
412                         err = -EFAULT;
413                 GOTO(out, err);
414         }
415
416         case OBD_IOC_GETDEVICE: {
417                 int     index = data->ioc_count;
418                 char    *status, *str;
419
420                 if (!data->ioc_inlbuf1) {
421                         CERROR("No buffer passed in ioctl\n");
422                         GOTO(out, err = -EINVAL);
423                 }
424                 if (data->ioc_inllen1 < 128) {
425                         CERROR("ioctl buffer too small to hold version\n");
426                         GOTO(out, err = -EINVAL);
427                 }
428
429                 obd = class_num2obd(index);
430                 if (!obd)
431                         GOTO(out, err = -ENOENT);
432
433                 if (obd->obd_stopping)
434                         status = "ST";
435                 else if (obd->obd_inactive)
436                         status = "IN";
437                 else if (obd->obd_set_up)
438                         status = "UP";
439                 else if (obd->obd_attached)
440                         status = "AT";
441                 else
442                         status = "--";
443
444                 str = (char *)data->ioc_bulk;
445                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
446                          (int)index, status, obd->obd_type->typ_name,
447                          obd->obd_name, obd->obd_uuid.uuid,
448                          atomic_read(&obd->obd_refcount));
449
450                 if (copy_to_user((void __user *)arg, data, len))
451                         err = -EFAULT;
452
453                 GOTO(out, err);
454         }
455
456         }
457
458         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
459                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
460                         GOTO(out, err = -EINVAL);
461                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
462                         GOTO(out, err = -EINVAL);
463                 obd = class_name2obd(data->ioc_inlbuf4);
464         } else if (data->ioc_dev < class_devno_max()) {
465                 obd = class_num2obd(data->ioc_dev);
466         } else {
467                 CERROR("OBD ioctl: No device\n");
468                 GOTO(out, err = -EINVAL);
469         }
470
471         if (obd == NULL) {
472                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
473                 GOTO(out, err = -EINVAL);
474         }
475         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
476
477         if (!obd->obd_set_up || obd->obd_stopping) {
478                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
479                 GOTO(out, err = -EINVAL);
480         }
481
482         err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
483         if (err)
484                 GOTO(out, err);
485
486         if (copy_to_user((void __user *)arg, data, len))
487                 err = -EFAULT;
488 out:
489         OBD_FREE_LARGE(data, len);
490         RETURN(err);
491 } /* class_handle_ioctl */
492
493 /* to control /dev/obd */
494 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
495                             unsigned long arg)
496 {
497         int err = 0;
498
499         ENTRY;
500         /* Allow non-root access for some limited ioctls */
501         if (!capable(CAP_SYS_ADMIN))
502                 RETURN(err = -EACCES);
503
504         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
505                 RETURN(err = -ENOTTY);
506
507         err = class_handle_ioctl(cmd, (unsigned long)arg);
508
509         RETURN(err);
510 }
511
512 /* declare character device */
513 static const struct file_operations obd_psdev_fops = {
514         .owner          = THIS_MODULE,
515         .unlocked_ioctl = obd_class_ioctl,      /* unlocked_ioctl */
516 };
517
518 /* modules setup */
519 static struct miscdevice obd_psdev = {
520         .minor  = MISC_DYNAMIC_MINOR,
521         .name   = OBD_DEV_NAME,
522         .fops   = &obd_psdev_fops,
523 };
524
525 #define test_string_to_size_err(value, expect, def_unit, __rc)                 \
526 ({                                                                             \
527         u64 __size;                                                            \
528         int __ret;                                                             \
529                                                                                \
530         BUILD_BUG_ON(sizeof(value) >= 23);                                     \
531         __ret = sysfs_memparse(value, sizeof(value) - 1, &__size, def_unit);   \
532         if (__ret != __rc)                                                     \
533                 CERROR("string_helper: parsing '%s' expect rc %d != got %d\n", \
534                        value, __rc, __ret);                                    \
535         else if (!__ret && (u64)expect != __size)                              \
536                 CERROR("string_helper: parsing '%s' expect %llu != got %llu\n",\
537                        value, (u64)expect, __size);                            \
538         __ret;                                                                 \
539 })
540 #define test_string_to_size_one(value, expect, def_unit)                       \
541         test_string_to_size_err(value, expect, def_unit, 0)
542
543 static int __init obd_init_checks(void)
544 {
545         __u64 u64val, div64val;
546         char buf[64];
547         int len, ret = 0;
548
549         CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
550
551         u64val = OBD_OBJECT_EOF;
552         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
553         if (u64val != OBD_OBJECT_EOF) {
554                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
555                        u64val, (int)sizeof(u64val));
556                 ret = -EINVAL;
557         }
558         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
559         if (len != 18) {
560                 CERROR("u64 hex wrong length, strlen(%s)=%d != 18\n", buf, len);
561                 ret = -EINVAL;
562         }
563
564         div64val = OBD_OBJECT_EOF;
565         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
566         if (u64val != OBD_OBJECT_EOF) {
567                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
568                        u64val, (int)sizeof(u64val));
569                 ret = -EOVERFLOW;
570         }
571         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
572                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
573                        u64val, (int)sizeof(u64val));
574                 ret = -EOVERFLOW;
575         }
576         if (do_div(div64val, 256) != (u64val & 255)) {
577                 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
578                 ret = -EOVERFLOW;
579         }
580         if (u64val >> 8 != div64val) {
581                 CERROR("do_div(%#llx,256) %llu != %llu\n",
582                        u64val, div64val, u64val >> 8);
583                 ret = -EOVERFLOW;
584         }
585         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
586         if (len != 18) {
587                 CERROR("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
588                 ret = -EINVAL;
589         }
590         len = snprintf(buf, sizeof(buf), "%llu", u64val);
591         if (len != 20) {
592                 CERROR("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
593                 ret = -EINVAL;
594         }
595         len = snprintf(buf, sizeof(buf), "%lld", u64val);
596         if (len != 2) {
597                 CERROR("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
598                 ret = -EINVAL;
599         }
600         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
601                 CERROR("mask failed: u64val %llu >= %llu\n", u64val,
602                        (__u64)PAGE_SIZE);
603                 ret = -EINVAL;
604         }
605         if (ret)
606                 RETURN(ret);
607
608         /* invalid string */
609         if (!test_string_to_size_err("256B34", 256, "B", -EINVAL)) {
610                 CERROR("string_helpers: format should be number then units\n");
611                 ret = -EINVAL;
612         }
613         if (!test_string_to_size_err("132OpQ", 132, "B", -EINVAL)) {
614                 CERROR("string_helpers: invalid units should be rejected\n");
615                 ret = -EINVAL;
616         }
617         if (!test_string_to_size_err("1.82B", 1, "B", -EINVAL)) {
618                 CERROR("string_helpers: 'B' with '.' should be invalid\n");
619                 ret = -EINVAL;
620         }
621         if (test_string_to_size_one("343\n", 343, "B")) {
622                 CERROR("string_helpers: should ignore newline\n");
623                 ret = -EINVAL;
624         }
625         if (ret)
626                 RETURN(ret);
627
628         /* memparse unit handling */
629         ret = 0;
630         ret += test_string_to_size_one("0B", 0, "B");
631         ret += test_string_to_size_one("512B", 512, "B");
632         ret += test_string_to_size_one("1.067kB", 1067, "B");
633         ret += test_string_to_size_one("1.042KiB", 1067, "B");
634         ret += test_string_to_size_one("8", 8388608, "M");
635         ret += test_string_to_size_one("65536", 65536, "B");
636         ret += test_string_to_size_one("128", 131072, "K");
637         ret += test_string_to_size_one("1M", 1048576, "B");
638         ret += test_string_to_size_one("0.5T", 549755813888ULL, "T");
639         ret += test_string_to_size_one("256.5G", 275414777856ULL, "G");
640         if (ret)
641                 RETURN(ret);
642
643         /* string helper values */
644         ret += test_string_to_size_one("16", 16777216, "MiB");
645         ret += test_string_to_size_one("8.39MB", 8390000, "MiB");
646         ret += test_string_to_size_one("8.00MiB", 8388608, "MiB");
647         ret += test_string_to_size_one("256GB", 256000000000ULL, "GiB");
648         ret += test_string_to_size_one("238.731GiB", 256335459385ULL, "GiB");
649         if (ret)
650                 RETURN(ret);
651
652         /* huge values */
653         ret += test_string_to_size_one("0.4TB", 400000000000ULL, "TiB");
654         ret += test_string_to_size_one("12.5TiB", 13743895347200ULL, "TiB");
655         ret += test_string_to_size_one("2PB", 2000000000000000ULL, "PiB");
656         ret += test_string_to_size_one("16PiB", 18014398509481984ULL, "PiB");
657         if (ret)
658                 RETURN(ret);
659
660         /* huge values should overflow */
661         if (!test_string_to_size_err("1000EiB", 0, "EiB", -EOVERFLOW)) {
662                 CERROR("string_helpers: failed to detect binary overflow\n");
663                 ret = -EINVAL;
664         }
665         if (!test_string_to_size_err("1000EB", 0, "EiB", -EOVERFLOW)) {
666                 CERROR("string_helpers: failed to detect decimal overflow\n");
667                 ret = -EINVAL;
668         }
669
670         return ret;
671 }
672
673 static int __init obdclass_init(void)
674 {
675         int err;
676
677         LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
678
679         register_oom_notifier(&obdclass_oom);
680
681         libcfs_kkuc_init();
682
683         err = obd_init_checks();
684         if (err)
685                 return err;
686
687 #ifdef CONFIG_PROC_FS
688         obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
689                                          LPROCFS_STATS_FLAG_NONE |
690                                          LPROCFS_STATS_FLAG_IRQ_SAFE);
691         if (obd_memory == NULL) {
692                 CERROR("kmalloc of 'obd_memory' failed\n");
693                 return -ENOMEM;
694         }
695
696         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
697                              LPROCFS_CNTR_AVGMINMAX,
698                              "memused", "bytes");
699 #endif
700         err = obd_zombie_impexp_init();
701         if (err)
702                 goto cleanup_obd_memory;
703
704         err = class_handle_init();
705         if (err)
706                 goto cleanup_zombie_impexp;
707
708         err = misc_register(&obd_psdev);
709         if (err) {
710                 CERROR("cannot register OBD miscdevice: err = %d\n", err);
711                 goto cleanup_class_handle;
712         }
713
714         /* Default the dirty page cache cap to 1/2 of system memory.
715          * For clients with less memory, a larger fraction is needed
716          * for other purposes (mostly for BGL). */
717         if (cfs_totalram_pages() <= 512 << (20 - PAGE_SHIFT))
718                 obd_max_dirty_pages = cfs_totalram_pages() / 4;
719         else
720                 obd_max_dirty_pages = cfs_totalram_pages() / 2;
721
722         err = obd_init_caches();
723         if (err)
724                 goto cleanup_deregister;
725
726         err = class_procfs_init();
727         if (err)
728                 goto cleanup_caches;
729
730         err = lu_global_init();
731         if (err)
732                 goto cleanup_class_procfs;
733
734         err = cl_global_init();
735         if (err != 0)
736                 goto cleanup_lu_global;
737
738         err = llog_info_init();
739         if (err)
740                 goto cleanup_cl_global;
741
742 #ifdef HAVE_SERVER_SUPPORT
743         err = dt_global_init();
744         if (err != 0)
745                 goto cleanup_llog_info;
746
747         err = lu_ucred_global_init();
748         if (err != 0)
749                 goto cleanup_dt_global;
750
751         err = lustre_tgt_register_fs();
752         if (err && err != -EBUSY) {
753                 /* Don't fail if server code also registers "lustre_tgt" */
754                 CERROR("obdclass: register fstype 'lustre_tgt' failed: rc = %d\n",
755                        err);
756                 goto cleanup_lu_ucred_global;
757         }
758 #endif /* HAVE_SERVER_SUPPORT */
759
760         /* simulate a late OOM situation now to require all
761          * alloc'ed/initialized resources to be freed
762          */
763         if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
764                 /* force error to ensure module will be unloaded/cleaned */
765                 err = -ENOMEM;
766                 goto cleanup_all;
767         }
768         return 0;
769
770 cleanup_all:
771 #ifdef HAVE_SERVER_SUPPORT
772         /* fake error but filesystem has been registered */
773         lustre_tgt_unregister_fs();
774
775 cleanup_lu_ucred_global:
776         lu_ucred_global_fini();
777
778 cleanup_dt_global:
779         dt_global_fini();
780
781 cleanup_llog_info:
782 #endif /* HAVE_SERVER_SUPPORT */
783         llog_info_fini();
784
785 cleanup_cl_global:
786         cl_global_fini();
787
788 cleanup_lu_global:
789         lu_global_fini();
790
791 cleanup_class_procfs:
792         class_procfs_clean();
793
794 cleanup_caches:
795         obd_cleanup_caches();
796
797 cleanup_deregister:
798         misc_deregister(&obd_psdev);
799
800 cleanup_class_handle:
801         class_handle_cleanup();
802
803 cleanup_zombie_impexp:
804         obd_zombie_impexp_stop();
805
806 cleanup_obd_memory:
807 #ifdef CONFIG_PROC_FS
808         lprocfs_free_stats(&obd_memory);
809 #endif
810
811         unregister_oom_notifier(&obdclass_oom);
812         return err;
813 }
814
815 void obd_update_maxusage(void)
816 {
817         __u64 max;
818
819         max = obd_memory_sum();
820
821         spin_lock(&obd_updatemax_lock);
822         if (max > obd_max_alloc)
823                 obd_max_alloc = max;
824         spin_unlock(&obd_updatemax_lock);
825 }
826 EXPORT_SYMBOL(obd_update_maxusage);
827
828 #ifdef CONFIG_PROC_FS
829 __u64 obd_memory_max(void)
830 {
831         __u64 ret;
832
833         obd_update_maxusage();
834         spin_lock(&obd_updatemax_lock);
835         ret = obd_max_alloc;
836         spin_unlock(&obd_updatemax_lock);
837
838         return ret;
839 }
840 #endif /* CONFIG_PROC_FS */
841
842 static void __exit obdclass_exit(void)
843 {
844 #ifdef CONFIG_PROC_FS
845         __u64 memory_leaked;
846         __u64 memory_max;
847 #endif /* CONFIG_PROC_FS */
848         ENTRY;
849
850         misc_deregister(&obd_psdev);
851 #ifdef HAVE_SERVER_SUPPORT
852         lustre_tgt_unregister_fs();
853         lu_ucred_global_fini();
854         dt_global_fini();
855 #endif /* HAVE_SERVER_SUPPORT */
856         llog_info_fini();
857         cl_global_fini();
858         lu_global_fini();
859
860         obd_cleanup_caches();
861
862         class_procfs_clean();
863
864         class_handle_cleanup();
865         class_del_uuid(NULL); /* Delete all UUIDs. */
866         obd_zombie_impexp_stop();
867
868 #ifdef CONFIG_PROC_FS
869         memory_leaked = obd_memory_sum();
870         memory_max = obd_memory_max();
871
872         lprocfs_free_stats(&obd_memory);
873         /* the below message is checked in test-framework.sh check_mem_leak() */
874         CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
875                "obd_memory max: %llu, leaked: %llu\n",
876                memory_max, memory_leaked);
877 #endif /* CONFIG_PROC_FS */
878
879         unregister_oom_notifier(&obdclass_oom);
880
881         EXIT;
882 }
883
884 void obd_heat_clear(struct obd_heat_instance *instance, int count)
885 {
886         ENTRY;
887
888         memset(instance, 0, sizeof(*instance) * count);
889         RETURN_EXIT;
890 }
891 EXPORT_SYMBOL(obd_heat_clear);
892
893 /*
894  * The file heat is calculated for every time interval period I. The access
895  * frequency during each period is counted. The file heat is only recalculated
896  * at the end of a time period.  And a percentage of the former file heat is
897  * lost when recalculated. The recursion formula to calculate the heat of the
898  * file f is as follow:
899  *
900  * Hi+1(f) = (1-P)*Hi(f)+ P*Ci
901  *
902  * Where Hi is the heat value in the period between time points i*I and
903  * (i+1)*I; Ci is the access count in the period; the symbol P refers to the
904  * weight of Ci. The larger the value the value of P is, the more influence Ci
905  * has on the file heat.
906  */
907 void obd_heat_decay(struct obd_heat_instance *instance,  __u64 time_second,
908                     unsigned int weight, unsigned int period_second)
909 {
910         u64 second;
911
912         ENTRY;
913
914         if (instance->ohi_time_second > time_second) {
915                 obd_heat_clear(instance, 1);
916                 RETURN_EXIT;
917         }
918
919         if (instance->ohi_time_second == 0)
920                 RETURN_EXIT;
921
922         for (second = instance->ohi_time_second + period_second;
923              second < time_second;
924              second += period_second) {
925                 instance->ohi_heat = instance->ohi_heat *
926                                 (256 - weight) / 256 +
927                                 instance->ohi_count * weight / 256;
928                 instance->ohi_count = 0;
929                 instance->ohi_time_second = second;
930         }
931         RETURN_EXIT;
932 }
933 EXPORT_SYMBOL(obd_heat_decay);
934
935 __u64 obd_heat_get(struct obd_heat_instance *instance, unsigned int time_second,
936                    unsigned int weight, unsigned int period_second)
937 {
938         ENTRY;
939
940         obd_heat_decay(instance, time_second, weight, period_second);
941
942         if (instance->ohi_count == 0)
943                 RETURN(instance->ohi_heat);
944
945         RETURN(instance->ohi_heat * (256 - weight) / 256 +
946                instance->ohi_count * weight / 256);
947 }
948 EXPORT_SYMBOL(obd_heat_get);
949
950 void obd_heat_add(struct obd_heat_instance *instance,
951                   unsigned int time_second,  __u64 count,
952                   unsigned int weight, unsigned int period_second)
953 {
954         ENTRY;
955
956         obd_heat_decay(instance, time_second, weight, period_second);
957         if (instance->ohi_time_second == 0) {
958                 instance->ohi_time_second = time_second;
959                 instance->ohi_heat = 0;
960                 instance->ohi_count = count;
961         } else {
962                 instance->ohi_count += count;
963         }
964         RETURN_EXIT;
965 }
966 EXPORT_SYMBOL(obd_heat_add);
967
968 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
969 MODULE_DESCRIPTION("Lustre Class Driver");
970 MODULE_VERSION(LUSTRE_VERSION_STRING);
971 MODULE_LICENSE("GPL");
972
973 module_init(obdclass_init);
974 module_exit(obdclass_exit);