Whamcloud - gitweb
- except for fixing the segfault in the documentation build, this is
[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  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  *              Copyright (C) 2002 Cluster File Systems, Inc.
14  */
15
16 #define EXPORT_SYMTAB
17 #include <linux/config.h> /* for CONFIG_PROC_FS */
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/kmod.h>   /* for request_module() */
23 #include <linux/sched.h>
24 #include <linux/lp.h>
25 #include <linux/slab.h>
26 #include <linux/ioport.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h>
31 #include <linux/fs.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/list.h>
35 #include <asm/io.h>
36 #include <asm/system.h>
37 #include <asm/poll.h>
38 #include <asm/uaccess.h>
39 #include <linux/miscdevice.h>
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <linux/obd_support.h>
44 #include <linux/obd_class.h>
45
46 struct obd_device obd_dev[MAX_OBD_DEVICES];
47 extern struct obd_type *class_nm_to_type(char *nm);
48 struct list_head obd_types;
49
50 unsigned long obd_memory = 0;
51 unsigned long obd_fail_loc = 0;
52
53
54 /*  opening /dev/obd */
55 static int obd_class_open(struct inode * inode, struct file * file)
56 {
57         ENTRY;
58
59         file->private_data = NULL;
60         MOD_INC_USE_COUNT;
61         RETURN(0);
62 }
63
64 /*  closing /dev/obd */
65 static int obd_class_release(struct inode * inode, struct file * file)
66 {
67         ENTRY;
68
69         if (file->private_data)
70                 file->private_data = NULL;
71
72         MOD_DEC_USE_COUNT;
73         RETURN(0);
74 }
75
76
77 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
78 {
79         conn->addr = data->ioc_addr;
80         conn->cookie = data->ioc_cookie;
81 }
82
83
84 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
85 {
86         data->ioc_addr = conn->addr;
87         data->ioc_cookie = conn->cookie;
88 }
89
90 /* to control /dev/obd */
91 static int obd_class_ioctl (struct inode * inode, struct file * filp,
92                             unsigned int cmd, unsigned long arg)
93 {
94         /* NOTE this must be larger than any of the ioctl data structs */
95         char buf[1024];
96         int len = 1024;
97         struct obd_ioctl_data *data;
98         struct obd_device *obd = filp->private_data;
99         struct lustre_handle conn;
100         int rw = OBD_BRW_READ;
101         int err = 0;
102         ENTRY;
103
104         memset(buf, 0, sizeof(buf));
105
106         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
107             cmd != OBD_IOC_LIST &&
108             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
109                 CERROR("OBD ioctl: No device\n");
110                 RETURN(-EINVAL);
111         }
112         if (obd_ioctl_getdata(buf, &len, (void *)arg)) {
113                 CERROR("OBD ioctl: data error\n");
114                 RETURN(-EINVAL);
115         }
116         data = (struct obd_ioctl_data *)buf;
117
118         switch (cmd) {
119         case TCGETS:
120                 RETURN(-EINVAL);
121         case OBD_IOC_DEVICE: {
122                 CDEBUG(D_IOCTL, "\n");
123                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
124                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
125                         RETURN(-EINVAL);
126                 }
127                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
128
129                 filp->private_data = &obd_dev[data->ioc_dev];
130                 RETURN(0);
131         }
132
133         case OBD_IOC_LIST: {
134                 int i;
135                 char *buf = data->ioc_bulk;
136                 int remains = data->ioc_inllen1;
137
138                 if (!data->ioc_inlbuf1) {
139                         CERROR("No buffer passed!\n");
140                         RETURN(-EINVAL);
141                 }
142
143
144                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
145                         int l;
146                         struct obd_device *obd = &obd_dev[i];
147                         if (!obd->obd_type) 
148                                 continue;
149                         l = snprintf(buf, remains, "%2d %s %s %s\n",
150                                      i, obd->obd_type->typ_name, 
151                                      obd->obd_name, obd->obd_uuid);
152                         buf +=l;
153                         remains -=l;
154                         if (remains <= 0) { 
155                                 CERROR("not enough space for device listing\n");
156                                 break;
157                         }
158                 }
159
160                 err = copy_to_user((int *)arg, data, len);
161                 RETURN(err);
162         }
163
164
165         case OBD_IOC_NAME2DEV: {
166                 /* Resolve a device name.  This does not change the
167                  * currently selected device.
168                  */
169                 int dev;
170
171                 if (!data->ioc_inlbuf1) {
172                         CERROR("No name passed!\n");
173                         RETURN(-EINVAL);
174                 }
175                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
176                 dev = class_name2dev(data->ioc_inlbuf1);
177                 data->ioc_dev = dev;
178                 if (dev == -1) {
179                         CDEBUG(D_IOCTL, "No device for name %s!\n",
180                                data->ioc_inlbuf1);
181                         RETURN(-EINVAL);
182                 }
183
184                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
185                        dev);
186                 err = copy_to_user((int *)arg, data, sizeof(*data));
187                 RETURN(err);
188         }
189
190         case OBD_IOC_UUID2DEV: {
191                 /* Resolve a device uuid.  This does not change the
192                  * currently selected device.
193                  */
194                 int dev;
195
196                 if (!data->ioc_inlbuf1) {
197                         CERROR("No UUID passed!\n");
198                         RETURN(-EINVAL);
199                 }
200                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
201                 dev = class_uuid2dev(data->ioc_inlbuf1);
202                 data->ioc_dev = dev;
203                 if (dev == -1) {
204                         CDEBUG(D_IOCTL, "No device for name %s!\n",
205                                data->ioc_inlbuf1);
206                         RETURN(-EINVAL);
207                 }
208
209                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
210                        dev);
211                 err = copy_to_user((int *)arg, data, sizeof(*data));
212                 RETURN(err);
213         }
214
215         case OBD_IOC_NEWDEV: {
216                 int dev = -1;
217                 int i;
218
219                 filp->private_data = NULL;
220                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
221                         struct obd_device *obd = &obd_dev[i];
222                         if (!obd->obd_type) {
223                                 filp->private_data = obd;
224                                 dev = i;
225                                 break;
226                         }
227                 }
228
229
230                 data->ioc_dev = dev;
231                 if (dev == -1)
232                         RETURN(-EINVAL);
233
234                 err = copy_to_user((int *)arg, data, sizeof(*data));
235                 RETURN(err);
236         }
237
238         case OBD_IOC_ATTACH: {
239                 struct obd_type *type;
240
241                 /* have we attached a type to this device */
242                 if (obd->obd_flags & OBD_ATTACHED) {
243                         CERROR("OBD: Device %d already typed as %s.\n",
244                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
245                         RETURN(-EBUSY);
246                 }
247
248                 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n", 
249                        MKSTR(data->ioc_inlbuf1),
250                        MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
251
252                 /* find the type */
253                 type = class_nm_to_type(data->ioc_inlbuf1);
254                 if (!type) {
255                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
256                         RETURN(-EINVAL);
257                 }
258
259                 obd->obd_type = type;
260                 obd->obd_multi_count = 0;
261                 INIT_LIST_HEAD(&obd->obd_exports);
262
263                 /* do the attach */
264                 if (OBT(obd) && OBP(obd, attach))
265                         err = OBP(obd,attach)(obd, sizeof(*data), data);
266                 if (err) {
267                         obd->obd_type = NULL;
268                 } else {
269                         obd->obd_flags |= OBD_ATTACHED;
270                         type->typ_refcnt++;
271                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
272                                obd->obd_minor, data->ioc_inlbuf1);
273                         obd->obd_proc_entry =
274                                 proc_lustre_register_obd_device(obd);
275                         if (data->ioc_inlbuf2) {
276                                 int len = strlen(data->ioc_inlbuf2) + 1;
277                                 OBD_ALLOC(obd->obd_name, len + 1);
278                                 if (!obd->obd_name) {
279                                         CERROR("no memory\n");
280                                         LBUG();
281                                 }
282                                 memcpy(obd->obd_name, data->ioc_inlbuf2, len + 1);
283                         }
284                         if (data->ioc_inlbuf3) {
285                                 int len = strlen(data->ioc_inlbuf3);
286                                 if (len > 37) { 
287                                         CERROR("uuid should be shorter than 37 bytes\n");
288                                         if (obd->obd_name)
289                                                 OBD_FREE(obd->obd_name, 
290                                                          strlen(obd->obd_name) + 1);
291                                         RETURN(-EINVAL);
292                                 }
293                                 memcpy(obd->obd_uuid, data->ioc_inlbuf3, 
294                                        sizeof(obd->obd_uuid));
295                         }
296
297                         MOD_INC_USE_COUNT;
298                 }
299
300                 RETURN(err);
301         }
302
303         case OBD_IOC_DETACH: {
304                 ENTRY;
305                 if (obd->obd_flags & OBD_SET_UP) {
306                         CERROR("OBD device %d still set up\n", obd->obd_minor);
307                         RETURN(-EBUSY);
308                 }
309                 if (! (obd->obd_flags & OBD_ATTACHED) ) {
310                         CERROR("OBD device %d not attached\n", obd->obd_minor);
311                         RETURN(-ENODEV);
312                 }
313                 if ( !list_empty(&obd->obd_exports) ) {
314                         CERROR("OBD device %d has exports\n",
315                                obd->obd_minor);
316                         RETURN(-EBUSY);
317                 }
318
319                 if (obd->obd_name) {
320                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+ 1);
321                         obd->obd_name = NULL;
322                 }
323
324                 if (obd->obd_proc_entry)
325                         proc_lustre_release_obd_device(obd);
326
327                 obd->obd_flags &= ~OBD_ATTACHED;
328                 obd->obd_type->typ_refcnt--;
329                 obd->obd_type = NULL;
330                 MOD_DEC_USE_COUNT;
331                 RETURN(0);
332         }
333
334         case OBD_IOC_SETUP: {
335                 /* have we attached a type to this device? */
336                 if (!(obd->obd_flags & OBD_ATTACHED)) {
337                         CERROR("Device %d not attached\n", obd->obd_minor);
338                         RETURN(-ENODEV);
339                 }
340
341                 /* has this been done already? */
342                 if ( obd->obd_flags & OBD_SET_UP ) {
343                         CERROR("Device %d already setup (type %s)\n",
344                                obd->obd_minor, obd->obd_type->typ_name);
345                         RETURN(-EBUSY);
346                 }
347
348                 if ( OBT(obd) && OBP(obd, setup) )
349                         err = obd_setup(obd, sizeof(*data), data);
350
351                 if (!err) {
352                         obd->obd_type->typ_refcnt++;
353                         obd->obd_flags |= OBD_SET_UP;
354                 }
355
356                 RETURN(err);
357         }
358         case OBD_IOC_CLEANUP: {
359                 /* have we attached a type to this device? */
360                 if (!(obd->obd_flags & OBD_ATTACHED)) {
361                         CERROR("Device %d not attached\n", obd->obd_minor);
362                         RETURN(-ENODEV);
363                 }
364
365                 if ( OBT(obd) && OBP(obd, cleanup) )
366                         err = obd_cleanup(obd);
367
368                 if (!err) {
369                         obd->obd_flags &= ~OBD_SET_UP;
370                         obd->obd_type->typ_refcnt--;
371                 }
372                 RETURN(err);
373         }
374
375         case OBD_IOC_CONNECT: {
376                 obd_data2conn(&conn, data); 
377
378                 err = obd_connect(&conn, obd);
379
380                 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
381                 obd_conn2data(data, &conn);
382                 if (err)
383                         RETURN(err);
384
385                 err = copy_to_user((int *)arg, data, sizeof(*data));
386                 RETURN(err);
387         }
388
389         case OBD_IOC_DISCONNECT: {
390                 obd_data2conn(&conn, data);
391                 err = obd_disconnect(&conn);
392                 RETURN(err);
393         }
394
395         case OBD_IOC_DEC_USE_COUNT: {
396                 MOD_DEC_USE_COUNT;
397                 RETURN(0);
398         }
399
400         case OBD_IOC_CREATE: {
401                 obd_data2conn(&conn, data);
402
403                 err = obd_create(&conn, &data->ioc_obdo1);
404                 if (err)
405                         RETURN(err);
406
407                 err = copy_to_user((int *)arg, data, sizeof(*data));
408                 RETURN(err);
409         }
410
411         case OBD_IOC_GETATTR: {
412
413                 obd_data2conn(&conn, data);
414                 err = obd_getattr(&conn, &data->ioc_obdo1);
415                 if (err)
416                         RETURN(err);
417
418                 err = copy_to_user((int *)arg, data, sizeof(*data));
419                 RETURN(err);
420         }
421
422         case OBD_IOC_SETATTR: {
423                 obd_data2conn(&conn, data);
424                 err = obd_setattr(&conn, &data->ioc_obdo1);
425                 if (err)
426                         RETURN(err);
427
428                 err = copy_to_user((int *)arg, data, sizeof(*data));
429                 RETURN(err);
430         }
431
432         case OBD_IOC_DESTROY: {
433                 obd_data2conn(&conn, data);
434
435                 err = obd_destroy(&conn, &data->ioc_obdo1);
436                 if (err)
437                         RETURN(err);
438
439                 err = copy_to_user((int *)arg, data, sizeof(*data));
440                 RETURN(err);
441         }
442
443         case OBD_IOC_BRW_WRITE:
444                 rw = OBD_BRW_WRITE;
445         case OBD_IOC_BRW_READ: {
446                 /* FIXME: use a better ioctl data struct than obd_ioctl_data.
447                  *        We don't really support multiple-obdo I/Os here,
448                  *        for example offset and count are not per-obdo.
449                  */
450                 struct obdo     *obdos[2] = { NULL, NULL };
451                 obd_count       oa_bufs[2] = { 0, 0 };
452                 struct page     **bufs = NULL;
453                 obd_size        *counts = NULL;
454                 obd_off         *offsets = NULL;
455                 obd_flag        *flags = NULL;
456                 int             num = 1;
457                 int             pages;
458                 int             i, j;
459
460                 obd_data2conn(&conn, data);
461
462                 pages = oa_bufs[0] = data->ioc_plen1 / PAGE_SIZE;
463                 if (data->ioc_obdo2.o_id) {
464                         num = 2;
465                         oa_bufs[1] = data->ioc_plen2 / PAGE_SIZE;
466                         pages += oa_bufs[1];
467                 }
468
469                 CDEBUG(D_INODE, "BRW %s with %dx%d pages\n",
470                        rw == OBD_BRW_READ ? "read" : "write",
471                        num, oa_bufs[0]);
472                 OBD_ALLOC(bufs, pages * sizeof(*bufs));
473                 OBD_ALLOC(counts, pages * sizeof(*counts));
474                 OBD_ALLOC(offsets, pages * sizeof(*offsets));
475                 OBD_ALLOC(flags, pages * sizeof(*flags));
476                 if (!bufs || !counts || !offsets || !flags) {
477                         CERROR("no memory for %d BRW per-page data\n", pages);
478                         err = -ENOMEM;
479                         GOTO(brw_free, err);
480                 }
481
482                 obdos[0] = &data->ioc_obdo1;
483                 if (num > 1)
484                         obdos[1] = &data->ioc_obdo2;
485
486                 for (i = 0, pages = 0; i < num; i++) {
487                         unsigned long off;
488                         void *from;
489
490                         from = (&data->ioc_pbuf1)[i];
491                         off = data->ioc_offset;
492
493                         for (j = 0; j < oa_bufs[i];
494                              j++, pages++, off += PAGE_SIZE, from += PAGE_SIZE){
495                                 unsigned long to;
496
497                                 to = __get_free_pages(GFP_KERNEL, 0);
498                                 if (!to) {
499                                 /*      ||
500                                     copy_from_user((void *)to,from,PAGE_SIZE))
501                                         free_pages(to, 0);
502                                  */
503                                         CERROR("no memory for brw pages\n");
504                                         err = -ENOMEM;
505                                         GOTO(brw_cleanup, err);
506                                 }
507                                 bufs[pages] = virt_to_page(to);
508                                 counts[pages] = PAGE_SIZE;
509                                 offsets[pages] = off;
510                                 flags[pages] = 0;
511                         }
512                 }
513
514                 err = obd_brw(rw, &conn, num, obdos, oa_bufs, bufs,
515                               counts, offsets, flags, NULL);
516
517                 EXIT;
518         brw_cleanup:
519                 i = pages;
520                 while (i-- > 0)
521                         __free_pages(bufs[i], 0);
522         brw_free:
523                 OBD_FREE(bufs, pages * sizeof(*bufs));
524                 OBD_FREE(counts, pages * sizeof(*counts));
525                 OBD_FREE(offsets, pages * sizeof(*offsets));
526                 OBD_FREE(flags, pages * sizeof(*flags));
527                 return err;
528         }
529         default: {
530                 obd_data2conn(&conn, data);
531
532                 err = obd_iocontrol(cmd, &conn, sizeof(*data), data, NULL);
533                 if (err)
534                         RETURN(err);
535
536                 err = copy_to_user((int *)arg, data, sizeof(*data));
537                 RETURN(err);
538         }
539         }
540 } /* obd_class_ioctl */
541
542
543
544 /* declare character device */
545 static struct file_operations obd_psdev_fops = {
546         ioctl: obd_class_ioctl,       /* ioctl */
547         open: obd_class_open,        /* open */
548         release: obd_class_release,     /* release */
549 };
550
551 /* modules setup */
552 #define OBD_MINOR 241
553 static struct miscdevice obd_psdev = {
554         OBD_MINOR,
555         "obd_psdev",
556         &obd_psdev_fops
557 };
558
559
560 EXPORT_SYMBOL(obd_dev);
561 EXPORT_SYMBOL(obdo_cachep);
562 EXPORT_SYMBOL(obd_memory);
563 EXPORT_SYMBOL(obd_fail_loc);
564
565 EXPORT_SYMBOL(class_register_type);
566 EXPORT_SYMBOL(class_unregister_type);
567 EXPORT_SYMBOL(class_name2dev);
568 EXPORT_SYMBOL(class_uuid2dev);
569 EXPORT_SYMBOL(class_connect);
570 EXPORT_SYMBOL(class_conn2export);
571 EXPORT_SYMBOL(class_conn2obd);
572 EXPORT_SYMBOL(class_disconnect);
573 EXPORT_SYMBOL(class_multi_setup);
574 EXPORT_SYMBOL(class_multi_cleanup);
575
576 static int __init init_obdclass(void)
577 {
578         int err;
579         int i;
580
581         printk(KERN_INFO "OBD class driver  v0.9, info@clusterfs.com\n");
582
583         INIT_LIST_HEAD(&obd_types);
584
585         if ((err = misc_register(&obd_psdev))) {
586                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
587                 return err;
588         }
589
590         for (i = 0; i < MAX_OBD_DEVICES; i++) {
591                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
592                 obd_dev[i].obd_minor = i;
593                 INIT_LIST_HEAD(&obd_dev[i].obd_exports);
594         }
595
596         err = obd_init_caches();
597         if (err)
598                 return err;
599         obd_sysctl_init();
600         return 0;
601 }
602
603 static void __exit cleanup_obdclass(void)
604 {
605         int i;
606         ENTRY;
607
608         misc_deregister(&obd_psdev);
609         for (i = 0; i < MAX_OBD_DEVICES; i++) {
610                 struct obd_device *obd = &obd_dev[i];
611                 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
612                     OBT(obd) && OBP(obd, detach)) {
613                         /* XXX should this call generic detach otherwise? */
614                         OBP(obd, detach)(obd);
615                 }
616         }
617
618         obd_cleanup_caches();
619         obd_sysctl_clean();
620         CERROR("obd memory leaked: %ld bytes\n", obd_memory);
621         EXIT;
622 }
623
624 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
625 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
626 MODULE_LICENSE("GPL");
627
628 module_init(init_obdclass);
629 module_exit(cleanup_obdclass);