1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/filter/filter.c
6 * Copyright (C) 2001 Cluster File Systems, Inc.
8 * This code is issued under the GNU General Public License.
9 * See the file COPYING in this distribution
11 * by Peter Braam <braam@clusterfs.com>
15 #define DEBUG_SUBSYSTEM S_FILTER
17 #include <linux/module.h>
18 #include <linux/obd_filter.h>
20 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
23 #define FILTER_ROOTINO 2
26 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
28 [S_IFREG >> S_SHIFT] "R",
29 [S_IFDIR >> S_SHIFT] "D",
30 [S_IFCHR >> S_SHIFT] "C",
31 [S_IFBLK >> S_SHIFT] "B",
32 [S_IFIFO >> S_SHIFT] "F",
33 [S_IFSOCK >> S_SHIFT] "S",
34 [S_IFLNK >> S_SHIFT] "L"
37 static inline const char *obd_mode_to_type(int mode)
39 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
42 /* write the pathname into the string */
43 static int filter_id(char *buf, obd_id id, obd_mode mode)
45 return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
46 (unsigned long long)id);
49 /* setup the object store with correct subdirectories */
50 static int filter_prep(struct obd_device *obddev)
52 struct obd_run_ctxt saved;
53 struct filter_obd *filter = &obddev->u.filter;
54 struct dentry *dentry;
63 push_ctxt(&saved, &filter->fo_ctxt);
64 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
65 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
68 CERROR("cannot open/create O: rc = %d\n", rc);
71 filter->fo_dentry_O = dentry;
72 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
73 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
76 CERROR("cannot open/create P: rc = %d\n", rc);
79 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,dentry->d_count.counter-1);
81 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
82 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
85 CERROR("cannot open/create D: rc = %d\n", rc);
88 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,dentry->d_count.counter-1);
92 * Create directories and/or get dentries for each object type.
93 * This saves us from having to do multiple lookups for each one.
95 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
96 char *type = obd_type_by_mode[mode];
99 filter->fo_dentry_O_mode[mode] = NULL;
102 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
103 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
104 if (IS_ERR(dentry)) {
105 rc = PTR_ERR(dentry);
106 CERROR("cannot create O/%s: rc = %d\n", type, rc);
107 GOTO(out_O_mode, rc);
109 filter->fo_dentry_O_mode[mode] = dentry;
112 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
113 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
116 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
118 GOTO(out_O_mode, rc);
122 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
123 if ( !file || IS_ERR(file) ) {
125 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
127 GOTO(out_O_mode, rc);
130 /* steal operations */
131 inode = file->f_dentry->d_inode;
132 filter->fo_fop = file->f_op;
133 filter->fo_iop = inode->i_op;
134 filter->fo_aops = inode->i_mapping->a_ops;
137 if (inode->i_size == 0) {
138 ssize_t retval = file->f_op->write(file, (char *)&lastino,
139 sizeof(lastino), &off);
140 if (retval != sizeof(lastino)) {
141 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
143 GOTO(out_O_mode, rc = -EIO);
146 ssize_t retval = file->f_op->read(file, (char *)&lastino,
147 sizeof(lastino), &off);
148 if (retval != sizeof(lastino)) {
149 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
151 GOTO(out_O_mode, rc = -EIO);
154 filter->fo_lastino = lastino;
164 while (--mode >= 0) {
165 if (filter->fo_dentry_O_mode[mode]) {
166 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
167 obd_type_by_mode[mode],
168 filter->fo_dentry_O_mode[mode],
169 filter->fo_dentry_O_mode[mode]->d_count.counter-1);
170 l_dput(filter->fo_dentry_O_mode[mode]);
171 filter->fo_dentry_O_mode[mode] = NULL;
175 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
176 filter->fo_dentry_O->d_count.counter);
177 l_dput(filter->fo_dentry_O);
178 filter->fo_dentry_O = NULL;
182 /* cleanup the filter: write last used object id to status file */
183 static void filter_post(struct obd_device *obddev)
185 struct obd_run_ctxt saved;
186 struct filter_obd *filter = &obddev->u.filter;
192 push_ctxt(&saved, &filter->fo_ctxt);
193 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
195 CERROR("OBD filter: cannot create status file\n");
198 rc = file->f_op->write(file, (char *)&filter->fo_lastino,
199 sizeof(filter->fo_lastino), &off);
200 if (rc != sizeof(filter->fo_lastino))
201 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
203 rc = filp_close(file, NULL);
205 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
207 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
208 if (filter->fo_dentry_O_mode[mode]) {
209 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
210 obd_type_by_mode[mode],
211 filter->fo_dentry_O_mode[mode],
212 filter->fo_dentry_O_mode[mode]->d_count.counter-1);
213 l_dput(filter->fo_dentry_O_mode[mode]);
214 filter->fo_dentry_O_mode[mode] = NULL;
217 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
218 filter->fo_dentry_O->d_count.counter);
219 l_dput(filter->fo_dentry_O);
225 static __u64 filter_next_id(struct obd_device *obddev)
228 spin_lock(&obddev->u.filter.fo_lock);
229 obddev->u.filter.fo_lastino++;
230 id = obddev->u.filter.fo_lastino;
231 spin_unlock(&obddev->u.filter.fo_lock);
235 /* how to get files, dentries, inodes from object id's */
236 /* parent i_sem is already held if needed for exclusivity */
237 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
238 struct dentry *dparent,
239 __u64 id, __u32 type)
241 struct super_block *sb = obddev->u.filter.fo_sb;
242 struct dentry *dchild;
247 if (!sb || !sb->s_dev) {
248 CERROR("fatal: device not initialized.\n");
249 RETURN(ERR_PTR(-ENXIO));
253 CERROR("fatal: invalid object #0\n");
254 RETURN(ERR_PTR(-ESTALE));
257 if (!(type & S_IFMT)) {
258 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
259 (unsigned long long)id, type);
260 RETURN(ERR_PTR(-EINVAL));
263 len = sprintf(name, "%Ld", id);
264 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
266 dchild = lookup_one_len(name, dparent, len);
267 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
268 obd_mode_to_type(type), name, dchild, dchild->d_count.counter);
270 if (IS_ERR(dchild)) {
271 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
278 static struct file *filter_obj_open(struct obd_device *obddev,
279 __u64 id, __u32 type)
281 struct super_block *sb = obddev->u.filter.fo_sb;
282 struct obd_run_ctxt saved;
287 if (!sb || !sb->s_dev) {
288 CERROR("fatal: device not initialized.\n");
289 RETURN(ERR_PTR(-ENXIO));
293 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
294 RETURN(ERR_PTR(-ESTALE));
297 if (!(type & S_IFMT)) {
298 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
299 (unsigned long long)id, type);
300 RETURN(ERR_PTR(-EINVAL));
303 filter_id(name, id, type);
304 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
305 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
308 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
315 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
317 struct filter_obd *filter = &obddev->u.filter;
319 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
323 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
324 __u64 id, __u32 type)
326 struct dentry *dentry;
329 dentry = filter_fid2dentry(obddev, filter_parent(obddev, type),
331 if (IS_ERR(dentry)) {
332 CERROR("%s: lookup failed: rc = %ld\n", __FUNCTION__,
338 inode = iget(dentry->d_inode->i_sb, dentry->d_inode->i_ino);
340 CDEBUG(D_INODE, "put child %p, count = %d\n", dentry, dentry->d_count.counter-1);
342 CDEBUG(D_INODE, "got inode %p (%ld), count = %d\n", inode, inode->i_ino,
343 inode->i_count.counter);
348 static int filter_connect(struct obd_conn *conn)
353 rc = gen_connect(conn);
361 static int filter_disconnect(struct obd_conn *conn)
365 rc = gen_disconnect(conn);
369 /* XXX cleanup preallocated inodes */
373 /* mount the file system (secretly) */
374 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
376 struct obd_ioctl_data* data = buf;
377 struct filter_obd *filter;
378 struct vfsmount *mnt;
382 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
386 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
391 filter = &obddev->u.filter;;
392 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
393 /* XXX is this even possible if do_kern_mount succeeded? */
395 GOTO(err_put, err = -ENODEV);
397 filter->fo_vfsmnt = mnt;
398 filter->fo_fstype = strdup(data->ioc_inlbuf2);
400 filter->fo_ctxt.pwdmnt = mnt;
401 filter->fo_ctxt.pwd = mnt->mnt_root;
402 filter->fo_ctxt.fs = KERNEL_DS;
404 err = filter_prep(obddev);
406 GOTO(err_kfree, err);
407 spin_lock_init(&filter->fo_lock);
412 kfree(filter->fo_fstype);
415 mntput(filter->fo_vfsmnt);
425 static int filter_cleanup(struct obd_device * obddev)
427 struct super_block *sb;
430 if (!(obddev->obd_flags & OBD_SET_UP))
433 if (!list_empty(&obddev->obd_gen_clients)) {
434 CERROR("still has clients!\n");
438 sb = obddev->u.filter.fo_sb;
439 if (!obddev->u.filter.fo_sb)
444 shrink_dcache_parent(sb->s_root);
446 CDEBUG(D_INODE, "putting sb->s_root: count = %d\n",
447 sb->s_root->d_count.counter-1);
448 mntput(obddev->u.filter.fo_vfsmnt);
449 obddev->u.filter.fo_sb = 0;
450 kfree(obddev->u.filter.fo_fstype);
459 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
461 int type = oa->o_mode & S_IFMT;
464 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
465 inode->i_ino, (long)oa->o_id, oa->o_valid);
466 obdo_from_inode(oa, inode);
467 oa->o_mode &= ~S_IFMT;
470 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
471 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
472 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
474 *((obd_rdev *)oa->o_inline) = rdev;
475 oa->o_obdflags |= OBD_FL_INLINEDATA;
476 oa->o_valid |= OBD_MD_FLINLINE;
480 else if (filter_has_inline(inode)) {
481 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
482 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
483 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
484 oa->o_obdflags |= OBD_FL_INLINEDATA;
485 oa->o_valid |= OBD_MD_FLINLINE;
488 if (filter_has_obdmd(inode)) {
489 /* XXX this will change when we don't store the obdmd in data */
490 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
491 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
492 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
493 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
494 oa->o_valid |= OBD_MD_FLOBDMD;
500 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
505 if (!gen_client(conn)) {
506 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
510 inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
514 oa->o_valid &= ~OBD_MD_FLID;
515 filter_from_inode(oa, inode);
517 CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
518 inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
523 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
525 struct obd_run_ctxt saved;
532 if (!gen_client(conn)) {
533 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
537 inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
541 iattr_from_obdo(&iattr, oa);
542 iattr.ia_mode &= ~S_IFMT;
543 iattr.ia_mode |= S_IFREG;
546 if (iattr.ia_mode & ATTR_SIZE)
548 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
549 if (inode->i_op->setattr)
550 rc = inode->i_op->setattr(&de, &iattr);
552 rc = inode_setattr(inode, &iattr);
554 if (iattr.ia_mode & ATTR_SIZE)
558 CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
559 inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
564 static int filter_open(struct obd_conn *conn, struct obdo *oa)
569 if (!gen_client(conn)) {
570 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
574 inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
581 static int filter_close(struct obd_conn *conn, struct obdo *oa)
586 if (!gen_client(conn)) {
587 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
591 inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
595 CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
596 inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
597 iput(inode); /* for the close */
598 CDEBUG(D_INODE, "objid #%ld has %d links, %d count after close\n",
599 inode->i_ino, inode->i_nlink, inode->i_count.counter - 1);
600 CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
601 inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
602 iput(inode); /* for this call */
606 static int filter_create(struct obd_conn* conn, struct obdo *oa)
609 struct obd_run_ctxt saved;
612 struct obd_device *obddev = conn->oc_dev;
616 if (!gen_client(conn)) {
617 CERROR("invalid client %u\n", conn->oc_id);
621 oa->o_id = filter_next_id(conn->oc_dev);
622 if (!(oa->o_mode && S_IFMT)) {
623 CERROR("filter obd: no type!\n");
627 filter_id(name, oa->o_id, oa->o_mode);
628 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
629 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
630 file = filp_open(name, O_RDONLY | O_CREAT, mode);
633 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
638 /* Set flags for fields we have set in the inode struct */
639 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
640 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
641 OBD_MD_FLUID | OBD_MD_FLGID;
643 /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
647 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
649 struct obd_run_ctxt saved;
650 struct obd_device *obddev;
651 struct obd_client *cli;
653 struct dentry *dir_dentry, *object_dentry;
657 if (!(cli = gen_client(conn))) {
658 CERROR("invalid client %u\n", conn->oc_id);
662 CDEBUG(D_INODE, "destroying object %Ld\n",oa->o_id);
663 obddev = conn->oc_dev;
665 dir_dentry = filter_parent(obddev, oa->o_mode);
666 down(&dir_dentry->d_inode->i_sem);
668 object_dentry = filter_fid2dentry(obddev, dir_dentry, oa->o_id,
670 if (IS_ERR(object_dentry))
671 GOTO(out, rc = -ENOENT);
673 inode = object_dentry->d_inode;
674 if (inode->i_nlink != 1) {
675 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
678 inode->i_mode = S_IFREG;
680 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
681 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
683 CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
684 object_dentry->d_count.counter-1);
685 l_dput(object_dentry);
689 up(&dir_dentry->d_inode->i_sem);
693 /* NB count and offset are used for punch, but not truncate */
694 static int filter_truncate(struct obd_conn *conn, struct obdo *oa,
695 obd_size count, obd_off offset)
700 CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
701 "o_size = %Ld\n", oa->o_id, oa->o_valid, oa->o_size);
702 error = filter_setattr(conn, oa);
703 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
708 /* buffer must lie in user memory here */
709 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
710 obd_size *count, obd_off offset)
713 unsigned long retval;
717 if (!gen_client(conn)) {
718 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
722 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
724 RETURN(PTR_ERR(file));
726 /* count doubles as retval */
727 retval = file->f_op->read(file, buf, *count, (loff_t *)&offset);
742 /* buffer must lie in user memory here */
743 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
744 obd_size *count, obd_off offset)
746 struct obd_run_ctxt saved;
749 unsigned long retval;
752 if (!gen_client(conn)) {
753 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
757 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
759 RETURN(PTR_ERR(file));
761 /* count doubles as retval */
762 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
763 retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
780 static int filter_pgcache_brw(int rw, struct obd_conn *conn, obd_count num_oa,
781 struct obdo **oa, obd_count *oa_bufs,
782 struct page **pages, obd_size *count,
783 obd_off *offset, obd_flag *flags)
785 struct obd_run_ctxt saved;
786 struct super_block *sb;
787 int onum; /* index to oas */
788 int pnum; /* index to pages (bufs) */
789 unsigned long retval;
794 if (!gen_client(conn)) {
795 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
799 sb = conn->oc_dev->u.filter.fo_sb;
801 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
802 pnum = 0; /* pnum indexes buf 0..num_pages */
803 for (onum = 0; onum < num_oa; onum++) {
806 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
809 GOTO(out, retval = PTR_ERR(file));
811 /* count doubles as retval */
812 for (pg = 0; pg < oa_bufs[onum]; pg++) {
813 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) "
814 "off count (%Ld,%Ld)\n",
815 rw, onum, pnum, file->f_dentry->d_inode->i_ino,
816 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
817 (unsigned long long)offset[pnum],
818 (unsigned long long)count[pnum]);
823 buffer = kmap(pages[pnum]);
824 retval = file->f_op->write(file, buffer, count[pnum], &off);
826 CDEBUG(D_INODE, "retval %ld\n", retval);
828 loff_t off = offset[pnum];
829 char *buffer = kmap(pages[pnum]);
831 if (off >= file->f_dentry->d_inode->i_size) {
832 memset(buffer, 0, count[pnum]);
833 retval = count[pnum];
835 retval = file->f_op->read(file, buffer, count[pnum], &off);
839 if (retval != count[pnum]) {
841 GOTO(out, retval = -EIO);
843 CDEBUG(D_INODE, "retval %ld\n", retval);
847 /* sizes and blocks are set by generic_file_write */
848 /* ctimes/mtimes will follow with a setattr call */
856 error = (retval >= 0) ? 0 : retval;
861 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
863 struct super_block *sb = conn->oc_dev->u.filter.fo_sb;
864 struct inode *inode = NULL;
867 if (!sb || !sb->s_dev) {
868 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
873 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
877 inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
878 if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
879 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
880 (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
881 "no links" : "NULL");
889 static int filter_preprw(int cmd, struct obd_conn *conn,
890 int objcount, struct obd_ioobj *obj,
891 int niocount, struct niobuf_remote *nb,
892 struct niobuf_local *res)
894 struct obd_run_ctxt saved;
895 struct obd_ioobj *o = obj;
896 struct niobuf_remote *b = nb;
897 struct niobuf_local *r = res;
901 memset(res, 0, sizeof(*res) * niocount);
903 // if (cmd == OBD_BRW_WRITE)
904 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
905 for (i = 0; i < objcount; i++, o++) {
907 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
908 unsigned long index = b->offset >> PAGE_SHIFT;
909 struct inode *inode = ioobj_to_inode(conn, o);
912 /* FIXME: we need to iput all inodes on error */
916 if (cmd == OBD_BRW_WRITE)
917 page = lustre_get_page_write(inode, index);
919 page = lustre_get_page_read(inode, index);
921 RETURN(PTR_ERR(page));
923 r->addr = (__u64)(unsigned long)page_address(page);
924 r->offset = b->offset;
929 // if (cmd == OBD_BRW_WRITE)
934 static int filter_commitrw(int cmd, struct obd_conn *conn,
935 int objcount, struct obd_ioobj *obj,
936 int niocount, struct niobuf_local *res)
938 struct obd_run_ctxt saved;
939 struct obd_ioobj *o = obj;
940 struct niobuf_local *r = res;
944 // if (cmd == OBD_BRW_WRITE)
945 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
946 for (i = 0; i < objcount; i++, obj++) {
948 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
949 struct page *page = r->page;
954 if (cmd == OBD_BRW_WRITE) {
955 int rc = lustre_commit_page(page, 0, PAGE_SIZE);
957 /* FIXME: still need to iput the other inodes */
961 lustre_put_page(page);
963 CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n",
965 page->mapping->host->i_ino,
966 page->mapping->host->i_count.counter - 1,
967 page->mapping->host->i_nlink);
968 iput(page->mapping->host);
971 // if (cmd == OBD_BRW_WRITE)
976 static int filter_statfs(struct obd_conn *conn, struct statfs * statfs)
978 struct super_block *sb;
982 if (!gen_client(conn)) {
983 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
987 sb = conn->oc_dev->u.filter.fo_sb;
989 err = sb->s_op->statfs(sb, statfs);
991 } /* filter_statfs */
994 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
995 void *key, obd_count *vallen, void **val)
997 struct obd_device *obddev;
998 struct obd_client * cli;
1001 if (!(cli = gen_client(conn))) {
1002 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1006 obddev = conn->oc_dev;
1008 if ( keylen == strlen("blocksize") &&
1009 memcmp(key, "blocksize", keylen) == 0 ) {
1010 *vallen = sizeof(long);
1011 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize;
1015 if ( keylen == strlen("blocksize_bits") &&
1016 memcmp(key, "blocksize_bits", keylen) == 0 ){
1017 *vallen = sizeof(long);
1018 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize_bits;
1022 if ( keylen == strlen("root_ino") &&
1023 memcmp(key, "root_ino", keylen) == 0 ){
1024 *vallen = sizeof(long);
1025 *val = (void *)(long)FILTER_ROOTINO;
1029 CDEBUG(D_IOCTL, "invalid key\n");
1034 struct obd_ops filter_obd_ops = {
1036 o_get_info: filter_get_info,
1037 o_setup: filter_setup,
1038 o_cleanup: filter_cleanup,
1039 o_connect: filter_connect,
1040 o_disconnect: filter_disconnect,
1041 o_statfs: filter_statfs,
1042 o_getattr: filter_getattr,
1043 o_create: filter_create,
1044 o_setattr: filter_setattr,
1045 o_destroy: filter_destroy,
1046 o_open: filter_open,
1047 o_close: filter_close,
1048 o_read: filter_read,
1049 o_write: filter_write,
1050 o_brw: filter_pgcache_brw,
1051 o_punch: filter_truncate,
1052 o_preprw: filter_preprw,
1053 o_commitrw: filter_commitrw
1055 o_preallocate: filter_preallocate_inodes,
1056 o_migrate: filter_migrate,
1057 o_copy: gen_copy_data,
1058 o_iterate: filter_iterate
1063 static int __init obdfilter_init(void)
1065 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
1066 return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1069 static void __exit obdfilter_exit(void)
1071 obd_unregister_type(OBD_FILTER_DEVICENAME);
1074 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
1075 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1076 MODULE_LICENSE("GPL");
1078 module_init(obdfilter_init);
1079 module_exit(obdfilter_exit);