Whamcloud - gitweb
Removes all traces of mds_req, mds_rep, ost_req, and ost_rep. All subsystems
[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 /* 
91  * support functions: we could use inter-module communication, but this 
92  * is more portable to other OS's
93  */
94 static struct obd_type *obd_search_type(char *nm)
95 {
96         struct list_head *tmp;
97         struct obd_type *type;
98         CDEBUG(D_INFO, "SEARCH %s\n", nm);
99         
100         tmp = &obd_types;
101         while ( (tmp = tmp->next) != &obd_types ) {
102                 type = list_entry(tmp, struct obd_type, typ_chain);
103                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
104                 if (strlen(type->typ_name) == strlen(nm) &&
105                     strcmp(type->typ_name, nm) == 0 ) {
106                         return type;
107                 }
108         }
109         return NULL;
110 }
111
112 static struct obd_type *obd_nm_to_type(char *nm) 
113 {
114         struct obd_type *type = obd_search_type(nm);
115
116 #ifdef CONFIG_KMOD
117         if ( !type ) {
118                 if ( !request_module(nm) ) {
119                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
120                         type = obd_search_type(nm);
121                 } else {
122                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
123                 }
124         }
125 #endif
126         return type;
127 }
128
129 /* to control /dev/obd */
130 static int obd_class_ioctl (struct inode * inode, struct file * filp, 
131                             unsigned int cmd, unsigned long arg)
132 {
133         /* NOTE this must be larger than any of the ioctl data structs */
134         char buf[1024];
135         struct obd_ioctl_data *data;
136         struct obd_device *obd = filp->private_data;
137         struct obd_conn conn;
138         int rw = OBD_BRW_READ;
139         int err = 0;
140         ENTRY;
141
142         memset(buf, 0, sizeof(buf));
143
144         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS) {
145                 CERROR("OBD ioctl: No device\n");
146                 RETURN(-EINVAL);
147         } 
148         if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) { 
149                 CERROR("OBD ioctl: data error\n");
150                 RETURN(-EINVAL);
151         }
152         data = (struct obd_ioctl_data *)buf;
153
154         switch (cmd) {
155         case TCGETS:
156                 RETURN(-EINVAL);
157         case OBD_IOC_DEVICE: { 
158                 CDEBUG(D_IOCTL, "\n");
159                 if (data->ioc_dev >= MAX_OBD_DEVICES ||
160                     data->ioc_dev < 0) { 
161                         CERROR("OBD ioctl: DEVICE insufficient devices\n");
162                         RETURN(-EINVAL);
163                 }
164                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
165
166                 filp->private_data = &obd_dev[data->ioc_dev];
167                 RETURN(0);
168         }
169
170         case OBD_IOC_ATTACH: {
171                 struct obd_type *type;
172
173                 /* have we attached a type to this device */
174                 if (obd->obd_flags & OBD_ATTACHED) {
175                         CERROR("OBD: Device %d already typed as  %s.\n",
176                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
177                         RETURN(-EBUSY);
178                 }
179
180                 CDEBUG(D_IOCTL, "attach %s %s\n",  MKSTR(data->ioc_inlbuf1), 
181                        MKSTR(data->ioc_inlbuf2));
182
183                 /* find the type */
184                 type = obd_nm_to_type(data->ioc_inlbuf1);
185                 if (!type) {
186                         CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
187                         RETURN(-EINVAL);
188                 }
189
190                 obd->obd_type = type;
191                 obd->obd_multi_count = 0;
192                 INIT_LIST_HEAD(&obd->obd_gen_clients);
193                 INIT_LIST_HEAD(&obd->obd_req_list);
194
195                 /* do the attach */
196                 if (OBT(obd) && OBP(obd, attach))
197                         err = OBP(obd, attach)(obd, sizeof(*data), data);
198
199                 if (err) {
200                         obd->obd_type = NULL;
201                 } else {
202                         obd->obd_flags |=  OBD_ATTACHED;
203                         type->typ_refcnt++;
204                         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n", 
205                                obd->obd_minor, data->ioc_inlbuf1);
206                         obd->obd_proc_entry = 
207                                 proc_lustre_register_obd_device(obd);
208                         MOD_INC_USE_COUNT;
209                 }
210
211                 RETURN(err);
212         }
213
214         case OBD_IOC_DETACH: {
215                 ENTRY;
216                 if (obd->obd_flags & OBD_SET_UP) {
217                         CERROR("OBD device %d still set up\n", obd->obd_minor);
218                         RETURN(-EBUSY);
219                 }
220                 if (! (obd->obd_flags & OBD_ATTACHED) ) {
221                         CERROR("OBD device %d not attached\n", obd->obd_minor);
222                         RETURN(-ENODEV);
223                 }
224                 if ( !list_empty(&obd->obd_gen_clients) ) {
225                         CERROR("OBD device %d has connected clients\n",
226                                obd->obd_minor);
227                         RETURN(-EBUSY);
228                 }
229                 if ( !list_empty(&obd->obd_req_list) ) {
230                         CERROR("OBD device %d has hanging requests\n",
231                                obd->obd_minor);
232                         RETURN(-EBUSY);
233                 }
234
235                 if (obd->obd_proc_entry)
236                         proc_lustre_release_obd_device(obd);
237
238                 obd->obd_flags &= ~OBD_ATTACHED;
239                 obd->obd_type->typ_refcnt--;
240                 obd->obd_type = NULL;
241                 MOD_DEC_USE_COUNT;
242                 RETURN(0);
243         }
244
245         case OBD_IOC_SETUP: {
246                 /* have we attached a type to this device? */
247                 if (!(obd->obd_flags & OBD_ATTACHED)) {
248                         CERROR("Device %d not attached\n", obd->obd_minor);
249                         RETURN(-ENODEV);
250                 }
251
252                 /* has this been done already? */
253                 if ( obd->obd_flags & OBD_SET_UP ) {
254                         CERROR("Device %d already setup (type %s)\n",
255                                obd->obd_minor, obd->obd_type->typ_name);
256                         RETURN(-EBUSY);
257                 }
258
259                 if ( OBT(obd) && OBP(obd, setup) )
260                         err = obd_setup(obd, sizeof(*data), data);
261
262                 if (!err) { 
263                         obd->obd_type->typ_refcnt++;
264                         obd->obd_flags |= OBD_SET_UP;
265                 }
266
267                 RETURN(err);
268         }
269         case OBD_IOC_CLEANUP: {
270                 /* have we attached a type to this device? */
271                 if (!(obd->obd_flags & OBD_ATTACHED)) {
272                         CERROR("Device %d not attached\n", obd->obd_minor);
273                         RETURN(-ENODEV);
274                 }
275
276                 if ( OBT(obd) && OBP(obd, cleanup) )
277                         err = obd_cleanup(obd);
278
279                 if (!err) {
280                         obd->obd_flags &= ~OBD_SET_UP;
281                         obd->obd_type->typ_refcnt--;
282                 }
283                 RETURN(err);
284         }
285
286         case OBD_IOC_CONNECT: {
287                 conn.oc_id = data->ioc_conn1;
288                 conn.oc_dev = obd; 
289
290                 err = obd_connect(&conn);
291
292                 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
293                 data->ioc_conn1 = conn.oc_id;
294                 if (err)
295                         RETURN(err);
296
297                 err = copy_to_user((int *)arg, data, sizeof(*data));
298                 RETURN(err);
299         }
300
301         case OBD_IOC_DISCONNECT: { 
302                 conn.oc_id = data->ioc_conn1;
303                 conn.oc_dev = obd;
304
305                 err = obd_disconnect(&conn);
306                 RETURN(err);
307         }               
308
309         case OBD_IOC_DEC_USE_COUNT: { 
310                 MOD_DEC_USE_COUNT;
311                 RETURN(0);
312         }
313
314         case OBD_IOC_CREATE: {
315                 conn.oc_id = data->ioc_conn1;
316                 conn.oc_dev = obd;
317
318                 err = obd_create(&conn, &data->ioc_obdo1);
319                 if (err)
320                         RETURN(err);
321
322                 err = copy_to_user((int *)arg, data, sizeof(*data));
323                 RETURN(err);
324         }
325
326         case OBD_IOC_GETATTR: {
327                 conn.oc_id = data->ioc_conn1;
328                 conn.oc_dev = obd;
329
330                 err = obd_getattr(&conn, &data->ioc_obdo1);
331                 if (err)
332                         RETURN(err);
333
334                 err = copy_to_user((int *)arg, data, sizeof(*data));
335                 RETURN(err);
336         }
337
338         case OBD_IOC_SETATTR: {
339                 conn.oc_id = data->ioc_conn1;
340                 conn.oc_dev = obd;
341
342                 err = obd_setattr(&conn, &data->ioc_obdo1);
343                 if (err)
344                         RETURN(err);
345
346                 err = copy_to_user((int *)arg, data, sizeof(*data));
347                 RETURN(err);
348         }
349
350         case OBD_IOC_DESTROY: {
351                 conn.oc_id = data->ioc_conn1;
352                 conn.oc_dev = obd;
353
354                 err = obd_destroy(&conn, &data->ioc_obdo1);
355                 if (err)
356                         RETURN(err);
357
358                 err = copy_to_user((int *)arg, data, sizeof(*data));
359                 RETURN(err);
360         }
361
362         case OBD_IOC_BRW_WRITE:
363                 rw = OBD_BRW_WRITE;
364         case OBD_IOC_BRW_READ: {
365                 /* FIXME: use a better ioctl data struct than obd_ioctl_data.
366                  *        We don't really support multiple-obdo I/Os here,
367                  *        for example offset and count are not per-obdo.
368                  */
369                 struct obd_conn conns[2];
370                 struct obdo     *obdos[2] = { NULL, NULL };
371                 obd_count       oa_bufs[2] = { 0, 0 };
372                 struct page     **bufs = NULL;
373                 obd_size        *counts = NULL;
374                 obd_off         *offsets = NULL;
375                 obd_flag        *flags = NULL;
376                 int             num = 1;
377                 int             pages;
378                 int             i, j;
379
380                 pages = oa_bufs[0] = data->ioc_plen1 / PAGE_SIZE;
381                 if (data->ioc_obdo2.o_id) {
382                         num = 2;
383                         oa_bufs[1] = data->ioc_plen2 / PAGE_SIZE;
384                         pages += oa_bufs[1];
385                 }
386
387                 CDEBUG(D_INODE, "BRW %s with %dx%d pages\n",
388                        rw == OBD_BRW_READ ? "read" : "write",
389                        num, oa_bufs[0]);
390                 bufs = kmalloc(pages * sizeof(*bufs), GFP_KERNEL);
391                 counts = kmalloc(pages * sizeof(*counts), GFP_KERNEL);
392                 offsets = kmalloc(pages * sizeof(*offsets), GFP_KERNEL);
393                 flags = kmalloc(pages * sizeof(*flags), GFP_KERNEL);
394                 if (!bufs || !counts || !offsets || !flags) {
395                         CERROR("no memory for %d BRW per-page data\n", pages);
396                         err = -ENOMEM;
397                         GOTO(brw_free, err);
398                 }
399
400                 obdos[0] = &data->ioc_obdo1;
401                 if (num > 1)
402                         obdos[1] = &data->ioc_obdo2;
403
404                 for (i = 0, pages = 0; i < num; i++) {
405                         unsigned long off;
406                         void *from;
407
408                         conns[i].oc_id = (&data->ioc_conn1)[i];
409                         conns[i].oc_dev = obd;
410
411                         from = (&data->ioc_pbuf1)[i];
412                         off = data->ioc_offset;
413
414                         for (j = 0; j < oa_bufs[i];
415                              j++, pages++, off += PAGE_SIZE, from += PAGE_SIZE){
416                                 unsigned long to;
417
418                                 to = __get_free_pages(GFP_KERNEL, 0);
419                                 if (!to) {
420                                 /*      ||
421                                     copy_from_user((void *)to,from,PAGE_SIZE))
422                                         free_pages(to, 0);
423                                  */
424                                         CERROR("no memory for brw pages\n");
425                                         err = -ENOMEM;
426                                         GOTO(brw_cleanup, err);
427                                 }
428                                 bufs[pages] = virt_to_page(to);
429                                 counts[pages] = PAGE_SIZE;
430                                 offsets[pages] = off;
431                                 flags[pages] = 0;
432                         }
433                 }
434
435                 err = obd_brw(rw, conns, num, obdos, oa_bufs, bufs,
436                               counts, offsets, flags);
437
438                 EXIT;
439         brw_cleanup:
440                 while (pages-- > 0)
441                         free_pages((unsigned long)page_address(bufs[pages]), 0);
442         brw_free:
443                 kfree(flags);
444                 kfree(offsets);
445                 kfree(counts);
446                 kfree(bufs);
447                 return err;
448         }
449         default: {
450                 conn.oc_id = data->ioc_conn1;
451                 conn.oc_dev = obd;
452
453                 err = obd_iocontrol(cmd, &conn, sizeof(*data), data, NULL);
454                 if (err)
455                         RETURN(err);
456
457                 err = copy_to_user((int *)arg, data, sizeof(*data));
458                 RETURN(err);
459         }
460         }
461 } /* obd_class_ioctl */
462
463
464 /* Driver interface done, utility functions follow */
465 int obd_register_type(struct obd_ops *ops, char *nm)
466 {
467         struct obd_type *type;
468
469         ENTRY;
470
471         if (obd_init_magic != 0x11223344) {
472                 CERROR("bad magic for type\n");
473                 RETURN(-EINVAL);
474         }
475
476         if  ( obd_nm_to_type(nm) ) {
477                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
478                 RETURN(-EEXIST);
479         }
480         
481         OBD_ALLOC(type, sizeof(*type));
482         if (!type)
483                 RETURN(-ENOMEM);
484         INIT_LIST_HEAD(&type->typ_chain);
485         MOD_INC_USE_COUNT;
486         list_add(&type->typ_chain, obd_types.next);
487         type->typ_ops = ops;
488         type->typ_name = nm;
489         RETURN(0);
490 }
491         
492 int obd_unregister_type(char *nm)
493 {
494         struct obd_type *type = obd_nm_to_type(nm);
495
496         ENTRY;
497
498         if ( !type ) {
499                 MOD_DEC_USE_COUNT;
500                 CERROR("unknown obd type\n");
501                 RETURN(-EINVAL);
502         }
503
504         if ( type->typ_refcnt ) {
505                 MOD_DEC_USE_COUNT;
506                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
507                 RETURN(-EBUSY);
508         }
509
510         list_del(&type->typ_chain);
511         OBD_FREE(type, sizeof(*type));
512         MOD_DEC_USE_COUNT;
513         RETURN(0);
514 } /* obd_unregister_type */
515
516 /* declare character device */
517 static struct file_operations obd_psdev_fops = {
518         ioctl: obd_class_ioctl,       /* ioctl */
519         open: obd_class_open,        /* open */
520         release: obd_class_release,     /* release */
521 };
522
523 /* modules setup */
524 #define OBD_MINOR 241
525 static struct miscdevice obd_psdev = {
526         OBD_MINOR,
527         "obd_psdev",
528         &obd_psdev_fops
529 };
530
531 EXPORT_SYMBOL(obd_register_type);
532 EXPORT_SYMBOL(obd_unregister_type);
533
534 EXPORT_SYMBOL(obd_dev);
535
536 EXPORT_SYMBOL(gen_connect);
537 EXPORT_SYMBOL(gen_client);
538 EXPORT_SYMBOL(gen_cleanup);
539 EXPORT_SYMBOL(gen_disconnect);
540 EXPORT_SYMBOL(gen_copy_data); 
541 EXPORT_SYMBOL(obdo_cachep);
542
543 /* EXPORT_SYMBOL(gen_multi_attach); */
544 EXPORT_SYMBOL(gen_multi_setup);
545 EXPORT_SYMBOL(gen_multi_cleanup);
546 EXPORT_SYMBOL(obd_memory);
547 EXPORT_SYMBOL(obd_fail_loc);
548
549 static int __init init_obdclass(void)
550 {
551         int err;
552         int i;
553
554         printk(KERN_INFO "OBD class driver  v0.01, braam@stelias.com\n");
555
556         INIT_LIST_HEAD(&obd_types);
557
558         if ((err = misc_register(&obd_psdev))) {
559                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
560                 return err;
561         }
562
563         for (i = 0; i < MAX_OBD_DEVICES; i++) {
564                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
565                 obd_dev[i].obd_minor = i;
566                 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
567                 INIT_LIST_HEAD(&obd_dev[i].obd_req_list);
568                 init_waitqueue_head(&obd_dev[i].obd_req_waitq);
569         }
570
571         err = obd_init_obdo_cache();
572         if (err)
573                 return err;
574         obd_sysctl_init();
575         obd_init_magic = 0x11223344;
576         return 0;
577 }
578
579 static void __exit cleanup_obdclass(void)
580 {
581         int i;
582         ENTRY;
583
584         misc_deregister(&obd_psdev);
585         for (i = 0; i < MAX_OBD_DEVICES; i++) {
586                 struct obd_device *obd = &obd_dev[i];
587                 if ( obd->obd_type && 
588                      (obd->obd_flags & OBD_SET_UP) &&
589                      OBT(obd) && OBP(obd, detach) ) {
590                         /* XXX should this call generic detach otherwise? */
591                         OBP(obd, detach)(obd);
592                 }
593         }
594
595         obd_cleanup_obdo_cache();
596         obd_sysctl_clean();
597         CERROR("obd memory leaked: %ld bytes\n", obd_memory);
598         obd_init_magic = 0;
599         EXIT;
600 }
601
602 MODULE_AUTHOR("Cluster File Systems, Inc. <braam@clusterfs.com>");
603 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
604 MODULE_LICENSE("GPL"); 
605
606 module_init(init_obdclass);
607 module_exit(cleanup_obdclass);