Whamcloud - gitweb
* landing 11667 (debug_peer_on_timeout) and 11684 (liblustre block for asynch
[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  * Object Devices Class Driver
5  *
6  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  * These are the only exported functions, they provide some generic
27  * infrastructure for managing object devices
28  */
29
30 #define DEBUG_SUBSYSTEM S_CLASS
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34 #ifndef __KERNEL__
35 # include <liblustre.h>
36 #endif
37
38 #include <obd_support.h>
39 #include <obd_class.h>
40 #include <lustre_debug.h>
41 #include <lprocfs_status.h>
42 #ifdef __KERNEL__
43 #include <linux/lustre_build_version.h>
44 #endif
45 #include <libcfs/list.h>
46 #include "llog_internal.h"
47
48 #ifndef __KERNEL__
49 /* liblustre workaround */
50 atomic_t libcfs_kmemory = {0};
51 #endif
52
53 struct obd_device *obd_devs[MAX_OBD_DEVICES];
54 struct list_head obd_types;
55 spinlock_t obd_dev_lock = SPIN_LOCK_UNLOCKED;
56 #ifndef __KERNEL__
57 atomic_t obd_memory;
58 int obd_memmax;
59 #endif
60
61 /* The following are visible and mutable through /proc/sys/lustre/. */
62 unsigned int obd_fail_loc;
63 unsigned int obd_debug_peer_on_timeout;
64 unsigned int obd_dump_on_timeout;
65 unsigned int obd_dump_on_eviction;
66 unsigned int obd_timeout = 100; /* seconds */
67 unsigned int ldlm_timeout = 20; /* seconds */
68 unsigned int obd_health_check_timeout = 120; /* seconds */
69 unsigned int obd_max_dirty_pages = 256;
70 atomic_t obd_dirty_pages;
71
72 cfs_waitq_t obd_race_waitq;
73 int obd_race_state;
74
75 #ifdef __KERNEL__
76 unsigned int obd_print_fail_loc(void)
77 {
78         CWARN("obd_fail_loc = %x\n", obd_fail_loc);
79         return obd_fail_loc;
80 }
81
82 void obd_set_fail_loc(unsigned int fl)
83 {
84         obd_fail_loc = fl;
85 }
86
87 /*  opening /dev/obd */
88 static int obd_class_open(unsigned long flags, void *args)
89 {
90         ENTRY;
91
92         PORTAL_MODULE_USE;
93         RETURN(0);
94 }
95
96 /*  closing /dev/obd */
97 static int obd_class_release(unsigned long flags, void *args)
98 {
99         ENTRY;
100
101         PORTAL_MODULE_UNUSE;
102         RETURN(0);
103 }
104 #endif
105
106 static inline void obd_data2conn(struct lustre_handle *conn,
107                                  struct obd_ioctl_data *data)
108 {
109         memset(conn, 0, sizeof *conn);
110         conn->cookie = data->ioc_cookie;
111 }
112
113 static inline void obd_conn2data(struct obd_ioctl_data *data,
114                                  struct lustre_handle *conn)
115 {
116         data->ioc_cookie = conn->cookie;
117 }
118
119 int class_resolve_dev_name(uint32_t len, const char *name)
120 {
121         int rc;
122         int dev;
123
124         ENTRY;
125         if (!len || !name) {
126                 CERROR("No name passed,!\n");
127                 GOTO(out, rc = -EINVAL);
128         }
129         if (name[len - 1] != 0) {
130                 CERROR("Name not nul terminated!\n");
131                 GOTO(out, rc = -EINVAL);
132         }
133
134         CDEBUG(D_IOCTL, "device name %s\n", name);
135         dev = class_name2dev(name);
136         if (dev == -1) {
137                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
138                 GOTO(out, rc = -EINVAL);
139         }
140
141         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
142         rc = dev;
143
144 out:
145         RETURN(rc);
146 }
147
148 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
149 {
150         char *buf = NULL;
151         struct obd_ioctl_data *data;
152         struct libcfs_debug_ioctl_data *debug_data;
153         struct obd_device *obd = NULL;
154         int err = 0, len = 0;
155         ENTRY;
156
157         /* only for debugging */
158         if (cmd == LIBCFS_IOC_DEBUG_MASK) {
159                 debug_data = (struct libcfs_debug_ioctl_data*)arg;
160                 libcfs_subsystem_debug = debug_data->subs;
161                 libcfs_debug = debug_data->debug;
162                 return 0;
163         }
164
165         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
166         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
167                 CERROR("OBD ioctl: data error\n");
168                 GOTO(out, err = -EINVAL);
169         }
170         data = (struct obd_ioctl_data *)buf;
171
172         switch (cmd) {
173         case OBD_IOC_PROCESS_CFG: {
174                 struct lustre_cfg *lcfg;
175
176                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
177                         CERROR("No config buffer passed!\n");
178                         GOTO(out, err = -EINVAL);
179                 }
180                 OBD_ALLOC(lcfg, data->ioc_plen1);
181                 if (lcfg == NULL)
182                         GOTO(out, err = -ENOMEM);
183                 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
184                 if (!err)
185                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
186                 if (!err)
187                         err = class_process_config(lcfg);
188                 OBD_FREE(lcfg, data->ioc_plen1);
189                 GOTO(out, err);
190         }
191
192         case OBD_GET_VERSION:
193                 if (!data->ioc_inlbuf1) {
194                         CERROR("No buffer passed in ioctl\n");
195                         GOTO(out, err = -EINVAL);
196                 }
197
198                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
199                         CERROR("ioctl buffer too small to hold version\n");
200                         GOTO(out, err = -EINVAL);
201                 }
202
203                 memcpy(data->ioc_bulk, BUILD_VERSION,
204                        strlen(BUILD_VERSION) + 1);
205
206                 err = obd_ioctl_popdata((void *)arg, data, len);
207                 if (err)
208                         err = -EFAULT;
209                 GOTO(out, err);
210
211         case OBD_IOC_NAME2DEV: {
212                 /* Resolve a device name.  This does not change the
213                  * currently selected device.
214                  */
215                 int dev;
216
217                 dev = class_resolve_dev_name(data->ioc_inllen1,
218                                              data->ioc_inlbuf1);
219                 data->ioc_dev = dev;
220                 if (dev < 0)
221                         GOTO(out, err = -EINVAL);
222
223                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
224                 if (err)
225                         err = -EFAULT;
226                 GOTO(out, err);
227         }
228
229         case OBD_IOC_UUID2DEV: {
230                 /* Resolve a device uuid.  This does not change the
231                  * currently selected device.
232                  */
233                 int dev;
234                 struct obd_uuid uuid;
235
236                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
237                         CERROR("No UUID passed!\n");
238                         GOTO(out, err = -EINVAL);
239                 }
240                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
241                         CERROR("UUID not NUL terminated!\n");
242                         GOTO(out, err = -EINVAL);
243                 }
244
245                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
246                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
247                 dev = class_uuid2dev(&uuid);
248                 data->ioc_dev = dev;
249                 if (dev == -1) {
250                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
251                                data->ioc_inlbuf1);
252                         GOTO(out, err = -EINVAL);
253                 }
254
255                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
256                        dev);
257                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
258                 if (err)
259                         err = -EFAULT;
260                 GOTO(out, err);
261         }
262
263         case OBD_IOC_CLOSE_UUID: {
264                 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
265                        data->ioc_inlbuf1);
266                 GOTO(out, err = 0);
267         }
268
269         case OBD_IOC_GETDEVICE: {
270                 int     index = data->ioc_count;
271                 char    *status, *str;
272
273                 if (!data->ioc_inlbuf1) {
274                         CERROR("No buffer passed in ioctl\n");
275                         GOTO(out, err = -EINVAL);
276                 } 
277                 if (data->ioc_inllen1 < 128) {
278                         CERROR("ioctl buffer too small to hold version\n");
279                         GOTO(out, err = -EINVAL);
280                 }
281                                 
282                 obd = class_num2obd(index);
283                 if (!obd)
284                         GOTO(out, err = -ENOENT);
285                 
286                 if (obd->obd_stopping)
287                         status = "ST";
288                 else if (obd->obd_set_up)
289                         status = "UP";
290                 else if (obd->obd_attached)
291                         status = "AT";
292                 else
293                         status = "--"; 
294                 str = (char *)data->ioc_bulk;
295                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
296                          (int)index, status, obd->obd_type->typ_name,
297                          obd->obd_name, obd->obd_uuid.uuid,
298                          atomic_read(&obd->obd_refcount));
299                 err = obd_ioctl_popdata((void *)arg, data, len);
300
301                 GOTO(out, err = 0);
302         }
303
304         }
305
306         if (data->ioc_dev >= class_devno_max()) {
307                 CERROR("OBD ioctl: No device\n");
308                 GOTO(out, err = -EINVAL);
309         }
310
311         obd = class_num2obd(data->ioc_dev);
312         if (obd == NULL) {
313                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
314                 GOTO(out, err = -EINVAL);
315         }
316         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
317
318         if (!obd->obd_set_up || obd->obd_stopping) {
319                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
320                 GOTO(out, err = -EINVAL);
321         }
322
323         switch(cmd) {
324         case OBD_IOC_NO_TRANSNO: {
325                 if (!obd->obd_attached) {
326                         CERROR("Device %d not attached\n", obd->obd_minor);
327                         GOTO(out, err = -ENODEV);
328                 }
329                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
330                        obd->obd_name);
331                 obd->obd_no_transno = 1;
332                 GOTO(out, err = 0);
333         }
334
335         default: {
336                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
337                 if (err)
338                         GOTO(out, err);
339
340                 err = obd_ioctl_popdata((void *)arg, data, len);
341                 if (err)
342                         err = -EFAULT;
343                 GOTO(out, err);
344         }
345         }
346
347  out:
348         if (buf)
349                 obd_ioctl_freedata(buf, len);
350         RETURN(err);
351 } /* class_handle_ioctl */
352
353
354
355 #define OBD_MINOR 241
356 #ifdef __KERNEL__
357 /* to control /dev/obd */
358 static int obd_class_ioctl (struct cfs_psdev_file *pfile, unsigned long cmd,
359                             void *arg)
360 {
361         return class_handle_ioctl(cmd, (unsigned long)arg);
362 }
363
364 /* declare character device */
365 struct cfs_psdev_ops obd_psdev_ops = {
366         /* .p_open    = */ obd_class_open,      /* open */
367         /* .p_close   = */ obd_class_release,   /* release */
368         /* .p_read    = */ NULL,
369         /* .p_write   = */ NULL,
370         /* .p_ioctl   = */ obd_class_ioctl     /* ioctl */
371 };
372
373 extern cfs_psdev_t obd_psdev;
374 #else
375 void *obd_psdev = NULL;
376 #endif
377
378 EXPORT_SYMBOL(obd_devs);
379 EXPORT_SYMBOL(obd_fail_loc);
380 EXPORT_SYMBOL(obd_print_fail_loc);
381 EXPORT_SYMBOL(obd_race_waitq);
382 EXPORT_SYMBOL(obd_race_state);
383 EXPORT_SYMBOL(obd_dump_on_timeout);
384 EXPORT_SYMBOL(obd_dump_on_eviction);
385 EXPORT_SYMBOL(obd_timeout);
386 EXPORT_SYMBOL(ldlm_timeout);
387 EXPORT_SYMBOL(obd_health_check_timeout);
388 EXPORT_SYMBOL(obd_max_dirty_pages);
389 EXPORT_SYMBOL(obd_dirty_pages);
390 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
391
392 EXPORT_SYMBOL(proc_lustre_root);
393
394 EXPORT_SYMBOL(class_register_type);
395 EXPORT_SYMBOL(class_unregister_type);
396 EXPORT_SYMBOL(class_get_type);
397 EXPORT_SYMBOL(class_put_type);
398 EXPORT_SYMBOL(class_name2dev);
399 EXPORT_SYMBOL(class_name2obd);
400 EXPORT_SYMBOL(class_uuid2dev);
401 EXPORT_SYMBOL(class_uuid2obd);
402 EXPORT_SYMBOL(class_find_client_obd);
403 EXPORT_SYMBOL(class_find_client_notype);
404 EXPORT_SYMBOL(class_devices_in_group);
405 EXPORT_SYMBOL(class_conn2export);
406 EXPORT_SYMBOL(class_exp2obd);
407 EXPORT_SYMBOL(class_conn2obd);
408 EXPORT_SYMBOL(class_exp2cliimp);
409 EXPORT_SYMBOL(class_conn2cliimp);
410 EXPORT_SYMBOL(class_disconnect);
411 EXPORT_SYMBOL(class_num2obd);
412
413 /* uuid.c */
414 EXPORT_SYMBOL(class_generate_random_uuid);
415 EXPORT_SYMBOL(class_uuid_unparse);
416 EXPORT_SYMBOL(lustre_uuid_to_peer);
417
418 EXPORT_SYMBOL(class_handle_hash);
419 EXPORT_SYMBOL(class_handle_unhash);
420 EXPORT_SYMBOL(class_handle2object);
421
422 /* obd_config.c */
423 EXPORT_SYMBOL(class_incref);
424 EXPORT_SYMBOL(class_decref);
425 EXPORT_SYMBOL(class_get_profile);
426 EXPORT_SYMBOL(class_del_profile);
427 EXPORT_SYMBOL(class_del_profiles);
428 EXPORT_SYMBOL(class_process_config);
429 EXPORT_SYMBOL(class_process_proc_param);
430 EXPORT_SYMBOL(class_config_parse_llog);
431 EXPORT_SYMBOL(class_config_dump_llog);
432 EXPORT_SYMBOL(class_attach);
433 EXPORT_SYMBOL(class_setup);
434 EXPORT_SYMBOL(class_cleanup);
435 EXPORT_SYMBOL(class_detach);
436 EXPORT_SYMBOL(class_manual_cleanup);
437
438 #define OBD_INIT_CHECK
439 #ifdef OBD_INIT_CHECK
440 int obd_init_checks(void)
441 {
442         __u64 u64val, div64val;
443         char buf[64];
444         int len, ret = 0;
445
446         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s, LPSZ=%s, LPSSZ=%s\n",
447                LPU64, LPD64, LPX64, LPSZ, LPSSZ);
448
449         CDEBUG(D_INFO, "OBD_OBJECT_EOF = "LPX64"\n", (__u64)OBD_OBJECT_EOF);
450
451         u64val = OBD_OBJECT_EOF;
452         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
453         if (u64val != OBD_OBJECT_EOF) {
454                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
455                        u64val, (int)sizeof(u64val));
456                 ret = -EINVAL;
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
464         div64val = OBD_OBJECT_EOF;
465         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = "LPX64"\n", u64val);
466         if (u64val != OBD_OBJECT_EOF) {
467                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
468                        u64val, (int)sizeof(u64val));
469                 ret = -EOVERFLOW;
470         }
471         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
472                 CERROR("__u64 "LPX64"(%d) != 0xffffffffffffffff\n",
473                        u64val, (int)sizeof(u64val));
474                 return -EOVERFLOW;
475         }
476         if (do_div(div64val, 256) != (u64val & 255)) {
477                 CERROR("do_div("LPX64",256) != "LPU64"\n", u64val, u64val &255);
478                 return -EOVERFLOW;
479         }
480         if (u64val >> 8 != div64val) {
481                 CERROR("do_div("LPX64",256) "LPU64" != "LPU64"\n",
482                        u64val, div64val, u64val >> 8);
483                 return -EOVERFLOW;
484         }
485         len = snprintf(buf, sizeof(buf), LPX64, u64val);
486         if (len != 18) {
487                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
488                 ret = -EINVAL;
489         }
490         len = snprintf(buf, sizeof(buf), LPU64, u64val);
491         if (len != 20) {
492                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
493                 ret = -EINVAL;
494         }
495         len = snprintf(buf, sizeof(buf), LPD64, u64val);
496         if (len != 2) {
497                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
498                 ret = -EINVAL;
499         }
500         if ((u64val & ~CFS_PAGE_MASK) >= CFS_PAGE_SIZE) {
501                 CWARN("mask failed: u64val "LPU64" >= %lu\n", u64val, 
502                       CFS_PAGE_SIZE);
503                 ret = -EINVAL;
504         }
505
506         return ret;
507 }
508 #else
509 #define obd_init_checks() do {} while(0)
510 #endif
511
512 extern spinlock_t obd_types_lock;
513 extern spinlock_t handle_lock;
514 extern int class_procfs_init(void);
515 extern int class_procfs_clean(void);
516
517 #ifdef __KERNEL__
518 static int __init init_obdclass(void)
519 #else
520 int init_obdclass(void)
521 #endif
522 {
523         int i, err;
524 #ifdef __KERNEL__
525         int lustre_register_fs(void);
526
527         printk(KERN_INFO "Lustre: OBD class driver, info@clusterfs.com\n");
528         printk(KERN_INFO "        Lustre Version: "LUSTRE_VERSION_STRING"\n");
529         printk(KERN_INFO "        Build Version: "BUILD_VERSION"\n");
530 #else
531         CDEBUG(D_INFO, "Lustre: OBD class driver, info@clusterfs.com\n");
532         CDEBUG(D_INFO, "        Lustre Version: "LUSTRE_VERSION_STRING"\n");
533         CDEBUG(D_INFO, "        Build Version: "BUILD_VERSION"\n");
534 #endif
535
536         spin_lock_init(&obd_types_lock);
537         spin_lock_init(&handle_lock);
538         cfs_waitq_init(&obd_race_waitq);
539         obd_zombie_impexp_init();
540
541         err = obd_init_checks();
542         if (err == -EOVERFLOW)
543                 return err;
544
545         class_init_uuidlist();
546         err = class_handle_init();
547         if (err)
548                 return err;
549
550         spin_lock_init(&obd_dev_lock);
551         INIT_LIST_HEAD(&obd_types);
552
553         err = cfs_psdev_register(&obd_psdev);
554         if (err) {
555                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
556                 return err;
557         }
558
559         /* This struct is already zerod for us (static global) */
560         for (i = 0; i < class_devno_max(); i++)
561                 obd_devs[i] = NULL;
562
563         /* Default the dirty page cache cap to 1/2 of system memory */
564         obd_max_dirty_pages = num_physpages / 2;
565
566         err = obd_init_caches();
567         if (err)
568                 return err;
569 #ifdef __KERNEL__
570         err = class_procfs_init();
571         if (err)
572                 return err;
573         err = lustre_register_fs();
574 #endif
575
576         return err;
577 }
578
579 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
580  * ifdef to the end of the file to cover module and versioning goo.*/
581 #ifdef __KERNEL__
582 static void cleanup_obdclass(void)
583 {
584         int i;
585         int lustre_unregister_fs(void);
586         ENTRY;
587
588         lustre_unregister_fs();
589
590         cfs_psdev_deregister(&obd_psdev);
591         for (i = 0; i < class_devno_max(); i++) {
592                 struct obd_device *obd = class_num2obd(i);
593                 if (obd && obd->obd_set_up &&
594                     OBT(obd) && OBP(obd, detach)) {
595                         /* XXX should this call generic detach otherwise? */
596                         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
597                         OBP(obd, detach)(obd);
598                 }
599         }
600
601         obd_cleanup_caches();
602         obd_sysctl_clean();
603
604         class_procfs_clean();
605
606         class_handle_cleanup();
607         class_exit_uuidlist();
608         EXIT;
609 }
610
611 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
612 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
613 MODULE_LICENSE("GPL");
614
615 cfs_module(obdclass, LUSTRE_VERSION_STRING, init_obdclass, cleanup_obdclass);
616 #endif