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/lustre_dlm.h>
19 #include <linux/obd_filter.h>
20 #include <linux/ext3_jbd.h>
21 #include <linux/quotaops.h>
22 #include <linux/init.h>
24 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
27 #define FILTER_ROOTINO 2
30 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
32 [S_IFREG >> S_SHIFT] "R",
33 [S_IFDIR >> S_SHIFT] "D",
34 [S_IFCHR >> S_SHIFT] "C",
35 [S_IFBLK >> S_SHIFT] "B",
36 [S_IFIFO >> S_SHIFT] "F",
37 [S_IFSOCK >> S_SHIFT] "S",
38 [S_IFLNK >> S_SHIFT] "L"
41 static inline const char *obd_mode_to_type(int mode)
43 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
46 /* write the pathname into the string */
47 static int filter_id(char *buf, obd_id id, obd_mode mode)
49 return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
50 (unsigned long long)id);
53 /* setup the object store with correct subdirectories */
54 static int filter_prep(struct obd_device *obddev)
56 struct obd_run_ctxt saved;
57 struct filter_obd *filter = &obddev->u.filter;
58 struct dentry *dentry;
67 push_ctxt(&saved, &filter->fo_ctxt);
68 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
69 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
72 CERROR("cannot open/create O: rc = %d\n", rc);
75 filter->fo_dentry_O = dentry;
76 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
77 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
80 CERROR("cannot open/create P: rc = %d\n", rc);
83 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
84 atomic_read(&dentry->d_count) - 1);
86 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
87 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
90 CERROR("cannot open/create D: rc = %d\n", rc);
93 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
94 atomic_read(&dentry->d_count) - 1);
98 * Create directories and/or get dentries for each object type.
99 * This saves us from having to do multiple lookups for each one.
101 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
102 char *type = obd_type_by_mode[mode];
105 filter->fo_dentry_O_mode[mode] = NULL;
108 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
109 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
110 if (IS_ERR(dentry)) {
111 rc = PTR_ERR(dentry);
112 CERROR("cannot create O/%s: rc = %d\n", type, rc);
113 GOTO(out_O_mode, rc);
115 filter->fo_dentry_O_mode[mode] = dentry;
118 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
119 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
122 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
124 GOTO(out_O_mode, rc);
128 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
129 if ( !file || IS_ERR(file) ) {
131 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
133 GOTO(out_O_mode, rc);
136 /* steal operations */
137 inode = file->f_dentry->d_inode;
138 filter->fo_fop = file->f_op;
139 filter->fo_iop = inode->i_op;
140 filter->fo_aops = inode->i_mapping->a_ops;
143 if (inode->i_size == 0) {
144 ssize_t retval = file->f_op->write(file, (char *)&lastino,
145 sizeof(lastino), &off);
146 if (retval != sizeof(lastino)) {
147 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
149 GOTO(out_O_mode, rc = -EIO);
152 ssize_t retval = file->f_op->read(file, (char *)&lastino,
153 sizeof(lastino), &off);
154 if (retval != sizeof(lastino)) {
155 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
157 GOTO(out_O_mode, rc = -EIO);
160 filter->fo_lastino = lastino;
171 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
173 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
174 obd_type_by_mode[mode], dentry,
175 atomic_read(&dentry->d_count) - 1);
177 filter->fo_dentry_O_mode[mode] = NULL;
181 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
182 atomic_read(&filter->fo_dentry_O->d_count) - 1);
183 dput(filter->fo_dentry_O);
184 filter->fo_dentry_O = NULL;
188 /* cleanup the filter: write last used object id to status file */
189 static void filter_post(struct obd_device *obddev)
191 struct obd_run_ctxt saved;
192 struct filter_obd *filter = &obddev->u.filter;
198 push_ctxt(&saved, &filter->fo_ctxt);
199 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
201 CERROR("OBD filter: cannot create status file\n");
204 rc = file->f_op->write(file, (char *)&filter->fo_lastino,
205 sizeof(filter->fo_lastino), &off);
206 if (rc != sizeof(filter->fo_lastino))
207 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
209 rc = filp_close(file, NULL);
211 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
213 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
214 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
216 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
217 obd_type_by_mode[mode], dentry,
218 atomic_read(&dentry->d_count) - 1);
220 filter->fo_dentry_O_mode[mode] = NULL;
223 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
224 atomic_read(&filter->fo_dentry_O->d_count) - 1);
225 dput(filter->fo_dentry_O);
231 static __u64 filter_next_id(struct obd_device *obddev)
234 spin_lock(&obddev->u.filter.fo_lock);
235 obddev->u.filter.fo_lastino++;
236 id = obddev->u.filter.fo_lastino;
237 spin_unlock(&obddev->u.filter.fo_lock);
241 /* how to get files, dentries, inodes from object id's */
242 /* parent i_sem is already held if needed for exclusivity */
243 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
244 struct dentry *dparent,
245 __u64 id, __u32 type)
247 struct super_block *sb = obddev->u.filter.fo_sb;
248 struct dentry *dchild;
253 if (!sb || !sb->s_dev) {
254 CERROR("fatal: device not initialized.\n");
255 RETURN(ERR_PTR(-ENXIO));
259 CERROR("fatal: invalid object #0\n");
261 RETURN(ERR_PTR(-ESTALE));
264 if (!(type & S_IFMT)) {
265 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
266 (unsigned long long)id, type);
267 RETURN(ERR_PTR(-EINVAL));
270 len = sprintf(name, "%Ld", id);
271 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
273 dchild = lookup_one_len(name, dparent, len);
274 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
275 obd_mode_to_type(type), name, dchild,
276 atomic_read(&dchild->d_count));
278 if (IS_ERR(dchild)) {
279 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
286 static struct file *filter_obj_open(struct obd_device *obddev,
287 __u64 id, __u32 type)
289 struct super_block *sb = obddev->u.filter.fo_sb;
290 struct obd_run_ctxt saved;
295 if (!sb || !sb->s_dev) {
296 CERROR("fatal: device not initialized.\n");
297 RETURN(ERR_PTR(-ENXIO));
301 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
302 RETURN(ERR_PTR(-ESTALE));
305 if (!(type & S_IFMT)) {
306 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
307 (unsigned long long)id, type);
308 RETURN(ERR_PTR(-EINVAL));
311 filter_id(name, id, type);
312 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
313 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
316 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
323 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
325 struct filter_obd *filter = &obddev->u.filter;
327 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
331 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
332 __u64 id, __u32 type)
334 struct dentry *dentry;
337 dentry = filter_fid2dentry(obddev, filter_parent(obddev, type),
339 if (IS_ERR(dentry)) {
340 CERROR("%s: lookup failed: rc = %ld\n", __FUNCTION__,
346 inode = iget(dentry->d_inode->i_sb, dentry->d_inode->i_ino);
348 CDEBUG(D_INODE, "put child %p, count = %d\n", dentry,
349 atomic_read(&dentry->d_count) - 1);
351 CDEBUG(D_INODE, "got inode %p (%ld), count = %d\n", inode, inode->i_ino,
352 atomic_read(&inode->i_count));
357 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd)
362 rc = class_connect(conn, obd);
368 static int filter_disconnect(struct lustre_handle *conn)
373 rc = class_disconnect(conn);
377 /* XXX cleanup preallocated inodes */
381 /* mount the file system (secretly) */
382 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
384 struct obd_ioctl_data* data = buf;
385 struct filter_obd *filter;
386 struct vfsmount *mnt;
390 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
394 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
399 filter = &obddev->u.filter;;
400 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
401 /* XXX is this even possible if do_kern_mount succeeded? */
403 GOTO(err_put, err = -ENODEV);
405 filter->fo_vfsmnt = mnt;
406 filter->fo_fstype = strdup(data->ioc_inlbuf2);
408 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
409 filter->fo_ctxt.pwdmnt = mnt;
410 filter->fo_ctxt.pwd = mnt->mnt_root;
411 filter->fo_ctxt.fs = get_ds();
413 err = filter_prep(obddev);
415 GOTO(err_kfree, err);
416 spin_lock_init(&filter->fo_lock);
418 obddev->obd_namespace =
419 ldlm_namespace_new("filter-tgt", LDLM_NAMESPACE_SERVER);
420 if (obddev->obd_namespace == NULL)
426 kfree(filter->fo_fstype);
429 mntput(filter->fo_vfsmnt);
439 static int filter_cleanup(struct obd_device * obddev)
441 struct super_block *sb;
444 if (!(obddev->obd_flags & OBD_SET_UP))
447 if (!list_empty(&obddev->obd_exports)) {
448 CERROR("still has clients!\n");
452 ldlm_namespace_free(obddev->obd_namespace);
454 sb = obddev->u.filter.fo_sb;
455 if (!obddev->u.filter.fo_sb)
460 shrink_dcache_parent(sb->s_root);
462 mntput(obddev->u.filter.fo_vfsmnt);
463 obddev->u.filter.fo_sb = 0;
464 kfree(obddev->u.filter.fo_fstype);
473 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
475 int type = oa->o_mode & S_IFMT;
478 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
479 inode->i_ino, (long)oa->o_id, oa->o_valid);
480 obdo_from_inode(oa, inode);
481 oa->o_mode &= ~S_IFMT;
484 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
485 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
487 oa->o_valid |= OBD_MD_FLRDEV;
493 static int filter_getattr(struct lustre_handle *conn, struct obdo *oa,
494 struct lov_stripe_md *md)
496 struct obd_device *obddev = class_conn2obd(conn);
497 struct dentry *dentry;
500 if (!class_conn2export(conn)) {
501 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
505 obddev = class_conn2obd(conn);
506 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
507 oa->o_id, oa->o_mode);
509 RETURN(PTR_ERR(dentry));
511 oa->o_valid &= ~OBD_MD_FLID;
512 filter_from_inode(oa, dentry->d_inode);
518 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
519 struct lov_stripe_md *md)
521 struct obd_run_ctxt saved;
522 struct obd_device *obd = class_conn2obd(conn);
523 struct dentry *dentry;
529 iattr_from_obdo(&iattr, oa);
530 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
531 dentry = filter_fid2dentry(obd, filter_parent(obd, iattr.ia_mode),
532 oa->o_id, iattr.ia_mode);
534 RETURN(PTR_ERR(dentry));
536 inode = dentry->d_inode;
538 if (iattr.ia_mode & ATTR_SIZE)
540 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
541 if (inode->i_op->setattr)
542 rc = inode->i_op->setattr(dentry, &iattr);
544 rc = inode_setattr(inode, &iattr);
546 if (iattr.ia_mode & ATTR_SIZE) {
548 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
549 obdo_from_inode(oa, inode);
553 CDEBUG(D_INODE, "put dentry %p, count = %d\n", inode,
554 atomic_read(&dentry->d_count) - 1);
559 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
560 struct lov_stripe_md *ea)
562 struct obd_device *obd;
563 struct dentry *dentry;
566 if (!class_conn2export(conn)) {
567 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
571 obd = class_conn2obd(conn);
572 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
573 oa->o_id, oa->o_mode);
575 RETURN(PTR_ERR(dentry));
580 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
581 struct lov_stripe_md *ea)
583 struct obd_device *obd;
584 struct dentry *dentry;
587 if (!class_conn2export(conn)) {
588 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
592 obd = class_conn2obd(conn);
593 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
594 oa->o_id, oa->o_mode);
596 RETURN(PTR_ERR(dentry));
598 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
599 atomic_read(&dentry->d_count) - 1);
600 dput(dentry); /* for the close */
601 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
602 atomic_read(&dentry->d_count) - 1);
603 dput(dentry); /* for this call */
607 static int filter_create(struct lustre_handle* conn, struct obdo *oa,
608 struct lov_stripe_md **ea)
611 struct obd_run_ctxt saved;
614 struct obd_device *obd = class_conn2obd(conn);
618 if (!class_conn2export(conn)) {
619 CERROR("invalid client %Lx\n", conn->addr);
623 oa->o_id = filter_next_id(obd);
624 if (!(oa->o_mode && S_IFMT)) {
625 CERROR("filter obd: no type!\n");
629 filter_id(name, oa->o_id, oa->o_mode);
630 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
631 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
632 file = filp_open(name, O_RDONLY | O_CREAT, mode);
635 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
640 /* Set flags for fields we have set in the inode struct */
641 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
642 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
643 OBD_MD_FLUID | OBD_MD_FLGID;
645 /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
649 static int filter_destroy(struct lustre_handle *conn, struct obdo *oa,
650 struct lov_stripe_md *ea)
652 struct obd_device *obd;
653 struct filter_obd *filter;
654 struct obd_run_ctxt saved;
655 struct obd_export *export;
657 struct dentry *dir_dentry, *object_dentry;
661 if (!(export = class_conn2export(conn))) {
662 CERROR("invalid client %Lx\n", conn->addr);
666 CDEBUG(D_INODE, "destroying object %Ld\n", oa->o_id);
667 obd = class_conn2obd(conn);
669 dir_dentry = filter_parent(obd, oa->o_mode);
670 down(&dir_dentry->d_inode->i_sem);
672 object_dentry = filter_fid2dentry(obd, dir_dentry, oa->o_id,
674 if (IS_ERR(object_dentry))
675 GOTO(out, rc = -ENOENT);
677 inode = object_dentry->d_inode;
678 if (inode->i_nlink != 1) {
679 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
683 inode->i_mode = S_IFREG;
685 filter = &obd->u.filter;
686 push_ctxt(&saved, &filter->fo_ctxt);
688 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
690 CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
691 atomic_read(&object_dentry->d_count) - 1);
696 up(&dir_dentry->d_inode->i_sem);
700 /* NB count and offset are used for punch, but not truncate */
701 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
702 struct lov_stripe_md *md,
703 obd_off start, obd_off end)
708 if ( end != 0xffffffffffffffff ) {
709 CERROR("PUNCH not supported, only truncate works\n");
712 CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
713 "o_size = %Ld\n", oa->o_id, oa->o_valid, start);
715 error = filter_setattr(conn, oa, NULL);
719 static int filter_pgcache_brw(int cmd, struct lustre_handle *conn,
720 struct lov_stripe_md *md, obd_count oa_bufs,
721 struct page **pages, obd_size *count,
722 obd_off *offset, obd_flag *flags,
723 brw_callback_t callback, void *data)
725 struct obd_run_ctxt saved;
726 struct super_block *sb;
727 int pnum; /* index to pages (bufs) */
728 unsigned long retval;
731 struct obd_device *obd = class_conn2obd(conn);
735 if (!class_conn2export(conn)) {
736 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
740 sb = obd->u.filter.fo_sb;
741 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
742 pnum = 0; /* pnum indexes buf 0..num_pages */
744 file = filter_obj_open(obd, md->lmd_object_id, S_IFREG);
746 GOTO(out, retval = PTR_ERR(file));
748 /* count doubles as retval */
749 for (pg = 0; pg < oa_bufs; pg++) {
750 CDEBUG(D_INODE, "OP %d obdo pgno: (%d) (%ld,%ld) "
751 "off count (%Ld,%Ld)\n",
752 cmd, pnum, file->f_dentry->d_inode->i_ino,
753 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
754 (unsigned long long)offset[pnum],
755 (unsigned long long)count[pnum]);
756 if (cmd & OBD_BRW_WRITE) {
760 buffer = kmap(pages[pnum]);
761 retval = file->f_op->write(file, buffer, count[pnum],
764 CDEBUG(D_INODE, "retval %ld\n", retval);
766 loff_t off = offset[pnum];
767 char *buffer = kmap(pages[pnum]);
769 if (off >= file->f_dentry->d_inode->i_size) {
770 memset(buffer, 0, count[pnum]);
771 retval = count[pnum];
773 retval = file->f_op->read(file, buffer,
778 if (retval != count[pnum]) {
780 GOTO(out, retval = -EIO);
782 CDEBUG(D_INODE, "retval %ld\n", retval);
786 /* sizes and blocks are set by generic_file_write */
787 /* ctimes/mtimes will follow with a setattr call */
790 /* XXX: do something with callback if it is set? */
795 error = (retval >= 0) ? 0 : retval;
800 struct inode *ioobj_to_inode(struct lustre_handle *conn, struct obd_ioobj *o)
802 struct obd_device *obd = class_conn2obd(conn);
803 struct super_block *sb = obd->u.filter.fo_sb;
804 struct inode *inode = NULL;
807 if (!sb || !sb->s_dev) {
808 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
813 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
817 inode = filter_inode_from_obj(obd, o->ioo_id, S_IFREG);
818 if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
819 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
820 (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
821 "no links" : "NULL");
830 * Calculate the number of buffer credits needed to write multiple pages in
831 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
832 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
834 * See comment above ext3_writepage_trans_blocks for details. We assume
835 * no data journaling is being done, but it does allow for all of the pages
836 * being non-contiguous. If we are guaranteed contiguous pages we could
837 * reduce the number of (d)indirect blocks a lot.
839 * With N blocks per page and P pages, for each inode we have at most:
841 * min(N*P, blocksize/4 + 1) dindirect blocks
844 * For the entire filesystem, we have at most:
845 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
846 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
849 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
851 static int ext3_credits_needed(struct super_block *sb, int objcount,
852 struct obd_ioobj *obj)
854 struct obd_ioobj *o = obj;
855 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
856 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
859 int needed = objcount + 1;
862 for (i = 0; i < objcount; i++, o++) {
863 int nblocks = o->ioo_bufcnt * blockpp;
864 int ndindirect = min(nblocks, addrpp + 1);
865 int nindir = nblocks + ndindirect + 1;
867 nbitmaps += nindir + nblocks;
868 ngdblocks += nindir + nblocks;
873 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
874 nbitmaps = EXT3_SB(sb)->s_groups_count;
875 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
876 ngdblocks = EXT3_SB(sb)->s_gdb_count;
878 needed += nbitmaps + ngdblocks;
881 /* We assume that there will be 1 bit set in s_dquot.flags for each
882 * quota file that is active. This is at least true for now.
884 needed += hweight32(sb_any_quota_enabled(sb)) *
885 EXT3_SINGLEDATA_TRANS_BLOCKS;
891 /* We have to start a huge journal transaction here to hold all of the
892 * metadata for the pages being written here. This is necessitated by
893 * the fact that we do lots of prepare_write operations before we do
894 * any of the matching commit_write operations, so even if we split
895 * up to use "smaller" transactions none of them could complete until
896 * all of them were opened. By having a single journal transaction,
897 * we eliminate duplicate reservations for common blocks like the
898 * superblock and group descriptors or bitmaps.
900 * We will start the transaction here, but each prepare_write will
901 * add a refcount to the transaction, and each commit_write will
902 * remove a refcount. The transaction will be closed when all of
903 * the pages have been written.
905 static void *ext3_filter_journal_start(struct filter_obd *filter,
906 int objcount, struct obd_ioobj *obj,
907 int niocount, struct niobuf_remote *nb)
909 journal_t *journal = NULL;
910 handle_t *handle = NULL;
913 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
914 * with having extN built properly before filterobd for now.
916 journal = EXT3_SB(filter->fo_sb)->s_journal;
917 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
919 /* The number of blocks we could _possibly_ dirty can very large.
920 * We reduce our request if it is absurd (and we couldn't get that
921 * many credits for a single handle anyways).
923 * At some point we have to limit the size of I/Os sent at one time,
924 * increase the size of the journal, or we have to calculate the
925 * actual journal requirements more carefully by checking all of
926 * the blocks instead of being maximally pessimistic. It remains to
927 * be seen if this is a real problem or not.
929 if (needed > journal->j_max_transaction_buffers) {
930 CERROR("want too many journal credits (%d) using %d instead\n",
931 needed, journal->j_max_transaction_buffers);
932 needed = journal->j_max_transaction_buffers;
935 handle = journal_start(journal, needed);
937 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
943 static void *filter_journal_start(void **journal_save,
944 struct filter_obd *filter,
945 int objcount, struct obd_ioobj *obj,
946 int niocount, struct niobuf_remote *nb)
950 /* This may not be necessary - we probably never have a
951 * transaction started when we enter here, so we can
952 * remove the saving of the journal state entirely.
953 * For now leave it in just to see if it ever happens.
955 *journal_save = current->journal_info;
957 CERROR("Already have handle %p???\n", *journal_save);
959 current->journal_info = NULL;
962 if (!strcmp(filter->fo_fstype, "ext3") ||
963 !strcmp(filter->fo_fstype, "extN"))
964 handle = ext3_filter_journal_start(filter, objcount, obj,
969 static int ext3_filter_journal_stop(void *handle)
973 /* We got a refcount on the handle for each call to prepare_write,
974 * so we can drop the "parent" handle here to avoid the need for
975 * osc to call back into filterobd to close the handle. The
976 * remaining references will be dropped in commit_write.
978 rc = journal_stop((handle_t *)handle);
983 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
988 if (!strcmp(filter->fo_fstype, "ext3") ||
989 !strcmp(filter->fo_fstype, "extN"))
990 rc = ext3_filter_journal_stop(handle);
993 CERROR("error on journal stop: rc = %d\n", rc);
995 current->journal_info = journal_save;
1000 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1001 struct niobuf_local *lnb)
1003 struct address_space *mapping = inode->i_mapping;
1007 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1008 page = grab_cache_page_nowait(mapping, index); /* locked page */
1010 /* This page is currently locked, so get a temporary page instead */
1013 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
1014 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1016 CERROR("no memory for a temp page\n");
1018 GOTO(err, rc = -ENOMEM);
1020 page = virt_to_page(addr);
1022 page->index = index;
1023 lnb->flags |= N_LOCAL_TEMP_PAGE;
1024 } else if (!IS_ERR(page)) {
1025 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1026 * a no-op for most filesystems, because we write the whole
1027 * page. For partial-page I/O this will read in the page.
1030 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1032 CERROR("page index %lu, rc = %d\n", index, rc);
1035 GOTO(err_unlock, rc);
1037 /* XXX not sure if we need this if we are overwriting page */
1038 if (PageError(page)) {
1039 CERROR("error on page index %lu, rc = %d\n", index, rc);
1041 GOTO(err_unlock, rc = -EIO);
1048 lustre_put_page(page);
1054 * We need to balance prepare_write() calls with commit_write() calls.
1055 * If the page has been prepared, but we have no data for it, we don't
1056 * want to overwrite valid data on disk, but we still need to zero out
1057 * data for space which was newly allocated. Like part of what happens
1058 * in __block_prepare_write() for newly allocated blocks.
1060 * XXX currently __block_prepare_write() creates buffers for all the
1061 * pages, and the filesystems mark these buffers as BH_New if they
1062 * were newly allocated from disk. We use the BH_New flag similarly.
1064 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1068 unsigned block_start, block_end;
1069 struct buffer_head *bh, *head = page->buffers;
1070 unsigned blocksize = head->b_size;
1071 void *addr = page_address(page);
1073 /* Currently one buffer per page, but in the future... */
1074 for (bh = head, block_start = 0; bh != head || !block_start;
1075 block_start = block_end, bh = bh->b_this_page) {
1076 block_end = block_start + blocksize;
1078 memset(addr + block_start, 0, blocksize);
1082 return lustre_commit_write(page, from, to);
1085 static int filter_preprw(int cmd, struct lustre_handle *conn,
1086 int objcount, struct obd_ioobj *obj,
1087 int niocount, struct niobuf_remote *nb,
1088 struct niobuf_local *res, void **desc_private)
1090 struct obd_run_ctxt saved;
1091 struct obd_device *obd;
1092 struct obd_ioobj *o = obj;
1093 struct niobuf_remote *b = nb;
1094 struct niobuf_local *r = res;
1095 void *journal_save = NULL;
1100 memset(res, 0, sizeof(*res) * niocount);
1101 obd = class_conn2obd(conn);
1103 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1105 if (cmd & OBD_BRW_WRITE) {
1106 *desc_private = filter_journal_start(&journal_save,
1108 objcount, obj, niocount,
1110 if (IS_ERR(*desc_private))
1111 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1114 for (i = 0; i < objcount; i++, o++) {
1115 struct dentry *dentry;
1116 struct inode *inode;
1119 dentry = filter_fid2dentry(obd,
1120 filter_parent(obd, S_IFREG),
1121 o->ioo_id, S_IFREG);
1123 GOTO(out_clean, rc = PTR_ERR(dentry));
1124 inode = dentry->d_inode;
1126 CERROR("trying to BRW to non-existent file %Ld\n",
1127 (unsigned long long)o->ioo_id);
1129 GOTO(out_clean, rc = -ENOENT);
1132 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1133 unsigned long index = b->offset >> PAGE_SHIFT;
1139 r->dentry = dget(dentry);
1141 if (cmd & OBD_BRW_WRITE)
1142 page = filter_get_page_write(inode, index, r);
1144 page = lustre_get_page_read(inode, index);
1148 GOTO(out_clean, rc = PTR_ERR(page));
1151 r->addr = page_address(page);
1152 r->offset = b->offset;
1159 if (cmd & OBD_BRW_WRITE) {
1160 int err = filter_journal_stop(journal_save, &obd->u.filter,
1171 if (cmd & OBD_BRW_WRITE)
1172 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1174 lustre_put_page(r->page);
1179 static int filter_write_locked_page(struct niobuf_local *lnb)
1184 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1185 if (IS_ERR(lpage)) {
1186 /* It is highly unlikely that we would ever get an error here.
1187 * The page we want to get was previously locked, so it had to
1188 * have already allocated the space, and we were just writing
1189 * over the same data, so there would be no hole in the file.
1191 * XXX: possibility of a race with truncate could exist, need
1192 * to check that. There are no guarantees w.r.t.
1193 * write order even on a local filesystem, although the
1194 * normal response would be to return the number of bytes
1195 * successfully written and leave the rest to the app.
1197 rc = PTR_ERR(lpage);
1198 CERROR("error getting locked page index %ld: rc = %d\n",
1199 lnb->page->index, rc);
1203 memcpy(page_address(lpage), kmap(lnb->page), PAGE_SIZE);
1204 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1206 CERROR("error committing locked page %ld: rc = %d\n",
1207 lnb->page->index, rc);
1210 __free_pages(lnb->page, 0);
1216 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1217 int objcount, struct obd_ioobj *obj,
1218 int niocount, struct niobuf_local *res,
1221 struct obd_run_ctxt saved;
1222 struct obd_ioobj *o = obj;
1223 struct niobuf_local *r = res;
1224 struct obd_device *obd = class_conn2obd(conn);
1226 int found_locked = 0;
1231 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1232 journal_save = current->journal_info;
1234 CERROR("Existing handle %p???\n", journal_save);
1235 current->journal_info = private;
1236 for (i = 0; i < objcount; i++, obj++) {
1238 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1239 struct page *page = r->page;
1244 if (r->flags & N_LOCAL_TEMP_PAGE) {
1249 if (cmd & OBD_BRW_WRITE) {
1250 int err = filter_commit_write(page, 0,
1256 lustre_put_page(page);
1259 "put inode %p (%ld), count = %d, nlink = %d\n",
1260 page->mapping->host,
1261 page->mapping->host->i_ino,
1262 atomic_read(&page->mapping->host->i_count) - 1,
1263 page->mapping->host->i_nlink);
1270 for (i = 0; i < objcount; i++, obj++) {
1272 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1274 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1277 err = filter_write_locked_page(r);
1284 current->journal_info = journal_save;
1289 static int filter_statfs(struct lustre_handle *conn, struct statfs *statfs)
1291 struct obd_device *obd = class_conn2obd(conn);
1294 RETURN(vfs_statfs(obd->u.filter.fo_sb, statfs));
1297 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1298 void *key, obd_count *vallen, void **val)
1300 struct obd_device *obd;
1301 struct obd_export * export;
1304 if (!(export = class_conn2export(conn))) {
1305 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
1309 obd = class_conn2obd(conn);
1311 if ( keylen == strlen("blocksize") &&
1312 memcmp(key, "blocksize", keylen) == 0 ) {
1313 *vallen = sizeof(long);
1314 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1318 if ( keylen == strlen("blocksize_bits") &&
1319 memcmp(key, "blocksize_bits", keylen) == 0 ){
1320 *vallen = sizeof(long);
1321 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1325 if ( keylen == strlen("root_ino") &&
1326 memcmp(key, "root_ino", keylen) == 0 ){
1327 *vallen = sizeof(obd_id);
1328 *val = (void *)(obd_id)FILTER_ROOTINO;
1332 CDEBUG(D_IOCTL, "invalid key\n");
1336 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1337 struct lustre_handle *src_conn, struct obdo *src,
1338 obd_size count, obd_off offset)
1341 struct lov_stripe_md srcmd, dstmd;
1342 unsigned long index = 0;
1345 memset(&srcmd, 0, sizeof(srcmd));
1346 memset(&dstmd, 0, sizeof(dstmd));
1347 srcmd.lmd_object_id = src->o_id;
1348 dstmd.lmd_object_id = dst->o_id;
1351 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
1352 (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
1353 (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
1354 page = alloc_page(GFP_USER);
1358 while (TryLockPage(page))
1359 ___wait_on_page(page);
1361 /* XXX with brw vector I/O, we could batch up reads and writes here,
1362 * all we need to do is allocate multiple pages to handle the I/Os
1363 * and arrays to handle the request parameters.
1365 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1366 obd_size brw_count = PAGE_SIZE;
1367 obd_off brw_offset = (page->index) << PAGE_SHIFT;
1369 obd_flag flagw = OBD_BRW_CREATE;
1371 page->index = index;
1372 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &page,
1373 &brw_count, &brw_offset, &flagr, NULL, NULL);
1379 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1381 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &page,
1382 &brw_count, &brw_offset, &flagw, NULL, NULL);
1384 /* XXX should handle dst->o_size, dst->o_blocks here */
1390 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1394 dst->o_size = src->o_size;
1395 dst->o_blocks = src->o_blocks;
1396 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1404 static struct obd_ops filter_obd_ops = {
1405 o_get_info: filter_get_info,
1406 o_setup: filter_setup,
1407 o_cleanup: filter_cleanup,
1408 o_connect: filter_connect,
1409 o_disconnect: filter_disconnect,
1410 o_statfs: filter_statfs,
1411 o_getattr: filter_getattr,
1412 o_create: filter_create,
1413 o_setattr: filter_setattr,
1414 o_destroy: filter_destroy,
1415 o_open: filter_open,
1416 o_close: filter_close,
1417 o_brw: filter_pgcache_brw,
1418 o_punch: filter_truncate,
1419 o_preprw: filter_preprw,
1420 o_commitrw: filter_commitrw
1422 o_preallocate: filter_preallocate_inodes,
1423 o_migrate: filter_migrate,
1424 o_copy: filter_copy_data,
1425 o_iterate: filter_iterate
1430 static int __init obdfilter_init(void)
1432 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1433 return class_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1436 static void __exit obdfilter_exit(void)
1438 class_unregister_type(OBD_FILTER_DEVICENAME);
1441 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1442 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1443 MODULE_LICENSE("GPL");
1445 module_init(obdfilter_init);
1446 module_exit(obdfilter_exit);