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