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