Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[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 #define EXPORT_SYMTAB
29 #ifdef __KERNEL__
30 #include <linux/config.h> /* for CONFIG_PROC_FS */
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/major.h>
35 #include <linux/sched.h>
36 #include <linux/lp.h>
37 #include <linux/slab.h>
38 #include <linux/ioport.h>
39 #include <linux/fcntl.h>
40 #include <linux/delay.h>
41 #include <linux/skbuff.h>
42 #include <linux/proc_fs.h>
43 #include <linux/fs.h>
44 #include <linux/poll.h>
45 #include <linux/init.h>
46 #include <linux/list.h>
47 #include <linux/highmem.h>
48 #include <asm/io.h>
49 #include <asm/ioctls.h>
50 #include <asm/system.h>
51 #include <asm/poll.h>
52 #include <asm/uaccess.h>
53 #include <linux/miscdevice.h>
54 #include <linux/smp_lock.h>
55 #else
56
57 # include <liblustre.h>
58
59 #endif
60
61 #include <linux/obd_support.h>
62 #include <linux/obd_class.h>
63 #include <linux/lustre_debug.h>
64 #include <linux/lprocfs_status.h>
65 #include <portals/lib-types.h> /* for PTL_MD_MAX_IOV */
66 #include <linux/lustre_build_version.h>
67
68 struct semaphore obd_conf_sem;   /* serialize configuration commands */
69 struct obd_device obd_dev[MAX_OBD_DEVICES];
70 struct list_head obd_types;
71 atomic_t obd_memory;
72 int obd_memmax;
73
74 /* Root for /proc/lustre */
75 struct proc_dir_entry *proc_lustre_root = NULL;
76 int obd_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data);
77 struct lprocfs_vars lprocfs_version[] = {{"version", obd_proc_read_version, NULL, NULL },{NULL,NULL,NULL,NULL}};
78 int proc_version;
79
80 /* The following are visible and mutable through /proc/sys/lustre/. */
81 unsigned long obd_fail_loc;
82 unsigned long obd_timeout = 100;
83 unsigned long obd_bulk_timeout = 1;
84 char obd_lustre_upcall[128] = "/usr/lib/lustre/lustre_upcall";
85 unsigned long obd_sync_filter; /* = 0, don't sync by default */
86
87 #ifdef __KERNEL__
88 /*  opening /dev/obd */
89 static int obd_class_open(struct inode * inode, struct file * file)
90 {
91         struct obd_class_user_state *ocus;
92         ENTRY;
93
94         OBD_ALLOC(ocus, sizeof(*ocus));
95         if (ocus == NULL)
96                 return (-ENOMEM);
97
98         INIT_LIST_HEAD(&ocus->ocus_conns);
99         file->private_data = ocus;
100
101         PORTAL_MODULE_USE;
102         RETURN(0);
103 }
104
105 /*  closing /dev/obd */
106 static int obd_class_release(struct inode * inode, struct file * file)
107 {
108         struct obd_class_user_state *ocus = file->private_data;
109         struct obd_class_user_conn  *c;
110         ENTRY;
111
112         while (!list_empty (&ocus->ocus_conns)) {
113                 c = list_entry (ocus->ocus_conns.next,
114                                 struct obd_class_user_conn, ocuc_chain);
115                 list_del (&c->ocuc_chain);
116
117                 CDEBUG (D_IOCTL, "Auto-disconnect %p\n", &c->ocuc_conn);
118
119                 down (&obd_conf_sem);
120                 obd_disconnect (&c->ocuc_conn, 0);
121                 up (&obd_conf_sem);
122
123                 OBD_FREE (c, sizeof (*c));
124         }
125
126         OBD_FREE (ocus, sizeof (*ocus));
127
128         PORTAL_MODULE_UNUSE;
129         RETURN(0);
130 }
131 #endif
132
133 static int
134 obd_class_add_user_conn (struct obd_class_user_state *ocus,
135                          struct lustre_handle *conn)
136 {
137         struct obd_class_user_conn *c;
138
139         /* NB holding obd_conf_sem */
140
141         OBD_ALLOC (c, sizeof (*c));
142         if (ocus == NULL)
143                 return (-ENOMEM);
144
145         c->ocuc_conn = *conn;
146         list_add (&c->ocuc_chain, &ocus->ocus_conns);
147         return (0);
148 }
149
150 static void
151 obd_class_remove_user_conn (struct obd_class_user_state *ocus,
152                             struct lustre_handle *conn)
153 {
154         struct list_head *e;
155         struct obd_class_user_conn *c;
156
157         /* NB holding obd_conf_sem or last reference */
158
159         list_for_each (e, &ocus->ocus_conns) {
160                 c = list_entry (e, struct obd_class_user_conn, ocuc_chain);
161                 if (conn->cookie == c->ocuc_conn.cookie) {
162                         list_del (&c->ocuc_chain);
163                         OBD_FREE (c, sizeof (*c));
164                         return;
165                 }
166         }
167 }
168
169 static inline void obd_data2conn(struct lustre_handle *conn,
170                                  struct obd_ioctl_data *data)
171 {
172         memset(conn, 0, sizeof *conn);
173         conn->cookie = data->ioc_cookie;
174 }
175
176 static inline void obd_conn2data(struct obd_ioctl_data *data,
177                                  struct lustre_handle *conn)
178 {
179         data->ioc_cookie = conn->cookie;
180 }
181
182 static void dump_exports(struct obd_device *obd)
183 {
184         struct list_head *tmp, *n;
185
186         list_for_each_safe(tmp, n, &obd->obd_exports) {
187                 struct obd_export *exp = list_entry(tmp, struct obd_export,
188                                                     exp_obd_chain);
189                 CDEBUG(D_ERROR, "%s: %p %s %d %d %p\n",
190                        obd->obd_name, exp, exp->exp_client_uuid.uuid,
191                        atomic_read(&exp->exp_refcount),
192                        exp->exp_failed, exp->exp_outstanding_reply );
193         }
194 }
195
196 int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd,
197                        unsigned long arg)
198 {
199         char *buf = NULL;
200         struct obd_ioctl_data *data;
201         struct portals_debug_ioctl_data *debug_data;
202         struct obd_device *obd = ocus->ocus_current_obd;
203         struct lustre_handle conn;
204         int err = 0, len = 0, serialised = 0;
205         ENTRY;
206
207         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
208                 RETURN(err = -ENOTTY);
209
210         /* only for debugging */
211         if (cmd == PTL_IOC_DEBUG_MASK) {
212                 debug_data = (struct portals_debug_ioctl_data*)arg;
213                 portal_subsystem_debug = debug_data->subs;
214                 portal_debug = debug_data->debug;
215                 return 0;
216         }
217
218         switch (cmd) {
219         case OBD_IOC_BRW_WRITE:
220         case OBD_IOC_BRW_READ:
221         case OBD_IOC_GETATTR:
222         case ECHO_IOC_ENQUEUE:
223         case ECHO_IOC_CANCEL:
224                 break;
225         default:
226                 down(&obd_conf_sem);
227                 serialised = 1;
228                 break;
229         }
230
231         CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
232         if (!obd && cmd != OBD_IOC_DEVICE &&
233             cmd != OBD_IOC_LIST && cmd != OBD_GET_VERSION &&
234             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_UUID2DEV &&
235             cmd != OBD_IOC_NEWDEV && cmd != OBD_IOC_ADD_UUID &&
236             cmd != OBD_IOC_DEL_UUID && cmd != OBD_IOC_CLOSE_UUID) {
237                 CERROR("OBD ioctl: No device\n");
238                 GOTO(out, err = -EINVAL);
239         }
240         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
241                 CERROR("OBD ioctl: data error\n");
242                 GOTO(out, err = -EINVAL);
243         }
244         data = (struct obd_ioctl_data *)buf;
245
246         switch (cmd) {
247         case OBD_IOC_DEVICE: {
248                 CDEBUG(D_IOCTL, "\n");
249                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
250                         CERROR("OBD ioctl: DEVICE invalid device %d\n",
251                                data->ioc_dev);
252                         GOTO(out, err = -EINVAL);
253                 }
254                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
255
256                 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
257                 GOTO(out, err = 0);
258         }
259
260         case OBD_IOC_LIST: {
261                 int i;
262                 char *buf2 = data->ioc_bulk;
263                 int remains = data->ioc_inllen1;
264
265                 if (!data->ioc_inlbuf1) {
266                         CERROR("No buffer passed!\n");
267                         GOTO(out, err = -EINVAL);
268                 }
269
270
271                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
272                         int l;
273                         char *status;
274                         struct obd_device *obd = &obd_dev[i];
275
276                         if (!obd->obd_type)
277                                 continue;
278                         if (obd->obd_stopping)
279                                 status = "ST";
280                         else if (obd->obd_set_up)
281                                 status = "UP";
282                         else if (obd->obd_attached)
283                                 status = "AT";
284                         else
285                                 status = "-";
286                         l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
287                                      i, status, obd->obd_type->typ_name,
288                                      obd->obd_name, obd->obd_uuid.uuid,
289                                      obd->obd_type->typ_refcnt);
290                         buf2 +=l;
291                         remains -=l;
292                         if (remains <= 0) {
293                                 CERROR("not enough space for device listing\n");
294                                 break;
295                         }
296                 }
297
298                 err = copy_to_user((void *)arg, data, len);
299                 if (err)
300                         err = -EFAULT;
301                 GOTO(out, err);
302         }
303
304         case OBD_GET_VERSION:
305                 if (!data->ioc_inlbuf1) {
306                         CERROR("No buffer passed in ioctl\n");
307                         GOTO(out, err = -EINVAL);
308                 }
309
310                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
311                         CERROR("ioctl buffer too small to hold version\n");
312                         GOTO(out, err = -EINVAL);
313                 }
314
315                 memcpy(data->ioc_bulk, BUILD_VERSION,
316                        strlen(BUILD_VERSION) + 1);
317
318                 err = copy_to_user((void *)arg, data, len);
319                 if (err)
320                         err = -EFAULT;
321                 GOTO(out, err);
322
323         case OBD_IOC_NAME2DEV: {
324                 /* Resolve a device name.  This does not change the
325                  * currently selected device.
326                  */
327                 int dev;
328
329                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
330                         CERROR("No name passed,!\n");
331                         GOTO(out, err = -EINVAL);
332                 }
333                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
334                         CERROR("Name not nul terminated!\n");
335                         GOTO(out, err = -EINVAL);
336                 }
337
338                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
339                 dev = class_name2dev(data->ioc_inlbuf1);
340                 data->ioc_dev = dev;
341                 if (dev == -1) {
342                         CDEBUG(D_IOCTL, "No device for name %s!\n",
343                                data->ioc_inlbuf1);
344                         GOTO(out, err = -EINVAL);
345                 }
346
347                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
348                        dev);
349                 err = copy_to_user((void *)arg, data, sizeof(*data));
350                 if (err)
351                         err = -EFAULT;
352                 GOTO(out, err);
353         }
354
355         case OBD_IOC_UUID2DEV: {
356                 /* Resolve a device uuid.  This does not change the
357                  * currently selected device.
358                  */
359                 int dev;
360                 struct obd_uuid uuid;
361
362                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
363                         CERROR("No UUID passed!\n");
364                         GOTO(out, err = -EINVAL);
365                 }
366                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
367                         CERROR("UUID not NUL terminated!\n");
368                         GOTO(out, err = -EINVAL);
369                 }
370
371                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
372                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
373                 dev = class_uuid2dev(&uuid);
374                 data->ioc_dev = dev;
375                 if (dev == -1) {
376                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
377                                data->ioc_inlbuf1);
378                         GOTO(out, err = -EINVAL);
379                 }
380
381                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
382                        dev);
383                 err = copy_to_user((void *)arg, data, sizeof(*data));
384                 if (err)
385                         err = -EFAULT;
386                 GOTO(out, err);
387         }
388
389
390
391         case OBD_IOC_NEWDEV: {
392                 int dev = -1;
393                 int i;
394
395                 ocus->ocus_current_obd = NULL;
396                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
397                         struct obd_device *obd = &obd_dev[i];
398                         if (!obd->obd_type) {
399                                 ocus->ocus_current_obd = obd;
400                                 dev = i;
401                                 break;
402                         }
403                 }
404
405
406                 data->ioc_dev = dev;
407                 if (dev == -1)
408                         GOTO(out, err = -EINVAL);
409
410                 err = copy_to_user((void *)arg, data, sizeof(*data));
411                 if (err)
412                         err = -EFAULT;
413                 GOTO(out, err);
414         }
415
416         case OBD_IOC_ATTACH: {
417                 struct obd_type *type;
418                 int minor, len;
419
420                 /* have we attached a type to this device */
421                 if (obd->obd_attached|| obd->obd_type) {
422                         CERROR("OBD: Device %d already typed as %s.\n",
423                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
424                         GOTO(out, err = -EBUSY);
425                 }
426
427                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
428                         CERROR("No type passed!\n");
429                         GOTO(out, err = -EINVAL);
430                 }
431                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
432                         CERROR("Type not nul terminated!\n");
433                         GOTO(out, err = -EINVAL);
434                 }
435                 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
436                         CERROR("No name passed!\n");
437                         GOTO(out, err = -EINVAL);
438                 }
439                 if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
440                         CERROR("Name not nul terminated!\n");
441                         GOTO(out, err = -EINVAL);
442                 }
443                 if (!data->ioc_inllen3 || !data->ioc_inlbuf3) {
444                         CERROR("No UUID passed!\n");
445                         GOTO(out, err = -EINVAL);
446                 }
447                 if (data->ioc_inlbuf3[data->ioc_inllen3 - 1] != 0) {
448                         CERROR("UUID not nul terminated!\n");
449                         GOTO(out, err = -EINVAL);
450                 }
451
452                 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
453                        MKSTR(data->ioc_inlbuf1),
454                        MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
455
456                 /* find the type */
457                 type = class_get_type(data->ioc_inlbuf1);
458                 if (!type) {
459                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
460                         GOTO(out, err = -EINVAL);
461                 }
462
463                 minor = obd->obd_minor;
464                 memset(obd, 0, sizeof(*obd));
465                 obd->obd_minor = minor;
466                 obd->obd_type = type;
467                 INIT_LIST_HEAD(&obd->obd_exports);
468                 obd->obd_num_exports = 0;
469                 INIT_LIST_HEAD(&obd->obd_imports);
470                 spin_lock_init(&obd->obd_dev_lock);
471                 init_waitqueue_head(&obd->obd_refcount_waitq);
472
473                 /* XXX belong ins setup not attach  */
474                 /* recovery data */
475                 spin_lock_init(&obd->obd_processing_task_lock);
476                 init_waitqueue_head(&obd->obd_next_transno_waitq);
477                 INIT_LIST_HEAD(&obd->obd_recovery_queue);
478                 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
479
480                 init_waitqueue_head(&obd->obd_commit_waitq);
481
482                 len = strlen(data->ioc_inlbuf2) + 1;
483                 OBD_ALLOC(obd->obd_name, len);
484                 if (!obd->obd_name) {
485                         class_put_type(obd->obd_type);
486                         obd->obd_type = NULL;
487                         GOTO(out, err = -ENOMEM);
488                 }
489                 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
490
491                 len = strlen(data->ioc_inlbuf3);
492                 if (len >= sizeof(obd->obd_uuid)) {
493                         CERROR("uuid must be < "LPSZ" bytes long\n",
494                                sizeof(obd->obd_uuid));
495                         if (obd->obd_name)
496                                 OBD_FREE(obd->obd_name,
497                                          strlen(obd->obd_name) + 1);
498                         class_put_type(obd->obd_type);
499                         obd->obd_type = NULL;
500                         GOTO(out, err = -EINVAL);
501                 }
502                 memcpy(obd->obd_uuid.uuid, data->ioc_inlbuf3, len);
503
504                 /* do the attach */
505                 if (OBP(obd, attach))
506                         err = OBP(obd,attach)(obd, sizeof(*data), data);
507                 if (err) {
508                         if(data->ioc_inlbuf2)
509                                 OBD_FREE(obd->obd_name,
510                                          strlen(obd->obd_name) + 1);
511                         class_put_type(obd->obd_type);
512                         obd->obd_type = NULL;
513                 } else {
514                         obd->obd_attached = 1;
515
516                         type->typ_refcnt++;
517                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
518                                obd->obd_minor, data->ioc_inlbuf1);
519                 }
520
521                 GOTO(out, err);
522         }
523
524         case OBD_IOC_DETACH: {
525                 ENTRY;
526                 if (obd->obd_set_up) {
527                         CERROR("OBD device %d still set up\n", obd->obd_minor);
528                         GOTO(out, err = -EBUSY);
529                 }
530                 if (!obd->obd_attached) {
531                         CERROR("OBD device %d not attached\n", obd->obd_minor);
532                         GOTO(out, err = -ENODEV);
533                 }
534                 if (OBP(obd, detach))
535                         err = OBP(obd,detach)(obd);
536
537                 if (obd->obd_name) {
538                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
539                         obd->obd_name = NULL;
540                 }
541
542                 obd->obd_attached = 0;
543                 obd->obd_type->typ_refcnt--;
544                 class_put_type(obd->obd_type);
545                 obd->obd_type = NULL;
546                 GOTO(out, err = 0);
547         }
548
549         case OBD_IOC_SETUP: {
550                 /* have we attached a type to this device? */
551                 if (!obd->obd_attached) {
552                         CERROR("Device %d not attached\n", obd->obd_minor);
553                         GOTO(out, err = -ENODEV);
554                 }
555
556                 /* has this been done already? */
557                 if (obd->obd_set_up) {
558                         CERROR("Device %d already setup (type %s)\n",
559                                obd->obd_minor, obd->obd_type->typ_name);
560                         GOTO(out, err = -EBUSY);
561                 }
562
563                 atomic_set(&obd->obd_refcount, 0);
564
565                 if ( OBT(obd) && OBP(obd, setup) )
566                         err = obd_setup(obd, sizeof(*data), data);
567
568                 if (!err) {
569                         obd->obd_type->typ_refcnt++;
570                         obd->obd_set_up = 1;
571                         atomic_inc(&obd->obd_refcount);
572                 }
573
574                 GOTO(out, err);
575         }
576         case OBD_IOC_CLEANUP: {
577                 int force = 0, failover = 0;
578                 char * flag;
579
580                 if (!obd->obd_set_up) {
581                         CERROR("Device %d not setup\n", obd->obd_minor);
582                         GOTO(out, err = -ENODEV);
583                 }
584
585                 if (data->ioc_inlbuf1) {
586                         for (flag = data->ioc_inlbuf1; *flag != 0; flag++)
587                                 switch (*flag) {
588                                 case 'F':
589                                         force = 1;
590                                         break;
591                                 case 'A':
592                                         failover = 1;
593                                         break;
594                                 default:
595                                         CERROR("unrecognised flag '%c'\n", 
596                                                *flag);
597                                 }
598                 }
599                 
600                 if (atomic_read(&obd->obd_refcount) == 1 || force) {
601                         /* this will stop new connections, and need to
602                            do it before class_disconnect_exports() */
603                         obd->obd_stopping = 1;
604                 }
605
606                 if (atomic_read(&obd->obd_refcount) > 1) {
607                         struct l_wait_info lwi = LWI_TIMEOUT_INTR(60 * HZ, NULL,
608                                                                   NULL, NULL);
609                         int rc;
610                         
611                         if (!force) {
612                                 CERROR("OBD device %d (%p) has refcount %d\n",
613                                        obd->obd_minor, obd, 
614                                        atomic_read(&obd->obd_refcount));
615                                 dump_exports(obd);
616                                 GOTO(out, err = -EBUSY);
617                         }
618                         class_disconnect_exports(obd, failover);
619                         CDEBUG(D_IOCTL, 
620                                "%s: waiting for obd refs to go away: %d\n", 
621                                obd->obd_name, atomic_read(&obd->obd_refcount));
622                 
623                         rc = l_wait_event(obd->obd_refcount_waitq,
624                                      atomic_read(&obd->obd_refcount) < 2, &lwi);
625                         if (rc == 0) {
626                                 LASSERT(atomic_read(&obd->obd_refcount) == 1);
627                         } else {
628                                 CERROR("wait cancelled cleaning anyway. "
629                                        "refcount: %d\n",
630                                        atomic_read(&obd->obd_refcount));
631                                 dump_exports(obd);
632                         }
633                         CDEBUG(D_IOCTL, "%s: awake, now finishing cleanup\n", 
634                                obd->obd_name);
635                 }
636
637                 if (OBT(obd) && OBP(obd, cleanup))
638                         err = obd_cleanup(obd, force, failover);
639
640                 if (!err) {
641                         obd->obd_set_up = obd->obd_stopping = 0;
642                         obd->obd_type->typ_refcnt--;
643                         atomic_dec(&obd->obd_refcount);
644                         /* XXX this should be an LASSERT */
645                         if (atomic_read(&obd->obd_refcount) > 0) 
646                                 CERROR("%s still has refcount %d after "
647                                        "cleanup.\n", obd->obd_name,
648                                        atomic_read(&obd->obd_refcount));
649                 }
650
651                 GOTO(out, err);
652         }
653
654         case OBD_IOC_CONNECT: {
655                 struct obd_uuid cluuid = { "OBD_CLASS_UUID" };
656                 obd_data2conn(&conn, data);
657
658                 err = obd_connect(&conn, obd, &cluuid);
659
660                 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.cookie);
661                 obd_conn2data(data, &conn);
662                 if (err)
663                         GOTO(out, err);
664
665                 err = obd_class_add_user_conn (ocus, &conn);
666                 if (err != 0) {
667                         obd_disconnect (&conn, 0);
668                         GOTO (out, err);
669                 }
670
671                 err = copy_to_user((void *)arg, data, sizeof(*data));
672                 if (err != 0) {
673                         obd_class_remove_user_conn (ocus, &conn);
674                         obd_disconnect (&conn, 0);
675                         GOTO (out, err = -EFAULT);
676                 }
677                 GOTO(out, err);
678         }
679
680         case OBD_IOC_DISCONNECT: {
681                 obd_data2conn(&conn, data);
682                 obd_class_remove_user_conn (ocus, &conn);
683                 err = obd_disconnect(&conn, 0);
684                 GOTO(out, err);
685         }
686
687         case OBD_IOC_NO_TRANSNO: {
688                 if (!obd->obd_attached) {
689                         CERROR("Device %d not attached\n", obd->obd_minor);
690                         GOTO(out, err = -ENODEV);
691                 }
692                 CDEBUG(D_IOCTL,
693                        "disabling committed-transno notifications on %d\n",
694                        obd->obd_minor);
695                 obd->obd_no_transno = 1;
696                 GOTO(out, err = 0);
697         }
698
699         case OBD_IOC_CLOSE_UUID: {
700                 struct lustre_peer peer;
701                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
702                        data->ioc_inlbuf1);
703                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer);
704                 GOTO(out, err = 0);
705         }
706         case OBD_IOC_ADD_UUID: {
707                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
708                        ", nal %d\n", data->ioc_inlbuf1, data->ioc_nid,
709                        data->ioc_nal);
710
711                 err = class_add_uuid(data->ioc_inlbuf1, data->ioc_nid,
712                                      data->ioc_nal);
713                 GOTO(out, err);
714         }
715         case OBD_IOC_DEL_UUID: {
716                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
717                        data->ioc_inlbuf1 == NULL ? "<all uuids>" :
718                        data->ioc_inlbuf1);
719
720                 err = class_del_uuid(data->ioc_inlbuf1);
721                 GOTO(out, err);
722         }
723         default: { 
724                 // obd_data2conn(&conn, data);
725                 struct obd_class_user_conn *oconn = list_entry(ocus->ocus_conns.next, struct obd_class_user_conn, ocuc_chain);
726                 err = obd_iocontrol(cmd, &oconn->ocuc_conn, len, data, NULL);
727                 if (err)
728                         GOTO(out, err);
729
730                 err = copy_to_user((void *)arg, data, len);
731                 if (err)
732                         err = -EFAULT;
733                 GOTO(out, err);
734         }
735         }
736
737  out:
738         if (buf)
739                 obd_ioctl_freedata(buf, len);
740         if (serialised)
741                 up(&obd_conf_sem);
742         RETURN(err);
743 } /* class_handle_ioctl */
744
745
746
747 #define OBD_MINOR 241
748 #ifdef __KERNEL__
749 /* to control /dev/obd */
750 static int obd_class_ioctl(struct inode *inode, struct file *filp,
751                            unsigned int cmd, unsigned long arg)
752 {
753         return class_handle_ioctl(filp->private_data, cmd, arg);
754 }
755
756 /* declare character device */
757 static struct file_operations obd_psdev_fops = {
758         ioctl:   obd_class_ioctl,       /* ioctl */
759         open:    obd_class_open,        /* open */
760         release: obd_class_release,     /* release */
761 };
762
763 /* modules setup */
764 static struct miscdevice obd_psdev = {
765         OBD_MINOR,
766         "obd_psdev",
767         &obd_psdev_fops
768 };
769 #else
770 void *obd_psdev = NULL;
771 #endif
772
773 EXPORT_SYMBOL(obd_dev);
774 EXPORT_SYMBOL(obdo_cachep);
775 EXPORT_SYMBOL(obd_memory);
776 EXPORT_SYMBOL(obd_memmax);
777 EXPORT_SYMBOL(obd_fail_loc);
778 EXPORT_SYMBOL(obd_timeout);
779 EXPORT_SYMBOL(obd_lustre_upcall);
780 EXPORT_SYMBOL(obd_sync_filter);
781 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
782 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
783 EXPORT_SYMBOL(proc_lustre_root);
784
785 EXPORT_SYMBOL(lctl_fake_uuid);
786
787 EXPORT_SYMBOL(class_register_type);
788 EXPORT_SYMBOL(class_unregister_type);
789 EXPORT_SYMBOL(class_get_type);
790 EXPORT_SYMBOL(class_put_type);
791 EXPORT_SYMBOL(class_name2dev);
792 EXPORT_SYMBOL(class_name2obd);
793 EXPORT_SYMBOL(class_uuid2dev);
794 EXPORT_SYMBOL(class_uuid2obd);
795 EXPORT_SYMBOL(class_export_get);
796 EXPORT_SYMBOL(class_export_put);
797 EXPORT_SYMBOL(class_new_export);
798 EXPORT_SYMBOL(class_unlink_export);
799 EXPORT_SYMBOL(class_import_get);
800 EXPORT_SYMBOL(class_import_put);
801 EXPORT_SYMBOL(class_new_import);
802 EXPORT_SYMBOL(class_destroy_import);
803 EXPORT_SYMBOL(class_connect);
804 EXPORT_SYMBOL(class_conn2export);
805 EXPORT_SYMBOL(class_conn2obd);
806 EXPORT_SYMBOL(class_conn2cliimp);
807 EXPORT_SYMBOL(class_conn2ldlmimp);
808 EXPORT_SYMBOL(class_disconnect);
809 EXPORT_SYMBOL(class_disconnect_exports);
810 EXPORT_SYMBOL(lustre_uuid_to_peer);
811
812 /* uuid.c */
813 EXPORT_SYMBOL(class_uuid_unparse);
814 EXPORT_SYMBOL(client_tgtuuid2obd);
815
816 EXPORT_SYMBOL(class_handle_hash);
817 EXPORT_SYMBOL(class_handle_unhash);
818 EXPORT_SYMBOL(class_handle2object);
819
820 #ifdef __KERNEL__
821 static int __init init_obdclass(void)
822 #else
823 int init_obdclass(void)
824 #endif
825 {
826         struct obd_device *obd;
827         int err;
828         int i;
829
830         printk(KERN_INFO "OBD class driver Build Version: " BUILD_VERSION
831                       ", info@clusterfs.com\n");
832
833         class_init_uuidlist();
834         class_handle_init();
835
836         sema_init(&obd_conf_sem, 1);
837         INIT_LIST_HEAD(&obd_types);
838
839         if ((err = misc_register(&obd_psdev))) {
840                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
841                 return err;
842         }
843
844         /* This struct is already zerod for us (static global) */
845         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
846                 obd->obd_minor = i;
847
848         err = obd_init_caches();
849         if (err)
850                 return err;
851
852 #ifdef __KERNEL__
853         obd_sysctl_init();
854 #endif
855
856 #ifdef LPROCFS
857         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
858         if (!proc_lustre_root)
859                 printk(KERN_ERR "error registering /proc/fs/lustre\n");
860         proc_version = lprocfs_add_vars(proc_lustre_root,lprocfs_version,NULL);
861 #else
862         proc_lustre_root = NULL;
863         proc_version = -1;
864 #endif
865         return 0;
866 }
867
868 #ifdef LPROCFS
869 int obd_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data) {
870         *eof = 1;
871         return snprintf(page, count, "%s\n", BUILD_VERSION);
872 }
873 #else
874 int obd_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data) { return 0; }
875 #endif
876
877 #ifdef __KERNEL__
878 static void __exit cleanup_obdclass(void)
879 #else
880 static void cleanup_obdclass(void)
881 #endif
882 {
883         int i;
884         ENTRY;
885
886         misc_deregister(&obd_psdev);
887         for (i = 0; i < MAX_OBD_DEVICES; i++) {
888                 struct obd_device *obd = &obd_dev[i];
889                 if (obd->obd_type && obd->obd_set_up &&
890                     OBT(obd) && OBP(obd, detach)) {
891                         /* XXX should this call generic detach otherwise? */
892                         OBP(obd, detach)(obd);
893                 }
894         }
895
896         obd_cleanup_caches();
897 #ifdef __KERNEL__
898         obd_sysctl_clean();
899 #endif
900         if (proc_lustre_root) {
901                 lprocfs_remove(proc_lustre_root);
902                 proc_lustre_root = NULL;
903         }
904
905         class_handle_cleanup();
906         class_exit_uuidlist();
907
908         CERROR("obd mem max: %d leaked: %d\n", obd_memmax,
909                atomic_read(&obd_memory));
910         EXIT;
911 }
912
913 /* Check that we're building against the appropriate version of the Lustre
914  * kernel patch */
915 #ifdef __KERNEL__
916 #include <linux/lustre_version.h>
917 #define LUSTRE_MIN_VERSION 18
918 #define LUSTRE_MAX_VERSION 19
919 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
920 # error Cannot continue: Your Lustre kernel patch is older than the sources
921 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
922 # error Cannot continue: Your Lustre sources are older than the kernel patch
923 #endif
924  #else
925 # warning "Lib Lustre - no versioning information"
926 #endif
927
928 #ifdef __KERNEL__
929 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
930 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
931 MODULE_LICENSE("GPL");
932
933 module_init(init_obdclass);
934 module_exit(cleanup_obdclass);
935 #endif