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;
111 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
112 cmd != OBD_IOC_LIST &&
113 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
114 CERROR("OBD ioctl: No device\n");
115 GOTO(out, err=-EINVAL);
117 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
118 CERROR("OBD ioctl: data error\n");
119 GOTO(out, err=-EINVAL);
121 data = (struct obd_ioctl_data *)buf;
125 GOTO(out, err=-EINVAL);
126 case OBD_IOC_DEVICE: {
127 CDEBUG(D_IOCTL, "\n");
128 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
129 CERROR("OBD ioctl: DEVICE insufficient devices\n");
130 GOTO(out, err=-EINVAL);
132 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
134 filp->private_data = &obd_dev[data->ioc_dev];
140 char *buf2 = data->ioc_bulk;
141 int remains = data->ioc_inllen1;
143 if (!data->ioc_inlbuf1) {
144 CERROR("No buffer passed!\n");
145 GOTO(out, err=-EINVAL);
149 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
152 struct obd_device *obd = &obd_dev[i];
155 if (obd->obd_flags & OBD_SET_UP)
159 l = snprintf(buf2, remains, "%2d %s %s %s %s\n",
160 i, status, obd->obd_type->typ_name,
161 obd->obd_name, obd->obd_uuid);
165 CERROR("not enough space for device listing\n");
170 err = copy_to_user((int *)arg, data, len);
175 case OBD_IOC_NAME2DEV: {
176 /* Resolve a device name. This does not change the
177 * currently selected device.
181 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
182 CERROR("No name passed,!\n");
183 GOTO(out, err=-EINVAL);
185 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
186 CERROR("Name not nul terminated!\n");
187 GOTO(out, err=-EINVAL);
190 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
191 dev = class_name2dev(data->ioc_inlbuf1);
194 CDEBUG(D_IOCTL, "No device for name %s!\n",
196 GOTO(out, err=-EINVAL);
199 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
201 err = copy_to_user((int *)arg, data, sizeof(*data));
205 case OBD_IOC_UUID2DEV: {
206 /* Resolve a device uuid. This does not change the
207 * currently selected device.
211 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
212 CERROR("No UUID passed!\n");
213 GOTO(out, err=-EINVAL);
215 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
216 CERROR("Name not nul terminated!\n");
217 GOTO(out, err=-EINVAL);
220 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
221 dev = class_uuid2dev(data->ioc_inlbuf1);
224 CDEBUG(D_IOCTL, "No device for name %s!\n",
226 GOTO(out, err=-EINVAL);
229 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
231 err = copy_to_user((int *)arg, data, sizeof(*data));
235 case OBD_IOC_NEWDEV: {
239 filp->private_data = NULL;
240 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
241 struct obd_device *obd = &obd_dev[i];
242 if (!obd->obd_type) {
243 filp->private_data = obd;
252 GOTO(out, err=-EINVAL);
254 err = copy_to_user((int *)arg, data, sizeof(*data));
258 case OBD_IOC_ATTACH: {
259 struct obd_type *type;
261 /* have we attached a type to this device */
262 if (obd->obd_flags & OBD_ATTACHED) {
263 CERROR("OBD: Device %d already typed as %s.\n",
264 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
265 GOTO(out, err=-EBUSY);
268 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
269 CERROR("No type passed!\n");
270 GOTO(out, err=-EINVAL);
272 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
273 CERROR("Type not nul terminated!\n");
274 GOTO(out, err=-EINVAL);
277 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
278 MKSTR(data->ioc_inlbuf1),
279 MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
282 type = class_nm_to_type(data->ioc_inlbuf1);
284 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
285 GOTO(out, err=-EINVAL);
288 obd->obd_type = type;
289 INIT_LIST_HEAD(&obd->obd_exports);
292 if (OBP(obd, attach))
293 err = OBP(obd,attach)(obd, sizeof(*data), data);
295 obd->obd_type = NULL;
297 obd->obd_flags |= OBD_ATTACHED;
299 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
300 obd->obd_minor, data->ioc_inlbuf1);
301 if (data->ioc_inlbuf2) {
302 int len = strlen(data->ioc_inlbuf2) + 1;
303 OBD_ALLOC(obd->obd_name, len);
304 if (!obd->obd_name) {
305 CERROR("no memory\n");
308 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
309 obd->obd_proc_entry =
310 proc_lustre_register_obd_device(obd);
312 CERROR("WARNING: unnamed obd device\n");
313 obd->obd_proc_entry = NULL;
316 if (data->ioc_inlbuf3) {
317 int len = strlen(data->ioc_inlbuf3);
319 CERROR("uuid should be shorter than 37 bytes\n");
321 OBD_FREE(obd->obd_name,
322 strlen(obd->obd_name) + 1);
323 GOTO(out, err=-EINVAL);
325 memcpy(obd->obd_uuid, data->ioc_inlbuf3,
326 sizeof(obd->obd_uuid));
335 case OBD_IOC_DETACH: {
337 if (obd->obd_flags & OBD_SET_UP) {
338 CERROR("OBD device %d still set up\n", obd->obd_minor);
339 GOTO(out, err=-EBUSY);
341 if (! (obd->obd_flags & OBD_ATTACHED) ) {
342 CERROR("OBD device %d not attached\n", obd->obd_minor);
343 GOTO(out, err=-ENODEV);
345 if ( !list_empty(&obd->obd_exports) ) {
346 CERROR("OBD device %d has exports\n",
348 GOTO(out, err=-EBUSY);
352 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
353 obd->obd_name = NULL;
356 if (obd->obd_proc_entry)
357 proc_lustre_release_obd_device(obd);
359 obd->obd_flags &= ~OBD_ATTACHED;
360 obd->obd_type->typ_refcnt--;
361 obd->obd_type = NULL;
366 case OBD_IOC_SETUP: {
367 /* have we attached a type to this device? */
368 if (!(obd->obd_flags & OBD_ATTACHED)) {
369 CERROR("Device %d not attached\n", obd->obd_minor);
370 GOTO(out, err=-ENODEV);
373 /* has this been done already? */
374 if ( obd->obd_flags & OBD_SET_UP ) {
375 CERROR("Device %d already setup (type %s)\n",
376 obd->obd_minor, obd->obd_type->typ_name);
377 GOTO(out, err=-EBUSY);
380 if ( OBT(obd) && OBP(obd, setup) )
381 err = obd_setup(obd, sizeof(*data), data);
384 obd->obd_type->typ_refcnt++;
385 obd->obd_flags |= OBD_SET_UP;
390 case OBD_IOC_CLEANUP: {
391 /* have we attached a type to this device? */
392 if (!(obd->obd_flags & OBD_ATTACHED)) {
393 CERROR("Device %d not attached\n", obd->obd_minor);
394 GOTO(out, err=-ENODEV);
397 if ( OBT(obd) && OBP(obd, cleanup) )
398 err = obd_cleanup(obd);
401 obd->obd_flags &= ~OBD_SET_UP;
402 obd->obd_type->typ_refcnt--;
407 case OBD_IOC_CONNECT: {
408 char * cluuid = "OBD_CLASS_UUID";
409 obd_data2conn(&conn, data);
411 err = obd_connect(&conn, obd, cluuid);
413 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
414 obd_conn2data(data, &conn);
418 err = copy_to_user((int *)arg, data, sizeof(*data));
422 case OBD_IOC_DISCONNECT: {
423 obd_data2conn(&conn, data);
424 err = obd_disconnect(&conn);
428 case OBD_IOC_DEC_USE_COUNT: {
433 case OBD_IOC_CREATE: {
434 struct lov_stripe_md *ea;
435 obd_data2conn(&conn, data);
438 err = obd_create(&conn, &data->ioc_obdo1, &ea);
442 err = copy_to_user((int *)arg, data, sizeof(*data));
446 case OBD_IOC_GETATTR: {
448 obd_data2conn(&conn, data);
449 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
453 err = copy_to_user((int *)arg, data, sizeof(*data));
457 case OBD_IOC_SETATTR: {
458 obd_data2conn(&conn, data);
459 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
463 err = copy_to_user((int *)arg, data, sizeof(*data));
467 case OBD_IOC_DESTROY: {
469 obd_data2conn(&conn, data);
471 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
475 err = copy_to_user((int *)arg, data, sizeof(*data));
479 case OBD_IOC_BRW_WRITE:
481 case OBD_IOC_BRW_READ: {
482 struct lov_stripe_md smd;
483 struct io_cb_data *cbd = ll_init_cb();
485 struct brw_page *pga, *pgp;
491 GOTO(out, err = -ENOMEM);
493 obd_data2conn(&conn, data);
495 pages = data->ioc_count / PAGE_SIZE;
497 CDEBUG(D_INODE, "BRW %s with %d pages\n",
498 rw == OBD_BRW_READ ? "read" : "write", pages);
499 OBD_ALLOC(pga, pages * sizeof(*pga));
501 CERROR("no memory for %d BRW per-page data\n", pages);
502 GOTO(brw_free, err = -ENOMEM);
505 memset(&smd, 0, sizeof(smd));
506 id = smd.lmd_object_id = data->ioc_obdo1.o_id;
508 off = data->ioc_offset;
510 for (j = 0, pgp = pga; j < pages; j++, off += PAGE_SIZE, pgp++){
511 pgp->pg = alloc_pages(GFP_KERNEL, 0);
513 CERROR("no memory for brw pages\n");
514 GOTO(brw_cleanup, err = -ENOMEM);
516 pgp->count = PAGE_SIZE;
520 if (rw == OBD_BRW_WRITE) {
521 page_debug_setup(kmap(pgp->pg), pgp->count,
527 err = obd_brw(rw, &conn, &smd, j, pga, ll_sync_io_cb, cbd);
530 for (j = 0, pgp = pga; j < pages; j++) {
531 if (pgp->pg != NULL) {
533 void *addr = kmap(pgp->pg);
535 err2 = page_debug_check("test_brw", addr,
536 PAGE_SIZE, pgp->off,id);
538 __free_pages(pgp->pg, 0);
544 OBD_FREE(pga, pages * sizeof(*pga));
548 obd_data2conn(&conn, data);
550 err = obd_iocontrol(cmd, &conn, len, data, NULL);
554 err = copy_to_user((int *)arg, data, len);
563 } /* obd_class_ioctl */
567 /* declare character device */
568 static struct file_operations obd_psdev_fops = {
569 ioctl: obd_class_ioctl, /* ioctl */
570 open: obd_class_open, /* open */
571 release: obd_class_release, /* release */
575 #define OBD_MINOR 241
576 static struct miscdevice obd_psdev = {
582 void (*class_signal_client_failure)(struct ptlrpc_client *);
584 EXPORT_SYMBOL(obd_dev);
585 EXPORT_SYMBOL(obdo_cachep);
586 EXPORT_SYMBOL(obd_memory);
587 EXPORT_SYMBOL(obd_fail_loc);
588 EXPORT_SYMBOL(obd_timeout);
589 EXPORT_SYMBOL(obd_recovery_upcall);
591 EXPORT_SYMBOL(class_register_type);
592 EXPORT_SYMBOL(class_unregister_type);
593 EXPORT_SYMBOL(class_name2dev);
594 EXPORT_SYMBOL(class_uuid2dev);
595 EXPORT_SYMBOL(class_uuid2obd);
596 EXPORT_SYMBOL(class_new_export);
597 EXPORT_SYMBOL(class_connect);
598 EXPORT_SYMBOL(class_conn2export);
599 EXPORT_SYMBOL(class_rconn2export);
600 EXPORT_SYMBOL(class_conn2obd);
601 EXPORT_SYMBOL(class_disconnect);
602 EXPORT_SYMBOL(class_disconnect_all);
603 EXPORT_SYMBOL(class_uuid_parse);
604 EXPORT_SYMBOL(class_uuid_unparse);
605 //EXPORT_SYMBOL(class_multi_setup);
606 //EXPORT_SYMBOL(class_multi_cleanup);
608 EXPORT_SYMBOL(class_signal_client_failure);
610 static int __init init_obdclass(void)
615 printk(KERN_INFO "OBD class driver v0.9, info@clusterfs.com\n");
617 sema_init(&obd_conf_sem, 1);
618 INIT_LIST_HEAD(&obd_types);
620 if ((err = misc_register(&obd_psdev))) {
621 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
625 for (i = 0; i < MAX_OBD_DEVICES; i++) {
626 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
627 obd_dev[i].obd_minor = i;
628 INIT_LIST_HEAD(&obd_dev[i].obd_exports);
631 err = obd_init_caches();
638 static void __exit cleanup_obdclass(void)
643 misc_deregister(&obd_psdev);
644 for (i = 0; i < MAX_OBD_DEVICES; i++) {
645 struct obd_device *obd = &obd_dev[i];
646 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
647 OBT(obd) && OBP(obd, detach)) {
648 /* XXX should this call generic detach otherwise? */
649 OBP(obd, detach)(obd);
653 obd_cleanup_caches();
655 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
659 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
660 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
661 MODULE_LICENSE("GPL");
663 module_init(init_obdclass);
664 module_exit(cleanup_obdclass);