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/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/miscdevice.h>
39 #define DEBUG_SUBSYSTEM S_CLASS
41 #include <linux/obd_support.h>
42 #include <linux/obd_class.h>
43 #include <linux/lustre_debug.h>
44 #include <linux/smp_lock.h>
46 struct semaphore obd_conf_sem; /* serialize configuration commands */
47 struct obd_device obd_dev[MAX_OBD_DEVICES];
48 struct list_head obd_types;
49 unsigned long obd_memory = 0;
51 /* The following are visible and mutable through /proc/sys/lustre/. */
52 unsigned long obd_fail_loc = 0;
53 unsigned long obd_timeout = 100;
54 char obd_recovery_upcall[128] = "/usr/lib/lustre/ha_assist";
56 extern struct obd_type *class_nm_to_type(char *nm);
58 /* opening /dev/obd */
59 static int obd_class_open(struct inode * inode, struct file * file)
63 file->private_data = NULL;
68 /* closing /dev/obd */
69 static int obd_class_release(struct inode * inode, struct file * file)
73 if (file->private_data)
74 file->private_data = NULL;
81 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
83 conn->addr = data->ioc_addr;
84 conn->cookie = data->ioc_cookie;
88 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
90 data->ioc_addr = conn->addr;
91 data->ioc_cookie = conn->cookie;
95 /* to control /dev/obd */
96 static int obd_class_ioctl (struct inode * inode, struct file * filp,
97 unsigned int cmd, unsigned long arg)
101 struct obd_ioctl_data *data;
102 struct obd_device *obd = filp->private_data;
104 struct lustre_handle conn;
105 int rw = OBD_BRW_READ;
112 case OBD_IOC_BRW_WRITE:
113 case OBD_IOC_BRW_READ:
114 case OBD_IOC_GETATTR:
122 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
123 cmd != OBD_IOC_LIST &&
124 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
125 CERROR("OBD ioctl: No device\n");
126 GOTO(out, err=-EINVAL);
128 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
129 CERROR("OBD ioctl: data error\n");
130 GOTO(out, err=-EINVAL);
132 data = (struct obd_ioctl_data *)buf;
136 GOTO(out, err=-EINVAL);
137 case OBD_IOC_DEVICE: {
138 CDEBUG(D_IOCTL, "\n");
139 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
140 CERROR("OBD ioctl: DEVICE insufficient devices\n");
141 GOTO(out, err=-EINVAL);
143 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
145 filp->private_data = &obd_dev[data->ioc_dev];
151 char *buf2 = data->ioc_bulk;
152 int remains = data->ioc_inllen1;
154 if (!data->ioc_inlbuf1) {
155 CERROR("No buffer passed!\n");
156 GOTO(out, err=-EINVAL);
160 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
163 struct obd_device *obd = &obd_dev[i];
166 if (obd->obd_flags & OBD_SET_UP)
168 else if (obd->obd_flags & OBD_ATTACHED)
172 l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
173 i, status, obd->obd_type->typ_name,
174 obd->obd_name, obd->obd_uuid, obd->obd_type->typ_refcnt);
178 CERROR("not enough space for device listing\n");
183 err = copy_to_user((int *)arg, data, len);
188 case OBD_IOC_NAME2DEV: {
189 /* Resolve a device name. This does not change the
190 * currently selected device.
194 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
195 CERROR("No name passed,!\n");
196 GOTO(out, err=-EINVAL);
198 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
199 CERROR("Name not nul terminated!\n");
200 GOTO(out, err=-EINVAL);
203 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
204 dev = class_name2dev(data->ioc_inlbuf1);
207 CDEBUG(D_IOCTL, "No device for name %s!\n",
209 GOTO(out, err=-EINVAL);
212 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
214 err = copy_to_user((int *)arg, data, sizeof(*data));
218 case OBD_IOC_UUID2DEV: {
219 /* Resolve a device uuid. This does not change the
220 * currently selected device.
224 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
225 CERROR("No UUID 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_uuid2dev(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((int *)arg, data, sizeof(*data));
248 case OBD_IOC_NEWDEV: {
252 filp->private_data = NULL;
253 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
254 struct obd_device *obd = &obd_dev[i];
255 if (!obd->obd_type) {
256 filp->private_data = obd;
265 GOTO(out, err=-EINVAL);
267 err = copy_to_user((int *)arg, data, sizeof(*data));
271 case OBD_IOC_ATTACH: {
272 struct obd_type *type;
274 /* have we attached a type to this device */
275 if (obd->obd_flags & OBD_ATTACHED) {
276 CERROR("OBD: Device %d already typed as %s.\n",
277 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
278 GOTO(out, err=-EBUSY);
281 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
282 CERROR("No type passed!\n");
283 GOTO(out, err=-EINVAL);
285 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
286 CERROR("Type not nul terminated!\n");
287 GOTO(out, err=-EINVAL);
290 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
291 MKSTR(data->ioc_inlbuf1),
292 MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
295 type = class_nm_to_type(data->ioc_inlbuf1);
297 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
298 GOTO(out, err=-EINVAL);
301 obd->obd_type = type;
302 INIT_LIST_HEAD(&obd->obd_exports);
305 if (OBP(obd, attach))
306 err = OBP(obd,attach)(obd, sizeof(*data), data);
308 obd->obd_type = NULL;
310 obd->obd_flags |= OBD_ATTACHED;
312 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
313 obd->obd_minor, data->ioc_inlbuf1);
314 if (data->ioc_inlbuf2) {
315 int len = strlen(data->ioc_inlbuf2) + 1;
316 OBD_ALLOC(obd->obd_name, len);
317 if (!obd->obd_name) {
318 CERROR("no memory\n");
321 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
322 obd->obd_proc_entry =
323 proc_lustre_register_obd_device(obd);
325 CERROR("WARNING: unnamed obd device\n");
326 obd->obd_proc_entry = NULL;
329 if (data->ioc_inlbuf3) {
330 int len = strlen(data->ioc_inlbuf3);
332 CERROR("uuid should be shorter than 37 bytes\n");
334 OBD_FREE(obd->obd_name,
335 strlen(obd->obd_name) + 1);
336 GOTO(out, err=-EINVAL);
338 memcpy(obd->obd_uuid, data->ioc_inlbuf3,
339 sizeof(obd->obd_uuid));
348 case OBD_IOC_DETACH: {
350 if (obd->obd_flags & OBD_SET_UP) {
351 CERROR("OBD device %d still set up\n", obd->obd_minor);
352 GOTO(out, err=-EBUSY);
354 if (!(obd->obd_flags & OBD_ATTACHED) ) {
355 CERROR("OBD device %d not attached\n", obd->obd_minor);
356 GOTO(out, err=-ENODEV);
358 #warning FIXME: Mike, we probably need some sort of "force detach" here
359 if (!list_empty(&obd->obd_exports) ) {
360 CERROR("OBD device %d (%p) has exports\n",
361 obd->obd_minor, obd);
362 GOTO(out, err=-EBUSY);
366 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
367 obd->obd_name = NULL;
370 if (obd->obd_proc_entry)
371 proc_lustre_release_obd_device(obd);
373 obd->obd_flags &= ~OBD_ATTACHED;
374 obd->obd_type->typ_refcnt--;
375 obd->obd_type = NULL;
380 case OBD_IOC_SETUP: {
381 /* have we attached a type to this device? */
382 if (!(obd->obd_flags & OBD_ATTACHED)) {
383 CERROR("Device %d not attached\n", obd->obd_minor);
384 GOTO(out, err=-ENODEV);
387 /* has this been done already? */
388 if ( obd->obd_flags & OBD_SET_UP ) {
389 CERROR("Device %d already setup (type %s)\n",
390 obd->obd_minor, obd->obd_type->typ_name);
391 GOTO(out, err=-EBUSY);
394 if ( OBT(obd) && OBP(obd, setup) )
395 err = obd_setup(obd, sizeof(*data), data);
398 obd->obd_type->typ_refcnt++;
399 obd->obd_flags |= OBD_SET_UP;
404 case OBD_IOC_CLEANUP: {
405 /* have we attached a type to this device? */
406 if (!(obd->obd_flags & OBD_ATTACHED)) {
407 CERROR("Device %d not attached\n", obd->obd_minor);
408 GOTO(out, err=-ENODEV);
411 if ( OBT(obd) && OBP(obd, cleanup) )
412 err = obd_cleanup(obd);
415 obd->obd_flags &= ~OBD_SET_UP;
416 obd->obd_type->typ_refcnt--;
421 case OBD_IOC_CONNECT: {
422 char * cluuid = "OBD_CLASS_UUID";
423 obd_data2conn(&conn, data);
425 err = obd_connect(&conn, obd, cluuid);
427 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
428 obd_conn2data(data, &conn);
432 err = copy_to_user((int *)arg, data, sizeof(*data));
436 case OBD_IOC_DISCONNECT: {
437 obd_data2conn(&conn, data);
438 err = obd_disconnect(&conn);
442 case OBD_IOC_DEC_USE_COUNT: {
447 case OBD_IOC_CREATE: {
448 struct lov_stripe_md *ea;
449 obd_data2conn(&conn, data);
452 err = obd_create(&conn, &data->ioc_obdo1, &ea);
456 err = copy_to_user((int *)arg, data, sizeof(*data));
460 case OBD_IOC_GETATTR: {
462 obd_data2conn(&conn, data);
463 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
467 err = copy_to_user((int *)arg, data, sizeof(*data));
471 case OBD_IOC_SETATTR: {
472 obd_data2conn(&conn, data);
473 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
477 err = copy_to_user((int *)arg, data, sizeof(*data));
481 case OBD_IOC_DESTROY: {
483 obd_data2conn(&conn, data);
485 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
489 err = copy_to_user((int *)arg, data, sizeof(*data));
493 case OBD_IOC_BRW_WRITE:
495 case OBD_IOC_BRW_READ: {
496 struct lov_stripe_md smd;
497 struct io_cb_data *cbd = ll_init_cb();
499 struct brw_page *pga, *pgp;
500 __u64 id = data->ioc_obdo1.o_id;
501 int gfp_mask = (id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
502 int verify = (id != 0);
507 GOTO(out, err = -ENOMEM);
509 obd_data2conn(&conn, data);
511 pages = data->ioc_count / PAGE_SIZE;
513 CDEBUG(D_INODE, "BRW %s with %d pages\n",
514 rw == OBD_BRW_READ ? "read" : "write", pages);
515 OBD_ALLOC(pga, pages * sizeof(*pga));
517 CERROR("no memory for %d BRW per-page data\n", pages);
518 GOTO(brw_free, err = -ENOMEM);
521 memset(&smd, 0, sizeof(smd));
522 smd.lmd_object_id = id;
524 off = data->ioc_offset;
526 for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
527 pgp->pg = alloc_pages(gfp_mask, 0);
529 CERROR("no memory for brw pages\n");
530 GOTO(brw_cleanup, err = -ENOMEM);
532 pgp->count = PAGE_SIZE;
537 void *addr = kmap(pgp->pg);
539 if (rw == OBD_BRW_WRITE)
540 page_debug_setup(addr, pgp->count,
543 page_debug_setup(addr, pgp->count,
550 err = obd_brw(rw, &conn, &smd, j, pga, ll_sync_io_cb, cbd);
552 CERROR("test_brw: error from obd_brw: err = %d\n", err);
555 for (j = 0, pgp = pga; j < pages; j++, pgp++) {
556 if (pgp->pg != NULL) {
557 if (verify && !err) {
558 void *addr = kmap(pgp->pg);
560 err = page_debug_check("test_brw",
566 __free_pages(pgp->pg, 0);
570 OBD_FREE(pga, pages * sizeof(*pga));
574 obd_data2conn(&conn, data);
576 err = obd_iocontrol(cmd, &conn, len, data, NULL);
580 err = copy_to_user((int *)arg, data, len);
590 } /* obd_class_ioctl */
594 /* declare character device */
595 static struct file_operations obd_psdev_fops = {
596 ioctl: obd_class_ioctl, /* ioctl */
597 open: obd_class_open, /* open */
598 release: obd_class_release, /* release */
602 #define OBD_MINOR 241
603 static struct miscdevice obd_psdev = {
609 void (*class_signal_connection_failure)(struct ptlrpc_connection *);
610 int (*mds_destroy_export)(struct obd_export *exp);
611 int (*ldlm_destroy_export)(struct obd_export *exp);
613 EXPORT_SYMBOL(obd_dev);
614 EXPORT_SYMBOL(obdo_cachep);
615 EXPORT_SYMBOL(obd_memory);
616 EXPORT_SYMBOL(obd_fail_loc);
617 EXPORT_SYMBOL(obd_timeout);
618 EXPORT_SYMBOL(obd_recovery_upcall);
620 EXPORT_SYMBOL(class_register_type);
621 EXPORT_SYMBOL(class_unregister_type);
622 EXPORT_SYMBOL(class_name2dev);
623 EXPORT_SYMBOL(class_uuid2dev);
624 EXPORT_SYMBOL(class_uuid2obd);
625 EXPORT_SYMBOL(class_new_export);
626 EXPORT_SYMBOL(class_destroy_export);
627 EXPORT_SYMBOL(class_connect);
628 EXPORT_SYMBOL(class_conn2export);
629 EXPORT_SYMBOL(class_conn2obd);
630 EXPORT_SYMBOL(class_conn2cliimp);
631 EXPORT_SYMBOL(class_conn2ldlmimp);
632 EXPORT_SYMBOL(class_disconnect);
633 EXPORT_SYMBOL(class_disconnect_all);
634 //EXPORT_SYMBOL(class_uuid_parse);
635 EXPORT_SYMBOL(class_uuid_unparse);
636 //EXPORT_SYMBOL(class_multi_setup);
637 //EXPORT_SYMBOL(class_multi_cleanup);
639 EXPORT_SYMBOL(class_signal_connection_failure);
640 EXPORT_SYMBOL(mds_destroy_export);
641 EXPORT_SYMBOL(ldlm_destroy_export);
643 static int __init init_obdclass(void)
648 printk(KERN_INFO "OBD class driver v0.9, info@clusterfs.com\n");
650 sema_init(&obd_conf_sem, 1);
651 INIT_LIST_HEAD(&obd_types);
653 if ((err = misc_register(&obd_psdev))) {
654 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
658 for (i = 0; i < MAX_OBD_DEVICES; i++) {
659 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
660 obd_dev[i].obd_minor = i;
661 INIT_LIST_HEAD(&obd_dev[i].obd_exports);
664 err = obd_init_caches();
671 static void __exit cleanup_obdclass(void)
676 misc_deregister(&obd_psdev);
677 for (i = 0; i < MAX_OBD_DEVICES; i++) {
678 struct obd_device *obd = &obd_dev[i];
679 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
680 OBT(obd) && OBP(obd, detach)) {
681 /* XXX should this call generic detach otherwise? */
682 OBP(obd, detach)(obd);
686 obd_cleanup_caches();
688 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
692 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
693 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
694 MODULE_LICENSE("GPL");
696 module_init(init_obdclass);
697 module_exit(cleanup_obdclass);