Whamcloud - gitweb
Use OBD_ALLOC for BRW vector test code.
[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  *              An implementation of a loadable kernel mode driver providing
5  *              multiple kernel/user space bidirectional communications links.
6  *
7  *              Author:         Alan Cox <alan@cymru.net>
8  *
9  *              This program is free software; you can redistribute it and/or
10  *              modify it under the terms of the GNU General Public License
11  *              version 2 as published by the Free Software Foundation.
12  *
13  *              Adapted to become the Linux 2.0 Coda pseudo device
14  *              Peter  Braam  <braam@maths.ox.ac.uk>
15  *              Michael Callahan <mjc@emmy.smith.edu>
16  *
17  *              Changes for Linux 2.1
18  *              Copyright (c) 1997 Carnegie-Mellon University
19  *
20  *              Redone again for Intermezzo
21  *              Copyright (c) 1998 Peter J. Braam
22  *
23  *              Hacked up again for simulated OBD
24  *              Copyright (c) 1999 Stelias Computing, Inc.
25  *                (authors {pschwan,braam}@stelias.com)
26  *              Copyright (C) 1999 Seagate Technology, Inc.
27  *              Copyright (C) 2001 Cluster File Systems, Inc.
28  *
29  *
30  */
31
32 #define EXPORT_SYMTAB
33 #include <linux/config.h> /* for CONFIG_PROC_FS */
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/kmod.h>   /* for request_module() */
39 #include <linux/sched.h>
40 #include <linux/lp.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
44 #include <linux/delay.h>
45 #include <linux/skbuff.h>
46 #include <linux/proc_fs.h>
47 #include <linux/fs.h>
48 #include <linux/poll.h>
49 #include <linux/init.h>
50 #include <linux/list.h>
51 #include <asm/io.h>
52 #include <asm/system.h>
53 #include <asm/poll.h>
54 #include <asm/uaccess.h>
55 #include <linux/miscdevice.h>
56
57 #define DEBUG_SUBSYSTEM S_CLASS
58
59 #include <linux/obd_support.h>
60 #include <linux/obd_class.h>
61
62 static int obd_init_magic;
63 unsigned long obd_memory = 0;
64 unsigned long obd_fail_loc = 0;
65 struct obd_device obd_dev[MAX_OBD_DEVICES];
66 struct list_head obd_types;
67
68 /*  opening /dev/obd */
69 static int obd_class_open(struct inode * inode, struct file * file)
70 {
71         ENTRY;
72
73         file->private_data = NULL;
74         MOD_INC_USE_COUNT;
75         RETURN(0);
76 }
77
78 /*  closing /dev/obd */
79 static int obd_class_release(struct inode * inode, struct file * file)
80 {
81         ENTRY;
82
83         if (file->private_data)
84                 file->private_data = NULL;
85
86         MOD_DEC_USE_COUNT;
87         RETURN(0);
88 }
89
90 static int obd_class_name2dev(char *name)
91 {
92         int res = -1;
93         int i;
94
95         for (i=0; i < MAX_OBD_DEVICES; i++) {
96                 struct obd_device *obd = &obd_dev[i];
97                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
98                         res = i;
99                         return res;
100                 }
101         }
102
103         return res;
104 }
105
106 /*
107  * support functions: we could use inter-module communication, but this
108  * is more portable to other OS's
109  */
110 static struct obd_type *obd_search_type(char *nm)
111 {
112         struct list_head *tmp;
113         struct obd_type *type;
114         CDEBUG(D_INFO, "SEARCH %s\n", nm);
115
116         tmp = &obd_types;
117         while ( (tmp = tmp->next) != &obd_types ) {
118                 type = list_entry(tmp, struct obd_type, typ_chain);
119                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
120                 if (strlen(type->typ_name) == strlen(nm) &&
121                     strcmp(type->typ_name, nm) == 0 ) {
122                         return type;
123                 }
124         }
125         return NULL;
126 }
127
128 static struct obd_type *obd_nm_to_type(char *nm)
129 {
130         struct obd_type *type = obd_search_type(nm);
131
132 #ifdef CONFIG_KMOD
133         if ( !type ) {
134                 if ( !request_module(nm) ) {
135                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
136                         type = obd_search_type(nm);
137                 } else {
138                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
139                 }
140         }
141 #endif
142         return type;
143 }
144
145 /* to control /dev/obd */
146 static int obd_class_ioctl (struct inode * inode, struct file * filp,
147                             unsigned int cmd, unsigned long arg)
148 {
149         /* NOTE this must be larger than any of the ioctl data structs */
150         char buf[1024];
151         struct obd_ioctl_data *data;
152         struct obd_device *obd = filp->private_data;
153         struct obd_conn conn;
154         int rw = OBD_BRW_READ;
155         int err = 0;
156         ENTRY;
157
158         memset(buf, 0, sizeof(buf));
159
160         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
161             cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
162                 CERROR("OBD ioctl: No device\n");
163                 RETURN(-EINVAL);
164         }
165         if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) {
166                 CERROR("OBD ioctl: data error\n");
167                 RETURN(-EINVAL);
168         }
169         data = (struct obd_ioctl_data *)buf;
170
171         switch (cmd) {
172         case TCGETS:
173                 RETURN(-EINVAL);
174         case OBD_IOC_DEVICE: {
175                 CDEBUG(D_IOCTL, "\n");
176                 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
177                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
178                         RETURN(-EINVAL);
179                 }
180                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
181
182                 filp->private_data = &obd_dev[data->ioc_dev];
183                 RETURN(0);
184         }
185
186         case OBD_IOC_NAME2DEV: {
187                 /* Resolve a device name.  This does not change the
188                  * currently selected device.
189                  */
190                 int dev;
191
192                 if (!data->ioc_inlbuf1) {
193                         CERROR("No name passed!\n");
194                         RETURN(-EINVAL);
195                 }
196                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
197                 dev = obd_class_name2dev(data->ioc_inlbuf1);
198                 data->ioc_dev = dev;
199                 if (dev == -1) {
200                         CDEBUG(D_IOCTL, "No device for name %s!\n",
201                                data->ioc_inlbuf1);
202                         RETURN(-EINVAL);
203                 }
204
205                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
206                        dev);
207                 err = copy_to_user((int *)arg, data, sizeof(*data));
208                 RETURN(err);
209         }
210
211         case OBD_IOC_NEWDEV: {
212                 int dev = -1;
213                 int i;
214
215                 filp->private_data = NULL;
216                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
217                         struct obd_device *obd = &obd_dev[i];
218                         if (!obd->obd_type) {
219                                 filp->private_data = obd;
220                                 dev = i;
221                                 break;
222                         }
223                 }
224
225
226                 data->ioc_dev = dev;
227                 if (dev == -1)
228                         RETURN(-EINVAL);
229
230                 err = copy_to_user((int *)arg, data, sizeof(*data));
231                 RETURN(err);
232         }
233
234         case OBD_IOC_ATTACH: {
235                 struct obd_type *type;
236
237                 /* have we attached a type to this device */
238                 if (obd->obd_flags & OBD_ATTACHED) {
239                         CERROR("OBD: Device %d already typed as %s.\n",
240                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
241                         RETURN(-EBUSY);
242                 }
243
244                 CDEBUG(D_IOCTL, "attach %s %s\n", MKSTR(data->ioc_inlbuf1),
245                        MKSTR(data->ioc_inlbuf2));
246
247                 /* find the type */
248                 type = obd_nm_to_type(data->ioc_inlbuf1);
249                 if (!type) {
250                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
251                         RETURN(-EINVAL);
252                 }
253
254                 obd->obd_type = type;
255                 obd->obd_multi_count = 0;
256                 INIT_LIST_HEAD(&obd->obd_gen_clients);
257                 INIT_LIST_HEAD(&obd->obd_req_list);
258
259                 /* do the attach */
260                 if (OBT(obd) && OBP(obd, attach))
261                         err = OBP(obd,attach)(obd, sizeof(*data), data);
262                 if (err) {
263                         obd->obd_type = NULL;
264                 } else {
265                         obd->obd_flags |= OBD_ATTACHED;
266                         type->typ_refcnt++;
267                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
268                                obd->obd_minor, data->ioc_inlbuf1);
269                         obd->obd_proc_entry =
270                                 proc_lustre_register_obd_device(obd);
271                         if (data->ioc_inlbuf2) {
272                                 int len = strlen(data->ioc_inlbuf2);
273                                 OBD_ALLOC(obd->obd_name, len + 1);
274                                 if (!obd->obd_name) {
275                                         CERROR("no memory\n");
276                                         LBUG();
277                                 }
278                                 memcpy(obd->obd_name, data->ioc_inlbuf2, len+1);
279                         }
280
281                         MOD_INC_USE_COUNT;
282                 }
283
284                 RETURN(err);
285         }
286
287         case OBD_IOC_DETACH: {
288                 ENTRY;
289                 if (obd->obd_flags & OBD_SET_UP) {
290                         CERROR("OBD device %d still set up\n", obd->obd_minor);
291                         RETURN(-EBUSY);
292                 }
293                 if (! (obd->obd_flags & OBD_ATTACHED) ) {
294                         CERROR("OBD device %d not attached\n", obd->obd_minor);
295                         RETURN(-ENODEV);
296                 }
297                 if ( !list_empty(&obd->obd_gen_clients) ) {
298                         CERROR("OBD device %d has connected clients\n",
299                                obd->obd_minor);
300                         RETURN(-EBUSY);
301                 }
302                 if ( !list_empty(&obd->obd_req_list) ) {
303                         CERROR("OBD device %d has hanging requests\n",
304                                obd->obd_minor);
305                         RETURN(-EBUSY);
306                 }
307
308                 if (obd->obd_name) {
309                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+ 1);
310                         obd->obd_name = NULL;
311                 }
312
313                 if (obd->obd_proc_entry)
314                         proc_lustre_release_obd_device(obd);
315
316                 obd->obd_flags &= ~OBD_ATTACHED;
317                 obd->obd_type->typ_refcnt--;
318                 obd->obd_type = NULL;
319                 MOD_DEC_USE_COUNT;
320                 RETURN(0);
321         }
322
323         case OBD_IOC_SETUP: {
324                 /* have we attached a type to this device? */
325                 if (!(obd->obd_flags & OBD_ATTACHED)) {
326                         CERROR("Device %d not attached\n", obd->obd_minor);
327                         RETURN(-ENODEV);
328                 }
329
330                 /* has this been done already? */
331                 if ( obd->obd_flags & OBD_SET_UP ) {
332                         CERROR("Device %d already setup (type %s)\n",
333                                obd->obd_minor, obd->obd_type->typ_name);
334                         RETURN(-EBUSY);
335                 }
336
337                 if ( OBT(obd) && OBP(obd, setup) )
338                         err = obd_setup(obd, sizeof(*data), data);
339
340                 if (!err) {
341                         obd->obd_type->typ_refcnt++;
342                         obd->obd_flags |= OBD_SET_UP;
343                 }
344
345                 RETURN(err);
346         }
347         case OBD_IOC_CLEANUP: {
348                 /* have we attached a type to this device? */
349                 if (!(obd->obd_flags & OBD_ATTACHED)) {
350                         CERROR("Device %d not attached\n", obd->obd_minor);
351                         RETURN(-ENODEV);
352                 }
353
354                 if ( OBT(obd) && OBP(obd, cleanup) )
355                         err = obd_cleanup(obd);
356
357                 if (!err) {
358                         obd->obd_flags &= ~OBD_SET_UP;
359                         obd->obd_type->typ_refcnt--;
360                 }
361                 RETURN(err);
362         }
363
364         case OBD_IOC_CONNECT: {
365                 conn.oc_id = data->ioc_conn1;
366                 conn.oc_dev = obd;
367
368                 err = obd_connect(&conn);
369
370                 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
371                 data->ioc_conn1 = conn.oc_id;
372                 if (err)
373                         RETURN(err);
374
375                 err = copy_to_user((int *)arg, data, sizeof(*data));
376                 RETURN(err);
377         }
378
379         case OBD_IOC_DISCONNECT: {
380                 conn.oc_id = data->ioc_conn1;
381                 conn.oc_dev = obd;
382
383                 err = obd_disconnect(&conn);
384                 RETURN(err);
385         }
386
387         case OBD_IOC_DEC_USE_COUNT: {
388                 MOD_DEC_USE_COUNT;
389                 RETURN(0);
390         }
391
392         case OBD_IOC_CREATE: {
393                 conn.oc_id = data->ioc_conn1;
394                 conn.oc_dev = obd;
395
396                 err = obd_create(&conn, &data->ioc_obdo1);
397                 if (err)
398                         RETURN(err);
399
400                 err = copy_to_user((int *)arg, data, sizeof(*data));
401                 RETURN(err);
402         }
403
404         case OBD_IOC_GETATTR: {
405                 conn.oc_id = data->ioc_conn1;
406                 conn.oc_dev = obd;
407
408                 err = obd_getattr(&conn, &data->ioc_obdo1);
409                 if (err)
410                         RETURN(err);
411
412                 err = copy_to_user((int *)arg, data, sizeof(*data));
413                 RETURN(err);
414         }
415
416         case OBD_IOC_SETATTR: {
417                 conn.oc_id = data->ioc_conn1;
418                 conn.oc_dev = obd;
419
420                 err = obd_setattr(&conn, &data->ioc_obdo1);
421                 if (err)
422                         RETURN(err);
423
424                 err = copy_to_user((int *)arg, data, sizeof(*data));
425                 RETURN(err);
426         }
427
428         case OBD_IOC_DESTROY: {
429                 conn.oc_id = data->ioc_conn1;
430                 conn.oc_dev = obd;
431
432                 err = obd_destroy(&conn, &data->ioc_obdo1);
433                 if (err)
434                         RETURN(err);
435
436                 err = copy_to_user((int *)arg, data, sizeof(*data));
437                 RETURN(err);
438         }
439
440         case OBD_IOC_BRW_WRITE:
441                 rw = OBD_BRW_WRITE;
442         case OBD_IOC_BRW_READ: {
443                 /* FIXME: use a better ioctl data struct than obd_ioctl_data.
444                  *        We don't really support multiple-obdo I/Os here,
445                  *        for example offset and count are not per-obdo.
446                  */
447                 struct obd_conn conns[2];
448                 struct obdo     *obdos[2] = { NULL, NULL };
449                 obd_count       oa_bufs[2] = { 0, 0 };
450                 struct page     **bufs = NULL;
451                 obd_size        *counts = NULL;
452                 obd_off         *offsets = NULL;
453                 obd_flag        *flags = NULL;
454                 int             num = 1;
455                 int             pages;
456                 int             i, j;
457
458                 pages = oa_bufs[0] = data->ioc_plen1 / PAGE_SIZE;
459                 if (data->ioc_obdo2.o_id) {
460                         num = 2;
461                         oa_bufs[1] = data->ioc_plen2 / PAGE_SIZE;
462                         pages += oa_bufs[1];
463                 }
464
465                 CDEBUG(D_INODE, "BRW %s with %dx%d pages\n",
466                        rw == OBD_BRW_READ ? "read" : "write",
467                        num, oa_bufs[0]);
468                 OBD_ALLOC(bufs, pages * sizeof(*bufs));
469                 OBD_ALLOC(counts, pages * sizeof(*counts));
470                 OBD_ALLOC(offsets, pages * sizeof(*offsets));
471                 OBD_ALLOC(flags, pages * sizeof(*flags));
472                 if (!bufs || !counts || !offsets || !flags) {
473                         CERROR("no memory for %d BRW per-page data\n", pages);
474                         err = -ENOMEM;
475                         GOTO(brw_free, err);
476                 }
477
478                 obdos[0] = &data->ioc_obdo1;
479                 if (num > 1)
480                         obdos[1] = &data->ioc_obdo2;
481
482                 for (i = 0, pages = 0; i < num; i++) {
483                         unsigned long off;
484                         void *from;
485
486                         conns[i].oc_id = (&data->ioc_conn1)[i];
487                         conns[i].oc_dev = obd;
488
489                         from = (&data->ioc_pbuf1)[i];
490                         off = data->ioc_offset;
491
492                         for (j = 0; j < oa_bufs[i];
493                              j++, pages++, off += PAGE_SIZE, from += PAGE_SIZE){
494                                 unsigned long to;
495
496                                 to = __get_free_pages(GFP_KERNEL, 0);
497                                 if (!to) {
498                                 /*      ||
499                                     copy_from_user((void *)to,from,PAGE_SIZE))
500                                         free_pages(to, 0);
501                                  */
502                                         CERROR("no memory for brw pages\n");
503                                         err = -ENOMEM;
504                                         GOTO(brw_cleanup, err);
505                                 }
506                                 bufs[pages] = virt_to_page(to);
507                                 counts[pages] = PAGE_SIZE;
508                                 offsets[pages] = off;
509                                 flags[pages] = 0;
510                         }
511                 }
512
513                 err = obd_brw(rw, conns, num, obdos, oa_bufs, bufs,
514                               counts, offsets, flags);
515
516                 EXIT;
517         brw_cleanup:
518                 while (pages-- > 0)
519                         __free_pages(bufs[pages], 0);
520         brw_free:
521                 OBD_FREE(bufs, pages * sizeof(*bufs));
522                 OBD_FREE(counts, pages * sizeof(*counts));
523                 OBD_FREE(offsets, pages * sizeof(*offsets));
524                 OBD_FREE(flags, pages * sizeof(*flags));
525                 return err;
526         }
527         default: {
528                 conn.oc_id = data->ioc_conn1;
529                 conn.oc_dev = obd;
530
531                 err = obd_iocontrol(cmd, &conn, sizeof(*data), data, NULL);
532                 if (err)
533                         RETURN(err);
534
535                 err = copy_to_user((int *)arg, data, sizeof(*data));
536                 RETURN(err);
537         }
538         }
539 } /* obd_class_ioctl */
540
541
542 /* Driver interface done, utility functions follow */
543 int obd_register_type(struct obd_ops *ops, char *nm)
544 {
545         struct obd_type *type;
546
547         ENTRY;
548
549         if (obd_init_magic != 0x11223344) {
550                 CERROR("bad magic for type\n");
551                 RETURN(-EINVAL);
552         }
553
554         if (obd_nm_to_type(nm)) {
555                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
556                 RETURN(-EEXIST);
557         }
558
559         OBD_ALLOC(type, sizeof(*type));
560         if (!type)
561                 RETURN(-ENOMEM);
562         INIT_LIST_HEAD(&type->typ_chain);
563         MOD_INC_USE_COUNT;
564         list_add(&type->typ_chain, obd_types.next);
565         type->typ_ops = ops;
566         type->typ_name = nm;
567         RETURN(0);
568 }
569
570 int obd_unregister_type(char *nm)
571 {
572         struct obd_type *type = obd_nm_to_type(nm);
573
574         ENTRY;
575
576         if ( !type ) {
577                 MOD_DEC_USE_COUNT;
578                 CERROR("unknown obd type\n");
579                 RETURN(-EINVAL);
580         }
581
582         if ( type->typ_refcnt ) {
583                 MOD_DEC_USE_COUNT;
584                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
585                 RETURN(-EBUSY);
586         }
587
588         list_del(&type->typ_chain);
589         OBD_FREE(type, sizeof(*type));
590         MOD_DEC_USE_COUNT;
591         RETURN(0);
592 } /* obd_unregister_type */
593
594 /* declare character device */
595 static struct file_operations obd_psdev_fops = {
596         ioctl: obd_class_ioctl,       /* ioctl */
597         open: obd_class_open,        /* open */
598         release: obd_class_release,     /* release */
599 };
600
601 /* modules setup */
602 #define OBD_MINOR 241
603 static struct miscdevice obd_psdev = {
604         OBD_MINOR,
605         "obd_psdev",
606         &obd_psdev_fops
607 };
608
609 EXPORT_SYMBOL(obd_register_type);
610 EXPORT_SYMBOL(obd_unregister_type);
611
612 EXPORT_SYMBOL(obd_dev);
613
614 EXPORT_SYMBOL(gen_connect);
615 EXPORT_SYMBOL(gen_client);
616 EXPORT_SYMBOL(gen_cleanup);
617 EXPORT_SYMBOL(gen_disconnect);
618 EXPORT_SYMBOL(gen_copy_data);
619 EXPORT_SYMBOL(obdo_cachep);
620
621 /* EXPORT_SYMBOL(gen_multi_attach); */
622 EXPORT_SYMBOL(gen_multi_setup);
623 EXPORT_SYMBOL(gen_multi_cleanup);
624 EXPORT_SYMBOL(obd_memory);
625 EXPORT_SYMBOL(obd_fail_loc);
626
627 static int __init init_obdclass(void)
628 {
629         int err;
630         int i;
631
632         printk(KERN_INFO "OBD class driver  v0.01, braam@stelias.com\n");
633
634         INIT_LIST_HEAD(&obd_types);
635
636         if ((err = misc_register(&obd_psdev))) {
637                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
638                 return err;
639         }
640
641         for (i = 0; i < MAX_OBD_DEVICES; i++) {
642                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
643                 obd_dev[i].obd_minor = i;
644                 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
645                 INIT_LIST_HEAD(&obd_dev[i].obd_req_list);
646                 init_waitqueue_head(&obd_dev[i].obd_req_waitq);
647         }
648
649         err = obd_init_obdo_cache();
650         if (err)
651                 return err;
652         obd_sysctl_init();
653         obd_init_magic = 0x11223344;
654         return 0;
655 }
656
657 static void __exit cleanup_obdclass(void)
658 {
659         int i;
660         ENTRY;
661
662         misc_deregister(&obd_psdev);
663         for (i = 0; i < MAX_OBD_DEVICES; i++) {
664                 struct obd_device *obd = &obd_dev[i];
665                 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
666                     OBT(obd) && OBP(obd, detach)) {
667                         /* XXX should this call generic detach otherwise? */
668                         OBP(obd, detach)(obd);
669                 }
670         }
671
672         obd_cleanup_obdo_cache();
673         obd_sysctl_clean();
674         CERROR("obd memory leaked: %ld bytes\n", obd_memory);
675         obd_init_magic = 0;
676         EXIT;
677 }
678
679 MODULE_AUTHOR("Cluster File Systems, Inc. <braam@clusterfs.com>");
680 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
681 MODULE_LICENSE("GPL");
682
683 module_init(init_obdclass);
684 module_exit(cleanup_obdclass);