Whamcloud - gitweb
Branch b1_4
[fs/lustre-release.git] / lustre / obdclass / class_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Object Devices Class Driver
5  *
6  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * These are the only exported functions, they provide some generic
24  * infrastructure for managing object devices
25  */
26
27 #define DEBUG_SUBSYSTEM S_CLASS
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #ifdef __KERNEL__
32 #include <linux/config.h> /* for CONFIG_PROC_FS */
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/sched.h>
38 #include <linux/lp.h>
39 #include <linux/slab.h>
40 #include <linux/ioport.h>
41 #include <linux/fcntl.h>
42 #include <linux/delay.h>
43 #include <linux/skbuff.h>
44 #include <linux/proc_fs.h>
45 #include <linux/fs.h>
46 #include <linux/poll.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <linux/highmem.h>
50 #include <asm/io.h>
51 #include <asm/ioctls.h>
52 #include <asm/system.h>
53 #include <asm/poll.h>
54 #include <asm/uaccess.h>
55 #include <linux/miscdevice.h>
56 #include <linux/smp_lock.h>
57 #include <linux/seq_file.h>
58 #else
59 # include <liblustre.h>
60 #endif
61
62 #include <linux/obd_support.h>
63 #include <linux/obd_class.h>
64 #include <linux/lustre_debug.h>
65 #include <linux/lprocfs_status.h>
66 #ifdef __KERNEL__
67 #include <linux/lustre_build_version.h>
68 #include <linux/lustre_version.h>
69 #endif
70 #include <libcfs/list.h>
71 #include "llog_internal.h"
72
73 #ifndef __KERNEL__
74 /* liblustre workaround */
75 atomic_t portal_kmemory = {0};
76 #endif
77
78 struct obd_device obd_dev[MAX_OBD_DEVICES];
79 struct list_head obd_types;
80 spinlock_t obd_dev_lock;
81 #ifndef __KERNEL__
82 atomic_t obd_memory;
83 int obd_memmax;
84 #endif
85
86 int proc_version;
87
88 /* The following are visible and mutable through /proc/sys/lustre/. */
89 unsigned int obd_fail_loc;
90 unsigned int obd_dump_on_timeout;
91 unsigned int obd_timeout = 100; /* seconds */
92 unsigned int ldlm_timeout = 20; /* seconds */
93 char obd_lustre_upcall[128] = "DEFAULT"; /* or NONE or /full/path/to/upcall  */
94 unsigned int obd_sync_filter; /* = 0, don't sync by default */
95
96 DECLARE_WAIT_QUEUE_HEAD(obd_race_waitq);
97
98 #ifdef __KERNEL__
99 unsigned int obd_print_fail_loc(void)
100 {
101         CWARN("obd_fail_loc = %x\n", obd_fail_loc);
102         return obd_fail_loc;
103 }
104
105 void ll_set_rdonly(ll_sbdev_type dev)
106 {
107         CDEBUG(D_IOCTL | D_HA, "set dev %ld rdonly\n", (long)dev);
108         ll_sbdev_sync(dev);
109 #ifdef HAVE_OLD_DEV_SET_RDONLY
110         dev_set_rdonly(dev, 2);
111 #else
112         dev_set_rdonly(dev);
113 #endif
114 }
115
116 void ll_clear_rdonly(ll_sbdev_type dev)
117 {
118 #ifndef HAVE_CLEAR_RDONLY_ON_PUT
119         CDEBUG(D_IOCTL | D_HA, "unset dev %ld rdonly\n", (long)dev);
120         if (ll_check_rdonly(dev)) {
121                 ll_sbdev_sync(dev);
122 #ifdef HAVE_OLD_DEV_SET_RDONLY
123                 dev_clear_rdonly(2);
124 #else
125                 dev_clear_rdonly(dev);
126 #endif
127         }
128 #else 
129         CDEBUG(D_IOCTL | D_HA, "(will unset dev %ld rdonly on put)\n",
130                (long)dev);
131 #endif
132 }
133
134 /*  opening /dev/obd */
135 static int obd_class_open(struct inode * inode, struct file * file)
136 {
137         ENTRY;
138
139         PORTAL_MODULE_USE;
140         RETURN(0);
141 }
142
143 /*  closing /dev/obd */
144 static int obd_class_release(struct inode * inode, struct file * file)
145 {
146         ENTRY;
147
148         PORTAL_MODULE_UNUSE;
149         RETURN(0);
150 }
151 #endif
152
153 static inline void obd_data2conn(struct lustre_handle *conn,
154                                  struct obd_ioctl_data *data)
155 {
156         memset(conn, 0, sizeof *conn);
157         conn->cookie = data->ioc_cookie;
158 }
159
160 static inline void obd_conn2data(struct obd_ioctl_data *data,
161                                  struct lustre_handle *conn)
162 {
163         data->ioc_cookie = conn->cookie;
164 }
165
166 int class_resolve_dev_name(uint32_t len, char *name)
167 {
168         int rc;
169         int dev;
170
171         if (!len || !name) {
172                 CERROR("No name passed,!\n");
173                 GOTO(out, rc = -EINVAL);
174         }
175         if (name[len - 1] != 0) {
176                 CERROR("Name not nul terminated!\n");
177                 GOTO(out, rc = -EINVAL);
178         }
179
180         CDEBUG(D_IOCTL, "device name %s\n", name);
181         dev = class_name2dev(name);
182         if (dev == -1) {
183                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
184                 GOTO(out, rc = -EINVAL);
185         }
186
187         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
188         rc = dev;
189
190 out:
191         RETURN(rc);
192 }
193
194 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
195 {
196         char *buf = NULL;
197         struct obd_ioctl_data *data;
198         struct portals_debug_ioctl_data *debug_data;
199         struct obd_device *obd = NULL;
200         int err = 0, len = 0;
201         ENTRY;
202
203         if (current->fsuid != 0)
204                 RETURN(err = -EACCES);
205
206         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
207                 RETURN(err = -ENOTTY);
208
209         /* only for debugging */
210         if (cmd == PTL_IOC_DEBUG_MASK) {
211                 debug_data = (struct portals_debug_ioctl_data*)arg;
212                 portal_subsystem_debug = debug_data->subs;
213                 portal_debug = debug_data->debug;
214                 return 0;
215         }
216
217         CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
218         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
219                 CERROR("OBD ioctl: data error\n");
220                 GOTO(out, err = -EINVAL);
221         }
222         data = (struct obd_ioctl_data *)buf;
223
224         switch (cmd) {
225         case OBD_IOC_PROCESS_CFG: {
226                 struct lustre_cfg *lcfg;
227
228                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
229                         CERROR("No config buffer passed!\n");
230                         GOTO(out, err = -EINVAL);
231                 }
232
233                 err = lustre_cfg_sanity_check(data->ioc_pbuf1,
234                                               data->ioc_plen1);
235                 if (err)
236                         GOTO(out, err);
237
238                 OBD_ALLOC(lcfg, data->ioc_plen1);
239                 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
240
241                 if (!err)
242                         err = class_process_config(lcfg);
243                 OBD_FREE(lcfg, data->ioc_plen1);
244                 GOTO(out, err);
245         }
246
247         case OBD_GET_VERSION:
248                 if (!data->ioc_inlbuf1) {
249                         CERROR("No buffer passed in ioctl\n");
250                         GOTO(out, err = -EINVAL);
251                 }
252
253                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
254                         CERROR("ioctl buffer too small to hold version\n");
255                         GOTO(out, err = -EINVAL);
256                 }
257
258                 memcpy(data->ioc_bulk, BUILD_VERSION,
259                        strlen(BUILD_VERSION) + 1);
260
261                 err = copy_to_user((void *)arg, data, len);
262                 if (err)
263                         err = -EFAULT;
264                 GOTO(out, err);
265
266         case OBD_IOC_NAME2DEV: {
267                 /* Resolve a device name.  This does not change the
268                  * currently selected device.
269                  */
270                 int dev;
271
272                 dev = class_resolve_dev_name(data->ioc_inllen1,
273                                              data->ioc_inlbuf1);
274                 data->ioc_dev = dev;
275                 if (dev < 0)
276                         GOTO(out, err = -EINVAL);
277
278                 err = copy_to_user((void *)arg, data, sizeof(*data));
279                 if (err)
280                         err = -EFAULT;
281                 GOTO(out, err);
282         }
283
284         case OBD_IOC_UUID2DEV: {
285                 /* Resolve a device uuid.  This does not change the
286                  * currently selected device.
287                  */
288                 int dev;
289                 struct obd_uuid uuid;
290
291                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
292                         CERROR("No UUID passed!\n");
293                         GOTO(out, err = -EINVAL);
294                 }
295                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
296                         CERROR("UUID not NUL terminated!\n");
297                         GOTO(out, err = -EINVAL);
298                 }
299
300                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
301                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
302                 dev = class_uuid2dev(&uuid);
303                 data->ioc_dev = dev;
304                 if (dev == -1) {
305                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
306                                data->ioc_inlbuf1);
307                         GOTO(out, err = -EINVAL);
308                 }
309
310                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
311                        dev);
312                 err = copy_to_user((void *)arg, data, sizeof(*data));
313                 if (err)
314                         err = -EFAULT;
315                 GOTO(out, err);
316         }
317
318
319         case OBD_IOC_CLOSE_UUID: {
320                 ptl_nid_t       peer_nid;
321                 __u32           peer_nal;
322                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
323                        data->ioc_inlbuf1);
324                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer_nal, &peer_nid);
325                 GOTO(out, err = 0);
326         }
327
328         }
329
330         if (data->ioc_dev >= MAX_OBD_DEVICES) {
331                 CERROR("OBD ioctl: No device\n");
332                 GOTO(out, err = -EINVAL);
333         }
334         obd = &obd_dev[data->ioc_dev];
335         if (!(obd && obd->obd_set_up) || obd->obd_stopping) {
336                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
337                 GOTO(out, err = -EINVAL);
338         }
339
340         switch(cmd) {
341         case OBD_IOC_NO_TRANSNO: {
342                 if (!obd->obd_attached) {
343                         CERROR("Device %d not attached\n", obd->obd_minor);
344                         GOTO(out, err = -ENODEV);
345                 }
346                 CDEBUG(D_IOCTL,
347                        "disabling committed-transno notifications on %d\n",
348                        obd->obd_minor);
349                 obd->obd_no_transno = 1;
350                 GOTO(out, err = 0);
351         }
352
353         default: {
354                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
355                 if (err)
356                         GOTO(out, err);
357
358                 err = copy_to_user((void *)arg, data, len);
359                 if (err)
360                         err = -EFAULT;
361                 GOTO(out, err);
362         }
363         }
364
365  out:
366         if (buf)
367                 obd_ioctl_freedata(buf, len);
368         RETURN(err);
369 } /* class_handle_ioctl */
370
371
372
373 #define OBD_MINOR 241
374 #ifdef __KERNEL__
375 /* to control /dev/obd */
376 static int obd_class_ioctl(struct inode *inode, struct file *filp,
377                            unsigned int cmd, unsigned long arg)
378 {
379         return class_handle_ioctl(cmd, arg);
380 }
381
382 /* declare character device */
383 static struct file_operations obd_psdev_fops = {
384         .owner   = THIS_MODULE,
385         .ioctl   = obd_class_ioctl,     /* ioctl */
386         .open    = obd_class_open,      /* open */
387         .release = obd_class_release,   /* release */
388 };
389
390 /* modules setup */
391 static struct miscdevice obd_psdev = {
392         .minor = OBD_MINOR,
393         .name  = "obd",
394         .fops  = &obd_psdev_fops,
395 };
396 #else
397 void *obd_psdev = NULL;
398 #endif
399
400 EXPORT_SYMBOL(obd_dev);
401 EXPORT_SYMBOL(obd_fail_loc);
402 EXPORT_SYMBOL(ll_set_rdonly);
403 EXPORT_SYMBOL(ll_clear_rdonly);
404 EXPORT_SYMBOL(obd_print_fail_loc);
405 EXPORT_SYMBOL(obd_race_waitq);
406 EXPORT_SYMBOL(obd_dump_on_timeout);
407 EXPORT_SYMBOL(obd_timeout);
408 EXPORT_SYMBOL(ldlm_timeout);
409 EXPORT_SYMBOL(obd_lustre_upcall);
410 EXPORT_SYMBOL(obd_sync_filter);
411 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
412 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
413
414 struct proc_dir_entry *proc_lustre_root;
415 EXPORT_SYMBOL(proc_lustre_root);
416
417 EXPORT_SYMBOL(class_register_type);
418 EXPORT_SYMBOL(class_unregister_type);
419 EXPORT_SYMBOL(class_get_type);
420 EXPORT_SYMBOL(class_put_type);
421 EXPORT_SYMBOL(class_name2dev);
422 EXPORT_SYMBOL(class_name2obd);
423 EXPORT_SYMBOL(class_uuid2dev);
424 EXPORT_SYMBOL(class_uuid2obd);
425 EXPORT_SYMBOL(class_find_client_obd);
426 EXPORT_SYMBOL(class_find_client_notype);
427 EXPORT_SYMBOL(class_devices_in_group);
428 EXPORT_SYMBOL(class_conn2export);
429 EXPORT_SYMBOL(class_exp2obd);
430 EXPORT_SYMBOL(class_conn2obd);
431 EXPORT_SYMBOL(class_exp2cliimp);
432 EXPORT_SYMBOL(class_conn2cliimp);
433 EXPORT_SYMBOL(class_disconnect);
434
435 /* uuid.c */
436 EXPORT_SYMBOL(class_uuid_unparse);
437 EXPORT_SYMBOL(lustre_uuid_to_peer);
438
439 EXPORT_SYMBOL(class_handle_hash);
440 EXPORT_SYMBOL(class_handle_unhash);
441 EXPORT_SYMBOL(class_handle2object);
442
443 /* config.c */
444 EXPORT_SYMBOL(class_get_profile);
445 EXPORT_SYMBOL(class_del_profile);
446 EXPORT_SYMBOL(class_process_config);
447 EXPORT_SYMBOL(class_config_parse_llog);
448 EXPORT_SYMBOL(class_config_dump_llog);
449 EXPORT_SYMBOL(class_attach);
450 EXPORT_SYMBOL(class_setup);
451 EXPORT_SYMBOL(class_cleanup);
452 EXPORT_SYMBOL(class_detach);
453
454 #ifdef LPROCFS
455 int obd_proc_read_version(char *page, char **start, off_t off, int count,
456                           int *eof, void *data)
457 {
458         *eof = 1;
459         return snprintf(page, count, "%s\n", BUILD_VERSION);
460 }
461
462 int obd_proc_read_kernel_version(char *page, char **start, off_t off, int count,
463                                  int *eof, void *data)
464 {
465         *eof = 1;
466         return snprintf(page, count, "%u\n", LUSTRE_KERNEL_VERSION);
467 }
468
469 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
470                          int *eof, void *data)
471 {
472         *eof = 1;
473         return snprintf(page, count, "%s\n",
474 #ifdef ENABLE_PINGER
475                         "on"
476 #else
477                         "off"
478 #endif
479                        );
480 }
481
482 /* Root for /proc/fs/lustre */
483 struct lprocfs_vars lprocfs_base[] = {
484         { "version", obd_proc_read_version, NULL, NULL },
485         { "kernel_version", obd_proc_read_kernel_version, NULL, NULL },
486         { "pinger", obd_proc_read_pinger, NULL, NULL },
487         { 0 }
488 };
489 #else
490 #define lprocfs_base NULL
491 #endif /* LPROCFS */
492
493 #ifdef __KERNEL__
494 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
495 {
496         if (*pos >= MAX_OBD_DEVICES)
497                 return NULL;
498         return &obd_dev[*pos];
499 }
500
501 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
502 {
503 }
504
505 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
506 {
507         ++*pos;
508         if (*pos >= MAX_OBD_DEVICES)
509                 return NULL;
510         return &obd_dev[*pos];
511 }
512
513 static int obd_device_list_seq_show(struct seq_file *p, void *v)
514 {
515         struct obd_device *obd = (struct obd_device *)v;
516         int index = obd - &obd_dev[0];
517         char *status;
518
519         if (!obd->obd_type)
520                 return 0;
521         if (obd->obd_stopping)
522                 status = "ST";
523         else if (obd->obd_set_up)
524                 status = "UP";
525         else if (obd->obd_attached)
526                 status = "AT";
527         else
528                 status = "--";
529
530         return seq_printf(p, "%3d %s %s %s %s %d\n",
531                           (int)index, status, obd->obd_type->typ_name,
532                           obd->obd_name, obd->obd_uuid.uuid,
533                           atomic_read(&obd->obd_refcount));
534 }
535
536 struct seq_operations obd_device_list_sops = {
537         .start = obd_device_list_seq_start,
538         .stop = obd_device_list_seq_stop,
539         .next = obd_device_list_seq_next,
540         .show = obd_device_list_seq_show,
541 };
542
543 static int obd_device_list_open(struct inode *inode, struct file *file)
544 {
545         struct proc_dir_entry *dp = PDE(inode);
546         struct seq_file *seq;
547         int rc = seq_open(file, &obd_device_list_sops);
548
549         if (rc)
550                 return rc;
551
552         seq = file->private_data;
553         seq->private = dp->data;
554
555         return 0;
556 }
557
558 struct file_operations obd_device_list_fops = {
559         .owner   = THIS_MODULE,
560         .open    = obd_device_list_open,
561         .read    = seq_read,
562         .llseek  = seq_lseek,
563         .release = seq_release,
564 };
565 #endif
566
567 #define OBD_INIT_CHECK
568 #ifdef OBD_INIT_CHECK
569 int obd_init_checks(void)
570 {
571         __u64 u64val, div64val;
572         char buf[64];
573         int len, ret = 0;
574
575         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
576                LPU64, LPD64, LPX64, LPSZ, LPSSZ);
577
578         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
579
580         u64val = OBD_OBJECT_EOF;
581         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
582         if (u64val != OBD_OBJECT_EOF) {
583                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
584                        u64val, (int)sizeof(u64val));
585                 ret = -EINVAL;
586         }
587         len = snprintf(buf, sizeof(buf), LPX64, u64val);
588         if (len != 18) {
589                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
590                 ret = -EINVAL;
591         }
592
593         div64val = OBD_OBJECT_EOF;
594         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
595         if (u64val != OBD_OBJECT_EOF) {
596                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
597                        u64val, (int)sizeof(u64val));
598                 ret = -EOVERFLOW;
599         }
600         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
601                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
602                        u64val, (int)sizeof(u64val));
603                 return -EOVERFLOW;
604         }
605         if (do_div(div64val, 256) != (u64val & 255)) {
606                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
607                 return -EOVERFLOW;
608         }
609         if (u64val >> 8 != div64val) {
610                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
611                        u64val, div64val, u64val >> 8);
612                 return -EOVERFLOW;
613         }
614         len = snprintf(buf, sizeof(buf), LPX64, u64val);
615         if (len != 18) {
616                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
617                 ret = -EINVAL;
618         }
619         len = snprintf(buf, sizeof(buf), LPU64, u64val);
620         if (len != 20) {
621                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
622                 ret = -EINVAL;
623         }
624         len = snprintf(buf, sizeof(buf), LPD64, u64val);
625         if (len != 2) {
626                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
627                 ret = -EINVAL;
628         }
629         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
630                 CWARN("mask failed: u64val "LPU64" >= %lu\n", u64val,PAGE_SIZE);
631                 ret = -EINVAL;
632         }
633
634         return ret;
635 }
636 #else
637 #define obd_init_checks() do {} while(0)
638 #endif
639
640 #ifdef __KERNEL__
641 static int __init init_obdclass(void)
642 #else
643 int init_obdclass(void)
644 #endif
645 {
646         struct obd_device *obd;
647 #ifdef __KERNEL__
648         struct proc_dir_entry *entry;
649 #endif
650         int err;
651         int i;
652
653         printk(KERN_INFO "Lustre: OBD class driver Build Version: "
654                BUILD_VERSION", info@clusterfs.com\n");
655
656         err = obd_init_checks();
657         if (err == -EOVERFLOW)
658                 return err;
659
660         class_init_uuidlist();
661         err = class_handle_init();
662         if (err)
663                 return err;
664
665         spin_lock_init(&obd_dev_lock);
666         INIT_LIST_HEAD(&obd_types);
667
668         err = misc_register(&obd_psdev);
669         if (err) {
670                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
671                 return err;
672         }
673
674         /* This struct is already zerod for us (static global) */
675         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
676                 obd->obd_minor = i;
677
678         err = obd_init_caches();
679         if (err)
680                 return err;
681
682 #ifdef __KERNEL__
683         obd_sysctl_init();
684
685         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
686         if (!proc_lustre_root) {
687                 printk(KERN_ERR
688                        "LustreError: error registering /proc/fs/lustre\n");
689                 RETURN(-ENOMEM);
690         }
691         proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
692         entry = create_proc_entry("devices", 0444, proc_lustre_root);
693         if (entry == NULL) {
694                 printk(KERN_ERR "LustreError: error registering "
695                        "/proc/fs/lustre/devices\n");
696                 lprocfs_remove(proc_lustre_root);
697                 RETURN(-ENOMEM);
698         }
699         entry->proc_fops = &obd_device_list_fops;
700 #endif
701         return 0;
702 }
703
704 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
705  * ifdef to the end of the file to cover module and versioning goo.*/
706 #ifdef __KERNEL__
707
708 static void cleanup_obdclass(void)
709 {
710         int i;
711         int leaked;
712         ENTRY;
713
714         misc_deregister(&obd_psdev);
715         for (i = 0; i < MAX_OBD_DEVICES; i++) {
716                 struct obd_device *obd = &obd_dev[i];
717                 if (obd->obd_type && obd->obd_set_up &&
718                     OBT(obd) && OBP(obd, detach)) {
719                         /* XXX should this call generic detach otherwise? */
720                         OBP(obd, detach)(obd);
721                 }
722         }
723
724         obd_cleanup_caches();
725         obd_sysctl_clean();
726
727         if (proc_lustre_root) {
728                 lprocfs_remove(proc_lustre_root);
729                 proc_lustre_root = NULL;
730         }
731
732         class_handle_cleanup();
733         class_exit_uuidlist();
734
735         leaked = atomic_read(&obd_memory);
736         CDEBUG(leaked ? D_ERROR : D_INFO,
737                "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
738
739         EXIT;
740 }
741
742 /* Check that we're building against the appropriate version of the Lustre
743  * kernel patch */
744 #include <linux/lustre_version.h>
745 #define LUSTRE_MIN_VERSION 32
746 #define LUSTRE_MAX_VERSION 46
747 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
748 # error Cannot continue: Your Lustre kernel patch is older than the sources
749 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
750 # error Cannot continue: Your Lustre sources are older than the kernel patch
751 #endif
752
753 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
754 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
755 MODULE_LICENSE("GPL");
756
757 module_init(init_obdclass);
758 module_exit(cleanup_obdclass);
759 #endif