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