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 (c) 1999, 2010, Oracle and/or its affiliates. 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 <lnet/lnetctl.h>
50 #include <lustre_debug.h>
51 #include <lprocfs_status.h>
52 #include <lustre/lustre_build_version.h>
53 #include <libcfs/list.h>
54 #include "llog_internal.h"
57 /* liblustre workaround */
58 cfs_atomic_t libcfs_kmemory = {0};
61 struct obd_device *obd_devs[MAX_OBD_DEVICES];
63 cfs_spinlock_t obd_dev_lock = CFS_SPIN_LOCK_UNLOCKED;
66 __u64 obd_max_pages = 0;
67 __u64 obd_max_alloc = 0;
72 /* The following are visible and mutable through /proc/sys/lustre/. */
73 unsigned int obd_debug_peer_on_timeout;
74 unsigned int obd_dump_on_timeout;
75 unsigned int obd_dump_on_eviction;
76 unsigned int obd_max_dirty_pages = 256;
77 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
78 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
79 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
80 unsigned int at_min = 0;
81 unsigned int at_max = 600;
82 unsigned int at_history = 600;
83 int at_early_margin = 5;
86 cfs_atomic_t obd_dirty_pages;
87 cfs_atomic_t obd_dirty_transit_pages;
89 cfs_waitq_t obd_race_waitq;
93 unsigned long obd_print_fail_loc(void)
95 CWARN("obd_fail_loc = %lx\n", obd_fail_loc);
100 static inline void obd_data2conn(struct lustre_handle *conn,
101 struct obd_ioctl_data *data)
103 memset(conn, 0, sizeof *conn);
104 conn->cookie = data->ioc_cookie;
107 static inline void obd_conn2data(struct obd_ioctl_data *data,
108 struct lustre_handle *conn)
110 data->ioc_cookie = conn->cookie;
113 int class_resolve_dev_name(__u32 len, const char *name)
120 CERROR("No name passed,!\n");
121 GOTO(out, rc = -EINVAL);
123 if (name[len - 1] != 0) {
124 CERROR("Name not nul terminated!\n");
125 GOTO(out, rc = -EINVAL);
128 CDEBUG(D_IOCTL, "device name %s\n", name);
129 dev = class_name2dev(name);
131 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
132 GOTO(out, rc = -EINVAL);
135 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
142 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
145 struct obd_ioctl_data *data;
146 struct libcfs_debug_ioctl_data *debug_data;
147 struct obd_device *obd = NULL;
148 int err = 0, len = 0;
151 /* only for debugging */
152 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
153 debug_data = (struct libcfs_debug_ioctl_data*)arg;
154 libcfs_subsystem_debug = debug_data->subs;
155 libcfs_debug = debug_data->debug;
159 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
160 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
161 CERROR("OBD ioctl: data error\n");
164 data = (struct obd_ioctl_data *)buf;
167 case OBD_IOC_PROCESS_CFG: {
168 struct lustre_cfg *lcfg;
170 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
171 CERROR("No config buffer passed!\n");
172 GOTO(out, err = -EINVAL);
174 OBD_ALLOC(lcfg, data->ioc_plen1);
176 GOTO(out, err = -ENOMEM);
177 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
180 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
182 err = class_process_config(lcfg);
184 OBD_FREE(lcfg, data->ioc_plen1);
188 case OBD_GET_VERSION:
189 if (!data->ioc_inlbuf1) {
190 CERROR("No buffer passed in ioctl\n");
191 GOTO(out, err = -EINVAL);
194 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
195 CERROR("ioctl buffer too small to hold version\n");
196 GOTO(out, err = -EINVAL);
199 memcpy(data->ioc_bulk, BUILD_VERSION,
200 strlen(BUILD_VERSION) + 1);
202 err = obd_ioctl_popdata((void *)arg, data, len);
207 case OBD_IOC_NAME2DEV: {
208 /* Resolve a device name. This does not change the
209 * currently selected device.
213 dev = class_resolve_dev_name(data->ioc_inllen1,
217 GOTO(out, err = -EINVAL);
219 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
225 case OBD_IOC_UUID2DEV: {
226 /* Resolve a device uuid. This does not change the
227 * currently selected device.
230 struct obd_uuid uuid;
232 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
233 CERROR("No UUID passed!\n");
234 GOTO(out, err = -EINVAL);
236 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
237 CERROR("UUID not NUL terminated!\n");
238 GOTO(out, err = -EINVAL);
241 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
242 obd_str2uuid(&uuid, data->ioc_inlbuf1);
243 dev = class_uuid2dev(&uuid);
246 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
248 GOTO(out, err = -EINVAL);
251 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
253 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
259 case OBD_IOC_CLOSE_UUID: {
260 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
265 case OBD_IOC_GETDEVICE: {
266 int index = data->ioc_count;
270 if (!data->ioc_inlbuf1) {
271 CERROR("No buffer passed in ioctl\n");
272 GOTO(out, err = -EINVAL);
274 if (data->ioc_inllen1 < 128) {
275 CERROR("ioctl buffer too small to hold version\n");
276 GOTO(out, err = -EINVAL);
279 obd = class_num2obd(index);
281 GOTO(out, err = -ENOENT);
283 status = obd_dev_status(obd);
284 str = (char *)data->ioc_bulk;
285 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
286 (int)index, status, obd->obd_type->typ_name,
287 obd->obd_name, obd->obd_uuid.uuid,
288 cfs_atomic_read(&obd->obd_refcount));
289 err = obd_ioctl_popdata((void *)arg, data, len);
296 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
297 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
298 GOTO(out, err = -EINVAL);
299 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
300 GOTO(out, err = -EINVAL);
301 obd = class_name2obd(data->ioc_inlbuf4);
302 } else if (obd_minor_valid(data->ioc_dev)) {
303 /* XXX = max allocated minor */
304 obd = class_num2obd(data->ioc_dev);
306 CERROR("OBD ioctl: No device\n");
307 GOTO(out, err = -EINVAL);
311 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
312 GOTO(out, err = -EINVAL);
314 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
316 if (!obd->obd_set_up || obd->obd_stopping) {
317 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
318 GOTO(out, err = -EINVAL);
322 case OBD_IOC_NO_TRANSNO: {
323 if (!obd->obd_attached) {
324 CERROR("Device %d not attached\n", obd->obd_minor);
325 GOTO(out, err = -ENODEV);
327 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
329 obd->obd_no_transno = 1;
334 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
338 err = obd_ioctl_popdata((void *)arg, data, len);
347 obd_ioctl_freedata(buf, len);
349 } /* class_handle_ioctl */
354 extern cfs_psdev_t obd_psdev;
356 void *obd_psdev = NULL;
359 EXPORT_SYMBOL(obd_devs);
360 EXPORT_SYMBOL(obd_print_fail_loc);
361 EXPORT_SYMBOL(obd_race_waitq);
362 EXPORT_SYMBOL(obd_race_state);
363 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
364 EXPORT_SYMBOL(obd_dump_on_timeout);
365 EXPORT_SYMBOL(obd_dump_on_eviction);
366 EXPORT_SYMBOL(obd_timeout);
367 EXPORT_SYMBOL(ldlm_timeout);
368 EXPORT_SYMBOL(obd_max_dirty_pages);
369 EXPORT_SYMBOL(obd_dirty_pages);
370 EXPORT_SYMBOL(obd_dirty_transit_pages);
371 EXPORT_SYMBOL(at_min);
372 EXPORT_SYMBOL(at_max);
373 EXPORT_SYMBOL(at_extra);
374 EXPORT_SYMBOL(at_early_margin);
375 EXPORT_SYMBOL(at_history);
376 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
378 EXPORT_SYMBOL(proc_lustre_root);
380 EXPORT_SYMBOL(class_register_type);
381 EXPORT_SYMBOL(class_unregister_type);
382 EXPORT_SYMBOL(class_get_type);
383 EXPORT_SYMBOL(class_put_type);
384 EXPORT_SYMBOL(class_name2obd);
385 EXPORT_SYMBOL(class_uuid2obd);
386 EXPORT_SYMBOL(class_find_client_obd);
387 EXPORT_SYMBOL(class_devices_in_group);
388 EXPORT_SYMBOL(class_conn2export);
389 EXPORT_SYMBOL(class_exp2obd);
390 EXPORT_SYMBOL(class_conn2obd);
391 EXPORT_SYMBOL(class_exp2cliimp);
392 EXPORT_SYMBOL(class_conn2cliimp);
393 EXPORT_SYMBOL(class_disconnect);
394 EXPORT_SYMBOL(class_num2obd);
397 EXPORT_SYMBOL(class_uuid_unparse);
398 EXPORT_SYMBOL(lustre_uuid_to_peer);
400 EXPORT_SYMBOL(class_handle_hash);
401 EXPORT_SYMBOL(class_handle_unhash);
402 EXPORT_SYMBOL(class_handle_hash_back);
403 EXPORT_SYMBOL(class_handle2object);
404 EXPORT_SYMBOL(class_handle_free_cb);
407 EXPORT_SYMBOL(class_incref);
408 EXPORT_SYMBOL(class_decref);
409 EXPORT_SYMBOL(class_get_profile);
410 EXPORT_SYMBOL(class_del_profile);
411 EXPORT_SYMBOL(class_del_profiles);
412 EXPORT_SYMBOL(class_process_config);
413 EXPORT_SYMBOL(class_process_proc_param);
414 EXPORT_SYMBOL(class_config_parse_llog);
415 EXPORT_SYMBOL(class_config_dump_llog);
416 EXPORT_SYMBOL(class_attach);
417 EXPORT_SYMBOL(class_setup);
418 EXPORT_SYMBOL(class_cleanup);
419 EXPORT_SYMBOL(class_detach);
420 EXPORT_SYMBOL(class_manual_cleanup);
423 EXPORT_SYMBOL(mea_name2idx);
424 EXPORT_SYMBOL(raw_name2idx);
426 #define OBD_INIT_CHECK
427 #ifdef OBD_INIT_CHECK
428 int obd_init_checks(void)
430 __u64 u64val, div64val;
434 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
436 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
438 u64val = OBD_OBJECT_EOF;
439 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
440 if (u64val != OBD_OBJECT_EOF) {
441 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
442 u64val, (int)sizeof(u64val));
445 len = snprintf(buf, sizeof(buf), LPX64, u64val);
447 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
451 div64val = OBD_OBJECT_EOF;
452 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
453 if (u64val != OBD_OBJECT_EOF) {
454 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
455 u64val, (int)sizeof(u64val));
458 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
459 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
460 u64val, (int)sizeof(u64val));
463 if (do_div(div64val, 256) != (u64val & 255)) {
464 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
467 if (u64val >> 8 != div64val) {
468 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
469 u64val, div64val, u64val >> 8);
472 len = snprintf(buf, sizeof(buf), LPX64, u64val);
474 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
477 len = snprintf(buf, sizeof(buf), LPU64, u64val);
479 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
482 len = snprintf(buf, sizeof(buf), LPD64, u64val);
484 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
487 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
488 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
489 (__u64)CFS_PAGE_SIZE);
496 #define obd_init_checks() do {} while(0)
499 extern cfs_spinlock_t obd_types_lock;
500 extern int class_procfs_init(void);
501 extern int class_procfs_clean(void);
504 static int __init init_obdclass(void)
506 int init_obdclass(void)
512 int lustre_register_fs(void);
514 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
515 CFS_INIT_LIST_HEAD(&capa_list[i]);
519 LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
521 LCONSOLE_INFO("OBD class driver, http://www.lustre.org/\n");
522 LCONSOLE_INFO(" Lustre Version: "LUSTRE_VERSION_STRING"\n");
523 LCONSOLE_INFO(" Build Version: "BUILD_VERSION"\n");
526 cfs_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_NONE);
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 cfs_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_DEV_MINOR, err);
563 /* Default the dirty page cache cap to 1/2 of system memory.
564 * For clients with less memory, a larger fraction is needed
565 * for other purposes (mostly for BGL). */
566 if (cfs_num_physpages <= 512 << (20 - CFS_PAGE_SHIFT))
567 obd_max_dirty_pages = cfs_num_physpages / 4;
569 obd_max_dirty_pages = cfs_num_physpages / 2;
571 err = obd_init_caches();
575 err = class_procfs_init();
580 err = lu_global_init();
585 err = lustre_register_fs();
589 err = obd_hashes_init();
596 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
597 * ifdef to the end of the file to cover module and versioning goo.*/
599 static void cleanup_obdclass(void)
601 int lustre_unregister_fs(void);
602 __u64 memory_leaked, pages_leaked;
603 __u64 memory_max, pages_max;
604 struct obd_device *obd;
607 lustre_unregister_fs();
609 cfs_psdev_deregister(&obd_psdev);
610 for (obd_devlist_first(&obd);
612 obd_devlist_next(&obd)) {
613 if (obd->obd_set_up &&
614 OBT(obd) && OBP(obd, detach)) {
615 /* XXX should this call generic detach otherwise? */
616 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
617 OBP(obd, detach)(obd);
624 obd_cleanup_caches();
627 class_procfs_clean();
629 class_handle_cleanup();
630 class_exit_uuidlist();
631 obd_zombie_impexp_stop();
633 memory_leaked = obd_memory_sum();
634 pages_leaked = obd_pages_sum();
636 memory_max = obd_memory_max();
637 pages_max = obd_pages_max();
639 lprocfs_free_stats(&obd_memory);
640 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
641 "obd_memory max: "LPU64", leaked: "LPU64"\n",
642 memory_max, memory_leaked);
643 CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
644 "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
645 pages_max, pages_leaked);
650 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
651 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
652 MODULE_LICENSE("GPL");
654 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);