Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[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 Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * These are the only exported functions, they provide some generic
24  * infrastructure for managing object devices
25  */
26
27 #define DEBUG_SUBSYSTEM S_CLASS
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #ifdef __KERNEL__
32 #include <linux/config.h> /* for CONFIG_PROC_FS */
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/sched.h>
38 #include <linux/lp.h>
39 #include <linux/slab.h>
40 #include <linux/ioport.h>
41 #include <linux/fcntl.h>
42 #include <linux/delay.h>
43 #include <linux/skbuff.h>
44 #include <linux/proc_fs.h>
45 #include <linux/fs.h>
46 #include <linux/poll.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <linux/highmem.h>
50 #include <asm/io.h>
51 #include <asm/ioctls.h>
52 #include <asm/system.h>
53 #include <asm/poll.h>
54 #include <asm/uaccess.h>
55 #include <linux/miscdevice.h>
56 #include <linux/smp_lock.h>
57 #include <linux/seq_file.h>
58 #else
59 # include <liblustre.h>
60 #endif
61
62 #include <linux/obd_support.h>
63 #include <linux/obd_class.h>
64 #include <linux/lustre_debug.h>
65 #include <linux/lprocfs_status.h>
66 #ifdef __KERNEL__
67 #include <linux/lustre_build_version.h>
68 #include <linux/lustre_version.h>
69 #endif
70 #include <portals/list.h>
71
72 #ifndef __KERNEL__
73 /* liblustre workaround */
74 atomic_t portal_kmemory = {0};
75 #endif
76
77 struct semaphore obd_conf_sem;   /* serialize configuration commands */
78 struct obd_device obd_dev[MAX_OBD_DEVICES];
79 struct list_head obd_types;
80 #ifndef __KERNEL__
81 atomic_t obd_memory;
82 int obd_memmax;
83 #endif
84
85 int proc_version;
86
87 /* The following are visible and mutable through /proc/sys/lustre/. */
88 unsigned int obd_fail_loc;
89 unsigned int obd_timeout = 100;
90 char obd_lustre_upcall[128] = "DEFAULT"; /* or NONE or /full/path/to/upcall  */
91 unsigned int obd_sync_filter; /* = 0, don't sync by default */
92
93 DECLARE_WAIT_QUEUE_HEAD(obd_race_waitq);
94
95 #ifdef __KERNEL__
96 /*  opening /dev/obd */
97 static int obd_class_open(struct inode * inode, struct file * file)
98 {
99         ENTRY;
100
101         PORTAL_MODULE_USE;
102         RETURN(0);
103 }
104
105 /*  closing /dev/obd */
106 static int obd_class_release(struct inode * inode, struct file * file)
107 {
108         ENTRY;
109
110         PORTAL_MODULE_UNUSE;
111         RETURN(0);
112 }
113 #endif
114
115 static inline void obd_data2conn(struct lustre_handle *conn,
116                                  struct obd_ioctl_data *data)
117 {
118         memset(conn, 0, sizeof *conn);
119         conn->cookie = data->ioc_cookie;
120 }
121
122 static inline void obd_conn2data(struct obd_ioctl_data *data,
123                                  struct lustre_handle *conn)
124 {
125         data->ioc_cookie = conn->cookie;
126 }
127
128 int class_resolve_dev_name(uint32_t len, char *name)
129 {
130         int rc;
131         int dev;
132
133         if (!len || !name) {
134                 CERROR("No name passed,!\n");
135                 GOTO(out, rc = -EINVAL);
136         }
137         if (name[len - 1] != 0) {
138                 CERROR("Name not nul terminated!\n");
139                 GOTO(out, rc = -EINVAL);
140         }
141
142         CDEBUG(D_IOCTL, "device name %s\n", name);
143         dev = class_name2dev(name);
144         if (dev == -1) {
145                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
146                 GOTO(out, rc = -EINVAL);
147         }
148
149         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
150         rc = dev;
151
152 out:
153         RETURN(rc);
154 }
155
156 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
157 {
158         char *buf = NULL;
159         struct obd_ioctl_data *data;
160         struct portals_debug_ioctl_data *debug_data;
161         struct obd_device *obd = NULL;
162         int err = 0, len = 0, serialised = 0;
163         ENTRY;
164
165         if (current->fsuid != 0)
166                 RETURN(err = -EACCES);
167
168         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
169                 RETURN(err = -ENOTTY);
170
171         /* only for debugging */
172         if (cmd == PTL_IOC_DEBUG_MASK) {
173                 debug_data = (struct portals_debug_ioctl_data*)arg;
174                 portal_subsystem_debug = debug_data->subs;
175                 portal_debug = debug_data->debug;
176                 return 0;
177         }
178
179         switch (cmd) {
180         case OBD_IOC_BRW_WRITE:
181         case OBD_IOC_BRW_READ:
182         case OBD_IOC_GETATTR:
183         case ECHO_IOC_ENQUEUE:
184         case ECHO_IOC_CANCEL:
185         case OBD_IOC_CLIENT_RECOVER:
186         case OBD_IOC_CATLOGLIST:
187         case OBD_IOC_LLOG_INFO:
188         case OBD_IOC_LLOG_PRINT:
189         case OBD_IOC_LLOG_CANCEL:
190         case OBD_IOC_LLOG_CHECK:
191         case OBD_IOC_LLOG_REMOVE:
192                 break;
193         default:
194                 down(&obd_conf_sem);
195                 serialised = 1;
196                 break;
197         }
198
199         CDEBUG(D_IOCTL, "cmd = %x, obd = %p\n", cmd, obd);
200         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
201                 CERROR("OBD ioctl: data error\n");
202                 GOTO(out, err = -EINVAL);
203         }
204         data = (struct obd_ioctl_data *)buf;
205
206         switch (cmd) {
207         case OBD_IOC_PROCESS_CFG: {
208                 char *buf;
209                 struct lustre_cfg *lcfg;
210
211                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
212                         CERROR("No config buffer passed!\n");
213                         GOTO(out, err = -EINVAL);
214                 }
215                 err = lustre_cfg_getdata(&buf, data->ioc_plen1,
216                                          data->ioc_pbuf1, 0);
217                 if (err)
218                         GOTO(out, err);
219                 lcfg = (struct lustre_cfg* ) buf;
220
221                 err = class_process_config(lcfg);
222                 lustre_cfg_freedata(buf, data->ioc_plen1);
223                 GOTO(out, err);
224         }
225
226         case OBD_GET_VERSION:
227                 if (!data->ioc_inlbuf1) {
228                         CERROR("No buffer passed in ioctl\n");
229                         GOTO(out, err = -EINVAL);
230                 }
231
232                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
233                         CERROR("ioctl buffer too small to hold version\n");
234                         GOTO(out, err = -EINVAL);
235                 }
236
237                 memcpy(data->ioc_bulk, BUILD_VERSION,
238                        strlen(BUILD_VERSION) + 1);
239
240                 err = copy_to_user((void *)arg, data, len);
241                 if (err)
242                         err = -EFAULT;
243                 GOTO(out, err);
244
245         case OBD_IOC_NAME2DEV: {
246                 /* Resolve a device name.  This does not change the
247                  * currently selected device.
248                  */
249                 int dev;
250
251                 dev = class_resolve_dev_name(data->ioc_inllen1,
252                                              data->ioc_inlbuf1);
253                 data->ioc_dev = dev;
254                 if (dev < 0)
255                         GOTO(out, err = -EINVAL);
256
257                 err = copy_to_user((void *)arg, data, sizeof(*data));
258                 if (err)
259                         err = -EFAULT;
260                 GOTO(out, err);
261         }
262
263         case OBD_IOC_UUID2DEV: {
264                 /* Resolve a device uuid.  This does not change the
265                  * currently selected device.
266                  */
267                 int dev;
268                 struct obd_uuid uuid;
269
270                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
271                         CERROR("No UUID passed!\n");
272                         GOTO(out, err = -EINVAL);
273                 }
274                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
275                         CERROR("UUID not NUL terminated!\n");
276                         GOTO(out, err = -EINVAL);
277                 }
278
279                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
280                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
281                 dev = class_uuid2dev(&uuid);
282                 data->ioc_dev = dev;
283                 if (dev == -1) {
284                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
285                                data->ioc_inlbuf1);
286                         GOTO(out, err = -EINVAL);
287                 }
288
289                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
290                        dev);
291                 err = copy_to_user((void *)arg, data, sizeof(*data));
292                 if (err)
293                         err = -EFAULT;
294                 GOTO(out, err);
295         }
296
297
298         case OBD_IOC_CLOSE_UUID: {
299                 ptl_nid_t       peer_nid;
300                 __u32           peer_nal;
301                 CDEBUG(D_IOCTL, "closing all connections to uuid %s\n",
302                        data->ioc_inlbuf1);
303                 lustre_uuid_to_peer(data->ioc_inlbuf1, &peer_nal, &peer_nid);
304                 GOTO(out, err = 0);
305         }
306
307         }
308
309         if (data->ioc_dev >= MAX_OBD_DEVICES) {
310                 CERROR("OBD ioctl: No device\n");
311                 GOTO(out, err = -EINVAL);
312         } 
313         obd = &obd_dev[data->ioc_dev];
314         if (!(obd && obd->obd_set_up) || obd->obd_stopping) {
315                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
316                 GOTO(out, err = -EINVAL);
317         }
318
319         switch(cmd) {
320         case OBD_IOC_NO_TRANSNO: {
321                 if (!obd->obd_attached) {
322                         CERROR("Device %d not attached\n", obd->obd_minor);
323                         GOTO(out, err = -ENODEV);
324                 }
325                 CDEBUG(D_IOCTL,
326                        "disabling committed-transno notifications on %d\n",
327                        obd->obd_minor);
328                 obd->obd_no_transno = 1;
329                 GOTO(out, err = 0);
330         }
331
332         default: {
333                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
334                 if (err)
335                         GOTO(out, err);
336
337                 err = copy_to_user((void *)arg, data, len);
338                 if (err)
339                         err = -EFAULT;
340                 GOTO(out, err);
341         }
342         }
343
344  out:
345         if (buf)
346                 obd_ioctl_freedata(buf, len);
347         if (serialised)
348                 up(&obd_conf_sem);
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 inode *inode, struct file *filp,
358                            unsigned int cmd, unsigned long arg)
359 {
360         return class_handle_ioctl(cmd, arg);
361 }
362
363 /* declare character device */
364 static struct file_operations obd_psdev_fops = {
365         .owner   = THIS_MODULE,
366         .ioctl   = obd_class_ioctl,     /* ioctl */
367         .open    = obd_class_open,      /* open */
368         .release = obd_class_release,   /* release */
369 };
370
371 /* modules setup */
372 static struct miscdevice obd_psdev = {
373         .minor = OBD_MINOR,
374         .name  = "obd_psdev",
375         .fops  = &obd_psdev_fops,
376 };
377 #else
378 void *obd_psdev = NULL;
379 #endif
380
381 EXPORT_SYMBOL(obd_dev);
382 EXPORT_SYMBOL(obdo_cachep);
383 EXPORT_SYMBOL(obd_fail_loc);
384 EXPORT_SYMBOL(obd_race_waitq);
385 EXPORT_SYMBOL(obd_timeout);
386 EXPORT_SYMBOL(obd_lustre_upcall);
387 EXPORT_SYMBOL(obd_sync_filter);
388 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
389 EXPORT_SYMBOL(ptlrpc_abort_inflight_superhack);
390 EXPORT_SYMBOL(proc_lustre_root);
391
392 EXPORT_SYMBOL(class_register_type);
393 EXPORT_SYMBOL(class_unregister_type);
394 EXPORT_SYMBOL(class_get_type);
395 EXPORT_SYMBOL(class_put_type);
396 EXPORT_SYMBOL(class_name2dev);
397 EXPORT_SYMBOL(class_name2obd);
398 EXPORT_SYMBOL(class_uuid2dev);
399 EXPORT_SYMBOL(class_uuid2obd);
400 EXPORT_SYMBOL(class_find_client_obd);
401 EXPORT_SYMBOL(class_devices_in_group);
402 EXPORT_SYMBOL(__class_export_put);
403 EXPORT_SYMBOL(class_new_export);
404 EXPORT_SYMBOL(class_unlink_export);
405 EXPORT_SYMBOL(class_import_get);
406 EXPORT_SYMBOL(class_import_put);
407 EXPORT_SYMBOL(class_new_import);
408 EXPORT_SYMBOL(class_destroy_import);
409 EXPORT_SYMBOL(class_connect);
410 EXPORT_SYMBOL(class_conn2export);
411 EXPORT_SYMBOL(class_exp2obd);
412 EXPORT_SYMBOL(class_conn2obd);
413 EXPORT_SYMBOL(class_exp2cliimp);
414 EXPORT_SYMBOL(class_conn2cliimp);
415 EXPORT_SYMBOL(class_disconnect);
416 EXPORT_SYMBOL(class_disconnect_exports);
417
418 EXPORT_SYMBOL(oig_init);
419 EXPORT_SYMBOL(oig_release);
420 EXPORT_SYMBOL(oig_add_one);
421 EXPORT_SYMBOL(oig_wait);
422 EXPORT_SYMBOL(oig_complete_one);
423
424 /* uuid.c */
425 EXPORT_SYMBOL(class_uuid_unparse);
426 EXPORT_SYMBOL(lustre_uuid_to_peer);
427
428 EXPORT_SYMBOL(class_handle_hash);
429 EXPORT_SYMBOL(class_handle_unhash);
430 EXPORT_SYMBOL(class_handle2object);
431
432 /* config.c */
433 EXPORT_SYMBOL(class_get_profile);
434 EXPORT_SYMBOL(class_del_profile);
435 EXPORT_SYMBOL(class_process_config);
436 EXPORT_SYMBOL(class_config_parse_llog);
437 EXPORT_SYMBOL(class_config_dump_llog);
438 EXPORT_SYMBOL(class_attach);
439 EXPORT_SYMBOL(class_setup);
440 EXPORT_SYMBOL(class_cleanup);
441 EXPORT_SYMBOL(class_detach);
442
443 #ifdef LPROCFS
444 int obd_proc_read_version(char *page, char **start, off_t off, int count,
445                           int *eof, void *data)
446 {
447         *eof = 1;
448         return snprintf(page, count, "%s\n", BUILD_VERSION);
449 }
450
451 int obd_proc_read_kernel_version(char *page, char **start, off_t off, int count,
452                                  int *eof, void *data)
453 {
454         *eof = 1;
455         return snprintf(page, count, "%u\n", LUSTRE_KERNEL_VERSION);
456 }
457
458 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
459                          int *eof, void *data)
460 {
461         *eof = 1;
462         return snprintf(page, count, "%s\n",
463 #ifdef ENABLE_PINGER
464                         "on"
465 #else
466                         "off"
467 #endif
468                        );
469 }
470
471 /* Root for /proc/fs/lustre */
472 struct proc_dir_entry *proc_lustre_root = NULL;
473 struct lprocfs_vars lprocfs_base[] = {
474         { "version", obd_proc_read_version, NULL, NULL },
475         { "kernel_version", obd_proc_read_kernel_version, NULL, NULL },
476         { "pinger", obd_proc_read_pinger, NULL, NULL },
477         { 0 }
478 };
479
480 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
481 {
482         if (*pos >= MAX_OBD_DEVICES)
483                 return NULL;
484         return &obd_dev[*pos];
485 }
486
487 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
488 {
489 }
490
491 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
492 {
493         ++*pos;
494         if (*pos >= MAX_OBD_DEVICES)
495                 return NULL;
496         return &obd_dev[*pos];
497 }
498
499 static int obd_device_list_seq_show(struct seq_file *p, void *v)
500 {
501         struct obd_device *obd = (struct obd_device *)v;
502         int index = obd - &obd_dev[0];
503         char *status;
504
505         if (!obd->obd_type)
506                 return 0;
507         if (obd->obd_stopping)
508                 status = "ST";
509         else if (obd->obd_set_up)
510                 status = "UP";
511         else if (obd->obd_attached)
512                 status = "AT";
513         else
514                 status = "--";
515
516         return seq_printf(p, "%3d %s %s %s %s %d\n",
517                           (int)index, status, obd->obd_type->typ_name,
518                           obd->obd_name, obd->obd_uuid.uuid,
519                           atomic_read(&obd->obd_refcount));
520 }
521
522 struct seq_operations obd_device_list_sops = {
523         .start = obd_device_list_seq_start,
524         .stop = obd_device_list_seq_stop,
525         .next = obd_device_list_seq_next,
526         .show = obd_device_list_seq_show,
527 };
528
529 static int obd_device_list_open(struct inode *inode, struct file *file)
530 {
531         struct proc_dir_entry *dp = PDE(inode);
532         struct seq_file *seq;
533         int rc = seq_open(file, &obd_device_list_sops);
534
535         if (rc)
536                 return rc;
537
538         seq = file->private_data;
539         seq->private = dp->data;
540
541         return 0;
542 }
543
544 struct file_operations obd_device_list_fops = {
545         .owner   = THIS_MODULE,
546         .open    = obd_device_list_open,
547         .read    = seq_read,
548         .llseek  = seq_lseek,
549         .release = seq_release,
550 };
551 #endif
552
553 #ifdef __KERNEL__
554 static int __init init_obdclass(void)
555 #else
556 int init_obdclass(void)
557 #endif
558 {
559         struct obd_device *obd;
560 #ifdef LPROCFS
561         struct proc_dir_entry *entry;
562 #endif
563         int err;
564         int i;
565
566         printk(KERN_INFO "Lustre: OBD class driver Build Version: "
567                BUILD_VERSION", info@clusterfs.com\n");
568
569         class_init_uuidlist();
570         err = class_handle_init();
571         if (err)
572                 return err;
573
574         sema_init(&obd_conf_sem, 1);
575         INIT_LIST_HEAD(&obd_types);
576
577         err = misc_register(&obd_psdev);
578         if (err) {
579                 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
580                 return err;
581         }
582
583         /* This struct is already zerod for us (static global) */
584         for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++)
585                 obd->obd_minor = i;
586
587         err = obd_init_caches();
588         if (err)
589                 return err;
590
591 #ifdef __KERNEL__
592         obd_sysctl_init();
593 #endif
594
595 #ifdef LPROCFS
596         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
597         if (!proc_lustre_root) {
598                 printk(KERN_ERR
599                        "LustreError: error registering /proc/fs/lustre\n");
600                 RETURN(-ENOMEM);
601         }
602         proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
603         entry = create_proc_entry("devices", 0444, proc_lustre_root);
604         if (entry == NULL) {
605                 printk(KERN_ERR "LustreError: error registering "
606                        "/proc/fs/lustre/devices\n");
607                 lprocfs_remove(proc_lustre_root);
608                 RETURN(-ENOMEM);
609         }
610         entry->proc_fops = &obd_device_list_fops;
611 #endif
612         return 0;
613 }
614
615 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
616  * ifdef to the end of the file to cover module and versioning goo.*/
617 #ifdef __KERNEL__
618
619 static void cleanup_obdclass(void)
620 {
621         int i;
622         int leaked;
623         ENTRY;
624
625         misc_deregister(&obd_psdev);
626         for (i = 0; i < MAX_OBD_DEVICES; i++) {
627                 struct obd_device *obd = &obd_dev[i];
628                 if (obd->obd_type && obd->obd_set_up &&
629                     OBT(obd) && OBP(obd, detach)) {
630                         /* XXX should this call generic detach otherwise? */
631                         OBP(obd, detach)(obd);
632                 }
633         }
634
635         obd_cleanup_caches();
636         obd_sysctl_clean();
637 #ifdef LPROCFS
638         if (proc_lustre_root) {
639                 lprocfs_remove(proc_lustre_root);
640                 proc_lustre_root = NULL;
641         }
642 #endif
643
644         class_handle_cleanup();
645         class_exit_uuidlist();
646
647         leaked = atomic_read(&obd_memory);
648         CDEBUG(leaked ? D_ERROR : D_INFO,
649                "obd mem max: %d leaked: %d\n", obd_memmax, leaked);
650
651         EXIT;
652 }
653
654 /* Check that we're building against the appropriate version of the Lustre
655  * kernel patch */
656 #include <linux/lustre_version.h>
657 #define LUSTRE_MIN_VERSION 32
658 #define LUSTRE_MAX_VERSION 36
659 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
660 # error Cannot continue: Your Lustre kernel patch is older than the sources
661 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
662 # error Cannot continue: Your Lustre sources are older than the kernel patch
663 #endif
664
665 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
666 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
667 MODULE_LICENSE("GPL");
668
669 module_init(init_obdclass);
670 module_exit(cleanup_obdclass);
671 #endif