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>
23 #define DEBUG_SUBSYSTEM S_FILTER
25 #include <linux/obd_class.h>
26 #include <linux/obd_ext2.h>
27 #include <linux/obd_filter.h>
29 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
32 #define FILTER_ROOTINO 2
35 static char * obd_type_by_mode[S_IFMT >> S_SHIFT] = {
37 [S_IFREG >> S_SHIFT] "R",
38 [S_IFDIR >> S_SHIFT] "D",
39 [S_IFCHR >> S_SHIFT] "C",
40 [S_IFBLK >> S_SHIFT] "B",
41 [S_IFIFO >> S_SHIFT] "F",
42 [S_IFSOCK >> S_SHIFT] "S",
43 [S_IFLNK >> S_SHIFT] "L"
48 /* push / pop to root of obd store */
49 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new)
52 save->pwd = dget(current->fs->pwd);
53 save->pwdmnt = mntget(current->fs->pwdmnt);
56 set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
59 void pop_ctxt(struct obd_run_ctxt *saved)
62 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
65 mntput(saved->pwdmnt);
68 /* utility to make a directory */
69 static int simple_mkdir(struct dentry *dir, char *name, int mode)
71 struct dentry *dchild;
75 dchild = lookup_one_len(name, dir, strlen(name));
78 return PTR_ERR(dchild);
81 if (dchild->d_inode) {
87 err = vfs_mkdir(dir->d_inode, dchild, mode);
94 /* write the pathname into the string */
95 static void filter_id(char *buf, obd_id id, obd_mode mode)
97 sprintf(buf, "O/%s/%Ld", obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT],
101 /* setup the object store with correct subdirectories */
102 static void filter_prep(struct obd_device *obddev)
104 struct obd_run_ctxt saved;
112 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
113 rc = simple_mkdir(current->fs->pwd, "O", 0700);
114 rc = simple_mkdir(current->fs->pwd, "P", 0700);
115 rc = simple_mkdir(current->fs->pwd, "D", 0700);
116 file = filp_open("O", O_RDONLY, 0);
117 if (!file || IS_ERR(file)) {
118 CERROR("cannot open O\n");
121 rc = simple_mkdir(file->f_dentry, "R", 0700); /* regular */
122 rc = simple_mkdir(file->f_dentry, "D", 0700); /* directory */
123 rc = simple_mkdir(file->f_dentry, "L", 0700); /* symbolic links */
124 rc = simple_mkdir(file->f_dentry, "C", 0700); /* character devices */
125 rc = simple_mkdir(file->f_dentry, "B", 0700); /* block devices */
126 rc = simple_mkdir(file->f_dentry, "F", 0700); /* fifo's */
127 rc = simple_mkdir(file->f_dentry, "S", 0700); /* sockets */
128 filp_close(file, NULL);
130 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
131 file = filp_open(rootid, O_RDWR | O_CREAT, 00755);
133 CERROR("OBD filter: cannot make root directory");
137 rc = simple_mkdir(current->fs->pwd, rootid, 0755);
139 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
140 if ( !file || IS_ERR(file) ) {
141 CERROR("OBD filter: cannot open/create status file\n");
145 /* steal operations */
146 inode = file->f_dentry->d_inode;
147 obddev->u.filter.fo_fop = file->f_op;
148 obddev->u.filter.fo_iop = inode->i_op;
149 obddev->u.filter.fo_aops = inode->i_mapping->a_ops;
152 if (inode->i_size == 0) {
153 rc = file->f_op->write(file, (char *)&lastino,
154 sizeof(lastino), &off);
155 if (rc != sizeof(lastino)) {
156 CERROR("OBD filter: error writing lastino\n");
160 rc = file->f_op->read(file, (char *)&lastino, sizeof(lastino),
162 if (rc != sizeof(lastino)) {
163 CERROR("OBD filter: error reading lastino\n");
167 obddev->u.filter.fo_lastino = lastino;
174 /* cleanup the filter: write last used object id to status file */
175 static void filter_post(struct obd_device *obddev)
177 struct obd_run_ctxt saved;
182 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
183 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
184 if ( !file || IS_ERR(file)) {
185 CERROR("OBD filter: cannot create status file\n");
188 rc = file->f_op->write(file, (char *)&obddev->u.filter.fo_lastino,
189 sizeof(obddev->u.filter.fo_lastino), &off);
190 if (rc != sizeof(obddev->u.filter.fo_lastino) ) {
191 CERROR("OBD filter: error writing lastino\n");
194 rc = filp_close(file, NULL);
196 CERROR("OBD filter: cannot close status file\n");
203 static __u64 filter_next_id(struct obd_device *obddev)
206 spin_lock(&obddev->u.filter.fo_lock);
207 obddev->u.filter.fo_lastino++;
208 id = obddev->u.filter.fo_lastino;
209 spin_unlock(&obddev->u.filter.fo_lock);
213 /* how to get files, dentries, inodes from object id's */
214 static struct file *filter_obj_open(struct obd_device *obddev,
215 __u64 id, __u32 type)
217 struct obd_run_ctxt saved;
219 struct super_block *sb;
222 sb = obddev->u.filter.fo_sb;
223 if (!sb || !sb->s_dev) {
224 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
230 CDEBUG(D_INODE, "fatal: invalid obdo %Lu\n", id);
235 if ( ! (type & S_IFMT) ) {
236 CERROR("OBD filter_obj_open, no type (%Ld), mode %o!\n",
240 filter_id(name, id, type);
241 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
242 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0);
245 CDEBUG(D_INODE, "opening obdo %s\n", name);
250 static struct file *filter_parent(obd_id id, obd_mode mode)
255 sprintf(path, "O/%s", obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT]);
257 file = filp_open(path, O_RDONLY, 0);
262 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
263 __u64 id, __u32 type)
268 file = filter_obj_open(obddev, id, type);
270 CERROR("filter_inode_from_obdo failed\n");
274 inode = iget(file->f_dentry->d_inode->i_sb,
275 file->f_dentry->d_inode->i_ino);
281 static int filter_connect(struct obd_conn *conn)
286 rc = gen_connect(conn);
294 static int filter_disconnect(struct obd_conn *conn)
298 rc = gen_disconnect(conn);
302 /* XXX cleanup preallocated inodes */
306 /* mount the file system (secretly) */
307 static int filter_setup(struct obd_device *obddev, obd_count len,
311 struct obd_ioctl_data* data = buf;
312 struct vfsmount *mnt;
317 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
318 data->ioc_inlbuf1, NULL);
325 obddev->u.filter.fo_sb = mnt->mnt_root->d_inode->i_sb;
326 if (!obddev->u.filter.fo_sb) {
331 obddev->u.filter.fo_vfsmnt = mnt;
332 obddev->u.filter.fo_fstype = strdup(data->ioc_inlbuf2);
334 obddev->u.filter.fo_ctxt.pwdmnt = mnt;
335 obddev->u.filter.fo_ctxt.pwd = mnt->mnt_root;
336 obddev->u.filter.fo_ctxt.fs = KERNEL_DS;
339 spin_lock_init(&obddev->u.filter.fo_lock);
347 static int filter_cleanup(struct obd_device * obddev)
349 struct super_block *sb;
353 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
358 if ( !list_empty(&obddev->obd_gen_clients) ) {
359 CERROR("still has clients!\n");
364 sb = obddev->u.filter.fo_sb;
365 if (!obddev->u.filter.fo_sb){
372 mntput(obddev->u.filter.fo_vfsmnt);
373 obddev->u.filter.fo_sb = 0;
374 kfree(obddev->u.filter.fo_fstype);
384 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
386 int type = oa->o_mode & S_IFMT;
389 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
390 inode->i_ino, (long)oa->o_id, oa->o_valid);
391 obdo_from_inode(oa, inode);
392 oa->o_mode &= ~S_IFMT;
395 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
396 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
397 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
399 *((obd_rdev *)oa->o_inline) = rdev;
400 oa->o_obdflags |= OBD_FL_INLINEDATA;
401 oa->o_valid |= OBD_MD_FLINLINE;
405 else if (ext2obd_has_inline(inode)) {
406 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
407 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
408 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
409 oa->o_obdflags |= OBD_FL_INLINEDATA;
410 oa->o_valid |= OBD_MD_FLINLINE;
413 if (ext2obd_has_obdmd(inode)) {
414 /* XXX this will change when we don't store the obdmd in data */
415 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
416 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
417 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
418 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
419 oa->o_valid |= OBD_MD_FLOBDMD;
425 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
430 if ( !gen_client(conn) ) {
431 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
436 if ( !(inode = filter_inode_from_obj(conn->oc_dev,
437 oa->o_id, oa->o_mode)) ) {
442 oa->o_valid &= ~OBD_MD_FLID;
443 filter_from_inode(oa, inode);
450 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
457 if (!gen_client(conn)) {
458 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
462 inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
468 iattr_from_obdo(&iattr, oa);
469 iattr.ia_mode &= ~S_IFMT;
470 iattr.ia_mode |= S_IFREG;
472 if ( inode->i_op->setattr ) {
473 rc = inode->i_op->setattr(&de, &iattr);
475 rc = inode_setattr(inode, &iattr);
483 static int filter_create (struct obd_conn* conn, struct obdo *oa)
486 struct obd_run_ctxt saved;
489 struct obd_device *obddev = conn->oc_dev;
493 if (!gen_client(conn)) {
494 CERROR("invalid client %u\n", conn->oc_id);
498 oa->o_id = filter_next_id(conn->oc_dev);
499 if ( !(oa->o_mode && S_IFMT) ) {
500 CERROR("filter obd: no type!\n");
504 filter_id(name, oa->o_id, oa->o_mode);
505 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
509 file = filp_open(name, O_RDONLY | O_CREAT, mode);
512 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
517 /* Set flags for fields we have set in ext2_new_inode */
518 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
519 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
520 OBD_MD_FLUID | OBD_MD_FLGID;
524 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
526 struct obd_device * obddev;
527 struct obd_client * cli;
528 struct inode * inode;
532 struct obd_run_ctxt saved;
534 if (!(cli = gen_client(conn))) {
535 CERROR("invalid client %u\n", conn->oc_id);
540 obddev = conn->oc_dev;
541 object = filter_obj_open(obddev, oa->o_id, oa->o_mode);
542 if (!object || IS_ERR(object)) {
547 inode = object->f_dentry->d_inode;
549 inode->i_mode = 010000;
551 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
552 dir = filter_parent(oa->o_id, oa->o_mode);
559 dget(object->f_dentry);
560 rc = vfs_unlink(dir->f_dentry->d_inode, object->f_dentry);
563 filp_close(object, 0);
570 static int filter_truncate(struct obd_conn *conn, struct obdo *oa, obd_size count,
575 error = filter_setattr(conn, oa);
576 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
582 /* buffer must lie in user memory here */
583 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
584 obd_size *count, obd_off offset)
587 unsigned long retval;
590 if (!gen_client(conn)) {
591 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
596 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
597 if (!file || IS_ERR(file)) {
599 return -PTR_ERR(file);
602 /* count doubles as retval */
603 retval = file->f_op->read(file, buf, *count, &offset);
618 /* buffer must lie in user memory here */
619 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
620 obd_size *count, obd_off offset)
624 unsigned long retval;
627 if (!gen_client(conn)) {
628 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
633 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
634 if (!file || IS_ERR(file)) {
636 return -PTR_ERR(file);
639 /* count doubles as retval */
640 retval = file->f_op->write(file, buf, *count, &offset);
654 } /* ext2obd_write */
656 static int filter_pgcache_brw(int rw, struct obd_conn *conn,
665 struct super_block *sb;
667 int onum; /* index to oas */
668 int pnum; /* index to pages (bufs) */
669 unsigned long retval;
675 if (!gen_client(conn)) {
676 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
681 sb = conn->oc_dev->u.filter.fo_sb;
685 pnum = 0; /* pnum indexes buf 0..num_pages */
686 for (onum = 0; onum < num_oa; onum++) {
689 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
691 if (!file || IS_ERR(file)) {
697 /* count doubles as retval */
698 for (pg = 0; pg < oa_bufs[onum]; pg++) {
699 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n",
700 rw, onum, pnum, file->f_dentry->d_inode->i_ino,
701 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
702 offset[pnum], count[pnum]);
707 buffer = kmap(pages[pnum]);
708 retval = file->f_op->write(file, buffer, count[pnum], &off);
710 CDEBUG(D_INODE, "retval %ld\n", retval);
712 loff_t off = offset[pnum];
713 char *buffer = kmap(pages[pnum]);
715 if (off >= file->f_dentry->d_inode->i_size) {
716 memset(buffer, 0, count[pnum]);
717 retval = count[pnum];
719 retval = file->f_op->read(file, buffer, count[pnum], &off);
723 if ( retval != count[pnum] ) {
729 CDEBUG(D_INODE, "retval %ld\n", retval);
733 /* sizes and blocks are set by generic_file_write */
734 /* ctimes/mtimes will follow with a setattr call */
741 error = (retval >= 0) ? 0 : retval;
746 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
748 struct inode *inode = NULL;
749 struct super_block *sb = conn->oc_dev->u.ext2.e2_sb;
751 if (!sb || !sb->s_dev) {
752 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
758 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
763 inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
764 if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
765 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
766 (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
767 "no links" : "NULL");
777 int filter_preprw(int cmd, struct obd_conn *conn,
778 int objcount, struct obd_ioobj *obj,
779 int niocount, struct niobuf *nb,
791 memset(res, 0, sizeof(*res) * niocount);
793 for (i=0; i < objcount; i++) {
796 for (j = 0 ; j < o->ioo_bufcnt ; j++) {
802 inode = ioobj_to_inode(conn, o);
805 /* FIXME: we need to iput all of previous inodes */
809 page = lustre_get_page(inode, b->offset >> PAGE_SHIFT);
812 return PTR_ERR(page);
814 if (cmd == OBD_BRW_WRITE) {
815 rc = lustre_prepare_page(0, PAGE_SIZE, page);
818 CERROR("i %d j %d objcount %d bufcnt %d , rc %d, offset %Ld\n", i, j, objcount, o->ioo_bufcnt, rc, b->offset);
821 r->addr = (__u64)(unsigned long)page_address(page);
822 r->offset = b->offset;
831 int filter_commitrw(int cmd, struct obd_conn *conn,
832 int objcount, struct obd_ioobj *obj,
833 int niocount, struct niobuf *res)
839 struct niobuf *r = NULL;
842 for (i=0; i < objcount; i++) {
846 for (j = 0 ; j < o->ioo_bufcnt ; j++) {
855 if (cmd == OBD_BRW_WRITE)
856 rc = lustre_commit_page(r->page, 0, PAGE_SIZE);
858 lustre_put_page(r->page);
866 inode = ((struct page *)r->page)->mapping->host;
873 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
875 struct super_block *sb;
880 if (!gen_client(conn)) {
881 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
886 sb = conn->oc_dev->u.filter.fo_sb;
888 err = sb->s_op->statfs(sb, statfs);
891 } /* ext2obd_statfs */
894 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
895 void *key, obd_count *vallen, void **val)
897 struct obd_device *obddev;
898 struct obd_client * cli;
901 if (!(cli = gen_client(conn))) {
902 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
906 obddev = conn->oc_dev;
908 if ( keylen == strlen("blocksize") &&
909 memcmp(key, "blocksize", keylen) == 0 ) {
910 *vallen = sizeof(int);
911 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
916 if ( keylen == strlen("blocksize_bits") &&
917 memcmp(key, "blocksize_bits", keylen) == 0 ){
918 *vallen = sizeof(int);
919 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
924 if ( keylen == strlen("root_ino") &&
925 memcmp(key, "root_ino", keylen) == 0 ){
926 *vallen = sizeof(int);
927 *val = (void *)(int) FILTER_ROOTINO;
932 CDEBUG(D_IOCTL, "invalid key\n");
937 struct obd_ops filter_obd_ops = {
939 o_get_info: filter_get_info,
940 o_setup: filter_setup,
941 o_cleanup: filter_cleanup,
942 o_connect: filter_connect,
943 o_disconnect: filter_disconnect,
944 o_statfs: filter_statfs,
945 o_getattr: filter_getattr,
946 o_create: filter_create,
947 o_setattr: filter_setattr,
948 o_destroy: filter_destroy,
950 o_write: filter_write,
951 o_brw: filter_pgcache_brw,
952 o_punch: filter_truncate,
953 o_preprw: filter_preprw,
954 o_commitrw: filter_commitrw
956 o_preallocate: ext2obd_preallocate_inodes,
957 o_migrate: ext2obd_migrate,
958 o_copy: gen_copy_data,
959 o_iterate: ext2obd_iterate
964 static int __init obdfilter_init(void)
966 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
967 return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
970 static void __exit obdfilter_exit(void)
972 obd_unregister_type(OBD_FILTER_DEVICENAME);
975 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
976 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
977 MODULE_LICENSE("GPL");
979 module_init(obdfilter_init);
980 module_exit(obdfilter_exit);