1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * These are the only exported functions, they provide some generic
10 * infrastructure for managing object devices
12 * Object Devices Class Driver
13 * Copyright (C) 2002 Cluster File Systems, Inc.
17 #include <linux/config.h> /* for CONFIG_PROC_FS */
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/ioport.h>
26 #include <linux/fcntl.h>
27 #include <linux/delay.h>
28 #include <linux/skbuff.h>
29 #include <linux/proc_fs.h>
31 #include <linux/poll.h>
32 #include <linux/init.h>
33 #include <linux/list.h>
35 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <linux/miscdevice.h>
40 #define DEBUG_SUBSYSTEM S_CLASS
42 #include <linux/obd_support.h>
43 #include <linux/obd_class.h>
44 #include <linux/smp_lock.h>
46 struct semaphore obd_conf_sem; /* serialize configuration commands */
47 struct obd_device obd_dev[MAX_OBD_DEVICES];
48 struct list_head obd_types;
49 unsigned long obd_memory = 0;
50 unsigned long obd_fail_loc = 0;
52 extern struct obd_type *class_nm_to_type(char *nm);
54 /* opening /dev/obd */
55 static int obd_class_open(struct inode * inode, struct file * file)
59 file->private_data = NULL;
64 /* closing /dev/obd */
65 static int obd_class_release(struct inode * inode, struct file * file)
69 if (file->private_data)
70 file->private_data = NULL;
77 inline void obd_data2conn(struct lustre_handle *conn, struct obd_ioctl_data *data)
79 conn->addr = data->ioc_addr;
80 conn->cookie = data->ioc_cookie;
84 inline void obd_conn2data(struct obd_ioctl_data *data, struct lustre_handle *conn)
86 data->ioc_addr = conn->addr;
87 data->ioc_cookie = conn->cookie;
91 /* to control /dev/obd */
92 static int obd_class_ioctl (struct inode * inode, struct file * filp,
93 unsigned int cmd, unsigned long arg)
97 struct obd_ioctl_data *data;
98 struct obd_device *obd = filp->private_data;
99 struct lustre_handle conn;
100 int rw = OBD_BRW_READ;
106 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS &&
107 cmd != OBD_IOC_LIST &&
108 cmd != OBD_IOC_NAME2DEV && cmd != OBD_IOC_NEWDEV) {
109 CERROR("OBD ioctl: No device\n");
110 GOTO(out, err=-EINVAL);
112 if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
113 CERROR("OBD ioctl: data error\n");
114 GOTO(out, err=-EINVAL);
116 data = (struct obd_ioctl_data *)buf;
120 GOTO(out, err=-EINVAL);
121 case OBD_IOC_DEVICE: {
122 CDEBUG(D_IOCTL, "\n");
123 if (data->ioc_dev >= MAX_OBD_DEVICES || data->ioc_dev < 0) {
124 CERROR("OBD ioctl: DEVICE insufficient devices\n");
125 GOTO(out, err=-EINVAL);
127 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
129 filp->private_data = &obd_dev[data->ioc_dev];
135 char *buf2 = data->ioc_bulk;
136 int remains = data->ioc_inllen1;
138 if (!data->ioc_inlbuf1) {
139 CERROR("No buffer passed!\n");
140 GOTO(out, err=-EINVAL);
144 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
147 struct obd_device *obd = &obd_dev[i];
150 if (obd->obd_flags & OBD_SET_UP)
154 l = snprintf(buf2, remains, "%2d %s %s %s %s\n",
155 i, status, obd->obd_type->typ_name,
156 obd->obd_name, obd->obd_uuid);
160 CERROR("not enough space for device listing\n");
165 err = copy_to_user((int *)arg, data, len);
170 case OBD_IOC_NAME2DEV: {
171 /* Resolve a device name. This does not change the
172 * currently selected device.
176 if (!data->ioc_inllen1 || !data->ioc_inlbuf1 ) {
177 CERROR("No name passed,!\n");
178 GOTO(out, err=-EINVAL);
180 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
181 CERROR("Name not nul terminated!\n");
182 GOTO(out, err=-EINVAL);
185 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
186 dev = class_name2dev(data->ioc_inlbuf1);
189 CDEBUG(D_IOCTL, "No device for name %s!\n",
191 GOTO(out, err=-EINVAL);
194 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
196 err = copy_to_user((int *)arg, data, sizeof(*data));
200 case OBD_IOC_UUID2DEV: {
201 /* Resolve a device uuid. This does not change the
202 * currently selected device.
206 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
207 CERROR("No UUID passed!\n");
208 GOTO(out, err=-EINVAL);
210 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
211 CERROR("Name not nul terminated!\n");
212 GOTO(out, err=-EINVAL);
215 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
216 dev = class_uuid2dev(data->ioc_inlbuf1);
219 CDEBUG(D_IOCTL, "No device for name %s!\n",
221 GOTO(out, err=-EINVAL);
224 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
226 err = copy_to_user((int *)arg, data, sizeof(*data));
230 case OBD_IOC_NEWDEV: {
234 filp->private_data = NULL;
235 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
236 struct obd_device *obd = &obd_dev[i];
237 if (!obd->obd_type) {
238 filp->private_data = obd;
247 GOTO(out, err=-EINVAL);
249 err = copy_to_user((int *)arg, data, sizeof(*data));
253 case OBD_IOC_ATTACH: {
254 struct obd_type *type;
256 /* have we attached a type to this device */
257 if (obd->obd_flags & OBD_ATTACHED) {
258 CERROR("OBD: Device %d already typed as %s.\n",
259 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
260 GOTO(out, err=-EBUSY);
263 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
264 CERROR("No type passed!\n");
265 GOTO(out, err=-EINVAL);
267 if (data->ioc_inlbuf1[data->ioc_inllen1-1] !=0) {
268 CERROR("Type not nul terminated!\n");
269 GOTO(out, err=-EINVAL);
272 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
273 MKSTR(data->ioc_inlbuf1),
274 MKSTR(data->ioc_inlbuf2), MKSTR(data->ioc_inlbuf3));
277 type = class_nm_to_type(data->ioc_inlbuf1);
279 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
280 GOTO(out, err=-EINVAL);
283 obd->obd_type = type;
284 INIT_LIST_HEAD(&obd->obd_exports);
287 if (OBT(obd) && OBP(obd, attach))
288 err = OBP(obd,attach)(obd, sizeof(*data), data);
290 obd->obd_type = NULL;
292 obd->obd_flags |= OBD_ATTACHED;
294 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
295 obd->obd_minor, data->ioc_inlbuf1);
296 if (data->ioc_inlbuf2) {
297 int len = strlen(data->ioc_inlbuf2) + 1;
298 OBD_ALLOC(obd->obd_name, len);
299 if (!obd->obd_name) {
300 CERROR("no memory\n");
303 memcpy(obd->obd_name, data->ioc_inlbuf2, len);
304 //obd->obd_proc_entry =
305 // proc_lustre_register_obd_device(obd);
307 CERROR("WARNING: unnamed obd device\n");
308 obd->obd_proc_entry = NULL;
311 if (data->ioc_inlbuf3) {
312 int len = strlen(data->ioc_inlbuf3);
314 CERROR("uuid should be shorter than 37 bytes\n");
316 OBD_FREE(obd->obd_name,
317 strlen(obd->obd_name) + 1);
318 GOTO(out, err=-EINVAL);
320 memcpy(obd->obd_uuid, data->ioc_inlbuf3,
321 sizeof(obd->obd_uuid));
330 case OBD_IOC_DETACH: {
332 if (obd->obd_flags & OBD_SET_UP) {
333 CERROR("OBD device %d still set up\n", obd->obd_minor);
334 GOTO(out, err=-EBUSY);
336 if (! (obd->obd_flags & OBD_ATTACHED) ) {
337 CERROR("OBD device %d not attached\n", obd->obd_minor);
338 GOTO(out, err=-ENODEV);
340 if ( !list_empty(&obd->obd_exports) ) {
341 CERROR("OBD device %d has exports\n",
343 GOTO(out, err=-EBUSY);
347 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
348 obd->obd_name = NULL;
351 //if (obd->obd_proc_entry)
352 // proc_lustre_release_obd_device(obd);
354 obd->obd_flags &= ~OBD_ATTACHED;
355 obd->obd_type->typ_refcnt--;
356 obd->obd_type = NULL;
361 case OBD_IOC_SETUP: {
362 /* have we attached a type to this device? */
363 if (!(obd->obd_flags & OBD_ATTACHED)) {
364 CERROR("Device %d not attached\n", obd->obd_minor);
365 GOTO(out, err=-ENODEV);
368 /* has this been done already? */
369 if ( obd->obd_flags & OBD_SET_UP ) {
370 CERROR("Device %d already setup (type %s)\n",
371 obd->obd_minor, obd->obd_type->typ_name);
372 GOTO(out, err=-EBUSY);
375 if ( OBT(obd) && OBP(obd, setup) )
376 err = obd_setup(obd, sizeof(*data), data);
379 obd->obd_type->typ_refcnt++;
380 obd->obd_flags |= OBD_SET_UP;
385 case OBD_IOC_CLEANUP: {
386 /* have we attached a type to this device? */
387 if (!(obd->obd_flags & OBD_ATTACHED)) {
388 CERROR("Device %d not attached\n", obd->obd_minor);
389 GOTO(out, err=-ENODEV);
392 if ( OBT(obd) && OBP(obd, cleanup) )
393 err = obd_cleanup(obd);
396 obd->obd_flags &= ~OBD_SET_UP;
397 obd->obd_type->typ_refcnt--;
402 case OBD_IOC_CONNECT: {
403 obd_data2conn(&conn, data);
405 err = obd_connect(&conn, obd);
407 CDEBUG(D_IOCTL, "assigned export %Lx\n", conn.addr);
408 obd_conn2data(data, &conn);
412 err = copy_to_user((int *)arg, data, sizeof(*data));
416 case OBD_IOC_DISCONNECT: {
417 obd_data2conn(&conn, data);
418 err = obd_disconnect(&conn);
422 case OBD_IOC_DEC_USE_COUNT: {
427 case OBD_IOC_CREATE: {
428 struct lov_stripe_md *ea;
429 obd_data2conn(&conn, data);
432 err = obd_create(&conn, &data->ioc_obdo1, &ea);
436 err = copy_to_user((int *)arg, data, sizeof(*data));
440 case OBD_IOC_GETATTR: {
442 obd_data2conn(&conn, data);
443 err = obd_getattr(&conn, &data->ioc_obdo1, NULL);
447 err = copy_to_user((int *)arg, data, sizeof(*data));
451 case OBD_IOC_SETATTR: {
452 obd_data2conn(&conn, data);
453 err = obd_setattr(&conn, &data->ioc_obdo1, NULL);
457 err = copy_to_user((int *)arg, data, sizeof(*data));
461 case OBD_IOC_DESTROY: {
463 obd_data2conn(&conn, data);
465 err = obd_destroy(&conn, &data->ioc_obdo1, NULL);
469 err = copy_to_user((int *)arg, data, sizeof(*data));
473 case OBD_IOC_BRW_WRITE:
475 case OBD_IOC_BRW_READ: {
476 struct lov_stripe_md smd;
478 struct page **bufs = NULL;
479 obd_size *counts = NULL;
480 obd_off *offsets = NULL;
481 obd_flag *flags = NULL;
486 obd_data2conn(&conn, data);
488 pages = data->ioc_plen1 / PAGE_SIZE;
490 CDEBUG(D_INODE, "BRW %s with %d pages\n",
491 rw == OBD_BRW_READ ? "read" : "write", pages);
492 OBD_ALLOC(bufs, pages * sizeof(*bufs));
493 OBD_ALLOC(counts, pages * sizeof(*counts));
494 OBD_ALLOC(offsets, pages * sizeof(*offsets));
495 OBD_ALLOC(flags, pages * sizeof(*flags));
496 if (!bufs || !counts || !offsets || !flags) {
497 CERROR("no memory for %d BRW per-page data\n", pages);
498 GOTO(brw_free, err = -ENOMEM);
501 memset(&smd, 0, sizeof(smd));
502 smd.lmd_object_id = data->ioc_obdo1.o_id;
504 from = (&data->ioc_pbuf1)[0];
505 off = data->ioc_offset;
507 for (j = 0; j < pages;
508 j++, off += PAGE_SIZE, from += PAGE_SIZE) {
511 to = __get_free_pages(GFP_KERNEL, 0);
515 *)to,from,PAGE_SIZE))
518 CERROR("no memory for brw pages\n");
519 GOTO(brw_cleanup, err = -ENOMEM);
521 bufs[j] = virt_to_page(to);
522 counts[j] = PAGE_SIZE;
527 err = obd_brw(rw, &conn, &smd, j, bufs, counts, offsets, flags,
532 for (j = 0; j < pages; j++)
534 __free_pages(bufs[j], 0);
536 OBD_FREE(bufs, pages * sizeof(*bufs));
537 OBD_FREE(counts, pages * sizeof(*counts));
538 OBD_FREE(offsets, pages * sizeof(*offsets));
539 OBD_FREE(flags, pages * sizeof(*flags));
543 obd_data2conn(&conn, data);
545 err = obd_iocontrol(cmd, &conn, len, data, NULL);
549 err = copy_to_user((int *)arg, data, len);
558 } /* obd_class_ioctl */
562 /* declare character device */
563 static struct file_operations obd_psdev_fops = {
564 ioctl: obd_class_ioctl, /* ioctl */
565 open: obd_class_open, /* open */
566 release: obd_class_release, /* release */
570 #define OBD_MINOR 241
571 static struct miscdevice obd_psdev = {
578 EXPORT_SYMBOL(obd_dev);
579 EXPORT_SYMBOL(obdo_cachep);
580 EXPORT_SYMBOL(obd_memory);
581 EXPORT_SYMBOL(obd_fail_loc);
583 EXPORT_SYMBOL(class_register_type);
584 EXPORT_SYMBOL(class_unregister_type);
585 EXPORT_SYMBOL(class_name2dev);
586 EXPORT_SYMBOL(class_uuid2dev);
587 EXPORT_SYMBOL(class_uuid2obd);
588 EXPORT_SYMBOL(class_new_export);
589 EXPORT_SYMBOL(class_connect);
590 EXPORT_SYMBOL(class_conn2export);
591 EXPORT_SYMBOL(class_rconn2export);
592 EXPORT_SYMBOL(class_conn2obd);
593 EXPORT_SYMBOL(class_disconnect);
594 //EXPORT_SYMBOL(class_multi_setup);
595 //EXPORT_SYMBOL(class_multi_cleanup);
597 static int __init init_obdclass(void)
602 printk(KERN_INFO "OBD class driver v0.9, info@clusterfs.com\n");
604 sema_init(&obd_conf_sem, 1);
605 INIT_LIST_HEAD(&obd_types);
607 if ((err = misc_register(&obd_psdev))) {
608 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
612 for (i = 0; i < MAX_OBD_DEVICES; i++) {
613 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
614 obd_dev[i].obd_minor = i;
615 INIT_LIST_HEAD(&obd_dev[i].obd_exports);
618 err = obd_init_caches();
625 static void __exit cleanup_obdclass(void)
630 misc_deregister(&obd_psdev);
631 for (i = 0; i < MAX_OBD_DEVICES; i++) {
632 struct obd_device *obd = &obd_dev[i];
633 if (obd->obd_type && (obd->obd_flags & OBD_SET_UP) &&
634 OBT(obd) && OBP(obd, detach)) {
635 /* XXX should this call generic detach otherwise? */
636 OBP(obd, detach)(obd);
640 obd_cleanup_caches();
642 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
646 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
647 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
648 MODULE_LICENSE("GPL");
650 module_init(init_obdclass);
651 module_exit(cleanup_obdclass);