Whamcloud - gitweb
431a42d27e2fe4752f0104a107792cf884bf40dc
[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_inactive)
435                         status = "IN";
436                 else if (obd->obd_set_up)
437                         status = "UP";
438                 else if (obd->obd_attached)
439                         status = "AT";
440                 else
441                         status = "--";
442
443                 str = (char *)data->ioc_bulk;
444                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
445                          (int)index, status, obd->obd_type->typ_name,
446                          obd->obd_name, obd->obd_uuid.uuid,
447                          atomic_read(&obd->obd_refcount));
448
449                 if (copy_to_user((void __user *)arg, data, len))
450                         err = -EFAULT;
451
452                 GOTO(out, err);
453         }
454
455         }
456
457         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
458                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
459                         GOTO(out, err = -EINVAL);
460                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
461                         GOTO(out, err = -EINVAL);
462                 obd = class_name2obd(data->ioc_inlbuf4);
463         } else if (data->ioc_dev < class_devno_max()) {
464                 obd = class_num2obd(data->ioc_dev);
465         } else {
466                 CERROR("OBD ioctl: No device\n");
467                 GOTO(out, err = -EINVAL);
468         }
469
470         if (obd == NULL) {
471                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
472                 GOTO(out, err = -EINVAL);
473         }
474         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
475
476         if (!obd->obd_set_up || obd->obd_stopping) {
477                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
478                 GOTO(out, err = -EINVAL);
479         }
480
481         switch(cmd) {
482         case OBD_IOC_NO_TRANSNO: {
483                 if (!obd->obd_attached) {
484                         CERROR("Device %d not attached\n", obd->obd_minor);
485                         GOTO(out, err = -ENODEV);
486                 }
487                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
488                        obd->obd_name);
489                 obd->obd_no_transno = 1;
490                 GOTO(out, err = 0);
491         }
492
493         default: {
494                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
495                 if (err)
496                         GOTO(out, err);
497
498                 if (copy_to_user((void __user *)arg, data, len))
499                         err = -EFAULT;
500                 GOTO(out, err);
501         }
502         }
503
504 out:
505         OBD_FREE_LARGE(buf, len);
506         RETURN(err);
507 } /* class_handle_ioctl */
508
509 /*  opening /dev/obd */
510 static int obd_class_open(struct inode * inode, struct file * file)
511 {
512         ENTRY;
513         try_module_get(THIS_MODULE);
514         RETURN(0);
515 }
516
517 /*  closing /dev/obd */
518 static int obd_class_release(struct inode * inode, struct file * file)
519 {
520         ENTRY;
521
522         module_put(THIS_MODULE);
523         RETURN(0);
524 }
525
526 /* to control /dev/obd */
527 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
528                             unsigned long arg)
529 {
530         int err = 0;
531
532         ENTRY;
533         /* Allow non-root access for some limited ioctls */
534         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
535                 RETURN(err = -EACCES);
536
537         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
538                 RETURN(err = -ENOTTY);
539
540         err = class_handle_ioctl(cmd, (unsigned long)arg);
541
542         RETURN(err);
543 }
544
545 /* declare character device */
546 static struct file_operations obd_psdev_fops = {
547         .owner          = THIS_MODULE,
548         .unlocked_ioctl = obd_class_ioctl,      /* unlocked_ioctl */
549         .open           = obd_class_open,       /* open */
550         .release        = obd_class_release,    /* release */
551 };
552
553 /* modules setup */
554 struct miscdevice obd_psdev = {
555         .minor  = MISC_DYNAMIC_MINOR,
556         .name   = OBD_DEV_NAME,
557         .fops   = &obd_psdev_fops,
558 };
559
560 static int obd_init_checks(void)
561 {
562         __u64 u64val, div64val;
563         char buf[64];
564         int len, ret = 0;
565
566         CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
567
568         u64val = OBD_OBJECT_EOF;
569         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
570         if (u64val != OBD_OBJECT_EOF) {
571                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
572                        u64val, (int)sizeof(u64val));
573                 ret = -EINVAL;
574         }
575         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
576         if (len != 18) {
577                 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
578                 ret = -EINVAL;
579         }
580
581         div64val = OBD_OBJECT_EOF;
582         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
583         if (u64val != OBD_OBJECT_EOF) {
584                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
585                        u64val, (int)sizeof(u64val));
586                 ret = -EOVERFLOW;
587         }
588         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
589                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
590                        u64val, (int)sizeof(u64val));
591                 return -EOVERFLOW;
592         }
593         if (do_div(div64val, 256) != (u64val & 255)) {
594                 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
595                 return -EOVERFLOW;
596         }
597         if (u64val >> 8 != div64val) {
598                 CERROR("do_div(%#llx,256) %llu != %llu\n",
599                        u64val, div64val, u64val >> 8);
600                 return -EOVERFLOW;
601         }
602         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
603         if (len != 18) {
604                 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
605                 ret = -EINVAL;
606         }
607         len = snprintf(buf, sizeof(buf), "%llu", u64val);
608         if (len != 20) {
609                 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
610                 ret = -EINVAL;
611         }
612         len = snprintf(buf, sizeof(buf), "%lld", u64val);
613         if (len != 2) {
614                 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
615                 ret = -EINVAL;
616         }
617         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
618                 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
619                       (__u64)PAGE_SIZE);
620                 ret = -EINVAL;
621         }
622
623         return ret;
624 }
625
626 static int __init obdclass_init(void)
627 {
628         int err;
629
630         LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
631
632         libcfs_kkuc_init();
633
634         err = obd_init_checks();
635         if (err == -EOVERFLOW)
636                 return err;
637
638 #ifdef CONFIG_PROC_FS
639         obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
640                                          LPROCFS_STATS_FLAG_NONE |
641                                          LPROCFS_STATS_FLAG_IRQ_SAFE);
642         if (obd_memory == NULL) {
643                 CERROR("kmalloc of 'obd_memory' failed\n");
644                 return -ENOMEM;
645         }
646
647         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
648                              LPROCFS_CNTR_AVGMINMAX,
649                              "memused", "bytes");
650 #endif
651         err = obd_zombie_impexp_init();
652         if (err)
653                 goto cleanup_obd_memory;
654
655         err = class_handle_init();
656         if (err)
657                 goto cleanup_zombie_impexp;
658
659         err = misc_register(&obd_psdev);
660         if (err) {
661                 CERROR("cannot register OBD miscdevice: err = %d\n", err);
662                 goto cleanup_class_handle;
663         }
664
665         /* Default the dirty page cache cap to 1/2 of system memory.
666          * For clients with less memory, a larger fraction is needed
667          * for other purposes (mostly for BGL). */
668         if (totalram_pages <= 512 << (20 - PAGE_SHIFT))
669                 obd_max_dirty_pages = totalram_pages / 4;
670         else
671                 obd_max_dirty_pages = totalram_pages / 2;
672
673         err = obd_init_caches();
674         if (err)
675                 goto cleanup_deregister;
676
677         err = class_procfs_init();
678         if (err)
679                 goto cleanup_caches;
680
681         err = lu_global_init();
682         if (err)
683                 goto cleanup_class_procfs;
684
685         err = cl_global_init();
686         if (err != 0)
687                 goto cleanup_lu_global;
688
689 #ifdef HAVE_SERVER_SUPPORT
690         err = dt_global_init();
691         if (err != 0)
692                 goto cleanup_cl_global;
693
694         err = lu_ucred_global_init();
695         if (err != 0)
696                 goto cleanup_dt_global;
697 #endif /* HAVE_SERVER_SUPPORT */
698
699         err = llog_info_init();
700         if (err)
701 #ifdef HAVE_SERVER_SUPPORT
702                 goto cleanup_lu_ucred_global;
703 #else /* !HAVE_SERVER_SUPPORT */
704                 goto cleanup_cl_global;
705 #endif /* HAVE_SERVER_SUPPORT */
706
707         err = lustre_register_fs();
708
709         /* simulate a late OOM situation now to require all
710          * alloc'ed/initialized resources to be freed */
711         if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
712                 /* fake error but filesystem has been registered */
713                 lustre_unregister_fs();
714                 /* force error to ensure module will be unloaded/cleaned */
715                 err = -ENOMEM;
716         }
717
718         if (err)
719                 goto cleanup_llog_info;
720
721         return 0;
722
723 cleanup_llog_info:
724         llog_info_fini();
725
726 #ifdef HAVE_SERVER_SUPPORT
727 cleanup_lu_ucred_global:
728         lu_ucred_global_fini();
729
730 cleanup_dt_global:
731         dt_global_fini();
732 #endif /* HAVE_SERVER_SUPPORT */
733
734 cleanup_cl_global:
735         cl_global_fini();
736
737 cleanup_lu_global:
738         lu_global_fini();
739
740 cleanup_class_procfs:
741         class_procfs_clean();
742
743 cleanup_caches:
744         obd_cleanup_caches();
745
746 cleanup_deregister:
747         misc_deregister(&obd_psdev);
748
749 cleanup_class_handle:
750         class_handle_cleanup();
751
752 cleanup_zombie_impexp:
753         obd_zombie_impexp_stop();
754
755 cleanup_obd_memory:
756 #ifdef CONFIG_PROC_FS
757         lprocfs_free_stats(&obd_memory);
758 #endif
759
760         return err;
761 }
762
763 void obd_update_maxusage(void)
764 {
765         __u64 max;
766
767         max = obd_memory_sum();
768
769         spin_lock(&obd_updatemax_lock);
770         if (max > obd_max_alloc)
771                 obd_max_alloc = max;
772         spin_unlock(&obd_updatemax_lock);
773 }
774 EXPORT_SYMBOL(obd_update_maxusage);
775
776 #ifdef CONFIG_PROC_FS
777 __u64 obd_memory_max(void)
778 {
779         __u64 ret;
780
781         obd_update_maxusage();
782         spin_lock(&obd_updatemax_lock);
783         ret = obd_max_alloc;
784         spin_unlock(&obd_updatemax_lock);
785
786         return ret;
787 }
788 #endif /* CONFIG_PROC_FS */
789
790 static void __exit obdclass_exit(void)
791 {
792 #ifdef CONFIG_PROC_FS
793         __u64 memory_leaked;
794         __u64 memory_max;
795 #endif /* CONFIG_PROC_FS */
796         ENTRY;
797
798         lustre_unregister_fs();
799
800         misc_deregister(&obd_psdev);
801         llog_info_fini();
802 #ifdef HAVE_SERVER_SUPPORT
803         lu_ucred_global_fini();
804         dt_global_fini();
805 #endif /* HAVE_SERVER_SUPPORT */
806         cl_global_fini();
807         lu_global_fini();
808
809         obd_cleanup_caches();
810
811         class_procfs_clean();
812
813         class_handle_cleanup();
814         class_del_uuid(NULL); /* Delete all UUIDs. */
815         obd_zombie_impexp_stop();
816
817 #ifdef CONFIG_PROC_FS
818         memory_leaked = obd_memory_sum();
819         memory_max = obd_memory_max();
820
821         lprocfs_free_stats(&obd_memory);
822         CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
823                "obd_memory max: %llu, leaked: %llu\n",
824                memory_max, memory_leaked);
825 #endif /* CONFIG_PROC_FS */
826
827         EXIT;
828 }
829
830 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
831 MODULE_DESCRIPTION("Lustre Class Driver");
832 MODULE_VERSION(LUSTRE_VERSION_STRING);
833 MODULE_LICENSE("GPL");
834
835 module_init(obdclass_init);
836 module_exit(obdclass_exit);