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 <liblustre.h>
38 #include <obd_support.h>
39 #include <obd_class.h>
40 #include <lustre_debug.h>
41 #include <lprocfs_status.h>
42 #include <lustre/lustre_build_version.h>
43 #include <libcfs/list.h>
44 #include "llog_internal.h"
47 /* liblustre workaround */
48 atomic_t libcfs_kmemory = {0};
51 struct obd_device *obd_devs[MAX_OBD_DEVICES];
52 struct list_head obd_types;
53 spinlock_t obd_dev_lock = SPIN_LOCK_UNLOCKED;
59 /* The following are visible and mutable through /proc/sys/lustre/. */
60 unsigned int obd_fail_loc;
61 unsigned int obd_debug_peer_on_timeout;
62 unsigned int obd_dump_on_timeout;
63 unsigned int obd_dump_on_eviction;
64 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
65 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
66 unsigned int obd_health_check_timeout = HEALTH_CHECK_TIMEOUT_DEFAULT; /* seconds */
67 unsigned int obd_max_dirty_pages = 256;
68 atomic_t obd_dirty_pages;
70 cfs_waitq_t obd_race_waitq;
74 unsigned int obd_print_fail_loc(void)
76 CWARN("obd_fail_loc = %x\n", obd_fail_loc);
80 void obd_set_fail_loc(unsigned int fl)
85 /* opening /dev/obd */
86 static int obd_class_open(unsigned long flags, void *args)
94 /* closing /dev/obd */
95 static int obd_class_release(unsigned long flags, void *args)
104 static inline void obd_data2conn(struct lustre_handle *conn,
105 struct obd_ioctl_data *data)
107 memset(conn, 0, sizeof *conn);
108 conn->cookie = data->ioc_cookie;
111 static inline void obd_conn2data(struct obd_ioctl_data *data,
112 struct lustre_handle *conn)
114 data->ioc_cookie = conn->cookie;
117 int class_resolve_dev_name(__u32 len, const char *name)
124 CERROR("No name passed,!\n");
125 GOTO(out, rc = -EINVAL);
127 if (name[len - 1] != 0) {
128 CERROR("Name not nul terminated!\n");
129 GOTO(out, rc = -EINVAL);
132 CDEBUG(D_IOCTL, "device name %s\n", name);
133 dev = class_name2dev(name);
135 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
136 GOTO(out, rc = -EINVAL);
139 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
146 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
149 struct obd_ioctl_data *data;
150 struct libcfs_debug_ioctl_data *debug_data;
151 struct obd_device *obd = NULL;
152 int err = 0, len = 0;
155 /* only for debugging */
156 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
157 debug_data = (struct libcfs_debug_ioctl_data*)arg;
158 libcfs_subsystem_debug = debug_data->subs;
159 libcfs_debug = debug_data->debug;
163 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
164 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
165 CERROR("OBD ioctl: data error\n");
166 GOTO(out, err = -EINVAL);
168 data = (struct obd_ioctl_data *)buf;
171 case OBD_IOC_PROCESS_CFG: {
172 struct lustre_cfg *lcfg;
174 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
175 CERROR("No config buffer passed!\n");
176 GOTO(out, err = -EINVAL);
178 OBD_ALLOC(lcfg, data->ioc_plen1);
180 GOTO(out, err = -ENOMEM);
181 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
183 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
185 err = class_process_config(lcfg);
187 OBD_FREE(lcfg, data->ioc_plen1);
191 case OBD_GET_VERSION:
192 if (!data->ioc_inlbuf1) {
193 CERROR("No buffer passed in ioctl\n");
194 GOTO(out, err = -EINVAL);
197 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
198 CERROR("ioctl buffer too small to hold version\n");
199 GOTO(out, err = -EINVAL);
202 memcpy(data->ioc_bulk, BUILD_VERSION,
203 strlen(BUILD_VERSION) + 1);
205 err = obd_ioctl_popdata((void *)arg, data, len);
210 case OBD_IOC_NAME2DEV: {
211 /* Resolve a device name. This does not change the
212 * currently selected device.
216 dev = class_resolve_dev_name(data->ioc_inllen1,
220 GOTO(out, err = -EINVAL);
222 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
228 case OBD_IOC_UUID2DEV: {
229 /* Resolve a device uuid. This does not change the
230 * currently selected device.
233 struct obd_uuid uuid;
235 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
236 CERROR("No UUID passed!\n");
237 GOTO(out, err = -EINVAL);
239 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
240 CERROR("UUID not NUL terminated!\n");
241 GOTO(out, err = -EINVAL);
244 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
245 obd_str2uuid(&uuid, data->ioc_inlbuf1);
246 dev = class_uuid2dev(&uuid);
249 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
251 GOTO(out, err = -EINVAL);
254 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
256 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
262 case OBD_IOC_CLOSE_UUID: {
263 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
268 case OBD_IOC_GETDEVICE: {
269 int index = data->ioc_count;
272 if (!data->ioc_inlbuf1) {
273 CERROR("No buffer passed in ioctl\n");
274 GOTO(out, err = -EINVAL);
276 if (data->ioc_inllen1 < 128) {
277 CERROR("ioctl buffer too small to hold version\n");
278 GOTO(out, err = -EINVAL);
281 obd = class_num2obd(index);
283 GOTO(out, err = -ENOENT);
285 if (obd->obd_stopping)
287 else if (obd->obd_set_up)
289 else if (obd->obd_attached)
293 str = (char *)data->ioc_bulk;
294 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
295 (int)index, status, obd->obd_type->typ_name,
296 obd->obd_name, obd->obd_uuid.uuid,
297 atomic_read(&obd->obd_refcount));
298 err = obd_ioctl_popdata((void *)arg, data, len);
305 if (data->ioc_dev >= class_devno_max()) {
306 CERROR("OBD ioctl: No device\n");
307 GOTO(out, err = -EINVAL);
310 obd = class_num2obd(data->ioc_dev);
312 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
313 GOTO(out, err = -EINVAL);
315 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
317 if (!obd->obd_set_up || obd->obd_stopping) {
318 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
319 GOTO(out, err = -EINVAL);
323 case OBD_IOC_NO_TRANSNO: {
324 if (!obd->obd_attached) {
325 CERROR("Device %d not attached\n", obd->obd_minor);
326 GOTO(out, err = -ENODEV);
328 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
330 obd->obd_no_transno = 1;
335 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
339 err = obd_ioctl_popdata((void *)arg, data, len);
348 obd_ioctl_freedata(buf, len);
350 } /* class_handle_ioctl */
354 #define OBD_MINOR 241
356 /* to control /dev/obd */
357 static int obd_class_ioctl (struct cfs_psdev_file *pfile, unsigned long cmd,
360 return class_handle_ioctl(cmd, (unsigned long)arg);
363 /* declare character device */
364 struct cfs_psdev_ops obd_psdev_ops = {
365 /* .p_open = */ obd_class_open, /* open */
366 /* .p_close = */ obd_class_release, /* release */
367 /* .p_read = */ NULL,
368 /* .p_write = */ NULL,
369 /* .p_ioctl = */ obd_class_ioctl /* ioctl */
372 extern cfs_psdev_t obd_psdev;
374 void *obd_psdev = NULL;
377 EXPORT_SYMBOL(obd_devs);
378 EXPORT_SYMBOL(obd_fail_loc);
379 EXPORT_SYMBOL(obd_print_fail_loc);
380 EXPORT_SYMBOL(obd_race_waitq);
381 EXPORT_SYMBOL(obd_race_state);
382 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
383 EXPORT_SYMBOL(obd_dump_on_timeout);
384 EXPORT_SYMBOL(obd_dump_on_eviction);
385 EXPORT_SYMBOL(obd_timeout);
386 EXPORT_SYMBOL(ldlm_timeout);
387 EXPORT_SYMBOL(obd_health_check_timeout);
388 EXPORT_SYMBOL(obd_max_dirty_pages);
389 EXPORT_SYMBOL(obd_dirty_pages);
390 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
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);
411 EXPORT_SYMBOL(class_num2obd);
414 EXPORT_SYMBOL(class_uuid_unparse);
415 EXPORT_SYMBOL(lustre_uuid_to_peer);
417 EXPORT_SYMBOL(class_handle_hash);
418 EXPORT_SYMBOL(class_handle_unhash);
419 EXPORT_SYMBOL(class_handle_hash_back);
420 EXPORT_SYMBOL(class_handle2object);
421 EXPORT_SYMBOL(class_handle_free_cb);
424 EXPORT_SYMBOL(class_incref);
425 EXPORT_SYMBOL(class_decref);
426 EXPORT_SYMBOL(class_get_profile);
427 EXPORT_SYMBOL(class_del_profile);
428 EXPORT_SYMBOL(class_del_profiles);
429 EXPORT_SYMBOL(class_process_config);
430 EXPORT_SYMBOL(class_process_proc_param);
431 EXPORT_SYMBOL(class_config_parse_llog);
432 EXPORT_SYMBOL(class_config_dump_llog);
433 EXPORT_SYMBOL(class_attach);
434 EXPORT_SYMBOL(class_setup);
435 EXPORT_SYMBOL(class_cleanup);
436 EXPORT_SYMBOL(class_detach);
437 EXPORT_SYMBOL(class_manual_cleanup);
440 EXPORT_SYMBOL(mea_name2idx);
441 EXPORT_SYMBOL(raw_name2idx);
443 #define OBD_INIT_CHECK
444 #ifdef OBD_INIT_CHECK
445 int obd_init_checks(void)
447 __u64 u64val, div64val;
451 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
452 LPU64, LPD64, LPX64, LPSZ, LPSSZ);
454 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
456 u64val = OBD_OBJECT_EOF;
457 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
458 if (u64val != OBD_OBJECT_EOF) {
459 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
460 u64val, (int)sizeof(u64val));
463 len = snprintf(buf, sizeof(buf), LPX64, u64val);
465 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
469 div64val = OBD_OBJECT_EOF;
470 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
471 if (u64val != OBD_OBJECT_EOF) {
472 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
473 u64val, (int)sizeof(u64val));
476 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
477 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
478 u64val, (int)sizeof(u64val));
481 if (do_div(div64val, 256) != (u64val & 255)) {
482 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
485 if (u64val >> 8 != div64val) {
486 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
487 u64val, div64val, u64val >> 8);
490 len = snprintf(buf, sizeof(buf), LPX64, u64val);
492 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
495 len = snprintf(buf, sizeof(buf), LPU64, u64val);
497 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
500 len = snprintf(buf, sizeof(buf), LPD64, u64val);
502 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
505 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
506 CWARN("mask failed: u64val "LPU64" >= %lu\n", u64val,
514 #define obd_init_checks() do {} while(0)
517 extern spinlock_t obd_types_lock;
518 extern int class_procfs_init(void);
519 extern int class_procfs_clean(void);
522 static int __init init_obdclass(void)
524 int init_obdclass(void)
529 int lustre_register_fs(void);
531 printk(KERN_INFO "Lustre: OBD class driver, info@clusterfs.com\n");
532 printk(KERN_INFO " Lustre Version: "LUSTRE_VERSION_STRING"\n");
533 printk(KERN_INFO " Build Version: "BUILD_VERSION"\n");
535 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
536 INIT_LIST_HEAD(&capa_list[i]);
538 CDEBUG(D_INFO, "Lustre: OBD class driver, info@clusterfs.com\n");
539 CDEBUG(D_INFO, " Lustre Version: "LUSTRE_VERSION_STRING"\n");
540 CDEBUG(D_INFO, " Build Version: "BUILD_VERSION"\n");
543 spin_lock_init(&obd_types_lock);
544 cfs_waitq_init(&obd_race_waitq);
545 obd_zombie_impexp_init();
547 err = obd_init_checks();
548 if (err == -EOVERFLOW)
551 class_init_uuidlist();
552 err = class_handle_init();
556 spin_lock_init(&obd_dev_lock);
557 INIT_LIST_HEAD(&obd_types);
559 err = cfs_psdev_register(&obd_psdev);
561 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
565 /* This struct is already zerod for us (static global) */
566 for (i = 0; i < class_devno_max(); i++)
569 /* Default the dirty page cache cap to 1/2 of system memory */
570 obd_max_dirty_pages = num_physpages / 2;
572 err = obd_init_caches();
576 err = lu_global_init();
579 err = class_procfs_init();
582 err = lustre_register_fs();
588 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
589 * ifdef to the end of the file to cover module and versioning goo.*/
591 static void cleanup_obdclass(void)
594 int lustre_unregister_fs(void);
597 lustre_unregister_fs();
599 cfs_psdev_deregister(&obd_psdev);
600 for (i = 0; i < class_devno_max(); i++) {
601 struct obd_device *obd = class_num2obd(i);
602 if (obd && obd->obd_set_up &&
603 OBT(obd) && OBP(obd, detach)) {
604 /* XXX should this call generic detach otherwise? */
605 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
606 OBP(obd, detach)(obd);
611 obd_cleanup_caches();
614 class_procfs_clean();
616 class_handle_cleanup();
617 class_exit_uuidlist();
621 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
622 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
623 MODULE_LICENSE("GPL");
625 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);