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