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