Whamcloud - gitweb
Updated top level obdclass file for LProcFS
[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  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * These are the only exported functions, they provide some generic
10  * infrastructure for managing object devices
11  *
12  * Object Devices Class Driver
13  */
14
15 #define EXPORT_SYMTAB
16 #include <linux/config.h> /* for CONFIG_PROC_FS */
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/sched.h>
22 #include <linux/lp.h>
23 #include <linux/slab.h>
24 #include <linux/ioport.h>
25 #include <linux/fcntl.h>
26 #include <linux/delay.h>
27 #include <linux/skbuff.h>
28 #include <linux/proc_fs.h>
29 #include <linux/fs.h>
30 #include <linux/poll.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <asm/io.h>
34 #include <asm/system.h>
35 #include <asm/poll.h>
36 #include <asm/uaccess.h>
37 #include <linux/miscdevice.h>
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <linux/obd_support.h>
42 #include <linux/obd_class.h>
43 #include <linux/lustre_debug.h>
44 #include <linux/smp_lock.h>
45 #include <linux/lprocfs.h>
46
47 struct semaphore obd_conf_sem;   /* serialize configuration commands */
48 struct obd_device obd_dev[MAX_OBD_DEVICES];
49 struct list_head obd_types;
50 unsigned long obd_memory;
51
52 /* The following are visible and mutable through /proc/sys/lustre/. */
53 unsigned long obd_fail_loc;
54 unsigned long obd_timeout = 100;
55 char obd_recovery_upcall[128] = "/usr/lib/lustre/ha_assist";
56
57 extern struct obd_type *class_nm_to_type(char *nm);
58
59 /*
60  * LProcFS specific data structures. These define the namespace for
61  * the various device classes. We will need to distribute these
62  * later, to individual modules (e.g. MDS, MDC etc)
63  */
64
65 #ifdef LPROCFS_EXISTS
66
67 /*
68  * Common OBD namespace for lprocFS (these are used very often)
69  */
70
71 char* obd_dir_namespace_1[]= {
72         "mgmt%",
73         "mgmt/setup",
74         "mgmt/cleanup",
75         "mgmt/connect",
76         "mgmt/disconnect",
77         0
78 };
79
80 lprocfs_vars_t obd_var_namespace_1[]= {
81         
82         {"num_ops", lprocfs_longlong_read, lprocfs_longlong_write},
83         {"min_time", lprocfs_longlong_read, lprocfs_longlong_write},
84         {"max_time", lprocfs_longlong_read, lprocfs_longlong_write},
85         {"sum_time", lprocfs_longlong_read, lprocfs_longlong_write},
86         {"0", 0, 0}
87 };
88
89
90 /* 
91  *  MDC Spcific namespace for lprocFS 
92  */
93
94 char* mdc_dir_namespace_1[]= {
95         "reint",
96         "getstatus",
97         "getattr",
98         "setattr",
99         "open",
100         "readpage",
101         "create",
102         "unlink",
103         "link",
104         "rename",
105         0
106         
107 };
108
109 /*
110  * Create the MDC groupings
111  */
112
113 lprocfs_group_t lprocfs_mdc_namespace[]= {
114
115         {obd_dir_namespace_1, obd_var_namespace_1, e_generic},
116         {mdc_dir_namespace_1, obd_var_namespace_1, e_generic},
117         {0, 0, 0}
118 };
119
120 /*
121  * MDS Device Groupings
122  */
123
124 char* mds_dir_namespace_1[]={
125         "getstatus",
126         "connect",
127         "disconnect_callback",
128         "getattr",
129         "readpage",
130         "open",
131         "close",
132         "create",
133         "unlink",
134         "link",
135         "rename",
136         "reint%",
137         "reint/summary",
138         "reint/setattr",
139         "reint/create",
140         "reint/unlink",
141         "reint/link",
142         "reint/rename",
143         "reint/recreate",
144         0
145 };
146
147 char* mds_dir_namespace_2[]={
148         "mstatfs",
149         0
150         
151 };
152
153
154 lprocfs_vars_t mds_var_namespace_2[]={
155         {"f_type", read_other, write_other},
156         {"f_bsize",read_other, write_other},
157         {"f_blocks",read_other, write_other},
158         {"f_bfree",read_other, write_other},
159         {"f_bavail",read_other, write_other},
160         {"uuid",read_string, write_string},
161         {"0", 0, 0}
162 };
163
164
165 lprocfs_group_t lprocfs_mds_namespace[]={
166          {obd_dir_namespace_1, obd_var_namespace_1, e_generic},
167          {mds_dir_namespace_1, obd_var_namespace_1, e_generic},
168          {mds_dir_namespace_2, mds_var_namespace_2, e_specific}, 
169          {0, 0, 0}
170 };
171
172 /* 
173  * OSC Namespace
174  */
175
176 char* osc_dir_namespace_1[]={
177         "create",
178         "destroy",
179         "getattr",
180         "setattr",
181         "open",
182         "close",
183         "brw",
184         "punch",
185         "summary",
186         "cancel",
187         0
188 };
189
190 lprocfs_group_t lprocfs_osc_namespace[]={
191          {obd_dir_namespace_1, obd_var_namespace_1, e_generic},
192          {osc_dir_namespace_1, obd_var_namespace_1, e_generic}, 
193          {0, 0, 0}
194 };
195
196
197 /*
198  * OST, LOV, OBD_FILTER namespace
199  * Note: These namespaces are exactly similar to the osc_dir_namespace
200  * Hence, I use the osc namespace as the base class and add only
201  * those attributes that are missing in osc_dir_namespace.
202  */
203
204 char* ost_lov_obdfilter_dir_namespace_1[]={
205         "getinfo",
206         0
207
208 };
209 char* ost_lov_obdfilter_dir_namespace_2[]={
210         "ostatfs",
211         0
212 }; 
213
214 lprocfs_vars_t ost_lov_obdfilter_var_namespace_2[]={
215         {"f_type", read_other, write_other},
216         {"f_bsize",read_other, write_other},
217         {"f_blocks",read_other, write_other},
218         {"f_bfree",read_other, write_other},
219         {"f_bavail",read_other, write_other},
220         {"f_objects", read_other, write_other},
221         {"f_ofree", read_other, write_other},
222         {"f_objectgroups", read_other, write_other},
223         {"f_uuid", read_string, write_string},
224         {"0", 0, 0}
225 };
226
227
228 lprocfs_group_t lprocfs_ost_lov_obdfilter_namespace[]={
229          {obd_dir_namespace_1, obd_var_namespace_1, e_generic},
230          {osc_dir_namespace_1, obd_var_namespace_1, e_generic},
231          {ost_lov_obdfilter_dir_namespace_1, obd_var_namespace_1, e_generic},
232          {ost_lov_obdfilter_dir_namespace_2, ost_lov_obdfilter_var_namespace_2, e_specific},
233          {0, 0, 0}
234 };
235
236 /*
237  * LDLM Device namespace
238  */
239
240
241 char* ldlm_dir_namespace_1[]={
242         "locks%",
243         "locks/enqueus",
244         "locks/cancels",
245         "locks/converts",
246         "locks/matches",
247         0
248 };
249  
250
251 lprocfs_vars_t ldlm_var_namespace_1[]= {
252         
253         {"num_total", lprocfs_longlong_read, lprocfs_longlong_write},
254         {"num_zerolatency", lprocfs_longlong_read, lprocfs_longlong_write},
255         {"num_zerolatency_inflight", lprocfs_longlong_read, lprocfs_longlong_write},
256         {"num_zerolatency_done", lprocfs_longlong_read, lprocfs_longlong_write},
257         {"nonzero_mintime", lprocfs_longlong_read, lprocfs_longlong_write},
258         {"nonzero_maxtime", lprocfs_longlong_read, lprocfs_longlong_write},
259         {"nonzero_sumtime", lprocfs_longlong_read, lprocfs_longlong_write},
260         {"0", 0, 0}
261
262 };
263
264 lprocfs_group_t lprocfs_ldlm_namespace[]={
265          {obd_dir_namespace_1, obd_var_namespace_1, e_generic},
266          {ldlm_dir_namespace_1, ldlm_var_namespace_1, e_generic}, 
267          {0, 0, 0}
268 };
269
270 /*
271  * Note: Need to add namespace for breaking out locks by device class
272  */
273
274 /*
275  * PTLRPC Namespace
276  */
277 char* ptlrpc_dir_namespace_1[]={
278         "counters",
279         0
280 };
281
282 lprocfs_vars_t ptlrpc_var_namespace_1[]={
283         {"msgs_alloc", lprocfs_longlong_read, lprocfs_longlong_write},
284         {"msgs_max", lprocfs_longlong_read, lprocfs_longlong_write},
285         {"recv_count", lprocfs_longlong_read, lprocfs_longlong_write},
286         {"recv_length", lprocfs_longlong_read, lprocfs_longlong_write},
287         {"send_count", lprocfs_longlong_read, lprocfs_longlong_write},
288         {"send_length", lprocfs_longlong_read, lprocfs_longlong_write},
289         {"portal_kmemory", lprocfs_longlong_read, lprocfs_longlong_write},
290         {"0", 0, 0}
291 };
292 char* ptlrpc_dir_namespace_2[] = {
293         "network",
294         0
295 };
296  
297 lprocfs_vars_t ptlrpc_var_namespace_2[] = {
298         {"type", read_string, write_string},
299         {"mtu", lprocfs_longlong_read, lprocfs_longlong_write},
300         {"rxpackets", lprocfs_longlong_read, lprocfs_longlong_write},
301         {"txpackets", lprocfs_longlong_read, lprocfs_longlong_write},
302         {"txbytes", lprocfs_longlong_read, lprocfs_longlong_write},
303         {"0", 0, 0}
304 };
305
306 lprocfs_group_t lprocfs_ptlrpc_namespace[]={
307          {obd_dir_namespace_1, obd_var_namespace_1, e_generic},
308          {ptlrpc_dir_namespace_1, ptlrpc_var_namespace_1, e_generic},
309          {ptlrpc_dir_namespace_2, ptlrpc_var_namespace_2, e_specific}, 
310          {0, 0, 0}
311 };
312
313
314 /*
315  * Building the entire device namespace. This will be used during attach and
316  * detach to associate the namespace with the class of the device
317  */
318
319 lprocfs_obd_namespace_t obd_namespace[]={
320         {"mdc", lprocfs_mdc_namespace, sizeof(struct lprocfs_profiler_gen)},
321         {"mds", lprocfs_mds_namespace, sizeof(struct lprocfs_profiler_gen)},
322         {"osc", lprocfs_osc_namespace, sizeof(struct lprocfs_profiler_gen)},
323         {"ost", lprocfs_ost_lov_obdfilter_namespace, sizeof(struct lprocfs_profiler_gen)},
324         {"lov", lprocfs_ost_lov_obdfilter_namespace, sizeof(struct lprocfs_profiler_gen)},
325         {"obdfilter", lprocfs_ost_lov_obdfilter_namespace, sizeof(struct lprocfs_profiler_gen)},
326         {"ldlm", lprocfs_ldlm_namespace, sizeof(struct lprocfs_profiler_ldlm)},
327         {"ptlrpc", lprocfs_ptlrpc_namespace, sizeof(struct lprocfs_profiler_ptlrpc)},
328         {"0", 0, 0}
329         
330 };
331
332 #else
333
334 lprocfs_obd_namespace_t* obd_namespace=0;
335
336 #endif
337
338
339
340 /*  opening /dev/obd */
341 static int obd_class_open(struct inode * inode, struct file * file)
342 {
343         ENTRY;
344
345         file->private_data = NULL;
346         MOD_INC_USE_COUNT;
347         RETURN(0);
348 }
349
350 /*  closing /dev/obd */
351 static int obd_class_release(struct inode * inode, struct file * file)
352 {
353         ENTRY;
354
355         if (file->private_data)
356                 file->private_data = NULL;
357
358         MOD_DEC_USE_COUNT;
359         RETURN(0);
360 }
361
362
363 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
364 {
365         conn->addr = data->ioc_addr;
366         conn->cookie = data->ioc_cookie;
367 }
368
369
370 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
371 {
372         data->ioc_addr = conn->addr;
373         data->ioc_cookie = conn->cookie;
374 }
375
376
377 /* to control /dev/obd */
378 static int obd_class_ioctl (struct inode * inode, struct file * filp,
379                             unsigned int cmd, unsigned long arg)
380 {
381         char *buf = NULL;
382         int len = 0;
383         struct obd_ioctl_data *data;
384         struct obd_device *obd = filp->private_data;
385         
386         struct lustre_handle conn;
387         int rw = OBD_BRW_READ;
388         int err = 0;
389         int serialised = 0;
390         int obd_lprocfs_index = 0;
391         ENTRY;
392
393         switch (cmd)
394         {
395         case OBD_IOC_BRW_WRITE:
396         case OBD_IOC_BRW_READ:
397         case OBD_IOC_GETATTR:
398                 break;
399         default:
400                 down(&obd_conf_sem);
401                 serialised = 1;
402                 break;
403         }
404         
405         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
406             cmd != OBD_IOC_LIST &&
407             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
408                 CERROR("OBD ioctl: No device\n");
409                 GOTO(out, err=-EINVAL);
410         }
411         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
412                 CERROR("OBD ioctl: data error\n");
413                 GOTO(out, err=-EINVAL);
414         }
415         data = (struct obd_ioctl_data *)buf;
416
417         switch (cmd) {
418         case TCGETS:
419                 GOTO(out, err=-EINVAL);
420         case OBD_IOC_DEVICE: {
421                 CDEBUG(D_IOCTL, "\n");
422                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
423                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
424                         GOTO(out, err=-EINVAL);
425                 }
426                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
427
428                 filp->private_data = &obd_dev[data->ioc_dev];
429                 GOTO(out, err=0);
430         }
431
432         case OBD_IOC_LIST: {
433                 int i;
434                 char *buf2 = data->ioc_bulk;
435                 int remains = data->ioc_inllen1;
436
437                 if (!data->ioc_inlbuf1) {
438                         CERROR("No buffer passed!\n");
439                         GOTO(out, err=-EINVAL);
440                 }
441
442
443                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
444                         int l;
445                         char *status;
446                         struct obd_device *obd = &obd_dev[i];
447                         if (!obd->obd_type)
448                                 continue;
449                         if (obd->obd_flags & OBD_SET_UP)
450                                 status = "UP";
451                         else if (obd->obd_flags & OBD_ATTACHED)
452                                 status = "AT";
453                         else
454                                 status = "-";
455                         l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
456                                      i, status, obd->obd_type->typ_name,
457                                      obd->obd_name, obd->obd_uuid, obd->obd_type->typ_refcnt);
458                         buf2 +=l;
459                         remains -=l;
460                         if (remains <= 0) {
461                                 CERROR("not enough space for device listing\n");
462                                 break;
463                         }
464                 }
465
466                 err = copy_to_user((int *)arg, data, len);
467                 GOTO(out, err);
468         }
469
470
471         case OBD_IOC_NAME2DEV: {
472                 /* Resolve a device name.  This does not change the
473                  * currently selected device.
474                  */
475                 int dev;
476
477                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
478                         CERROR("No name passed,!\n");
479                         GOTO(out, err=-EINVAL);
480                 }
481                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
482                         CERROR("Name not nul terminated!\n");
483                         GOTO(out, err=-EINVAL);
484                 }
485
486                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
487                 dev = class_name2dev(data->ioc_inlbuf1);
488                 data->ioc_dev = dev;
489                 if (dev == -1) {
490                         CDEBUG(D_IOCTL, "No device for name %s!\n",
491                                data->ioc_inlbuf1);
492                         GOTO(out, err=-EINVAL);
493                 }
494
495                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
496                        dev);
497                 err = copy_to_user((int *)arg, data, sizeof(*data));
498                 GOTO(out, err);
499         }
500
501         case OBD_IOC_UUID2DEV: {
502                 /* Resolve a device uuid.  This does not change the
503                  * currently selected device.
504                  */
505                 int dev;
506
507                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
508                         CERROR("No UUID passed!\n");
509                         GOTO(out, err=-EINVAL);
510                 }
511                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
512                         CERROR("Name not nul terminated!\n");
513                         GOTO(out, err=-EINVAL);
514                 }
515
516                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
517                 dev = class_uuid2dev(data->ioc_inlbuf1);
518                 data->ioc_dev = dev;
519                 if (dev == -1) {
520                         CDEBUG(D_IOCTL, "No device for name %s!\n",
521                                data->ioc_inlbuf1);
522                         GOTO(out, err=-EINVAL);
523                 }
524
525                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
526                        dev);
527                 err = copy_to_user((int *)arg, data, sizeof(*data));
528                 GOTO(out, err);
529         }
530
531         case OBD_IOC_NEWDEV: {
532                 int dev = -1;
533                 int i;
534
535                 filp->private_data = NULL;
536                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
537                         struct obd_device *obd = &obd_dev[i];
538                         if (!obd->obd_type) {
539                                 filp->private_data = obd;
540                                 dev = i;
541                                 break;
542                         }
543                 }
544
545
546                 data->ioc_dev = dev;
547                 if (dev == -1)
548                         GOTO(out, err=-EINVAL);
549
550                 err = copy_to_user((int *)arg, data, sizeof(*data));
551                 GOTO(out, err);
552         }
553
554         case OBD_IOC_ATTACH: {
555                 struct obd_type *type;
556                 int minor;
557
558                 /* have we attached a type to this device */
559                 if (obd->obd_flags & OBD_ATTACHED || obd->obd_type) {
560                         CERROR("OBD: Device %d already typed as %s.\n",
561                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
562                         GOTO(out, err=-EBUSY);
563                 }
564
565                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
566                         CERROR("No type passed!\n");
567                         GOTO(out, err=-EINVAL);
568                 }
569                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
570                         CERROR("Type not nul terminated!\n");
571                         GOTO(out, err=-EINVAL);
572                 }
573
574                 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
575                        MKSTR(data->ioc_inlbuf1),
576                        MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
577
578                 /* find the type */
579                 type = class_nm_to_type(data->ioc_inlbuf1);
580                 if (!type) {
581                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
582                         GOTO(out, err=-EINVAL);
583                 }
584
585                 minor = obd->obd_minor;
586                 memset(obd, 0, sizeof(*obd));
587                 obd->obd_minor = minor;
588                 obd->obd_type = type;
589                 INIT_LIST_HEAD(&obd->obd_exports);
590                 INIT_LIST_HEAD(&obd->obd_imports);
591                 spin_lock_init(&obd->obd_dev_lock);
592
593                 /* do the attach */
594                 if (OBP(obd, attach))
595                         err = OBP(obd,attach)(obd, sizeof(*data), data);
596                 if (err) {
597                         obd->obd_type = NULL;
598                 } else {
599                         obd->obd_flags |= OBD_ATTACHED;
600
601                         type->typ_refcnt++;
602                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
603                                obd->obd_minor, data->ioc_inlbuf1);
604                         if (data->ioc_inlbuf2) {
605                                 int len = strlen(data->ioc_inlbuf2) + 1;
606                                 OBD_ALLOC(obd->obd_name, len);
607                                 if (!obd->obd_name) {
608                                         CERROR("no memory\n");
609                                         LBUG();
610                                 }
611                                 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
612                                 /* obd->obd_proc_entry =
613                                         proc_lustre_register_obd_device(obd);
614                                 */
615                                  
616                         } else {
617                                 CERROR("WARNING: unnamed obd device\n");
618                                 obd->obd_proc_entry = NULL;
619                         }
620
621                         if (data->ioc_inlbuf3) {
622                                 int len = strlen(data->ioc_inlbuf3);
623                                 if (len >= sizeof(obd->obd_uuid)) {
624                                         CERROR("uuid must be < %d bytes long\n",
625                                                sizeof(obd->obd_uuid));
626                                         if (obd->obd_name)
627                                                 OBD_FREE(obd->obd_name,
628                                                          strlen(obd->obd_name) + 1);
629                                         GOTO(out, err=-EINVAL);
630                                 }
631                                 memcpy(obd->obd_uuid, data->ioc_inlbuf3, len);
632                                 
633                         }
634                          /*
635                           * Get the LprocFS namespace for this device class
636                           */
637                         
638                         obd_lprocfs_index=lprocfs_get_namespace(data->ioc_inlbuf1, \
639                                                                 obd_namespace);
640                         
641                         if(obd_lprocfs_index<0) {
642                                 CERROR("Non-existent device class or proc/lustre not compiled \n");
643                                 
644                         } else {
645                                 lprocfs_register_dev(obd,                                           \
646                                                      obd_namespace[obd_lprocfs_index].obd_namespace, \
647                                                      obd_namespace[obd_lprocfs_index].cntr_blk_size);     
648                         }
649                         MOD_INC_USE_COUNT;
650                         
651                 }
652
653                 GOTO(out, err);
654         }
655
656         case OBD_IOC_DETACH: {
657                 ENTRY;
658                 if (obd->obd_flags & OBD_SET_UP) {
659                         CERROR("OBD device %d still set up\n", obd->obd_minor);
660                         GOTO(out, err=-EBUSY);
661                 }
662                 if (!(obd->obd_flags & OBD_ATTACHED) ) {
663                         CERROR("OBD device %d not attached\n", obd->obd_minor);
664                         GOTO(out, err=-ENODEV);
665                 }
666 #warning FIXME: Mike, we probably need some sort of "force detach" here
667                 if (!list_empty(&obd->obd_exports) ) {
668                         CERROR("OBD device %d (%p) has exports\n",
669                                obd->obd_minor, obd);
670                         GOTO(out, err=-EBUSY);
671                 }
672
673                 if(lprocfs_deregister_dev(obd)!=LPROCFS_SUCCESS){
674                         CERROR("Could not remove Proc Entry\n");
675                         GOTO(out, err=-ENODEV);
676                         
677                 }
678                 
679                 if (obd->obd_name) {
680                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
681                         obd->obd_name = NULL;
682                 }
683                 /*
684                 if (obd->obd_proc_entry)
685                         proc_lustre_release_obd_device(obd);
686                 */        
687                 obd->obd_flags &= ~OBD_ATTACHED;
688                 
689                 obd->obd_type->typ_refcnt--;
690                 obd->obd_type = NULL;
691                 MOD_DEC_USE_COUNT;
692                 GOTO(out, err=0);
693         }
694
695         case OBD_IOC_SETUP: {
696                 /* have we attached a type to this device? */
697                 if (!(obd->obd_flags & OBD_ATTACHED)) {
698                         CERROR("Device %d not attached\n", obd->obd_minor);
699                         GOTO(out, err=-ENODEV);
700                 }
701
702                 /* has this been done already? */
703                 if ( obd->obd_flags & OBD_SET_UP ) {
704                         CERROR("Device %d already setup (type %s)\n",
705                                obd->obd_minor, obd->obd_type->typ_name);
706                         GOTO(out, err=-EBUSY);
707                 }
708
709                 if ( OBT(obd) && OBP(obd, setup) )
710                         err = obd_setup(obd, sizeof(*data), data);
711
712                 if (!err) {
713                         obd->obd_type->typ_refcnt++;
714                         obd->obd_flags |= OBD_SET_UP;
715                 }
716
717                 GOTO(out, err);
718         }
719         case OBD_IOC_CLEANUP: {
720                 /* have we attached a type to this device? */
721                 if (!(obd->obd_flags & OBD_ATTACHED)) {
722                         CERROR("Device %d not attached\n", obd->obd_minor);
723                         GOTO(out, err=-ENODEV);
724                 }
725
726                 if ( OBT(obd) && OBP(obd, cleanup) )
727                         err = obd_cleanup(obd);
728
729                 if (!err) {
730                         obd->obd_flags &= ~OBD_SET_UP;
731                         obd->obd_type->typ_refcnt--;
732                 }
733                 GOTO(out, err);
734         }
735
736         case OBD_IOC_CONNECT: {
737                 char * cluuid = "OBD_CLASS_UUID";
738                 obd_data2conn(&conn, data);
739
740                 err = obd_connect(&conn, obd, cluuid);
741
742                 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
743                 obd_conn2data(data, &conn);
744                 if (err)
745                         GOTO(out, err);
746
747                 err = copy_to_user((int *)arg, data, sizeof(*data));
748                 GOTO(out, err);
749         }
750
751         case OBD_IOC_DISCONNECT: {
752                 obd_data2conn(&conn, data);
753                 err = obd_disconnect(&conn);
754                 GOTO(out, err);
755         }
756
757         case OBD_IOC_DEC_USE_COUNT: {
758                 MOD_DEC_USE_COUNT;
759                 GOTO(out, err=0);
760         }
761
762         case OBD_IOC_CREATE: {
763                 struct lov_stripe_md *ea;
764                 obd_data2conn(&conn, data);
765
766
767                 err = obd_create(&conn, &data->ioc_obdo1, &ea);
768                 if (err)
769                         GOTO(out, err);
770
771                 err = copy_to_user((int *)arg, data, sizeof(*data));
772                 GOTO(out, err);
773         }
774
775         case OBD_IOC_GETATTR: {
776
777                 obd_data2conn(&conn, data);
778                 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
779                 if (err)
780                         GOTO(out, err);
781
782                 err = copy_to_user((int *)arg, data, sizeof(*data));
783                 GOTO(out, err);
784         }
785
786         case OBD_IOC_SETATTR: {
787                 obd_data2conn(&conn, data);
788                 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
789                 if (err)
790                         GOTO(out, err);
791
792                 err = copy_to_user((int *)arg, data, sizeof(*data));
793                 GOTO(out, err);
794         }
795
796         case OBD_IOC_DESTROY: {
797                 //void *ea;
798                 obd_data2conn(&conn, data);
799
800                 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
801                 if (err)
802                         GOTO(out, err);
803
804                 err = copy_to_user((int *)arg, data, sizeof(*data));
805                 GOTO(out, err);
806         }
807
808         case OBD_IOC_BRW_WRITE:
809                 rw = OBD_BRW_WRITE;
810         case OBD_IOC_BRW_READ: {
811                 struct lov_stripe_md smd;
812                 struct io_cb_data *cbd = ll_init_cb();
813                 obd_count       pages = 0;
814                 struct brw_page *pga, *pgp;
815                 __u64 id = data->ioc_obdo1.o_id;
816                 int gfp_mask = (id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
817                 int verify = (id != 0);
818                 unsigned long off;
819                 int j;
820
821                 if (!cbd)
822                         GOTO(out, err = -ENOMEM);
823
824                 obd_data2conn(&conn, data);
825
826                 pages = data->ioc_count / PAGE_SIZE;
827
828                 CDEBUG(D_INODE, "BRW %s with %d pages\n",
829                        rw == OBD_BRW_READ ? "read" : "write", pages);
830                 OBD_ALLOC(pga, pages * sizeof(*pga));
831                 if (!pga) {
832                         CERROR("no memory for %d BRW per-page data\n", pages);
833                         GOTO(brw_free, err = -ENOMEM);
834                 }
835
836                 memset(&smd, 0, sizeof(smd));
837                 smd.lmd_object_id = id;
838
839                 off = data->ioc_offset;
840
841                 for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
842                         pgp->pg = alloc_pages(gfp_mask, 0);
843                         if (!pgp->pg) {
844                                 CERROR("no memory for brw pages\n");
845                                 GOTO(brw_cleanup, err = -ENOMEM);
846                         }
847                         pgp->count = PAGE_SIZE;
848                         pgp->off = off;
849                         pgp->flag = 0;
850
851                         if (verify) {
852                                 void *addr = kmap(pgp->pg);
853
854                                 if (rw == OBD_BRW_WRITE)
855                                         page_debug_setup(addr, pgp->count,
856                                                          pgp->off, id);
857                                 else
858                                         page_debug_setup(addr, pgp->count,
859                                                          0xdeadbeef00c0ffee,
860                                                          0xdeadbeef00c0ffee);
861                                 kunmap(pgp->pg);
862                         }
863                 }
864
865                 err = obd_brw(rw, &conn, &smd, j, pga, ll_sync_io_cb, cbd);
866                 if (err)
867                         CERROR("test_brw: error from obd_brw: err = %d\n", err);
868                 EXIT;
869         brw_cleanup:
870                 for (j = 0, pgp = pga; j < pages; j++, pgp++) {
871                         if (pgp->pg != NULL) {
872                                 if (verify && !err) {
873                                         void *addr = kmap(pgp->pg);
874
875                                         err = page_debug_check("test_brw",
876                                                                addr,
877                                                                PAGE_SIZE,
878                                                                pgp->off,id);
879                                         kunmap(pgp->pg);
880                                 }
881                                 __free_pages(pgp->pg, 0);
882                         }
883                 }
884         brw_free:
885                 OBD_FREE(pga, pages * sizeof(*pga));
886                 GOTO(out, err);
887         }
888         default:
889                 obd_data2conn(&conn, data);
890         
891
892                 err = obd_iocontrol(cmd, &conn, len, data, NULL);
893                 if (err)
894                         GOTO(out, err);
895
896                 err = copy_to_user((int *)arg, data, len);
897                 GOTO(out, err);
898         }
899
900  out:
901         if (buf)
902                 OBD_FREE(buf, len);
903         if (serialised)
904                 up(&obd_conf_sem);
905         RETURN(err);
906 } /* obd_class_ioctl */
907
908
909
910 /* declare character device */
911 static struct file_operations obd_psdev_fops = {
912         ioctl: obd_class_ioctl,       /* ioctl */
913         open: obd_class_open,        /* open */
914         release: obd_class_release,     /* release */
915 };
916
917 /* modules setup */
918 #define OBD_MINOR 241
919 static struct miscdevice obd_psdev = {
920         OBD_MINOR,
921         "obd_psdev",
922         &obd_psdev_fops
923 };
924
925 void (*class_signal_connection_failure)(struct ptlrpc_connection *);
926 int (*mds_destroy_export)(struct obd_export *exp);
927 int (*ldlm_destroy_export)(struct obd_export *exp);
928
929 EXPORT_SYMBOL(obd_dev);
930 EXPORT_SYMBOL(obdo_cachep);
931 EXPORT_SYMBOL(obd_memory);
932 EXPORT_SYMBOL(obd_fail_loc);
933 EXPORT_SYMBOL(obd_timeout);
934 EXPORT_SYMBOL(obd_recovery_upcall);
935
936 EXPORT_SYMBOL(class_register_type);
937 EXPORT_SYMBOL(class_unregister_type);
938 EXPORT_SYMBOL(class_name2dev);
939 EXPORT_SYMBOL(class_uuid2dev);
940 EXPORT_SYMBOL(class_uuid2obd);
941 EXPORT_SYMBOL(class_new_export);
942 EXPORT_SYMBOL(class_destroy_export);
943 EXPORT_SYMBOL(class_connect);
944 EXPORT_SYMBOL(class_conn2export);
945 EXPORT_SYMBOL(class_conn2obd);
946 EXPORT_SYMBOL(class_conn2cliimp);
947 EXPORT_SYMBOL(class_conn2ldlmimp);
948 EXPORT_SYMBOL(class_disconnect);
949 EXPORT_SYMBOL(class_disconnect_all);
950 //EXPORT_SYMBOL(class_uuid_parse);
951 EXPORT_SYMBOL(class_uuid_unparse);
952 //EXPORT_SYMBOL(class_multi_setup);
953 //EXPORT_SYMBOL(class_multi_cleanup);
954
955 EXPORT_SYMBOL(class_signal_connection_failure);
956 EXPORT_SYMBOL(mds_destroy_export);
957 EXPORT_SYMBOL(ldlm_destroy_export);
958 EXPORT_SYMBOL(ll_sync_io_cb);
959 EXPORT_SYMBOL(ll_init_cb);
960
961 static int __init init_obdclass(void)
962 {
963         struct obd_device *obd;
964         int err;
965         int i;
966
967         printk(KERN_INFO "OBD class driver  v0.9, info@clusterfs.com\n");
968
969         sema_init(&obd_conf_sem, 1);
970         INIT_LIST_HEAD(&obd_types);
971
972         if ((err = misc_register(&obd_psdev))) {
973                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
974                 return err;
975         }
976
977         /* This struct is already zerod for us (static global) */
978         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
979                 obd->obd_minor = i;
980
981         err = obd_init_caches();
982         if (err)
983                 return err;
984         obd_sysctl_init();
985         return 0;
986 }
987
988 static void __exit cleanup_obdclass(void)
989 {
990         int i;
991         ENTRY;
992
993         misc_deregister(&obd_psdev);
994         for (i = 0; i < MAX_OBD_DEVICES; i++) {
995                 struct obd_device *obd = &obd_dev[i];
996                 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
997                     OBT(obd) && OBP(obd, detach)) {
998                         /* XXX should this call generic detach otherwise? */
999                         OBP(obd, detach)(obd);
1000                 }
1001         }
1002
1003         obd_cleanup_caches();
1004         obd_sysctl_clean();
1005         CERROR("obd memory leaked: %ld bytes\n", obd_memory);
1006         EXIT;
1007 }
1008
1009 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1010 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
1011 MODULE_LICENSE("GPL");
1012
1013 module_init(init_obdclass);
1014 module_exit(cleanup_obdclass);