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