Whamcloud - gitweb
LU-9679 obdclass: don't manage module refs in open/close.
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_CLASS
34
35 #include <linux/miscdevice.h>
36 #include <linux/user_namespace.h>
37 #ifdef HAVE_UIDGID_HEADER
38 # include <linux/uidgid.h>
39 #endif
40 #include <linux/atomic.h>
41 #include <linux/list.h>
42
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <uapi/linux/lnet/lnetctl.h>
46 #include <lustre_debug.h>
47 #include <lustre_kernelcomm.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #ifdef HAVE_SERVER_SUPPORT
51 # include <dt_object.h>
52 # include <md_object.h>
53 #endif /* HAVE_SERVER_SUPPORT */
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include "llog_internal.h"
56
57 #ifdef CONFIG_PROC_FS
58 static __u64 obd_max_alloc;
59 #else
60 __u64 obd_max_alloc;
61 #endif
62
63 static DEFINE_SPINLOCK(obd_updatemax_lock);
64
65 /* The following are visible and mutable through /proc/sys/lustre/. */
66 unsigned int obd_debug_peer_on_timeout;
67 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
68 unsigned int obd_dump_on_timeout;
69 EXPORT_SYMBOL(obd_dump_on_timeout);
70 unsigned int obd_dump_on_eviction;
71 EXPORT_SYMBOL(obd_dump_on_eviction);
72 unsigned int obd_lbug_on_eviction;
73 EXPORT_SYMBOL(obd_lbug_on_eviction);
74 unsigned long obd_max_dirty_pages;
75 EXPORT_SYMBOL(obd_max_dirty_pages);
76 atomic_long_t obd_dirty_pages;
77 EXPORT_SYMBOL(obd_dirty_pages);
78 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT;   /* seconds */
79 EXPORT_SYMBOL(obd_timeout);
80 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
81 EXPORT_SYMBOL(ldlm_timeout);
82 unsigned int obd_timeout_set;
83 EXPORT_SYMBOL(obd_timeout_set);
84 unsigned int ldlm_timeout_set;
85 EXPORT_SYMBOL(ldlm_timeout_set);
86 /* bulk transfer timeout, give up after 100s by default */
87 unsigned int bulk_timeout = 100; /* seconds */
88 EXPORT_SYMBOL(bulk_timeout);
89 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
90 unsigned int at_min = 0;
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 #ifdef CONFIG_PROC_FS
102 struct lprocfs_stats *obd_memory = NULL;
103 EXPORT_SYMBOL(obd_memory);
104 #endif
105
106 static int class_resolve_dev_name(__u32 len, const char *name)
107 {
108         int rc;
109         int dev;
110
111         ENTRY;
112         if (!len || !name) {
113                 CERROR("No name passed,!\n");
114                 GOTO(out, rc = -EINVAL);
115         }
116         if (name[len - 1] != 0) {
117                 CERROR("Name not nul terminated!\n");
118                 GOTO(out, rc = -EINVAL);
119         }
120
121         CDEBUG(D_IOCTL, "device name %s\n", name);
122         dev = class_name2dev(name);
123         if (dev == -1) {
124                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
125                 GOTO(out, rc = -EINVAL);
126         }
127
128         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
129         rc = dev;
130
131 out:
132         RETURN(rc);
133 }
134
135 #define OBD_MAX_IOCTL_BUFFER    8192
136
137 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
138 {
139         if (data->ioc_len > BIT(30)) {
140                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
141                 return 1;
142         }
143
144         if (data->ioc_inllen1 > BIT(30)) {
145                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
146                 return 1;
147         }
148
149         if (data->ioc_inllen2 > BIT(30)) {
150                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
151                 return 1;
152         }
153
154         if (data->ioc_inllen3 > BIT(30)) {
155                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
156                 return 1;
157         }
158
159         if (data->ioc_inllen4 > BIT(30)) {
160                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
161                 return 1;
162         }
163
164         if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
165                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
166                 return 1;
167         }
168
169         if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
170                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
171                 return 1;
172         }
173
174         if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
175                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
176                 return 1;
177         }
178
179         if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
180                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
181                 return 1;
182         }
183
184         if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
185                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
186                 return 1;
187         }
188
189         if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
190                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
191                 return 1;
192         }
193
194         if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
195                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
196                 return 1;
197         }
198
199         if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
200                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
201                 return 1;
202         }
203
204         if (obd_ioctl_packlen(data) > data->ioc_len) {
205                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
206                        obd_ioctl_packlen(data), data->ioc_len);
207                 return 1;
208         }
209
210         return 0;
211 }
212
213 /* buffer MUST be at least the size of obd_ioctl_hdr */
214 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
215 {
216         struct obd_ioctl_hdr hdr;
217         struct obd_ioctl_data *data;
218         int offset = 0;
219
220         ENTRY;
221         if (copy_from_user(&hdr, arg, sizeof(hdr)))
222                 RETURN(-EFAULT);
223
224         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
225                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
226                        OBD_IOCTL_VERSION, hdr.ioc_version);
227                 RETURN(-EINVAL);
228         }
229
230         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
231                 CERROR("User buffer len %d exceeds %d max buffer\n",
232                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
233                 RETURN(-EINVAL);
234         }
235
236         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
237                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
238                 RETURN(-EINVAL);
239         }
240
241         /* When there are lots of processes calling vmalloc on multi-core
242          * system, the high lock contention will hurt performance badly,
243          * obdfilter-survey is an example, which relies on ioctl. So we'd
244          * better avoid vmalloc on ioctl path. LU-66
245          */
246         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
247         if (!*buf) {
248                 CERROR("Cannot allocate control buffer of len %d\n",
249                        hdr.ioc_len);
250                 RETURN(-EINVAL);
251         }
252         *len = hdr.ioc_len;
253         data = (struct obd_ioctl_data *)*buf;
254
255         if (copy_from_user(*buf, arg, hdr.ioc_len)) {
256                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
257                 RETURN(-EFAULT);
258         }
259
260         if (obd_ioctl_is_invalid(data)) {
261                 CERROR("ioctl not correctly formatted\n");
262                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
263                 RETURN(-EINVAL);
264         }
265
266         if (data->ioc_inllen1) {
267                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
268                 offset += cfs_size_round(data->ioc_inllen1);
269         }
270
271         if (data->ioc_inllen2) {
272                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
273                 offset += cfs_size_round(data->ioc_inllen2);
274         }
275
276         if (data->ioc_inllen3) {
277                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
278                 offset += cfs_size_round(data->ioc_inllen3);
279         }
280
281         if (data->ioc_inllen4)
282                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
283
284         RETURN(0);
285 }
286 EXPORT_SYMBOL(obd_ioctl_getdata);
287
288 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
289 {
290         char *buf = NULL;
291         struct obd_ioctl_data *data;
292         struct obd_device *obd = NULL;
293         int err = 0, len = 0;
294
295         ENTRY;
296         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
297         if (obd_ioctl_getdata(&buf, &len, (void __user *)arg)) {
298                 CERROR("OBD ioctl: data error\n");
299                 RETURN(-EINVAL);
300         }
301         data = (struct obd_ioctl_data *)buf;
302
303         switch (cmd) {
304         case OBD_IOC_PROCESS_CFG: {
305                 struct lustre_cfg *lcfg;
306
307                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
308                         CERROR("No config buffer passed!\n");
309                         GOTO(out, err = -EINVAL);
310                 }
311                 OBD_ALLOC(lcfg, data->ioc_plen1);
312                 if (lcfg == NULL)
313                         GOTO(out, err = -ENOMEM);
314                 err = copy_from_user(lcfg, data->ioc_pbuf1,
315                                          data->ioc_plen1);
316                 if (!err)
317                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
318                 if (!err)
319                         err = class_process_config(lcfg);
320
321                 OBD_FREE(lcfg, data->ioc_plen1);
322                 GOTO(out, err);
323         }
324
325 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
326         case OBD_GET_VERSION: {
327                 static bool warned;
328
329                 if (!data->ioc_inlbuf1) {
330                         CERROR("No buffer passed in ioctl\n");
331                         GOTO(out, err = -EINVAL);
332                 }
333
334                 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
335                         CERROR("ioctl buffer too small to hold version\n");
336                         GOTO(out, err = -EINVAL);
337                 }
338
339                 if (!warned) {
340                         warned = true;
341                         CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
342                               "use llapi_get_version_string() and/or relink\n",
343                               current->comm);
344                 }
345                 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
346                        strlen(LUSTRE_VERSION_STRING) + 1);
347
348                 if (copy_to_user((void __user *)arg, data, len))
349                         err = -EFAULT;
350                 GOTO(out, err);
351         }
352 #endif
353         case OBD_IOC_NAME2DEV: {
354                 /* Resolve a device name.  This does not change the
355                  * currently selected device.
356                  */
357                 int dev;
358
359                 dev = class_resolve_dev_name(data->ioc_inllen1,
360                                              data->ioc_inlbuf1);
361                 data->ioc_dev = dev;
362                 if (dev < 0)
363                         GOTO(out, err = -EINVAL);
364
365                 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
366                         err = -EFAULT;
367                 GOTO(out, err);
368         }
369
370         case OBD_IOC_UUID2DEV: {
371                 /* Resolve a device uuid.  This does not change the
372                  * currently selected device.
373                  */
374                 int dev;
375                 struct obd_uuid uuid;
376
377                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
378                         CERROR("No UUID passed!\n");
379                         GOTO(out, err = -EINVAL);
380                 }
381                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
382                         CERROR("UUID not NUL terminated!\n");
383                         GOTO(out, err = -EINVAL);
384                 }
385
386                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
387                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
388                 dev = class_uuid2dev(&uuid);
389                 data->ioc_dev = dev;
390                 if (dev == -1) {
391                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
392                                data->ioc_inlbuf1);
393                         GOTO(out, err = -EINVAL);
394                 }
395
396                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
397                        dev);
398                 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
399                         err = -EFAULT;
400                 GOTO(out, err);
401         }
402
403         case OBD_IOC_GETDEVICE: {
404                 int     index = data->ioc_count;
405                 char    *status, *str;
406
407                 if (!data->ioc_inlbuf1) {
408                         CERROR("No buffer passed in ioctl\n");
409                         GOTO(out, err = -EINVAL);
410                 }
411                 if (data->ioc_inllen1 < 128) {
412                         CERROR("ioctl buffer too small to hold version\n");
413                         GOTO(out, err = -EINVAL);
414                 }
415
416                 obd = class_num2obd(index);
417                 if (!obd)
418                         GOTO(out, err = -ENOENT);
419
420                 if (obd->obd_stopping)
421                         status = "ST";
422                 else if (obd->obd_inactive)
423                         status = "IN";
424                 else if (obd->obd_set_up)
425                         status = "UP";
426                 else if (obd->obd_attached)
427                         status = "AT";
428                 else
429                         status = "--";
430
431                 str = (char *)data->ioc_bulk;
432                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
433                          (int)index, status, obd->obd_type->typ_name,
434                          obd->obd_name, obd->obd_uuid.uuid,
435                          atomic_read(&obd->obd_refcount));
436
437                 if (copy_to_user((void __user *)arg, data, len))
438                         err = -EFAULT;
439
440                 GOTO(out, err);
441         }
442
443         }
444
445         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
446                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
447                         GOTO(out, err = -EINVAL);
448                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
449                         GOTO(out, err = -EINVAL);
450                 obd = class_name2obd(data->ioc_inlbuf4);
451         } else if (data->ioc_dev < class_devno_max()) {
452                 obd = class_num2obd(data->ioc_dev);
453         } else {
454                 CERROR("OBD ioctl: No device\n");
455                 GOTO(out, err = -EINVAL);
456         }
457
458         if (obd == NULL) {
459                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
460                 GOTO(out, err = -EINVAL);
461         }
462         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
463
464         if (!obd->obd_set_up || obd->obd_stopping) {
465                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
466                 GOTO(out, err = -EINVAL);
467         }
468
469         switch(cmd) {
470         case OBD_IOC_NO_TRANSNO: {
471                 if (!obd->obd_attached) {
472                         CERROR("Device %d not attached\n", obd->obd_minor);
473                         GOTO(out, err = -ENODEV);
474                 }
475                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
476                        obd->obd_name);
477                 obd->obd_no_transno = 1;
478                 GOTO(out, err = 0);
479         }
480
481         default: {
482                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
483                 if (err)
484                         GOTO(out, err);
485
486                 if (copy_to_user((void __user *)arg, data, len))
487                         err = -EFAULT;
488                 GOTO(out, err);
489         }
490         }
491
492 out:
493         OBD_FREE_LARGE(buf, len);
494         RETURN(err);
495 } /* class_handle_ioctl */
496
497 /* to control /dev/obd */
498 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
499                             unsigned long arg)
500 {
501         int err = 0;
502
503         ENTRY;
504         /* Allow non-root access for some limited ioctls */
505         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
506                 RETURN(err = -EACCES);
507
508         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
509                 RETURN(err = -ENOTTY);
510
511         err = class_handle_ioctl(cmd, (unsigned long)arg);
512
513         RETURN(err);
514 }
515
516 /* declare character device */
517 static struct file_operations obd_psdev_fops = {
518         .owner          = THIS_MODULE,
519         .unlocked_ioctl = obd_class_ioctl,      /* unlocked_ioctl */
520 };
521
522 /* modules setup */
523 struct miscdevice obd_psdev = {
524         .minor  = MISC_DYNAMIC_MINOR,
525         .name   = OBD_DEV_NAME,
526         .fops   = &obd_psdev_fops,
527 };
528
529 static int obd_init_checks(void)
530 {
531         __u64 u64val, div64val;
532         char buf[64];
533         int len, ret = 0;
534
535         CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
536
537         u64val = OBD_OBJECT_EOF;
538         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
539         if (u64val != OBD_OBJECT_EOF) {
540                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
541                        u64val, (int)sizeof(u64val));
542                 ret = -EINVAL;
543         }
544         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
545         if (len != 18) {
546                 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
547                 ret = -EINVAL;
548         }
549
550         div64val = OBD_OBJECT_EOF;
551         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
552         if (u64val != OBD_OBJECT_EOF) {
553                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
554                        u64val, (int)sizeof(u64val));
555                 ret = -EOVERFLOW;
556         }
557         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
558                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
559                        u64val, (int)sizeof(u64val));
560                 return -EOVERFLOW;
561         }
562         if (do_div(div64val, 256) != (u64val & 255)) {
563                 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
564                 return -EOVERFLOW;
565         }
566         if (u64val >> 8 != div64val) {
567                 CERROR("do_div(%#llx,256) %llu != %llu\n",
568                        u64val, div64val, u64val >> 8);
569                 return -EOVERFLOW;
570         }
571         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
572         if (len != 18) {
573                 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
574                 ret = -EINVAL;
575         }
576         len = snprintf(buf, sizeof(buf), "%llu", u64val);
577         if (len != 20) {
578                 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
579                 ret = -EINVAL;
580         }
581         len = snprintf(buf, sizeof(buf), "%lld", u64val);
582         if (len != 2) {
583                 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
584                 ret = -EINVAL;
585         }
586         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
587                 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
588                       (__u64)PAGE_SIZE);
589                 ret = -EINVAL;
590         }
591
592         return ret;
593 }
594
595 static int __init obdclass_init(void)
596 {
597         int err;
598
599         LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
600
601         libcfs_kkuc_init();
602
603         err = obd_init_checks();
604         if (err == -EOVERFLOW)
605                 return err;
606
607 #ifdef CONFIG_PROC_FS
608         obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
609                                          LPROCFS_STATS_FLAG_NONE |
610                                          LPROCFS_STATS_FLAG_IRQ_SAFE);
611         if (obd_memory == NULL) {
612                 CERROR("kmalloc of 'obd_memory' failed\n");
613                 return -ENOMEM;
614         }
615
616         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
617                              LPROCFS_CNTR_AVGMINMAX,
618                              "memused", "bytes");
619 #endif
620         err = obd_zombie_impexp_init();
621         if (err)
622                 goto cleanup_obd_memory;
623
624         err = class_handle_init();
625         if (err)
626                 goto cleanup_zombie_impexp;
627
628         err = misc_register(&obd_psdev);
629         if (err) {
630                 CERROR("cannot register OBD miscdevice: err = %d\n", err);
631                 goto cleanup_class_handle;
632         }
633
634         /* Default the dirty page cache cap to 1/2 of system memory.
635          * For clients with less memory, a larger fraction is needed
636          * for other purposes (mostly for BGL). */
637         if (cfs_totalram_pages() <= 512 << (20 - PAGE_SHIFT))
638                 obd_max_dirty_pages = cfs_totalram_pages() / 4;
639         else
640                 obd_max_dirty_pages = cfs_totalram_pages() / 2;
641
642         err = obd_init_caches();
643         if (err)
644                 goto cleanup_deregister;
645
646         err = class_procfs_init();
647         if (err)
648                 goto cleanup_caches;
649
650         err = lu_global_init();
651         if (err)
652                 goto cleanup_class_procfs;
653
654         err = cl_global_init();
655         if (err != 0)
656                 goto cleanup_lu_global;
657
658 #ifdef HAVE_SERVER_SUPPORT
659         err = dt_global_init();
660         if (err != 0)
661                 goto cleanup_cl_global;
662
663         err = lu_ucred_global_init();
664         if (err != 0)
665                 goto cleanup_dt_global;
666 #endif /* HAVE_SERVER_SUPPORT */
667
668         err = llog_info_init();
669         if (err)
670 #ifdef HAVE_SERVER_SUPPORT
671                 goto cleanup_lu_ucred_global;
672 #else /* !HAVE_SERVER_SUPPORT */
673                 goto cleanup_cl_global;
674 #endif /* HAVE_SERVER_SUPPORT */
675
676         err = lustre_register_fs();
677
678         /* simulate a late OOM situation now to require all
679          * alloc'ed/initialized resources to be freed */
680         if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
681                 /* fake error but filesystem has been registered */
682                 lustre_unregister_fs();
683                 /* force error to ensure module will be unloaded/cleaned */
684                 err = -ENOMEM;
685         }
686
687         if (err)
688                 goto cleanup_llog_info;
689
690         return 0;
691
692 cleanup_llog_info:
693         llog_info_fini();
694
695 #ifdef HAVE_SERVER_SUPPORT
696 cleanup_lu_ucred_global:
697         lu_ucred_global_fini();
698
699 cleanup_dt_global:
700         dt_global_fini();
701 #endif /* HAVE_SERVER_SUPPORT */
702
703 cleanup_cl_global:
704         cl_global_fini();
705
706 cleanup_lu_global:
707         lu_global_fini();
708
709 cleanup_class_procfs:
710         class_procfs_clean();
711
712 cleanup_caches:
713         obd_cleanup_caches();
714
715 cleanup_deregister:
716         misc_deregister(&obd_psdev);
717
718 cleanup_class_handle:
719         class_handle_cleanup();
720
721 cleanup_zombie_impexp:
722         obd_zombie_impexp_stop();
723
724 cleanup_obd_memory:
725 #ifdef CONFIG_PROC_FS
726         lprocfs_free_stats(&obd_memory);
727 #endif
728
729         return err;
730 }
731
732 void obd_update_maxusage(void)
733 {
734         __u64 max;
735
736         max = obd_memory_sum();
737
738         spin_lock(&obd_updatemax_lock);
739         if (max > obd_max_alloc)
740                 obd_max_alloc = max;
741         spin_unlock(&obd_updatemax_lock);
742 }
743 EXPORT_SYMBOL(obd_update_maxusage);
744
745 #ifdef CONFIG_PROC_FS
746 __u64 obd_memory_max(void)
747 {
748         __u64 ret;
749
750         obd_update_maxusage();
751         spin_lock(&obd_updatemax_lock);
752         ret = obd_max_alloc;
753         spin_unlock(&obd_updatemax_lock);
754
755         return ret;
756 }
757 #endif /* CONFIG_PROC_FS */
758
759 static void __exit obdclass_exit(void)
760 {
761 #ifdef CONFIG_PROC_FS
762         __u64 memory_leaked;
763         __u64 memory_max;
764 #endif /* CONFIG_PROC_FS */
765         ENTRY;
766
767         lustre_unregister_fs();
768
769         misc_deregister(&obd_psdev);
770         llog_info_fini();
771 #ifdef HAVE_SERVER_SUPPORT
772         lu_ucred_global_fini();
773         dt_global_fini();
774 #endif /* HAVE_SERVER_SUPPORT */
775         cl_global_fini();
776         lu_global_fini();
777
778         obd_cleanup_caches();
779
780         class_procfs_clean();
781
782         class_handle_cleanup();
783         class_del_uuid(NULL); /* Delete all UUIDs. */
784         obd_zombie_impexp_stop();
785
786 #ifdef CONFIG_PROC_FS
787         memory_leaked = obd_memory_sum();
788         memory_max = obd_memory_max();
789
790         lprocfs_free_stats(&obd_memory);
791         /* the below message is checked in test-framework.sh check_mem_leak() */
792         CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
793                "obd_memory max: %llu, leaked: %llu\n",
794                memory_max, memory_leaked);
795 #endif /* CONFIG_PROC_FS */
796
797         EXIT;
798 }
799
800 void obd_heat_clear(struct obd_heat_instance *instance, int count)
801 {
802         ENTRY;
803
804         memset(instance, 0, sizeof(*instance) * count);
805         RETURN_EXIT;
806 }
807 EXPORT_SYMBOL(obd_heat_clear);
808
809 /*
810  * The file heat is calculated for every time interval period I. The access
811  * frequency during each period is counted. The file heat is only recalculated
812  * at the end of a time period.  And a percentage of the former file heat is
813  * lost when recalculated. The recursion formula to calculate the heat of the
814  * file f is as follow:
815  *
816  * Hi+1(f) = (1-P)*Hi(f)+ P*Ci
817  *
818  * Where Hi is the heat value in the period between time points i*I and
819  * (i+1)*I; Ci is the access count in the period; the symbol P refers to the
820  * weight of Ci. The larger the value the value of P is, the more influence Ci
821  * has on the file heat.
822  */
823 void obd_heat_decay(struct obd_heat_instance *instance,  __u64 time_second,
824                     unsigned int weight, unsigned int period_second)
825 {
826         u64 second;
827
828         ENTRY;
829
830         if (instance->ohi_time_second > time_second) {
831                 obd_heat_clear(instance, 1);
832                 RETURN_EXIT;
833         }
834
835         if (instance->ohi_time_second == 0)
836                 RETURN_EXIT;
837
838         for (second = instance->ohi_time_second + period_second;
839              second < time_second;
840              second += period_second) {
841                 instance->ohi_heat = instance->ohi_heat *
842                                 (256 - weight) / 256 +
843                                 instance->ohi_count * weight / 256;
844                 instance->ohi_count = 0;
845                 instance->ohi_time_second = second;
846         }
847         RETURN_EXIT;
848 }
849 EXPORT_SYMBOL(obd_heat_decay);
850
851 __u64 obd_heat_get(struct obd_heat_instance *instance, unsigned int time_second,
852                    unsigned int weight, unsigned int period_second)
853 {
854         ENTRY;
855
856         obd_heat_decay(instance, time_second, weight, period_second);
857
858         if (instance->ohi_count == 0)
859                 RETURN(instance->ohi_heat);
860
861         RETURN(instance->ohi_heat * (256 - weight) / 256 +
862                instance->ohi_count * weight / 256);
863 }
864 EXPORT_SYMBOL(obd_heat_get);
865
866 void obd_heat_add(struct obd_heat_instance *instance,
867                   unsigned int time_second,  __u64 count,
868                   unsigned int weight, unsigned int period_second)
869 {
870         ENTRY;
871
872         obd_heat_decay(instance, time_second, weight, period_second);
873         if (instance->ohi_time_second == 0) {
874                 instance->ohi_time_second = time_second;
875                 instance->ohi_heat = 0;
876                 instance->ohi_count = count;
877         } else {
878                 instance->ohi_count += count;
879         }
880         RETURN_EXIT;
881 }
882 EXPORT_SYMBOL(obd_heat_add);
883
884 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
885 MODULE_DESCRIPTION("Lustre Class Driver");
886 MODULE_VERSION(LUSTRE_VERSION_STRING);
887 MODULE_LICENSE("GPL");
888
889 module_init(obdclass_init);
890 module_exit(obdclass_exit);