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