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;
173 /* cleanup the filter: write last used object id to status file */
174 static void filter_post(struct obd_device *obddev)
176 struct obd_run_ctxt saved;
181 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
182 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
183 if ( !file || IS_ERR(file)) {
184 CERROR("OBD filter: cannot create status file\n");
187 rc = file->f_op->write(file, (char *)&obddev->u.filter.fo_lastino,
188 sizeof(obddev->u.filter.fo_lastino), &off);
189 if (rc != sizeof(obddev->u.filter.fo_lastino) ) {
190 CERROR("OBD filter: error writing lastino\n");
193 rc = filp_close(file, NULL);
195 CERROR("OBD filter: cannot close status file\n");
202 static __u64 filter_next_id(struct obd_device *obddev)
205 spin_lock(&obddev->u.filter.fo_lock);
206 obddev->u.filter.fo_lastino++;
207 id = obddev->u.filter.fo_lastino;
208 spin_unlock(&obddev->u.filter.fo_lock);
212 /* how to get files, dentries, inodes from object id's */
213 static struct file *filter_obj_open(struct obd_device *obddev,
214 __u64 id, __u32 type)
216 struct obd_run_ctxt saved;
218 struct super_block *sb;
221 sb = obddev->u.filter.fo_sb;
222 if (!sb || !sb->s_dev) {
223 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
229 CDEBUG(D_INODE, "fatal: invalid obdo %Lu\n", id);
234 if ( ! (type & S_IFMT) ) {
235 CERROR("OBD filter_obj_open, no type (%Ld), mode %o!\n",
239 filter_id(name, id, type);
240 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
241 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0);
244 CDEBUG(D_INODE, "opening obdo %s\n", name);
249 static struct file *filter_parent(obd_id id, obd_mode mode)
254 sprintf(path, "O/%s", obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT]);
256 file = filp_open(path, O_RDONLY, 0);
261 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
262 __u64 id, __u32 type)
267 file = filter_obj_open(obddev, id, type);
269 CERROR("filter_inode_from_obdo failed\n");
273 inode = iget(file->f_dentry->d_inode->i_sb,
274 file->f_dentry->d_inode->i_ino);
280 static int filter_connect(struct obd_conn *conn)
285 rc = gen_connect(conn);
293 static int filter_disconnect(struct obd_conn *conn)
297 rc = gen_disconnect(conn);
301 /* XXX cleanup preallocated inodes */
305 /* mount the file system (secretly) */
306 static int filter_setup(struct obd_device *obddev, obd_count len,
310 struct obd_ioctl_data* data = buf;
311 struct vfsmount *mnt;
316 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
317 data->ioc_inlbuf1, NULL);
324 obddev->u.filter.fo_sb = mnt->mnt_root->d_inode->i_sb;
325 if (!obddev->u.filter.fo_sb) {
330 obddev->u.filter.fo_vfsmnt = mnt;
331 obddev->u.filter.fo_fstype = strdup(data->ioc_inlbuf2);
333 obddev->u.filter.fo_ctxt.pwdmnt = mnt;
334 obddev->u.filter.fo_ctxt.pwd = mnt->mnt_root;
335 obddev->u.filter.fo_ctxt.fs = KERNEL_DS;
338 spin_lock_init(&obddev->u.filter.fo_lock);
346 static int filter_cleanup(struct obd_device * obddev)
348 struct super_block *sb;
352 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
357 if ( !list_empty(&obddev->obd_gen_clients) ) {
358 CERROR("still has clients!\n");
363 sb = obddev->u.filter.fo_sb;
364 if (!obddev->u.filter.fo_sb){
371 mntput(obddev->u.filter.fo_vfsmnt);
372 obddev->u.filter.fo_sb = 0;
373 kfree(obddev->u.filter.fo_fstype);
383 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
385 int type = oa->o_mode & S_IFMT;
388 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
389 inode->i_ino, (long)oa->o_id, oa->o_valid);
390 obdo_from_inode(oa, inode);
391 oa->o_mode &= ~S_IFMT;
394 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
395 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
396 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
398 *((obd_rdev *)oa->o_inline) = rdev;
399 oa->o_obdflags |= OBD_FL_INLINEDATA;
400 oa->o_valid |= OBD_MD_FLINLINE;
404 else if (ext2obd_has_inline(inode)) {
405 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
406 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
407 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
408 oa->o_obdflags |= OBD_FL_INLINEDATA;
409 oa->o_valid |= OBD_MD_FLINLINE;
412 if (ext2obd_has_obdmd(inode)) {
413 /* XXX this will change when we don't store the obdmd in data */
414 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
415 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
416 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
417 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
418 oa->o_valid |= OBD_MD_FLOBDMD;
424 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
429 if ( !gen_client(conn) ) {
430 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
435 if ( !(inode = filter_inode_from_obj(conn->oc_dev,
436 oa->o_id, oa->o_mode)) ) {
441 oa->o_valid &= ~OBD_MD_FLID;
442 filter_from_inode(oa, inode);
449 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
456 if (!gen_client(conn)) {
457 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
461 inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
467 iattr_from_obdo(&iattr, oa);
468 iattr.ia_mode &= ~S_IFMT;
469 iattr.ia_mode |= S_IFREG;
471 if ( inode->i_op->setattr ) {
472 rc = inode->i_op->setattr(&de, &iattr);
474 rc = inode_setattr(inode, &iattr);
482 static int filter_create (struct obd_conn* conn, struct obdo *oa)
485 struct obd_run_ctxt saved;
488 struct obd_device *obddev = conn->oc_dev;
492 if (!gen_client(conn)) {
493 CERROR("invalid client %u\n", conn->oc_id);
497 oa->o_id = filter_next_id(conn->oc_dev);
498 if ( !(oa->o_mode && S_IFMT) ) {
499 CERROR("filter obd: no type!\n");
503 filter_id(name, oa->o_id, oa->o_mode);
504 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
508 file = filp_open(name, O_RDONLY | O_CREAT, mode);
511 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
516 /* Set flags for fields we have set in ext2_new_inode */
517 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
518 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
519 OBD_MD_FLUID | OBD_MD_FLGID;
523 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
525 struct obd_device * obddev;
526 struct obd_client * cli;
527 struct inode * inode;
531 struct obd_run_ctxt saved;
533 if (!(cli = gen_client(conn))) {
534 CERROR("invalid client %u\n", conn->oc_id);
539 obddev = conn->oc_dev;
540 object = filter_obj_open(obddev, oa->o_id, oa->o_mode);
541 if (!object || IS_ERR(object)) {
546 inode = object->f_dentry->d_inode;
548 inode->i_mode = 010000;
550 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
551 dir = filter_parent(oa->o_id, oa->o_mode);
558 dget(object->f_dentry);
559 rc = vfs_unlink(dir->f_dentry->d_inode, object->f_dentry);
562 filp_close(object, 0);
569 static int filter_truncate(struct obd_conn *conn, struct obdo *oa, obd_size count,
574 error = filter_setattr(conn, oa);
575 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
581 /* buffer must lie in user memory here */
582 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
583 obd_size *count, obd_off offset)
586 unsigned long retval;
589 if (!gen_client(conn)) {
590 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
595 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
596 if (!file || IS_ERR(file)) {
598 return -PTR_ERR(file);
601 /* count doubles as retval */
602 retval = file->f_op->read(file, buf, *count, &offset);
617 /* buffer must lie in user memory here */
618 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
619 obd_size *count, obd_off offset)
623 unsigned long retval;
626 if (!gen_client(conn)) {
627 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
632 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
633 if (!file || IS_ERR(file)) {
635 return -PTR_ERR(file);
638 /* count doubles as retval */
639 retval = file->f_op->write(file, buf, *count, &offset);
653 } /* ext2obd_write */
655 static int filter_pgcache_brw(int rw, struct obd_conn *conn,
664 struct super_block *sb;
666 int onum; /* index to oas */
667 int pnum; /* index to pages (bufs) */
668 unsigned long retval;
674 if (!gen_client(conn)) {
675 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
680 sb = conn->oc_dev->u.filter.fo_sb;
684 pnum = 0; /* pnum indexes buf 0..num_pages */
685 for (onum = 0; onum < num_oa; onum++) {
688 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
690 if (!file || IS_ERR(file)) {
696 /* count doubles as retval */
697 for (pg = 0; pg < oa_bufs[onum]; pg++) {
698 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n",
699 rw, onum, pnum, file->f_dentry->d_inode->i_ino,
700 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
701 offset[pnum], count[pnum]);
706 buffer = kmap(pages[pnum]);
707 retval = file->f_op->write(file, buffer, count[pnum], &off);
709 CDEBUG(D_INODE, "retval %ld\n", retval);
711 loff_t off = offset[pnum];
712 char *buffer = kmap(pages[pnum]);
714 if (off >= file->f_dentry->d_inode->i_size) {
715 memset(buffer, 0, count[pnum]);
716 retval = count[pnum];
718 retval = file->f_op->read(file, buffer, count[pnum], &off);
722 if ( retval != count[pnum] ) {
728 CDEBUG(D_INODE, "retval %ld\n", retval);
732 /* sizes and blocks are set by generic_file_write */
733 /* ctimes/mtimes will follow with a setattr call */
740 error = (retval >= 0) ? 0 : retval;
745 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
747 struct inode *inode = NULL;
748 struct super_block *sb = conn->oc_dev->u.ext2.e2_sb;
750 if (!sb || !sb->s_dev) {
751 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
757 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
762 inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
763 if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
764 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
765 (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
766 "no links" : "NULL");
776 int filter_preprw(int cmd, struct obd_conn *conn,
777 int objcount, struct obd_ioobj *obj,
778 int niocount, struct niobuf *nb,
790 memset(res, 0, sizeof(*res) * niocount);
792 for (i=0; i < objcount; i++) {
795 for (j = 0 ; j < o->ioo_bufcnt ; j++) {
801 inode = ioobj_to_inode(conn, o);
804 /* FIXME: we need to iput all of previous inodes */
808 page = lustre_get_page(inode, b->offset >> PAGE_SHIFT);
811 return PTR_ERR(page);
813 if (cmd == OBD_BRW_WRITE) {
814 rc = lustre_prepare_page(0, PAGE_SIZE, page);
817 CERROR("i %d j %d objcount %d bufcnt %d , rc %d, offset %Ld\n", i, j, objcount, o->ioo_bufcnt, rc, b->offset);
820 r->addr = (__u64)(unsigned long)page_address(page);
821 r->offset = b->offset;
830 int filter_commitrw(int cmd, struct obd_conn *conn,
831 int objcount, struct obd_ioobj *obj,
832 int niocount, struct niobuf *res)
838 struct niobuf *r = NULL;
841 for (i=0; i < objcount; i++) {
845 for (j = 0 ; j < o->ioo_bufcnt ; j++) {
854 if (cmd == OBD_BRW_WRITE)
855 rc = lustre_commit_page(r->page, 0, PAGE_SIZE);
857 lustre_put_page(r->page);
865 inode = ((struct page *)r->page)->mapping->host;
872 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
874 struct super_block *sb;
879 if (!gen_client(conn)) {
880 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
885 sb = conn->oc_dev->u.filter.fo_sb;
887 err = sb->s_op->statfs(sb, statfs);
890 } /* ext2obd_statfs */
893 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
894 void *key, obd_count *vallen, void **val)
896 struct obd_device *obddev;
897 struct obd_client * cli;
900 if (!(cli = gen_client(conn))) {
901 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
905 obddev = conn->oc_dev;
907 if ( keylen == strlen("blocksize") &&
908 memcmp(key, "blocksize", keylen) == 0 ) {
909 *vallen = sizeof(int);
910 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
915 if ( keylen == strlen("blocksize_bits") &&
916 memcmp(key, "blocksize_bits", keylen) == 0 ){
917 *vallen = sizeof(int);
918 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
923 if ( keylen == strlen("root_ino") &&
924 memcmp(key, "root_ino", keylen) == 0 ){
925 *vallen = sizeof(int);
926 *val = (void *)(int) FILTER_ROOTINO;
931 CDEBUG(D_IOCTL, "invalid key\n");
936 struct obd_ops filter_obd_ops = {
938 o_get_info: filter_get_info,
939 o_setup: filter_setup,
940 o_cleanup: filter_cleanup,
941 o_connect: filter_connect,
942 o_disconnect: filter_disconnect,
943 o_statfs: filter_statfs,
944 o_getattr: filter_getattr,
945 o_create: filter_create,
946 o_setattr: filter_setattr,
947 o_destroy: filter_destroy,
949 o_write: filter_write,
950 o_brw: filter_pgcache_brw,
951 o_punch: filter_truncate,
952 o_preprw: filter_preprw,
953 o_commitrw: filter_commitrw
955 o_preallocate: ext2obd_preallocate_inodes,
956 o_migrate: ext2obd_migrate,
957 o_copy: gen_copy_data,
958 o_iterate: ext2obd_iterate
963 static int __init obdfilter_init(void)
965 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
966 return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
969 static void __exit obdfilter_exit(void)
971 obd_unregister_type(OBD_FILTER_DEVICENAME);
974 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
975 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
976 MODULE_LICENSE("GPL");
978 module_init(obdfilter_init);
979 module_exit(obdfilter_exit);