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