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_CHECK:
244 case OBD_IOC_LLOG_REMOVE:
252 CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
253 if (!obd && cmd != OBD_IOC_DEVICE &&
254 cmd != OBD_IOC_LIST && cmd != OBD_GET_VERSION &&
255 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_UUID2DEV &&
256 cmd != OBD_IOC_PROCESS_CFG) {
257 CERROR("OBD ioctl: No device\n");
258 GOTO(out, err = -EINVAL);
260 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
261 CERROR("OBD ioctl: data error\n");
262 GOTO(out, err = -EINVAL);
264 data = (struct obd_ioctl_data *)buf;
267 case OBD_IOC_DEVICE: {
268 CDEBUG(D_IOCTL, "\n");
269 if (data->ioc_dev >= MAX_OBD_DEVICES) {
270 CERROR("OBD ioctl: DEVICE invalid device %d\n",
272 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
273 GOTO(out, err = -EINVAL);
275 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
276 ocus->ocus_current_obd = &obd_dev[data->ioc_dev];
280 case OBD_IOC_PROCESS_CFG: {
282 struct lustre_cfg *lcfg;
284 /* FIXME hack to liblustre dump, remove when switch
287 data->ioc_pbuf1 = data->ioc_inlbuf1;
288 data->ioc_plen1 = data->ioc_inllen1;
290 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
291 CERROR("No config buffer passed!\n");
292 GOTO(out, err = -EINVAL);
294 err = lustre_cfg_getdata(&buf, data->ioc_plen1,
298 lcfg = (struct lustre_cfg* ) buf;
300 err = class_process_config(lcfg);
301 lustre_cfg_freedata(buf, data->ioc_plen1);
307 char *buf2 = data->ioc_bulk;
308 int remains = data->ioc_inllen1;
310 if (!data->ioc_inlbuf1) {
311 CERROR("No buffer passed!\n");
312 GOTO(out, err = -EINVAL);
316 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
319 struct obd_device *obd = &obd_dev[i];
323 if (obd->obd_stopping)
325 else if (obd->obd_set_up)
327 else if (obd->obd_attached)
331 l = snprintf(buf2, remains, "%2d %s %s %s %s %d\n",
332 i, status, obd->obd_type->typ_name,
333 obd->obd_name, obd->obd_uuid.uuid,
334 obd->obd_type->typ_refcnt);
338 CERROR("not enough space for device listing\n");
343 err = copy_to_user((void *)arg, data, len);
349 case OBD_GET_VERSION:
350 if (!data->ioc_inlbuf1) {
351 CERROR("No buffer passed in ioctl\n");
352 GOTO(out, err = -EINVAL);
355 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
356 CERROR("ioctl buffer too small to hold version\n");
357 GOTO(out, err = -EINVAL);
360 memcpy(data->ioc_bulk, BUILD_VERSION,
361 strlen(BUILD_VERSION) + 1);
363 err = copy_to_user((void *)arg, data, len);
368 case OBD_IOC_NAME2DEV: {
369 /* Resolve a device name. This does not change the
370 * currently selected device.
374 dev = class_resolve_dev_name(data->ioc_inllen1,
378 GOTO(out, err = -EINVAL);
380 err = copy_to_user((void *)arg, data, sizeof(*data));
386 case OBD_IOC_UUID2DEV: {
387 /* Resolve a device uuid. This does not change the
388 * currently selected device.
391 struct obd_uuid uuid;
393 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
394 CERROR("No UUID passed!\n");
395 GOTO(out, err = -EINVAL);
397 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
398 CERROR("UUID not NUL terminated!\n");
399 GOTO(out, err = -EINVAL);
402 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
403 obd_str2uuid(&uuid, data->ioc_inlbuf1);
404 dev = class_uuid2dev(&uuid);
407 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
409 GOTO(out, err = -EINVAL);
412 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
414 err = copy_to_user((void *)arg, data, sizeof(*data));
422 case OBD_IOC_CONNECT: {
423 struct obd_export *exp;
424 obd_data2conn(&conn, data);
426 err = obd_connect(&conn, obd, &obd->obd_uuid);
428 CDEBUG(D_IOCTL, "assigned export "LPX64"\n", conn.cookie);
429 obd_conn2data(data, &conn);
433 exp = class_conn2export(&conn);
435 GOTO(out, err = -EINVAL);
437 err = obd_class_add_user_exp(ocus, exp);
439 obd_disconnect(exp, 0);
443 err = copy_to_user((void *)arg, data, sizeof(*data));
445 obd_class_remove_user_exp(ocus, exp);
446 obd_disconnect(exp, 0);
447 GOTO (out, err = -EFAULT);
449 class_export_put(exp);
453 case OBD_IOC_DISCONNECT: {
454 struct obd_export *exp;
455 obd_data2conn(&conn, data);
456 exp = class_conn2export(&conn);
458 GOTO(out, err = -EINVAL);
460 obd_class_remove_user_exp(ocus, exp);
461 err = obd_disconnect(exp, 0);
465 case OBD_IOC_NO_TRANSNO: {
466 if (!obd->obd_attached) {
467 CERROR("Device %d not attached\n", obd->obd_minor);
468 GOTO(out, err = -ENODEV);
471 "disabling committed-transno notifications on %d\n",
473 obd->obd_no_transno = 1;
477 case OBD_IOC_CLOSE_UUID: {
478 struct lustre_peer peer;
479 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
481 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer);
486 // obd_data2conn(&conn, data);
487 struct obd_class_user_conn *oconn;
489 if (list_empty(&ocus->ocus_conns))
490 GOTO(out, err = -ENOTCONN);
492 oconn = list_entry(ocus->ocus_conns.next, struct obd_class_user_conn, ocuc_chain);
493 err = obd_iocontrol(cmd, oconn->ocuc_exp, len, data, NULL);
497 err = copy_to_user((void *)arg, data, len);
506 obd_ioctl_freedata(buf, len);
510 } /* class_handle_ioctl */
514 #define OBD_MINOR 241
516 /* to control /dev/obd */
517 static int obd_class_ioctl(struct inode *inode, struct file *filp,
518 unsigned int cmd, unsigned long arg)
520 return class_handle_ioctl(filp->private_data, cmd, arg);
523 /* declare character device */
524 static struct file_operations obd_psdev_fops = {
525 ioctl: obd_class_ioctl, /* ioctl */
526 open: obd_class_open, /* open */
527 release: obd_class_release, /* release */
531 static struct miscdevice obd_psdev = {
537 void *obd_psdev = NULL;
540 EXPORT_SYMBOL(obd_dev);
541 EXPORT_SYMBOL(obdo_cachep);
542 EXPORT_SYMBOL(obd_memory);
543 EXPORT_SYMBOL(obd_memmax);
544 EXPORT_SYMBOL(obd_fail_loc);
545 EXPORT_SYMBOL(obd_timeout);
546 EXPORT_SYMBOL(obd_lustre_upcall);
547 EXPORT_SYMBOL(obd_sync_filter);
548 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
549 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
550 EXPORT_SYMBOL(proc_lustre_root);
552 EXPORT_SYMBOL(class_register_type);
553 EXPORT_SYMBOL(class_unregister_type);
554 EXPORT_SYMBOL(class_get_type);
555 EXPORT_SYMBOL(class_put_type);
556 EXPORT_SYMBOL(class_name2dev);
557 EXPORT_SYMBOL(class_name2obd);
558 EXPORT_SYMBOL(class_uuid2dev);
559 EXPORT_SYMBOL(class_uuid2obd);
560 EXPORT_SYMBOL(class_find_client_obd);
561 EXPORT_SYMBOL(__class_export_put);
562 EXPORT_SYMBOL(class_new_export);
563 EXPORT_SYMBOL(class_unlink_export);
564 EXPORT_SYMBOL(class_import_get);
565 EXPORT_SYMBOL(class_import_put);
566 EXPORT_SYMBOL(class_new_import);
567 EXPORT_SYMBOL(class_destroy_import);
568 EXPORT_SYMBOL(class_connect);
569 EXPORT_SYMBOL(class_conn2export);
570 EXPORT_SYMBOL(class_exp2obd);
571 EXPORT_SYMBOL(class_conn2obd);
572 EXPORT_SYMBOL(class_exp2cliimp);
573 EXPORT_SYMBOL(class_conn2cliimp);
574 EXPORT_SYMBOL(class_disconnect);
575 EXPORT_SYMBOL(class_disconnect_exports);
577 EXPORT_SYMBOL(osic_init);
578 EXPORT_SYMBOL(osic_release);
579 EXPORT_SYMBOL(osic_add_one);
580 EXPORT_SYMBOL(osic_wait);
581 EXPORT_SYMBOL(osic_complete_one);
584 EXPORT_SYMBOL(class_uuid_unparse);
585 EXPORT_SYMBOL(lustre_uuid_to_peer);
587 EXPORT_SYMBOL(class_handle_hash);
588 EXPORT_SYMBOL(class_handle_unhash);
589 EXPORT_SYMBOL(class_handle2object);
592 EXPORT_SYMBOL(class_get_profile);
593 EXPORT_SYMBOL(class_process_config);
594 EXPORT_SYMBOL(class_config_parse_llog);
595 EXPORT_SYMBOL(class_config_dump_llog);
596 EXPORT_SYMBOL(class_attach);
597 EXPORT_SYMBOL(class_setup);
598 EXPORT_SYMBOL(class_cleanup);
599 EXPORT_SYMBOL(class_detach);
602 int obd_proc_read_version(char *page, char **start, off_t off, int count,
603 int *eof, void *data)
606 return snprintf(page, count, "%s\n", BUILD_VERSION);
609 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
610 int *eof, void *data)
613 return snprintf(page, count, "%s\n",
622 /* Root for /proc/fs/lustre */
623 struct proc_dir_entry *proc_lustre_root = NULL;
624 struct lprocfs_vars lprocfs_base[] = {
625 { "version", obd_proc_read_version, NULL, NULL },
626 { "pinger", obd_proc_read_pinger, NULL, NULL },
630 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
632 if (*pos >= MAX_OBD_DEVICES)
634 return &obd_dev[*pos];
637 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
641 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
644 if (*pos >= MAX_OBD_DEVICES)
646 return &obd_dev[*pos];
649 static int obd_device_list_seq_show(struct seq_file *p, void *v)
651 struct obd_device *obd = (struct obd_device *)v;
652 int index = obd - &obd_dev[0];
657 if (obd->obd_stopping)
659 else if (obd->obd_set_up)
661 else if (obd->obd_attached)
666 return seq_printf(p, "%3d %s %s %s %s %d\n",
667 (int)index, status, obd->obd_type->typ_name,
668 obd->obd_name, obd->obd_uuid.uuid,
669 atomic_read(&obd->obd_refcount));
672 struct seq_operations obd_device_list_sops = {
673 .start = obd_device_list_seq_start,
674 .stop = obd_device_list_seq_stop,
675 .next = obd_device_list_seq_next,
676 .show = obd_device_list_seq_show,
679 static int obd_device_list_open(struct inode *inode, struct file *file)
681 struct proc_dir_entry *dp = inode->u.generic_ip;
682 struct seq_file *seq;
683 int rc = seq_open(file, &obd_device_list_sops);
688 seq = file->private_data;
689 seq->private = dp->data;
694 struct file_operations obd_device_list_fops = {
695 .open = obd_device_list_open,
698 .release = seq_release,
703 static int __init init_obdclass(void)
705 int init_obdclass(void)
708 struct obd_device *obd;
710 struct proc_dir_entry *entry;
715 printk(KERN_INFO "Lustre: OBD class driver Build Version: "
716 BUILD_VERSION", info@clusterfs.com\n");
718 class_init_uuidlist();
719 err = class_handle_init();
723 sema_init(&obd_conf_sem, 1);
724 INIT_LIST_HEAD(&obd_types);
726 err = misc_register(&obd_psdev);
728 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
732 /* This struct is already zerod for us (static global) */
733 for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
736 err = obd_init_caches();
745 proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
746 if (!proc_lustre_root) {
748 "LustreError: error registering /proc/fs/lustre\n");
751 proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
752 entry = create_proc_entry("devices", 0444, proc_lustre_root);
754 printk(KERN_ERR "LustreError: error registering "
755 "/proc/fs/lustre/devices\n");
756 lprocfs_remove(proc_lustre_root);
759 entry->proc_fops = &obd_device_list_fops;
765 static void /*__exit*/ cleanup_obdclass(void)
767 static void cleanup_obdclass(void)
773 misc_deregister(&obd_psdev);
774 for (i = 0; i < MAX_OBD_DEVICES; i++) {
775 struct obd_device *obd = &obd_dev[i];
776 if (obd->obd_type && obd->obd_set_up &&
777 OBT(obd) && OBP(obd, detach)) {
778 /* XXX should this call generic detach otherwise? */
779 OBP(obd, detach)(obd);
783 obd_cleanup_caches();
788 if (proc_lustre_root) {
789 lprocfs_remove(proc_lustre_root);
790 proc_lustre_root = NULL;
794 class_handle_cleanup();
795 class_exit_uuidlist();
797 leaked = atomic_read(&obd_memory);
798 CDEBUG(leaked ? D_ERROR : D_INFO,
799 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
804 /* Check that we're building against the appropriate version of the Lustre
807 #include <linux/lustre_version.h>
808 #define LUSTRE_MIN_VERSION 28
809 #define LUSTRE_MAX_VERSION 32
810 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
811 # error Cannot continue: Your Lustre kernel patch is older than the sources
812 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
813 # error Cannot continue: Your Lustre sources are older than the kernel patch
816 # warning "Lib Lustre - no versioning information"
820 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
821 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
822 MODULE_LICENSE("GPL");
824 module_init(init_obdclass);
825 module_exit(cleanup_obdclass);