1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Object Devices Class Driver
6 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * These are the only exported functions, they provide some generic
24 * infrastructure for managing object devices
27 #define DEBUG_SUBSYSTEM S_CLASS
29 # define EXPORT_SYMTAB
32 #include <linux/config.h> /* for CONFIG_PROC_FS */
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/ioport.h>
41 #include <linux/fcntl.h>
42 #include <linux/delay.h>
43 #include <linux/skbuff.h>
44 #include <linux/proc_fs.h>
46 #include <linux/poll.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <linux/highmem.h>
51 #include <asm/ioctls.h>
52 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/miscdevice.h>
56 #include <linux/smp_lock.h>
57 #include <linux/seq_file.h>
59 # include <liblustre.h>
62 #include <linux/obd_support.h>
63 #include <linux/obd_class.h>
64 #include <linux/lustre_debug.h>
65 #include <linux/lprocfs_status.h>
66 #include <linux/lustre_build_version.h>
67 #include <portals/list.h>
68 #include "llog_internal.h"
70 struct semaphore obd_conf_sem; /* serialize configuration commands */
71 struct obd_device obd_dev[MAX_OBD_DEVICES];
72 struct list_head obd_types;
78 /* The following are visible and mutable through /proc/sys/lustre/. */
79 unsigned int obd_fail_loc;
80 unsigned int obd_timeout = 100;
81 char obd_lustre_upcall[128] = "/usr/lib/lustre/lustre_upcall";
82 unsigned int obd_sync_filter; /* = 0, don't sync by default */
85 /* opening /dev/obd */
86 static int obd_class_open(struct inode * inode, struct file * file)
88 struct obd_class_user_state *ocus;
91 OBD_ALLOC(ocus, sizeof(*ocus));
95 INIT_LIST_HEAD(&ocus->ocus_conns);
96 file->private_data = ocus;
102 /* closing /dev/obd */
103 static int obd_class_release(struct inode * inode, struct file * file)
105 struct obd_class_user_state *ocus = file->private_data;
106 struct obd_class_user_conn *c;
109 while (!list_empty (&ocus->ocus_conns)) {
110 c = list_entry (ocus->ocus_conns.next,
111 struct obd_class_user_conn, ocuc_chain);
112 list_del (&c->ocuc_chain);
114 CDEBUG (D_IOCTL, "Auto-disconnect exp %p\n", c->ocuc_exp);
116 down (&obd_conf_sem);
117 obd_disconnect (c->ocuc_exp, 0);
120 OBD_FREE (c, sizeof (*c));
123 OBD_FREE (ocus, sizeof (*ocus));
130 static int obd_class_add_user_exp(struct obd_class_user_state *ocus,
131 struct obd_export *exp)
133 struct obd_class_user_conn *c;
135 /* NB holding obd_conf_sem */
137 OBD_ALLOC (c, sizeof (*c));
141 c->ocuc_exp = class_export_get(exp);
142 list_add (&c->ocuc_chain, &ocus->ocus_conns);
146 static void obd_class_remove_user_exp(struct obd_class_user_state *ocus,
147 struct obd_export *exp)
149 struct obd_class_user_conn *c;
152 /* NB holding obd_conf_sem or last reference */
154 list_for_each_entry(c, &ocus->ocus_conns, ocuc_chain) {
155 if (exp == c->ocuc_exp) {
156 list_del (&c->ocuc_chain);
157 OBD_FREE (c, sizeof (*c));
158 class_export_put(exp);
166 static inline void obd_data2conn(struct lustre_handle *conn,
167 struct obd_ioctl_data *data)
169 memset(conn, 0, sizeof *conn);
170 conn->cookie = data->ioc_cookie;
173 static inline void obd_conn2data(struct obd_ioctl_data *data,
174 struct lustre_handle *conn)
176 data->ioc_cookie = conn->cookie;
179 int class_resolve_dev_name(uint32_t len, char *name)
185 CERROR("No name passed,!\n");
186 GOTO(out, rc = -EINVAL);
188 if (name[len - 1] != 0) {
189 CERROR("Name not nul terminated!\n");
190 GOTO(out, rc = -EINVAL);
193 CDEBUG(D_IOCTL, "device name %s\n", name);
194 dev = class_name2dev(name);
196 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
197 GOTO(out, rc = -EINVAL);
200 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
207 int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd,
211 struct obd_ioctl_data *data;
212 struct portals_debug_ioctl_data *debug_data;
213 struct obd_device *obd = ocus->ocus_current_obd;
214 struct lustre_handle conn;
215 int err = 0, len = 0, serialised = 0;
218 if (current->fsuid != 0)
219 RETURN(err = -EACCES);
221 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
222 RETURN(err = -ENOTTY);
224 /* only for debugging */
225 if (cmd == PTL_IOC_DEBUG_MASK) {
226 debug_data = (struct portals_debug_ioctl_data*)arg;
227 portal_subsystem_debug = debug_data->subs;
228 portal_debug = debug_data->debug;
233 case OBD_IOC_BRW_WRITE:
234 case OBD_IOC_BRW_READ:
235 case OBD_IOC_GETATTR:
236 case ECHO_IOC_ENQUEUE:
237 case ECHO_IOC_CANCEL:
238 case OBD_IOC_CLIENT_RECOVER:
239 case OBD_IOC_CATLOGLIST:
240 case OBD_IOC_LLOG_INFO:
241 case OBD_IOC_LLOG_PRINT:
242 case OBD_IOC_LLOG_CANCEL:
243 case OBD_IOC_LLOG_REMOVE:
251 CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
252 if (!obd && cmd != OBD_IOC_DEVICE &&
253 cmd != OBD_IOC_LIST && cmd != OBD_GET_VERSION &&
254 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_UUID2DEV &&
255 cmd != OBD_IOC_PROCESS_CFG) {
256 CERROR("OBD ioctl: No device\n");
257 GOTO(out, err = -EINVAL);
259 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
260 CERROR("OBD ioctl: data error\n");
261 GOTO(out, err = -EINVAL);
263 data = (struct obd_ioctl_data *)buf;
266 case OBD_IOC_DEVICE: {
267 CDEBUG(D_IOCTL, "\n");
268 if (data->ioc_dev >= MAX_OBD_DEVICES) {
269 CERROR("OBD ioctl: DEVICE invalid device %d\n",
271 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
272 GOTO(out, err = -EINVAL);
274 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
275 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
279 case OBD_IOC_PROCESS_CFG: {
281 struct lustre_cfg *lcfg;
283 /* FIXME hack to liblustre dump, remove when switch
286 data->ioc_pbuf1 = data->ioc_inlbuf1;
287 data->ioc_plen1 = data->ioc_inllen1;
289 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
290 CERROR("No config buffer passed!\n");
291 GOTO(out, err = -EINVAL);
293 err = lustre_cfg_getdata(&buf, data->ioc_plen1,
297 lcfg = (struct lustre_cfg* ) buf;
299 err = class_process_config(lcfg);
300 lustre_cfg_freedata(buf, data->ioc_plen1);
306 char *buf2 = data->ioc_bulk;
307 int remains = data->ioc_inllen1;
309 if (!data->ioc_inlbuf1) {
310 CERROR("No buffer passed!\n");
311 GOTO(out, err = -EINVAL);
315 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
318 struct obd_device *obd = &obd_dev[i];
322 if (obd->obd_stopping)
324 else if (obd->obd_set_up)
326 else if (obd->obd_attached)
330 l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
331 i, status, obd->obd_type->typ_name,
332 obd->obd_name, obd->obd_uuid.uuid,
333 obd->obd_type->typ_refcnt);
337 CERROR("not enough space for device listing\n");
342 err = copy_to_user((void *)arg, data, len);
348 case OBD_GET_VERSION:
349 if (!data->ioc_inlbuf1) {
350 CERROR("No buffer passed in ioctl\n");
351 GOTO(out, err = -EINVAL);
354 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
355 CERROR("ioctl buffer too small to hold version\n");
356 GOTO(out, err = -EINVAL);
359 memcpy(data->ioc_bulk, BUILD_VERSION,
360 strlen(BUILD_VERSION) + 1);
362 err = copy_to_user((void *)arg, data, len);
367 case OBD_IOC_NAME2DEV: {
368 /* Resolve a device name. This does not change the
369 * currently selected device.
373 dev = class_resolve_dev_name(data->ioc_inllen1,
377 GOTO(out, err = -EINVAL);
379 err = copy_to_user((void *)arg, data, sizeof(*data));
385 case OBD_IOC_UUID2DEV: {
386 /* Resolve a device uuid. This does not change the
387 * currently selected device.
390 struct obd_uuid uuid;
392 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
393 CERROR("No UUID passed!\n");
394 GOTO(out, err = -EINVAL);
396 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
397 CERROR("UUID not NUL terminated!\n");
398 GOTO(out, err = -EINVAL);
401 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
402 obd_str2uuid(&uuid, data->ioc_inlbuf1);
403 dev = class_uuid2dev(&uuid);
406 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
408 GOTO(out, err = -EINVAL);
411 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
413 err = copy_to_user((void *)arg, data, sizeof(*data));
421 case OBD_IOC_CONNECT: {
422 struct obd_export *exp;
423 obd_data2conn(&conn, data);
425 err = obd_connect(&conn, obd, &obd->obd_uuid);
427 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.cookie);
428 obd_conn2data(data, &conn);
432 exp = class_conn2export(&conn);
434 GOTO(out, err = -EINVAL);
436 err = obd_class_add_user_exp(ocus, exp);
438 obd_disconnect(exp, 0);
442 err = copy_to_user((void *)arg, data, sizeof(*data));
444 obd_class_remove_user_exp(ocus, exp);
445 obd_disconnect(exp, 0);
446 GOTO (out, err = -EFAULT);
448 class_export_put(exp);
452 case OBD_IOC_DISCONNECT: {
453 struct obd_export *exp;
454 obd_data2conn(&conn, data);
455 exp = class_conn2export(&conn);
457 GOTO(out, err = -EINVAL);
459 obd_class_remove_user_exp(ocus, exp);
460 err = obd_disconnect(exp, 0);
464 case OBD_IOC_NO_TRANSNO: {
465 if (!obd->obd_attached) {
466 CERROR("Device %d not attached\n", obd->obd_minor);
467 GOTO(out, err = -ENODEV);
470 "disabling committed-transno notifications on %d\n",
472 obd->obd_no_transno = 1;
476 case OBD_IOC_CLOSE_UUID: {
477 struct lustre_peer peer;
478 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
480 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer);
485 // obd_data2conn(&conn, data);
486 struct obd_class_user_conn *oconn;
488 if (list_empty(&ocus->ocus_conns))
489 GOTO(out, err = -ENOTCONN);
491 oconn = list_entry(ocus->ocus_conns.next, struct obd_class_user_conn, ocuc_chain);
492 err = obd_iocontrol(cmd, oconn->ocuc_exp, len, data, NULL);
496 err = copy_to_user((void *)arg, data, len);
505 obd_ioctl_freedata(buf, len);
509 } /* class_handle_ioctl */
513 #define OBD_MINOR 241
515 /* to control /dev/obd */
516 static int obd_class_ioctl(struct inode *inode, struct file *filp,
517 unsigned int cmd, unsigned long arg)
519 return class_handle_ioctl(filp->private_data, cmd, arg);
522 /* declare character device */
523 static struct file_operations obd_psdev_fops = {
524 ioctl: obd_class_ioctl, /* ioctl */
525 open: obd_class_open, /* open */
526 release: obd_class_release, /* release */
530 static struct miscdevice obd_psdev = {
536 void *obd_psdev = NULL;
539 EXPORT_SYMBOL(obd_dev);
540 EXPORT_SYMBOL(obdo_cachep);
541 EXPORT_SYMBOL(obd_memory);
542 EXPORT_SYMBOL(obd_memmax);
543 EXPORT_SYMBOL(obd_fail_loc);
544 EXPORT_SYMBOL(obd_timeout);
545 EXPORT_SYMBOL(obd_lustre_upcall);
546 EXPORT_SYMBOL(obd_sync_filter);
547 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
548 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
549 EXPORT_SYMBOL(proc_lustre_root);
551 EXPORT_SYMBOL(class_register_type);
552 EXPORT_SYMBOL(class_unregister_type);
553 EXPORT_SYMBOL(class_get_type);
554 EXPORT_SYMBOL(class_put_type);
555 EXPORT_SYMBOL(class_name2dev);
556 EXPORT_SYMBOL(class_name2obd);
557 EXPORT_SYMBOL(class_uuid2dev);
558 EXPORT_SYMBOL(class_uuid2obd);
559 EXPORT_SYMBOL(class_find_client_obd);
560 EXPORT_SYMBOL(__class_export_put);
561 EXPORT_SYMBOL(class_new_export);
562 EXPORT_SYMBOL(class_unlink_export);
563 EXPORT_SYMBOL(class_import_get);
564 EXPORT_SYMBOL(class_import_put);
565 EXPORT_SYMBOL(class_new_import);
566 EXPORT_SYMBOL(class_destroy_import);
567 EXPORT_SYMBOL(class_connect);
568 EXPORT_SYMBOL(class_conn2export);
569 EXPORT_SYMBOL(class_exp2obd);
570 EXPORT_SYMBOL(class_conn2obd);
571 EXPORT_SYMBOL(class_exp2cliimp);
572 EXPORT_SYMBOL(class_conn2cliimp);
573 EXPORT_SYMBOL(class_disconnect);
574 EXPORT_SYMBOL(class_disconnect_exports);
576 EXPORT_SYMBOL(osic_init);
577 EXPORT_SYMBOL(osic_add_one);
578 EXPORT_SYMBOL(osic_wait);
579 EXPORT_SYMBOL(osic_complete_one);
582 EXPORT_SYMBOL(class_uuid_unparse);
583 EXPORT_SYMBOL(lustre_uuid_to_peer);
585 EXPORT_SYMBOL(class_handle_hash);
586 EXPORT_SYMBOL(class_handle_unhash);
587 EXPORT_SYMBOL(class_handle2object);
590 EXPORT_SYMBOL(class_get_profile);
591 EXPORT_SYMBOL(class_process_config);
592 EXPORT_SYMBOL(class_config_parse_llog);
593 EXPORT_SYMBOL(class_config_dump_llog);
594 EXPORT_SYMBOL(class_attach);
595 EXPORT_SYMBOL(class_setup);
596 EXPORT_SYMBOL(class_cleanup);
597 EXPORT_SYMBOL(class_detach);
600 int obd_proc_read_version(char *page, char **start, off_t off, int count,
601 int *eof, void *data)
604 return snprintf(page, count, "%s\n", BUILD_VERSION);
607 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
608 int *eof, void *data)
611 return snprintf(page, count, "%s\n",
620 /* Root for /proc/fs/lustre */
621 struct proc_dir_entry *proc_lustre_root = NULL;
622 struct lprocfs_vars lprocfs_base[] = {
623 { "version", obd_proc_read_version, NULL, NULL },
624 { "pinger", obd_proc_read_pinger, NULL, NULL },
628 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
630 if (*pos >= MAX_OBD_DEVICES)
632 return &obd_dev[*pos];
635 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
639 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
642 if (*pos >= MAX_OBD_DEVICES)
644 return &obd_dev[*pos];
647 static int obd_device_list_seq_show(struct seq_file *p, void *v)
649 struct obd_device *obd = (struct obd_device *)v;
650 int index = obd - &obd_dev[0];
655 if (obd->obd_stopping)
657 else if (obd->obd_set_up)
659 else if (obd->obd_attached)
664 return seq_printf(p, "%3d %s %s %s %s %d\n",
665 (int)index, status, obd->obd_type->typ_name,
666 obd->obd_name, obd->obd_uuid.uuid,
667 atomic_read(&obd->obd_refcount));
670 struct seq_operations obd_device_list_sops = {
671 .start = obd_device_list_seq_start,
672 .stop = obd_device_list_seq_stop,
673 .next = obd_device_list_seq_next,
674 .show = obd_device_list_seq_show,
677 static int obd_device_list_open(struct inode *inode, struct file *file)
679 struct proc_dir_entry *dp = inode->u.generic_ip;
680 struct seq_file *seq;
681 int rc = seq_open(file, &obd_device_list_sops);
686 seq = file->private_data;
687 seq->private = dp->data;
692 struct file_operations obd_device_list_fops = {
693 .open = obd_device_list_open,
696 .release = seq_release,
701 static int __init init_obdclass(void)
703 int init_obdclass(void)
706 struct obd_device *obd;
708 struct proc_dir_entry *entry;
713 printk(KERN_INFO "Lustre: OBD class driver Build Version: "
714 BUILD_VERSION", info@clusterfs.com\n");
716 class_init_uuidlist();
717 err = class_handle_init();
721 sema_init(&obd_conf_sem, 1);
722 INIT_LIST_HEAD(&obd_types);
724 err = misc_register(&obd_psdev);
726 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
730 /* This struct is already zerod for us (static global) */
731 for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
734 err = obd_init_caches();
743 proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
744 if (!proc_lustre_root) {
746 "LustreError: error registering /proc/fs/lustre\n");
749 proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
750 entry = create_proc_entry("devices", 0444, proc_lustre_root);
752 printk(KERN_ERR "LustreError: error registering "
753 "/proc/fs/lustre/devices\n");
754 lprocfs_remove(proc_lustre_root);
757 entry->proc_fops = &obd_device_list_fops;
763 static void /*__exit*/ cleanup_obdclass(void)
765 static void cleanup_obdclass(void)
771 misc_deregister(&obd_psdev);
772 for (i = 0; i < MAX_OBD_DEVICES; i++) {
773 struct obd_device *obd = &obd_dev[i];
774 if (obd->obd_type && obd->obd_set_up &&
775 OBT(obd) && OBP(obd, detach)) {
776 /* XXX should this call generic detach otherwise? */
777 OBP(obd, detach)(obd);
781 obd_cleanup_caches();
786 if (proc_lustre_root) {
787 lprocfs_remove(proc_lustre_root);
788 proc_lustre_root = NULL;
792 class_handle_cleanup();
793 class_exit_uuidlist();
795 leaked = atomic_read(&obd_memory);
796 CDEBUG(leaked ? D_ERROR : D_INFO,
797 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
802 /* Check that we're building against the appropriate version of the Lustre
805 #include <linux/lustre_version.h>
806 #define LUSTRE_MIN_VERSION 28
807 #define LUSTRE_MAX_VERSION 32
808 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
809 # error Cannot continue: Your Lustre kernel patch is older than the sources
810 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
811 # error Cannot continue: Your Lustre sources are older than the kernel patch
814 # warning "Lib Lustre - no versioning information"
818 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
819 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
820 MODULE_LICENSE("GPL");
822 module_init(init_obdclass);
823 module_exit(cleanup_obdclass);