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 = 6;  /* 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                 if (err)
241                         GOTO(out, err);
242
243                 err = class_process_config(lcfg);
244                 OBD_FREE(lcfg, data->ioc_plen1);
245                 GOTO(out, err);
246         }
247
248         case OBD_GET_VERSION:
249                 if (!data->ioc_inlbuf1) {
250                         CERROR("No buffer passed in ioctl\n");
251                         GOTO(out, err = -EINVAL);
252                 }
253
254                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
255                         CERROR("ioctl buffer too small to hold version\n");
256                         GOTO(out, err = -EINVAL);
257                 }
258
259                 memcpy(data->ioc_bulk, BUILD_VERSION,
260                        strlen(BUILD_VERSION) + 1);
261
262                 err = copy_to_user((void *)arg, data, len);
263                 if (err)
264                         err = -EFAULT;
265                 GOTO(out, err);
266
267         case OBD_IOC_NAME2DEV: {
268                 /* Resolve a device name.  This does not change the
269                  * currently selected device.
270                  */
271                 int dev;
272
273                 dev = class_resolve_dev_name(data->ioc_inllen1,
274                                              data->ioc_inlbuf1);
275                 data->ioc_dev = dev;
276                 if (dev < 0)
277                         GOTO(out, err = -EINVAL);
278
279                 err = copy_to_user((void *)arg, data, sizeof(*data));
280                 if (err)
281                         err = -EFAULT;
282                 GOTO(out, err);
283         }
284
285         case OBD_IOC_UUID2DEV: {
286                 /* Resolve a device uuid.  This does not change the
287                  * currently selected device.
288                  */
289                 int dev;
290                 struct obd_uuid uuid;
291
292                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
293                         CERROR("No UUID passed!\n");
294                         GOTO(out, err = -EINVAL);
295                 }
296                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
297                         CERROR("UUID not NUL terminated!\n");
298                         GOTO(out, err = -EINVAL);
299                 }
300
301                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
302                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
303                 dev = class_uuid2dev(&uuid);
304                 data->ioc_dev = dev;
305                 if (dev == -1) {
306                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
307                                data->ioc_inlbuf1);
308                         GOTO(out, err = -EINVAL);
309                 }
310
311                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
312                        dev);
313                 err = copy_to_user((void *)arg, data, sizeof(*data));
314                 if (err)
315                         err = -EFAULT;
316                 GOTO(out, err);
317         }
318
319
320         case OBD_IOC_CLOSE_UUID: {
321                 ptl_nid_t       peer_nid;
322                 __u32           peer_nal;
323                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
324                        data->ioc_inlbuf1);
325                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer_nal, &peer_nid);
326                 GOTO(out, err = 0);
327         }
328
329         }
330
331         if (data->ioc_dev >= MAX_OBD_DEVICES) {
332                 CERROR("OBD ioctl: No device\n");
333                 GOTO(out, err = -EINVAL);
334         } 
335         obd = &obd_dev[data->ioc_dev];
336         if (!(obd && obd->obd_set_up) || obd->obd_stopping) {
337                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
338                 GOTO(out, err = -EINVAL);
339         }
340
341         switch(cmd) {
342         case OBD_IOC_NO_TRANSNO: {
343                 if (!obd->obd_attached) {
344                         CERROR("Device %d not attached\n", obd->obd_minor);
345                         GOTO(out, err = -ENODEV);
346                 }
347                 CDEBUG(D_IOCTL,
348                        "disabling committed-transno notifications on %d\n",
349                        obd->obd_minor);
350                 obd->obd_no_transno = 1;
351                 GOTO(out, err = 0);
352         }
353
354         default: {
355                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
356                 if (err)
357                         GOTO(out, err);
358
359                 err = copy_to_user((void *)arg, data, len);
360                 if (err)
361                         err = -EFAULT;
362                 GOTO(out, err);
363         }
364         }
365
366  out:
367         if (buf)
368                 obd_ioctl_freedata(buf, len);
369         RETURN(err);
370 } /* class_handle_ioctl */
371
372
373
374 #define OBD_MINOR 241
375 #ifdef __KERNEL__
376 /* to control /dev/obd */
377 static int obd_class_ioctl(struct inode *inode, struct file *filp,
378                            unsigned int cmd, unsigned long arg)
379 {
380         return class_handle_ioctl(cmd, arg);
381 }
382
383 /* declare character device */
384 static struct file_operations obd_psdev_fops = {
385         .owner   = THIS_MODULE,
386         .ioctl   = obd_class_ioctl,     /* ioctl */
387         .open    = obd_class_open,      /* open */
388         .release = obd_class_release,   /* release */
389 };
390
391 /* modules setup */
392 static struct miscdevice obd_psdev = {
393         .minor = OBD_MINOR,
394         .name  = "obd_psdev",
395         .fops  = &obd_psdev_fops,
396 };
397 #else
398 void *obd_psdev = NULL;
399 #endif
400
401 EXPORT_SYMBOL(obd_dev);
402 EXPORT_SYMBOL(obdo_cachep);
403 EXPORT_SYMBOL(qunit_cachep);
404 EXPORT_SYMBOL(qunit_hash_lock);
405 EXPORT_SYMBOL(qunit_hash);
406 EXPORT_SYMBOL(obd_fail_loc);
407 EXPORT_SYMBOL(ll_set_rdonly);
408 EXPORT_SYMBOL(ll_clear_rdonly);
409 EXPORT_SYMBOL(obd_print_fail_loc);
410 EXPORT_SYMBOL(obd_race_waitq);
411 EXPORT_SYMBOL(obd_dump_on_timeout);
412 EXPORT_SYMBOL(obd_timeout);
413 EXPORT_SYMBOL(ldlm_timeout);
414 EXPORT_SYMBOL(obd_lustre_upcall);
415 EXPORT_SYMBOL(obd_sync_filter);
416 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
417 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
418 EXPORT_SYMBOL(proc_lustre_root);
419
420 EXPORT_SYMBOL(class_register_type);
421 EXPORT_SYMBOL(class_unregister_type);
422 EXPORT_SYMBOL(class_get_type);
423 EXPORT_SYMBOL(class_put_type);
424 EXPORT_SYMBOL(class_name2dev);
425 EXPORT_SYMBOL(class_name2obd);
426 EXPORT_SYMBOL(class_uuid2dev);
427 EXPORT_SYMBOL(class_uuid2obd);
428 EXPORT_SYMBOL(class_find_client_obd);
429 EXPORT_SYMBOL(class_find_client_notype);
430 EXPORT_SYMBOL(class_devices_in_group);
431 EXPORT_SYMBOL(__class_export_put);
432 EXPORT_SYMBOL(class_new_export);
433 EXPORT_SYMBOL(class_unlink_export);
434 EXPORT_SYMBOL(class_import_get);
435 EXPORT_SYMBOL(class_import_put);
436 EXPORT_SYMBOL(class_new_import);
437 EXPORT_SYMBOL(class_destroy_import);
438 EXPORT_SYMBOL(class_connect);
439 EXPORT_SYMBOL(class_conn2export);
440 EXPORT_SYMBOL(class_exp2obd);
441 EXPORT_SYMBOL(class_conn2obd);
442 EXPORT_SYMBOL(class_exp2cliimp);
443 EXPORT_SYMBOL(class_conn2cliimp);
444 EXPORT_SYMBOL(class_disconnect);
445 EXPORT_SYMBOL(class_disconnect_exports);
446 EXPORT_SYMBOL(class_disconnect_stale_exports);
447 EXPORT_SYMBOL(class_update_export_timer);
448
449 EXPORT_SYMBOL(oig_init);
450 EXPORT_SYMBOL(oig_release);
451 EXPORT_SYMBOL(oig_add_one);
452 EXPORT_SYMBOL(oig_wait);
453 EXPORT_SYMBOL(oig_complete_one);
454
455 EXPORT_SYMBOL(ping_evictor_start);
456 EXPORT_SYMBOL(ping_evictor_stop);
457
458 /* uuid.c */
459 EXPORT_SYMBOL(class_uuid_unparse);
460 EXPORT_SYMBOL(lustre_uuid_to_peer);
461
462 EXPORT_SYMBOL(class_handle_hash);
463 EXPORT_SYMBOL(class_handle_unhash);
464 EXPORT_SYMBOL(class_handle2object);
465
466 /* config.c */
467 EXPORT_SYMBOL(class_get_profile);
468 EXPORT_SYMBOL(class_del_profile);
469 EXPORT_SYMBOL(class_process_config);
470 EXPORT_SYMBOL(class_config_parse_llog);
471 EXPORT_SYMBOL(class_config_dump_llog);
472 EXPORT_SYMBOL(class_attach);
473 EXPORT_SYMBOL(class_setup);
474 EXPORT_SYMBOL(class_cleanup);
475 EXPORT_SYMBOL(class_detach);
476
477 #ifdef LPROCFS
478 int obd_proc_read_version(char *page, char **start, off_t off, int count,
479                           int *eof, void *data)
480 {
481         *eof = 1;
482         return snprintf(page, count, "%s\n", BUILD_VERSION);
483 }
484
485 int obd_proc_read_kernel_version(char *page, char **start, off_t off, int count,
486                                  int *eof, void *data)
487 {
488         *eof = 1;
489         return snprintf(page, count, "%u\n", LUSTRE_KERNEL_VERSION);
490 }
491
492 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
493                          int *eof, void *data)
494 {
495         *eof = 1;
496         return snprintf(page, count, "%s\n",
497 #ifdef ENABLE_PINGER
498                         "on"
499 #else
500                         "off"
501 #endif
502                        );
503 }
504
505 /* Root for /proc/fs/lustre */
506 struct proc_dir_entry *proc_lustre_root = NULL;
507 struct lprocfs_vars lprocfs_base[] = {
508         { "version", obd_proc_read_version, NULL, NULL },
509         { "kernel_version", obd_proc_read_kernel_version, NULL, NULL },
510         { "pinger", obd_proc_read_pinger, NULL, NULL },
511         { 0 }
512 };
513
514 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
515 {
516         if (*pos >= MAX_OBD_DEVICES)
517                 return NULL;
518         return &obd_dev[*pos];
519 }
520
521 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
522 {
523 }
524
525 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
526 {
527         ++*pos;
528         if (*pos >= MAX_OBD_DEVICES)
529                 return NULL;
530         return &obd_dev[*pos];
531 }
532
533 static int obd_device_list_seq_show(struct seq_file *p, void *v)
534 {
535         struct obd_device *obd = (struct obd_device *)v;
536         int index = obd - &obd_dev[0];
537         char *status;
538
539         if (!obd->obd_type)
540                 return 0;
541         if (obd->obd_stopping)
542                 status = "ST";
543         else if (obd->obd_set_up)
544                 status = "UP";
545         else if (obd->obd_attached)
546                 status = "AT";
547         else
548                 status = "--";
549
550         return seq_printf(p, "%3d %s %s %s %s %d\n",
551                           (int)index, status, obd->obd_type->typ_name,
552                           obd->obd_name, obd->obd_uuid.uuid,
553                           atomic_read(&obd->obd_refcount));
554 }
555
556 struct seq_operations obd_device_list_sops = {
557         .start = obd_device_list_seq_start,
558         .stop = obd_device_list_seq_stop,
559         .next = obd_device_list_seq_next,
560         .show = obd_device_list_seq_show,
561 };
562
563 static int obd_device_list_open(struct inode *inode, struct file *file)
564 {
565         struct proc_dir_entry *dp = PDE(inode);
566         struct seq_file *seq;
567         int rc = seq_open(file, &obd_device_list_sops);
568
569         if (rc)
570                 return rc;
571
572         seq = file->private_data;
573         seq->private = dp->data;
574
575         return 0;
576 }
577
578 struct file_operations obd_device_list_fops = {
579         .owner   = THIS_MODULE,
580         .open    = obd_device_list_open,
581         .read    = seq_read,
582         .llseek  = seq_lseek,
583         .release = seq_release,
584 };
585 #endif
586
587 #define OBD_INIT_CHECK
588 #ifdef OBD_INIT_CHECK
589 int obd_init_checks(void)
590 {
591         __u64 u64val, div64val;
592         char buf[64];
593         int len, ret = 0;
594
595         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
596                LPU64, LPD64, LPX64, LPSZ, LPSSZ);
597
598         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", OBD_OBJECT_EOF);
599
600         u64val = OBD_OBJECT_EOF;
601         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
602         if (u64val != OBD_OBJECT_EOF) {
603                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
604                        u64val, sizeof(u64val));
605                 ret = -EINVAL;
606         }
607         len = snprintf(buf, sizeof(buf), LPX64, u64val);
608         if (len != 18) {
609                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
610                 ret = -EINVAL;
611         }
612
613         div64val = OBD_OBJECT_EOF;
614         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
615         if (u64val != OBD_OBJECT_EOF) {
616                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
617                        u64val, sizeof(u64val));
618                 ret = -EOVERFLOW;
619         }
620         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
621                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
622                        u64val, sizeof(u64val));
623                 return -EOVERFLOW;
624         }
625         if (do_div(div64val, 256) != (u64val & 255)) {
626                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
627                 return -EOVERFLOW;
628         }
629         if (u64val >> 8 != div64val) {
630                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
631                        u64val, div64val, u64val >> 8);
632                 return -EOVERFLOW;
633         }
634         len = snprintf(buf, sizeof(buf), LPX64, u64val);
635         if (len != 18) {
636                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
637                 ret = -EINVAL;
638         }
639         len = snprintf(buf, sizeof(buf), LPU64, u64val);
640         if (len != 20) {
641                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
642                 ret = -EINVAL;
643         }
644         len = snprintf(buf, sizeof(buf), LPD64, u64val);
645         if (len != 2) {
646                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
647                 ret = -EINVAL;
648         }
649         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
650                 CWARN("mask failed: u64val "LPU64" >= %lu\n", u64val,PAGE_SIZE);
651                 ret = -EINVAL;
652         }
653
654         return ret;
655 }
656 #else
657 #define obd_init_checks() do {} while(0)
658 #endif
659
660 #ifdef __KERNEL__
661 static int __init init_obdclass(void)
662 #else
663 int init_obdclass(void)
664 #endif
665 {
666         struct obd_device *obd;
667 #ifdef LPROCFS
668         struct proc_dir_entry *entry;
669 #endif
670         int err;
671         int i;
672
673         printk(KERN_INFO "Lustre: OBD class driver Build Version: "
674                BUILD_VERSION", info@clusterfs.com\n");
675
676         err = obd_init_checks();
677         if (err == -EOVERFLOW)
678                 return err;
679
680         class_init_uuidlist();
681         err = class_handle_init();
682         if (err)
683                 return err;
684
685         spin_lock_init(&obd_dev_lock);
686         INIT_LIST_HEAD(&obd_types);
687
688         err = misc_register(&obd_psdev);
689         if (err) {
690                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
691                 return err;
692         }
693
694         /* This struct is already zerod for us (static global) */
695         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
696                 obd->obd_minor = i;
697
698         err = obd_init_caches();
699         if (err)
700                 return err;
701
702 #ifdef __KERNEL__
703         obd_sysctl_init();
704 #endif
705
706 #ifdef LPROCFS
707         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
708         if (!proc_lustre_root) {
709                 printk(KERN_ERR
710                        "LustreError: error registering /proc/fs/lustre\n");
711                 RETURN(-ENOMEM);
712         }
713         proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
714         entry = create_proc_entry("devices", 0444, proc_lustre_root);
715         if (entry == NULL) {
716                 printk(KERN_ERR "LustreError: error registering "
717                        "/proc/fs/lustre/devices\n");
718                 lprocfs_remove(proc_lustre_root);
719                 RETURN(-ENOMEM);
720         }
721         entry->proc_fops = &obd_device_list_fops;
722 #endif
723         return 0;
724 }
725
726 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
727  * ifdef to the end of the file to cover module and versioning goo.*/
728 #ifdef __KERNEL__
729
730 static void cleanup_obdclass(void)
731 {
732         int i;
733         int leaked;
734         ENTRY;
735
736         misc_deregister(&obd_psdev);
737         for (i = 0; i < MAX_OBD_DEVICES; i++) {
738                 struct obd_device *obd = &obd_dev[i];
739                 if (obd->obd_type && obd->obd_set_up &&
740                     OBT(obd) && OBP(obd, detach)) {
741                         /* XXX should this call generic detach otherwise? */
742                         OBP(obd, detach)(obd);
743                 }
744         }
745
746         obd_cleanup_caches();
747         obd_sysctl_clean();
748 #ifdef LPROCFS
749         if (proc_lustre_root) {
750                 lprocfs_remove(proc_lustre_root);
751                 proc_lustre_root = NULL;
752         }
753 #endif
754
755         class_handle_cleanup();
756         class_exit_uuidlist();
757
758         leaked = atomic_read(&obd_memory);
759         CDEBUG(leaked ? D_ERROR : D_INFO,
760                "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
761
762         EXIT;
763 }
764
765 /* Check that we're building against the appropriate version of the Lustre
766  * kernel patch */
767 #include <linux/lustre_version.h>
768 #define LUSTRE_MIN_VERSION 32
769 #define LUSTRE_MAX_VERSION 45
770 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
771 # error Cannot continue: Your Lustre kernel patch is older than the sources
772 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
773 # error Cannot continue: Your Lustre sources are older than the kernel patch
774 #endif
775
776 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
777 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
778 MODULE_LICENSE("GPL");
779
780 module_init(init_obdclass);
781 module_exit(cleanup_obdclass);
782 #endif