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