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 #define FILTER_ROOTINO 2
33 static char * obd_type_by_mode[S_IFMT >> S_SHIFT] = {
35 [S_IFREG >> S_SHIFT] "R",
36 [S_IFDIR >> S_SHIFT] "D",
37 [S_IFCHR >> S_SHIFT] "C",
38 [S_IFBLK >> S_SHIFT] "B",
39 [S_IFIFO >> S_SHIFT] "F",
40 [S_IFSOCK >> S_SHIFT] "S",
41 [S_IFLNK >> S_SHIFT] "L"
44 static void filter_id(char *buf, obd_id id, obd_mode mode)
46 sprintf(buf, "O/%s/%Ld",
47 obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT],
51 void push_ctxt(struct run_ctxt *save, struct run_ctxt *new)
54 save->pwd = dget(current->fs->pwd);
55 save->pwdmnt = mntget(current->fs->pwdmnt);
58 set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
61 void pop_ctxt(struct run_ctxt *saved)
64 set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
67 mntput(saved->pwdmnt);
70 static void filter_prep(struct obd_device *obddev)
72 struct run_ctxt saved;
81 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
82 rc = sys_mkdir("O", 0700);
83 rc = sys_mkdir("P", 0700);
84 rc = sys_mkdir("D", 0700);
85 rc = sys_mkdir("O/R", 0700); /* regular */
86 rc = sys_mkdir("O/D", 0700); /* directory */
87 rc = sys_mkdir("O/L", 0700); /* symbolic links */
88 rc = sys_mkdir("O/C", 0700); /* character devices */
89 rc = sys_mkdir("O/B", 0700); /* block devices */
90 rc = sys_mkdir("O/F", 0700); /* fifo's */
91 rc = sys_mkdir("O/S", 0700); /* sockets */
93 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
94 file = filp_open(rootid, O_RDWR | O_CREAT, 00755);
96 printk("OBD filter: cannot make root directory");
100 rc = sys_mkdir(rootid, 0755);
101 if ( (fd = sys_open("D/status", O_RDWR | O_CREAT, 0700)) == -1 ) {
102 printk("OBD filter: cannot create status file\n");
105 if ( (rc = sys_fstat64(fd, &buf, 0)) ) {
106 printk("OBD filter: cannot stat status file\n");
109 if (buf.st_size == 0) {
110 rc = sys_write(fd, (char *)&lastino, sizeof(lastino));
111 if (rc != sizeof(lastino)) {
112 printk("OBD filter: error writing lastino\n");
116 rc = sys_read(fd, (char *)&lastino, sizeof(lastino));
117 if (rc != sizeof(lastino)) {
118 printk("OBD filter: error reading lastino\n");
122 obddev->u.filter.fo_lastino = lastino;
124 /* this is also the moment to steal operations */
125 file = filp_open("D/status", O_RDONLY | O_LARGEFILE, 0);
126 if (!file || IS_ERR(file)) {
130 inode = file->f_dentry->d_inode;
131 obddev->u.filter.fo_fop = file->f_op;
132 obddev->u.filter.fo_iop = inode->i_op;
133 obddev->u.filter.fo_aops = inode->i_mapping->a_ops;
139 printk("OBD filter: cannot close status file\n");
145 static void filter_post(struct obd_device *obddev)
147 struct run_ctxt saved;
151 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
152 if ( (fd = sys_open("D/status", O_RDWR | O_CREAT, 0700)) == -1 ) {
153 printk("OBD filter: cannot create status file\n");
156 rc = sys_write(fd, (char *)&obddev->u.filter.fo_lastino,
157 sizeof(obddev->u.filter.fo_lastino));
158 if (rc != sizeof(sizeof(obddev->u.filter.fo_lastino)) ) {
159 printk("OBD filter: error writing lastino\n");
164 printk("OBD filter: cannot close status file\n");
171 /* release per client resources */
172 static int filter_disconnect(struct obd_conn *conn)
174 /* XXX cleanup preallocated inodes */
175 return gen_disconnect(conn);
176 } /* ext2obd_disconnect */
178 /* mount the file system (secretly) */
179 static int filter_setup(struct obd_device *obddev, obd_count len,
183 struct obd_ioctl_data* data = buf;
184 struct vfsmount *mnt;
189 mnt = do_kern_mount(data->ioc_inlbuf2, 0,
190 data->ioc_inlbuf1, NULL);
197 obddev->u.filter.fo_sb = mnt->mnt_root->d_inode->i_sb;
198 if (!obddev->u.filter.fo_sb) {
203 obddev->u.filter.fo_vfsmnt = mnt;
204 obddev->u.filter.fo_fstype = strdup(data->ioc_inlbuf2);
206 obddev->u.filter.fo_ctxt.pwdmnt = mnt;
207 obddev->u.filter.fo_ctxt.pwd = mnt->mnt_root;
208 obddev->u.filter.fo_ctxt.fs = KERNEL_DS;
211 spin_lock_init(&obddev->u.filter.fo_lock);
218 static __u64 filter_next_id(struct obd_device *obddev)
221 spin_lock(&obddev->u.filter.fo_lock);
222 obddev->u.filter.fo_lastino++;
223 id = obddev->u.filter.fo_lastino;
224 spin_unlock(&obddev->u.filter.fo_lock);
228 static int filter_cleanup(struct obd_device * obddev)
230 struct super_block *sb;
234 if ( !(obddev->obd_flags & OBD_SET_UP) ) {
239 if ( !list_empty(&obddev->obd_gen_clients) ) {
240 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
245 sb = obddev->u.filter.fo_sb;
246 if (!obddev->u.filter.fo_sb){
253 mntput(obddev->u.filter.fo_vfsmnt);
254 obddev->u.filter.fo_sb = 0;
255 kfree(obddev->u.filter.fo_fstype);
265 static struct file *filter_obj_open(struct obd_device *obddev,
271 struct run_ctxt saved;
272 struct super_block *sb;
274 sb = obddev->u.filter.fo_sb;
275 if (!sb || !sb->s_dev) {
276 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
282 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)oa->o_id);
287 if ( ! (oa->o_mode & S_IFMT) ) {
288 printk("OBD filter_obj_open, no type (%Ld), mode %o!\n",
289 oa->o_id, oa->o_mode);
291 filter_id(id, oa->o_id, oa->o_mode);
292 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
293 file = filp_open(id , O_RDONLY | O_LARGEFILE, 0);
297 error = PTR_ERR(file);
300 CDEBUG(D_INODE, "opening obdo %s\n", id);
305 static struct inode *filter_inode_from_obdo(struct obd_device *obddev,
311 file = filter_obj_open(obddev, oa);
313 printk("filter_inode_from_obdo failed\n");
317 inode = iget(file->f_dentry->d_inode->i_sb, file->f_dentry->d_inode->i_ino);
322 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
324 int type = oa->o_mode & S_IFMT;
327 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
328 inode->i_ino, (long)oa->o_id, oa->o_valid);
329 obdo_from_inode(oa, inode);
330 oa->o_mode &= ~S_IFMT;
333 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
334 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
335 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
337 *((obd_rdev *)oa->o_inline) = rdev;
338 oa->o_obdflags |= OBD_FL_INLINEDATA;
339 oa->o_valid |= OBD_MD_FLINLINE;
343 else if (ext2obd_has_inline(inode)) {
344 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
345 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
346 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
347 oa->o_obdflags |= OBD_FL_INLINEDATA;
348 oa->o_valid |= OBD_MD_FLINLINE;
351 if (ext2obd_has_obdmd(inode)) {
352 /* XXX this will change when we don't store the obdmd in data */
353 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
354 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
355 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
356 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
357 oa->o_valid |= OBD_MD_FLOBDMD;
363 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
368 if ( !gen_client(conn) ) {
369 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
374 if ( !(inode = filter_inode_from_obdo(conn->oc_dev, oa)) ) {
379 oa->o_valid &= ~OBD_MD_FLID;
380 filter_from_inode(oa, inode);
387 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
394 if (!gen_client(conn)) {
395 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
399 inode = filter_inode_from_obdo(conn->oc_dev, oa);
405 iattr_from_obdo(&iattr, oa);
406 iattr.ia_mode &= ~S_IFMT;
407 iattr.ia_mode |= S_IFREG;
409 if ( inode->i_op->setattr ) {
410 rc = inode->i_op->setattr(&de, &iattr);
412 rc = inode_setattr(inode, &iattr);
420 static int filter_create (struct obd_conn* conn, struct obdo *oa)
423 struct run_ctxt saved;
426 struct obd_device *obddev = conn->oc_dev;
430 if (!gen_client(conn)) {
431 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
434 CDEBUG(D_IOCTL, "\n");
436 oa->o_id = filter_next_id(conn->oc_dev);
437 if ( !(oa->o_mode && S_IFMT) ) {
438 printk("filter obd: no type!\n");
442 filter_id(name, oa->o_id, oa->o_mode);
443 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
447 file = filp_open(name, O_RDONLY | O_CREAT, mode);
450 printk("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
454 CDEBUG(D_IOCTL, "\n");
456 /* Set flags for fields we have set in ext2_new_inode */
457 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
458 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
459 OBD_MD_FLUID | OBD_MD_FLGID;
463 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
465 struct obd_device * obddev;
466 struct obd_client * cli;
467 struct inode * inode;
468 struct run_ctxt saved;
471 if (!(cli = gen_client(conn))) {
472 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
477 obddev = conn->oc_dev;
478 inode = filter_inode_from_obdo(obddev, oa);
486 inode->i_mode = 010000;
489 filter_id(id, oa->o_id, oa->o_mode);
490 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
491 if (sys_unlink(id)) {
502 static int filter_truncate(struct obd_conn *conn, struct obdo *oa, obd_size count,
507 error = filter_setattr(conn, oa);
508 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
514 /* buffer must lie in user memory here */
515 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
516 obd_size *count, obd_off offset)
519 unsigned long retval;
522 if (!gen_client(conn)) {
523 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
528 file = filter_obj_open(conn->oc_dev, oa);
529 if (!file || IS_ERR(file)) {
531 return -PTR_ERR(file);
534 /* count doubles as retval */
535 retval = file->f_op->read(file, buf, *count, &offset);
550 /* buffer must lie in user memory here */
551 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
552 obd_size *count, obd_off offset)
556 unsigned long retval;
559 if (!gen_client(conn)) {
560 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
565 file = filter_obj_open(conn->oc_dev, oa);
566 if (!file || IS_ERR(file)) {
568 return -PTR_ERR(file);
571 /* count doubles as retval */
572 retval = file->f_op->write(file, buf, *count, &offset);
586 } /* ext2obd_write */
588 static int filter_pgcache_brw(int rw, struct obd_conn *conn,
597 struct super_block *sb;
599 int onum; /* index to oas */
600 int pnum; /* index to pages (bufs) */
601 unsigned long retval;
607 if (!gen_client(conn)) {
608 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
613 sb = conn->oc_dev->u.filter.fo_sb;
617 pnum = 0; /* pnum indexes buf 0..num_pages */
618 for (onum = 0; onum < num_oa; onum++) {
621 file = filter_obj_open(conn->oc_dev, oa[onum]);
622 if (!file || IS_ERR(file)) {
628 /* count doubles as retval */
629 for (pg = 0; pg < oa_bufs[onum]; pg++) {
630 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n",
631 rw, onum, pnum, file->f_dentry->d_inode->i_ino,
632 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
633 offset[pnum], count[pnum]);
638 buffer = kmap(pages[pnum]);
639 retval = file->f_op->write(file, buffer, count[pnum], &off);
641 CDEBUG(D_INODE, "retval %ld\n", retval);
643 loff_t off = offset[pnum];
644 char *buffer = kmap(pages[pnum]);
646 if (off >= file->f_dentry->d_inode->i_size) {
647 memset(buffer, 0, count[pnum]);
648 retval = count[pnum];
650 retval = file->f_op->read(file, buffer, count[pnum], &off);
654 if ( retval != count[pnum] ) {
660 CDEBUG(D_INODE, "retval %ld\n", retval);
664 /* sizes and blocks are set by generic_file_write */
665 /* ctimes/mtimes will follow with a setattr call */
672 error = (retval >= 0) ? 0 : retval;
676 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
678 struct super_block *sb;
683 if (!gen_client(conn)) {
684 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
689 sb = conn->oc_dev->u.filter.fo_sb;
691 err = sb->s_op->statfs(sb, statfs);
694 } /* ext2obd_statfs */
697 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
698 void *key, obd_count *vallen, void **val)
700 struct obd_device *obddev;
701 struct obd_client * cli;
704 if (!(cli = gen_client(conn))) {
705 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
709 obddev = conn->oc_dev;
711 if ( keylen == strlen("blocksize") &&
712 memcmp(key, "blocksize", keylen) == 0 ) {
713 *vallen = sizeof(int);
714 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
719 if ( keylen == strlen("blocksize_bits") &&
720 memcmp(key, "blocksize_bits", keylen) == 0 ){
721 *vallen = sizeof(int);
722 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
727 if ( keylen == strlen("root_ino") &&
728 memcmp(key, "root_ino", keylen) == 0 ){
729 *vallen = sizeof(int);
730 *val = (void *)(int) FILTER_ROOTINO;
735 CDEBUG(D_IOCTL, "invalid key\n");
740 struct obd_ops filter_obd_ops = {
742 o_get_info: filter_get_info,
743 o_setup: filter_setup,
744 o_cleanup: filter_cleanup,
745 o_connect: gen_connect,
746 o_disconnect: filter_disconnect,
747 o_statfs: filter_statfs,
748 o_getattr: filter_getattr,
749 o_create: filter_create,
750 o_setattr: filter_setattr,
751 o_destroy: filter_destroy,
753 o_write: filter_write,
754 o_brw: filter_pgcache_brw,
755 o_punch: filter_truncate,
757 o_preallocate: ext2obd_preallocate_inodes,
758 o_migrate: ext2obd_migrate,
759 o_copy: gen_copy_data,
760 o_iterate: ext2obd_iterate
767 void init_module(void)
769 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
770 obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
773 void cleanup_module(void)
775 obd_unregister_type(OBD_FILTER_DEVICENAME);
776 CDEBUG(D_MALLOC, "FILTER mem used %ld\n", filter_memory);