Whamcloud - gitweb
merge b_devel into HEAD (20030626 merge tag) for 0.7.1
[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                 INIT_LIST_HEAD(&obd->obd_imports);
469                 spin_lock_init(&obd->obd_dev_lock);
470                 init_waitqueue_head(&obd->obd_refcount_waitq);
471
472                 /* XXX belong ins setup not attach  */
473                 /* recovery data */
474                 spin_lock_init(&obd->obd_processing_task_lock);
475                 init_waitqueue_head(&obd->obd_next_transno_waitq);
476                 INIT_LIST_HEAD(&obd->obd_recovery_queue);
477                 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
478
479                 init_waitqueue_head(&obd->obd_commit_waitq);
480
481                 len = strlen(data->ioc_inlbuf2) + 1;
482                 OBD_ALLOC(obd->obd_name, len);
483                 if (!obd->obd_name) {
484                         class_put_type(obd->obd_type);
485                         obd->obd_type = NULL;
486                         GOTO(out, err = -ENOMEM);
487                 }
488                 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
489
490                 len = strlen(data->ioc_inlbuf3);
491                 if (len >= sizeof(obd->obd_uuid)) {
492                         CERROR("uuid must be < "LPSZ" bytes long\n",
493                                sizeof(obd->obd_uuid));
494                         if (obd->obd_name)
495                                 OBD_FREE(obd->obd_name,
496                                          strlen(obd->obd_name) + 1);
497                         class_put_type(obd->obd_type);
498                         obd->obd_type = NULL;
499                         GOTO(out, err = -EINVAL);
500                 }
501                 memcpy(obd->obd_uuid.uuid, data->ioc_inlbuf3, len);
502
503                 /* do the attach */
504                 if (OBP(obd, attach))
505                         err = OBP(obd,attach)(obd, sizeof(*data), data);
506                 if (err) {
507                         if(data->ioc_inlbuf2)
508                                 OBD_FREE(obd->obd_name,
509                                          strlen(obd->obd_name) + 1);
510                         class_put_type(obd->obd_type);
511                         obd->obd_type = NULL;
512                 } else {
513                         obd->obd_attached = 1;
514
515                         type->typ_refcnt++;
516                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
517                                obd->obd_minor, data->ioc_inlbuf1);
518                 }
519
520                 GOTO(out, err);
521         }
522
523         case OBD_IOC_DETACH: {
524                 ENTRY;
525                 if (obd->obd_set_up) {
526                         CERROR("OBD device %d still set up\n", obd->obd_minor);
527                         GOTO(out, err = -EBUSY);
528                 }
529                 if (!obd->obd_attached) {
530                         CERROR("OBD device %d not attached\n", obd->obd_minor);
531                         GOTO(out, err = -ENODEV);
532                 }
533                 if (OBP(obd, detach))
534                         err = OBP(obd,detach)(obd);
535
536                 if (obd->obd_name) {
537                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
538                         obd->obd_name = NULL;
539                 }
540
541                 obd->obd_attached = 0;
542                 obd->obd_type->typ_refcnt--;
543                 class_put_type(obd->obd_type);
544                 obd->obd_type = NULL;
545                 GOTO(out, err = 0);
546         }
547
548         case OBD_IOC_SETUP: {
549                 /* have we attached a type to this device? */
550                 if (!obd->obd_attached) {
551                         CERROR("Device %d not attached\n", obd->obd_minor);
552                         GOTO(out, err = -ENODEV);
553                 }
554
555                 /* has this been done already? */
556                 if (obd->obd_set_up) {
557                         CERROR("Device %d already setup (type %s)\n",
558                                obd->obd_minor, obd->obd_type->typ_name);
559                         GOTO(out, err = -EBUSY);
560                 }
561
562                 atomic_set(&obd->obd_refcount, 0);
563
564                 if ( OBT(obd) && OBP(obd, setup) )
565                         err = obd_setup(obd, sizeof(*data), data);
566
567                 if (!err) {
568                         obd->obd_type->typ_refcnt++;
569                         obd->obd_set_up = 1;
570                         atomic_inc(&obd->obd_refcount);
571                 }
572
573                 GOTO(out, err);
574         }
575         case OBD_IOC_CLEANUP: {
576                 int force = 0, failover = 0;
577                 char * flag;
578
579                 if (!obd->obd_set_up) {
580                         CERROR("Device %d not setup\n", obd->obd_minor);
581                         GOTO(out, err = -ENODEV);
582                 }
583
584                 if (data->ioc_inlbuf1) {
585                         for (flag = data->ioc_inlbuf1; *flag != 0; flag++)
586                                 switch (*flag) {
587                                 case 'F':
588                                         force = 1;
589                                         break;
590                                 case 'A':
591                                         failover = 1;
592                                         break;
593                                 default:
594                                         CERROR("unrecognised flag '%c'\n", 
595                                                *flag);
596                                 }
597                 }
598                 
599                 if (atomic_read(&obd->obd_refcount) == 1 || force) {
600                         /* this will stop new connections, and need to
601                            do it before class_disconnect_exports() */
602                         obd->obd_stopping = 1;
603                 }
604
605                 if (atomic_read(&obd->obd_refcount) > 1) {
606                         struct l_wait_info lwi = LWI_TIMEOUT_INTR(60 * HZ, NULL,
607                                                                   NULL, NULL);
608                         int rc;
609                         
610                         if (!force) {
611                                 CERROR("OBD device %d (%p) has refcount %d\n",
612                                        obd->obd_minor, obd, 
613                                        atomic_read(&obd->obd_refcount));
614                                 dump_exports(obd);
615                                 GOTO(out, err = -EBUSY);
616                         }
617                         class_disconnect_exports(obd, failover);
618                         CDEBUG(D_IOCTL, 
619                                "%s: waiting for obd refs to go away: %d\n", 
620                                obd->obd_name, atomic_read(&obd->obd_refcount));
621                 
622                         rc = l_wait_event(obd->obd_refcount_waitq,
623                                      atomic_read(&obd->obd_refcount) < 2, &lwi);
624                         if (rc == 0) {
625                                 LASSERT(atomic_read(&obd->obd_refcount) == 1);
626                         } else {
627                                 CERROR("wait cancelled cleaning anyway. "
628                                        "refcount: %d\n",
629                                        atomic_read(&obd->obd_refcount));
630                                 dump_exports(obd);
631                         }
632                         CDEBUG(D_IOCTL, "%s: awake, now finishing cleanup\n", 
633                                obd->obd_name);
634                 }
635
636                 if (OBT(obd) && OBP(obd, cleanup))
637                         err = obd_cleanup(obd, force, failover);
638
639                 if (!err) {
640                         obd->obd_set_up = obd->obd_stopping = 0;
641                         obd->obd_type->typ_refcnt--;
642                         atomic_dec(&obd->obd_refcount);
643                         /* XXX this should be an LASSERT */
644                         if (atomic_read(&obd->obd_refcount) > 0) 
645                                 CERROR("%s still has refcount %d after "
646                                        "cleanup.\n", obd->obd_name,
647                                        atomic_read(&obd->obd_refcount));
648                 }
649
650                 GOTO(out, err);
651         }
652
653         case OBD_IOC_CONNECT: {
654                 struct obd_uuid cluuid = { "OBD_CLASS_UUID" };
655                 obd_data2conn(&conn, data);
656
657                 err = obd_connect(&conn, obd, &cluuid);
658
659                 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.cookie);
660                 obd_conn2data(data, &conn);
661                 if (err)
662                         GOTO(out, err);
663
664                 err = obd_class_add_user_conn (ocus, &conn);
665                 if (err != 0) {
666                         obd_disconnect (&conn, 0);
667                         GOTO (out, err);
668                 }
669
670                 err = copy_to_user((void *)arg, data, sizeof(*data));
671                 if (err != 0) {
672                         obd_class_remove_user_conn (ocus, &conn);
673                         obd_disconnect (&conn, 0);
674                         GOTO (out, err = -EFAULT);
675                 }
676                 GOTO(out, err);
677         }
678
679         case OBD_IOC_DISCONNECT: {
680                 obd_data2conn(&conn, data);
681                 obd_class_remove_user_conn (ocus, &conn);
682                 err = obd_disconnect(&conn, 0);
683                 GOTO(out, err);
684         }
685
686         case OBD_IOC_NO_TRANSNO: {
687                 if (!obd->obd_attached) {
688                         CERROR("Device %d not attached\n", obd->obd_minor);
689                         GOTO(out, err = -ENODEV);
690                 }
691                 CDEBUG(D_IOCTL,
692                        "disabling committed-transno notifications on %d\n",
693                        obd->obd_minor);
694                 obd->obd_no_transno = 1;
695                 GOTO(out, err = 0);
696         }
697
698         case OBD_IOC_CLOSE_UUID: {
699                 struct lustre_peer peer;
700                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
701                        data->ioc_inlbuf1);
702                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer);
703                 GOTO(out, err = 0);
704         }
705         case OBD_IOC_ADD_UUID: {
706                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
707                        ", nal %d\n", data->ioc_inlbuf1, data->ioc_nid,
708                        data->ioc_nal);
709
710                 err = class_add_uuid(data->ioc_inlbuf1, data->ioc_nid,
711                                      data->ioc_nal);
712                 GOTO(out, err);
713         }
714         case OBD_IOC_DEL_UUID: {
715                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
716                        data->ioc_inlbuf1 == NULL ? "<all uuids>" :
717                        data->ioc_inlbuf1);
718
719                 err = class_del_uuid(data->ioc_inlbuf1);
720                 GOTO(out, err);
721         }
722         default: { 
723                 // obd_data2conn(&conn, data);
724                 struct obd_class_user_conn *oconn = list_entry(ocus->ocus_conns.next, struct obd_class_user_conn, ocuc_chain);
725                 err = obd_iocontrol(cmd, &oconn->ocuc_conn, len, data, NULL);
726                 if (err)
727                         GOTO(out, err);
728
729                 err = copy_to_user((void *)arg, data, len);
730                 if (err)
731                         err = -EFAULT;
732                 GOTO(out, err);
733         }
734         }
735
736  out:
737         if (buf)
738                 obd_ioctl_freedata(buf, len);
739         if (serialised)
740                 up(&obd_conf_sem);
741         RETURN(err);
742 } /* class_handle_ioctl */
743
744
745
746 #define OBD_MINOR 241
747 #ifdef __KERNEL__
748 /* to control /dev/obd */
749 static int obd_class_ioctl(struct inode *inode, struct file *filp,
750                            unsigned int cmd, unsigned long arg)
751 {
752         return class_handle_ioctl(filp->private_data, cmd, arg);
753 }
754
755 /* declare character device */
756 static struct file_operations obd_psdev_fops = {
757         ioctl:   obd_class_ioctl,       /* ioctl */
758         open:    obd_class_open,        /* open */
759         release: obd_class_release,     /* release */
760 };
761
762 /* modules setup */
763 static struct miscdevice obd_psdev = {
764         OBD_MINOR,
765         "obd_psdev",
766         &obd_psdev_fops
767 };
768 #else
769 void *obd_psdev = NULL;
770 #endif
771
772 EXPORT_SYMBOL(obd_dev);
773 EXPORT_SYMBOL(obdo_cachep);
774 EXPORT_SYMBOL(obd_memory);
775 EXPORT_SYMBOL(obd_memmax);
776 EXPORT_SYMBOL(obd_fail_loc);
777 EXPORT_SYMBOL(obd_timeout);
778 EXPORT_SYMBOL(obd_lustre_upcall);
779 EXPORT_SYMBOL(obd_sync_filter);
780 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
781 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
782 EXPORT_SYMBOL(proc_lustre_root);
783
784 EXPORT_SYMBOL(lctl_fake_uuid);
785
786 EXPORT_SYMBOL(class_register_type);
787 EXPORT_SYMBOL(class_unregister_type);
788 EXPORT_SYMBOL(class_get_type);
789 EXPORT_SYMBOL(class_put_type);
790 EXPORT_SYMBOL(class_name2dev);
791 EXPORT_SYMBOL(class_uuid2dev);
792 EXPORT_SYMBOL(class_uuid2obd);
793 EXPORT_SYMBOL(class_export_get);
794 EXPORT_SYMBOL(class_export_put);
795 EXPORT_SYMBOL(class_new_export);
796 EXPORT_SYMBOL(class_unlink_export);
797 EXPORT_SYMBOL(class_import_get);
798 EXPORT_SYMBOL(class_import_put);
799 EXPORT_SYMBOL(class_new_import);
800 EXPORT_SYMBOL(class_destroy_import);
801 EXPORT_SYMBOL(class_connect);
802 EXPORT_SYMBOL(class_conn2export);
803 EXPORT_SYMBOL(class_conn2obd);
804 EXPORT_SYMBOL(class_conn2cliimp);
805 EXPORT_SYMBOL(class_conn2ldlmimp);
806 EXPORT_SYMBOL(class_disconnect);
807 EXPORT_SYMBOL(class_disconnect_exports);
808 EXPORT_SYMBOL(lustre_uuid_to_peer);
809
810 /* uuid.c */
811 EXPORT_SYMBOL(class_uuid_unparse);
812 EXPORT_SYMBOL(client_tgtuuid2obd);
813
814 EXPORT_SYMBOL(class_handle_hash);
815 EXPORT_SYMBOL(class_handle_unhash);
816 EXPORT_SYMBOL(class_handle2object);
817
818 #ifdef __KERNEL__
819 static int __init init_obdclass(void)
820 #else
821 int init_obdclass(void)
822 #endif
823 {
824         struct obd_device *obd;
825         int err;
826         int i;
827
828         printk(KERN_INFO "OBD class driver Build Version: " BUILD_VERSION
829                       ", info@clusterfs.com\n");
830
831         class_init_uuidlist();
832         class_handle_init();
833
834         sema_init(&obd_conf_sem, 1);
835         INIT_LIST_HEAD(&obd_types);
836
837         if ((err = misc_register(&obd_psdev))) {
838                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
839                 return err;
840         }
841
842         /* This struct is already zerod for us (static global) */
843         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
844                 obd->obd_minor = i;
845
846         err = obd_init_caches();
847         if (err)
848                 return err;
849
850 #ifdef __KERNEL__
851         obd_sysctl_init();
852 #endif
853
854 #ifdef LPROCFS
855         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
856         if (!proc_lustre_root)
857                 printk(KERN_ERR "error registering /proc/fs/lustre\n");
858         proc_version = lprocfs_add_vars(proc_lustre_root,lprocfs_version,NULL);
859 #else
860         proc_lustre_root = NULL;
861         proc_version = -1;
862 #endif
863         return 0;
864 }
865
866 #ifdef LPROCFS
867 int obd_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data) {
868         *eof = 1;
869         return snprintf(page, count, "%s\n", BUILD_VERSION);
870 }
871 #else
872 int obd_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data) { return 0; }
873 #endif
874
875 #ifdef __KERNEL__
876 static void __exit cleanup_obdclass(void)
877 #else
878 static void cleanup_obdclass(void)
879 #endif
880 {
881         int i;
882         ENTRY;
883
884         misc_deregister(&obd_psdev);
885         for (i = 0; i < MAX_OBD_DEVICES; i++) {
886                 struct obd_device *obd = &obd_dev[i];
887                 if (obd->obd_type && obd->obd_set_up &&
888                     OBT(obd) && OBP(obd, detach)) {
889                         /* XXX should this call generic detach otherwise? */
890                         OBP(obd, detach)(obd);
891                 }
892         }
893
894         obd_cleanup_caches();
895 #ifdef __KERNEL__
896         obd_sysctl_clean();
897 #endif
898         if (proc_lustre_root) {
899                 lprocfs_remove(proc_lustre_root);
900                 proc_lustre_root = NULL;
901         }
902
903         class_handle_cleanup();
904         class_exit_uuidlist();
905
906         CERROR("obd mem max: %d leaked: %d\n", obd_memmax,
907                atomic_read(&obd_memory));
908         EXIT;
909 }
910
911 /* Check that we're building against the appropriate version of the Lustre
912  * kernel patch */
913 #ifdef __KERNEL__
914 #include <linux/lustre_version.h>
915 #define LUSTRE_MIN_VERSION 18
916 #define LUSTRE_MAX_VERSION 19
917 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
918 # error Cannot continue: Your Lustre kernel patch is older than the sources
919 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
920 # error Cannot continue: Your Lustre sources are older than the kernel patch
921 #endif
922  #else
923 # warning "Lib Lustre - no versioning information"
924 #endif
925
926 #ifdef __KERNEL__
927 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
928 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
929 MODULE_LICENSE("GPL");
930
931 module_init(init_obdclass);
932 module_exit(cleanup_obdclass);
933 #endif