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 # include <liblustre.h>
41 # include <asm/atomic.h>
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lnet/lnetctl.h>
47 #include <lustre_debug.h>
48 #include <lprocfs_status.h>
49 #include <lustre/lustre_build_version.h>
50 #include <libcfs/list.h>
51 #include "llog_internal.h"
54 /* liblustre workaround */
55 cfs_atomic_t libcfs_kmemory = {0};
58 struct obd_device *obd_devs[MAX_OBD_DEVICES];
59 EXPORT_SYMBOL(obd_devs);
61 DEFINE_RWLOCK(obd_dev_lock);
64 __u64 obd_max_pages = 0;
65 __u64 obd_max_alloc = 0;
70 /* The following are visible and mutable through /proc/sys/lustre/. */
71 unsigned int obd_debug_peer_on_timeout;
72 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
73 unsigned int obd_dump_on_timeout;
74 EXPORT_SYMBOL(obd_dump_on_timeout);
75 unsigned int obd_dump_on_eviction;
76 EXPORT_SYMBOL(obd_dump_on_eviction);
77 unsigned int obd_max_dirty_pages = 256;
78 EXPORT_SYMBOL(obd_max_dirty_pages);
79 cfs_atomic_t obd_dirty_pages;
80 EXPORT_SYMBOL(obd_dirty_pages);
81 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT; /* seconds */
82 EXPORT_SYMBOL(obd_timeout);
83 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
84 EXPORT_SYMBOL(ldlm_timeout);
85 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
86 unsigned int at_min = 0;
87 EXPORT_SYMBOL(at_min);
88 unsigned int at_max = 600;
89 EXPORT_SYMBOL(at_max);
90 unsigned int at_history = 600;
91 EXPORT_SYMBOL(at_history);
92 int at_early_margin = 5;
93 EXPORT_SYMBOL(at_early_margin);
95 EXPORT_SYMBOL(at_extra);
97 cfs_atomic_t obd_dirty_transit_pages;
98 EXPORT_SYMBOL(obd_dirty_transit_pages);
100 char obd_jobid_var[JOBSTATS_JOBID_VAR_MAX_LEN + 1] = JOBSTATS_DISABLE;
101 EXPORT_SYMBOL(obd_jobid_var);
103 /* Get jobid of current process by reading the environment variable
104 * stored in between the "env_start" & "env_end" of task struct.
107 * It's better to cache the jobid for later use if there is any
108 * efficient way, the cl_env code probably could be reused for this
111 * If some job scheduler doesn't store jobid in the "env_start/end",
112 * then an upcall could be issued here to get the jobid by utilizing
113 * the userspace tools/api. Then, the jobid must be cached.
115 int lustre_get_jobid(char *jobid)
118 int jobid_len = JOBSTATS_JOBID_SIZE;
123 memset(jobid, 0, JOBSTATS_JOBID_SIZE);
124 /* Jobstats isn't enabled */
125 if (!memcmp(obd_jobid_var, JOBSTATS_DISABLE,
126 strlen(JOBSTATS_DISABLE))) {
130 /* Use process name + fsuid as jobid */
131 if (!memcmp(obd_jobid_var, JOBSTATS_PROCNAME_UID,
132 strlen(JOBSTATS_PROCNAME_UID))) {
133 snprintf(jobid, JOBSTATS_JOBID_SIZE, "%s_%u",
134 cfs_curproc_comm(), cfs_curproc_fsuid());
139 ret = cfs_get_environ(obd_jobid_var, jobid, &jobid_len);
141 CDEBUG((ret != -ENOENT && ret != -EINVAL && ret != -EDEADLK) ?
142 D_ERROR : D_INFO, "Get jobid for (%s) failed: rc = %d\n",
148 EXPORT_SYMBOL(lustre_get_jobid);
150 static inline void obd_data2conn(struct lustre_handle *conn,
151 struct obd_ioctl_data *data)
153 memset(conn, 0, sizeof *conn);
154 conn->cookie = data->ioc_cookie;
157 static inline void obd_conn2data(struct obd_ioctl_data *data,
158 struct lustre_handle *conn)
160 data->ioc_cookie = conn->cookie;
163 int class_resolve_dev_name(__u32 len, const char *name)
170 CERROR("No name passed,!\n");
171 GOTO(out, rc = -EINVAL);
173 if (name[len - 1] != 0) {
174 CERROR("Name not nul terminated!\n");
175 GOTO(out, rc = -EINVAL);
178 CDEBUG(D_IOCTL, "device name %s\n", name);
179 dev = class_name2dev(name);
181 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
182 GOTO(out, rc = -EINVAL);
185 CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
192 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
195 struct obd_ioctl_data *data;
196 struct libcfs_debug_ioctl_data *debug_data;
197 struct obd_device *obd = NULL;
198 int err = 0, len = 0;
201 /* only for debugging */
202 if (cmd == LIBCFS_IOC_DEBUG_MASK) {
203 debug_data = (struct libcfs_debug_ioctl_data*)arg;
204 libcfs_subsystem_debug = debug_data->subs;
205 libcfs_debug = debug_data->debug;
209 CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
210 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
211 CERROR("OBD ioctl: data error\n");
214 data = (struct obd_ioctl_data *)buf;
217 case OBD_IOC_PROCESS_CFG: {
218 struct lustre_cfg *lcfg;
220 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
221 CERROR("No config buffer passed!\n");
222 GOTO(out, err = -EINVAL);
224 OBD_ALLOC(lcfg, data->ioc_plen1);
226 GOTO(out, err = -ENOMEM);
227 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
230 err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
232 err = class_process_config(lcfg);
234 OBD_FREE(lcfg, data->ioc_plen1);
238 case OBD_GET_VERSION:
239 if (!data->ioc_inlbuf1) {
240 CERROR("No buffer passed in ioctl\n");
241 GOTO(out, err = -EINVAL);
244 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
245 CERROR("ioctl buffer too small to hold version\n");
246 GOTO(out, err = -EINVAL);
249 memcpy(data->ioc_bulk, BUILD_VERSION,
250 strlen(BUILD_VERSION) + 1);
252 err = obd_ioctl_popdata((void *)arg, data, len);
257 case OBD_IOC_NAME2DEV: {
258 /* Resolve a device name. This does not change the
259 * currently selected device.
263 dev = class_resolve_dev_name(data->ioc_inllen1,
267 GOTO(out, err = -EINVAL);
269 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
275 case OBD_IOC_UUID2DEV: {
276 /* Resolve a device uuid. This does not change the
277 * currently selected device.
280 struct obd_uuid uuid;
282 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
283 CERROR("No UUID passed!\n");
284 GOTO(out, err = -EINVAL);
286 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
287 CERROR("UUID not NUL terminated!\n");
288 GOTO(out, err = -EINVAL);
291 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
292 obd_str2uuid(&uuid, data->ioc_inlbuf1);
293 dev = class_uuid2dev(&uuid);
296 CDEBUG(D_IOCTL, "No device for UUID %s!\n",
298 GOTO(out, err = -EINVAL);
301 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
303 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
309 case OBD_IOC_CLOSE_UUID: {
310 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
315 case OBD_IOC_GETDEVICE: {
316 int index = data->ioc_count;
319 if (!data->ioc_inlbuf1) {
320 CERROR("No buffer passed in ioctl\n");
321 GOTO(out, err = -EINVAL);
323 if (data->ioc_inllen1 < 128) {
324 CERROR("ioctl buffer too small to hold version\n");
325 GOTO(out, err = -EINVAL);
328 obd = class_num2obd(index);
330 GOTO(out, err = -ENOENT);
332 if (obd->obd_stopping)
334 else if (obd->obd_set_up)
336 else if (obd->obd_attached)
340 str = (char *)data->ioc_bulk;
341 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
342 (int)index, status, obd->obd_type->typ_name,
343 obd->obd_name, obd->obd_uuid.uuid,
344 cfs_atomic_read(&obd->obd_refcount));
345 err = obd_ioctl_popdata((void *)arg, data, len);
352 if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
353 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
354 GOTO(out, err = -EINVAL);
355 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
356 GOTO(out, err = -EINVAL);
357 obd = class_name2obd(data->ioc_inlbuf4);
358 } else if (data->ioc_dev < class_devno_max()) {
359 obd = class_num2obd(data->ioc_dev);
361 CERROR("OBD ioctl: No device\n");
362 GOTO(out, err = -EINVAL);
366 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
367 GOTO(out, err = -EINVAL);
369 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
371 if (!obd->obd_set_up || obd->obd_stopping) {
372 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
373 GOTO(out, err = -EINVAL);
377 case OBD_IOC_NO_TRANSNO: {
378 if (!obd->obd_attached) {
379 CERROR("Device %d not attached\n", obd->obd_minor);
380 GOTO(out, err = -ENODEV);
382 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
384 obd->obd_no_transno = 1;
389 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
393 err = obd_ioctl_popdata((void *)arg, data, len);
402 obd_ioctl_freedata(buf, len);
404 } /* class_handle_ioctl */
407 extern cfs_psdev_t obd_psdev;
409 void *obd_psdev = NULL;
412 #define OBD_INIT_CHECK
413 #ifdef OBD_INIT_CHECK
414 int obd_init_checks(void)
416 __u64 u64val, div64val;
420 CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
422 CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
424 u64val = OBD_OBJECT_EOF;
425 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
426 if (u64val != OBD_OBJECT_EOF) {
427 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
428 u64val, (int)sizeof(u64val));
431 len = snprintf(buf, sizeof(buf), LPX64, u64val);
433 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
437 div64val = OBD_OBJECT_EOF;
438 CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
439 if (u64val != OBD_OBJECT_EOF) {
440 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
441 u64val, (int)sizeof(u64val));
444 if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
445 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
446 u64val, (int)sizeof(u64val));
449 if (do_div(div64val, 256) != (u64val & 255)) {
450 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
453 if (u64val >> 8 != div64val) {
454 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
455 u64val, div64val, u64val >> 8);
458 len = snprintf(buf, sizeof(buf), LPX64, u64val);
460 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
463 len = snprintf(buf, sizeof(buf), LPU64, u64val);
465 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
468 len = snprintf(buf, sizeof(buf), LPD64, u64val);
470 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
473 if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
474 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
475 (__u64)CFS_PAGE_SIZE);
482 #define obd_init_checks() do {} while(0)
485 extern cfs_spinlock_t obd_types_lock;
486 extern int class_procfs_init(void);
487 extern int class_procfs_clean(void);
490 static int __init init_obdclass(void)
492 int init_obdclass(void)
497 int lustre_register_fs(void);
499 for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
500 CFS_INIT_LIST_HEAD(&capa_list[i]);
503 LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
505 cfs_spin_lock_init(&obd_types_lock);
506 obd_zombie_impexp_init();
508 obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
509 LPROCFS_STATS_FLAG_NONE |
510 LPROCFS_STATS_FLAG_IRQ_SAFE);
511 if (obd_memory == NULL) {
512 CERROR("kmalloc of 'obd_memory' failed\n");
516 lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
517 LPROCFS_CNTR_AVGMINMAX,
519 lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
520 LPROCFS_CNTR_AVGMINMAX,
521 "pagesused", "pages");
523 err = obd_init_checks();
524 if (err == -EOVERFLOW)
527 class_init_uuidlist();
528 err = class_handle_init();
532 CFS_INIT_LIST_HEAD(&obd_types);
534 err = cfs_psdev_register(&obd_psdev);
536 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
540 /* This struct is already zeroed for us (static global) */
541 for (i = 0; i < class_devno_max(); i++)
544 /* Default the dirty page cache cap to 1/2 of system memory.
545 * For clients with less memory, a larger fraction is needed
546 * for other purposes (mostly for BGL). */
547 if (cfs_num_physpages <= 512 << (20 - CFS_PAGE_SHIFT))
548 obd_max_dirty_pages = cfs_num_physpages / 4;
550 obd_max_dirty_pages = cfs_num_physpages / 2;
552 err = obd_init_caches();
556 err = class_procfs_init();
561 err = lu_global_init();
566 err = lustre_register_fs();
572 /* liblustre doesn't call cleanup_obdclass, apparently. we carry on in this
573 * ifdef to the end of the file to cover module and versioning goo.*/
575 static void cleanup_obdclass(void)
578 int lustre_unregister_fs(void);
579 __u64 memory_leaked, pages_leaked;
580 __u64 memory_max, pages_max;
583 lustre_unregister_fs();
585 cfs_psdev_deregister(&obd_psdev);
586 for (i = 0; i < class_devno_max(); i++) {
587 struct obd_device *obd = class_num2obd(i);
588 if (obd && obd->obd_set_up &&
589 OBT(obd) && OBP(obd, detach)) {
590 /* XXX should this call generic detach otherwise? */
591 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
592 OBP(obd, detach)(obd);
597 obd_cleanup_caches();
600 class_procfs_clean();
602 class_handle_cleanup();
603 class_exit_uuidlist();
604 obd_zombie_impexp_stop();
606 memory_leaked = obd_memory_sum();
607 pages_leaked = obd_pages_sum();
609 memory_max = obd_memory_max();
610 pages_max = obd_pages_max();
612 lprocfs_free_stats(&obd_memory);
613 CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
614 "obd_memory max: "LPU64", leaked: "LPU64"\n",
615 memory_max, memory_leaked);
616 CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
617 "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
618 pages_max, pages_leaked);
623 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
624 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
625 MODULE_LICENSE("GPL");
627 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);