1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_CLASS
39 # define EXPORT_SYMTAB
42 # include <liblustre.h>
44 # include <asm/atomic.h>
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include <lustre_debug.h>
50 #include <lprocfs_status.h>
51 #include <lustre/lustre_build_version.h>
52 #include <libcfs/list.h>
53 #include "llog_internal.h"
56 /* liblustre workaround */
57 atomic_t libcfs_kmemory = {0};
60 struct obd_device *obd_devs[MAX_OBD_DEVICES];
61 struct list_head obd_types;
62 spinlock_t obd_dev_lock = SPIN_LOCK_UNLOCKED;
65 __u64 obd_max_pages = 0;
66 __u64 obd_max_alloc = 0;
71 /* The following are visible and mutable through /proc/sys/lustre/. */
72 unsigned int obd_debug_peer_on_timeout;
73 unsigned int obd_dump_on_timeout;
74 unsigned int obd_dump_on_eviction;
75 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
76 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
77 unsigned int obd_max_dirty_pages = 256;
78 atomic_t obd_dirty_pages;
79 atomic_t obd_dirty_transit_pages;
81 cfs_waitq_t obd_race_waitq;
85 unsigned long obd_print_fail_loc(void)
87 CWARN("obd_fail_loc = %lx\n", obd_fail_loc);
91 /* opening /dev/obd */
92 static int obd_class_open(unsigned long flags, void *args)
100 /* closing /dev/obd */
101 static int obd_class_release(unsigned long flags, void *args)
110 static inline void obd_data2conn(struct lustre_handle *conn,
111 struct obd_ioctl_data *data)
113 memset(conn, 0, sizeof *conn);
114 conn->cookie = data->ioc_cookie;
117 static inline void obd_conn2data(struct obd_ioctl_data *data,
118 struct lustre_handle *conn)
120 data->ioc_cookie = conn->cookie;
123 int class_resolve_dev_name(__u32 len, const char *name)
130 CERROR("No name passed,!\n");
131 GOTO(out, rc = -EINVAL);
133 if (name[len - 1] != 0) {
134 CERROR("Name not nul terminated!\n");
135 GOTO(out, rc = -EINVAL);
138 CDEBUG(D_IOCTL, "device name %s\n", name);
139 dev = class_name2dev(name);
141 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
142 GOTO(out, rc = -EINVAL);
145 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
152 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
155 struct obd_ioctl_data *data;
156 struct libcfs_debug_ioctl_data *debug_data;
157 struct obd_device *obd = NULL;
158 int err = 0, len = 0;
161 /* only for debugging */
162 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
163 debug_data = (struct libcfs_debug_ioctl_data*)arg;
164 libcfs_subsystem_debug = debug_data->subs;
165 libcfs_debug = debug_data->debug;
169 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
170 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
171 CERROR("OBD ioctl: data error\n");
172 GOTO(out, err = -EINVAL);
174 data = (struct obd_ioctl_data *)buf;
177 case OBD_IOC_PROCESS_CFG: {
178 struct lustre_cfg *lcfg;
180 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
181 CERROR("No config buffer passed!\n");
182 GOTO(out, err = -EINVAL);
184 OBD_ALLOC(lcfg, data->ioc_plen1);
186 GOTO(out, err = -ENOMEM);
187 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
189 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
191 err = class_process_config(lcfg);
193 OBD_FREE(lcfg, data->ioc_plen1);
197 case OBD_GET_VERSION:
198 if (!data->ioc_inlbuf1) {
199 CERROR("No buffer passed in ioctl\n");
200 GOTO(out, err = -EINVAL);
203 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
204 CERROR("ioctl buffer too small to hold version\n");
205 GOTO(out, err = -EINVAL);
208 memcpy(data->ioc_bulk, BUILD_VERSION,
209 strlen(BUILD_VERSION) + 1);
211 err = obd_ioctl_popdata((void *)arg, data, len);
216 case OBD_IOC_NAME2DEV: {
217 /* Resolve a device name. This does not change the
218 * currently selected device.
222 dev = class_resolve_dev_name(data->ioc_inllen1,
226 GOTO(out, err = -EINVAL);
228 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
234 case OBD_IOC_UUID2DEV: {
235 /* Resolve a device uuid. This does not change the
236 * currently selected device.
239 struct obd_uuid uuid;
241 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
242 CERROR("No UUID passed!\n");
243 GOTO(out, err = -EINVAL);
245 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
246 CERROR("UUID not NUL terminated!\n");
247 GOTO(out, err = -EINVAL);
250 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
251 obd_str2uuid(&uuid, data->ioc_inlbuf1);
252 dev = class_uuid2dev(&uuid);
255 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
257 GOTO(out, err = -EINVAL);
260 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
262 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
268 case OBD_IOC_CLOSE_UUID: {
269 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
274 case OBD_IOC_GETDEVICE: {
275 int index = data->ioc_count;
278 if (!data->ioc_inlbuf1) {
279 CERROR("No buffer passed in ioctl\n");
280 GOTO(out, err = -EINVAL);
282 if (data->ioc_inllen1 < 128) {
283 CERROR("ioctl buffer too small to hold version\n");
284 GOTO(out, err = -EINVAL);
287 obd = class_num2obd(index);
289 GOTO(out, err = -ENOENT);
291 if (obd->obd_stopping)
293 else if (obd->obd_set_up)
295 else if (obd->obd_attached)
299 str = (char *)data->ioc_bulk;
300 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
301 (int)index, status, obd->obd_type->typ_name,
302 obd->obd_name, obd->obd_uuid.uuid,
303 atomic_read(&obd->obd_refcount));
304 err = obd_ioctl_popdata((void *)arg, data, len);
311 if (data->ioc_dev >= class_devno_max()) {
312 CERROR("OBD ioctl: No device\n");
313 GOTO(out, err = -EINVAL);
316 obd = class_num2obd(data->ioc_dev);
318 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
319 GOTO(out, err = -EINVAL);
321 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
323 if (!obd->obd_set_up || obd->obd_stopping) {
324 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
325 GOTO(out, err = -EINVAL);
329 case OBD_IOC_NO_TRANSNO: {
330 if (!obd->obd_attached) {
331 CERROR("Device %d not attached\n", obd->obd_minor);
332 GOTO(out, err = -ENODEV);
334 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
336 obd->obd_no_transno = 1;
341 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
345 err = obd_ioctl_popdata((void *)arg, data, len);
354 obd_ioctl_freedata(buf, len);
356 } /* class_handle_ioctl */
360 #define OBD_MINOR 241
362 /* to control /dev/obd */
363 static int obd_class_ioctl (struct cfs_psdev_file *pfile, unsigned long cmd,
366 return class_handle_ioctl(cmd, (unsigned long)arg);
369 /* declare character device */
370 struct cfs_psdev_ops obd_psdev_ops = {
371 /* .p_open = */ obd_class_open, /* open */
372 /* .p_close = */ obd_class_release, /* release */
373 /* .p_read = */ NULL,
374 /* .p_write = */ NULL,
375 /* .p_ioctl = */ obd_class_ioctl /* ioctl */
378 extern cfs_psdev_t obd_psdev;
380 void *obd_psdev = NULL;
383 EXPORT_SYMBOL(obd_devs);
384 EXPORT_SYMBOL(obd_print_fail_loc);
385 EXPORT_SYMBOL(obd_race_waitq);
386 EXPORT_SYMBOL(obd_race_state);
387 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
388 EXPORT_SYMBOL(obd_dump_on_timeout);
389 EXPORT_SYMBOL(obd_dump_on_eviction);
390 EXPORT_SYMBOL(obd_timeout);
391 EXPORT_SYMBOL(ldlm_timeout);
392 EXPORT_SYMBOL(obd_max_dirty_pages);
393 EXPORT_SYMBOL(obd_dirty_pages);
394 EXPORT_SYMBOL(obd_dirty_transit_pages);
395 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
397 EXPORT_SYMBOL(proc_lustre_root);
399 EXPORT_SYMBOL(class_register_type);
400 EXPORT_SYMBOL(class_unregister_type);
401 EXPORT_SYMBOL(class_get_type);
402 EXPORT_SYMBOL(class_put_type);
403 EXPORT_SYMBOL(class_name2dev);
404 EXPORT_SYMBOL(class_name2obd);
405 EXPORT_SYMBOL(class_uuid2dev);
406 EXPORT_SYMBOL(class_uuid2obd);
407 EXPORT_SYMBOL(class_find_client_obd);
408 EXPORT_SYMBOL(class_find_client_notype);
409 EXPORT_SYMBOL(class_devices_in_group);
410 EXPORT_SYMBOL(class_conn2export);
411 EXPORT_SYMBOL(class_exp2obd);
412 EXPORT_SYMBOL(class_conn2obd);
413 EXPORT_SYMBOL(class_exp2cliimp);
414 EXPORT_SYMBOL(class_conn2cliimp);
415 EXPORT_SYMBOL(class_disconnect);
416 EXPORT_SYMBOL(class_num2obd);
419 EXPORT_SYMBOL(class_uuid_unparse);
420 EXPORT_SYMBOL(lustre_uuid_to_peer);
422 EXPORT_SYMBOL(class_handle_hash);
423 EXPORT_SYMBOL(class_handle_unhash);
424 EXPORT_SYMBOL(class_handle_hash_back);
425 EXPORT_SYMBOL(class_handle2object);
426 EXPORT_SYMBOL(class_handle_free_cb);
429 EXPORT_SYMBOL(class_incref);
430 EXPORT_SYMBOL(class_decref);
431 EXPORT_SYMBOL(class_get_profile);
432 EXPORT_SYMBOL(class_del_profile);
433 EXPORT_SYMBOL(class_del_profiles);
434 EXPORT_SYMBOL(class_process_config);
435 EXPORT_SYMBOL(class_process_proc_param);
436 EXPORT_SYMBOL(class_config_parse_llog);
437 EXPORT_SYMBOL(class_config_dump_llog);
438 EXPORT_SYMBOL(class_attach);
439 EXPORT_SYMBOL(class_setup);
440 EXPORT_SYMBOL(class_cleanup);
441 EXPORT_SYMBOL(class_detach);
442 EXPORT_SYMBOL(class_manual_cleanup);
445 EXPORT_SYMBOL(mea_name2idx);
446 EXPORT_SYMBOL(raw_name2idx);
448 #define OBD_INIT_CHECK
449 #ifdef OBD_INIT_CHECK
450 int obd_init_checks(void)
452 __u64 u64val, div64val;
456 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
457 LPU64, LPD64, LPX64, LPSZ, LPSSZ);
459 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
461 u64val = OBD_OBJECT_EOF;
462 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
463 if (u64val != OBD_OBJECT_EOF) {
464 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
465 u64val, (int)sizeof(u64val));
468 len = snprintf(buf, sizeof(buf), LPX64, u64val);
470 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
474 div64val = OBD_OBJECT_EOF;
475 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
476 if (u64val != OBD_OBJECT_EOF) {
477 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
478 u64val, (int)sizeof(u64val));
481 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
482 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
483 u64val, (int)sizeof(u64val));
486 if (do_div(div64val, 256) != (u64val & 255)) {
487 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
490 if (u64val >> 8 != div64val) {
491 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
492 u64val, div64val, u64val >> 8);
495 len = snprintf(buf, sizeof(buf), LPX64, u64val);
497 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
500 len = snprintf(buf, sizeof(buf), LPU64, u64val);
502 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
505 len = snprintf(buf, sizeof(buf), LPD64, u64val);
507 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
510 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
511 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
512 (__u64)CFS_PAGE_SIZE);
519 #define obd_init_checks() do {} while(0)
522 extern spinlock_t obd_types_lock;
523 extern int class_procfs_init(void);
524 extern int class_procfs_clean(void);
527 static int __init init_obdclass(void)
529 int init_obdclass(void)
534 int lustre_register_fs(void);
536 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
537 CFS_INIT_LIST_HEAD(&capa_list[i]);
540 LCONSOLE_INFO("OBD class driver, http://www.lustre.org/\n");
541 LCONSOLE_INFO(" Lustre Version: "LUSTRE_VERSION_STRING"\n");
542 LCONSOLE_INFO(" Build Version: "BUILD_VERSION"\n");
544 spin_lock_init(&obd_types_lock);
545 cfs_waitq_init(&obd_race_waitq);
546 obd_zombie_impexp_init();
548 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
549 LPROCFS_STATS_FLAG_PERCPU);
550 if (obd_memory == NULL) {
551 CERROR("kmalloc of 'obd_memory' failed\n");
555 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
556 LPROCFS_CNTR_AVGMINMAX,
558 lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
559 LPROCFS_CNTR_AVGMINMAX,
560 "pagesused", "pages");
562 err = obd_init_checks();
563 if (err == -EOVERFLOW)
566 class_init_uuidlist();
567 err = class_handle_init();
571 spin_lock_init(&obd_dev_lock);
572 CFS_INIT_LIST_HEAD(&obd_types);
574 err = cfs_psdev_register(&obd_psdev);
576 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
580 /* This struct is already zeroed for us (static global) */
581 for (i = 0; i < class_devno_max(); i++)
584 /* Default the dirty page cache cap to 1/2 of system memory.
585 * For clients with less memory, a larger fraction is needed
586 * for other purposes (mostly for BGL). */
587 if (num_physpages <= 512 << (20 - CFS_PAGE_SHIFT))
588 obd_max_dirty_pages = num_physpages / 4;
590 obd_max_dirty_pages = num_physpages / 2;
592 err = obd_init_caches();
595 err = lu_global_init();
599 err = class_procfs_init();
602 err = lustre_register_fs();
608 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
609 * ifdef to the end of the file to cover module and versioning goo.*/
611 static void cleanup_obdclass(void)
614 int lustre_unregister_fs(void);
615 __u64 memory_leaked, pages_leaked;
616 __u64 memory_max, pages_max;
619 lustre_unregister_fs();
621 cfs_psdev_deregister(&obd_psdev);
622 for (i = 0; i < class_devno_max(); i++) {
623 struct obd_device *obd = class_num2obd(i);
624 if (obd && obd->obd_set_up &&
625 OBT(obd) && OBP(obd, detach)) {
626 /* XXX should this call generic detach otherwise? */
627 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
628 OBP(obd, detach)(obd);
633 obd_cleanup_caches();
636 class_procfs_clean();
638 class_handle_cleanup();
639 class_exit_uuidlist();
640 obd_zombie_impexp_stop();
642 memory_leaked = obd_memory_sum();
643 pages_leaked = obd_pages_sum();
645 memory_max = obd_memory_max();
646 pages_max = obd_pages_max();
648 lprocfs_free_stats(&obd_memory);
649 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
650 "obd_memory max: "LPU64", leaked: "LPU64"\n",
651 memory_max, memory_leaked);
652 CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
653 "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
654 pages_max, pages_leaked);
659 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
660 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
661 MODULE_LICENSE("GPL");
663 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);