Whamcloud - gitweb
Whitespace cleanup only.
[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                         CERROR("No device for name %s!\n", data->ioc_inlbuf1);
201                         RETURN(-EINVAL);
202                 }
203
204                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
205                        dev);
206                 err = copy_to_user((int *)arg, data, sizeof(*data));
207                 RETURN(err);
208         }
209
210         case OBD_IOC_NEWDEV: {
211                 int dev = -1;
212                 int i;
213
214                 filp->private_data = NULL;
215                 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) { 
216                         struct obd_device *obd = &obd_dev[i];
217                         if (!obd->obd_type) { 
218                                 filp->private_data = obd;
219                                 dev = i;
220                                 break;
221                         }
222                 }
223
224                 
225                 data->ioc_dev = dev; 
226                 if (dev == -1) 
227                         RETURN(-EINVAL); 
228
229                 err = copy_to_user((int *)arg, data, sizeof(*data));
230                 RETURN(err);
231         }
232
233         case OBD_IOC_ATTACH: {
234                 struct obd_type *type;
235
236                 /* have we attached a type to this device */
237                 if (obd->obd_flags & OBD_ATTACHED) {
238                         CERROR("OBD: Device %d already typed as %s.\n",
239                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
240                         RETURN(-EBUSY);
241                 }
242
243                 CDEBUG(D_IOCTL, "attach %s %s\n", MKSTR(data->ioc_inlbuf1),
244                        MKSTR(data->ioc_inlbuf2));
245
246                 /* find the type */
247                 type = obd_nm_to_type(data->ioc_inlbuf1);
248                 if (!type) {
249                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
250                         RETURN(-EINVAL);
251                 }
252
253                 obd->obd_type = type;
254                 obd->obd_multi_count = 0;
255                 INIT_LIST_HEAD(&obd->obd_gen_clients);
256                 INIT_LIST_HEAD(&obd->obd_req_list);
257
258                 /* do the attach */
259                 if (OBT(obd) && OBP(obd, attach))
260                         err = OBP(obd,attach)(obd, sizeof(*data), data);
261                 if (err) {
262                         obd->obd_type = NULL;
263                 } else {
264                         obd->obd_flags |= OBD_ATTACHED;
265                         type->typ_refcnt++;
266                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
267                                obd->obd_minor, data->ioc_inlbuf1);
268                         obd->obd_proc_entry =
269                                 proc_lustre_register_obd_device(obd);
270                         if (data->ioc_inlbuf2) {
271                                 int len = strlen(data->ioc_inlbuf2);
272                                 OBD_ALLOC(obd->obd_name, len + 1);
273                                 if (!obd->obd_name) {
274                                         CERROR("no memory\n");
275                                         LBUG();
276                                 }
277                                 memcpy(obd->obd_name, data->ioc_inlbuf2, len+1);
278                         }
279
280                         MOD_INC_USE_COUNT;
281                 }
282
283                 RETURN(err);
284         }
285
286         case OBD_IOC_DETACH: {
287                 ENTRY;
288                 if (obd->obd_flags & OBD_SET_UP) {
289                         CERROR("OBD device %d still set up\n", obd->obd_minor);
290                         RETURN(-EBUSY);
291                 }
292                 if (! (obd->obd_flags & OBD_ATTACHED) ) {
293                         CERROR("OBD device %d not attached\n", obd->obd_minor);
294                         RETURN(-ENODEV);
295                 }
296                 if ( !list_empty(&obd->obd_gen_clients) ) {
297                         CERROR("OBD device %d has connected clients\n",
298                                obd->obd_minor);
299                         RETURN(-EBUSY);
300                 }
301                 if ( !list_empty(&obd->obd_req_list) ) {
302                         CERROR("OBD device %d has hanging requests\n",
303                                obd->obd_minor);
304                         RETURN(-EBUSY);
305                 }
306
307                 if (obd->obd_name) {
308                         OBD_FREE(obd->obd_name, strlen(obd->obd_name)+ 1);
309                         obd->obd_name = NULL;
310                 }
311
312                 if (obd->obd_proc_entry)
313                         proc_lustre_release_obd_device(obd);
314
315                 obd->obd_flags &= ~OBD_ATTACHED;
316                 obd->obd_type->typ_refcnt--;
317                 obd->obd_type = NULL;
318                 MOD_DEC_USE_COUNT;
319                 RETURN(0);
320         }
321
322         case OBD_IOC_SETUP: {
323                 /* have we attached a type to this device? */
324                 if (!(obd->obd_flags & OBD_ATTACHED)) {
325                         CERROR("Device %d not attached\n", obd->obd_minor);
326                         RETURN(-ENODEV);
327                 }
328
329                 /* has this been done already? */
330                 if ( obd->obd_flags & OBD_SET_UP ) {
331                         CERROR("Device %d already setup (type %s)\n",
332                                obd->obd_minor, obd->obd_type->typ_name);
333                         RETURN(-EBUSY);
334                 }
335
336                 if ( OBT(obd) && OBP(obd, setup) )
337                         err = obd_setup(obd, sizeof(*data), data);
338
339                 if (!err) { 
340                         obd->obd_type->typ_refcnt++;
341                         obd->obd_flags |= OBD_SET_UP;
342                 }
343
344                 RETURN(err);
345         }
346         case OBD_IOC_CLEANUP: {
347                 /* have we attached a type to this device? */
348                 if (!(obd->obd_flags & OBD_ATTACHED)) {
349                         CERROR("Device %d not attached\n", obd->obd_minor);
350                         RETURN(-ENODEV);
351                 }
352
353                 if ( OBT(obd) && OBP(obd, cleanup) )
354                         err = obd_cleanup(obd);
355
356                 if (!err) {
357                         obd->obd_flags &= ~OBD_SET_UP;
358                         obd->obd_type->typ_refcnt--;
359                 }
360                 RETURN(err);
361         }
362
363         case OBD_IOC_CONNECT: {
364                 conn.oc_id = data->ioc_conn1;
365                 conn.oc_dev = obd; 
366
367                 err = obd_connect(&conn);
368
369                 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
370                 data->ioc_conn1 = conn.oc_id;
371                 if (err)
372                         RETURN(err);
373
374                 err = copy_to_user((int *)arg, data, sizeof(*data));
375                 RETURN(err);
376         }
377
378         case OBD_IOC_DISCONNECT: { 
379                 conn.oc_id = data->ioc_conn1;
380                 conn.oc_dev = obd;
381
382                 err = obd_disconnect(&conn);
383                 RETURN(err);
384         }               
385
386         case OBD_IOC_DEC_USE_COUNT: { 
387                 MOD_DEC_USE_COUNT;
388                 RETURN(0);
389         }
390
391         case OBD_IOC_CREATE: {
392                 conn.oc_id = data->ioc_conn1;
393                 conn.oc_dev = obd;
394
395                 err = obd_create(&conn, &data->ioc_obdo1);
396                 if (err)
397                         RETURN(err);
398
399                 err = copy_to_user((int *)arg, data, sizeof(*data));
400                 RETURN(err);
401         }
402
403         case OBD_IOC_GETATTR: {
404                 conn.oc_id = data->ioc_conn1;
405                 conn.oc_dev = obd;
406
407                 err = obd_getattr(&conn, &data->ioc_obdo1);
408                 if (err)
409                         RETURN(err);
410
411                 err = copy_to_user((int *)arg, data, sizeof(*data));
412                 RETURN(err);
413         }
414
415         case OBD_IOC_SETATTR: {
416                 conn.oc_id = data->ioc_conn1;
417                 conn.oc_dev = obd;
418
419                 err = obd_setattr(&conn, &data->ioc_obdo1);
420                 if (err)
421                         RETURN(err);
422
423                 err = copy_to_user((int *)arg, data, sizeof(*data));
424                 RETURN(err);
425         }
426
427         case OBD_IOC_DESTROY: {
428                 conn.oc_id = data->ioc_conn1;
429                 conn.oc_dev = obd;
430
431                 err = obd_destroy(&conn, &data->ioc_obdo1);
432                 if (err)
433                         RETURN(err);
434
435                 err = copy_to_user((int *)arg, data, sizeof(*data));
436                 RETURN(err);
437         }
438
439         case OBD_IOC_BRW_WRITE:
440                 rw = OBD_BRW_WRITE;
441         case OBD_IOC_BRW_READ: {
442                 /* FIXME: use a better ioctl data struct than obd_ioctl_data.
443                  *        We don't really support multiple-obdo I/Os here,
444                  *        for example offset and count are not per-obdo.
445                  */
446                 struct obd_conn conns[2];
447                 struct obdo     *obdos[2] = { NULL, NULL };
448                 obd_count       oa_bufs[2] = { 0, 0 };
449                 struct page     **bufs = NULL;
450                 obd_size        *counts = NULL;
451                 obd_off         *offsets = NULL;
452                 obd_flag        *flags = NULL;
453                 int             num = 1;
454                 int             pages;
455                 int             i, j;
456
457                 pages = oa_bufs[0] = data->ioc_plen1 / PAGE_SIZE;
458                 if (data->ioc_obdo2.o_id) {
459                         num = 2;
460                         oa_bufs[1] = data->ioc_plen2 / PAGE_SIZE;
461                         pages += oa_bufs[1];
462                 }
463
464                 CDEBUG(D_INODE, "BRW %s with %dx%d pages\n",
465                        rw == OBD_BRW_READ ? "read" : "write",
466                        num, oa_bufs[0]);
467                 bufs = kmalloc(pages * sizeof(*bufs), GFP_KERNEL);
468                 counts = kmalloc(pages * sizeof(*counts), GFP_KERNEL);
469                 offsets = kmalloc(pages * sizeof(*offsets), GFP_KERNEL);
470                 flags = kmalloc(pages * sizeof(*flags), GFP_KERNEL);
471                 if (!bufs || !counts || !offsets || !flags) {
472                         CERROR("no memory for %d BRW per-page data\n", pages);
473                         err = -ENOMEM;
474                         GOTO(brw_free, err);
475                 }
476
477                 obdos[0] = &data->ioc_obdo1;
478                 if (num > 1)
479                         obdos[1] = &data->ioc_obdo2;
480
481                 for (i = 0, pages = 0; i < num; i++) {
482                         unsigned long off;
483                         void *from;
484
485                         conns[i].oc_id = (&data->ioc_conn1)[i];
486                         conns[i].oc_dev = obd;
487
488                         from = (&data->ioc_pbuf1)[i];
489                         off = data->ioc_offset;
490
491                         for (j = 0; j < oa_bufs[i];
492                              j++, pages++, off += PAGE_SIZE, from += PAGE_SIZE){
493                                 unsigned long to;
494
495                                 to = __get_free_pages(GFP_KERNEL, 0);
496                                 if (!to) {
497                                 /*      ||
498                                     copy_from_user((void *)to,from,PAGE_SIZE))
499                                         free_pages(to, 0);
500                                  */
501                                         CERROR("no memory for brw pages\n");
502                                         err = -ENOMEM;
503                                         GOTO(brw_cleanup, err);
504                                 }
505                                 bufs[pages] = virt_to_page(to);
506                                 counts[pages] = PAGE_SIZE;
507                                 offsets[pages] = off;
508                                 flags[pages] = 0;
509                         }
510                 }
511
512                 err = obd_brw(rw, conns, num, obdos, oa_bufs, bufs,
513                               counts, offsets, flags);
514
515                 EXIT;
516         brw_cleanup:
517                 while (pages-- > 0)
518                         free_pages((unsigned long)page_address(bufs[pages]), 0);
519         brw_free:
520                 kfree(flags);
521                 kfree(offsets);
522                 kfree(counts);
523                 kfree(bufs);
524                 return err;
525         }
526         default: {
527                 conn.oc_id = data->ioc_conn1;
528                 conn.oc_dev = obd;
529
530                 err = obd_iocontrol(cmd, &conn, sizeof(*data), data, NULL);
531                 if (err)
532                         RETURN(err);
533
534                 err = copy_to_user((int *)arg, data, sizeof(*data));
535                 RETURN(err);
536         }
537         }
538 } /* obd_class_ioctl */
539
540
541 /* Driver interface done, utility functions follow */
542 int obd_register_type(struct obd_ops *ops, char *nm)
543 {
544         struct obd_type *type;
545
546         ENTRY;
547
548         if (obd_init_magic != 0x11223344) {
549                 CERROR("bad magic for type\n");
550                 RETURN(-EINVAL);
551         }
552
553         if (obd_nm_to_type(nm)) {
554                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
555                 RETURN(-EEXIST);
556         }
557
558         OBD_ALLOC(type, sizeof(*type));
559         if (!type)
560                 RETURN(-ENOMEM);
561         INIT_LIST_HEAD(&type->typ_chain);
562         MOD_INC_USE_COUNT;
563         list_add(&type->typ_chain, obd_types.next);
564         type->typ_ops = ops;
565         type->typ_name = nm;
566         RETURN(0);
567 }
568
569 int obd_unregister_type(char *nm)
570 {
571         struct obd_type *type = obd_nm_to_type(nm);
572
573         ENTRY;
574
575         if ( !type ) {
576                 MOD_DEC_USE_COUNT;
577                 CERROR("unknown obd type\n");
578                 RETURN(-EINVAL);
579         }
580
581         if ( type->typ_refcnt ) {
582                 MOD_DEC_USE_COUNT;
583                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
584                 RETURN(-EBUSY);
585         }
586
587         list_del(&type->typ_chain);
588         OBD_FREE(type, sizeof(*type));
589         MOD_DEC_USE_COUNT;
590         RETURN(0);
591 } /* obd_unregister_type */
592
593 /* declare character device */
594 static struct file_operations obd_psdev_fops = {
595         ioctl: obd_class_ioctl,       /* ioctl */
596         open: obd_class_open,        /* open */
597         release: obd_class_release,     /* release */
598 };
599
600 /* modules setup */
601 #define OBD_MINOR 241
602 static struct miscdevice obd_psdev = {
603         OBD_MINOR,
604         "obd_psdev",
605         &obd_psdev_fops
606 };
607
608 EXPORT_SYMBOL(obd_register_type);
609 EXPORT_SYMBOL(obd_unregister_type);
610
611 EXPORT_SYMBOL(obd_dev);
612
613 EXPORT_SYMBOL(gen_connect);
614 EXPORT_SYMBOL(gen_client);
615 EXPORT_SYMBOL(gen_cleanup);
616 EXPORT_SYMBOL(gen_disconnect);
617 EXPORT_SYMBOL(gen_copy_data); 
618 EXPORT_SYMBOL(obdo_cachep);
619
620 /* EXPORT_SYMBOL(gen_multi_attach); */
621 EXPORT_SYMBOL(gen_multi_setup);
622 EXPORT_SYMBOL(gen_multi_cleanup);
623 EXPORT_SYMBOL(obd_memory);
624 EXPORT_SYMBOL(obd_fail_loc);
625
626 static int __init init_obdclass(void)
627 {
628         int err;
629         int i;
630
631         printk(KERN_INFO "OBD class driver  v0.01, braam@stelias.com\n");
632
633         INIT_LIST_HEAD(&obd_types);
634
635         if ((err = misc_register(&obd_psdev))) {
636                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
637                 return err;
638         }
639
640         for (i = 0; i < MAX_OBD_DEVICES; i++) {
641                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
642                 obd_dev[i].obd_minor = i;
643                 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
644                 INIT_LIST_HEAD(&obd_dev[i].obd_req_list);
645                 init_waitqueue_head(&obd_dev[i].obd_req_waitq);
646         }
647
648         err = obd_init_obdo_cache();
649         if (err)
650                 return err;
651         obd_sysctl_init();
652         obd_init_magic = 0x11223344;
653         return 0;
654 }
655
656 static void __exit cleanup_obdclass(void)
657 {
658         int i;
659         ENTRY;
660
661         misc_deregister(&obd_psdev);
662         for (i = 0; i < MAX_OBD_DEVICES; i++) {
663                 struct obd_device *obd = &obd_dev[i];
664                 if ( obd->obd_type && 
665                      (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);