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