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