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