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