Whamcloud - gitweb
Don't oops if data->ioc_inlbuf1 is not set.
[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 static void forcibly_detach_exports(struct obd_device *obd)
474 {
475         int rc;
476         struct list_head *tmp, *n;
477         struct lustre_handle fake_conn;
478
479         CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
480                "disconnecting them", obd->obd_minor, obd);
481         list_for_each_safe(tmp, n, &obd->obd_exports) {
482                 struct obd_export *exp = list_entry(tmp, struct obd_export,
483                                                     exp_obd_chain);
484                 fake_conn.addr = (__u64)(unsigned long)exp;
485                 fake_conn.cookie = exp->exp_cookie;
486                 rc = obd_disconnect(&fake_conn);
487                 if (rc) {
488                         CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
489                                exp, rc);
490                 } else {
491                         CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
492                 }
493         }
494 }
495
496 /* to control /dev/obd */
497 static int obd_class_ioctl (struct inode * inode, struct file * filp,
498                             unsigned int cmd, unsigned long arg)
499 {
500         char *buf = NULL;
501         int len = 0;
502         struct obd_ioctl_data *data;
503         struct obd_device *obd = filp->private_data;
504
505         struct lustre_handle conn;
506         int rw = OBD_BRW_READ;
507         int err = 0;
508         int serialised = 0;
509         int l_idx = 0;
510         ENTRY;
511
512         switch (cmd)
513         {
514         case OBD_IOC_BRW_WRITE:
515         case OBD_IOC_BRW_READ:
516         case OBD_IOC_GETATTR:
517                 break;
518         default:
519                 down(&obd_conf_sem);
520                 serialised = 1;
521                 break;
522         }
523
524         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
525             cmd != OBD_IOC_LIST &&
526             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
527                 CERROR("OBD ioctl: No device\n");
528                 GOTO(out, err=-EINVAL);
529         }
530         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
531                 CERROR("OBD ioctl: data error\n");
532                 GOTO(out, err=-EINVAL);
533         }
534         data = (struct obd_ioctl_data *)buf;
535
536         switch (cmd) {
537         case TCGETS:
538                 GOTO(out, err=-EINVAL);
539         case OBD_IOC_DEVICE: {
540                 CDEBUG(D_IOCTL, "\n");
541                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
542                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
543                         GOTO(out, err=-EINVAL);
544                 }
545                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
546
547                 filp->private_data = &obd_dev[data->ioc_dev];
548                 GOTO(out, err=0);
549         }
550
551         case OBD_IOC_LIST: {
552                 int i;
553                 char *buf2 = data->ioc_bulk;
554                 int remains = data->ioc_inllen1;
555
556                 if (!data->ioc_inlbuf1) {
557                         CERROR("No buffer passed!\n");
558                         GOTO(out, err=-EINVAL);
559                 }
560
561
562                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
563                         int l;
564                         char *status;
565                         struct obd_device *obd = &obd_dev[i];
566                         if (!obd->obd_type)
567                                 continue;
568                         if (obd->obd_flags & OBD_SET_UP)
569                                 status = "UP";
570                         else if (obd->obd_flags & OBD_ATTACHED)
571                                 status = "AT";
572                         else
573                                 status = "-";
574                         l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
575                                      i, status, obd->obd_type->typ_name,
576                                      obd->obd_name, obd->obd_uuid, obd->obd_type->typ_refcnt);
577                         buf2 +=l;
578                         remains -=l;
579                         if (remains <= 0) {
580                                 CERROR("not enough space for device listing\n");
581                                 break;
582                         }
583                 }
584
585                 err = copy_to_user((void *)arg, data, len);
586                 GOTO(out, err);
587         }
588
589
590         case OBD_IOC_NAME2DEV: {
591                 /* Resolve a device name.  This does not change the
592                  * currently selected device.
593                  */
594                 int dev;
595
596                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
597                         CERROR("No name passed,!\n");
598                         GOTO(out, err=-EINVAL);
599                 }
600                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
601                         CERROR("Name not nul terminated!\n");
602                         GOTO(out, err=-EINVAL);
603                 }
604
605                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
606                 dev = class_name2dev(data->ioc_inlbuf1);
607                 data->ioc_dev = dev;
608                 if (dev == -1) {
609                         CDEBUG(D_IOCTL, "No device for name %s!\n",
610                                data->ioc_inlbuf1);
611                         GOTO(out, err=-EINVAL);
612                 }
613
614                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
615                        dev);
616                 err = copy_to_user((void *)arg, data, sizeof(*data));
617                 GOTO(out, err);
618         }
619
620         case OBD_IOC_UUID2DEV: {
621                 /* Resolve a device uuid.  This does not change the
622                  * currently selected device.
623                  */
624                 int dev;
625
626                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
627                         CERROR("No UUID passed!\n");
628                         GOTO(out, err=-EINVAL);
629                 }
630                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
631                         CERROR("Name not nul terminated!\n");
632                         GOTO(out, err=-EINVAL);
633                 }
634
635                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
636                 dev = class_uuid2dev(data->ioc_inlbuf1);
637                 data->ioc_dev = dev;
638                 if (dev == -1) {
639                         CDEBUG(D_IOCTL, "No device for name %s!\n",
640                                data->ioc_inlbuf1);
641                         GOTO(out, err=-EINVAL);
642                 }
643
644                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
645                        dev);
646                 err = copy_to_user((void *)arg, data, sizeof(*data));
647                 GOTO(out, err);
648         }
649
650         case OBD_IOC_NEWDEV: {
651                 int dev = -1;
652                 int i;
653
654                 filp->private_data = NULL;
655                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
656                         struct obd_device *obd = &obd_dev[i];
657                         if (!obd->obd_type) {
658                                 filp->private_data = obd;
659                                 dev = i;
660                                 break;
661                         }
662                 }
663
664
665                 data->ioc_dev = dev;
666                 if (dev == -1)
667                         GOTO(out, err=-EINVAL);
668
669                 err = copy_to_user((void *)arg, data, sizeof(*data));
670                 GOTO(out, err);
671         }
672
673         case OBD_IOC_ATTACH: {
674                 struct obd_type *type;
675                 int minor;
676
677                 /* have we attached a type to this device */
678                 if (obd->obd_flags & OBD_ATTACHED || obd->obd_type) {
679                         CERROR("OBD: Device %d already typed as %s.\n",
680                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
681                         GOTO(out, err=-EBUSY);
682                 }
683
684                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
685                         CERROR("No type passed!\n");
686                         GOTO(out, err=-EINVAL);
687                 }
688                 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
689                         CERROR("Type not nul terminated!\n");
690                         GOTO(out, err=-EINVAL);
691                 }
692
693                 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
694                        MKSTR(data->ioc_inlbuf1),
695                        MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
696
697                 /* find the type */
698                 type = class_nm_to_type(data->ioc_inlbuf1);
699                 if (!type) {
700                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
701                         GOTO(out, err=-EINVAL);
702                 }
703
704                 minor = obd->obd_minor;
705                 memset(obd, 0, sizeof(*obd));
706                 obd->obd_minor = minor;
707                 obd->obd_type = type;
708                 INIT_LIST_HEAD(&obd->obd_exports);
709                 INIT_LIST_HEAD(&obd->obd_imports);
710                 spin_lock_init(&obd->obd_dev_lock);
711
712                 /* do the attach */
713                 if (OBP(obd, attach))
714                         err = OBP(obd,attach)(obd, sizeof(*data), data);
715                 if (err) {
716                         obd->obd_type = NULL;
717                 } else {
718                         obd->obd_flags |= OBD_ATTACHED;
719
720                         type->typ_refcnt++;
721                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
722                                obd->obd_minor, data->ioc_inlbuf1);
723                         if (data->ioc_inlbuf2) {
724                                 int len = strlen(data->ioc_inlbuf2) + 1;
725                                 OBD_ALLOC(obd->obd_name, len);
726                                 if (!obd->obd_name) {
727                                         CERROR("no memory\n");
728                                         LBUG();
729                                 }
730                                 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
731                                 /* obd->obd_proc_entry =
732                                         proc_lustre_register_obd_device(obd);
733                                 */
734                         } else {
735                                 CERROR("WARNING: unnamed obd device\n");
736                                 obd->obd_proc_entry = NULL;
737                         }
738
739                         if (data->ioc_inlbuf3) {
740                                 int len = strlen(data->ioc_inlbuf3);
741                                 if (len >= sizeof(obd->obd_uuid)) {
742                                         CERROR("uuid must be < %d bytes long\n",
743                                                sizeof(obd->obd_uuid));
744                                         if (obd->obd_name)
745                                                 OBD_FREE(obd->obd_name,
746                                                          strlen(obd->obd_name) + 1);
747                                         GOTO(out, err=-EINVAL);
748                                 }
749                                 memcpy(obd->obd_uuid, data->ioc_inlbuf3, len);
750                         }
751
752                         /* Get the LprocFS namespace for this device class */
753                         l_idx = lprocfs_get_nm(data->ioc_inlbuf1, obd_nm);
754                         if (l_idx < 0) {
755                                 CERROR("Non-existent device class"
756                                        "or proc/lustre not compiled \n");
757                         } else {
758                                 lprocfs_reg_dev(obd, obd_nm[l_idx].obd_names,
759                                                 obd_nm[l_idx].cntr_blk_sz);
760                         }
761
762                         CDEBUG(D_IOCTL, "MOD_INC_USE for attach: count = %d\n",
763                                atomic_read(&(THIS_MODULE)->uc.usecount));
764                         MOD_INC_USE_COUNT;
765                 }
766
767                 GOTO(out, err);
768         }
769
770         case OBD_IOC_DETACH: {
771                 ENTRY;
772                 if (obd->obd_flags & OBD_SET_UP) {
773                         CERROR("OBD device %d still set up\n", obd->obd_minor);
774                         GOTO(out, err=-EBUSY);
775                 }
776                 if (!(obd->obd_flags & OBD_ATTACHED) ) {
777                         CERROR("OBD device %d not attached\n", obd->obd_minor);
778                         GOTO(out, err=-ENODEV);
779                 }
780                 if (!list_empty(&obd->obd_exports)) {
781                         if (!data->ioc_inlbuf1 || data->ioc_inlbuf1[0] != 'F') {
782                                 CERROR("OBD device %d (%p) has exports\n",
783                                        obd->obd_minor, obd);
784                                 GOTO(out, err=-EBUSY);
785                         }
786                         forcibly_detach_exports(obd);
787                 }
788
789                 if (lprocfs_dereg_dev(obd) != LPROCFS_SUCCESS) {
790                         CERROR("Could not remove /proc entry\n");
791                 }
792
793                 if (obd->obd_name) {
794                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
795                         obd->obd_name = NULL;
796                 }
797                 /*
798                 if (obd->obd_proc_entry)
799                         proc_lustre_release_obd_device(obd);
800                 */
801
802                 obd->obd_flags &= ~OBD_ATTACHED;
803                 obd->obd_type->typ_refcnt--;
804                 obd->obd_type = NULL;
805                 CDEBUG(D_IOCTL, "MOD_DEC_USE for detach: count = %d\n",
806                        atomic_read(&(THIS_MODULE)->uc.usecount) - 1);
807                 MOD_DEC_USE_COUNT;
808                 GOTO(out, err = 0);
809         }
810
811         case OBD_IOC_SETUP: {
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                 /* has this been done already? */
819                 if ( obd->obd_flags & OBD_SET_UP ) {
820                         CERROR("Device %d already setup (type %s)\n",
821                                obd->obd_minor, obd->obd_type->typ_name);
822                         GOTO(out, err=-EBUSY);
823                 }
824
825                 if ( OBT(obd) && OBP(obd, setup) )
826                         err = obd_setup(obd, sizeof(*data), data);
827
828                 if (!err) {
829                         obd->obd_type->typ_refcnt++;
830                         obd->obd_flags |= OBD_SET_UP;
831                 }
832
833                 GOTO(out, err);
834         }
835         case OBD_IOC_CLEANUP: {
836                 /* have we attached a type to this device? */
837                 if (!(obd->obd_flags & OBD_ATTACHED)) {
838                         CERROR("Device %d not attached\n", obd->obd_minor);
839                         GOTO(out, err=-ENODEV);
840                 }
841
842                 if ( OBT(obd) && OBP(obd, cleanup) )
843                         err = obd_cleanup(obd);
844
845                 if (!err) {
846                         obd->obd_flags &= ~OBD_SET_UP;
847                         obd->obd_type->typ_refcnt--;
848                 }
849                 GOTO(out, err);
850         }
851
852         case OBD_IOC_CONNECT: {
853                 char * cluuid = "OBD_CLASS_UUID";
854                 obd_data2conn(&conn, data);
855
856                 err = obd_connect(&conn, obd, cluuid, NULL, NULL);
857
858                 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.addr);
859                 obd_conn2data(data, &conn);
860                 if (err)
861                         GOTO(out, err);
862
863                 err = copy_to_user((void *)arg, data, sizeof(*data));
864                 // XXX save connection data into file handle
865                 GOTO(out, err);
866         }
867
868         case OBD_IOC_DISCONNECT: {
869                 obd_data2conn(&conn, data);
870                 err = obd_disconnect(&conn);
871                 GOTO(out, err);
872         }
873
874         case OBD_IOC_DEC_USE_COUNT: {
875                 CDEBUG(D_IOCTL, "MOD_DEC_USE for force dec: count = %d\n",
876                        atomic_read(&(THIS_MODULE)->uc.usecount) - 1);
877                 MOD_DEC_USE_COUNT;
878                 GOTO(out, err=0);
879         }
880
881         case OBD_IOC_CREATE: {
882                 struct lov_stripe_md *lsm = NULL;
883                 obd_data2conn(&conn, data);
884
885 #warning FIXME: save lsm into file handle for other ops, release on close
886                 err = obd_create(&conn, &data->ioc_obdo1, &lsm);
887                 if (err)
888                         GOTO(out, err);
889
890                 err = copy_to_user((void *)arg, data, sizeof(*data));
891                 GOTO(out, err);
892         }
893
894         case OBD_IOC_GETATTR: {
895
896                 obd_data2conn(&conn, data);
897                 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
898                 if (err)
899                         GOTO(out, err);
900
901                 err = copy_to_user((void *)arg, data, sizeof(*data));
902                 GOTO(out, err);
903         }
904
905         case OBD_IOC_SETATTR: {
906                 obd_data2conn(&conn, data);
907                 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
908                 if (err)
909                         GOTO(out, err);
910
911                 err = copy_to_user((void *)arg, data, sizeof(*data));
912                 GOTO(out, err);
913         }
914
915         case OBD_IOC_DESTROY: {
916                 //void *ea;
917                 obd_data2conn(&conn, data);
918
919                 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
920                 if (err)
921                         GOTO(out, err);
922
923                 err = copy_to_user((void *)arg, data, sizeof(*data));
924                 GOTO(out, err);
925         }
926
927         case OBD_IOC_OPEN: {
928                 struct lov_stripe_md *lsm = NULL; // XXX fill in from create
929
930                 obd_data2conn(&conn, data);
931                 err = obd_open(&conn, &data->ioc_obdo1, lsm);
932                 GOTO(out, err);
933         }
934
935         case OBD_IOC_CLOSE: {
936                 struct lov_stripe_md *lsm = NULL; // XXX fill in from create
937
938                 obd_data2conn(&conn, data);
939                 obd_data2conn(&conn, data);
940                 err = obd_close(&conn, &data->ioc_obdo1, lsm);
941                 GOTO(out, err);
942         }
943
944         case OBD_IOC_BRW_WRITE:
945                 rw = OBD_BRW_WRITE;
946         case OBD_IOC_BRW_READ: {
947                 struct lov_stripe_md tmp_lsm; // XXX fill in from create
948                 struct lov_stripe_md *lsm = &tmp_lsm; // XXX fill in from create
949                 struct io_cb_data *cbd = ll_init_cb();
950                 obd_count       pages = 0;
951                 struct brw_page *pga, *pgp;
952                 __u64 id = data->ioc_obdo1.o_id;
953                 int gfp_mask = (id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
954                 int verify = (id != 0);
955                 __u64 off;
956                 int j;
957
958                 if (!cbd)
959                         GOTO(out, err = -ENOMEM);
960
961                 obd_data2conn(&conn, data);
962
963                 pages = data->ioc_count / PAGE_SIZE;
964                 off = data->ioc_offset;
965
966                 CDEBUG(D_INODE, "BRW %s with %d pages @ "LPX64"\n",
967                        rw == OBD_BRW_READ ? "read" : "write", pages, off);
968                 OBD_ALLOC(pga, pages * sizeof(*pga));
969                 if (!pga) {
970                         CERROR("no memory for %d BRW per-page data\n", pages);
971                         GOTO(brw_free, err = -ENOMEM);
972                 }
973
974                 memset(lsm, 0, sizeof(*lsm)); // XXX don't do this later
975                 lsm->lsm_object_id = id; // ensure id == lsm->lsm_object_id
976
977                 for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
978                         pgp->pg = alloc_pages(gfp_mask, 0);
979                         if (!pgp->pg) {
980                                 CERROR("no memory for brw pages\n");
981                                 GOTO(brw_cleanup, err = -ENOMEM);
982                         }
983                         pgp->count = PAGE_SIZE;
984                         pgp->off = off;
985                         pgp->flag = 0;
986
987                         if (verify) {
988                                 void *addr = kmap(pgp->pg);
989
990                                 if (rw == OBD_BRW_WRITE)
991                                         page_debug_setup(addr, pgp->count,
992                                                          pgp->off, id);
993                                 else
994                                         page_debug_setup(addr, pgp->count,
995                                                          0xdeadbeef00c0ffee,
996                                                          0xdeadbeef00c0ffee);
997                                 kunmap(pgp->pg);
998                         }
999                 }
1000
1001                 err = obd_brw(rw, &conn, lsm, j, pga, ll_sync_io_cb, cbd);
1002                 if (err)
1003                         CERROR("test_brw: error from obd_brw: err = %d\n", err);
1004                 EXIT;
1005         brw_cleanup:
1006                 for (j = 0, pgp = pga; j < pages; j++, pgp++) {
1007                         if (pgp->pg != NULL) {
1008                                 if (verify && !err) {
1009                                         void *addr = kmap(pgp->pg);
1010
1011                                         err = page_debug_check("test_brw",
1012                                                                addr,
1013                                                                PAGE_SIZE,
1014                                                                pgp->off,id);
1015                                         kunmap(pgp->pg);
1016                                 }
1017                                 __free_pages(pgp->pg, 0);
1018                         }
1019                 }
1020         brw_free:
1021                 OBD_FREE(pga, pages * sizeof(*pga));
1022                 GOTO(out, err);
1023         }
1024         default:
1025                 obd_data2conn(&conn, data);
1026
1027                 err = obd_iocontrol(cmd, &conn, len, data, NULL);
1028                 if (err)
1029                         GOTO(out, err);
1030
1031                 err = copy_to_user((void *)arg, data, len);
1032                 GOTO(out, err);
1033         }
1034
1035  out:
1036         if (buf)
1037                 OBD_FREE(buf, len);
1038         if (serialised)
1039                 up(&obd_conf_sem);
1040         RETURN(err);
1041 } /* obd_class_ioctl */
1042
1043
1044
1045 /* declare character device */
1046 static struct file_operations obd_psdev_fops = {
1047         ioctl: obd_class_ioctl,       /* ioctl */
1048         open: obd_class_open,        /* open */
1049         release: obd_class_release,     /* release */
1050 };
1051
1052 /* modules setup */
1053 #define OBD_MINOR 241
1054 static struct miscdevice obd_psdev = {
1055         OBD_MINOR,
1056         "obd_psdev",
1057         &obd_psdev_fops
1058 };
1059
1060 void (*class_signal_connection_failure)(struct ptlrpc_connection *);
1061
1062 EXPORT_SYMBOL(obd_dev);
1063 EXPORT_SYMBOL(obdo_cachep);
1064 EXPORT_SYMBOL(handle_cachep);
1065 EXPORT_SYMBOL(obd_memory);
1066 EXPORT_SYMBOL(obd_fail_loc);
1067 EXPORT_SYMBOL(obd_timeout);
1068 EXPORT_SYMBOL(obd_recovery_upcall);
1069 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
1070
1071 EXPORT_SYMBOL(class_register_type);
1072 EXPORT_SYMBOL(class_unregister_type);
1073 EXPORT_SYMBOL(class_name2dev);
1074 EXPORT_SYMBOL(class_uuid2dev);
1075 EXPORT_SYMBOL(class_uuid2obd);
1076 EXPORT_SYMBOL(class_new_export);
1077 EXPORT_SYMBOL(class_destroy_export);
1078 EXPORT_SYMBOL(class_connect);
1079 EXPORT_SYMBOL(class_conn2export);
1080 EXPORT_SYMBOL(class_conn2obd);
1081 EXPORT_SYMBOL(class_conn2cliimp);
1082 EXPORT_SYMBOL(class_conn2ldlmimp);
1083 EXPORT_SYMBOL(class_disconnect);
1084 EXPORT_SYMBOL(class_disconnect_all);
1085 //EXPORT_SYMBOL(class_uuid_parse);
1086 EXPORT_SYMBOL(class_uuid_unparse);
1087 //EXPORT_SYMBOL(class_multi_setup);
1088 //EXPORT_SYMBOL(class_multi_cleanup);
1089
1090 EXPORT_SYMBOL(class_signal_connection_failure);
1091 EXPORT_SYMBOL(ll_sync_io_cb);
1092 EXPORT_SYMBOL(ll_init_cb);
1093
1094 static int __init init_obdclass(void)
1095 {
1096         struct obd_device *obd;
1097         int err;
1098         int i;
1099
1100         printk(KERN_INFO "OBD class driver  v0.9, info@clusterfs.com\n");
1101
1102         sema_init(&obd_conf_sem, 1);
1103         INIT_LIST_HEAD(&obd_types);
1104
1105         if ((err = misc_register(&obd_psdev))) {
1106                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
1107                 return err;
1108         }
1109
1110         /* This struct is already zerod for us (static global) */
1111         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
1112                 obd->obd_minor = i;
1113
1114         err = obd_init_caches();
1115         if (err)
1116                 return err;
1117         obd_sysctl_init();
1118         return 0;
1119 }
1120
1121 static void __exit cleanup_obdclass(void)
1122 {
1123         int i;
1124         ENTRY;
1125
1126         misc_deregister(&obd_psdev);
1127         for (i = 0; i < MAX_OBD_DEVICES; i++) {
1128                 struct obd_device *obd = &obd_dev[i];
1129                 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
1130                     OBT(obd) && OBP(obd, detach)) {
1131                         /* XXX should this call generic detach otherwise? */
1132                         OBP(obd, detach)(obd);
1133                 }
1134         }
1135
1136         obd_cleanup_caches();
1137         obd_sysctl_clean();
1138         CERROR("obd memory leaked: %ld bytes\n", obd_memory);
1139         EXIT;
1140 }
1141
1142 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1143 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
1144 MODULE_LICENSE("GPL");
1145
1146 module_init(init_obdclass);
1147 module_exit(cleanup_obdclass);