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>
19 #include <linux/ext3_jbd.h>
20 #include <linux/quotaops.h>
22 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
25 #define FILTER_ROOTINO 2
28 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
30 [S_IFREG >> S_SHIFT] "R",
31 [S_IFDIR >> S_SHIFT] "D",
32 [S_IFCHR >> S_SHIFT] "C",
33 [S_IFBLK >> S_SHIFT] "B",
34 [S_IFIFO >> S_SHIFT] "F",
35 [S_IFSOCK >> S_SHIFT] "S",
36 [S_IFLNK >> S_SHIFT] "L"
39 static inline const char *obd_mode_to_type(int mode)
41 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
44 /* write the pathname into the string */
45 static int filter_id(char *buf, obd_id id, obd_mode mode)
47 return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
48 (unsigned long long)id);
51 /* setup the object store with correct subdirectories */
52 static int filter_prep(struct obd_device *obddev)
54 struct obd_run_ctxt saved;
55 struct filter_obd *filter = &obddev->u.filter;
56 struct dentry *dentry;
65 push_ctxt(&saved, &filter->fo_ctxt);
66 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
67 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
70 CERROR("cannot open/create O: rc = %d\n", rc);
73 filter->fo_dentry_O = dentry;
74 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
75 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
78 CERROR("cannot open/create P: rc = %d\n", rc);
81 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
82 atomic_read(&dentry->d_count) - 1);
84 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
85 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
88 CERROR("cannot open/create D: rc = %d\n", rc);
91 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
92 atomic_read(&dentry->d_count) - 1);
96 * Create directories and/or get dentries for each object type.
97 * This saves us from having to do multiple lookups for each one.
99 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
100 char *type = obd_type_by_mode[mode];
103 filter->fo_dentry_O_mode[mode] = NULL;
106 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
107 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
108 if (IS_ERR(dentry)) {
109 rc = PTR_ERR(dentry);
110 CERROR("cannot create O/%s: rc = %d\n", type, rc);
111 GOTO(out_O_mode, rc);
113 filter->fo_dentry_O_mode[mode] = dentry;
116 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
117 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
120 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
122 GOTO(out_O_mode, rc);
126 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
127 if ( !file || IS_ERR(file) ) {
129 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
131 GOTO(out_O_mode, rc);
134 /* steal operations */
135 inode = file->f_dentry->d_inode;
136 filter->fo_fop = file->f_op;
137 filter->fo_iop = inode->i_op;
138 filter->fo_aops = inode->i_mapping->a_ops;
141 if (inode->i_size == 0) {
142 ssize_t retval = file->f_op->write(file, (char *)&lastino,
143 sizeof(lastino), &off);
144 if (retval != sizeof(lastino)) {
145 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
147 GOTO(out_O_mode, rc = -EIO);
150 ssize_t retval = file->f_op->read(file, (char *)&lastino,
151 sizeof(lastino), &off);
152 if (retval != sizeof(lastino)) {
153 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
155 GOTO(out_O_mode, rc = -EIO);
158 filter->fo_lastino = lastino;
168 while (--mode >= 0) {
169 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
171 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
172 obd_type_by_mode[mode], dentry,
173 atomic_read(&dentry->d_count) - 1);
175 filter->fo_dentry_O_mode[mode] = NULL;
179 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
180 atomic_read(&filter->fo_dentry_O->d_count) - 1);
181 dput(filter->fo_dentry_O);
182 filter->fo_dentry_O = NULL;
186 /* cleanup the filter: write last used object id to status file */
187 static void filter_post(struct obd_device *obddev)
189 struct obd_run_ctxt saved;
190 struct filter_obd *filter = &obddev->u.filter;
196 push_ctxt(&saved, &filter->fo_ctxt);
197 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
199 CERROR("OBD filter: cannot create status file\n");
202 rc = file->f_op->write(file, (char *)&filter->fo_lastino,
203 sizeof(filter->fo_lastino), &off);
204 if (rc != sizeof(filter->fo_lastino))
205 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
207 rc = filp_close(file, NULL);
209 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
211 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
212 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
214 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
215 obd_type_by_mode[mode], dentry,
216 atomic_read(&dentry->d_count) - 1);
218 filter->fo_dentry_O_mode[mode] = NULL;
221 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
222 atomic_read(&filter->fo_dentry_O->d_count) - 1);
223 dput(filter->fo_dentry_O);
229 static __u64 filter_next_id(struct obd_device *obddev)
232 spin_lock(&obddev->u.filter.fo_lock);
233 obddev->u.filter.fo_lastino++;
234 id = obddev->u.filter.fo_lastino;
235 spin_unlock(&obddev->u.filter.fo_lock);
239 /* how to get files, dentries, inodes from object id's */
240 /* parent i_sem is already held if needed for exclusivity */
241 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
242 struct dentry *dparent,
243 __u64 id, __u32 type)
245 struct super_block *sb = obddev->u.filter.fo_sb;
246 struct dentry *dchild;
251 if (!sb || !sb->s_dev) {
252 CERROR("fatal: device not initialized.\n");
253 RETURN(ERR_PTR(-ENXIO));
257 CERROR("fatal: invalid object #0\n");
258 RETURN(ERR_PTR(-ESTALE));
261 if (!(type & S_IFMT)) {
262 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
263 (unsigned long long)id, type);
264 RETURN(ERR_PTR(-EINVAL));
267 len = sprintf(name, "%Ld", id);
268 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
270 dchild = lookup_one_len(name, dparent, len);
271 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
272 obd_mode_to_type(type), name, dchild,
273 atomic_read(&dchild->d_count));
275 if (IS_ERR(dchild)) {
276 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
283 static struct file *filter_obj_open(struct obd_device *obddev,
284 __u64 id, __u32 type)
286 struct super_block *sb = obddev->u.filter.fo_sb;
287 struct obd_run_ctxt saved;
292 if (!sb || !sb->s_dev) {
293 CERROR("fatal: device not initialized.\n");
294 RETURN(ERR_PTR(-ENXIO));
298 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
299 RETURN(ERR_PTR(-ESTALE));
302 if (!(type & S_IFMT)) {
303 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
304 (unsigned long long)id, type);
305 RETURN(ERR_PTR(-EINVAL));
308 filter_id(name, id, type);
309 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
310 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
313 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
320 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
322 struct filter_obd *filter = &obddev->u.filter;
324 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
328 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
329 __u64 id, __u32 type)
331 struct dentry *dentry;
334 dentry = filter_fid2dentry(obddev, filter_parent(obddev, type),
336 if (IS_ERR(dentry)) {
337 CERROR("%s: lookup failed: rc = %ld\n", __FUNCTION__,
343 inode = iget(dentry->d_inode->i_sb, dentry->d_inode->i_ino);
345 CDEBUG(D_INODE, "put child %p, count = %d\n", dentry,
346 atomic_read(&dentry->d_count) - 1);
348 CDEBUG(D_INODE, "got inode %p (%ld), count = %d\n", inode, inode->i_ino,
349 atomic_read(&inode->i_count));
354 static int filter_connect(struct obd_conn *conn)
359 rc = gen_connect(conn);
367 static int filter_disconnect(struct obd_conn *conn)
371 rc = gen_disconnect(conn);
375 /* XXX cleanup preallocated inodes */
379 /* mount the file system (secretly) */
380 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
382 struct obd_ioctl_data* data = buf;
383 struct filter_obd *filter;
384 struct vfsmount *mnt;
388 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
392 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
397 filter = &obddev->u.filter;;
398 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
399 /* XXX is this even possible if do_kern_mount succeeded? */
401 GOTO(err_put, err = -ENODEV);
403 filter->fo_vfsmnt = mnt;
404 filter->fo_fstype = strdup(data->ioc_inlbuf2);
406 filter->fo_ctxt.pwdmnt = mnt;
407 filter->fo_ctxt.pwd = mnt->mnt_root;
408 filter->fo_ctxt.fs = KERNEL_DS;
410 err = filter_prep(obddev);
412 GOTO(err_kfree, err);
413 spin_lock_init(&filter->fo_lock);
418 kfree(filter->fo_fstype);
421 mntput(filter->fo_vfsmnt);
431 static int filter_cleanup(struct obd_device * obddev)
433 struct super_block *sb;
436 if (!(obddev->obd_flags & OBD_SET_UP))
439 if (!list_empty(&obddev->obd_gen_clients)) {
440 CERROR("still has clients!\n");
444 sb = obddev->u.filter.fo_sb;
445 if (!obddev->u.filter.fo_sb)
450 shrink_dcache_parent(sb->s_root);
452 mntput(obddev->u.filter.fo_vfsmnt);
453 obddev->u.filter.fo_sb = 0;
454 kfree(obddev->u.filter.fo_fstype);
463 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
465 int type = oa->o_mode & S_IFMT;
468 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
469 inode->i_ino, (long)oa->o_id, oa->o_valid);
470 obdo_from_inode(oa, inode);
471 oa->o_mode &= ~S_IFMT;
474 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
475 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
476 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
478 *((obd_rdev *)oa->o_inline) = rdev;
479 oa->o_obdflags |= OBD_FL_INLINEDATA;
480 oa->o_valid |= OBD_MD_FLINLINE;
484 else if (filter_has_inline(inode)) {
485 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
486 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
487 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
488 oa->o_obdflags |= OBD_FL_INLINEDATA;
489 oa->o_valid |= OBD_MD_FLINLINE;
492 if (filter_has_obdmd(inode)) {
493 /* XXX this will change when we don't store the obdmd in data */
494 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
495 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
496 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
497 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
498 oa->o_valid |= OBD_MD_FLOBDMD;
504 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
506 struct obd_device *obddev;
507 struct dentry *dentry;
510 if (!gen_client(conn)) {
511 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
515 obddev = conn->oc_dev;
516 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
517 oa->o_id, oa->o_mode);
519 RETURN(PTR_ERR(dentry));
521 oa->o_valid &= ~OBD_MD_FLID;
522 filter_from_inode(oa, dentry->d_inode);
528 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
530 struct obd_run_ctxt saved;
531 struct obd_device *obddev;
532 struct dentry *dentry;
538 if (!gen_client(conn)) {
539 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
543 obddev = conn->oc_dev;
544 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
545 oa->o_id, oa->o_mode);
547 RETURN(PTR_ERR(dentry));
549 inode = dentry->d_inode;
550 iattr_from_obdo(&iattr, oa);
551 iattr.ia_mode &= ~S_IFMT;
552 iattr.ia_mode |= S_IFREG;
554 if (iattr.ia_mode & ATTR_SIZE)
556 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
557 if (inode->i_op->setattr)
558 rc = inode->i_op->setattr(dentry, &iattr);
560 rc = inode_setattr(inode, &iattr);
562 if (iattr.ia_mode & ATTR_SIZE)
566 CDEBUG(D_INODE, "put dentry %p, count = %d\n", inode,
567 atomic_read(&dentry->d_count) - 1);
572 static int filter_open(struct obd_conn *conn, struct obdo *oa)
574 struct obd_device *obddev;
575 struct dentry *dentry;
578 if (!gen_client(conn)) {
579 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
583 obddev = conn->oc_dev;
584 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
585 oa->o_id, oa->o_mode);
587 RETURN(PTR_ERR(dentry));
592 static int filter_close(struct obd_conn *conn, struct obdo *oa)
594 struct obd_device *obddev;
595 struct dentry *dentry;
598 if (!gen_client(conn)) {
599 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
603 obddev = conn->oc_dev;
604 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
605 oa->o_id, oa->o_mode);
607 RETURN(PTR_ERR(dentry));
609 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
610 atomic_read(&dentry->d_count) - 1);
611 dput(dentry); /* for the close */
612 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
613 atomic_read(&dentry->d_count) - 1);
614 dput(dentry); /* for this call */
618 static int filter_create(struct obd_conn* conn, struct obdo *oa)
621 struct obd_run_ctxt saved;
624 struct obd_device *obddev = conn->oc_dev;
628 if (!gen_client(conn)) {
629 CERROR("invalid client %u\n", conn->oc_id);
633 oa->o_id = filter_next_id(conn->oc_dev);
634 if (!(oa->o_mode && S_IFMT)) {
635 CERROR("filter obd: no type!\n");
639 filter_id(name, oa->o_id, oa->o_mode);
640 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
641 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
642 file = filp_open(name, O_RDONLY | O_CREAT, mode);
645 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
650 /* Set flags for fields we have set in the inode struct */
651 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
652 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
653 OBD_MD_FLUID | OBD_MD_FLGID;
655 /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
659 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
661 struct obd_run_ctxt saved;
662 struct obd_device *obddev;
663 struct obd_client *cli;
665 struct dentry *dir_dentry, *object_dentry;
669 if (!(cli = gen_client(conn))) {
670 CERROR("invalid client %u\n", conn->oc_id);
674 CDEBUG(D_INODE, "destroying object %Ld\n",oa->o_id);
675 obddev = conn->oc_dev;
677 dir_dentry = filter_parent(obddev, oa->o_mode);
678 down(&dir_dentry->d_inode->i_sem);
680 object_dentry = filter_fid2dentry(obddev, dir_dentry, oa->o_id,
682 if (IS_ERR(object_dentry))
683 GOTO(out, rc = -ENOENT);
685 inode = object_dentry->d_inode;
686 if (inode->i_nlink != 1) {
687 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
690 inode->i_mode = S_IFREG;
692 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
693 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
695 CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
696 atomic_read(&object_dentry->d_count) - 1);
701 up(&dir_dentry->d_inode->i_sem);
705 /* NB count and offset are used for punch, but not truncate */
706 static int filter_truncate(struct obd_conn *conn, struct obdo *oa,
707 obd_size count, obd_off offset)
712 CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
713 "o_size = %Ld\n", oa->o_id, oa->o_valid, oa->o_size);
714 error = filter_setattr(conn, oa);
715 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
720 /* buffer must lie in user memory here */
721 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
722 obd_size *count, obd_off offset)
725 unsigned long retval;
729 if (!gen_client(conn)) {
730 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
734 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
736 RETURN(PTR_ERR(file));
738 /* count doubles as retval */
739 retval = file->f_op->read(file, buf, *count, (loff_t *)&offset);
754 /* buffer must lie in user memory here */
755 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
756 obd_size *count, obd_off offset)
758 struct obd_run_ctxt saved;
761 unsigned long retval;
764 if (!gen_client(conn)) {
765 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
769 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
771 RETURN(PTR_ERR(file));
773 /* count doubles as retval */
774 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
775 retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
792 static int filter_pgcache_brw(int rw, struct obd_conn *conn, obd_count num_oa,
793 struct obdo **oa, obd_count *oa_bufs,
794 struct page **pages, obd_size *count,
795 obd_off *offset, obd_flag *flags)
797 struct obd_run_ctxt saved;
798 struct super_block *sb;
799 int onum; /* index to oas */
800 int pnum; /* index to pages (bufs) */
801 unsigned long retval;
806 if (!gen_client(conn)) {
807 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
811 sb = conn->oc_dev->u.filter.fo_sb;
813 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
814 pnum = 0; /* pnum indexes buf 0..num_pages */
815 for (onum = 0; onum < num_oa; onum++) {
818 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
821 GOTO(out, retval = PTR_ERR(file));
823 /* count doubles as retval */
824 for (pg = 0; pg < oa_bufs[onum]; pg++) {
825 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) "
826 "off count (%Ld,%Ld)\n",
827 rw, onum, pnum, file->f_dentry->d_inode->i_ino,
828 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
829 (unsigned long long)offset[pnum],
830 (unsigned long long)count[pnum]);
835 buffer = kmap(pages[pnum]);
836 retval = file->f_op->write(file, buffer, count[pnum], &off);
838 CDEBUG(D_INODE, "retval %ld\n", retval);
840 loff_t off = offset[pnum];
841 char *buffer = kmap(pages[pnum]);
843 if (off >= file->f_dentry->d_inode->i_size) {
844 memset(buffer, 0, count[pnum]);
845 retval = count[pnum];
847 retval = file->f_op->read(file, buffer, count[pnum], &off);
851 if (retval != count[pnum]) {
853 GOTO(out, retval = -EIO);
855 CDEBUG(D_INODE, "retval %ld\n", retval);
859 /* sizes and blocks are set by generic_file_write */
860 /* ctimes/mtimes will follow with a setattr call */
868 error = (retval >= 0) ? 0 : retval;
873 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
875 struct super_block *sb = conn->oc_dev->u.filter.fo_sb;
876 struct inode *inode = NULL;
879 if (!sb || !sb->s_dev) {
880 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
885 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
889 inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
890 if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
891 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
892 (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
893 "no links" : "NULL");
902 * Calculate the number of buffer credits needed to write multiple pages in
903 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
904 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
906 * See comment above ext3_writepage_trans_blocks for details. We assume
907 * no data journaling is being done, but it does allow for all of the pages
908 * being non-contiguous. If we are guaranteed contiguous pages we could
909 * reduce the number of (d)indirect blocks a lot.
911 * With N blocks per page and P pages, for each inode we have at most:
913 * min(N*P, blocksize/4 + 1) dindirect blocks
916 * For the entire filesystem, we have at most:
917 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
918 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
921 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
923 static int ext3_credits_needed(struct super_block *sb, int objcount,
924 struct obd_ioobj *obj)
926 struct obd_ioobj *o = obj;
927 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
928 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
931 int needed = objcount + 1;
934 for (i = 0; i < objcount; i++, o++) {
935 int nblocks = o->ioo_bufcnt * blockpp;
936 int ndindirect = min(nblocks, addrpp + 1);
937 int nindir = nblocks + ndindirect + 1;
939 nbitmaps += nindir + nblocks;
940 ngdblocks += nindir + nblocks;
945 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
946 nbitmaps = EXT3_SB(sb)->s_groups_count;
947 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
948 ngdblocks = EXT3_SB(sb)->s_gdb_count;
950 needed += nbitmaps + ngdblocks;
953 /* We assume that there will be 1 bit set in s_dquot.flags for each
954 * quota file that is active. This is at least true for now.
956 needed += hweight32(sb_any_quota_enabled(sb)) *
957 EXT3_SINGLEDATA_TRANS_BLOCKS;
963 /* We have to start a huge journal transaction here to hold all of the
964 * metadata for the pages being written here. This is necessitated by
965 * the fact that we do lots of prepare_write operations before we do
966 * any of the matching commit_write operations, so even if we split
967 * up to use "smaller" transactions none of them could complete until
968 * all of them were opened. By having a single journal transaction,
969 * we eliminate duplicate reservations for common blocks like the
970 * superblock and group descriptors or bitmaps.
972 * We will start the transaction here, but each prepare_write will
973 * add a refcount to the transaction, and each commit_write will
974 * remove a refcount. The transaction will be closed when all of
975 * the pages have been written.
977 static void *ext3_filter_journal_start(struct filter_obd *filter,
978 int objcount, struct obd_ioobj *obj,
979 int niocount, struct niobuf_remote *nb)
981 journal_t *journal = NULL;
982 handle_t *handle = NULL;
985 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
986 * with having extN built properly before filterobd for now.
988 journal = EXT3_SB(filter->fo_sb)->s_journal;
989 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
991 /* The number of blocks we could _possibly_ dirty can very large.
992 * We reduce our request if it is absurd (and we couldn't get that
993 * many credits for a single handle anyways).
995 * At some point we have to limit the size of I/Os sent at one time,
996 * increase the size of the journal, or we have to calculate the
997 * actual journal requirements more carefully by checking all of
998 * the blocks instead of being maximally pessimistic. It remains to
999 * be seen if this is a real problem or not.
1001 if (needed > journal->j_max_transaction_buffers) {
1002 CERROR("want too many journal credits (%d) using %d instead\n",
1003 needed, journal->j_max_transaction_buffers);
1004 needed = journal->j_max_transaction_buffers;
1007 handle = journal_start(journal, needed);
1009 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
1015 static void *filter_journal_start(void **journal_save,
1016 struct filter_obd *filter,
1017 int objcount, struct obd_ioobj *obj,
1018 int niocount, struct niobuf_remote *nb)
1020 void *handle = NULL;
1022 /* This may not be necessary - we probably never have a
1023 * transaction started when we enter here, so we can
1024 * remove the saving of the journal state entirely.
1025 * For now leave it in just to see if it ever happens.
1027 *journal_save = current->journal_info;
1028 if (*journal_save) {
1029 CERROR("Already have handle %p???\n", *journal_save);
1031 current->journal_info = NULL;
1034 if (!strcmp(filter->fo_fstype, "ext3") ||
1035 !strcmp(filter->fo_fstype, "extN"))
1036 handle = ext3_filter_journal_start(filter, objcount, obj,
1041 static int ext3_filter_journal_stop(void *handle)
1045 /* We got a refcount on the handle for each call to prepare_write,
1046 * so we can drop the "parent" handle here to avoid the need for
1047 * osc to call back into filterobd to close the handle. The
1048 * remaining references will be dropped in commit_write.
1050 rc = journal_stop((handle_t *)handle);
1055 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
1060 if (!strcmp(filter->fo_fstype, "ext3") ||
1061 !strcmp(filter->fo_fstype, "extN"))
1062 rc = ext3_filter_journal_stop(handle);
1064 current->journal_info = journal_save;
1069 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1070 struct niobuf_local *lnb)
1072 struct address_space *mapping = inode->i_mapping;
1076 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1077 page = grab_cache_page_nowait(mapping, index); /* locked page */
1079 /* This page is currently locked, so we grab a new one temporarily */
1082 addr = __get_free_pages(GFP_KERNEL, 0);
1084 CERROR("no memory for a temp page\n");
1086 GOTO(err, rc = -ENOMEM);
1088 page = virt_to_page(addr);
1090 page->index = index;
1091 lnb->flags |= N_LOCAL_TEMP_PAGE;
1092 } else if (!IS_ERR(page)) {
1093 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1094 * a no-op for most filesystems, because we write the whole
1095 * page. For partial-page I/O this will read in the page.
1097 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1099 CERROR("page index %lu, rc = %d\n", index, rc);
1102 GOTO(err_unlock, rc);
1104 /* XXX not sure if we need this if we are overwriting page */
1105 if (PageError(page)) {
1106 CERROR("error on page index %lu, rc = %d\n", index, rc);
1108 GOTO(err_unlock, rc = -EIO);
1117 lustre_put_page(page);
1122 static int filter_preprw(int cmd, struct obd_conn *conn,
1123 int objcount, struct obd_ioobj *obj,
1124 int niocount, struct niobuf_remote *nb,
1125 struct niobuf_local *res, void **desc_private)
1127 struct obd_run_ctxt saved;
1128 struct obd_device *obddev;
1129 struct obd_ioobj *o = obj;
1130 struct niobuf_remote *b = nb;
1131 struct niobuf_local *r = res;
1132 void *journal_save = NULL;
1137 memset(res, 0, sizeof(*res) * niocount);
1138 obddev = conn->oc_dev;
1140 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
1142 if (cmd == OBD_BRW_WRITE) {
1143 *desc_private = filter_journal_start(&journal_save,
1145 objcount, obj, niocount,
1147 if (IS_ERR(*desc_private))
1148 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1151 for (i = 0; i < objcount; i++, o++) {
1152 struct dentry *dentry;
1153 struct inode *inode;
1156 dentry = filter_fid2dentry(obddev,
1157 filter_parent(obddev, S_IFREG),
1158 o->ioo_id, S_IFREG);
1159 inode = dentry->d_inode;
1161 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1162 unsigned long index = b->offset >> PAGE_SHIFT;
1165 /* XXX We _might_ change this to a dcount if we
1166 * wanted to pass a dentry pointer in the niobuf
1167 * to avoid doing so many igets on an inode we
1168 * already have. It appears to be solely for the
1169 * purpose of having a refcount that we can drop
1170 * in commitrw where we get one call per page.
1173 r->dentry = dget(dentry);
1177 /* FIXME: we need to iput all inodes on error */
1181 if (cmd == OBD_BRW_WRITE) {
1182 page = filter_get_page_write(inode, index, r);
1184 /* We unlock the page to avoid deadlocks with
1185 * the page I/O because we are preparing
1186 * multiple pages at one time and we have lock
1187 * ordering problems. Lustre I/O and disk I/O
1188 * on this page can happen concurrently.
1191 page = lustre_get_page_read(inode, index);
1193 /* FIXME: we need to clean up here... */
1195 RETURN(PTR_ERR(page));
1197 r->addr = page_address(page);
1198 r->offset = b->offset;
1204 if (cmd == OBD_BRW_WRITE) {
1205 /* FIXME: need to clean up here */
1206 rc = filter_journal_stop(journal_save, &obddev->u.filter,
1214 static int filter_write_locked_page(struct niobuf_local *lnb)
1219 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1222 memcpy(page_address(lpage), kmap(lnb->page), PAGE_SIZE);
1224 __free_pages(lnb->page, 0);
1226 rc = lustre_commit_page(lpage, 0, PAGE_SIZE);
1232 static int filter_commitrw(int cmd, struct obd_conn *conn,
1233 int objcount, struct obd_ioobj *obj,
1234 int niocount, struct niobuf_local *res,
1237 struct obd_run_ctxt saved;
1238 struct obd_ioobj *o = obj;
1239 struct niobuf_local *r = res;
1241 int found_locked = 0;
1245 // if (cmd == OBD_BRW_WRITE)
1246 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
1247 journal_save = current->journal_info;
1249 CERROR("Existing handle %p???\n", journal_save);
1250 current->journal_info = private;
1251 for (i = 0; i < objcount; i++, obj++) {
1253 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1254 struct page *page = r->page;
1256 /* If there was an error setting up a particular page
1257 * for I/O we still need to continue with the rest of
1258 * the pages in order to balance prepate/commit_write
1259 * calls, and to complete as much I/O as possible.
1264 if (r->flags & N_LOCAL_TEMP_PAGE) {
1269 if (cmd == OBD_BRW_WRITE) {
1271 rc = lustre_commit_page(page, 0, PAGE_SIZE);
1273 /* FIXME: still need to iput the other inodes */
1277 lustre_put_page(page);
1280 "put inode %p (%ld), count = %d, nlink = %d\n",
1281 page->mapping->host,
1282 page->mapping->host->i_ino,
1283 atomic_read(&page->mapping->host->i_count) - 1,
1284 page->mapping->host->i_nlink);
1291 for (i = 0; i < objcount; i++, obj++) {
1293 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1295 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1298 rc = filter_write_locked_page(r);
1304 current->journal_info = journal_save;
1309 static int filter_statfs(struct obd_conn *conn, struct statfs * statfs)
1311 struct super_block *sb;
1315 if (!gen_client(conn)) {
1316 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1320 sb = conn->oc_dev->u.filter.fo_sb;
1322 err = sb->s_op->statfs(sb, statfs);
1324 } /* filter_statfs */
1327 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
1328 void *key, obd_count *vallen, void **val)
1330 struct obd_device *obddev;
1331 struct obd_client * cli;
1334 if (!(cli = gen_client(conn))) {
1335 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1339 obddev = conn->oc_dev;
1341 if ( keylen == strlen("blocksize") &&
1342 memcmp(key, "blocksize", keylen) == 0 ) {
1343 *vallen = sizeof(long);
1344 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize;
1348 if ( keylen == strlen("blocksize_bits") &&
1349 memcmp(key, "blocksize_bits", keylen) == 0 ){
1350 *vallen = sizeof(long);
1351 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize_bits;
1355 if ( keylen == strlen("root_ino") &&
1356 memcmp(key, "root_ino", keylen) == 0 ){
1357 *vallen = sizeof(long);
1358 *val = (void *)(long)FILTER_ROOTINO;
1362 CDEBUG(D_IOCTL, "invalid key\n");
1367 static struct obd_ops filter_obd_ops = {
1368 o_get_info: filter_get_info,
1369 o_setup: filter_setup,
1370 o_cleanup: filter_cleanup,
1371 o_connect: filter_connect,
1372 o_disconnect: filter_disconnect,
1373 o_statfs: filter_statfs,
1374 o_getattr: filter_getattr,
1375 o_create: filter_create,
1376 o_setattr: filter_setattr,
1377 o_destroy: filter_destroy,
1378 o_open: filter_open,
1379 o_close: filter_close,
1380 o_read: filter_read,
1381 o_write: filter_write,
1382 o_brw: filter_pgcache_brw,
1383 o_punch: filter_truncate,
1384 o_preprw: filter_preprw,
1385 o_commitrw: filter_commitrw
1387 o_preallocate: filter_preallocate_inodes,
1388 o_migrate: filter_migrate,
1389 o_copy: gen_copy_data,
1390 o_iterate: filter_iterate
1395 static int __init obdfilter_init(void)
1397 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
1398 return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1401 static void __exit obdfilter_exit(void)
1403 obd_unregister_type(OBD_FILTER_DEVICENAME);
1406 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
1407 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1408 MODULE_LICENSE("GPL");
1410 module_init(obdfilter_init);
1411 module_exit(obdfilter_exit);