4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
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_rwlock_t obd_dev_lock = CFS_RW_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 static inline void obd_data2conn(struct lustre_handle *conn,
90 struct obd_ioctl_data *data)
92 memset(conn, 0, sizeof *conn);
93 conn->cookie = data->ioc_cookie;
96 static inline void obd_conn2data(struct obd_ioctl_data *data,
97 struct lustre_handle *conn)
99 data->ioc_cookie = conn->cookie;
102 int class_resolve_dev_name(__u32 len, const char *name)
109 CERROR("No name passed,!\n");
110 GOTO(out, rc = -EINVAL);
112 if (name[len - 1] != 0) {
113 CERROR("Name not nul terminated!\n");
114 GOTO(out, rc = -EINVAL);
117 CDEBUG(D_IOCTL, "device name %s\n", name);
118 dev = class_name2dev(name);
120 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
121 GOTO(out, rc = -EINVAL);
124 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
131 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
134 struct obd_ioctl_data *data;
135 struct libcfs_debug_ioctl_data *debug_data;
136 struct obd_device *obd = NULL;
137 int err = 0, len = 0;
140 /* only for debugging */
141 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
142 debug_data = (struct libcfs_debug_ioctl_data*)arg;
143 libcfs_subsystem_debug = debug_data->subs;
144 libcfs_debug = debug_data->debug;
148 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
149 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
150 CERROR("OBD ioctl: data error\n");
153 data = (struct obd_ioctl_data *)buf;
156 case OBD_IOC_PROCESS_CFG: {
157 struct lustre_cfg *lcfg;
159 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
160 CERROR("No config buffer passed!\n");
161 GOTO(out, err = -EINVAL);
163 OBD_ALLOC(lcfg, data->ioc_plen1);
165 GOTO(out, err = -ENOMEM);
166 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
169 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
171 err = class_process_config(lcfg);
173 OBD_FREE(lcfg, data->ioc_plen1);
177 case OBD_GET_VERSION:
178 if (!data->ioc_inlbuf1) {
179 CERROR("No buffer passed in ioctl\n");
180 GOTO(out, err = -EINVAL);
183 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
184 CERROR("ioctl buffer too small to hold version\n");
185 GOTO(out, err = -EINVAL);
188 memcpy(data->ioc_bulk, BUILD_VERSION,
189 strlen(BUILD_VERSION) + 1);
191 err = obd_ioctl_popdata((void *)arg, data, len);
196 case OBD_IOC_NAME2DEV: {
197 /* Resolve a device name. This does not change the
198 * currently selected device.
202 dev = class_resolve_dev_name(data->ioc_inllen1,
206 GOTO(out, err = -EINVAL);
208 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
214 case OBD_IOC_UUID2DEV: {
215 /* Resolve a device uuid. This does not change the
216 * currently selected device.
219 struct obd_uuid uuid;
221 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
222 CERROR("No UUID passed!\n");
223 GOTO(out, err = -EINVAL);
225 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
226 CERROR("UUID not NUL terminated!\n");
227 GOTO(out, err = -EINVAL);
230 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
231 obd_str2uuid(&uuid, data->ioc_inlbuf1);
232 dev = class_uuid2dev(&uuid);
235 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
237 GOTO(out, err = -EINVAL);
240 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
242 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
248 case OBD_IOC_CLOSE_UUID: {
249 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
254 case OBD_IOC_GETDEVICE: {
255 int index = data->ioc_count;
258 if (!data->ioc_inlbuf1) {
259 CERROR("No buffer passed in ioctl\n");
260 GOTO(out, err = -EINVAL);
262 if (data->ioc_inllen1 < 128) {
263 CERROR("ioctl buffer too small to hold version\n");
264 GOTO(out, err = -EINVAL);
267 obd = class_num2obd(index);
269 GOTO(out, err = -ENOENT);
271 if (obd->obd_stopping)
273 else if (obd->obd_set_up)
275 else if (obd->obd_attached)
279 str = (char *)data->ioc_bulk;
280 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
281 (int)index, status, obd->obd_type->typ_name,
282 obd->obd_name, obd->obd_uuid.uuid,
283 cfs_atomic_read(&obd->obd_refcount));
284 err = obd_ioctl_popdata((void *)arg, data, len);
291 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
292 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
293 GOTO(out, err = -EINVAL);
294 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
295 GOTO(out, err = -EINVAL);
296 obd = class_name2obd(data->ioc_inlbuf4);
297 } else if (data->ioc_dev < class_devno_max()) {
298 obd = class_num2obd(data->ioc_dev);
300 CERROR("OBD ioctl: No device\n");
301 GOTO(out, err = -EINVAL);
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 */
348 extern cfs_psdev_t obd_psdev;
350 void *obd_psdev = NULL;
353 EXPORT_SYMBOL(obd_devs);
354 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
355 EXPORT_SYMBOL(obd_dump_on_timeout);
356 EXPORT_SYMBOL(obd_dump_on_eviction);
357 EXPORT_SYMBOL(obd_timeout);
358 EXPORT_SYMBOL(ldlm_timeout);
359 EXPORT_SYMBOL(obd_max_dirty_pages);
360 EXPORT_SYMBOL(obd_dirty_pages);
361 EXPORT_SYMBOL(obd_dirty_transit_pages);
362 EXPORT_SYMBOL(at_min);
363 EXPORT_SYMBOL(at_max);
364 EXPORT_SYMBOL(at_extra);
365 EXPORT_SYMBOL(at_early_margin);
366 EXPORT_SYMBOL(at_history);
367 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
369 EXPORT_SYMBOL(proc_lustre_root);
372 EXPORT_SYMBOL(class_uuid_unparse);
373 EXPORT_SYMBOL(lustre_uuid_to_peer);
375 EXPORT_SYMBOL(class_handle_hash);
376 EXPORT_SYMBOL(class_handle_unhash);
377 EXPORT_SYMBOL(class_handle_hash_back);
378 EXPORT_SYMBOL(class_handle2object);
379 EXPORT_SYMBOL(class_handle_free_cb);
382 EXPORT_SYMBOL(class_incref);
383 EXPORT_SYMBOL(class_decref);
384 EXPORT_SYMBOL(class_get_profile);
385 EXPORT_SYMBOL(class_del_profile);
386 EXPORT_SYMBOL(class_del_profiles);
387 EXPORT_SYMBOL(class_process_config);
388 EXPORT_SYMBOL(class_process_proc_param);
389 EXPORT_SYMBOL(class_config_parse_llog);
390 EXPORT_SYMBOL(class_config_dump_llog);
391 EXPORT_SYMBOL(class_attach);
392 EXPORT_SYMBOL(class_setup);
393 EXPORT_SYMBOL(class_cleanup);
394 EXPORT_SYMBOL(class_detach);
395 EXPORT_SYMBOL(class_manual_cleanup);
398 EXPORT_SYMBOL(mea_name2idx);
399 EXPORT_SYMBOL(raw_name2idx);
401 #define OBD_INIT_CHECK
402 #ifdef OBD_INIT_CHECK
403 int obd_init_checks(void)
405 __u64 u64val, div64val;
409 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
411 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
413 u64val = OBD_OBJECT_EOF;
414 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
415 if (u64val != OBD_OBJECT_EOF) {
416 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
417 u64val, (int)sizeof(u64val));
420 len = snprintf(buf, sizeof(buf), LPX64, u64val);
422 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
426 div64val = OBD_OBJECT_EOF;
427 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
428 if (u64val != OBD_OBJECT_EOF) {
429 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
430 u64val, (int)sizeof(u64val));
433 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
434 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
435 u64val, (int)sizeof(u64val));
438 if (do_div(div64val, 256) != (u64val & 255)) {
439 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
442 if (u64val >> 8 != div64val) {
443 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
444 u64val, div64val, u64val >> 8);
447 len = snprintf(buf, sizeof(buf), LPX64, u64val);
449 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
452 len = snprintf(buf, sizeof(buf), LPU64, u64val);
454 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
457 len = snprintf(buf, sizeof(buf), LPD64, u64val);
459 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
462 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
463 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
464 (__u64)CFS_PAGE_SIZE);
471 #define obd_init_checks() do {} while(0)
474 extern cfs_spinlock_t obd_types_lock;
475 extern int class_procfs_init(void);
476 extern int class_procfs_clean(void);
479 static int __init init_obdclass(void)
481 int init_obdclass(void)
486 int lustre_register_fs(void);
488 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
489 CFS_INIT_LIST_HEAD(&capa_list[i]);
492 LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
494 cfs_spin_lock_init(&obd_types_lock);
495 obd_zombie_impexp_init();
497 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
498 LPROCFS_STATS_FLAG_NONE);
499 if (obd_memory == NULL) {
500 CERROR("kmalloc of 'obd_memory' failed\n");
504 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
505 LPROCFS_CNTR_AVGMINMAX,
507 lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
508 LPROCFS_CNTR_AVGMINMAX,
509 "pagesused", "pages");
511 err = obd_init_checks();
512 if (err == -EOVERFLOW)
515 class_init_uuidlist();
516 err = class_handle_init();
520 CFS_INIT_LIST_HEAD(&obd_types);
522 err = cfs_psdev_register(&obd_psdev);
524 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
528 /* This struct is already zeroed for us (static global) */
529 for (i = 0; i < class_devno_max(); i++)
532 /* Default the dirty page cache cap to 1/2 of system memory.
533 * For clients with less memory, a larger fraction is needed
534 * for other purposes (mostly for BGL). */
535 if (cfs_num_physpages <= 512 << (20 - CFS_PAGE_SHIFT))
536 obd_max_dirty_pages = cfs_num_physpages / 4;
538 obd_max_dirty_pages = cfs_num_physpages / 2;
540 err = obd_init_caches();
544 err = class_procfs_init();
549 err = lu_global_init();
554 err = lustre_register_fs();
560 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
561 * ifdef to the end of the file to cover module and versioning goo.*/
563 static void cleanup_obdclass(void)
566 int lustre_unregister_fs(void);
567 __u64 memory_leaked, pages_leaked;
568 __u64 memory_max, pages_max;
571 lustre_unregister_fs();
573 cfs_psdev_deregister(&obd_psdev);
574 for (i = 0; i < class_devno_max(); i++) {
575 struct obd_device *obd = class_num2obd(i);
576 if (obd && obd->obd_set_up &&
577 OBT(obd) && OBP(obd, detach)) {
578 /* XXX should this call generic detach otherwise? */
579 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
580 OBP(obd, detach)(obd);
585 obd_cleanup_caches();
588 class_procfs_clean();
590 class_handle_cleanup();
591 class_exit_uuidlist();
592 obd_zombie_impexp_stop();
594 memory_leaked = obd_memory_sum();
595 pages_leaked = obd_pages_sum();
597 memory_max = obd_memory_max();
598 pages_max = obd_pages_max();
600 lprocfs_free_stats(&obd_memory);
601 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
602 "obd_memory max: "LPU64", leaked: "LPU64"\n",
603 memory_max, memory_leaked);
604 CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
605 "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
606 pages_max, pages_leaked);
611 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
612 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
613 MODULE_LICENSE("GPL");
615 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);