1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/obdfilter/filter.c
6 * Copyright (C) 2001, 2002 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>
12 * and Andreas Dilger <adilger@clusterfs.com>
16 #define DEBUG_SUBSYSTEM S_FILTER
18 #include <linux/module.h>
19 #include <linux/lustre_dlm.h>
20 #include <linux/obd_filter.h>
21 #include <linux/ext3_jbd.h>
22 #include <linux/quotaops.h>
23 #include <linux/init.h>
25 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
28 #define FILTER_ROOTINO 2
31 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
33 [S_IFREG >> S_SHIFT] "R",
34 [S_IFDIR >> S_SHIFT] "D",
35 [S_IFCHR >> S_SHIFT] "C",
36 [S_IFBLK >> S_SHIFT] "B",
37 [S_IFIFO >> S_SHIFT] "F",
38 [S_IFSOCK >> S_SHIFT] "S",
39 [S_IFLNK >> S_SHIFT] "L"
42 static inline const char *obd_mode_to_type(int mode)
44 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
47 /* write the pathname into the string */
48 static int filter_id(char *buf, obd_id id, obd_mode mode)
50 return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
51 (unsigned long long)id);
54 static inline void f_dput(struct dentry *dentry)
56 CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
57 dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
58 LASSERT(atomic_read(&dentry->d_count) > 0);
62 /* setup the object store with correct subdirectories */
63 static int filter_prep(struct obd_device *obddev)
65 struct obd_run_ctxt saved;
66 struct filter_obd *filter = &obddev->u.filter;
67 struct dentry *dentry;
75 push_ctxt(&saved, &filter->fo_ctxt);
76 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
77 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
80 CERROR("cannot open/create O: rc = %d\n", rc);
83 filter->fo_dentry_O = dentry;
84 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
85 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
88 CERROR("cannot open/create P: rc = %d\n", rc);
91 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
92 atomic_read(&dentry->d_count) - 1);
94 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
95 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
98 CERROR("cannot open/create D: rc = %d\n", rc);
101 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
102 atomic_read(&dentry->d_count) - 1);
106 * Create directories and/or get dentries for each object type.
107 * This saves us from having to do multiple lookups for each one.
109 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
110 char *type = obd_type_by_mode[mode];
113 filter->fo_dentry_O_mode[mode] = NULL;
116 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
117 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
118 if (IS_ERR(dentry)) {
119 rc = PTR_ERR(dentry);
120 CERROR("cannot create O/%s: rc = %d\n", type, rc);
121 GOTO(out_O_mode, rc);
123 filter->fo_dentry_O_mode[mode] = dentry;
126 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
127 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
130 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
132 GOTO(out_O_mode, rc);
136 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
137 if ( !file || IS_ERR(file) ) {
139 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
141 GOTO(out_O_mode, rc);
144 /* steal operations */
145 inode = file->f_dentry->d_inode;
146 filter->fo_fop = file->f_op;
147 filter->fo_iop = inode->i_op;
148 filter->fo_aops = inode->i_mapping->a_ops;
150 if (inode->i_size == 0) {
151 __u64 disk_lastino = cpu_to_le64(lastino);
152 ssize_t retval = file->f_op->write(file, (char *)&disk_lastino,
153 sizeof(disk_lastino),
155 if (retval != sizeof(disk_lastino)) {
156 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
158 GOTO(out_O_mode, rc = -EIO);
162 ssize_t retval = file->f_op->read(file, (char *)&disk_lastino,
163 sizeof(disk_lastino),
165 if (retval != sizeof(disk_lastino)) {
166 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
168 GOTO(out_O_mode, rc = -EIO);
170 lastino = le64_to_cpu(disk_lastino);
172 filter->fo_lastino = lastino;
183 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
186 filter->fo_dentry_O_mode[mode] = NULL;
190 f_dput(filter->fo_dentry_O);
191 filter->fo_dentry_O = NULL;
195 /* cleanup the filter: write last used object id to status file */
196 static void filter_post(struct obd_device *obddev)
198 struct obd_run_ctxt saved;
199 struct filter_obd *filter = &obddev->u.filter;
205 push_ctxt(&saved, &filter->fo_ctxt);
206 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
208 CERROR("OBD filter: cannot create status file\n");
213 disk_lastino = cpu_to_le64(filter->fo_lastino);
214 rc = file->f_op->write(file, (char *)&disk_lastino,
215 sizeof(disk_lastino), &file->f_pos);
216 if (rc != sizeof(disk_lastino))
217 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
219 rc = filp_close(file, NULL);
221 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
223 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
224 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
227 filter->fo_dentry_O_mode[mode] = NULL;
230 f_dput(filter->fo_dentry_O);
236 static __u64 filter_next_id(struct obd_device *obddev)
240 spin_lock(&obddev->u.filter.fo_lock);
241 id = ++obddev->u.filter.fo_lastino;
242 spin_unlock(&obddev->u.filter.fo_lock);
244 /* FIXME: write the lastino to disk here */
248 /* how to get files, dentries, inodes from object id's */
249 /* parent i_sem is already held if needed for exclusivity */
250 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
251 struct dentry *dparent,
252 __u64 id, __u32 type)
254 struct super_block *sb = obddev->u.filter.fo_sb;
255 struct dentry *dchild;
260 if (!sb || !sb->s_dev) {
261 CERROR("fatal: device not initialized.\n");
262 RETURN(ERR_PTR(-ENXIO));
266 CERROR("fatal: invalid object #0\n");
268 RETURN(ERR_PTR(-ESTALE));
271 if (!(type & S_IFMT)) {
272 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
273 (unsigned long long)id, type);
274 RETURN(ERR_PTR(-EINVAL));
277 len = sprintf(name, "%Ld", id);
278 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
280 dchild = lookup_one_len(name, dparent, len);
281 if (IS_ERR(dchild)) {
282 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
286 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
287 obd_mode_to_type(type), name, dchild,
288 atomic_read(&dchild->d_count));
290 LASSERT(atomic_read(&dchild->d_count) > 0);
295 static struct file *filter_obj_open(struct obd_device *obddev,
296 __u64 id, __u32 type)
298 struct super_block *sb = obddev->u.filter.fo_sb;
299 struct obd_run_ctxt saved;
304 if (!sb || !sb->s_dev) {
305 CERROR("fatal: device not initialized.\n");
306 RETURN(ERR_PTR(-ENXIO));
310 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
311 RETURN(ERR_PTR(-ESTALE));
314 if (!(type & S_IFMT)) {
315 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
316 (unsigned long long)id, type);
317 RETURN(ERR_PTR(-EINVAL));
320 filter_id(name, id, type);
321 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
322 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
325 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
332 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
334 struct filter_obd *filter = &obddev->u.filter;
336 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
341 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
347 rc = class_connect(conn, obd, cluuid);
353 static int filter_disconnect(struct lustre_handle *conn)
358 rc = class_disconnect(conn);
362 /* XXX cleanup preallocated inodes */
366 /* mount the file system (secretly) */
367 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
369 struct obd_ioctl_data* data = buf;
370 struct filter_obd *filter;
371 struct vfsmount *mnt;
375 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
379 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
384 filter = &obddev->u.filter;;
385 filter->fo_vfsmnt = mnt;
386 filter->fo_fstype = strdup(data->ioc_inlbuf2);
387 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
388 /* XXX is this even possible if do_kern_mount succeeded? */
390 GOTO(err_kfree, err = -ENODEV);
392 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
393 filter->fo_ctxt.pwdmnt = mnt;
394 filter->fo_ctxt.pwd = mnt->mnt_root;
395 filter->fo_ctxt.fs = get_ds();
397 err = filter_prep(obddev);
399 GOTO(err_kfree, err);
400 spin_lock_init(&filter->fo_lock);
402 obddev->obd_namespace =
403 ldlm_namespace_new("filter-tgt", LDLM_NAMESPACE_SERVER);
404 if (obddev->obd_namespace == NULL)
407 ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
408 "filter_ldlm_client", &obddev->obd_ldlm_client);
413 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 (!list_empty(&obddev->obd_exports)) {
431 CERROR("still has clients!\n");
432 class_disconnect_all(obddev);
433 if (!list_empty(&obddev->obd_exports)) {
434 CERROR("still has exports after forced cleanup?\n");
439 ldlm_namespace_free(obddev->obd_namespace);
441 sb = obddev->u.filter.fo_sb;
442 if (!obddev->u.filter.fo_sb)
447 shrink_dcache_parent(sb->s_root);
449 mntput(obddev->u.filter.fo_vfsmnt);
450 obddev->u.filter.fo_sb = 0;
451 kfree(obddev->u.filter.fo_fstype);
460 static inline void filter_from_inode(struct obdo *oa, struct inode *inode,
463 int type = oa->o_mode & S_IFMT;
466 CDEBUG(D_INFO, "src inode %ld (%p), dst obdo %ld valid 0x%08x\n",
467 inode->i_ino, inode, (long)oa->o_id, valid);
468 /* Don't copy the inode number in place of the object ID */
469 obdo_from_inode(oa, inode, valid);
470 oa->o_mode &= ~S_IFMT;
473 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
474 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
476 oa->o_valid |= OBD_MD_FLRDEV;
482 static int filter_getattr(struct lustre_handle *conn, struct obdo *oa,
483 struct lov_stripe_md *md)
485 struct obd_device *obddev = class_conn2obd(conn);
486 struct dentry *dentry;
489 if (!class_conn2export(conn)) {
490 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
494 obddev = class_conn2obd(conn);
495 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
496 oa->o_id, oa->o_mode);
498 RETURN(PTR_ERR(dentry));
500 filter_from_inode(oa, dentry->d_inode, oa->o_valid);
506 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
507 struct lov_stripe_md *md)
509 struct obd_run_ctxt saved;
510 struct obd_device *obd = class_conn2obd(conn);
511 struct dentry *dentry;
517 iattr_from_obdo(&iattr, oa, oa->o_valid);
518 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
519 dentry = filter_fid2dentry(obd, filter_parent(obd, iattr.ia_mode),
520 oa->o_id, iattr.ia_mode);
522 RETURN(PTR_ERR(dentry));
524 inode = dentry->d_inode;
526 CERROR("setattr on non-existent object: "LPU64"\n", oa->o_id);
527 GOTO(out_setattr, rc = -ENOENT);
531 if (iattr.ia_valid & ATTR_SIZE)
533 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
534 if (inode->i_op->setattr)
535 rc = inode->i_op->setattr(dentry, &iattr);
537 rc = inode_setattr(inode, &iattr);
539 if (iattr.ia_valid & ATTR_SIZE) {
541 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
542 obdo_from_inode(oa, inode, oa->o_valid);
551 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
552 struct lov_stripe_md *ea)
554 struct obd_export *export;
555 struct obd_device *obd;
556 struct dentry *dentry;
559 export = class_conn2export(conn);
561 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
565 obd = export->exp_obd;
566 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
567 oa->o_id, oa->o_mode);
569 RETURN(PTR_ERR(dentry));
571 filter_from_inode(oa, dentry->d_inode, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
572 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
577 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
578 struct lov_stripe_md *ea)
580 struct obd_device *obd;
581 struct dentry *dentry;
584 obd = class_conn2obd(conn);
586 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
590 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
591 oa->o_id, oa->o_mode);
593 RETURN(PTR_ERR(dentry));
594 LASSERT(atomic_read(&dentry->d_count) > 1);
596 f_dput(dentry); /* for the close */
597 f_dput(dentry); /* for this call */
601 static int filter_create(struct lustre_handle* conn, struct obdo *oa,
602 struct lov_stripe_md **ea)
605 struct obd_run_ctxt saved;
608 struct obd_device *obd = class_conn2obd(conn);
613 CERROR("invalid client "LPX64"\n", conn->addr);
617 if (!(oa->o_mode && S_IFMT)) {
618 CERROR("filter obd: no type!\n");
622 oa->o_id = filter_next_id(obd);
624 filter_id(name, oa->o_id, oa->o_mode);
625 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
626 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
627 file = filp_open(name, O_RDONLY | O_CREAT, mode);
630 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
634 /* Set flags for fields we have set in the inode struct */
635 oa->o_valid = OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
636 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME;
637 filter_from_inode(oa, file->f_dentry->d_inode, oa->o_valid);
643 static int filter_destroy(struct lustre_handle *conn, struct obdo *oa,
644 struct lov_stripe_md *ea)
646 struct obd_device *obd;
647 struct filter_obd *filter;
648 struct obd_run_ctxt saved;
650 struct dentry *dir_dentry, *object_dentry;
654 obd = class_conn2obd(conn);
656 CERROR("invalid client "LPX64"\n", conn->addr);
660 CDEBUG(D_INODE, "destroying object "LPD64"\n", oa->o_id);
662 dir_dentry = filter_parent(obd, oa->o_mode);
663 down(&dir_dentry->d_inode->i_sem);
665 object_dentry = filter_fid2dentry(obd, dir_dentry, oa->o_id,
667 if (IS_ERR(object_dentry))
668 GOTO(out, rc = -ENOENT);
670 inode = object_dentry->d_inode;
672 CERROR("trying to destroy negative inode "LPD64"!\n", oa->o_id);
673 GOTO(out, rc = -ENOENT);
676 if (inode->i_nlink != 1) {
677 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
681 inode->i_mode = S_IFREG;
683 filter = &obd->u.filter;
684 push_ctxt(&saved, &filter->fo_ctxt);
686 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
688 f_dput(object_dentry);
692 up(&dir_dentry->d_inode->i_sem);
696 /* NB count and offset are used for punch, but not truncate */
697 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
698 struct lov_stripe_md *md,
699 obd_off start, obd_off end)
704 if (end != 0xffffffffffffffff)
705 CERROR("PUNCH not supported, only truncate works\n");
707 CDEBUG(D_INODE, "calling truncate for object #"LPD64", valid = %x, "
708 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
710 error = filter_setattr(conn, oa, NULL);
714 static int filter_pgcache_brw(int cmd, struct lustre_handle *conn,
715 struct lov_stripe_md *md, obd_count oa_bufs,
716 struct brw_page *pga, brw_callback_t callback,
717 struct io_cb_data *data)
719 struct obd_run_ctxt saved;
720 struct super_block *sb;
721 int pnum; /* index to pages (bufs) */
722 unsigned long retval;
725 struct obd_device *obd = class_conn2obd(conn);
730 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
734 sb = obd->u.filter.fo_sb;
735 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
736 pnum = 0; /* pnum indexes buf 0..num_pages */
738 file = filter_obj_open(obd, md->lmd_object_id, S_IFREG);
740 GOTO(out, retval = PTR_ERR(file));
742 /* count doubles as retval */
743 for (pg = 0; pg < oa_bufs; pg++) {
744 CDEBUG(D_INODE, "OP %d obdo pgno: (%d) (%ld,"LPU64
745 ") off count ("LPU64",%d)\n",
746 cmd, pnum, file->f_dentry->d_inode->i_ino,
747 pga[pnum].off >> PAGE_CACHE_SHIFT, pga[pnum].off,
748 (int)pga[pnum].count);
749 if (cmd & OBD_BRW_WRITE) {
753 buffer = kmap(pga[pnum].pg);
754 retval = file->f_op->write(file, buffer,
757 kunmap(pga[pnum].pg);
758 CDEBUG(D_INODE, "retval %ld\n", retval);
760 loff_t off = pga[pnum].off;
761 char *buffer = kmap(pga[pnum].pg);
763 if (off >= file->f_dentry->d_inode->i_size) {
764 memset(buffer, 0, pga[pnum].count);
765 retval = pga[pnum].count;
767 retval = file->f_op->read(file, buffer,
768 pga[pnum].count, &off);
770 kunmap(pga[pnum].pg);
772 if (retval != pga[pnum].count) {
774 GOTO(out, retval = -EIO);
776 CDEBUG(D_INODE, "retval %ld\n", retval);
780 /* sizes and blocks are set by generic_file_write */
781 /* ctimes/mtimes will follow with a setattr call */
784 /* XXX: do something with callback if it is set? */
789 error = (retval >= 0) ? 0 : retval;
794 * Calculate the number of buffer credits needed to write multiple pages in
795 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
796 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
798 * See comment above ext3_writepage_trans_blocks for details. We assume
799 * no data journaling is being done, but it does allow for all of the pages
800 * being non-contiguous. If we are guaranteed contiguous pages we could
801 * reduce the number of (d)indirect blocks a lot.
803 * With N blocks per page and P pages, for each inode we have at most:
805 * min(N*P, blocksize/4 + 1) dindirect blocks
808 * For the entire filesystem, we have at most:
809 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
810 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
813 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
815 static int ext3_credits_needed(struct super_block *sb, int objcount,
816 struct obd_ioobj *obj)
818 struct obd_ioobj *o = obj;
819 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
820 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
823 int needed = objcount + 1;
826 for (i = 0; i < objcount; i++, o++) {
827 int nblocks = o->ioo_bufcnt * blockpp;
828 int ndindirect = min(nblocks, addrpp + 1);
829 int nindir = nblocks + ndindirect + 1;
831 nbitmaps += nindir + nblocks;
832 ngdblocks += nindir + nblocks;
837 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
838 nbitmaps = EXT3_SB(sb)->s_groups_count;
839 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
840 ngdblocks = EXT3_SB(sb)->s_gdb_count;
842 needed += nbitmaps + ngdblocks;
845 /* We assume that there will be 1 bit set in s_dquot.flags for each
846 * quota file that is active. This is at least true for now.
848 needed += hweight32(sb_any_quota_enabled(sb)) *
849 EXT3_SINGLEDATA_TRANS_BLOCKS;
855 /* We have to start a huge journal transaction here to hold all of the
856 * metadata for the pages being written here. This is necessitated by
857 * the fact that we do lots of prepare_write operations before we do
858 * any of the matching commit_write operations, so even if we split
859 * up to use "smaller" transactions none of them could complete until
860 * all of them were opened. By having a single journal transaction,
861 * we eliminate duplicate reservations for common blocks like the
862 * superblock and group descriptors or bitmaps.
864 * We will start the transaction here, but each prepare_write will
865 * add a refcount to the transaction, and each commit_write will
866 * remove a refcount. The transaction will be closed when all of
867 * the pages have been written.
869 static void *ext3_filter_journal_start(struct filter_obd *filter,
870 int objcount, struct obd_ioobj *obj,
871 int niocount, struct niobuf_remote *nb)
873 journal_t *journal = NULL;
874 handle_t *handle = NULL;
877 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
878 * with having extN built properly before filterobd for now.
880 journal = EXT3_SB(filter->fo_sb)->s_journal;
881 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
883 /* The number of blocks we could _possibly_ dirty can very large.
884 * We reduce our request if it is absurd (and we couldn't get that
885 * many credits for a single handle anyways).
887 * At some point we have to limit the size of I/Os sent at one time,
888 * increase the size of the journal, or we have to calculate the
889 * actual journal requirements more carefully by checking all of
890 * the blocks instead of being maximally pessimistic. It remains to
891 * be seen if this is a real problem or not.
893 if (needed > journal->j_max_transaction_buffers) {
894 CERROR("want too many journal credits (%d) using %d instead\n",
895 needed, journal->j_max_transaction_buffers);
896 needed = journal->j_max_transaction_buffers;
899 handle = journal_start(journal, needed);
901 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
907 static void *filter_journal_start(void **journal_save,
908 struct filter_obd *filter,
909 int objcount, struct obd_ioobj *obj,
910 int niocount, struct niobuf_remote *nb)
914 /* This may not be necessary - we probably never have a
915 * transaction started when we enter here, so we can
916 * remove the saving of the journal state entirely.
917 * For now leave it in just to see if it ever happens.
919 *journal_save = current->journal_info;
921 CERROR("Already have handle %p???\n", *journal_save);
923 current->journal_info = NULL;
926 if (!strcmp(filter->fo_fstype, "ext3") ||
927 !strcmp(filter->fo_fstype, "extN"))
928 handle = ext3_filter_journal_start(filter, objcount, obj,
933 static int ext3_filter_journal_stop(void *handle)
937 /* We got a refcount on the handle for each call to prepare_write,
938 * so we can drop the "parent" handle here to avoid the need for
939 * osc to call back into filterobd to close the handle. The
940 * remaining references will be dropped in commit_write.
942 rc = journal_stop((handle_t *)handle);
947 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
952 if (!strcmp(filter->fo_fstype, "ext3") ||
953 !strcmp(filter->fo_fstype, "extN"))
954 rc = ext3_filter_journal_stop(handle);
957 CERROR("error on journal stop: rc = %d\n", rc);
959 current->journal_info = journal_save;
964 static inline void lustre_put_page(struct page *page)
967 page_cache_release(page);
971 lustre_get_page_read(struct inode *inode, unsigned long index)
973 struct address_space *mapping = inode->i_mapping;
977 page = read_cache_page(mapping, index,
978 (filler_t*)mapping->a_ops->readpage, NULL);
982 if (!Page_Uptodate(page)) {
983 CERROR("page index %lu not uptodate\n", index);
984 GOTO(err_page, rc = -EIO);
986 if (PageError(page)) {
987 CERROR("page index %lu has error\n", index);
988 GOTO(err_page, rc = -EIO);
994 lustre_put_page(page);
999 lustre_get_page_write(struct inode *inode, unsigned long index)
1001 struct address_space *mapping = inode->i_mapping;
1005 page = grab_cache_page(mapping, index); /* locked page */
1007 if (!IS_ERR(page)) {
1009 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1010 * a no-op for most filesystems, because we write the whole
1011 * page. For partial-page I/O this will read in the page.
1013 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1015 CERROR("page index %lu, rc = %d\n", index, rc);
1018 GOTO(err_unlock, rc);
1020 /* XXX not sure if we need this if we are overwriting page */
1021 if (PageError(page)) {
1022 CERROR("error on page index %lu, rc = %d\n", index, rc);
1024 GOTO(err_unlock, rc = -EIO);
1031 lustre_put_page(page);
1035 static int lustre_commit_write(struct page *page, unsigned from, unsigned to)
1037 struct inode *inode = page->mapping->host;
1040 err = page->mapping->a_ops->commit_write(NULL, page, from, to);
1041 if (!err && IS_SYNC(inode))
1042 err = waitfor_one_page(page);
1044 //SetPageUptodate(page); // the client commit_write will do this
1046 SetPageReferenced(page);
1048 lustre_put_page(page);
1052 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1053 struct niobuf_local *lnb, int *pglocked)
1055 struct address_space *mapping = inode->i_mapping;
1059 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1061 page = grab_cache_page_nowait(mapping, index); /* locked page */
1063 page = grab_cache_page(mapping, index); /* locked page */
1066 /* This page is currently locked, so get a temporary page instead. */
1067 /* XXX I believe this is a very dangerous thing to do - consider if
1068 * we had multiple writers for the same file (definitely the case
1069 * if we are using this codepath). If writer A locks the page,
1070 * writer B writes to a copy (as here), writer A drops the page
1071 * lock, and writer C grabs the lock before B does, then B will
1072 * later overwrite the data from C, even if C had LDLM locked
1073 * and initiated the write after B did.
1077 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
1078 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1080 CERROR("no memory for a temp page\n");
1082 GOTO(err, rc = -ENOMEM);
1084 page = virt_to_page(addr);
1086 page->index = index;
1087 lnb->flags |= N_LOCAL_TEMP_PAGE;
1088 } else if (!IS_ERR(page)) {
1092 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1093 * a no-op for most filesystems, because we write the whole
1094 * page. For partial-page I/O this will read in the page.
1096 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1098 CERROR("page index %lu, rc = %d\n", index, rc);
1101 GOTO(err_unlock, rc);
1103 /* XXX not sure if we need this if we are overwriting page */
1104 if (PageError(page)) {
1105 CERROR("error on page index %lu, rc = %d\n", index, rc);
1107 GOTO(err_unlock, rc = -EIO);
1114 lustre_put_page(page);
1120 * We need to balance prepare_write() calls with commit_write() calls.
1121 * If the page has been prepared, but we have no data for it, we don't
1122 * want to overwrite valid data on disk, but we still need to zero out
1123 * data for space which was newly allocated. Like part of what happens
1124 * in __block_prepare_write() for newly allocated blocks.
1126 * XXX currently __block_prepare_write() creates buffers for all the
1127 * pages, and the filesystems mark these buffers as BH_New if they
1128 * were newly allocated from disk. We use the BH_New flag similarly.
1130 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1134 unsigned block_start, block_end;
1135 struct buffer_head *bh, *head = page->buffers;
1136 unsigned blocksize = head->b_size;
1137 void *addr = page_address(page);
1139 /* debugging: just seeing if this ever happens */
1140 CERROR("called filter_commit_write for obj %ld:%ld on err %d\n",
1141 page->index, page->mapping->host->i_ino, err);
1143 /* Currently one buffer per page, but in the future... */
1144 for (bh = head, block_start = 0; bh != head || !block_start;
1145 block_start = block_end, bh = bh->b_this_page) {
1146 block_end = block_start + blocksize;
1148 memset(addr + block_start, 0, blocksize);
1152 return lustre_commit_write(page, from, to);
1155 static int filter_preprw(int cmd, struct lustre_handle *conn,
1156 int objcount, struct obd_ioobj *obj,
1157 int niocount, struct niobuf_remote *nb,
1158 struct niobuf_local *res, void **desc_private)
1160 struct obd_run_ctxt saved;
1161 struct obd_device *obd;
1162 struct obd_ioobj *o = obj;
1163 struct niobuf_remote *b = nb;
1164 struct niobuf_local *r = res;
1165 void *journal_save = NULL;
1171 obd = class_conn2obd(conn);
1173 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1176 memset(res, 0, sizeof(*res) * niocount);
1178 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1180 if (cmd & OBD_BRW_WRITE) {
1181 *desc_private = filter_journal_start(&journal_save,
1183 objcount, obj, niocount,
1185 if (IS_ERR(*desc_private))
1186 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1189 for (i = 0; i < objcount; i++, o++) {
1190 struct dentry *dentry;
1191 struct inode *inode;
1194 dentry = filter_fid2dentry(obd, filter_parent(obd, S_IFREG),
1195 o->ioo_id, S_IFREG);
1197 GOTO(out_clean, rc = PTR_ERR(dentry));
1198 inode = dentry->d_inode;
1200 CERROR("trying to BRW to non-existent file %Ld\n",
1201 (unsigned long long)o->ioo_id);
1203 GOTO(out_clean, rc = -ENOENT);
1206 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1207 unsigned long index = b->offset >> PAGE_SHIFT;
1213 r->dentry = dget(dentry);
1215 if (cmd & OBD_BRW_WRITE)
1216 page = filter_get_page_write(inode, index, r,
1219 page = lustre_get_page_read(inode, index);
1223 GOTO(out_clean, rc = PTR_ERR(page));
1226 r->addr = page_address(page);
1227 r->offset = b->offset;
1234 if (cmd & OBD_BRW_WRITE) {
1235 int err = filter_journal_stop(journal_save, &obd->u.filter,
1245 CERROR("error cleanup on brw\n");
1247 if (cmd & OBD_BRW_WRITE)
1248 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1250 lustre_put_page(r->page);
1255 static int filter_write_locked_page(struct niobuf_local *lnb)
1260 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1261 if (IS_ERR(lpage)) {
1262 /* It is highly unlikely that we would ever get an error here.
1263 * The page we want to get was previously locked, so it had to
1264 * have already allocated the space, and we were just writing
1265 * over the same data, so there would be no hole in the file.
1267 * XXX: possibility of a race with truncate could exist, need
1268 * to check that. There are no guarantees w.r.t.
1269 * write order even on a local filesystem, although the
1270 * normal response would be to return the number of bytes
1271 * successfully written and leave the rest to the app.
1273 rc = PTR_ERR(lpage);
1274 CERROR("error getting locked page index %ld: rc = %d\n",
1275 lnb->page->index, rc);
1279 /* lpage is kmapped in lustre_get_page_write() above and kunmapped in
1280 * lustre_commit_write() below, lnb->page was kmapped previously in
1281 * filter_get_page_write() and kunmapped in lustre_put_page() below.
1283 memcpy(page_address(lpage), page_address(lnb->page), PAGE_SIZE);
1284 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1286 CERROR("error committing locked page %ld: rc = %d\n",
1287 lnb->page->index, rc);
1289 lustre_put_page(lnb->page);
1294 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1295 int objcount, struct obd_ioobj *obj,
1296 int niocount, struct niobuf_local *res,
1299 struct obd_run_ctxt saved;
1300 struct obd_ioobj *o;
1301 struct niobuf_local *r;
1302 struct obd_device *obd = class_conn2obd(conn);
1304 int found_locked = 0;
1309 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1310 journal_save = current->journal_info;
1311 LASSERT(!journal_save);
1313 current->journal_info = private;
1314 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1316 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1317 struct page *page = r->page;
1322 if (r->flags & N_LOCAL_TEMP_PAGE) {
1327 if (cmd & OBD_BRW_WRITE) {
1328 int err = filter_commit_write(page, 0,
1334 lustre_put_page(page);
1339 current->journal_info = journal_save;
1344 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1346 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1348 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1351 err = filter_write_locked_page(r);
1363 static int filter_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
1365 struct obd_device *obd = class_conn2obd(conn);
1370 rc = vfs_statfs(obd->u.filter.fo_sb, &sfs);
1372 statfs_pack(osfs, &sfs);
1377 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1378 void *key, obd_count *vallen, void **val)
1380 struct obd_device *obd;
1383 obd = class_conn2obd(conn);
1385 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1389 if ( keylen == strlen("blocksize") &&
1390 memcmp(key, "blocksize", keylen) == 0 ) {
1391 *vallen = sizeof(long);
1392 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1396 if ( keylen == strlen("blocksize_bits") &&
1397 memcmp(key, "blocksize_bits", keylen) == 0 ){
1398 *vallen = sizeof(long);
1399 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1403 if ( keylen == strlen("root_ino") &&
1404 memcmp(key, "root_ino", keylen) == 0 ){
1405 *vallen = sizeof(obd_id);
1406 *val = (void *)(obd_id)FILTER_ROOTINO;
1410 CDEBUG(D_IOCTL, "invalid key\n");
1414 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1415 struct lustre_handle *src_conn, struct obdo *src,
1416 obd_size count, obd_off offset)
1419 struct lov_stripe_md srcmd, dstmd;
1420 unsigned long index = 0;
1423 memset(&srcmd, 0, sizeof(srcmd));
1424 memset(&dstmd, 0, sizeof(dstmd));
1425 srcmd.lmd_object_id = src->o_id;
1426 dstmd.lmd_object_id = dst->o_id;
1429 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
1430 (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
1431 (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
1432 page = alloc_page(GFP_USER);
1436 while (TryLockPage(page))
1437 ___wait_on_page(page);
1439 /* XXX with brw vector I/O, we could batch up reads and writes here,
1440 * all we need to do is allocate multiple pages to handle the I/Os
1441 * and arrays to handle the request parameters.
1443 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1445 struct io_cb_data *cbd = ll_init_cb();
1454 pg.count = PAGE_SIZE;
1455 pg.off = (page->index) << PAGE_SHIFT;
1458 page->index = index;
1459 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &pg,
1460 ll_sync_io_cb, cbd);
1473 pg.flag = OBD_BRW_CREATE;
1474 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1476 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &pg,
1477 ll_sync_io_cb, cbd);
1479 /* XXX should handle dst->o_size, dst->o_blocks here */
1485 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1489 dst->o_size = src->o_size;
1490 dst->o_blocks = src->o_blocks;
1491 dst->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1499 static struct obd_ops filter_obd_ops = {
1500 o_get_info: filter_get_info,
1501 o_setup: filter_setup,
1502 o_cleanup: filter_cleanup,
1503 o_connect: filter_connect,
1504 o_disconnect: filter_disconnect,
1505 o_statfs: filter_statfs,
1506 o_getattr: filter_getattr,
1507 o_create: filter_create,
1508 o_setattr: filter_setattr,
1509 o_destroy: filter_destroy,
1510 o_open: filter_open,
1511 o_close: filter_close,
1512 o_brw: filter_pgcache_brw,
1513 o_punch: filter_truncate,
1514 o_preprw: filter_preprw,
1515 o_commitrw: filter_commitrw
1517 o_preallocate: filter_preallocate_inodes,
1518 o_migrate: filter_migrate,
1519 o_copy: filter_copy_data,
1520 o_iterate: filter_iterate
1525 static int __init obdfilter_init(void)
1527 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1528 return class_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1531 static void __exit obdfilter_exit(void)
1533 class_unregister_type(OBD_FILTER_DEVICENAME);
1536 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1537 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1538 MODULE_LICENSE("GPL");
1540 module_init(obdfilter_init);
1541 module_exit(obdfilter_exit);