1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * These are the only exported functions, they provide some generic
10 * infrastructure for managing object devices
12 * Object Devices Class Driver
13 * Copyright (C) 2002 Cluster File Systems, Inc.
17 #include <linux/config.h> /* for CONFIG_PROC_FS */
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/kmod.h> /* for request_module() */
23 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/ioport.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/list.h>
36 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <linux/miscdevice.h>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <linux/obd_support.h>
44 #include <linux/obd_class.h>
46 struct obd_device obd_dev[MAX_OBD_DEVICES];
47 extern struct obd_type *class_nm_to_type(char *nm);
48 struct list_head obd_types;
50 unsigned long obd_memory = 0;
51 unsigned long obd_fail_loc = 0;
54 /* opening /dev/obd */
55 static int obd_class_open(struct inode * inode, struct file * file)
59 file->private_data = NULL;
64 /* closing /dev/obd */
65 static int obd_class_release(struct inode * inode, struct file * file)
69 if (file->private_data)
70 file->private_data = NULL;
77 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
79 conn->addr = data->ioc_addr;
80 conn->cookie = data->ioc_cookie;
84 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
86 data->ioc_addr = conn->addr;
87 data->ioc_cookie = conn->cookie;
90 /* to control /dev/obd */
91 static int obd_class_ioctl (struct inode * inode, struct file * filp,
92 unsigned int cmd, unsigned long arg)
94 /* NOTE this must be larger than any of the ioctl data structs */
97 struct obd_ioctl_data *data;
98 struct obd_device *obd = filp->private_data;
99 struct lustre_handle conn;
100 int rw = OBD_BRW_READ;
104 memset(buf, 0, sizeof(buf));
106 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
107 cmd != OBD_IOC_LIST &&
108 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
109 CERROR("OBD ioctl: No device\n");
112 if (obd_ioctl_getdata(buf, &len, (void *)arg)) {
113 CERROR("OBD ioctl: data error\n");
116 data = (struct obd_ioctl_data *)buf;
121 case OBD_IOC_DEVICE: {
122 CDEBUG(D_IOCTL, "\n");
123 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
124 CERROR("OBD ioctl: DEVICE insufficient devices\n");
127 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
129 filp->private_data = &obd_dev[data->ioc_dev];
135 char *buf = data->ioc_bulk;
136 int remains = data->ioc_inllen1;
138 if (!data->ioc_inlbuf1) {
139 CERROR("No buffer passed!\n");
144 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
146 struct obd_device *obd = &obd_dev[i];
149 l = snprintf(buf, remains, "%2d %s %s %s\n",
150 i, obd->obd_type->typ_name,
151 obd->obd_name, obd->obd_uuid);
155 CERROR("not enough space for device listing\n");
160 err = copy_to_user((int *)arg, data, len);
165 case OBD_IOC_NAME2DEV: {
166 /* Resolve a device name. This does not change the
167 * currently selected device.
171 if (!data->ioc_inlbuf1) {
172 CERROR("No name passed!\n");
175 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
176 dev = class_name2dev(data->ioc_inlbuf1);
179 CDEBUG(D_IOCTL, "No device for name %s!\n",
184 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
186 err = copy_to_user((int *)arg, data, sizeof(*data));
190 case OBD_IOC_UUID2DEV: {
191 /* Resolve a device uuid. This does not change the
192 * currently selected device.
196 if (!data->ioc_inlbuf1) {
197 CERROR("No UUID passed!\n");
200 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
201 dev = class_uuid2dev(data->ioc_inlbuf1);
204 CDEBUG(D_IOCTL, "No device for name %s!\n",
209 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
211 err = copy_to_user((int *)arg, data, sizeof(*data));
215 case OBD_IOC_NEWDEV: {
219 filp->private_data = NULL;
220 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
221 struct obd_device *obd = &obd_dev[i];
222 if (!obd->obd_type) {
223 filp->private_data = obd;
234 err = copy_to_user((int *)arg, data, sizeof(*data));
238 case OBD_IOC_ATTACH: {
239 struct obd_type *type;
241 /* have we attached a type to this device */
242 if (obd->obd_flags & OBD_ATTACHED) {
243 CERROR("OBD: Device %d already typed as %s.\n",
244 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
248 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
249 MKSTR(data->ioc_inlbuf1),
250 MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
253 type = class_nm_to_type(data->ioc_inlbuf1);
255 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
259 obd->obd_type = type;
260 obd->obd_multi_count = 0;
261 INIT_LIST_HEAD(&obd->obd_exports);
264 if (OBT(obd) && OBP(obd, attach))
265 err = OBP(obd,attach)(obd, sizeof(*data), data);
267 obd->obd_type = NULL;
269 obd->obd_flags |= OBD_ATTACHED;
271 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
272 obd->obd_minor, data->ioc_inlbuf1);
273 obd->obd_proc_entry =
274 proc_lustre_register_obd_device(obd);
275 if (data->ioc_inlbuf2) {
276 int len = strlen(data->ioc_inlbuf2) + 1;
277 OBD_ALLOC(obd->obd_name, len + 1);
278 if (!obd->obd_name) {
279 CERROR("no memory\n");
282 memcpy(obd->obd_name, data->ioc_inlbuf2, len + 1);
284 if (data->ioc_inlbuf3) {
285 int len = strlen(data->ioc_inlbuf3);
287 CERROR("uuid should be shorter than 37 bytes\n");
289 OBD_FREE(obd->obd_name,
290 strlen(obd->obd_name) + 1);
293 memcpy(obd->obd_uuid, data->ioc_inlbuf3,
294 sizeof(obd->obd_uuid));
303 case OBD_IOC_DETACH: {
305 if (obd->obd_flags & OBD_SET_UP) {
306 CERROR("OBD device %d still set up\n", obd->obd_minor);
309 if (! (obd->obd_flags & OBD_ATTACHED) ) {
310 CERROR("OBD device %d not attached\n", obd->obd_minor);
313 if ( !list_empty(&obd->obd_exports) ) {
314 CERROR("OBD device %d has exports\n",
320 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+ 1);
321 obd->obd_name = NULL;
324 if (obd->obd_proc_entry)
325 proc_lustre_release_obd_device(obd);
327 obd->obd_flags &= ~OBD_ATTACHED;
328 obd->obd_type->typ_refcnt--;
329 obd->obd_type = NULL;
334 case OBD_IOC_SETUP: {
335 /* have we attached a type to this device? */
336 if (!(obd->obd_flags & OBD_ATTACHED)) {
337 CERROR("Device %d not attached\n", obd->obd_minor);
341 /* has this been done already? */
342 if ( obd->obd_flags & OBD_SET_UP ) {
343 CERROR("Device %d already setup (type %s)\n",
344 obd->obd_minor, obd->obd_type->typ_name);
348 if ( OBT(obd) && OBP(obd, setup) )
349 err = obd_setup(obd, sizeof(*data), data);
352 obd->obd_type->typ_refcnt++;
353 obd->obd_flags |= OBD_SET_UP;
358 case OBD_IOC_CLEANUP: {
359 /* have we attached a type to this device? */
360 if (!(obd->obd_flags & OBD_ATTACHED)) {
361 CERROR("Device %d not attached\n", obd->obd_minor);
365 if ( OBT(obd) && OBP(obd, cleanup) )
366 err = obd_cleanup(obd);
369 obd->obd_flags &= ~OBD_SET_UP;
370 obd->obd_type->typ_refcnt--;
375 case OBD_IOC_CONNECT: {
376 obd_data2conn(&conn, data);
378 err = obd_connect(&conn, obd);
380 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
381 obd_conn2data(data, &conn);
385 err = copy_to_user((int *)arg, data, sizeof(*data));
389 case OBD_IOC_DISCONNECT: {
390 obd_data2conn(&conn, data);
391 err = obd_disconnect(&conn);
395 case OBD_IOC_DEC_USE_COUNT: {
400 case OBD_IOC_CREATE: {
401 obd_data2conn(&conn, data);
403 err = obd_create(&conn, &data->ioc_obdo1);
407 err = copy_to_user((int *)arg, data, sizeof(*data));
411 case OBD_IOC_GETATTR: {
413 obd_data2conn(&conn, data);
414 err = obd_getattr(&conn, &data->ioc_obdo1);
418 err = copy_to_user((int *)arg, data, sizeof(*data));
422 case OBD_IOC_SETATTR: {
423 obd_data2conn(&conn, data);
424 err = obd_setattr(&conn, &data->ioc_obdo1);
428 err = copy_to_user((int *)arg, data, sizeof(*data));
432 case OBD_IOC_DESTROY: {
433 obd_data2conn(&conn, data);
435 err = obd_destroy(&conn, &data->ioc_obdo1);
439 err = copy_to_user((int *)arg, data, sizeof(*data));
443 case OBD_IOC_BRW_WRITE:
445 case OBD_IOC_BRW_READ: {
446 /* FIXME: use a better ioctl data struct than obd_ioctl_data.
447 * We don't really support multiple-obdo I/Os here,
448 * for example offset and count are not per-obdo.
450 struct obdo *obdos[2] = { NULL, NULL };
451 obd_count oa_bufs[2] = { 0, 0 };
452 struct page **bufs = NULL;
453 obd_size *counts = NULL;
454 obd_off *offsets = NULL;
455 obd_flag *flags = NULL;
460 obd_data2conn(&conn, data);
462 pages = oa_bufs[0] = data->ioc_plen1 / PAGE_SIZE;
463 if (data->ioc_obdo2.o_id) {
465 oa_bufs[1] = data->ioc_plen2 / PAGE_SIZE;
469 CDEBUG(D_INODE, "BRW %s with %dx%d pages\n",
470 rw == OBD_BRW_READ ? "read" : "write",
472 OBD_ALLOC(bufs, pages * sizeof(*bufs));
473 OBD_ALLOC(counts, pages * sizeof(*counts));
474 OBD_ALLOC(offsets, pages * sizeof(*offsets));
475 OBD_ALLOC(flags, pages * sizeof(*flags));
476 if (!bufs || !counts || !offsets || !flags) {
477 CERROR("no memory for %d BRW per-page data\n", pages);
482 obdos[0] = &data->ioc_obdo1;
484 obdos[1] = &data->ioc_obdo2;
486 for (i = 0, pages = 0; i < num; i++) {
490 from = (&data->ioc_pbuf1)[i];
491 off = data->ioc_offset;
493 for (j = 0; j < oa_bufs[i];
494 j++, pages++, off += PAGE_SIZE, from += PAGE_SIZE){
497 to = __get_free_pages(GFP_KERNEL, 0);
500 copy_from_user((void *)to,from,PAGE_SIZE))
503 CERROR("no memory for brw pages\n");
505 GOTO(brw_cleanup, err);
507 bufs[pages] = virt_to_page(to);
508 counts[pages] = PAGE_SIZE;
509 offsets[pages] = off;
514 err = obd_brw(rw, &conn, num, obdos, oa_bufs, bufs,
515 counts, offsets, flags, NULL);
521 __free_pages(bufs[i], 0);
523 OBD_FREE(bufs, pages * sizeof(*bufs));
524 OBD_FREE(counts, pages * sizeof(*counts));
525 OBD_FREE(offsets, pages * sizeof(*offsets));
526 OBD_FREE(flags, pages * sizeof(*flags));
530 obd_data2conn(&conn, data);
532 err = obd_iocontrol(cmd, &conn, sizeof(*data), data, NULL);
536 err = copy_to_user((int *)arg, data, sizeof(*data));
540 } /* obd_class_ioctl */
544 /* declare character device */
545 static struct file_operations obd_psdev_fops = {
546 ioctl: obd_class_ioctl, /* ioctl */
547 open: obd_class_open, /* open */
548 release: obd_class_release, /* release */
552 #define OBD_MINOR 241
553 static struct miscdevice obd_psdev = {
560 EXPORT_SYMBOL(obd_dev);
561 EXPORT_SYMBOL(obdo_cachep);
562 EXPORT_SYMBOL(obd_memory);
563 EXPORT_SYMBOL(obd_fail_loc);
565 EXPORT_SYMBOL(class_register_type);
566 EXPORT_SYMBOL(class_unregister_type);
567 EXPORT_SYMBOL(class_name2dev);
568 EXPORT_SYMBOL(class_uuid2dev);
569 EXPORT_SYMBOL(class_connect);
570 EXPORT_SYMBOL(class_conn2export);
571 EXPORT_SYMBOL(class_conn2obd);
572 EXPORT_SYMBOL(class_disconnect);
573 EXPORT_SYMBOL(class_multi_setup);
574 EXPORT_SYMBOL(class_multi_cleanup);
576 static int __init init_obdclass(void)
581 printk(KERN_INFO "OBD class driver v0.9, info@clusterfs.com\n");
583 INIT_LIST_HEAD(&obd_types);
585 if ((err = misc_register(&obd_psdev))) {
586 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
590 for (i = 0; i < MAX_OBD_DEVICES; i++) {
591 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
592 obd_dev[i].obd_minor = i;
593 INIT_LIST_HEAD(&obd_dev[i].obd_exports);
596 err = obd_init_caches();
603 static void __exit cleanup_obdclass(void)
608 misc_deregister(&obd_psdev);
609 for (i = 0; i < MAX_OBD_DEVICES; i++) {
610 struct obd_device *obd = &obd_dev[i];
611 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
612 OBT(obd) && OBP(obd, detach)) {
613 /* XXX should this call generic detach otherwise? */
614 OBP(obd, detach)(obd);
618 obd_cleanup_caches();
620 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
624 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
625 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
626 MODULE_LICENSE("GPL");
628 module_init(init_obdclass);
629 module_exit(cleanup_obdclass);