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.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_CLASS
41 # define EXPORT_SYMTAB
44 # include <liblustre.h>
46 # include <asm/atomic.h>
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <lnet/lnetctl.h>
52 #include <lustre_debug.h>
53 #include <lprocfs_status.h>
54 #include <lustre/lustre_build_version.h>
55 #include <libcfs/list.h>
56 #include "llog_internal.h"
59 /* liblustre workaround */
60 cfs_atomic_t libcfs_kmemory = {0};
63 struct obd_device *obd_devs[MAX_OBD_DEVICES];
65 cfs_rwlock_t obd_dev_lock = CFS_RW_LOCK_UNLOCKED;
68 __u64 obd_max_pages = 0;
69 __u64 obd_max_alloc = 0;
74 /* The following are visible and mutable through /proc/sys/lustre/. */
75 unsigned int obd_debug_peer_on_timeout;
76 unsigned int obd_dump_on_timeout;
77 unsigned int obd_dump_on_eviction;
78 unsigned int obd_max_dirty_pages = 256;
79 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
80 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
81 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
82 unsigned int at_min = 0;
83 unsigned int at_max = 600;
84 unsigned int at_history = 600;
85 int at_early_margin = 5;
88 cfs_atomic_t obd_dirty_pages;
89 cfs_atomic_t obd_dirty_transit_pages;
91 static inline void obd_data2conn(struct lustre_handle *conn,
92 struct obd_ioctl_data *data)
94 memset(conn, 0, sizeof *conn);
95 conn->cookie = data->ioc_cookie;
98 static inline void obd_conn2data(struct obd_ioctl_data *data,
99 struct lustre_handle *conn)
101 data->ioc_cookie = conn->cookie;
104 int class_resolve_dev_name(__u32 len, const char *name)
111 CERROR("No name passed,!\n");
112 GOTO(out, rc = -EINVAL);
114 if (name[len - 1] != 0) {
115 CERROR("Name not nul terminated!\n");
116 GOTO(out, rc = -EINVAL);
119 CDEBUG(D_IOCTL, "device name %s\n", name);
120 dev = class_name2dev(name);
122 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
123 GOTO(out, rc = -EINVAL);
126 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
133 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
136 struct obd_ioctl_data *data;
137 struct libcfs_debug_ioctl_data *debug_data;
138 struct obd_device *obd = NULL;
139 int err = 0, len = 0;
142 /* only for debugging */
143 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
144 debug_data = (struct libcfs_debug_ioctl_data*)arg;
145 libcfs_subsystem_debug = debug_data->subs;
146 libcfs_debug = debug_data->debug;
150 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
151 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
152 CERROR("OBD ioctl: data error\n");
155 data = (struct obd_ioctl_data *)buf;
158 case OBD_IOC_PROCESS_CFG: {
159 struct lustre_cfg *lcfg;
161 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
162 CERROR("No config buffer passed!\n");
163 GOTO(out, err = -EINVAL);
165 OBD_ALLOC(lcfg, data->ioc_plen1);
167 GOTO(out, err = -ENOMEM);
168 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
171 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
173 err = class_process_config(lcfg);
175 OBD_FREE(lcfg, data->ioc_plen1);
179 case OBD_GET_VERSION:
180 if (!data->ioc_inlbuf1) {
181 CERROR("No buffer passed in ioctl\n");
182 GOTO(out, err = -EINVAL);
185 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
186 CERROR("ioctl buffer too small to hold version\n");
187 GOTO(out, err = -EINVAL);
190 memcpy(data->ioc_bulk, BUILD_VERSION,
191 strlen(BUILD_VERSION) + 1);
193 err = obd_ioctl_popdata((void *)arg, data, len);
198 case OBD_IOC_NAME2DEV: {
199 /* Resolve a device name. This does not change the
200 * currently selected device.
204 dev = class_resolve_dev_name(data->ioc_inllen1,
208 GOTO(out, err = -EINVAL);
210 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
216 case OBD_IOC_UUID2DEV: {
217 /* Resolve a device uuid. This does not change the
218 * currently selected device.
221 struct obd_uuid uuid;
223 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
224 CERROR("No UUID passed!\n");
225 GOTO(out, err = -EINVAL);
227 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
228 CERROR("UUID not NUL terminated!\n");
229 GOTO(out, err = -EINVAL);
232 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
233 obd_str2uuid(&uuid, data->ioc_inlbuf1);
234 dev = class_uuid2dev(&uuid);
237 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
239 GOTO(out, err = -EINVAL);
242 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
244 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
250 case OBD_IOC_CLOSE_UUID: {
251 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
256 case OBD_IOC_GETDEVICE: {
257 int index = data->ioc_count;
260 if (!data->ioc_inlbuf1) {
261 CERROR("No buffer passed in ioctl\n");
262 GOTO(out, err = -EINVAL);
264 if (data->ioc_inllen1 < 128) {
265 CERROR("ioctl buffer too small to hold version\n");
266 GOTO(out, err = -EINVAL);
269 obd = class_num2obd(index);
271 GOTO(out, err = -ENOENT);
273 if (obd->obd_stopping)
275 else if (obd->obd_set_up)
277 else if (obd->obd_attached)
281 str = (char *)data->ioc_bulk;
282 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
283 (int)index, status, obd->obd_type->typ_name,
284 obd->obd_name, obd->obd_uuid.uuid,
285 cfs_atomic_read(&obd->obd_refcount));
286 err = obd_ioctl_popdata((void *)arg, data, len);
293 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
294 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
295 GOTO(out, err = -EINVAL);
296 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
297 GOTO(out, err = -EINVAL);
298 obd = class_name2obd(data->ioc_inlbuf4);
299 } else if (data->ioc_dev < class_devno_max()) {
300 obd = class_num2obd(data->ioc_dev);
302 CERROR("OBD ioctl: No device\n");
303 GOTO(out, err = -EINVAL);
307 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
308 GOTO(out, err = -EINVAL);
310 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
312 if (!obd->obd_set_up || obd->obd_stopping) {
313 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
314 GOTO(out, err = -EINVAL);
318 case OBD_IOC_NO_TRANSNO: {
319 if (!obd->obd_attached) {
320 CERROR("Device %d not attached\n", obd->obd_minor);
321 GOTO(out, err = -ENODEV);
323 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
325 obd->obd_no_transno = 1;
330 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
334 err = obd_ioctl_popdata((void *)arg, data, len);
343 obd_ioctl_freedata(buf, len);
345 } /* class_handle_ioctl */
350 extern cfs_psdev_t obd_psdev;
352 void *obd_psdev = NULL;
355 EXPORT_SYMBOL(obd_devs);
356 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
357 EXPORT_SYMBOL(obd_dump_on_timeout);
358 EXPORT_SYMBOL(obd_dump_on_eviction);
359 EXPORT_SYMBOL(obd_timeout);
360 EXPORT_SYMBOL(ldlm_timeout);
361 EXPORT_SYMBOL(obd_max_dirty_pages);
362 EXPORT_SYMBOL(obd_dirty_pages);
363 EXPORT_SYMBOL(obd_dirty_transit_pages);
364 EXPORT_SYMBOL(at_min);
365 EXPORT_SYMBOL(at_max);
366 EXPORT_SYMBOL(at_extra);
367 EXPORT_SYMBOL(at_early_margin);
368 EXPORT_SYMBOL(at_history);
369 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
371 EXPORT_SYMBOL(proc_lustre_root);
374 EXPORT_SYMBOL(class_uuid_unparse);
375 EXPORT_SYMBOL(lustre_uuid_to_peer);
377 EXPORT_SYMBOL(class_handle_hash);
378 EXPORT_SYMBOL(class_handle_unhash);
379 EXPORT_SYMBOL(class_handle_hash_back);
380 EXPORT_SYMBOL(class_handle2object);
381 EXPORT_SYMBOL(class_handle_free_cb);
384 EXPORT_SYMBOL(class_incref);
385 EXPORT_SYMBOL(class_decref);
386 EXPORT_SYMBOL(class_get_profile);
387 EXPORT_SYMBOL(class_del_profile);
388 EXPORT_SYMBOL(class_del_profiles);
389 EXPORT_SYMBOL(class_process_config);
390 EXPORT_SYMBOL(class_process_proc_param);
391 EXPORT_SYMBOL(class_config_parse_llog);
392 EXPORT_SYMBOL(class_config_dump_llog);
393 EXPORT_SYMBOL(class_attach);
394 EXPORT_SYMBOL(class_setup);
395 EXPORT_SYMBOL(class_cleanup);
396 EXPORT_SYMBOL(class_detach);
397 EXPORT_SYMBOL(class_manual_cleanup);
400 EXPORT_SYMBOL(mea_name2idx);
401 EXPORT_SYMBOL(raw_name2idx);
403 #define OBD_INIT_CHECK
404 #ifdef OBD_INIT_CHECK
405 int obd_init_checks(void)
407 __u64 u64val, div64val;
411 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
413 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
415 u64val = OBD_OBJECT_EOF;
416 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
417 if (u64val != OBD_OBJECT_EOF) {
418 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
419 u64val, (int)sizeof(u64val));
422 len = snprintf(buf, sizeof(buf), LPX64, u64val);
424 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
428 div64val = OBD_OBJECT_EOF;
429 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
430 if (u64val != OBD_OBJECT_EOF) {
431 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
432 u64val, (int)sizeof(u64val));
435 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
436 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
437 u64val, (int)sizeof(u64val));
440 if (do_div(div64val, 256) != (u64val & 255)) {
441 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
444 if (u64val >> 8 != div64val) {
445 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
446 u64val, div64val, u64val >> 8);
449 len = snprintf(buf, sizeof(buf), LPX64, u64val);
451 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
454 len = snprintf(buf, sizeof(buf), LPU64, u64val);
456 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
459 len = snprintf(buf, sizeof(buf), LPD64, u64val);
461 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
464 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
465 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
466 (__u64)CFS_PAGE_SIZE);
473 #define obd_init_checks() do {} while(0)
476 extern cfs_spinlock_t obd_types_lock;
477 extern int class_procfs_init(void);
478 extern int class_procfs_clean(void);
481 static int __init init_obdclass(void)
483 int init_obdclass(void)
488 int lustre_register_fs(void);
490 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
491 CFS_INIT_LIST_HEAD(&capa_list[i]);
494 LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
496 cfs_spin_lock_init(&obd_types_lock);
497 obd_zombie_impexp_init();
499 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
500 LPROCFS_STATS_FLAG_NONE);
501 if (obd_memory == NULL) {
502 CERROR("kmalloc of 'obd_memory' failed\n");
506 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
507 LPROCFS_CNTR_AVGMINMAX,
509 lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
510 LPROCFS_CNTR_AVGMINMAX,
511 "pagesused", "pages");
513 err = obd_init_checks();
514 if (err == -EOVERFLOW)
517 class_init_uuidlist();
518 err = class_handle_init();
522 CFS_INIT_LIST_HEAD(&obd_types);
524 err = cfs_psdev_register(&obd_psdev);
526 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
530 /* This struct is already zeroed for us (static global) */
531 for (i = 0; i < class_devno_max(); i++)
534 /* Default the dirty page cache cap to 1/2 of system memory.
535 * For clients with less memory, a larger fraction is needed
536 * for other purposes (mostly for BGL). */
537 if (cfs_num_physpages <= 512 << (20 - CFS_PAGE_SHIFT))
538 obd_max_dirty_pages = cfs_num_physpages / 4;
540 obd_max_dirty_pages = cfs_num_physpages / 2;
542 err = obd_init_caches();
546 err = class_procfs_init();
551 err = lu_global_init();
556 err = lustre_register_fs();
562 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
563 * ifdef to the end of the file to cover module and versioning goo.*/
565 static void cleanup_obdclass(void)
568 int lustre_unregister_fs(void);
569 __u64 memory_leaked, pages_leaked;
570 __u64 memory_max, pages_max;
573 lustre_unregister_fs();
575 cfs_psdev_deregister(&obd_psdev);
576 for (i = 0; i < class_devno_max(); i++) {
577 struct obd_device *obd = class_num2obd(i);
578 if (obd && obd->obd_set_up &&
579 OBT(obd) && OBP(obd, detach)) {
580 /* XXX should this call generic detach otherwise? */
581 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
582 OBP(obd, detach)(obd);
587 obd_cleanup_caches();
590 class_procfs_clean();
592 class_handle_cleanup();
593 class_exit_uuidlist();
594 obd_zombie_impexp_stop();
596 memory_leaked = obd_memory_sum();
597 pages_leaked = obd_pages_sum();
599 memory_max = obd_memory_max();
600 pages_max = obd_pages_max();
602 lprocfs_free_stats(&obd_memory);
603 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
604 "obd_memory max: "LPU64", leaked: "LPU64"\n",
605 memory_max, memory_leaked);
606 CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
607 "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
608 pages_max, pages_leaked);
613 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
614 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
615 MODULE_LICENSE("GPL");
617 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);