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