Whamcloud - gitweb
668967831fb4566d7daee9414a7ea1ce497982be
[fs/lustre-release.git] / lustre / obdclass / class_obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38 #ifndef __KERNEL__
39 # include <liblustre.h>
40 #else
41 # include <asm/atomic.h>
42 #endif
43
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"
52
53 #ifndef __KERNEL__
54 /* liblustre workaround */
55 cfs_atomic_t libcfs_kmemory = {0};
56 #endif
57
58 struct obd_device *obd_devs[MAX_OBD_DEVICES];
59 EXPORT_SYMBOL(obd_devs);
60 cfs_list_t obd_types;
61 DEFINE_RWLOCK(obd_dev_lock);
62
63 #ifndef __KERNEL__
64 __u64 obd_max_pages = 0;
65 __u64 obd_max_alloc = 0;
66 __u64 obd_alloc;
67 __u64 obd_pages;
68 #endif
69
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);
94 int at_extra = 30;
95 EXPORT_SYMBOL(at_extra);
96
97 cfs_atomic_t obd_dirty_transit_pages;
98 EXPORT_SYMBOL(obd_dirty_transit_pages);
99
100 char obd_jobid_var[JOBSTATS_JOBID_VAR_MAX_LEN + 1] = JOBSTATS_DISABLE;
101 EXPORT_SYMBOL(obd_jobid_var);
102
103 /* Get jobid of current process by reading the environment variable
104  * stored in between the "env_start" & "env_end" of task struct.
105  *
106  * TODO:
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
109  * purpose.
110  *
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.
114  */
115 int lustre_get_jobid(char *jobid)
116 {
117 #ifdef __KERNEL__
118         int jobid_len = JOBSTATS_JOBID_SIZE;
119 #endif
120         int ret = 0;
121         ENTRY;
122
123         memset(jobid, 0, JOBSTATS_JOBID_SIZE);
124         /* Jobstats isn't enabled */
125         if (!memcmp(obd_jobid_var, JOBSTATS_DISABLE,
126                     strlen(JOBSTATS_DISABLE))) {
127                 RETURN(0);
128         }
129
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());
135                 RETURN(0);
136         }
137
138 #ifdef __KERNEL__
139         ret = cfs_get_environ(obd_jobid_var, jobid, &jobid_len);
140         if (ret) {
141                 CDEBUG((ret != -ENOENT && ret != -EINVAL && ret != -EDEADLK) ?
142                        D_ERROR : D_INFO, "Get jobid for (%s) failed: rc = %d\n",
143                        obd_jobid_var, ret);
144         }
145 #endif
146         RETURN(ret);
147 }
148 EXPORT_SYMBOL(lustre_get_jobid);
149
150 static inline void obd_data2conn(struct lustre_handle *conn,
151                                  struct obd_ioctl_data *data)
152 {
153         memset(conn, 0, sizeof *conn);
154         conn->cookie = data->ioc_cookie;
155 }
156
157 static inline void obd_conn2data(struct obd_ioctl_data *data,
158                                  struct lustre_handle *conn)
159 {
160         data->ioc_cookie = conn->cookie;
161 }
162
163 int class_resolve_dev_name(__u32 len, const char *name)
164 {
165         int rc;
166         int dev;
167
168         ENTRY;
169         if (!len || !name) {
170                 CERROR("No name passed,!\n");
171                 GOTO(out, rc = -EINVAL);
172         }
173         if (name[len - 1] != 0) {
174                 CERROR("Name not nul terminated!\n");
175                 GOTO(out, rc = -EINVAL);
176         }
177
178         CDEBUG(D_IOCTL, "device name %s\n", name);
179         dev = class_name2dev(name);
180         if (dev == -1) {
181                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
182                 GOTO(out, rc = -EINVAL);
183         }
184
185         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
186         rc = dev;
187
188 out:
189         RETURN(rc);
190 }
191
192 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
193 {
194         char *buf = NULL;
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;
199         ENTRY;
200
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;
206                 return 0;
207         }
208
209         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
210         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
211                 CERROR("OBD ioctl: data error\n");
212                 RETURN(-EINVAL);
213         }
214         data = (struct obd_ioctl_data *)buf;
215
216         switch (cmd) {
217         case OBD_IOC_PROCESS_CFG: {
218                 struct lustre_cfg *lcfg;
219
220                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
221                         CERROR("No config buffer passed!\n");
222                         GOTO(out, err = -EINVAL);
223                 }
224                 OBD_ALLOC(lcfg, data->ioc_plen1);
225                 if (lcfg == NULL)
226                         GOTO(out, err = -ENOMEM);
227                 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
228                                          data->ioc_plen1);
229                 if (!err)
230                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
231                 if (!err)
232                         err = class_process_config(lcfg);
233
234                 OBD_FREE(lcfg, data->ioc_plen1);
235                 GOTO(out, err);
236         }
237
238         case OBD_GET_VERSION:
239                 if (!data->ioc_inlbuf1) {
240                         CERROR("No buffer passed in ioctl\n");
241                         GOTO(out, err = -EINVAL);
242                 }
243
244                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
245                         CERROR("ioctl buffer too small to hold version\n");
246                         GOTO(out, err = -EINVAL);
247                 }
248
249                 memcpy(data->ioc_bulk, BUILD_VERSION,
250                        strlen(BUILD_VERSION) + 1);
251
252                 err = obd_ioctl_popdata((void *)arg, data, len);
253                 if (err)
254                         err = -EFAULT;
255                 GOTO(out, err);
256
257         case OBD_IOC_NAME2DEV: {
258                 /* Resolve a device name.  This does not change the
259                  * currently selected device.
260                  */
261                 int dev;
262
263                 dev = class_resolve_dev_name(data->ioc_inllen1,
264                                              data->ioc_inlbuf1);
265                 data->ioc_dev = dev;
266                 if (dev < 0)
267                         GOTO(out, err = -EINVAL);
268
269                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
270                 if (err)
271                         err = -EFAULT;
272                 GOTO(out, err);
273         }
274
275         case OBD_IOC_UUID2DEV: {
276                 /* Resolve a device uuid.  This does not change the
277                  * currently selected device.
278                  */
279                 int dev;
280                 struct obd_uuid uuid;
281
282                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
283                         CERROR("No UUID passed!\n");
284                         GOTO(out, err = -EINVAL);
285                 }
286                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
287                         CERROR("UUID not NUL terminated!\n");
288                         GOTO(out, err = -EINVAL);
289                 }
290
291                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
292                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
293                 dev = class_uuid2dev(&uuid);
294                 data->ioc_dev = dev;
295                 if (dev == -1) {
296                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
297                                data->ioc_inlbuf1);
298                         GOTO(out, err = -EINVAL);
299                 }
300
301                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
302                        dev);
303                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
304                 if (err)
305                         err = -EFAULT;
306                 GOTO(out, err);
307         }
308
309         case OBD_IOC_CLOSE_UUID: {
310                 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
311                        data->ioc_inlbuf1);
312                 GOTO(out, err = 0);
313         }
314
315         case OBD_IOC_GETDEVICE: {
316                 int     index = data->ioc_count;
317                 char    *status, *str;
318
319                 if (!data->ioc_inlbuf1) {
320                         CERROR("No buffer passed in ioctl\n");
321                         GOTO(out, err = -EINVAL);
322                 }
323                 if (data->ioc_inllen1 < 128) {
324                         CERROR("ioctl buffer too small to hold version\n");
325                         GOTO(out, err = -EINVAL);
326                 }
327
328                 obd = class_num2obd(index);
329                 if (!obd)
330                         GOTO(out, err = -ENOENT);
331
332                 if (obd->obd_stopping)
333                         status = "ST";
334                 else if (obd->obd_set_up)
335                         status = "UP";
336                 else if (obd->obd_attached)
337                         status = "AT";
338                 else
339                         status = "--";
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);
346
347                 GOTO(out, err = 0);
348         }
349
350         }
351
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);
360         } else {
361                 CERROR("OBD ioctl: No device\n");
362                 GOTO(out, err = -EINVAL);
363         }
364
365         if (obd == NULL) {
366                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
367                 GOTO(out, err = -EINVAL);
368         }
369         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
370
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);
374         }
375
376         switch(cmd) {
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);
381                 }
382                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
383                        obd->obd_name);
384                 obd->obd_no_transno = 1;
385                 GOTO(out, err = 0);
386         }
387
388         default: {
389                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
390                 if (err)
391                         GOTO(out, err);
392
393                 err = obd_ioctl_popdata((void *)arg, data, len);
394                 if (err)
395                         err = -EFAULT;
396                 GOTO(out, err);
397         }
398         }
399
400  out:
401         if (buf)
402                 obd_ioctl_freedata(buf, len);
403         RETURN(err);
404 } /* class_handle_ioctl */
405
406 #ifdef __KERNEL__
407 extern cfs_psdev_t obd_psdev;
408 #else
409 void *obd_psdev = NULL;
410 #endif
411
412 #define OBD_INIT_CHECK
413 #ifdef OBD_INIT_CHECK
414 int obd_init_checks(void)
415 {
416         __u64 u64val, div64val;
417         char buf[64];
418         int len, ret = 0;
419
420         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
421
422         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
423
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));
429                 ret = -EINVAL;
430         }
431         len = snprintf(buf, sizeof(buf), LPX64, u64val);
432         if (len != 18) {
433                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
434                 ret = -EINVAL;
435         }
436
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));
442                 ret = -EOVERFLOW;
443         }
444         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
445                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
446                        u64val, (int)sizeof(u64val));
447                 return -EOVERFLOW;
448         }
449         if (do_div(div64val, 256) != (u64val & 255)) {
450                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
451                 return -EOVERFLOW;
452         }
453         if (u64val >> 8 != div64val) {
454                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
455                        u64val, div64val, u64val >> 8);
456                 return -EOVERFLOW;
457         }
458         len = snprintf(buf, sizeof(buf), LPX64, u64val);
459         if (len != 18) {
460                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
461                 ret = -EINVAL;
462         }
463         len = snprintf(buf, sizeof(buf), LPU64, u64val);
464         if (len != 20) {
465                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
466                 ret = -EINVAL;
467         }
468         len = snprintf(buf, sizeof(buf), LPD64, u64val);
469         if (len != 2) {
470                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
471                 ret = -EINVAL;
472         }
473         if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
474                 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
475                       (__u64)CFS_PAGE_SIZE);
476                 ret = -EINVAL;
477         }
478
479         return ret;
480 }
481 #else
482 #define obd_init_checks() do {} while(0)
483 #endif
484
485 extern cfs_spinlock_t obd_types_lock;
486 extern int class_procfs_init(void);
487 extern int class_procfs_clean(void);
488
489 #ifdef __KERNEL__
490 static int __init init_obdclass(void)
491 #else
492 int init_obdclass(void)
493 #endif
494 {
495         int i, err;
496 #ifdef __KERNEL__
497         int lustre_register_fs(void);
498
499         for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
500                 CFS_INIT_LIST_HEAD(&capa_list[i]);
501 #endif
502
503         LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
504
505         cfs_spin_lock_init(&obd_types_lock);
506         obd_zombie_impexp_init();
507 #ifdef LPROCFS
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");
513                 RETURN(-ENOMEM);
514         }
515
516         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
517                              LPROCFS_CNTR_AVGMINMAX,
518                              "memused", "bytes");
519         lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
520                              LPROCFS_CNTR_AVGMINMAX,
521                              "pagesused", "pages");
522 #endif
523         err = obd_init_checks();
524         if (err == -EOVERFLOW)
525                 return err;
526
527         class_init_uuidlist();
528         err = class_handle_init();
529         if (err)
530                 return err;
531
532         CFS_INIT_LIST_HEAD(&obd_types);
533
534         err = cfs_psdev_register(&obd_psdev);
535         if (err) {
536                 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
537                 return err;
538         }
539
540         /* This struct is already zeroed for us (static global) */
541         for (i = 0; i < class_devno_max(); i++)
542                 obd_devs[i] = NULL;
543
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;
549         else
550                 obd_max_dirty_pages = cfs_num_physpages / 2;
551
552         err = obd_init_caches();
553         if (err)
554                 return err;
555 #ifdef __KERNEL__
556         err = class_procfs_init();
557         if (err)
558                 return err;
559 #endif
560
561         err = lu_global_init();
562         if (err)
563                 return err;
564
565 #ifdef __KERNEL__
566         err = lustre_register_fs();
567 #endif
568
569         return err;
570 }
571
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.*/
574 #ifdef __KERNEL__
575 static void cleanup_obdclass(void)
576 {
577         int i;
578         int lustre_unregister_fs(void);
579         __u64 memory_leaked, pages_leaked;
580         __u64 memory_max, pages_max;
581         ENTRY;
582
583         lustre_unregister_fs();
584
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);
593                 }
594         }
595         lu_global_fini();
596
597         obd_cleanup_caches();
598         obd_sysctl_clean();
599
600         class_procfs_clean();
601
602         class_handle_cleanup();
603         class_exit_uuidlist();
604         obd_zombie_impexp_stop();
605
606         memory_leaked = obd_memory_sum();
607         pages_leaked = obd_pages_sum();
608
609         memory_max = obd_memory_max();
610         pages_max = obd_pages_max();
611
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);
619
620         EXIT;
621 }
622
623 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
624 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
625 MODULE_LICENSE("GPL");
626
627 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);
628 #endif