2 * linux/fs/ext2_obd/ext2_obd.c
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 * by Peter Braam <braam@clusterfs.com>
14 #include <linux/version.h>
15 #include <linux/module.h>
17 #include <linux/stat.h>
18 #include <linux/locks.h>
19 #include <linux/ext2_fs.h>
20 #include <linux/quotaops.h>
21 #include <asm/unistd.h>
22 #include <linux/obd_support.h>
23 #include <linux/obd_class.h>
24 #include <linux/obd_ext2.h>
26 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
29 void push_ctxt(struct run_ctxt *save, struct run_ctxt *new)
32 save->pwd = dget(current->fs->pwd);
33 save->pwdmnt = mntget(current->fs->pwdmnt);
36 set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
39 void pop_ctxt(struct run_ctxt *saved)
42 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
45 mntput(saved->pwdmnt);
48 static void filter_prep(struct obd_device *obddev)
50 struct run_ctxt saved;
58 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
59 rc = sys_mkdir("O", 0700);
60 rc = sys_mkdir("P", 0700);
61 rc = sys_mkdir("D", 0700);
62 rc = sys_mkdir("O/2", 0755);
63 if ( (fd = sys_open("D/status", O_RDWR | O_CREAT, 0700)) == -1 ) {
64 printk("OBD filter: cannot create status file\n");
67 if ( (rc = sys_fstat64(fd, &buf, 0)) ) {
68 printk("OBD filter: cannot stat status file\n");
71 if (buf.st_size == 0) {
72 rc = sys_write(fd, (char *)&lastino, sizeof(lastino));
73 if (rc != sizeof(lastino)) {
74 printk("OBD filter: error writing lastino\n");
78 rc = sys_read(fd, (char *)&lastino, sizeof(lastino));
79 if (rc != sizeof(lastino)) {
80 printk("OBD filter: error reading lastino\n");
84 obddev->u.filter.fo_lastino = lastino;
86 /* this is also the moment to steal operations */
87 file = filp_open("D/status", O_RDONLY, 0);
88 if (!file || IS_ERR(file)) {
92 inode = file->f_dentry->d_inode;
93 obddev->u.filter.fo_fop = file->f_op;
94 obddev->u.filter.fo_iop = inode->i_op;
95 obddev->u.filter.fo_aops = inode->i_mapping->a_ops;
101 printk("OBD filter: cannot close status file\n");
107 static void filter_post(struct obd_device *obddev)
109 struct run_ctxt saved;
113 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
114 if ( (fd = sys_open("D/status", O_RDWR | O_CREAT, 0700)) == -1 ) {
115 printk("OBD filter: cannot create status file\n");
118 rc = sys_write(fd, (char *)&obddev->u.filter.fo_lastino,
119 sizeof(obddev->u.filter.fo_lastino));
120 if (rc != sizeof(sizeof(obddev->u.filter.fo_lastino)) ) {
121 printk("OBD filter: error writing lastino\n");
126 printk("OBD filter: cannot close status file\n");
133 /* release per client resources */
134 static int filter_disconnect(struct obd_conn *conn)
136 /* XXX cleanup preallocated inodes */
137 return gen_disconnect(conn);
138 } /* ext2obd_disconnect */
140 /* mount the file system (secretly) */
141 static int filter_setup(struct obd_device *obddev, obd_count len,
145 struct obd_ioctl_data* data = buf;
146 struct vfsmount *mnt;
151 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
152 data->ioc_inlbuf1, NULL);
159 obddev->u.filter.fo_sb = mnt->mnt_root->d_inode->i_sb;
160 if (!obddev->u.filter.fo_sb) {
165 obddev->u.filter.fo_vfsmnt = mnt;
166 obddev->u.filter.fo_fstype = strdup(data->ioc_inlbuf2);
168 obddev->u.filter.fo_ctxt.pwdmnt = mnt;
169 obddev->u.filter.fo_ctxt.pwd = mnt->mnt_root;
170 obddev->u.filter.fo_ctxt.fs = KERNEL_DS;
179 static int filter_cleanup(struct obd_device * obddev)
181 struct super_block *sb;
185 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
190 if ( !list_empty(&obddev->obd_gen_clients) ) {
191 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
196 sb = obddev->u.filter.fo_sb;
197 if (!obddev->u.filter.fo_sb){
204 mntput(obddev->u.filter.fo_vfsmnt);
205 obddev->u.filter.fo_sb = 0;
206 kfree(obddev->u.filter.fo_fstype);
216 static struct file *filter_obj_open(struct obd_device *obddev,
222 struct run_ctxt saved;
223 struct super_block *sb;
225 sb = obddev->u.filter.fo_sb;
226 if (!sb || !sb->s_dev) {
227 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
233 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)oa->o_id);
238 sprintf(id, "O/%Ld", oa->o_id);
239 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
240 file = filp_open(id , O_RDONLY, 0);
244 error = PTR_ERR(file);
247 CDEBUG(D_INODE, "opening obdo %s\n", id);
250 file->f_op = obddev->u.filter.fo_fop;
251 file->f_dentry->d_inode->i_op = obddev->u.filter.fo_iop;
252 file->f_dentry->d_inode->i_mapping->a_ops = obddev->u.filter.fo_aops;
254 printk("Error opening object %s, error %d\n", id, error);
259 static struct inode *inode_from_obdo(struct obd_device *obddev,
265 file = filter_obj_open(obddev, oa);
267 printk("inode_from_obdo failed\n");
271 inode = iget(file->f_dentry->d_inode->i_sb, file->f_dentry->d_inode->i_ino);
276 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
280 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
281 inode->i_ino, (long)oa->o_id, oa->o_valid);
282 obdo_from_inode(oa, inode);
284 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
285 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
286 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
288 *((obd_rdev *)oa->o_inline) = rdev;
289 oa->o_obdflags |= OBD_FL_INLINEDATA;
290 oa->o_valid |= OBD_MD_FLINLINE;
294 else if (ext2obd_has_inline(inode)) {
295 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
296 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
297 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
298 oa->o_obdflags |= OBD_FL_INLINEDATA;
299 oa->o_valid |= OBD_MD_FLINLINE;
302 if (ext2obd_has_obdmd(inode)) {
303 /* XXX this will change when we don't store the obdmd in data */
304 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
305 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
306 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
307 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
308 oa->o_valid |= OBD_MD_FLOBDMD;
314 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
319 if ( !gen_client(conn) ) {
320 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
325 if ( !(inode = inode_from_obdo(conn->oc_dev, oa)) ) {
330 oa->o_valid &= ~OBD_MD_FLID;
331 filter_from_inode(oa, inode);
338 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
345 if (!gen_client(conn)) {
346 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
350 inode = inode_from_obdo(conn->oc_dev, oa);
356 iattr_from_obdo(&iattr, oa);
358 if ( inode->i_op->setattr ) {
359 rc = inode->i_op->setattr(&de, &iattr);
361 rc = inode_setattr(inode, &iattr);
369 static int filter_create (struct obd_conn* conn, struct obdo *oa)
372 struct run_ctxt saved;
373 struct obd_device *obddev = conn->oc_dev;
378 if (!gen_client(conn)) {
379 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
382 CDEBUG(D_IOCTL, "\n");
384 conn->oc_dev->u.filter.fo_lastino++;
385 oa->o_id = conn->oc_dev->u.filter.fo_lastino;
386 sprintf(name, "O/%Ld", oa->o_id);
387 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
388 CDEBUG(D_IOCTL, "\n");
389 if (sys_mknod(name, 0100644, 0)) {
390 printk("Error mknod obj %s\n", name);
395 CDEBUG(D_IOCTL, "\n");
396 rc = filter_setattr(conn, oa);
401 CDEBUG(D_IOCTL, "\n");
403 /* Set flags for fields we have set in ext2_new_inode */
404 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
405 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
406 OBD_MD_FLUID | OBD_MD_FLGID;
410 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
412 struct obd_device * obddev;
413 struct obd_client * cli;
414 struct inode * inode;
415 struct run_ctxt saved;
418 if (!(cli = gen_client(conn))) {
419 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
424 obddev = conn->oc_dev;
425 inode = inode_from_obdo(obddev, oa);
433 inode->i_mode = 010000;
436 sprintf(id, "O/%Ld", oa->o_id);
437 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
438 if (sys_unlink(id)) {
448 /* buffer must lie in user memory here */
449 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
450 obd_size *count, obd_off offset)
453 unsigned long retval;
456 if (!gen_client(conn)) {
457 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
462 file = filter_obj_open(conn->oc_dev, oa);
463 if (!file || IS_ERR(file)) {
465 return -PTR_ERR(file);
468 /* count doubles as retval */
469 retval = file->f_op->read(file, buf, *count, &offset);
484 /* buffer must lie in user memory here */
485 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
486 obd_size *count, obd_off offset)
490 unsigned long retval;
493 if (!gen_client(conn)) {
494 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
499 file = filter_obj_open(conn->oc_dev, oa);
500 if (!file || IS_ERR(file)) {
502 return -PTR_ERR(file);
505 /* count doubles as retval */
506 retval = file->f_op->write(file, buf, *count, &offset);
520 } /* ext2obd_write */
522 static int filter_pgcache_brw(int rw, struct obd_conn *conn,
531 struct super_block *sb;
533 int onum; /* index to oas */
534 int pnum; /* index to pages (bufs) */
535 unsigned long retval;
541 if (!gen_client(conn)) {
542 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
547 sb = conn->oc_dev->u.filter.fo_sb;
551 pnum = 0; /* pnum indexes buf 0..num_pages */
552 for (onum = 0; onum < num_oa; onum++) {
555 file = filter_obj_open(conn->oc_dev, oa[onum]);
556 if (!file || IS_ERR(file)) {
562 /* count doubles as retval */
563 for (pg = 0; pg < oa_bufs[onum]; pg++) {
564 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n",
565 rw, onum, pnum, file->f_dentry->d_inode->i_ino,
566 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
567 offset[pnum], count[pnum]);
572 buffer = kmap(pages[pnum]);
573 retval = file->f_op->write(file, buffer, count[pnum], &off);
575 CDEBUG(D_INODE, "retval %ld\n", retval);
577 loff_t off = offset[pnum];
578 char *buffer = kmap(pages[pnum]);
580 if (off >= file->f_dentry->d_inode->i_size) {
581 memset(buffer, 0, count[pnum]);
582 retval = count[pnum];
584 retval = file->f_op->read(file, buffer, count[pnum], &off);
588 if ( retval != count[pnum] ) {
594 CDEBUG(D_INODE, "retval %ld\n", retval);
598 /* sizes and blocks are set by generic_file_write */
599 /* ctimes/mtimes will follow with a setattr call */
606 error = (retval >= 0) ? 0 : retval;
610 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
612 struct super_block *sb;
617 if (!gen_client(conn)) {
618 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
623 sb = conn->oc_dev->u.filter.fo_sb;
625 err = sb->s_op->statfs(sb, statfs);
628 } /* ext2obd_statfs */
631 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
632 void *key, obd_count *vallen, void **val)
634 struct obd_device *obddev;
635 struct obd_client * cli;
638 if (!(cli = gen_client(conn))) {
639 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
643 obddev = conn->oc_dev;
645 if ( keylen == strlen("blocksize") &&
646 memcmp(key, "blocksize", keylen) == 0 ) {
647 *vallen = sizeof(int);
648 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
653 if ( keylen == strlen("blocksize_bits") &&
654 memcmp(key, "blocksize_bits", keylen) == 0 ){
655 *vallen = sizeof(int);
656 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
661 if ( keylen == strlen("root_ino") &&
662 memcmp(key, "root_ino", keylen) == 0 ){
663 *vallen = sizeof(int);
665 obddev->u.filter.fo_sb->s_root->d_inode->i_ino;
670 CDEBUG(D_IOCTL, "invalid key\n");
675 struct obd_ops filter_obd_ops = {
677 o_get_info: filter_get_info,
678 o_setup: filter_setup,
679 o_cleanup: filter_cleanup,
680 o_connect: gen_connect,
681 o_disconnect: filter_disconnect,
682 o_statfs: filter_statfs,
683 o_getattr: filter_getattr,
684 o_create: filter_create,
685 o_setattr: filter_setattr,
686 o_destroy: filter_destroy,
688 o_write: filter_write,
689 o_brw: filter_pgcache_brw,
691 o_preallocate: ext2obd_preallocate_inodes,
692 o_setattr: ext2obd_setattr,
693 o_punch: ext2obd_punch,
694 o_migrate: ext2obd_migrate,
695 o_copy: gen_copy_data,
696 o_iterate: ext2obd_iterate
703 void init_module(void)
705 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
706 obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
709 void cleanup_module(void)
711 obd_unregister_type(OBD_FILTER_DEVICENAME);
712 CDEBUG(D_MALLOC, "FILTER mem used %ld\n", filter_memory);