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