Whamcloud - gitweb
f8c5f9c777286a234b6b39075229e575b8865259
[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 cfs_rwlock_t obd_dev_lock = CFS_RW_LOCK_UNLOCKED;
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) ? D_ERROR : D_INFO,
142                        "Get jobid for (%s) failed(%d).\n", obd_jobid_var, ret);
143         }
144 #endif
145         RETURN(ret);
146 }
147 EXPORT_SYMBOL(lustre_get_jobid);
148
149 static inline void obd_data2conn(struct lustre_handle *conn,
150                                  struct obd_ioctl_data *data)
151 {
152         memset(conn, 0, sizeof *conn);
153         conn->cookie = data->ioc_cookie;
154 }
155
156 static inline void obd_conn2data(struct obd_ioctl_data *data,
157                                  struct lustre_handle *conn)
158 {
159         data->ioc_cookie = conn->cookie;
160 }
161
162 int class_resolve_dev_name(__u32 len, const char *name)
163 {
164         int rc;
165         int dev;
166
167         ENTRY;
168         if (!len || !name) {
169                 CERROR("No name passed,!\n");
170                 GOTO(out, rc = -EINVAL);
171         }
172         if (name[len - 1] != 0) {
173                 CERROR("Name not nul terminated!\n");
174                 GOTO(out, rc = -EINVAL);
175         }
176
177         CDEBUG(D_IOCTL, "device name %s\n", name);
178         dev = class_name2dev(name);
179         if (dev == -1) {
180                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
181                 GOTO(out, rc = -EINVAL);
182         }
183
184         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
185         rc = dev;
186
187 out:
188         RETURN(rc);
189 }
190
191 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
192 {
193         char *buf = NULL;
194         struct obd_ioctl_data *data;
195         struct libcfs_debug_ioctl_data *debug_data;
196         struct obd_device *obd = NULL;
197         int err = 0, len = 0;
198         ENTRY;
199
200         /* only for debugging */
201         if (cmd == LIBCFS_IOC_DEBUG_MASK) {
202                 debug_data = (struct libcfs_debug_ioctl_data*)arg;
203                 libcfs_subsystem_debug = debug_data->subs;
204                 libcfs_debug = debug_data->debug;
205                 return 0;
206         }
207
208         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
209         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
210                 CERROR("OBD ioctl: data error\n");
211                 RETURN(-EINVAL);
212         }
213         data = (struct obd_ioctl_data *)buf;
214
215         switch (cmd) {
216         case OBD_IOC_PROCESS_CFG: {
217                 struct lustre_cfg *lcfg;
218
219                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
220                         CERROR("No config buffer passed!\n");
221                         GOTO(out, err = -EINVAL);
222                 }
223                 OBD_ALLOC(lcfg, data->ioc_plen1);
224                 if (lcfg == NULL)
225                         GOTO(out, err = -ENOMEM);
226                 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
227                                          data->ioc_plen1);
228                 if (!err)
229                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
230                 if (!err)
231                         err = class_process_config(lcfg);
232
233                 OBD_FREE(lcfg, data->ioc_plen1);
234                 GOTO(out, err);
235         }
236
237         case OBD_GET_VERSION:
238                 if (!data->ioc_inlbuf1) {
239                         CERROR("No buffer passed in ioctl\n");
240                         GOTO(out, err = -EINVAL);
241                 }
242
243                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
244                         CERROR("ioctl buffer too small to hold version\n");
245                         GOTO(out, err = -EINVAL);
246                 }
247
248                 memcpy(data->ioc_bulk, BUILD_VERSION,
249                        strlen(BUILD_VERSION) + 1);
250
251                 err = obd_ioctl_popdata((void *)arg, data, len);
252                 if (err)
253                         err = -EFAULT;
254                 GOTO(out, err);
255
256         case OBD_IOC_NAME2DEV: {
257                 /* Resolve a device name.  This does not change the
258                  * currently selected device.
259                  */
260                 int dev;
261
262                 dev = class_resolve_dev_name(data->ioc_inllen1,
263                                              data->ioc_inlbuf1);
264                 data->ioc_dev = dev;
265                 if (dev < 0)
266                         GOTO(out, err = -EINVAL);
267
268                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
269                 if (err)
270                         err = -EFAULT;
271                 GOTO(out, err);
272         }
273
274         case OBD_IOC_UUID2DEV: {
275                 /* Resolve a device uuid.  This does not change the
276                  * currently selected device.
277                  */
278                 int dev;
279                 struct obd_uuid uuid;
280
281                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
282                         CERROR("No UUID passed!\n");
283                         GOTO(out, err = -EINVAL);
284                 }
285                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
286                         CERROR("UUID not NUL terminated!\n");
287                         GOTO(out, err = -EINVAL);
288                 }
289
290                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
291                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
292                 dev = class_uuid2dev(&uuid);
293                 data->ioc_dev = dev;
294                 if (dev == -1) {
295                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
296                                data->ioc_inlbuf1);
297                         GOTO(out, err = -EINVAL);
298                 }
299
300                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
301                        dev);
302                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
303                 if (err)
304                         err = -EFAULT;
305                 GOTO(out, err);
306         }
307
308         case OBD_IOC_CLOSE_UUID: {
309                 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
310                        data->ioc_inlbuf1);
311                 GOTO(out, err = 0);
312         }
313
314         case OBD_IOC_GETDEVICE: {
315                 int     index = data->ioc_count;
316                 char    *status, *str;
317
318                 if (!data->ioc_inlbuf1) {
319                         CERROR("No buffer passed in ioctl\n");
320                         GOTO(out, err = -EINVAL);
321                 }
322                 if (data->ioc_inllen1 < 128) {
323                         CERROR("ioctl buffer too small to hold version\n");
324                         GOTO(out, err = -EINVAL);
325                 }
326
327                 obd = class_num2obd(index);
328                 if (!obd)
329                         GOTO(out, err = -ENOENT);
330
331                 if (obd->obd_stopping)
332                         status = "ST";
333                 else if (obd->obd_set_up)
334                         status = "UP";
335                 else if (obd->obd_attached)
336                         status = "AT";
337                 else
338                         status = "--";
339                 str = (char *)data->ioc_bulk;
340                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
341                          (int)index, status, obd->obd_type->typ_name,
342                          obd->obd_name, obd->obd_uuid.uuid,
343                          cfs_atomic_read(&obd->obd_refcount));
344                 err = obd_ioctl_popdata((void *)arg, data, len);
345
346                 GOTO(out, err = 0);
347         }
348
349         }
350
351         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
352                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
353                         GOTO(out, err = -EINVAL);
354                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
355                         GOTO(out, err = -EINVAL);
356                 obd = class_name2obd(data->ioc_inlbuf4);
357         } else if (data->ioc_dev < class_devno_max()) {
358                 obd = class_num2obd(data->ioc_dev);
359         } else {
360                 CERROR("OBD ioctl: No device\n");
361                 GOTO(out, err = -EINVAL);
362         }
363
364         if (obd == NULL) {
365                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
366                 GOTO(out, err = -EINVAL);
367         }
368         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
369
370         if (!obd->obd_set_up || obd->obd_stopping) {
371                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
372                 GOTO(out, err = -EINVAL);
373         }
374
375         switch(cmd) {
376         case OBD_IOC_NO_TRANSNO: {
377                 if (!obd->obd_attached) {
378                         CERROR("Device %d not attached\n", obd->obd_minor);
379                         GOTO(out, err = -ENODEV);
380                 }
381                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
382                        obd->obd_name);
383                 obd->obd_no_transno = 1;
384                 GOTO(out, err = 0);
385         }
386
387         default: {
388                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
389                 if (err)
390                         GOTO(out, err);
391
392                 err = obd_ioctl_popdata((void *)arg, data, len);
393                 if (err)
394                         err = -EFAULT;
395                 GOTO(out, err);
396         }
397         }
398
399  out:
400         if (buf)
401                 obd_ioctl_freedata(buf, len);
402         RETURN(err);
403 } /* class_handle_ioctl */
404
405 #ifdef __KERNEL__
406 extern cfs_psdev_t obd_psdev;
407 #else
408 void *obd_psdev = NULL;
409 #endif
410
411 #define OBD_INIT_CHECK
412 #ifdef OBD_INIT_CHECK
413 int obd_init_checks(void)
414 {
415         __u64 u64val, div64val;
416         char buf[64];
417         int len, ret = 0;
418
419         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
420
421         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
422
423         u64val = OBD_OBJECT_EOF;
424         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
425         if (u64val != OBD_OBJECT_EOF) {
426                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
427                        u64val, (int)sizeof(u64val));
428                 ret = -EINVAL;
429         }
430         len = snprintf(buf, sizeof(buf), LPX64, u64val);
431         if (len != 18) {
432                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
433                 ret = -EINVAL;
434         }
435
436         div64val = OBD_OBJECT_EOF;
437         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
438         if (u64val != OBD_OBJECT_EOF) {
439                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
440                        u64val, (int)sizeof(u64val));
441                 ret = -EOVERFLOW;
442         }
443         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
444                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
445                        u64val, (int)sizeof(u64val));
446                 return -EOVERFLOW;
447         }
448         if (do_div(div64val, 256) != (u64val & 255)) {
449                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
450                 return -EOVERFLOW;
451         }
452         if (u64val >> 8 != div64val) {
453                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
454                        u64val, div64val, u64val >> 8);
455                 return -EOVERFLOW;
456         }
457         len = snprintf(buf, sizeof(buf), LPX64, u64val);
458         if (len != 18) {
459                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
460                 ret = -EINVAL;
461         }
462         len = snprintf(buf, sizeof(buf), LPU64, u64val);
463         if (len != 20) {
464                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
465                 ret = -EINVAL;
466         }
467         len = snprintf(buf, sizeof(buf), LPD64, u64val);
468         if (len != 2) {
469                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
470                 ret = -EINVAL;
471         }
472         if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
473                 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
474                       (__u64)CFS_PAGE_SIZE);
475                 ret = -EINVAL;
476         }
477
478         return ret;
479 }
480 #else
481 #define obd_init_checks() do {} while(0)
482 #endif
483
484 extern cfs_spinlock_t obd_types_lock;
485 extern int class_procfs_init(void);
486 extern int class_procfs_clean(void);
487
488 #ifdef __KERNEL__
489 static int __init init_obdclass(void)
490 #else
491 int init_obdclass(void)
492 #endif
493 {
494         int i, err;
495 #ifdef __KERNEL__
496         int lustre_register_fs(void);
497
498         for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
499                 CFS_INIT_LIST_HEAD(&capa_list[i]);
500 #endif
501
502         LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
503
504         cfs_spin_lock_init(&obd_types_lock);
505         obd_zombie_impexp_init();
506 #ifdef LPROCFS
507         obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
508                                          LPROCFS_STATS_FLAG_NONE |
509                                          LPROCFS_STATS_FLAG_IRQ_SAFE);
510         if (obd_memory == NULL) {
511                 CERROR("kmalloc of 'obd_memory' failed\n");
512                 RETURN(-ENOMEM);
513         }
514
515         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
516                              LPROCFS_CNTR_AVGMINMAX,
517                              "memused", "bytes");
518         lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
519                              LPROCFS_CNTR_AVGMINMAX,
520                              "pagesused", "pages");
521 #endif
522         err = obd_init_checks();
523         if (err == -EOVERFLOW)
524                 return err;
525
526         class_init_uuidlist();
527         err = class_handle_init();
528         if (err)
529                 return err;
530
531         CFS_INIT_LIST_HEAD(&obd_types);
532
533         err = cfs_psdev_register(&obd_psdev);
534         if (err) {
535                 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
536                 return err;
537         }
538
539         /* This struct is already zeroed for us (static global) */
540         for (i = 0; i < class_devno_max(); i++)
541                 obd_devs[i] = NULL;
542
543         /* Default the dirty page cache cap to 1/2 of system memory.
544          * For clients with less memory, a larger fraction is needed
545          * for other purposes (mostly for BGL). */
546         if (cfs_num_physpages <= 512 << (20 - CFS_PAGE_SHIFT))
547                 obd_max_dirty_pages = cfs_num_physpages / 4;
548         else
549                 obd_max_dirty_pages = cfs_num_physpages / 2;
550
551         err = obd_init_caches();
552         if (err)
553                 return err;
554 #ifdef __KERNEL__
555         err = class_procfs_init();
556         if (err)
557                 return err;
558 #endif
559
560         err = lu_global_init();
561         if (err)
562                 return err;
563
564 #ifdef __KERNEL__
565         err = lustre_register_fs();
566 #endif
567
568         return err;
569 }
570
571 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
572  * ifdef to the end of the file to cover module and versioning goo.*/
573 #ifdef __KERNEL__
574 static void cleanup_obdclass(void)
575 {
576         int i;
577         int lustre_unregister_fs(void);
578         __u64 memory_leaked, pages_leaked;
579         __u64 memory_max, pages_max;
580         ENTRY;
581
582         lustre_unregister_fs();
583
584         cfs_psdev_deregister(&obd_psdev);
585         for (i = 0; i < class_devno_max(); i++) {
586                 struct obd_device *obd = class_num2obd(i);
587                 if (obd && obd->obd_set_up &&
588                     OBT(obd) && OBP(obd, detach)) {
589                         /* XXX should this call generic detach otherwise? */
590                         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
591                         OBP(obd, detach)(obd);
592                 }
593         }
594         lu_global_fini();
595
596         obd_cleanup_caches();
597         obd_sysctl_clean();
598
599         class_procfs_clean();
600
601         class_handle_cleanup();
602         class_exit_uuidlist();
603         obd_zombie_impexp_stop();
604
605         memory_leaked = obd_memory_sum();
606         pages_leaked = obd_pages_sum();
607
608         memory_max = obd_memory_max();
609         pages_max = obd_pages_max();
610
611         lprocfs_free_stats(&obd_memory);
612         CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
613                "obd_memory max: "LPU64", leaked: "LPU64"\n",
614                memory_max, memory_leaked);
615         CDEBUG((pages_leaked) ? D_ERROR : D_INFO,
616                "obd_memory_pages max: "LPU64", leaked: "LPU64"\n",
617                pages_max, pages_leaked);
618
619         EXIT;
620 }
621
622 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
623 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
624 MODULE_LICENSE("GPL");
625
626 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);
627 #endif