Whamcloud - gitweb
LU-1308 Properly add multihomed nids to peer table
[fs/lustre-release.git] / lustre / obdclass / class_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40 #ifndef EXPORT_SYMTAB
41 # define EXPORT_SYMTAB
42 #endif
43 #ifndef __KERNEL__
44 # include <liblustre.h>
45 #else
46 # include <asm/atomic.h>
47 #endif
48
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"
57
58 #ifndef __KERNEL__
59 /* liblustre workaround */
60 cfs_atomic_t libcfs_kmemory = {0};
61 #endif
62
63 struct obd_device *obd_devs[MAX_OBD_DEVICES];
64 cfs_list_t obd_types;
65 cfs_rwlock_t obd_dev_lock = CFS_RW_LOCK_UNLOCKED;
66
67 #ifndef __KERNEL__
68 __u64 obd_max_pages = 0;
69 __u64 obd_max_alloc = 0;
70 __u64 obd_alloc;
71 __u64 obd_pages;
72 #endif
73
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;
86 int at_extra = 30;
87
88 cfs_atomic_t obd_dirty_pages;
89 cfs_atomic_t obd_dirty_transit_pages;
90
91 static inline void obd_data2conn(struct lustre_handle *conn,
92                                  struct obd_ioctl_data *data)
93 {
94         memset(conn, 0, sizeof *conn);
95         conn->cookie = data->ioc_cookie;
96 }
97
98 static inline void obd_conn2data(struct obd_ioctl_data *data,
99                                  struct lustre_handle *conn)
100 {
101         data->ioc_cookie = conn->cookie;
102 }
103
104 int class_resolve_dev_name(__u32 len, const char *name)
105 {
106         int rc;
107         int dev;
108
109         ENTRY;
110         if (!len || !name) {
111                 CERROR("No name passed,!\n");
112                 GOTO(out, rc = -EINVAL);
113         }
114         if (name[len - 1] != 0) {
115                 CERROR("Name not nul terminated!\n");
116                 GOTO(out, rc = -EINVAL);
117         }
118
119         CDEBUG(D_IOCTL, "device name %s\n", name);
120         dev = class_name2dev(name);
121         if (dev == -1) {
122                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
123                 GOTO(out, rc = -EINVAL);
124         }
125
126         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
127         rc = dev;
128
129 out:
130         RETURN(rc);
131 }
132
133 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
134 {
135         char *buf = NULL;
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;
140         ENTRY;
141
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;
147                 return 0;
148         }
149
150         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
151         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
152                 CERROR("OBD ioctl: data error\n");
153                 RETURN(-EINVAL);
154         }
155         data = (struct obd_ioctl_data *)buf;
156
157         switch (cmd) {
158         case OBD_IOC_PROCESS_CFG: {
159                 struct lustre_cfg *lcfg;
160
161                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
162                         CERROR("No config buffer passed!\n");
163                         GOTO(out, err = -EINVAL);
164                 }
165                 OBD_ALLOC(lcfg, data->ioc_plen1);
166                 if (lcfg == NULL)
167                         GOTO(out, err = -ENOMEM);
168                 err = cfs_copy_from_user(lcfg, data->ioc_pbuf1,
169                                          data->ioc_plen1);
170                 if (!err)
171                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
172                 if (!err)
173                         err = class_process_config(lcfg);
174
175                 OBD_FREE(lcfg, data->ioc_plen1);
176                 GOTO(out, err);
177         }
178
179         case OBD_GET_VERSION:
180                 if (!data->ioc_inlbuf1) {
181                         CERROR("No buffer passed in ioctl\n");
182                         GOTO(out, err = -EINVAL);
183                 }
184
185                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
186                         CERROR("ioctl buffer too small to hold version\n");
187                         GOTO(out, err = -EINVAL);
188                 }
189
190                 memcpy(data->ioc_bulk, BUILD_VERSION,
191                        strlen(BUILD_VERSION) + 1);
192
193                 err = obd_ioctl_popdata((void *)arg, data, len);
194                 if (err)
195                         err = -EFAULT;
196                 GOTO(out, err);
197
198         case OBD_IOC_NAME2DEV: {
199                 /* Resolve a device name.  This does not change the
200                  * currently selected device.
201                  */
202                 int dev;
203
204                 dev = class_resolve_dev_name(data->ioc_inllen1,
205                                              data->ioc_inlbuf1);
206                 data->ioc_dev = dev;
207                 if (dev < 0)
208                         GOTO(out, err = -EINVAL);
209
210                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
211                 if (err)
212                         err = -EFAULT;
213                 GOTO(out, err);
214         }
215
216         case OBD_IOC_UUID2DEV: {
217                 /* Resolve a device uuid.  This does not change the
218                  * currently selected device.
219                  */
220                 int dev;
221                 struct obd_uuid uuid;
222
223                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
224                         CERROR("No UUID passed!\n");
225                         GOTO(out, err = -EINVAL);
226                 }
227                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
228                         CERROR("UUID not NUL terminated!\n");
229                         GOTO(out, err = -EINVAL);
230                 }
231
232                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
233                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
234                 dev = class_uuid2dev(&uuid);
235                 data->ioc_dev = dev;
236                 if (dev == -1) {
237                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
238                                data->ioc_inlbuf1);
239                         GOTO(out, err = -EINVAL);
240                 }
241
242                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
243                        dev);
244                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
245                 if (err)
246                         err = -EFAULT;
247                 GOTO(out, err);
248         }
249
250         case OBD_IOC_CLOSE_UUID: {
251                 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
252                        data->ioc_inlbuf1);
253                 GOTO(out, err = 0);
254         }
255
256         case OBD_IOC_GETDEVICE: {
257                 int     index = data->ioc_count;
258                 char    *status, *str;
259
260                 if (!data->ioc_inlbuf1) {
261                         CERROR("No buffer passed in ioctl\n");
262                         GOTO(out, err = -EINVAL);
263                 }
264                 if (data->ioc_inllen1 < 128) {
265                         CERROR("ioctl buffer too small to hold version\n");
266                         GOTO(out, err = -EINVAL);
267                 }
268
269                 obd = class_num2obd(index);
270                 if (!obd)
271                         GOTO(out, err = -ENOENT);
272
273                 if (obd->obd_stopping)
274                         status = "ST";
275                 else if (obd->obd_set_up)
276                         status = "UP";
277                 else if (obd->obd_attached)
278                         status = "AT";
279                 else
280                         status = "--";
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);
287
288                 GOTO(out, err = 0);
289         }
290
291         }
292
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);
301         } else {
302                 CERROR("OBD ioctl: No device\n");
303                 GOTO(out, err = -EINVAL);
304         }
305
306         if (obd == NULL) {
307                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
308                 GOTO(out, err = -EINVAL);
309         }
310         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
311
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);
315         }
316
317         switch(cmd) {
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);
322                 }
323                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
324                        obd->obd_name);
325                 obd->obd_no_transno = 1;
326                 GOTO(out, err = 0);
327         }
328
329         default: {
330                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
331                 if (err)
332                         GOTO(out, err);
333
334                 err = obd_ioctl_popdata((void *)arg, data, len);
335                 if (err)
336                         err = -EFAULT;
337                 GOTO(out, err);
338         }
339         }
340
341  out:
342         if (buf)
343                 obd_ioctl_freedata(buf, len);
344         RETURN(err);
345 } /* class_handle_ioctl */
346
347
348
349 #ifdef __KERNEL__
350 extern cfs_psdev_t obd_psdev;
351 #else
352 void *obd_psdev = NULL;
353 #endif
354
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);
370
371 EXPORT_SYMBOL(proc_lustre_root);
372
373 /* uuid.c */
374 EXPORT_SYMBOL(class_uuid_unparse);
375 EXPORT_SYMBOL(lustre_uuid_to_peer);
376
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);
382
383 /* obd_config.c */
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);
398
399 /* mea.c */
400 EXPORT_SYMBOL(mea_name2idx);
401 EXPORT_SYMBOL(raw_name2idx);
402
403 #define OBD_INIT_CHECK
404 #ifdef OBD_INIT_CHECK
405 int obd_init_checks(void)
406 {
407         __u64 u64val, div64val;
408         char buf[64];
409         int len, ret = 0;
410
411         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", LPU64, LPD64, LPX64);
412
413         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
414
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));
420                 ret = -EINVAL;
421         }
422         len = snprintf(buf, sizeof(buf), LPX64, u64val);
423         if (len != 18) {
424                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
425                 ret = -EINVAL;
426         }
427
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));
433                 ret = -EOVERFLOW;
434         }
435         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
436                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
437                        u64val, (int)sizeof(u64val));
438                 return -EOVERFLOW;
439         }
440         if (do_div(div64val, 256) != (u64val & 255)) {
441                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
442                 return -EOVERFLOW;
443         }
444         if (u64val >> 8 != div64val) {
445                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
446                        u64val, div64val, u64val >> 8);
447                 return -EOVERFLOW;
448         }
449         len = snprintf(buf, sizeof(buf), LPX64, u64val);
450         if (len != 18) {
451                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
452                 ret = -EINVAL;
453         }
454         len = snprintf(buf, sizeof(buf), LPU64, u64val);
455         if (len != 20) {
456                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
457                 ret = -EINVAL;
458         }
459         len = snprintf(buf, sizeof(buf), LPD64, u64val);
460         if (len != 2) {
461                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
462                 ret = -EINVAL;
463         }
464         if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
465                 CWARN("mask failed: u64val "LPU64" >= "LPU64"\n", u64val,
466                       (__u64)CFS_PAGE_SIZE);
467                 ret = -EINVAL;
468         }
469
470         return ret;
471 }
472 #else
473 #define obd_init_checks() do {} while(0)
474 #endif
475
476 extern cfs_spinlock_t obd_types_lock;
477 extern int class_procfs_init(void);
478 extern int class_procfs_clean(void);
479
480 #ifdef __KERNEL__
481 static int __init init_obdclass(void)
482 #else
483 int init_obdclass(void)
484 #endif
485 {
486         int i, err;
487 #ifdef __KERNEL__
488         int lustre_register_fs(void);
489
490         for (i = CAPA_SITE_CLIENT; i < CAPA_SITE_MAX; i++)
491                 CFS_INIT_LIST_HEAD(&capa_list[i]);
492 #endif
493
494         LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
495
496         cfs_spin_lock_init(&obd_types_lock);
497         obd_zombie_impexp_init();
498 #ifdef LPROCFS
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");
503                 RETURN(-ENOMEM);
504         }
505
506         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
507                              LPROCFS_CNTR_AVGMINMAX,
508                              "memused", "bytes");
509         lprocfs_counter_init(obd_memory, OBD_MEMORY_PAGES_STAT,
510                              LPROCFS_CNTR_AVGMINMAX,
511                              "pagesused", "pages");
512 #endif
513         err = obd_init_checks();
514         if (err == -EOVERFLOW)
515                 return err;
516
517         class_init_uuidlist();
518         err = class_handle_init();
519         if (err)
520                 return err;
521
522         CFS_INIT_LIST_HEAD(&obd_types);
523
524         err = cfs_psdev_register(&obd_psdev);
525         if (err) {
526                 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
527                 return err;
528         }
529
530         /* This struct is already zeroed for us (static global) */
531         for (i = 0; i < class_devno_max(); i++)
532                 obd_devs[i] = NULL;
533
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;
539         else
540                 obd_max_dirty_pages = cfs_num_physpages / 2;
541
542         err = obd_init_caches();
543         if (err)
544                 return err;
545 #ifdef __KERNEL__
546         err = class_procfs_init();
547         if (err)
548                 return err;
549 #endif
550
551         err = lu_global_init();
552         if (err)
553                 return err;
554
555 #ifdef __KERNEL__
556         err = lustre_register_fs();
557 #endif
558
559         return err;
560 }
561
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.*/
564 #ifdef __KERNEL__
565 static void cleanup_obdclass(void)
566 {
567         int i;
568         int lustre_unregister_fs(void);
569         __u64 memory_leaked, pages_leaked;
570         __u64 memory_max, pages_max;
571         ENTRY;
572
573         lustre_unregister_fs();
574
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);
583                 }
584         }
585         lu_global_fini();
586
587         obd_cleanup_caches();
588         obd_sysctl_clean();
589
590         class_procfs_clean();
591
592         class_handle_cleanup();
593         class_exit_uuidlist();
594         obd_zombie_impexp_stop();
595
596         memory_leaked = obd_memory_sum();
597         pages_leaked = obd_pages_sum();
598
599         memory_max = obd_memory_max();
600         pages_max = obd_pages_max();
601
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);
609
610         EXIT;
611 }
612
613 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
614 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
615 MODULE_LICENSE("GPL");
616
617 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);
618 #endif