Whamcloud - gitweb
r=shaver
[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 #include <linux/lustre_build_version.h>
67 #include <portals/list.h>
68 #include "llog_internal.h"
69
70 struct semaphore obd_conf_sem;   /* serialize configuration commands */
71 struct obd_device obd_dev[MAX_OBD_DEVICES];
72 struct list_head obd_types;
73 atomic_t obd_memory;
74 int obd_memmax;
75
76 int proc_version;
77
78 /* The following are visible and mutable through /proc/sys/lustre/. */
79 unsigned int obd_fail_loc;
80 unsigned int obd_timeout = 100;
81 char obd_lustre_upcall[128] = "/usr/lib/lustre/lustre_upcall";
82 unsigned int obd_sync_filter; /* = 0, don't sync by default */
83
84 #ifdef __KERNEL__
85 /*  opening /dev/obd */
86 static int obd_class_open(struct inode * inode, struct file * file)
87 {
88         struct obd_class_user_state *ocus;
89         ENTRY;
90
91         OBD_ALLOC(ocus, sizeof(*ocus));
92         if (ocus == NULL)
93                 return (-ENOMEM);
94
95         INIT_LIST_HEAD(&ocus->ocus_conns);
96         file->private_data = ocus;
97
98         PORTAL_MODULE_USE;
99         RETURN(0);
100 }
101
102 /*  closing /dev/obd */
103 static int obd_class_release(struct inode * inode, struct file * file)
104 {
105         struct obd_class_user_state *ocus = file->private_data;
106         struct obd_class_user_conn  *c;
107         ENTRY;
108
109         while (!list_empty (&ocus->ocus_conns)) {
110                 c = list_entry (ocus->ocus_conns.next,
111                                 struct obd_class_user_conn, ocuc_chain);
112                 list_del (&c->ocuc_chain);
113
114                 CDEBUG (D_IOCTL, "Auto-disconnect exp %p\n", c->ocuc_exp);
115
116                 down (&obd_conf_sem);
117                 obd_disconnect (c->ocuc_exp, 0);
118                 up (&obd_conf_sem);
119
120                 OBD_FREE (c, sizeof (*c));
121         }
122
123         OBD_FREE (ocus, sizeof (*ocus));
124
125         PORTAL_MODULE_UNUSE;
126         RETURN(0);
127 }
128 #endif
129
130 static int obd_class_add_user_exp(struct obd_class_user_state *ocus,
131                                   struct obd_export *exp)
132 {
133         struct obd_class_user_conn *c;
134         ENTRY;
135         /* NB holding obd_conf_sem */
136
137         OBD_ALLOC (c, sizeof (*c));
138         if (c == NULL)
139                 RETURN(-ENOMEM);
140
141         c->ocuc_exp = class_export_get(exp);
142         list_add (&c->ocuc_chain, &ocus->ocus_conns);
143         RETURN(0);
144 }
145
146 static void obd_class_remove_user_exp(struct obd_class_user_state *ocus,
147                                       struct obd_export *exp)
148 {
149         struct obd_class_user_conn *c;
150         ENTRY;
151
152         /* NB holding obd_conf_sem or last reference */
153
154         list_for_each_entry(c, &ocus->ocus_conns, ocuc_chain) {
155                 if (exp == c->ocuc_exp) {
156                         list_del (&c->ocuc_chain);
157                         OBD_FREE (c, sizeof (*c));
158                         class_export_put(exp);
159                         EXIT;
160                         return;
161                 }
162         }
163         EXIT;
164 }
165
166 static inline void obd_data2conn(struct lustre_handle *conn,
167                                  struct obd_ioctl_data *data)
168 {
169         memset(conn, 0, sizeof *conn);
170         conn->cookie = data->ioc_cookie;
171 }
172
173 static inline void obd_conn2data(struct obd_ioctl_data *data,
174                                  struct lustre_handle *conn)
175 {
176         data->ioc_cookie = conn->cookie;
177 }
178
179 int class_resolve_dev_name(uint32_t len, char *name)
180 {
181         int rc;
182         int dev;
183
184         if (!len || !name) {
185                 CERROR("No name passed,!\n");
186                 GOTO(out, rc = -EINVAL);
187         }
188         if (name[len - 1] != 0) {
189                 CERROR("Name not nul terminated!\n");
190                 GOTO(out, rc = -EINVAL);
191         }
192
193         CDEBUG(D_IOCTL, "device name %s\n", name);
194         dev = class_name2dev(name);
195         if (dev == -1) {
196                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
197                 GOTO(out, rc = -EINVAL);
198         }
199
200         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
201         rc = dev;
202
203 out:
204         RETURN(rc);
205 }
206
207 int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd,
208                        unsigned long arg)
209 {
210         char *buf = NULL;
211         struct obd_ioctl_data *data;
212         struct portals_debug_ioctl_data *debug_data;
213         struct obd_device *obd = ocus->ocus_current_obd;
214         struct lustre_handle conn;
215         int err = 0, len = 0, serialised = 0;
216         ENTRY;
217
218         if (current->fsuid != 0)
219                 RETURN(err = -EACCES);
220
221         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
222                 RETURN(err = -ENOTTY);
223
224         /* only for debugging */
225         if (cmd == PTL_IOC_DEBUG_MASK) {
226                 debug_data = (struct portals_debug_ioctl_data*)arg;
227                 portal_subsystem_debug = debug_data->subs;
228                 portal_debug = debug_data->debug;
229                 return 0;
230         }
231
232         switch (cmd) {
233         case OBD_IOC_BRW_WRITE:
234         case OBD_IOC_BRW_READ:
235         case OBD_IOC_GETATTR:
236         case ECHO_IOC_ENQUEUE:
237         case ECHO_IOC_CANCEL:
238         case OBD_IOC_CLIENT_RECOVER:
239         case OBD_IOC_CATLOGLIST:
240         case OBD_IOC_LLOG_INFO:
241         case OBD_IOC_LLOG_PRINT:
242         case OBD_IOC_LLOG_CANCEL:
243         case OBD_IOC_LLOG_CHECK:
244         case OBD_IOC_LLOG_REMOVE:
245                 break;
246         default:
247                 down(&obd_conf_sem);
248                 serialised = 1;
249                 break;
250         }
251
252         CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
253         if (!obd && cmd != OBD_IOC_DEVICE &&
254             cmd != OBD_IOC_LIST && cmd != OBD_GET_VERSION &&
255             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_UUID2DEV &&
256             cmd != OBD_IOC_PROCESS_CFG) {
257                 CERROR("OBD ioctl: No device\n");
258                 GOTO(out, err = -EINVAL);
259         }
260         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
261                 CERROR("OBD ioctl: data error\n");
262                 GOTO(out, err = -EINVAL);
263         }
264         data = (struct obd_ioctl_data *)buf;
265
266         switch (cmd) {
267         case OBD_IOC_DEVICE: {
268                 CDEBUG(D_IOCTL, "\n");
269                 if (data->ioc_dev >= MAX_OBD_DEVICES) {
270                         CERROR("OBD ioctl: DEVICE invalid device %d\n",
271                                data->ioc_dev);
272                         ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
273                         GOTO(out, err = -EINVAL);
274                 }
275                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
276                 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
277                 GOTO(out, err = 0);
278         }
279
280         case OBD_IOC_PROCESS_CFG: {
281                 char *buf;
282                 struct lustre_cfg *lcfg;
283
284                 /* FIXME hack to liblustre dump, remove when switch
285                    to zeroconf */
286 #ifndef __KERNEL__
287                 data->ioc_pbuf1 = data->ioc_inlbuf1;
288                 data->ioc_plen1 = data->ioc_inllen1;
289 #endif
290                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
291                         CERROR("No config buffer passed!\n");
292                         GOTO(out, err = -EINVAL);
293                 }
294                 err = lustre_cfg_getdata(&buf, data->ioc_plen1,
295                                          data->ioc_pbuf1, 0);
296                 if (err)
297                         GOTO(out, err);
298                 lcfg = (struct lustre_cfg* ) buf;
299
300                 err = class_process_config(lcfg);
301                 lustre_cfg_freedata(buf, data->ioc_plen1);
302                 GOTO(out, err);
303         }
304
305         case OBD_IOC_LIST: {
306                 int i;
307                 char *buf2 = data->ioc_bulk;
308                 int remains = data->ioc_inllen1;
309
310                 if (!data->ioc_inlbuf1) {
311                         CERROR("No buffer passed!\n");
312                         GOTO(out, err = -EINVAL);
313                 }
314
315
316                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
317                         int l;
318                         char *status;
319                         struct obd_device *obd = &obd_dev[i];
320
321                         if (!obd->obd_type)
322                                 continue;
323                         if (obd->obd_stopping)
324                                 status = "ST";
325                         else if (obd->obd_set_up)
326                                 status = "UP";
327                         else if (obd->obd_attached)
328                                 status = "AT";
329                         else
330                                 status = "-";
331                         l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
332                                      i, status, obd->obd_type->typ_name,
333                                      obd->obd_name, obd->obd_uuid.uuid,
334                                      obd->obd_type->typ_refcnt);
335                         buf2 +=l;
336                         remains -=l;
337                         if (remains <= 0) {
338                                 CERROR("not enough space for device listing\n");
339                                 break;
340                         }
341                 }
342
343                 err = copy_to_user((void *)arg, data, len);
344                 if (err)
345                         err = -EFAULT;
346                 GOTO(out, err);
347         }
348
349         case OBD_GET_VERSION:
350                 if (!data->ioc_inlbuf1) {
351                         CERROR("No buffer passed in ioctl\n");
352                         GOTO(out, err = -EINVAL);
353                 }
354
355                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
356                         CERROR("ioctl buffer too small to hold version\n");
357                         GOTO(out, err = -EINVAL);
358                 }
359
360                 memcpy(data->ioc_bulk, BUILD_VERSION,
361                        strlen(BUILD_VERSION) + 1);
362
363                 err = copy_to_user((void *)arg, data, len);
364                 if (err)
365                         err = -EFAULT;
366                 GOTO(out, err);
367
368         case OBD_IOC_NAME2DEV: {
369                 /* Resolve a device name.  This does not change the
370                  * currently selected device.
371                  */
372                 int dev;
373
374                 dev = class_resolve_dev_name(data->ioc_inllen1,
375                                              data->ioc_inlbuf1);
376                 data->ioc_dev = dev;
377                 if (dev < 0)
378                         GOTO(out, err = -EINVAL);
379
380                 err = copy_to_user((void *)arg, data, sizeof(*data));
381                 if (err)
382                         err = -EFAULT;
383                 GOTO(out, err);
384         }
385
386         case OBD_IOC_UUID2DEV: {
387                 /* Resolve a device uuid.  This does not change the
388                  * currently selected device.
389                  */
390                 int dev;
391                 struct obd_uuid uuid;
392
393                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
394                         CERROR("No UUID passed!\n");
395                         GOTO(out, err = -EINVAL);
396                 }
397                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
398                         CERROR("UUID not NUL terminated!\n");
399                         GOTO(out, err = -EINVAL);
400                 }
401
402                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
403                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
404                 dev = class_uuid2dev(&uuid);
405                 data->ioc_dev = dev;
406                 if (dev == -1) {
407                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
408                                data->ioc_inlbuf1);
409                         GOTO(out, err = -EINVAL);
410                 }
411
412                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
413                        dev);
414                 err = copy_to_user((void *)arg, data, sizeof(*data));
415                 if (err)
416                         err = -EFAULT;
417                 GOTO(out, err);
418         }
419
420
421
422         case OBD_IOC_CONNECT: {
423                 struct obd_export *exp;
424                 obd_data2conn(&conn, data);
425
426                 err = obd_connect(&conn, obd, &obd->obd_uuid);
427
428                 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.cookie);
429                 obd_conn2data(data, &conn);
430                 if (err)
431                         GOTO(out, err);
432
433                 exp = class_conn2export(&conn);
434                 if (exp == NULL)
435                         GOTO(out, err = -EINVAL);
436
437                 err = obd_class_add_user_exp(ocus, exp);
438                 if (err != 0) {
439                         obd_disconnect(exp, 0);
440                         GOTO (out, err);
441                 }
442
443                 err = copy_to_user((void *)arg, data, sizeof(*data));
444                 if (err != 0) {
445                         obd_class_remove_user_exp(ocus, exp);
446                         obd_disconnect(exp, 0);
447                         GOTO (out, err = -EFAULT);
448                 }
449                 class_export_put(exp);
450                 GOTO(out, err);
451         }
452
453         case OBD_IOC_DISCONNECT: {
454                 struct obd_export *exp;
455                 obd_data2conn(&conn, data);
456                 exp = class_conn2export(&conn);
457                 if (exp == NULL)
458                         GOTO(out, err = -EINVAL);
459
460                 obd_class_remove_user_exp(ocus, exp);
461                 err = obd_disconnect(exp, 0);
462                 GOTO(out, err);
463         }
464
465         case OBD_IOC_NO_TRANSNO: {
466                 if (!obd->obd_attached) {
467                         CERROR("Device %d not attached\n", obd->obd_minor);
468                         GOTO(out, err = -ENODEV);
469                 }
470                 CDEBUG(D_IOCTL,
471                        "disabling committed-transno notifications on %d\n",
472                        obd->obd_minor);
473                 obd->obd_no_transno = 1;
474                 GOTO(out, err = 0);
475         }
476
477         case OBD_IOC_CLOSE_UUID: {
478                 struct lustre_peer peer;
479                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
480                        data->ioc_inlbuf1);
481                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer);
482                 GOTO(out, err = 0);
483         }
484
485         default: {
486                 // obd_data2conn(&conn, data);
487                 struct obd_class_user_conn *oconn;
488
489                 if (list_empty(&ocus->ocus_conns)) 
490                         GOTO(out, err = -ENOTCONN);
491                         
492                 oconn = list_entry(ocus->ocus_conns.next, struct obd_class_user_conn, ocuc_chain);
493                 err = obd_iocontrol(cmd, oconn->ocuc_exp, len, data, NULL);
494                 if (err)
495                         GOTO(out, err);
496
497                 err = copy_to_user((void *)arg, data, len);
498                 if (err)
499                         err = -EFAULT;
500                 GOTO(out, err);
501         }
502         }
503
504  out:
505         if (buf)
506                 obd_ioctl_freedata(buf, len);
507         if (serialised)
508                 up(&obd_conf_sem);
509         RETURN(err);
510 } /* class_handle_ioctl */
511
512
513
514 #define OBD_MINOR 241
515 #ifdef __KERNEL__
516 /* to control /dev/obd */
517 static int obd_class_ioctl(struct inode *inode, struct file *filp,
518                            unsigned int cmd, unsigned long arg)
519 {
520         return class_handle_ioctl(filp->private_data, cmd, arg);
521 }
522
523 /* declare character device */
524 static struct file_operations obd_psdev_fops = {
525         ioctl:   obd_class_ioctl,       /* ioctl */
526         open:    obd_class_open,        /* open */
527         release: obd_class_release,     /* release */
528 };
529
530 /* modules setup */
531 static struct miscdevice obd_psdev = {
532         OBD_MINOR,
533         "obd_psdev",
534         &obd_psdev_fops
535 };
536 #else
537 void *obd_psdev = NULL;
538 #endif
539
540 EXPORT_SYMBOL(obd_dev);
541 EXPORT_SYMBOL(obdo_cachep);
542 EXPORT_SYMBOL(obd_memory);
543 EXPORT_SYMBOL(obd_memmax);
544 EXPORT_SYMBOL(obd_fail_loc);
545 EXPORT_SYMBOL(obd_timeout);
546 EXPORT_SYMBOL(obd_lustre_upcall);
547 EXPORT_SYMBOL(obd_sync_filter);
548 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
549 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
550 EXPORT_SYMBOL(proc_lustre_root);
551
552 EXPORT_SYMBOL(class_register_type);
553 EXPORT_SYMBOL(class_unregister_type);
554 EXPORT_SYMBOL(class_get_type);
555 EXPORT_SYMBOL(class_put_type);
556 EXPORT_SYMBOL(class_name2dev);
557 EXPORT_SYMBOL(class_name2obd);
558 EXPORT_SYMBOL(class_uuid2dev);
559 EXPORT_SYMBOL(class_uuid2obd);
560 EXPORT_SYMBOL(class_find_client_obd);
561 EXPORT_SYMBOL(__class_export_put);
562 EXPORT_SYMBOL(class_new_export);
563 EXPORT_SYMBOL(class_unlink_export);
564 EXPORT_SYMBOL(class_import_get);
565 EXPORT_SYMBOL(class_import_put);
566 EXPORT_SYMBOL(class_new_import);
567 EXPORT_SYMBOL(class_destroy_import);
568 EXPORT_SYMBOL(class_connect);
569 EXPORT_SYMBOL(class_conn2export);
570 EXPORT_SYMBOL(class_exp2obd);
571 EXPORT_SYMBOL(class_conn2obd);
572 EXPORT_SYMBOL(class_exp2cliimp);
573 EXPORT_SYMBOL(class_conn2cliimp);
574 EXPORT_SYMBOL(class_disconnect);
575 EXPORT_SYMBOL(class_disconnect_exports);
576
577 EXPORT_SYMBOL(osic_init);
578 EXPORT_SYMBOL(osic_release);
579 EXPORT_SYMBOL(osic_add_one);
580 EXPORT_SYMBOL(osic_wait);
581 EXPORT_SYMBOL(osic_complete_one);
582
583 /* uuid.c */
584 EXPORT_SYMBOL(class_uuid_unparse);
585 EXPORT_SYMBOL(lustre_uuid_to_peer);
586
587 EXPORT_SYMBOL(class_handle_hash);
588 EXPORT_SYMBOL(class_handle_unhash);
589 EXPORT_SYMBOL(class_handle2object);
590
591 /* config.c */
592 EXPORT_SYMBOL(class_get_profile);
593 EXPORT_SYMBOL(class_process_config);
594 EXPORT_SYMBOL(class_config_parse_llog);
595 EXPORT_SYMBOL(class_config_dump_llog);
596 EXPORT_SYMBOL(class_attach);
597 EXPORT_SYMBOL(class_setup);
598 EXPORT_SYMBOL(class_cleanup);
599 EXPORT_SYMBOL(class_detach);
600
601 #ifdef LPROCFS
602 int obd_proc_read_version(char *page, char **start, off_t off, int count,
603                           int *eof, void *data)
604 {
605         *eof = 1;
606         return snprintf(page, count, "%s\n", BUILD_VERSION);
607 }
608
609 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
610                          int *eof, void *data)
611 {
612         *eof = 1;
613         return snprintf(page, count, "%s\n",
614 #ifdef ENABLE_PINGER
615                         "on"
616 #else
617                         "off"
618 #endif
619                        );
620 }
621
622 /* Root for /proc/fs/lustre */
623 struct proc_dir_entry *proc_lustre_root = NULL;
624 struct lprocfs_vars lprocfs_base[] = {
625         { "version", obd_proc_read_version, NULL, NULL },
626         { "pinger", obd_proc_read_pinger, NULL, NULL },
627         { 0 }
628 };
629
630 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
631 {
632         if (*pos >= MAX_OBD_DEVICES)
633                 return NULL;
634         return &obd_dev[*pos];
635 }
636
637 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
638 {
639 }
640
641 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
642 {
643         ++*pos;
644         if (*pos >= MAX_OBD_DEVICES)
645                 return NULL;
646         return &obd_dev[*pos];
647 }
648
649 static int obd_device_list_seq_show(struct seq_file *p, void *v)
650 {
651         struct obd_device *obd = (struct obd_device *)v;
652         int index = obd - &obd_dev[0];
653         char *status;
654
655         if (!obd->obd_type)
656                 return 0;
657         if (obd->obd_stopping)
658                 status = "ST";
659         else if (obd->obd_set_up)
660                 status = "UP";
661         else if (obd->obd_attached)
662                 status = "AT";
663         else
664                 status = "--";
665
666         return seq_printf(p, "%3d %s %s %s %s %d\n",
667                           (int)index, status, obd->obd_type->typ_name,
668                           obd->obd_name, obd->obd_uuid.uuid,
669                           atomic_read(&obd->obd_refcount));
670 }
671
672 struct seq_operations obd_device_list_sops = {
673         .start = obd_device_list_seq_start,
674         .stop = obd_device_list_seq_stop,
675         .next = obd_device_list_seq_next,
676         .show = obd_device_list_seq_show,
677 };
678
679 static int obd_device_list_open(struct inode *inode, struct file *file)
680 {
681         struct proc_dir_entry *dp = inode->u.generic_ip;
682         struct seq_file *seq;
683         int rc = seq_open(file, &obd_device_list_sops);
684
685         if (rc)
686                 return rc;
687
688         seq = file->private_data;
689         seq->private = dp->data;
690
691         return 0;
692 }
693
694 struct file_operations obd_device_list_fops = {
695         .open = obd_device_list_open,
696         .read = seq_read,
697         .llseek = seq_lseek,
698         .release = seq_release,
699 };
700 #endif
701
702 #ifdef __KERNEL__
703 static int __init init_obdclass(void)
704 #else
705 int init_obdclass(void)
706 #endif
707 {
708         struct obd_device *obd;
709 #ifdef LPROCFS
710         struct proc_dir_entry *entry;
711 #endif
712         int err;
713         int i;
714
715         printk(KERN_INFO "Lustre: OBD class driver Build Version: "
716                BUILD_VERSION", info@clusterfs.com\n");
717
718         class_init_uuidlist();
719         err = class_handle_init();
720         if (err)
721                 return err;
722
723         sema_init(&obd_conf_sem, 1);
724         INIT_LIST_HEAD(&obd_types);
725
726         err = misc_register(&obd_psdev);
727         if (err) {
728                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
729                 return err;
730         }
731
732         /* This struct is already zerod for us (static global) */
733         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
734                 obd->obd_minor = i;
735
736         err = obd_init_caches();
737         if (err)
738                 return err;
739
740 #ifdef __KERNEL__
741         obd_sysctl_init();
742 #endif
743
744 #ifdef LPROCFS
745         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
746         if (!proc_lustre_root) {
747                 printk(KERN_ERR
748                        "LustreError: error registering /proc/fs/lustre\n");
749                 RETURN(-ENOMEM);
750         }
751         proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
752         entry = create_proc_entry("devices", 0444, proc_lustre_root);
753         if (entry == NULL) {
754                 printk(KERN_ERR "LustreError: error registering "
755                        "/proc/fs/lustre/devices\n");
756                 lprocfs_remove(proc_lustre_root);
757                 RETURN(-ENOMEM);
758         }
759         entry->proc_fops = &obd_device_list_fops;
760 #endif
761         return 0;
762 }
763
764 #ifdef __KERNEL__
765 static void /*__exit*/ cleanup_obdclass(void)
766 #else
767 static void cleanup_obdclass(void)
768 #endif
769 {
770         int i, leaked;
771         ENTRY;
772
773         misc_deregister(&obd_psdev);
774         for (i = 0; i < MAX_OBD_DEVICES; i++) {
775                 struct obd_device *obd = &obd_dev[i];
776                 if (obd->obd_type && obd->obd_set_up &&
777                     OBT(obd) && OBP(obd, detach)) {
778                         /* XXX should this call generic detach otherwise? */
779                         OBP(obd, detach)(obd);
780                 }
781         }
782
783         obd_cleanup_caches();
784 #ifdef __KERNEL__
785         obd_sysctl_clean();
786 #endif
787 #ifdef LPROCFS
788         if (proc_lustre_root) {
789                 lprocfs_remove(proc_lustre_root);
790                 proc_lustre_root = NULL;
791         }
792 #endif
793
794         class_handle_cleanup();
795         class_exit_uuidlist();
796
797         leaked = atomic_read(&obd_memory);
798         CDEBUG(leaked ? D_ERROR : D_INFO,
799                "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
800
801         EXIT;
802 }
803
804 /* Check that we're building against the appropriate version of the Lustre
805  * kernel patch */
806 #ifdef __KERNEL__
807 #include <linux/lustre_version.h>
808 #define LUSTRE_MIN_VERSION 28
809 #define LUSTRE_MAX_VERSION 32
810 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
811 # error Cannot continue: Your Lustre kernel patch is older than the sources
812 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
813 # error Cannot continue: Your Lustre sources are older than the kernel patch
814 #endif
815  #else
816 # warning "Lib Lustre - no versioning information"
817 #endif
818
819 #ifdef __KERNEL__
820 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
821 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
822 MODULE_LICENSE("GPL");
823
824 module_init(init_obdclass);
825 module_exit(cleanup_obdclass);
826 #endif