Whamcloud - gitweb
LU-3963 libcfs: convert obdecho,obdclass code to atomics
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38 #ifndef __KERNEL__
39 # include <liblustre.h>
40 #else
41 # include <asm/atomic.h>
42 #endif
43
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lnet/lnetctl.h>
47 #include <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <lustre/lustre_build_version.h>
50 #include <libcfs/list.h>
51 #include <cl_object.h>
52 #ifdef HAVE_SERVER_SUPPORT
53 # include <dt_object.h>
54 # include <md_object.h>
55 #endif /* HAVE_SERVER_SUPPORT */
56 #include "llog_internal.h"
57
58 #ifndef __KERNEL__
59 /* liblustre workaround */
60 atomic_t libcfs_kmemory = {0};
61 #endif
62
63 struct obd_device *obd_devs[MAX_OBD_DEVICES];
64 EXPORT_SYMBOL(obd_devs);
65 cfs_list_t obd_types;
66 DEFINE_RWLOCK(obd_dev_lock);
67
68 __u64 obd_max_pages = 0;
69 __u64 obd_max_alloc = 0;
70 #ifndef __KERNEL__
71 __u64 obd_alloc;
72 __u64 obd_pages;
73 #endif
74 DEFINE_SPINLOCK(obd_updatemax_lock);
75
76 /* The following are visible and mutable through /proc/sys/lustre/. */
77 unsigned int obd_alloc_fail_rate = 0;
78 EXPORT_SYMBOL(obd_alloc_fail_rate);
79 unsigned int obd_debug_peer_on_timeout;
80 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
81 unsigned int obd_dump_on_timeout;
82 EXPORT_SYMBOL(obd_dump_on_timeout);
83 unsigned int obd_dump_on_eviction;
84 EXPORT_SYMBOL(obd_dump_on_eviction);
85 unsigned int obd_max_dirty_pages = 256;
86 EXPORT_SYMBOL(obd_max_dirty_pages);
87 atomic_t obd_unstable_pages;
88 EXPORT_SYMBOL(obd_unstable_pages);
89 atomic_t obd_dirty_pages;
90 EXPORT_SYMBOL(obd_dirty_pages);
91 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT;   /* seconds */
92 EXPORT_SYMBOL(obd_timeout);
93 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
94 EXPORT_SYMBOL(ldlm_timeout);
95 unsigned int obd_timeout_set;
96 EXPORT_SYMBOL(obd_timeout_set);
97 unsigned int ldlm_timeout_set;
98 EXPORT_SYMBOL(ldlm_timeout_set);
99 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
100 unsigned int at_min = 0;
101 EXPORT_SYMBOL(at_min);
102 unsigned int at_max = 600;
103 EXPORT_SYMBOL(at_max);
104 unsigned int at_history = 600;
105 EXPORT_SYMBOL(at_history);
106 int at_early_margin = 5;
107 EXPORT_SYMBOL(at_early_margin);
108 int at_extra = 30;
109 EXPORT_SYMBOL(at_extra);
110
111 atomic_t obd_dirty_transit_pages;
112 EXPORT_SYMBOL(obd_dirty_transit_pages);
113
114 char obd_jobid_var[JOBSTATS_JOBID_VAR_MAX_LEN + 1] = JOBSTATS_DISABLE;
115 EXPORT_SYMBOL(obd_jobid_var);
116
117 #ifdef LPROCFS
118 struct lprocfs_stats *obd_memory = NULL;
119 EXPORT_SYMBOL(obd_memory);
120 #endif
121
122 /* Get jobid of current process by reading the environment variable
123  * stored in between the "env_start" & "env_end" of task struct.
124  *
125  * TODO:
126  * It's better to cache the jobid for later use if there is any
127  * efficient way, the cl_env code probably could be reused for this
128  * purpose.
129  *
130  * If some job scheduler doesn't store jobid in the "env_start/end",
131  * then an upcall could be issued here to get the jobid by utilizing
132  * the userspace tools/api. Then, the jobid must be cached.
133  */
134 int lustre_get_jobid(char *jobid)
135 {
136         int jobid_len = JOBSTATS_JOBID_SIZE;
137         int rc = 0;
138         ENTRY;
139
140         memset(jobid, 0, JOBSTATS_JOBID_SIZE);
141         /* Jobstats isn't enabled */
142         if (strcmp(obd_jobid_var, JOBSTATS_DISABLE) == 0)
143                 RETURN(0);
144
145         /* Use process name + fsuid as jobid */
146         if (strcmp(obd_jobid_var, JOBSTATS_PROCNAME_UID) == 0) {
147                 snprintf(jobid, JOBSTATS_JOBID_SIZE, "%s.%u",
148                          current_comm(), current_fsuid());
149                 RETURN(0);
150         }
151
152         rc = cfs_get_environ(obd_jobid_var, jobid, &jobid_len);
153         if (rc) {
154                 if (rc == -EOVERFLOW) {
155                         /* For the PBS_JOBID and LOADL_STEP_ID keys (which are
156                          * variable length strings instead of just numbers), it
157                          * might make sense to keep the unique parts for JobID,
158                          * instead of just returning an error.  That means a
159                          * larger temp buffer for cfs_get_environ(), then
160                          * truncating the string at some separator to fit into
161                          * the specified jobid_len.  Fix later if needed. */
162                         static bool printed;
163                         if (unlikely(!printed)) {
164                                 LCONSOLE_ERROR_MSG(0x16b, "%s value too large "
165                                                    "for JobID buffer (%d)\n",
166                                                    obd_jobid_var, jobid_len);
167                                 printed = true;
168                         }
169                 } else {
170                         CDEBUG((rc == -ENOENT || rc == -EINVAL ||
171                                 rc == -EDEADLK) ? D_INFO : D_ERROR,
172                                "Get jobid for (%s) failed: rc = %d\n",
173                                obd_jobid_var, rc);
174                 }
175         }
176         RETURN(rc);
177 }
178 EXPORT_SYMBOL(lustre_get_jobid);
179
180 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
181                    size_t size, const char *file, int line)
182 {
183         if (ptr == NULL ||
184             (cfs_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
185                 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
186                        ptr ? "force " :"", type, name, (__u64)size, file,
187                        line);
188                 CERROR(LPU64" total bytes and "LPU64" total pages "
189                        "("LPU64" bytes) allocated by Lustre, "
190                        "%d total bytes by LNET\n",
191                        obd_memory_sum(),
192                        obd_pages_sum() << PAGE_CACHE_SHIFT,
193                        obd_pages_sum(),
194                         atomic_read(&libcfs_kmemory));
195                 return 1;
196         }
197         return 0;
198 }
199 EXPORT_SYMBOL(obd_alloc_fail);
200
201 static inline void obd_data2conn(struct lustre_handle *conn,
202                                  struct obd_ioctl_data *data)
203 {
204         memset(conn, 0, sizeof *conn);
205         conn->cookie = data->ioc_cookie;
206 }
207
208 static inline void obd_conn2data(struct obd_ioctl_data *data,
209                                  struct lustre_handle *conn)
210 {
211         data->ioc_cookie = conn->cookie;
212 }
213
214 int class_resolve_dev_name(__u32 len, const char *name)
215 {
216         int rc;
217         int dev;
218
219         ENTRY;
220         if (!len || !name) {
221                 CERROR("No name passed,!\n");
222                 GOTO(out, rc = -EINVAL);
223         }
224         if (name[len - 1] != 0) {
225                 CERROR("Name not nul terminated!\n");
226                 GOTO(out, rc = -EINVAL);
227         }
228
229         CDEBUG(D_IOCTL, "device name %s\n", name);
230         dev = class_name2dev(name);
231         if (dev == -1) {
232                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
233                 GOTO(out, rc = -EINVAL);
234         }
235
236         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
237         rc = dev;
238
239 out:
240         RETURN(rc);
241 }
242
243 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
244 {
245         char *buf = NULL;
246         struct obd_ioctl_data *data;
247         struct libcfs_debug_ioctl_data *debug_data;
248         struct obd_device *obd = NULL;
249         int err = 0, len = 0;
250         ENTRY;
251
252         /* only for debugging */
253         if (cmd == LIBCFS_IOC_DEBUG_MASK) {
254                 debug_data = (struct libcfs_debug_ioctl_data*)arg;
255                 libcfs_subsystem_debug = debug_data->subs;
256                 libcfs_debug = debug_data->debug;
257                 return 0;
258         }
259
260         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
261         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
262                 CERROR("OBD ioctl: data error\n");
263                 RETURN(-EINVAL);
264         }
265         data = (struct obd_ioctl_data *)buf;
266
267         switch (cmd) {
268         case OBD_IOC_PROCESS_CFG: {
269                 struct lustre_cfg *lcfg;
270
271                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
272                         CERROR("No config buffer passed!\n");
273                         GOTO(out, err = -EINVAL);
274                 }
275                 OBD_ALLOC(lcfg, data->ioc_plen1);
276                 if (lcfg == NULL)
277                         GOTO(out, err = -ENOMEM);
278                 err = copy_from_user(lcfg, data->ioc_pbuf1,
279                                          data->ioc_plen1);
280                 if (!err)
281                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
282                 if (!err)
283                         err = class_process_config(lcfg);
284
285                 OBD_FREE(lcfg, data->ioc_plen1);
286                 GOTO(out, err);
287         }
288
289         case OBD_GET_VERSION:
290                 if (!data->ioc_inlbuf1) {
291                         CERROR("No buffer passed in ioctl\n");
292                         GOTO(out, err = -EINVAL);
293                 }
294
295                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
296                         CERROR("ioctl buffer too small to hold version\n");
297                         GOTO(out, err = -EINVAL);
298                 }
299
300                 memcpy(data->ioc_bulk, BUILD_VERSION,
301                        strlen(BUILD_VERSION) + 1);
302
303                 err = obd_ioctl_popdata((void *)arg, data, len);
304                 if (err)
305                         err = -EFAULT;
306                 GOTO(out, err);
307
308         case OBD_IOC_NAME2DEV: {
309                 /* Resolve a device name.  This does not change the
310                  * currently selected device.
311                  */
312                 int dev;
313
314                 dev = class_resolve_dev_name(data->ioc_inllen1,
315                                              data->ioc_inlbuf1);
316                 data->ioc_dev = dev;
317                 if (dev < 0)
318                         GOTO(out, err = -EINVAL);
319
320                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
321                 if (err)
322                         err = -EFAULT;
323                 GOTO(out, err);
324         }
325
326         case OBD_IOC_UUID2DEV: {
327                 /* Resolve a device uuid.  This does not change the
328                  * currently selected device.
329                  */
330                 int dev;
331                 struct obd_uuid uuid;
332
333                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
334                         CERROR("No UUID passed!\n");
335                         GOTO(out, err = -EINVAL);
336                 }
337                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
338                         CERROR("UUID not NUL terminated!\n");
339                         GOTO(out, err = -EINVAL);
340                 }
341
342                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
343                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
344                 dev = class_uuid2dev(&uuid);
345                 data->ioc_dev = dev;
346                 if (dev == -1) {
347                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
348                                data->ioc_inlbuf1);
349                         GOTO(out, err = -EINVAL);
350                 }
351
352                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
353                        dev);
354                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
355                 if (err)
356                         err = -EFAULT;
357                 GOTO(out, err);
358         }
359
360         case OBD_IOC_CLOSE_UUID: {
361                 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
362                        data->ioc_inlbuf1);
363                 GOTO(out, err = 0);
364         }
365
366         case OBD_IOC_GETDEVICE: {
367                 int     index = data->ioc_count;
368                 char    *status, *str;
369
370                 if (!data->ioc_inlbuf1) {
371                         CERROR("No buffer passed in ioctl\n");
372                         GOTO(out, err = -EINVAL);
373                 }
374                 if (data->ioc_inllen1 < 128) {
375                         CERROR("ioctl buffer too small to hold version\n");
376                         GOTO(out, err = -EINVAL);
377                 }
378
379                 obd = class_num2obd(index);
380                 if (!obd)
381                         GOTO(out, err = -ENOENT);
382
383                 if (obd->obd_stopping)
384                         status = "ST";
385                 else if (obd->obd_set_up)
386                         status = "UP";
387                 else if (obd->obd_attached)
388                         status = "AT";
389                 else
390                         status = "--";
391                 str = (char *)data->ioc_bulk;
392                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
393                          (int)index, status, obd->obd_type->typ_name,
394                          obd->obd_name, obd->obd_uuid.uuid,
395                          atomic_read(&obd->obd_refcount));
396                 err = obd_ioctl_popdata((void *)arg, data, len);
397
398                 GOTO(out, err = 0);
399         }
400
401         }
402
403         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
404                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
405                         GOTO(out, err = -EINVAL);
406                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
407                         GOTO(out, err = -EINVAL);
408                 obd = class_name2obd(data->ioc_inlbuf4);
409         } else if (data->ioc_dev < class_devno_max()) {
410                 obd = class_num2obd(data->ioc_dev);
411         } else {
412                 CERROR("OBD ioctl: No device\n");
413                 GOTO(out, err = -EINVAL);
414         }
415
416         if (obd == NULL) {
417                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
418                 GOTO(out, err = -EINVAL);
419         }
420         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
421
422         if (!obd->obd_set_up || obd->obd_stopping) {
423                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
424                 GOTO(out, err = -EINVAL);
425         }
426
427         switch(cmd) {
428         case OBD_IOC_NO_TRANSNO: {
429                 if (!obd->obd_attached) {
430                         CERROR("Device %d not attached\n", obd->obd_minor);
431                         GOTO(out, err = -ENODEV);
432                 }
433                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
434                        obd->obd_name);
435                 obd->obd_no_transno = 1;
436                 GOTO(out, err = 0);
437         }
438
439         default: {
440                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
441                 if (err)
442                         GOTO(out, err);
443
444                 err = obd_ioctl_popdata((void *)arg, data, len);
445                 if (err)
446                         err = -EFAULT;
447                 GOTO(out, err);
448         }
449         }
450
451  out:
452         if (buf)
453                 obd_ioctl_freedata(buf, len);
454         RETURN(err);
455 } /* class_handle_ioctl */
456
457 #ifdef __KERNEL__
458 extern struct miscdevice obd_psdev;
459 #else
460 struct miscdevice obd_psdev;
461 #endif
462
463 #define OBD_INIT_CHECK
464 #ifdef OBD_INIT_CHECK
465 int obd_init_checks(void)
466 {
467         __u64 u64val, div64val;
468         char buf[64];
469         int len, ret = 0;
470
471         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
472
473         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
474
475         u64val = OBD_OBJECT_EOF;
476         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
477         if (u64val != OBD_OBJECT_EOF) {
478                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
479                        u64val, (int)sizeof(u64val));
480                 ret = -EINVAL;
481         }
482         len = snprintf(buf, sizeof(buf), LPX64, u64val);
483         if (len != 18) {
484                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
485                 ret = -EINVAL;
486         }
487
488         div64val = OBD_OBJECT_EOF;
489         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
490         if (u64val != OBD_OBJECT_EOF) {
491                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
492                        u64val, (int)sizeof(u64val));
493                 ret = -EOVERFLOW;
494         }
495         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
496                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
497                        u64val, (int)sizeof(u64val));
498                 return -EOVERFLOW;
499         }
500         if (do_div(div64val, 256) != (u64val & 255)) {
501                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
502                 return -EOVERFLOW;
503         }
504         if (u64val >> 8 != div64val) {
505                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
506                        u64val, div64val, u64val >> 8);
507                 return -EOVERFLOW;
508         }
509         len = snprintf(buf, sizeof(buf), LPX64, u64val);
510         if (len != 18) {
511                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
512                 ret = -EINVAL;
513         }
514         len = snprintf(buf, sizeof(buf), LPU64, u64val);
515         if (len != 20) {
516                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
517                 ret = -EINVAL;
518         }
519         len = snprintf(buf, sizeof(buf), LPD64, u64val);
520         if (len != 2) {
521                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
522                 ret = -EINVAL;
523         }
524         if ((u64val & ~CFS_PAGE_MASK) >= PAGE_CACHE_SIZE) {
525                 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
526                       (__u64)PAGE_CACHE_SIZE);
527                 ret = -EINVAL;
528         }
529
530         return ret;
531 }
532 #else
533 #define obd_init_checks() do {} while(0)
534 #endif
535
536 extern spinlock_t obd_types_lock;
537 extern int class_procfs_init(void);
538 extern int class_procfs_clean(void);
539
540 #ifdef __KERNEL__
541 static int __init init_obdclass(void)
542 #else
543 int init_obdclass(void)
544 #endif
545 {
546         int i, err;
547 #ifdef __KERNEL__
548         int lustre_register_fs(void);
549
550         for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
551                 CFS_INIT_LIST_HEAD(&capa_list[i]);
552 #endif
553
554         LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
555
556         spin_lock_init(&obd_types_lock);
557         obd_zombie_impexp_init();
558 #ifdef LPROCFS
559         obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
560                                          LPROCFS_STATS_FLAG_NONE |
561                                          LPROCFS_STATS_FLAG_IRQ_SAFE);
562         if (obd_memory == NULL) {
563                 CERROR("kmalloc of 'obd_memory' failed\n");
564                 RETURN(-ENOMEM);
565         }
566
567         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
568                              LPROCFS_CNTR_AVGMINMAX,
569                              "memused", "bytes");
570         lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
571                              LPROCFS_CNTR_AVGMINMAX,
572                              "pagesused", "pages");
573 #endif
574         err = obd_init_checks();
575         if (err == -EOVERFLOW)
576                 return err;
577
578         class_init_uuidlist();
579         err = class_handle_init();
580         if (err)
581                 return err;
582
583         CFS_INIT_LIST_HEAD(&obd_types);
584
585         err = misc_register(&obd_psdev);
586         if (err) {
587                 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
588                 return err;
589         }
590
591         /* This struct is already zeroed for us (static global) */
592         for (i = 0; i < class_devno_max(); i++)
593                 obd_devs[i] = NULL;
594
595         /* Default the dirty page cache cap to 1/2 of system memory.
596          * For clients with less memory, a larger fraction is needed
597          * for other purposes (mostly for BGL). */
598         if (totalram_pages <= 512 << (20 - PAGE_CACHE_SHIFT))
599                 obd_max_dirty_pages = totalram_pages / 4;
600         else
601                 obd_max_dirty_pages = totalram_pages / 2;
602
603         err = obd_init_caches();
604         if (err)
605                 return err;
606 #ifdef __KERNEL__
607         err = class_procfs_init();
608         if (err)
609                 return err;
610 #endif
611
612         err = lu_global_init();
613         if (err)
614                 return err;
615
616         err = lu_capainfo_init();
617         if (err)
618                 return err;
619
620         err = cl_global_init();
621         if (err != 0)
622                 return err;
623
624 #if defined(__KERNEL__) && defined(HAVE_SERVER_SUPPORT)
625         err = dt_global_init();
626         if (err != 0)
627                 return err;
628
629         err = lu_ucred_global_init();
630         if (err != 0)
631                 return err;
632 #endif
633
634         err = llog_info_init();
635         if (err)
636                 return err;
637
638 #ifdef __KERNEL__
639         err = lustre_register_fs();
640 #endif
641
642         return err;
643 }
644
645 void obd_update_maxusage(void)
646 {
647         __u64 max1, max2;
648
649         max1 = obd_pages_sum();
650         max2 = obd_memory_sum();
651
652         spin_lock(&obd_updatemax_lock);
653         if (max1 > obd_max_pages)
654                 obd_max_pages = max1;
655         if (max2 > obd_max_alloc)
656                 obd_max_alloc = max2;
657         spin_unlock(&obd_updatemax_lock);
658 }
659 EXPORT_SYMBOL(obd_update_maxusage);
660
661 #ifdef LPROCFS
662 __u64 obd_memory_max(void)
663 {
664         __u64 ret;
665
666         spin_lock(&obd_updatemax_lock);
667         ret = obd_max_alloc;
668         spin_unlock(&obd_updatemax_lock);
669
670         return ret;
671 }
672 EXPORT_SYMBOL(obd_memory_max);
673
674 __u64 obd_pages_max(void)
675 {
676         __u64 ret;
677
678         spin_lock(&obd_updatemax_lock);
679         ret = obd_max_pages;
680         spin_unlock(&obd_updatemax_lock);
681
682         return ret;
683 }
684 EXPORT_SYMBOL(obd_pages_max);
685 #endif
686
687 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
688  * ifdef to the end of the file to cover module and versioning goo.*/
689 #ifdef __KERNEL__
690 static void cleanup_obdclass(void)
691 {
692         int i;
693         int lustre_unregister_fs(void);
694         __u64 memory_leaked, pages_leaked;
695         __u64 memory_max, pages_max;
696         ENTRY;
697
698         lustre_unregister_fs();
699
700         misc_deregister(&obd_psdev);
701         for (i = 0; i < class_devno_max(); i++) {
702                 struct obd_device *obd = class_num2obd(i);
703                 if (obd && obd->obd_set_up &&
704                     OBT(obd) && OBP(obd, detach)) {
705                         /* XXX should this call generic detach otherwise? */
706                         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
707                         OBP(obd, detach)(obd);
708                 }
709         }
710         llog_info_fini();
711 #ifdef HAVE_SERVER_SUPPORT
712         lu_ucred_global_fini();
713         dt_global_fini();
714 #endif
715         cl_global_fini();
716         lu_capainfo_fini();
717         lu_global_fini();
718
719         obd_cleanup_caches();
720         obd_sysctl_clean();
721
722         class_procfs_clean();
723
724         class_handle_cleanup();
725         class_exit_uuidlist();
726         obd_zombie_impexp_stop();
727
728         memory_leaked = obd_memory_sum();
729         pages_leaked = obd_pages_sum();
730
731         memory_max = obd_memory_max();
732         pages_max = obd_pages_max();
733
734         lprocfs_free_stats(&obd_memory);
735         CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
736                "obd_memory max: "LPU64", leaked: "LPU64"\n",
737                memory_max, memory_leaked);
738         CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
739                "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
740                pages_max, pages_leaked);
741
742         EXIT;
743 }
744
745 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
746 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
747 MODULE_LICENSE("GPL");
748
749 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);
750 #endif