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;
55 /* The following are visible and mutable through /proc/sys/lustre/. */
56 unsigned int obd_debug_peer_on_timeout;
57 unsigned int obd_dump_on_timeout;
58 unsigned int obd_dump_on_eviction;
59 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
60 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
61 unsigned int obd_max_dirty_pages = 256;
62 atomic_t obd_dirty_pages;
64 cfs_waitq_t obd_race_waitq;
68 unsigned int obd_print_fail_loc(void)
70 CWARN("obd_fail_loc = %x\n", obd_fail_loc);
74 void obd_set_fail_loc(unsigned int fl)
79 /* opening /dev/obd */
80 static int obd_class_open(unsigned long flags, void *args)
88 /* closing /dev/obd */
89 static int obd_class_release(unsigned long flags, void *args)
98 static inline void obd_data2conn(struct lustre_handle *conn,
99 struct obd_ioctl_data *data)
101 memset(conn, 0, sizeof *conn);
102 conn->cookie = data->ioc_cookie;
105 static inline void obd_conn2data(struct obd_ioctl_data *data,
106 struct lustre_handle *conn)
108 data->ioc_cookie = conn->cookie;
111 int class_resolve_dev_name(__u32 len, const char *name)
118 CERROR("No name passed,!\n");
119 GOTO(out, rc = -EINVAL);
121 if (name[len - 1] != 0) {
122 CERROR("Name not nul terminated!\n");
123 GOTO(out, rc = -EINVAL);
126 CDEBUG(D_IOCTL, "device name %s\n", name);
127 dev = class_name2dev(name);
129 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
130 GOTO(out, rc = -EINVAL);
133 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
140 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
143 struct obd_ioctl_data *data;
144 struct libcfs_debug_ioctl_data *debug_data;
145 struct obd_device *obd = NULL;
146 int err = 0, len = 0;
149 /* only for debugging */
150 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
151 debug_data = (struct libcfs_debug_ioctl_data*)arg;
152 libcfs_subsystem_debug = debug_data->subs;
153 libcfs_debug = debug_data->debug;
157 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
158 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
159 CERROR("OBD ioctl: data error\n");
160 GOTO(out, err = -EINVAL);
162 data = (struct obd_ioctl_data *)buf;
165 case OBD_IOC_PROCESS_CFG: {
166 struct lustre_cfg *lcfg;
168 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
169 CERROR("No config buffer passed!\n");
170 GOTO(out, err = -EINVAL);
172 OBD_ALLOC(lcfg, data->ioc_plen1);
174 GOTO(out, err = -ENOMEM);
175 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
177 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
179 err = class_process_config(lcfg);
180 OBD_FREE(lcfg, data->ioc_plen1);
184 case OBD_GET_VERSION:
185 if (!data->ioc_inlbuf1) {
186 CERROR("No buffer passed in ioctl\n");
187 GOTO(out, err = -EINVAL);
190 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
191 CERROR("ioctl buffer too small to hold version\n");
192 GOTO(out, err = -EINVAL);
195 memcpy(data->ioc_bulk, BUILD_VERSION,
196 strlen(BUILD_VERSION) + 1);
198 err = obd_ioctl_popdata((void *)arg, data, len);
203 case OBD_IOC_NAME2DEV: {
204 /* Resolve a device name. This does not change the
205 * currently selected device.
209 dev = class_resolve_dev_name(data->ioc_inllen1,
213 GOTO(out, err = -EINVAL);
215 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
221 case OBD_IOC_UUID2DEV: {
222 /* Resolve a device uuid. This does not change the
223 * currently selected device.
226 struct obd_uuid uuid;
228 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
229 CERROR("No UUID passed!\n");
230 GOTO(out, err = -EINVAL);
232 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
233 CERROR("UUID not NUL terminated!\n");
234 GOTO(out, err = -EINVAL);
237 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
238 obd_str2uuid(&uuid, data->ioc_inlbuf1);
239 dev = class_uuid2dev(&uuid);
242 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
244 GOTO(out, err = -EINVAL);
247 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
249 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
255 case OBD_IOC_CLOSE_UUID: {
256 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
261 case OBD_IOC_GETDEVICE: {
262 int index = data->ioc_count;
265 if (!data->ioc_inlbuf1) {
266 CERROR("No buffer passed in ioctl\n");
267 GOTO(out, err = -EINVAL);
269 if (data->ioc_inllen1 < 128) {
270 CERROR("ioctl buffer too small to hold version\n");
271 GOTO(out, err = -EINVAL);
274 obd = class_num2obd(index);
276 GOTO(out, err = -ENOENT);
278 if (obd->obd_stopping)
280 else if (obd->obd_set_up)
282 else if (obd->obd_attached)
286 str = (char *)data->ioc_bulk;
287 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
288 (int)index, status, obd->obd_type->typ_name,
289 obd->obd_name, obd->obd_uuid.uuid,
290 atomic_read(&obd->obd_refcount));
291 err = obd_ioctl_popdata((void *)arg, data, len);
298 if (data->ioc_dev >= class_devno_max()) {
299 CERROR("OBD ioctl: No device\n");
300 GOTO(out, err = -EINVAL);
303 obd = class_num2obd(data->ioc_dev);
305 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
306 GOTO(out, err = -EINVAL);
308 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
310 if (!obd->obd_set_up || obd->obd_stopping) {
311 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
312 GOTO(out, err = -EINVAL);
316 case OBD_IOC_NO_TRANSNO: {
317 if (!obd->obd_attached) {
318 CERROR("Device %d not attached\n", obd->obd_minor);
319 GOTO(out, err = -ENODEV);
321 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
323 obd->obd_no_transno = 1;
328 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
332 err = obd_ioctl_popdata((void *)arg, data, len);
341 obd_ioctl_freedata(buf, len);
343 } /* class_handle_ioctl */
347 #define OBD_MINOR 241
349 /* to control /dev/obd */
350 static int obd_class_ioctl (struct cfs_psdev_file *pfile, unsigned long cmd,
353 return class_handle_ioctl(cmd, (unsigned long)arg);
356 /* declare character device */
357 struct cfs_psdev_ops obd_psdev_ops = {
358 /* .p_open = */ obd_class_open, /* open */
359 /* .p_close = */ obd_class_release, /* release */
360 /* .p_read = */ NULL,
361 /* .p_write = */ NULL,
362 /* .p_ioctl = */ obd_class_ioctl /* ioctl */
365 extern cfs_psdev_t obd_psdev;
367 void *obd_psdev = NULL;
370 EXPORT_SYMBOL(obd_devs);
371 EXPORT_SYMBOL(obd_print_fail_loc);
372 EXPORT_SYMBOL(obd_race_waitq);
373 EXPORT_SYMBOL(obd_race_state);
374 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
375 EXPORT_SYMBOL(obd_dump_on_timeout);
376 EXPORT_SYMBOL(obd_dump_on_eviction);
377 EXPORT_SYMBOL(obd_timeout);
378 EXPORT_SYMBOL(ldlm_timeout);
379 EXPORT_SYMBOL(obd_max_dirty_pages);
380 EXPORT_SYMBOL(obd_dirty_pages);
381 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
383 EXPORT_SYMBOL(proc_lustre_root);
385 EXPORT_SYMBOL(class_register_type);
386 EXPORT_SYMBOL(class_unregister_type);
387 EXPORT_SYMBOL(class_get_type);
388 EXPORT_SYMBOL(class_put_type);
389 EXPORT_SYMBOL(class_name2dev);
390 EXPORT_SYMBOL(class_name2obd);
391 EXPORT_SYMBOL(class_uuid2dev);
392 EXPORT_SYMBOL(class_uuid2obd);
393 EXPORT_SYMBOL(class_find_client_obd);
394 EXPORT_SYMBOL(class_find_client_notype);
395 EXPORT_SYMBOL(class_devices_in_group);
396 EXPORT_SYMBOL(class_conn2export);
397 EXPORT_SYMBOL(class_exp2obd);
398 EXPORT_SYMBOL(class_conn2obd);
399 EXPORT_SYMBOL(class_exp2cliimp);
400 EXPORT_SYMBOL(class_conn2cliimp);
401 EXPORT_SYMBOL(class_disconnect);
402 EXPORT_SYMBOL(class_num2obd);
405 EXPORT_SYMBOL(class_uuid_unparse);
406 EXPORT_SYMBOL(lustre_uuid_to_peer);
408 EXPORT_SYMBOL(class_handle_hash);
409 EXPORT_SYMBOL(class_handle_unhash);
410 EXPORT_SYMBOL(class_handle2object);
411 EXPORT_SYMBOL(class_handle_free_cb);
414 EXPORT_SYMBOL(class_incref);
415 EXPORT_SYMBOL(class_decref);
416 EXPORT_SYMBOL(class_get_profile);
417 EXPORT_SYMBOL(class_del_profile);
418 EXPORT_SYMBOL(class_del_profiles);
419 EXPORT_SYMBOL(class_process_config);
420 EXPORT_SYMBOL(class_process_proc_param);
421 EXPORT_SYMBOL(class_config_parse_llog);
422 EXPORT_SYMBOL(class_config_dump_llog);
423 EXPORT_SYMBOL(class_attach);
424 EXPORT_SYMBOL(class_setup);
425 EXPORT_SYMBOL(class_cleanup);
426 EXPORT_SYMBOL(class_detach);
427 EXPORT_SYMBOL(class_manual_cleanup);
429 #define OBD_INIT_CHECK
430 #ifdef OBD_INIT_CHECK
431 int obd_init_checks(void)
433 __u64 u64val, div64val;
437 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
438 LPU64, LPD64, LPX64, LPSZ, LPSSZ);
440 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
442 u64val = OBD_OBJECT_EOF;
443 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
444 if (u64val != OBD_OBJECT_EOF) {
445 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
446 u64val, (int)sizeof(u64val));
449 len = snprintf(buf, sizeof(buf), LPX64, u64val);
451 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
455 div64val = OBD_OBJECT_EOF;
456 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
457 if (u64val != OBD_OBJECT_EOF) {
458 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
459 u64val, (int)sizeof(u64val));
462 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
463 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
464 u64val, (int)sizeof(u64val));
467 if (do_div(div64val, 256) != (u64val & 255)) {
468 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
471 if (u64val >> 8 != div64val) {
472 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
473 u64val, div64val, u64val >> 8);
476 len = snprintf(buf, sizeof(buf), LPX64, u64val);
478 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
481 len = snprintf(buf, sizeof(buf), LPU64, u64val);
483 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
486 len = snprintf(buf, sizeof(buf), LPD64, u64val);
488 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
491 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
492 CWARN("mask failed: u64val "LPU64" >= %lu\n", u64val,
493 (unsigned long)CFS_PAGE_SIZE);
500 #define obd_init_checks() do {} while(0)
503 extern spinlock_t obd_types_lock;
504 extern int class_procfs_init(void);
505 extern int class_procfs_clean(void);
508 static int __init init_obdclass(void)
510 int init_obdclass(void)
515 int lustre_register_fs(void);
517 printk(KERN_INFO "Lustre: OBD class driver, info@clusterfs.com\n");
518 printk(KERN_INFO " Lustre Version: "LUSTRE_VERSION_STRING"\n");
519 printk(KERN_INFO " Build Version: "BUILD_VERSION"\n");
521 CDEBUG(D_INFO, "Lustre: OBD class driver, info@clusterfs.com\n");
522 CDEBUG(D_INFO, " Lustre Version: "LUSTRE_VERSION_STRING"\n");
523 CDEBUG(D_INFO, " Build Version: "BUILD_VERSION"\n");
526 spin_lock_init(&obd_types_lock);
527 cfs_waitq_init(&obd_race_waitq);
528 obd_zombie_impexp_init();
530 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
531 LPROCFS_STATS_FLAG_PERCPU);
532 if (obd_memory == NULL) {
533 CERROR("kmalloc of 'obd_memory' failed\n");
537 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
538 LPROCFS_CNTR_AVGMINMAX,
540 lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
541 LPROCFS_CNTR_AVGMINMAX,
542 "pagesused", "pages");
544 err = obd_init_checks();
545 if (err == -EOVERFLOW)
548 class_init_uuidlist();
549 err = class_handle_init();
553 spin_lock_init(&obd_dev_lock);
554 CFS_INIT_LIST_HEAD(&obd_types);
556 err = cfs_psdev_register(&obd_psdev);
558 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
562 /* This struct is already zerod for us (static global) */
563 for (i = 0; i < class_devno_max(); i++)
566 /* Default the dirty page cache cap to 1/2 of system memory */
567 obd_max_dirty_pages = num_physpages / 2;
569 err = obd_init_caches();
573 err = class_procfs_init();
576 err = lustre_register_fs();
582 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
583 * ifdef to the end of the file to cover module and versioning goo.*/
585 static void cleanup_obdclass(void)
588 __u64 memory_leaked, pages_leaked;
589 __u64 memory_max, pages_max;
590 int lustre_unregister_fs(void);
593 lustre_unregister_fs();
595 cfs_psdev_deregister(&obd_psdev);
596 for (i = 0; i < class_devno_max(); i++) {
597 struct obd_device *obd = class_num2obd(i);
598 if (obd && obd->obd_set_up &&
599 OBT(obd) && OBP(obd, detach)) {
600 /* XXX should this call generic detach otherwise? */
601 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
602 OBP(obd, detach)(obd);
606 obd_cleanup_caches();
609 class_procfs_clean();
611 class_handle_cleanup();
612 class_exit_uuidlist();
613 obd_zombie_impexp_stop();
615 memory_leaked = obd_memory_sum();
616 pages_leaked = obd_pages_sum();
618 memory_max = obd_memory_max();
619 pages_max = obd_pages_max();
621 lprocfs_free_stats(&obd_memory);
622 CDEBUG((memory_leaked | pages_leaked) ? D_ERROR : D_INFO,
623 "obd_memory max: "LPU64", leaked: "LPU64" "
624 "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
625 memory_max, memory_leaked,
626 pages_max, pages_leaked);
631 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
632 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
633 MODULE_LICENSE("GPL");
635 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);