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 the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
26 * These are the only exported functions, they provide some generic
27 * infrastructure for managing object devices
30 #define DEBUG_SUBSYSTEM S_CLASS
32 # define EXPORT_SYMTAB
35 #include <linux/config.h> /* for CONFIG_PROC_FS */
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/kernel.h>
39 #include <linux/major.h>
40 #include <linux/sched.h>
42 #include <linux/slab.h>
43 #include <linux/ioport.h>
44 #include <linux/fcntl.h>
45 #include <linux/delay.h>
46 #include <linux/skbuff.h>
47 #include <linux/proc_fs.h>
49 #include <linux/poll.h>
50 #include <linux/init.h>
51 #include <linux/list.h>
52 #include <linux/highmem.h>
54 #include <asm/ioctls.h>
55 #include <asm/system.h>
57 #include <asm/uaccess.h>
58 #include <linux/miscdevice.h>
59 #include <linux/smp_lock.h>
60 #include <linux/seq_file.h>
62 # include <liblustre.h>
65 #include <linux/obd_support.h>
66 #include <linux/obd_class.h>
67 #include <linux/lustre_debug.h>
68 #include <linux/lprocfs_status.h>
70 #include <linux/lustre_build_version.h>
71 #include <linux/lustre_version.h>
73 #include <libcfs/list.h>
74 #include "llog_internal.h"
77 /* liblustre workaround */
78 atomic_t libcfs_kmemory = {0};
81 struct obd_device obd_dev[MAX_OBD_DEVICES];
82 struct list_head obd_types;
83 spinlock_t obd_dev_lock;
91 /* The following are visible and mutable through /proc/sys/lustre/. */
92 unsigned int obd_fail_loc;
93 unsigned int obd_dump_on_timeout;
94 unsigned int obd_timeout = 100; /* seconds */
95 unsigned int ldlm_timeout = 20; /* seconds */
96 unsigned int obd_health_check_timeout = 120; /* seconds */
97 char obd_lustre_upcall[128] = "DEFAULT"; /* or NONE or /full/path/to/upcall */
98 unsigned int obd_sync_filter; /* = 0, don't sync by default */
100 DECLARE_WAIT_QUEUE_HEAD(obd_race_waitq);
103 unsigned int obd_print_fail_loc(void)
105 CWARN("obd_fail_loc = %x\n", obd_fail_loc);
109 void obd_set_fail_loc(unsigned int fl)
114 /* opening /dev/obd */
115 static int obd_class_open(struct inode * inode, struct file * file)
123 /* closing /dev/obd */
124 static int obd_class_release(struct inode * inode, struct file * file)
133 static inline void obd_data2conn(struct lustre_handle *conn,
134 struct obd_ioctl_data *data)
136 memset(conn, 0, sizeof *conn);
137 conn->cookie = data->ioc_cookie;
140 static inline void obd_conn2data(struct obd_ioctl_data *data,
141 struct lustre_handle *conn)
143 data->ioc_cookie = conn->cookie;
146 int class_resolve_dev_name(uint32_t len, char *name)
152 CERROR("No name passed,!\n");
153 GOTO(out, rc = -EINVAL);
155 if (name[len - 1] != 0) {
156 CERROR("Name not nul terminated!\n");
157 GOTO(out, rc = -EINVAL);
160 CDEBUG(D_IOCTL, "device name %s\n", name);
161 dev = class_name2dev(name);
163 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
164 GOTO(out, rc = -EINVAL);
167 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
174 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
177 struct obd_ioctl_data *data;
178 struct libcfs_debug_ioctl_data *debug_data;
179 struct obd_device *obd = NULL;
180 int err = 0, len = 0;
184 if (current->fsuid != 0)
185 RETURN(err = -EACCES);
188 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
189 RETURN(err = -ENOTTY);
191 /* only for debugging */
192 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
193 debug_data = (struct libcfs_debug_ioctl_data*)arg;
194 libcfs_subsystem_debug = debug_data->subs;
195 libcfs_debug = debug_data->debug;
199 CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
200 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
201 CERROR("OBD ioctl: data error\n");
202 GOTO(out, err = -EINVAL);
204 data = (struct obd_ioctl_data *)buf;
207 case OBD_IOC_PROCESS_CFG: {
208 struct lustre_cfg *lcfg;
210 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
211 CERROR("No config buffer passed!\n");
212 GOTO(out, err = -EINVAL);
215 err = lustre_cfg_sanity_check(data->ioc_pbuf1,
220 OBD_ALLOC(lcfg, data->ioc_plen1);
221 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
223 err = class_process_config(lcfg);
224 OBD_FREE(lcfg, data->ioc_plen1);
228 case OBD_GET_VERSION:
229 if (!data->ioc_inlbuf1) {
230 CERROR("No buffer passed in ioctl\n");
231 GOTO(out, err = -EINVAL);
234 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
235 CERROR("ioctl buffer too small to hold version\n");
236 GOTO(out, err = -EINVAL);
239 memcpy(data->ioc_bulk, BUILD_VERSION,
240 strlen(BUILD_VERSION) + 1);
242 err = copy_to_user((void *)arg, data, len);
247 case OBD_IOC_NAME2DEV: {
248 /* Resolve a device name. This does not change the
249 * currently selected device.
253 dev = class_resolve_dev_name(data->ioc_inllen1,
257 GOTO(out, err = -EINVAL);
259 err = copy_to_user((void *)arg, data, sizeof(*data));
265 case OBD_IOC_UUID2DEV: {
266 /* Resolve a device uuid. This does not change the
267 * currently selected device.
270 struct obd_uuid uuid;
272 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
273 CERROR("No UUID passed!\n");
274 GOTO(out, err = -EINVAL);
276 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
277 CERROR("UUID not NUL terminated!\n");
278 GOTO(out, err = -EINVAL);
281 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
282 obd_str2uuid(&uuid, data->ioc_inlbuf1);
283 dev = class_uuid2dev(&uuid);
286 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
288 GOTO(out, err = -EINVAL);
291 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
293 err = copy_to_user((void *)arg, data, sizeof(*data));
300 case OBD_IOC_CLOSE_UUID: {
301 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
308 if (data->ioc_dev >= MAX_OBD_DEVICES) {
309 CERROR("OBD ioctl: No device\n");
310 GOTO(out, err = -EINVAL);
312 obd = &obd_dev[data->ioc_dev];
313 if (!(obd && obd->obd_set_up) || obd->obd_stopping) {
314 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
315 GOTO(out, err = -EINVAL);
319 case OBD_IOC_NO_TRANSNO: {
320 if (!obd->obd_attached) {
321 CERROR("Device %d not attached\n", obd->obd_minor);
322 GOTO(out, err = -ENODEV);
325 "disabling committed-transno notifications on %d\n",
327 obd->obd_no_transno = 1;
332 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
336 err = copy_to_user((void *)arg, data, len);
345 obd_ioctl_freedata(buf, len);
347 } /* class_handle_ioctl */
351 #define OBD_MINOR 241
353 /* to control /dev/obd */
354 static int obd_class_ioctl(struct inode *inode, struct file *filp,
355 unsigned int cmd, unsigned long arg)
357 return class_handle_ioctl(cmd, arg);
360 /* declare character device */
361 static struct file_operations obd_psdev_fops = {
362 .owner = THIS_MODULE,
363 .ioctl = obd_class_ioctl, /* ioctl */
364 .open = obd_class_open, /* open */
365 .release = obd_class_release, /* release */
369 static struct miscdevice obd_psdev = {
372 .fops = &obd_psdev_fops,
375 void *obd_psdev = NULL;
378 EXPORT_SYMBOL(obd_dev);
379 EXPORT_SYMBOL(obd_fail_loc);
380 EXPORT_SYMBOL(obd_print_fail_loc);
381 EXPORT_SYMBOL(obd_race_waitq);
382 EXPORT_SYMBOL(obd_dump_on_timeout);
383 EXPORT_SYMBOL(obd_timeout);
384 EXPORT_SYMBOL(ldlm_timeout);
385 EXPORT_SYMBOL(obd_health_check_timeout);
386 EXPORT_SYMBOL(obd_lustre_upcall);
387 EXPORT_SYMBOL(obd_sync_filter);
388 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
389 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
391 struct proc_dir_entry *proc_lustre_root;
392 EXPORT_SYMBOL(proc_lustre_root);
394 EXPORT_SYMBOL(class_register_type);
395 EXPORT_SYMBOL(class_unregister_type);
396 EXPORT_SYMBOL(class_get_type);
397 EXPORT_SYMBOL(class_put_type);
398 EXPORT_SYMBOL(class_name2dev);
399 EXPORT_SYMBOL(class_name2obd);
400 EXPORT_SYMBOL(class_uuid2dev);
401 EXPORT_SYMBOL(class_uuid2obd);
402 EXPORT_SYMBOL(class_find_client_obd);
403 EXPORT_SYMBOL(class_find_client_notype);
404 EXPORT_SYMBOL(class_devices_in_group);
405 EXPORT_SYMBOL(class_conn2export);
406 EXPORT_SYMBOL(class_exp2obd);
407 EXPORT_SYMBOL(class_conn2obd);
408 EXPORT_SYMBOL(class_exp2cliimp);
409 EXPORT_SYMBOL(class_conn2cliimp);
410 EXPORT_SYMBOL(class_disconnect);
413 EXPORT_SYMBOL(class_uuid_unparse);
414 EXPORT_SYMBOL(lustre_uuid_to_peer);
416 EXPORT_SYMBOL(class_handle_hash);
417 EXPORT_SYMBOL(class_handle_unhash);
418 EXPORT_SYMBOL(class_handle2object);
421 EXPORT_SYMBOL(class_get_profile);
422 EXPORT_SYMBOL(class_del_profile);
423 EXPORT_SYMBOL(class_process_config);
424 EXPORT_SYMBOL(class_config_parse_llog);
425 EXPORT_SYMBOL(class_config_dump_llog);
426 EXPORT_SYMBOL(class_attach);
427 EXPORT_SYMBOL(class_setup);
428 EXPORT_SYMBOL(class_cleanup);
429 EXPORT_SYMBOL(class_detach);
430 EXPORT_SYMBOL(class_manual_cleanup);
433 int obd_proc_read_version(char *page, char **start, off_t off, int count,
434 int *eof, void *data)
437 return snprintf(page, count, "%s\n", BUILD_VERSION);
440 int obd_proc_read_kernel_version(char *page, char **start, off_t off, int count,
441 int *eof, void *data)
444 return snprintf(page, count, "%u\n", LUSTRE_KERNEL_VERSION);
447 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
448 int *eof, void *data)
451 return snprintf(page, count, "%s\n",
460 static int obd_proc_read_health(char *page, char **start, off_t off,
461 int count, int *eof, void *data)
466 if (libcfs_catastrophe)
467 rc += snprintf(page + rc, count - rc, "LBUG\n");
469 spin_lock(&obd_dev_lock);
470 for (i = 0; i < MAX_OBD_DEVICES; i++) {
471 struct obd_device *obd;
474 if (obd->obd_type == NULL)
477 atomic_inc(&obd->obd_refcount);
478 spin_unlock(&obd_dev_lock);
480 if (obd_health_check(obd)) {
481 rc += snprintf(page + rc, count - rc,
482 "device %s reported unhealthy\n",
486 spin_lock(&obd_dev_lock);
488 spin_unlock(&obd_dev_lock);
491 return snprintf(page, count, "healthy\n");
493 rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
497 static int obd_proc_rd_health_timeout(char *page, char **start, off_t off,
498 int count, int *eof, void *data)
501 return snprintf(page, count, "%d\n", obd_health_check_timeout);
504 static int obd_proc_wr_health_timeout(struct file *file, const char *buffer,
505 unsigned long count, void *data)
509 rc = lprocfs_write_helper(buffer, count, &val);
513 obd_health_check_timeout = val;
518 /* Root for /proc/fs/lustre */
519 struct lprocfs_vars lprocfs_base[] = {
520 { "version", obd_proc_read_version, NULL, NULL },
521 { "kernel_version", obd_proc_read_kernel_version, NULL, NULL },
522 { "pinger", obd_proc_read_pinger, NULL, NULL },
523 { "health_check", obd_proc_read_health, NULL, NULL },
524 { "health_check_timeout", obd_proc_rd_health_timeout,
525 obd_proc_wr_health_timeout, NULL },
529 #define lprocfs_base NULL
533 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
535 if (*pos >= MAX_OBD_DEVICES)
537 return &obd_dev[*pos];
540 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
544 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
547 if (*pos >= MAX_OBD_DEVICES)
549 return &obd_dev[*pos];
552 static int obd_device_list_seq_show(struct seq_file *p, void *v)
554 struct obd_device *obd = (struct obd_device *)v;
555 int index = obd - &obd_dev[0];
560 if (obd->obd_stopping)
562 else if (obd->obd_set_up)
564 else if (obd->obd_attached)
569 return seq_printf(p, "%3d %s %s %s %s %d\n",
570 (int)index, status, obd->obd_type->typ_name,
571 obd->obd_name, obd->obd_uuid.uuid,
572 atomic_read(&obd->obd_refcount));
575 struct seq_operations obd_device_list_sops = {
576 .start = obd_device_list_seq_start,
577 .stop = obd_device_list_seq_stop,
578 .next = obd_device_list_seq_next,
579 .show = obd_device_list_seq_show,
582 static int obd_device_list_open(struct inode *inode, struct file *file)
584 struct proc_dir_entry *dp = PDE(inode);
585 struct seq_file *seq;
586 int rc = seq_open(file, &obd_device_list_sops);
591 seq = file->private_data;
592 seq->private = dp->data;
597 struct file_operations obd_device_list_fops = {
598 .owner = THIS_MODULE,
599 .open = obd_device_list_open,
602 .release = seq_release,
606 #define OBD_INIT_CHECK
607 #ifdef OBD_INIT_CHECK
608 int obd_init_checks(void)
610 __u64 u64val, div64val;
614 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
615 LPU64, LPD64, LPX64, LPSZ, LPSSZ);
617 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
619 u64val = OBD_OBJECT_EOF;
620 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
621 if (u64val != OBD_OBJECT_EOF) {
622 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
623 u64val, (int)sizeof(u64val));
626 len = snprintf(buf, sizeof(buf), LPX64, u64val);
628 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
632 div64val = OBD_OBJECT_EOF;
633 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
634 if (u64val != OBD_OBJECT_EOF) {
635 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
636 u64val, (int)sizeof(u64val));
639 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
640 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
641 u64val, (int)sizeof(u64val));
644 if (do_div(div64val, 256) != (u64val & 255)) {
645 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
648 if (u64val >> 8 != div64val) {
649 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
650 u64val, div64val, u64val >> 8);
653 len = snprintf(buf, sizeof(buf), LPX64, u64val);
655 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
658 len = snprintf(buf, sizeof(buf), LPU64, u64val);
660 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
663 len = snprintf(buf, sizeof(buf), LPD64, u64val);
665 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
668 if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
669 CWARN("mask failed: u64val "LPU64" >= %lu\n", u64val,PAGE_SIZE);
676 #define obd_init_checks() do {} while(0)
680 static int __init init_obdclass(void)
682 int init_obdclass(void)
685 struct obd_device *obd;
687 struct proc_dir_entry *entry;
688 int lustre_register_fs(void);
694 printk(KERN_INFO "Lustre: OBD class driver Build Version: "
695 BUILD_VERSION", info@clusterfs.com\n");
697 CDEBUG(D_INFO, "Lustre: OBD class driver Build Version: "
698 BUILD_VERSION", info@clusterfs.com\n");
701 err = obd_init_checks();
702 if (err == -EOVERFLOW)
705 class_init_uuidlist();
706 err = class_handle_init();
710 spin_lock_init(&obd_dev_lock);
711 INIT_LIST_HEAD(&obd_types);
713 err = misc_register(&obd_psdev);
715 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
719 /* This struct is already zerod for us (static global) */
720 for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
723 err = obd_init_caches();
730 proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
731 if (!proc_lustre_root) {
733 "LustreError: error registering /proc/fs/lustre\n");
736 proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
737 entry = create_proc_entry("devices", 0444, proc_lustre_root);
739 CERROR("error registering /proc/fs/lustre/devices\n");
740 lprocfs_remove(proc_lustre_root);
743 entry->proc_fops = &obd_device_list_fops;
745 lustre_register_fs();
750 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
751 * ifdef to the end of the file to cover module and versioning goo.*/
754 static void cleanup_obdclass(void)
758 int lustre_unregister_fs(void);
761 lustre_unregister_fs();
763 misc_deregister(&obd_psdev);
764 for (i = 0; i < MAX_OBD_DEVICES; i++) {
765 struct obd_device *obd = &obd_dev[i];
766 if (obd->obd_type && obd->obd_set_up &&
767 OBT(obd) && OBP(obd, detach)) {
768 /* XXX should this call generic detach otherwise? */
769 OBP(obd, detach)(obd);
773 obd_cleanup_caches();
776 if (proc_lustre_root) {
777 lprocfs_remove(proc_lustre_root);
778 proc_lustre_root = NULL;
781 class_handle_cleanup();
782 class_exit_uuidlist();
784 leaked = atomic_read(&obd_memory);
785 CDEBUG(leaked ? D_ERROR : D_INFO,
786 "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
791 /* Check that we're building against the appropriate version of the Lustre
793 #include <linux/lustre_version.h>
794 #define LUSTRE_MIN_VERSION 37
795 #define LUSTRE_MAX_VERSION 47
796 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
797 # error Cannot continue: Your Lustre kernel patch is older than the sources
798 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
799 # error Cannot continue: Your Lustre sources are older than the kernel patch
802 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
803 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
804 MODULE_LICENSE("GPL");
806 module_init(init_obdclass);
807 module_exit(cleanup_obdclass);