1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 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
16 #include <linux/config.h> /* for CONFIG_PROC_FS */
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/ioport.h>
25 #include <linux/fcntl.h>
26 #include <linux/delay.h>
27 #include <linux/skbuff.h>
28 #include <linux/proc_fs.h>
30 #include <linux/poll.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
34 #include <asm/ioctls.h>
35 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <linux/miscdevice.h>
40 #define DEBUG_SUBSYSTEM S_CLASS
42 #include <linux/obd_support.h>
43 #include <linux/obd_class.h>
44 #include <linux/lustre_debug.h>
45 #include <linux/smp_lock.h>
46 #include <linux/lprocfs_status.h>
48 struct semaphore obd_conf_sem; /* serialize configuration commands */
49 struct obd_device obd_dev[MAX_OBD_DEVICES];
50 struct list_head obd_types;
51 unsigned long obd_memory;
53 /* The following are visible and mutable through /proc/sys/lustre/. */
54 unsigned long obd_fail_loc;
55 unsigned long obd_timeout = 100;
56 char obd_recovery_upcall[128] = "/usr/lib/lustre/ha_assist";
58 extern struct obd_type *class_nm_to_type(char *nm);
60 /* opening /dev/obd */
61 static int obd_class_open(struct inode * inode, struct file * file)
65 file->private_data = NULL;
66 CDEBUG(D_IOCTL, "MOD_INC_USE for open: count = %d\n",
67 atomic_read(&(THIS_MODULE)->uc.usecount));
72 /* closing /dev/obd */
73 static int obd_class_release(struct inode * inode, struct file * file)
77 // XXX drop lsm, connections here
78 if (file->private_data)
79 file->private_data = NULL;
81 CDEBUG(D_IOCTL, "MOD_DEC_USE for close: count = %d\n",
82 atomic_read(&(THIS_MODULE)->uc.usecount) - 1);
88 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
90 conn->addr = data->ioc_addr;
91 conn->cookie = data->ioc_cookie;
95 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
97 data->ioc_addr = conn->addr;
98 data->ioc_cookie = conn->cookie;
101 static void forcibly_detach_exports(struct obd_device *obd)
104 struct list_head *tmp, *n;
105 struct lustre_handle fake_conn;
107 CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
108 "disconnecting them", obd->obd_minor, obd);
109 list_for_each_safe(tmp, n, &obd->obd_exports) {
110 struct obd_export *exp = list_entry(tmp, struct obd_export,
112 fake_conn.addr = (__u64)(unsigned long)exp;
113 fake_conn.cookie = exp->exp_cookie;
114 rc = obd_disconnect(&fake_conn);
116 CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
119 CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
124 /* to control /dev/obd */
125 static int obd_class_ioctl (struct inode * inode, struct file * filp,
126 unsigned int cmd, unsigned long arg)
130 struct obd_ioctl_data *data;
131 struct obd_device *obd = filp->private_data;
133 struct lustre_handle conn;
134 int rw = OBD_BRW_READ;
142 case OBD_IOC_BRW_WRITE:
143 case OBD_IOC_BRW_READ:
144 case OBD_IOC_GETATTR:
152 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
153 cmd != OBD_IOC_LIST &&
154 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
155 CERROR("OBD ioctl: No device\n");
156 GOTO(out, err=-EINVAL);
158 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
159 CERROR("OBD ioctl: data error\n");
160 GOTO(out, err=-EINVAL);
162 data = (struct obd_ioctl_data *)buf;
166 GOTO(out, err=-EINVAL);
167 case OBD_IOC_DEVICE: {
168 CDEBUG(D_IOCTL, "\n");
169 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
170 CERROR("OBD ioctl: DEVICE insufficient devices\n");
171 GOTO(out, err=-EINVAL);
173 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
175 filp->private_data = &obd_dev[data->ioc_dev];
181 char *buf2 = data->ioc_bulk;
182 int remains = data->ioc_inllen1;
184 if (!data->ioc_inlbuf1) {
185 CERROR("No buffer passed!\n");
186 GOTO(out, err=-EINVAL);
190 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
193 struct obd_device *obd = &obd_dev[i];
196 if (obd->obd_flags & OBD_SET_UP)
198 else if (obd->obd_flags & OBD_ATTACHED)
202 l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
203 i, status, obd->obd_type->typ_name,
204 obd->obd_name, obd->obd_uuid, obd->obd_type->typ_refcnt);
208 CERROR("not enough space for device listing\n");
213 err = copy_to_user((void *)arg, data, len);
218 case OBD_IOC_NAME2DEV: {
219 /* Resolve a device name. This does not change the
220 * currently selected device.
224 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
225 CERROR("No name passed,!\n");
226 GOTO(out, err=-EINVAL);
228 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
229 CERROR("Name not nul terminated!\n");
230 GOTO(out, err=-EINVAL);
233 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
234 dev = class_name2dev(data->ioc_inlbuf1);
237 CDEBUG(D_IOCTL, "No device for name %s!\n",
239 GOTO(out, err=-EINVAL);
242 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
244 err = copy_to_user((void *)arg, data, sizeof(*data));
248 case OBD_IOC_UUID2DEV: {
249 /* Resolve a device uuid. This does not change the
250 * currently selected device.
254 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
255 CERROR("No UUID passed!\n");
256 GOTO(out, err=-EINVAL);
258 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
259 CERROR("Name not nul terminated!\n");
260 GOTO(out, err=-EINVAL);
263 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
264 dev = class_uuid2dev(data->ioc_inlbuf1);
267 CDEBUG(D_IOCTL, "No device for name %s!\n",
269 GOTO(out, err=-EINVAL);
272 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
274 err = copy_to_user((void *)arg, data, sizeof(*data));
278 case OBD_IOC_NEWDEV: {
282 filp->private_data = NULL;
283 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
284 struct obd_device *obd = &obd_dev[i];
285 if (!obd->obd_type) {
286 filp->private_data = obd;
295 GOTO(out, err=-EINVAL);
297 err = copy_to_user((void *)arg, data, sizeof(*data));
301 case OBD_IOC_ATTACH: {
302 struct obd_type *type;
305 /* have we attached a type to this device */
306 if (obd->obd_flags & OBD_ATTACHED || obd->obd_type) {
307 CERROR("OBD: Device %d already typed as %s.\n",
308 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
309 GOTO(out, err=-EBUSY);
312 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
313 CERROR("No type passed!\n");
314 GOTO(out, err=-EINVAL);
316 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
317 CERROR("Type not nul terminated!\n");
318 GOTO(out, err=-EINVAL);
321 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
322 MKSTR(data->ioc_inlbuf1),
323 MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
326 type = class_nm_to_type(data->ioc_inlbuf1);
328 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
329 GOTO(out, err=-EINVAL);
332 minor = obd->obd_minor;
333 memset(obd, 0, sizeof(*obd));
334 obd->obd_minor = minor;
335 obd->obd_type = type;
336 INIT_LIST_HEAD(&obd->obd_exports);
337 INIT_LIST_HEAD(&obd->obd_imports);
338 spin_lock_init(&obd->obd_dev_lock);
340 if (data->ioc_inlbuf2) {
341 int len = strlen(data->ioc_inlbuf2) + 1;
342 OBD_ALLOC(obd->obd_name, len);
343 if (!obd->obd_name) {
344 CERROR("no memory\n");
347 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
349 CERROR("WARNING: unnamed obd device\n");
351 if (data->ioc_inlbuf3) {
352 int len = strlen(data->ioc_inlbuf3);
353 if (len >= sizeof(obd->obd_uuid)) {
354 CERROR("uuid must be < %d bytes long\n",
355 sizeof(obd->obd_uuid));
357 OBD_FREE(obd->obd_name,
358 strlen(obd->obd_name) + 1);
359 GOTO(out, err=-EINVAL);
361 memcpy(obd->obd_uuid, data->ioc_inlbuf3, len);
364 if (OBP(obd, attach))
365 err = OBP(obd,attach)(obd, sizeof(*data), data);
367 if(data->ioc_inlbuf2)
368 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
369 obd->obd_type = NULL;
372 obd->obd_flags |= OBD_ATTACHED;
375 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
376 obd->obd_minor, data->ioc_inlbuf1);
378 CDEBUG(D_IOCTL, "MOD_INC_USE for attach: count = %d\n",
379 atomic_read(&(THIS_MODULE)->uc.usecount));
386 case OBD_IOC_DETACH: {
388 if (obd->obd_flags & OBD_SET_UP) {
389 CERROR("OBD device %d still set up\n", obd->obd_minor);
390 GOTO(out, err=-EBUSY);
392 if (!(obd->obd_flags & OBD_ATTACHED) ) {
393 CERROR("OBD device %d not attached\n", obd->obd_minor);
394 GOTO(out, err=-ENODEV);
396 if (!list_empty(&obd->obd_exports)) {
397 if (!data->ioc_inlbuf1 || data->ioc_inlbuf1[0] != 'F') {
398 CERROR("OBD device %d (%p) has exports\n",
399 obd->obd_minor, obd);
400 GOTO(out, err=-EBUSY);
402 forcibly_detach_exports(obd);
404 if (OBP(obd, detach))
405 err=OBP(obd,detach)(obd);
408 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
409 obd->obd_name = NULL;
412 obd->obd_flags &= ~OBD_ATTACHED;
413 obd->obd_type->typ_refcnt--;
414 obd->obd_type = NULL;
415 CDEBUG(D_IOCTL, "MOD_DEC_USE for detach: count = %d\n",
416 atomic_read(&(THIS_MODULE)->uc.usecount) - 1);
421 case OBD_IOC_SETUP: {
422 /* have we attached a type to this device? */
423 if (!(obd->obd_flags & OBD_ATTACHED)) {
424 CERROR("Device %d not attached\n", obd->obd_minor);
425 GOTO(out, err=-ENODEV);
428 /* has this been done already? */
429 if ( obd->obd_flags & OBD_SET_UP ) {
430 CERROR("Device %d already setup (type %s)\n",
431 obd->obd_minor, obd->obd_type->typ_name);
432 GOTO(out, err=-EBUSY);
435 if ( OBT(obd) && OBP(obd, setup) )
436 err = obd_setup(obd, sizeof(*data), data);
439 obd->obd_type->typ_refcnt++;
440 obd->obd_flags |= OBD_SET_UP;
445 case OBD_IOC_CLEANUP: {
446 /* have we attached a type to this device? */
447 if (!(obd->obd_flags & OBD_ATTACHED)) {
448 CERROR("Device %d not attached\n", obd->obd_minor);
449 GOTO(out, err=-ENODEV);
452 if ( OBT(obd) && OBP(obd, cleanup) )
453 err = obd_cleanup(obd);
456 obd->obd_flags &= ~OBD_SET_UP;
457 obd->obd_type->typ_refcnt--;
462 case OBD_IOC_CONNECT: {
463 char * cluuid = "OBD_CLASS_UUID";
464 obd_data2conn(&conn, data);
466 err = obd_connect(&conn, obd, cluuid, NULL, NULL);
468 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.addr);
469 obd_conn2data(data, &conn);
473 err = copy_to_user((void *)arg, data, sizeof(*data));
474 // XXX save connection data into file handle
478 case OBD_IOC_DISCONNECT: {
479 obd_data2conn(&conn, data);
480 err = obd_disconnect(&conn);
484 case OBD_IOC_DEC_USE_COUNT: {
485 CDEBUG(D_IOCTL, "MOD_DEC_USE for force dec: count = %d\n",
486 atomic_read(&(THIS_MODULE)->uc.usecount) - 1);
491 case OBD_IOC_CREATE: {
492 struct lov_stripe_md *lsm = NULL;
493 obd_data2conn(&conn, data);
495 #warning FIXME: save lsm into file handle for other ops, release on close
496 err = obd_create(&conn, &data->ioc_obdo1, &lsm);
498 err = copy_to_user((void *)arg, data, sizeof(*data));
502 case OBD_IOC_GETATTR: {
504 obd_data2conn(&conn, data);
505 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
507 err = copy_to_user((void *)arg, data, sizeof(*data));
511 case OBD_IOC_SETATTR: {
512 obd_data2conn(&conn, data);
513 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
515 err = copy_to_user((void *)arg, data, sizeof(*data));
519 case OBD_IOC_DESTROY: {
521 obd_data2conn(&conn, data);
523 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
525 err = copy_to_user((void *)arg, data, sizeof(*data));
530 struct lov_stripe_md *lsm = NULL; // XXX fill in from create
532 obd_data2conn(&conn, data);
533 err = obd_open(&conn, &data->ioc_obdo1, lsm);
535 err = copy_to_user((void *)arg, data, sizeof(*data));
539 case OBD_IOC_CLOSE: {
540 struct lov_stripe_md *lsm = NULL; // XXX fill in from create
542 obd_data2conn(&conn, data);
543 err = obd_close(&conn, &data->ioc_obdo1, lsm);
547 case OBD_IOC_BRW_WRITE:
549 case OBD_IOC_BRW_READ: {
550 struct lov_stripe_md tmp_lsm; // XXX fill in from create
551 struct lov_stripe_md *lsm = &tmp_lsm; // XXX fill in from create
552 struct brw_cb_data *brw_cbd = ll_init_brw_cb_data();
554 struct brw_page *pga, *pgp;
555 __u64 id = data->ioc_obdo1.o_id;
556 int gfp_mask = (id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
557 int verify = (id != 0);
562 GOTO(out, err = -ENOMEM);
564 obd_data2conn(&conn, data);
566 pages = data->ioc_count / PAGE_SIZE;
567 off = data->ioc_offset;
569 CDEBUG(D_INODE, "BRW %s with %d pages @ "LPX64"\n",
570 rw == OBD_BRW_READ ? "read" : "write", pages, off);
571 OBD_ALLOC(pga, pages * sizeof(*pga));
573 CERROR("no memory for %d BRW per-page data\n", pages);
574 GOTO(brw_free, err = -ENOMEM);
577 memset(lsm, 0, sizeof(*lsm)); // XXX don't do this later
578 lsm->lsm_object_id = id; // ensure id == lsm->lsm_object_id
580 for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
581 pgp->pg = alloc_pages(gfp_mask, 0);
583 CERROR("no memory for brw pages\n");
584 GOTO(brw_cleanup, err = -ENOMEM);
586 pgp->count = PAGE_SIZE;
591 void *addr = kmap(pgp->pg);
593 if (rw == OBD_BRW_WRITE)
594 page_debug_setup(addr, pgp->count,
597 page_debug_setup(addr, pgp->count,
604 err = obd_brw(rw, &conn, lsm, j, pga, ll_sync_brw_cb, brw_cbd);
606 CERROR("test_brw: error from obd_brw: err = %d\n", err);
609 for (j = 0, pgp = pga; j < pages; j++, pgp++) {
610 if (pgp->pg != NULL) {
611 if (verify && !err) {
612 void *addr = kmap(pgp->pg);
614 err = page_debug_check("test_brw",
620 __free_pages(pgp->pg, 0);
624 OBD_FREE(pga, pages * sizeof(*pga));
628 obd_data2conn(&conn, data);
630 err = obd_iocontrol(cmd, &conn, len, data, NULL);
634 err = copy_to_user((void *)arg, data, len);
644 } /* obd_class_ioctl */
648 /* declare character device */
649 static struct file_operations obd_psdev_fops = {
650 ioctl: obd_class_ioctl, /* ioctl */
651 open: obd_class_open, /* open */
652 release: obd_class_release, /* release */
656 #define OBD_MINOR 241
657 static struct miscdevice obd_psdev = {
663 void (*class_signal_connection_failure)(struct ptlrpc_connection *);
665 EXPORT_SYMBOL(obd_dev);
666 EXPORT_SYMBOL(obdo_cachep);
667 EXPORT_SYMBOL(obd_memory);
668 EXPORT_SYMBOL(obd_fail_loc);
669 EXPORT_SYMBOL(obd_timeout);
670 EXPORT_SYMBOL(obd_recovery_upcall);
671 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
673 EXPORT_SYMBOL(class_register_type);
674 EXPORT_SYMBOL(class_unregister_type);
675 EXPORT_SYMBOL(class_name2dev);
676 EXPORT_SYMBOL(class_uuid2dev);
677 EXPORT_SYMBOL(class_uuid2obd);
678 EXPORT_SYMBOL(class_new_export);
679 EXPORT_SYMBOL(class_destroy_export);
680 EXPORT_SYMBOL(class_connect);
681 EXPORT_SYMBOL(class_conn2export);
682 EXPORT_SYMBOL(class_conn2obd);
683 EXPORT_SYMBOL(class_conn2cliimp);
684 EXPORT_SYMBOL(class_conn2ldlmimp);
685 EXPORT_SYMBOL(class_disconnect);
686 EXPORT_SYMBOL(class_disconnect_all);
687 //EXPORT_SYMBOL(class_uuid_parse);
688 EXPORT_SYMBOL(class_uuid_unparse);
689 //EXPORT_SYMBOL(class_multi_setup);
690 //EXPORT_SYMBOL(class_multi_cleanup);
692 EXPORT_SYMBOL(class_signal_connection_failure);
693 EXPORT_SYMBOL(ll_sync_brw_cb);
694 EXPORT_SYMBOL(ll_init_brw_cb_data);
695 EXPORT_SYMBOL(class_nm_to_type);
697 static int __init init_obdclass(void)
699 struct obd_device *obd;
703 printk(KERN_INFO "OBD class driver v0.9, info@clusterfs.com\n");
705 sema_init(&obd_conf_sem, 1);
706 INIT_LIST_HEAD(&obd_types);
708 if ((err = misc_register(&obd_psdev))) {
709 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
713 /* This struct is already zerod for us (static global) */
714 for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
717 err = obd_init_caches();
723 err=lprocfs_reg_main();
728 static void __exit cleanup_obdclass(void)
733 misc_deregister(&obd_psdev);
734 for (i = 0; i < MAX_OBD_DEVICES; i++) {
735 struct obd_device *obd = &obd_dev[i];
736 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
737 OBT(obd) && OBP(obd, detach)) {
738 /* XXX should this call generic detach otherwise? */
739 OBP(obd, detach)(obd);
743 obd_cleanup_caches();
746 err = lprocfs_dereg_main();
748 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
752 /* Check that we're building against the appropriate version of the Lustre
754 #ifndef LUSTRE_KERNEL_VERSION
755 # define LUSTRE_KERNEL_VERSION 1
758 #if (LUSTRE_KERNEL_VERSION != 1)
759 # error Cannot continue: Your Lustre kernel patch is out of date
762 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
763 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
764 MODULE_LICENSE("GPL");
766 module_init(init_obdclass);
767 module_exit(cleanup_obdclass);