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/pagemap.h>
20 #include <linux/lustre_dlm.h>
21 #include <linux/obd_filter.h>
22 #include <linux/ext3_jbd.h>
23 #include <linux/quotaops.h>
24 #include <linux/init.h>
28 #define FILTER_ROOTINO 2
29 #define FILTER_ROOTINO_STR __stringify(FILTER_ROOTINO)
32 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
34 [S_IFREG >> S_SHIFT] "R",
35 [S_IFDIR >> S_SHIFT] "D",
36 [S_IFCHR >> S_SHIFT] "C",
37 [S_IFBLK >> S_SHIFT] "B",
38 [S_IFIFO >> S_SHIFT] "F",
39 [S_IFSOCK >> S_SHIFT] "S",
40 [S_IFLNK >> S_SHIFT] "L"
43 static inline const char *obd_mode_to_type(int mode)
45 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
48 /* write the pathname into the string */
49 static int filter_id(char *buf, obd_id id, obd_mode mode)
51 return sprintf(buf, "O/%s/"LPU64, obd_mode_to_type(mode), 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);
92 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
93 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
96 CERROR("cannot open/create D: rc = %d\n", rc);
100 root = simple_mknod(dentry, FILTER_ROOTINO_STR, S_IFREG | 0755);
104 CERROR("OBD filter: cannot open/create root %d: rc = %d\n",
111 * Create directories and/or get dentries for each object type.
112 * This saves us from having to do multiple lookups for each one.
114 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
115 char *type = obd_type_by_mode[mode];
118 filter->fo_dentry_O_mode[mode] = NULL;
121 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
122 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
123 if (IS_ERR(dentry)) {
124 rc = PTR_ERR(dentry);
125 CERROR("cannot create O/%s: rc = %d\n", type, rc);
126 GOTO(out_O_mode, rc);
128 filter->fo_dentry_O_mode[mode] = dentry;
131 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
132 if ( !file || IS_ERR(file) ) {
134 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
136 GOTO(out_O_mode, rc);
139 /* steal operations */
140 inode = file->f_dentry->d_inode;
141 filter->fo_fop = file->f_op;
142 filter->fo_iop = inode->i_op;
143 filter->fo_aops = inode->i_mapping->a_ops;
145 if (inode->i_size == 0) {
146 __u64 disk_lastino = cpu_to_le64(lastino);
147 ssize_t retval = file->f_op->write(file, (char *)&disk_lastino,
148 sizeof(disk_lastino),
150 if (retval != sizeof(disk_lastino)) {
151 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
153 GOTO(out_O_mode, rc = -EIO);
157 ssize_t retval = file->f_op->read(file, (char *)&disk_lastino,
158 sizeof(disk_lastino),
160 if (retval != sizeof(disk_lastino)) {
161 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
163 GOTO(out_O_mode, rc = -EIO);
165 lastino = le64_to_cpu(disk_lastino);
167 filter->fo_lastino = lastino;
178 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
181 filter->fo_dentry_O_mode[mode] = NULL;
185 f_dput(filter->fo_dentry_O);
186 filter->fo_dentry_O = NULL;
190 /* cleanup the filter: write last used object id to status file */
191 static void filter_post(struct obd_device *obddev)
193 struct obd_run_ctxt saved;
194 struct filter_obd *filter = &obddev->u.filter;
200 push_ctxt(&saved, &filter->fo_ctxt);
201 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
203 CERROR("OBD filter: cannot create status file\n");
208 disk_lastino = cpu_to_le64(filter->fo_lastino);
209 rc = file->f_op->write(file, (char *)&disk_lastino,
210 sizeof(disk_lastino), &file->f_pos);
211 if (rc != sizeof(disk_lastino))
212 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
214 rc = filp_close(file, NULL);
216 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
218 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
219 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
222 filter->fo_dentry_O_mode[mode] = NULL;
225 f_dput(filter->fo_dentry_O);
231 static __u64 filter_next_id(struct obd_device *obddev)
235 spin_lock(&obddev->u.filter.fo_lock);
236 id = ++obddev->u.filter.fo_lastino;
237 spin_unlock(&obddev->u.filter.fo_lock);
239 /* FIXME: write the lastino to disk here */
243 /* how to get files, dentries, inodes from object id's */
244 /* parent i_sem is already held if needed for exclusivity */
245 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
246 struct dentry *dparent,
247 __u64 id, __u32 type)
249 struct super_block *sb = obddev->u.filter.fo_sb;
250 struct dentry *dchild;
255 if (!sb || !sb->s_dev) {
256 CERROR("fatal: device not initialized.\n");
257 RETURN(ERR_PTR(-ENXIO));
261 CERROR("fatal: invalid object #0\n");
263 RETURN(ERR_PTR(-ESTALE));
266 if (!(type & S_IFMT)) {
267 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
268 __FUNCTION__, id, type);
269 RETURN(ERR_PTR(-EINVAL));
272 len = sprintf(name, LPU64, id);
273 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
275 dchild = lookup_one_len(name, dparent, len);
276 if (IS_ERR(dchild)) {
277 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
281 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
282 obd_mode_to_type(type), name, dchild,
283 atomic_read(&dchild->d_count));
285 LASSERT(atomic_read(&dchild->d_count) > 0);
290 static struct file *filter_obj_open(struct obd_device *obddev,
291 __u64 id, __u32 type)
293 struct super_block *sb = obddev->u.filter.fo_sb;
294 struct obd_run_ctxt saved;
299 if (!sb || !sb->s_dev) {
300 CERROR("fatal: device not initialized.\n");
301 RETURN(ERR_PTR(-ENXIO));
305 CERROR("fatal: invalid obdo "LPU64"\n", id);
306 RETURN(ERR_PTR(-ESTALE));
309 if (!(type & S_IFMT)) {
310 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
311 __FUNCTION__, id, type);
312 RETURN(ERR_PTR(-EINVAL));
315 filter_id(name, id, type);
316 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
317 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
320 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
327 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
329 struct filter_obd *filter = &obddev->u.filter;
331 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
336 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
342 rc = class_connect(conn, obd, cluuid);
348 static int filter_disconnect(struct lustre_handle *conn)
353 rc = class_disconnect(conn);
357 /* XXX cleanup preallocated inodes */
361 /* mount the file system (secretly) */
362 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
364 struct obd_ioctl_data* data = buf;
365 struct filter_obd *filter;
366 struct vfsmount *mnt;
370 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
374 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
379 filter = &obddev->u.filter;;
380 filter->fo_vfsmnt = mnt;
381 filter->fo_fstype = strdup(data->ioc_inlbuf2);
382 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
383 /* XXX is this even possible if do_kern_mount succeeded? */
385 GOTO(err_kfree, err = -ENODEV);
387 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
388 filter->fo_ctxt.pwdmnt = mnt;
389 filter->fo_ctxt.pwd = mnt->mnt_root;
390 filter->fo_ctxt.fs = get_ds();
392 err = filter_prep(obddev);
394 GOTO(err_kfree, err);
395 spin_lock_init(&filter->fo_lock);
397 obddev->obd_namespace =
398 ldlm_namespace_new("filter-tgt", LDLM_NAMESPACE_SERVER);
399 if (obddev->obd_namespace == NULL)
402 ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
403 "filter_ldlm_client", &obddev->obd_ldlm_client);
408 kfree(filter->fo_fstype);
410 mntput(filter->fo_vfsmnt);
420 static int filter_cleanup(struct obd_device * obddev)
422 struct super_block *sb;
425 if (!list_empty(&obddev->obd_exports)) {
426 CERROR("still has clients!\n");
427 class_disconnect_all(obddev);
428 if (!list_empty(&obddev->obd_exports)) {
429 CERROR("still has exports after forced cleanup?\n");
434 ldlm_namespace_free(obddev->obd_namespace);
436 sb = obddev->u.filter.fo_sb;
437 if (!obddev->u.filter.fo_sb)
442 shrink_dcache_parent(sb->s_root);
444 mntput(obddev->u.filter.fo_vfsmnt);
445 obddev->u.filter.fo_sb = 0;
446 kfree(obddev->u.filter.fo_fstype);
455 static inline void filter_from_inode(struct obdo *oa, struct inode *inode,
458 int type = oa->o_mode & S_IFMT;
461 CDEBUG(D_INFO, "src inode %ld (%p), dst obdo %ld valid 0x%08x\n",
462 inode->i_ino, inode, (long)oa->o_id, valid);
463 /* Don't copy the inode number in place of the object ID */
464 obdo_from_inode(oa, inode, valid);
465 oa->o_mode &= ~S_IFMT;
468 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
469 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
471 oa->o_valid |= OBD_MD_FLRDEV;
477 static int filter_getattr(struct lustre_handle *conn, struct obdo *oa,
478 struct lov_stripe_md *md)
480 struct obd_device *obddev = class_conn2obd(conn);
481 struct dentry *dentry;
485 if (!class_conn2export(conn)) {
486 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
490 obddev = class_conn2obd(conn);
491 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
492 oa->o_id, oa->o_mode);
494 RETURN(PTR_ERR(dentry));
496 if (!dentry->d_inode) {
497 CERROR("getattr on non-existent object: "LPU64"\n", oa->o_id);
498 GOTO(out_getattr, rc = -ENOENT);
501 filter_from_inode(oa, dentry->d_inode, oa->o_valid);
508 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
509 struct lov_stripe_md *md)
511 struct obd_run_ctxt saved;
512 struct obd_device *obd = class_conn2obd(conn);
513 struct dentry *dentry;
519 iattr_from_obdo(&iattr, oa, oa->o_valid);
520 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
521 dentry = filter_fid2dentry(obd, filter_parent(obd, iattr.ia_mode),
522 oa->o_id, iattr.ia_mode);
524 RETURN(PTR_ERR(dentry));
526 inode = dentry->d_inode;
528 CERROR("setattr on non-existent object: "LPU64"\n", oa->o_id);
529 GOTO(out_setattr, rc = -ENOENT);
533 if (iattr.ia_valid & ATTR_SIZE)
535 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
536 if (inode->i_op->setattr)
537 rc = inode->i_op->setattr(dentry, &iattr);
539 rc = inode_setattr(inode, &iattr);
541 if (iattr.ia_valid & ATTR_SIZE) {
543 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
544 obdo_from_inode(oa, inode, oa->o_valid);
553 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
554 struct lov_stripe_md *ea)
556 struct obd_export *export;
557 struct obd_device *obd;
558 struct dentry *dentry;
561 export = class_conn2export(conn);
563 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
567 obd = export->exp_obd;
568 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
569 oa->o_id, oa->o_mode);
571 RETURN(PTR_ERR(dentry));
573 if (!dentry->d_inode) {
574 CERROR("opening non-existent objid "LPX64"\n", oa->o_id);
578 filter_from_inode(oa, dentry->d_inode, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
579 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
584 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
585 struct lov_stripe_md *ea)
587 struct obd_device *obd;
588 struct dentry *dentry;
591 obd = class_conn2obd(conn);
593 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
597 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
598 oa->o_id, oa->o_mode);
600 RETURN(PTR_ERR(dentry));
601 LASSERT(atomic_read(&dentry->d_count) > 1);
603 f_dput(dentry); /* for the close */
604 f_dput(dentry); /* for this call */
608 static int filter_create(struct lustre_handle* conn, struct obdo *oa,
609 struct lov_stripe_md **ea)
612 struct obd_run_ctxt saved;
615 struct obd_device *obd = class_conn2obd(conn);
616 struct filter_obd *filter = &obd->u.filter;
621 CERROR("invalid client "LPX64"\n", conn->addr);
625 if (!(oa->o_mode && S_IFMT)) {
626 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
627 __FUNCTION__, oa->o_id, oa->o_mode);
631 oa->o_id = filter_next_id(obd);
633 //filter_id(name, oa->o_id, oa->o_mode);
634 sprintf(name, LPU64, oa->o_id);
635 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
636 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
637 new = simple_mknod(filter->fo_dentry_O_mode[S_IFREG >> S_SHIFT], name,
641 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(new));
645 /* Set flags for fields we have set in the inode struct */
646 oa->o_valid = OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
647 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME;
648 filter_from_inode(oa, new->d_inode, oa->o_valid);
654 static int filter_destroy(struct lustre_handle *conn, struct obdo *oa,
655 struct lov_stripe_md *ea)
657 struct obd_device *obd;
658 struct filter_obd *filter;
659 struct obd_run_ctxt saved;
661 struct dentry *dir_dentry, *object_dentry;
665 obd = class_conn2obd(conn);
667 CERROR("invalid client "LPX64"\n", conn->addr);
671 CDEBUG(D_INODE, "destroying object "LPD64"\n", oa->o_id);
673 dir_dentry = filter_parent(obd, oa->o_mode);
674 down(&dir_dentry->d_inode->i_sem);
676 object_dentry = filter_fid2dentry(obd, dir_dentry, oa->o_id,
678 if (IS_ERR(object_dentry))
679 GOTO(out, rc = -ENOENT);
681 inode = object_dentry->d_inode;
683 CERROR("trying to destroy negative inode "LPX64"!\n", oa->o_id);
684 GOTO(out, rc = -ENOENT);
687 if (inode->i_nlink != 1) {
688 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
692 inode->i_mode = S_IFREG;
694 if (atomic_read(&inode->i_count) > 1) {
695 #warning FIXME: need to handle open-unlinked case and move to hold dir
696 CERROR("inode has count %d\n", atomic_read(&inode->i_count));
699 filter = &obd->u.filter;
700 push_ctxt(&saved, &filter->fo_ctxt);
702 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
704 f_dput(object_dentry);
708 up(&dir_dentry->d_inode->i_sem);
712 /* NB count and offset are used for punch, but not truncate */
713 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
714 struct lov_stripe_md *md,
715 obd_off start, obd_off end)
720 if (end != OBD_PUNCH_EOF)
721 CERROR("PUNCH not supported, only truncate works\n");
723 CDEBUG(D_INODE, "calling truncate for object "LPX64", valid = %x, "
724 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
726 error = filter_setattr(conn, oa, NULL);
730 static int filter_pgcache_brw(int cmd, struct lustre_handle *conn,
731 struct lov_stripe_md *lsm, obd_count oa_bufs,
732 struct brw_page *pga, brw_callback_t callback,
733 struct io_cb_data *data)
735 struct obd_run_ctxt saved;
736 struct super_block *sb;
737 int pnum; /* index to pages (bufs) */
738 unsigned long retval;
741 struct obd_device *obd = class_conn2obd(conn);
746 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
750 sb = obd->u.filter.fo_sb;
751 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
752 pnum = 0; /* pnum indexes buf 0..num_pages */
754 file = filter_obj_open(obd, lsm->lsm_object_id, S_IFREG);
756 GOTO(out, retval = PTR_ERR(file));
758 /* count doubles as retval */
759 for (pg = 0; pg < oa_bufs; pg++) {
760 CDEBUG(D_INODE, "OP %d obdo pgno: (%d) (%ld,"LPU64
761 ") off count ("LPU64",%d)\n",
762 cmd, pnum, file->f_dentry->d_inode->i_ino,
763 pga[pnum].off >> PAGE_CACHE_SHIFT, pga[pnum].off,
764 (int)pga[pnum].count);
765 if (cmd & OBD_BRW_WRITE) {
769 buffer = kmap(pga[pnum].pg);
770 retval = file->f_op->write(file, buffer,
773 kunmap(pga[pnum].pg);
774 CDEBUG(D_INODE, "retval %ld\n", retval);
776 loff_t off = pga[pnum].off;
777 char *buffer = kmap(pga[pnum].pg);
779 if (off >= file->f_dentry->d_inode->i_size) {
780 memset(buffer, 0, pga[pnum].count);
781 retval = pga[pnum].count;
783 retval = file->f_op->read(file, buffer,
784 pga[pnum].count, &off);
786 kunmap(pga[pnum].pg);
788 if (retval != pga[pnum].count) {
790 GOTO(out, retval = -EIO);
792 CDEBUG(D_INODE, "retval %ld\n", retval);
796 /* sizes and blocks are set by generic_file_write */
797 /* ctimes/mtimes will follow with a setattr call */
800 /* XXX: do something with callback if it is set? */
805 error = (retval >= 0) ? 0 : retval;
810 * Calculate the number of buffer credits needed to write multiple pages in
811 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
812 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
814 * See comment above ext3_writepage_trans_blocks for details. We assume
815 * no data journaling is being done, but it does allow for all of the pages
816 * being non-contiguous. If we are guaranteed contiguous pages we could
817 * reduce the number of (d)indirect blocks a lot.
819 * With N blocks per page and P pages, for each inode we have at most:
821 * min(N*P, blocksize/4 + 1) dindirect blocks
824 * For the entire filesystem, we have at most:
825 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
826 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
829 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
831 static int ext3_credits_needed(struct super_block *sb, int objcount,
832 struct obd_ioobj *obj)
834 struct obd_ioobj *o = obj;
835 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
836 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
839 int needed = objcount + 1;
842 for (i = 0; i < objcount; i++, o++) {
843 int nblocks = o->ioo_bufcnt * blockpp;
844 int ndindirect = min(nblocks, addrpp + 1);
845 int nindir = nblocks + ndindirect + 1;
847 nbitmaps += nindir + nblocks;
848 ngdblocks += nindir + nblocks;
853 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
854 nbitmaps = EXT3_SB(sb)->s_groups_count;
855 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
856 ngdblocks = EXT3_SB(sb)->s_gdb_count;
858 needed += nbitmaps + ngdblocks;
861 /* We assume that there will be 1 bit set in s_dquot.flags for each
862 * quota file that is active. This is at least true for now.
864 needed += hweight32(sb_any_quota_enabled(sb)) *
865 EXT3_SINGLEDATA_TRANS_BLOCKS;
871 /* We have to start a huge journal transaction here to hold all of the
872 * metadata for the pages being written here. This is necessitated by
873 * the fact that we do lots of prepare_write operations before we do
874 * any of the matching commit_write operations, so even if we split
875 * up to use "smaller" transactions none of them could complete until
876 * all of them were opened. By having a single journal transaction,
877 * we eliminate duplicate reservations for common blocks like the
878 * superblock and group descriptors or bitmaps.
880 * We will start the transaction here, but each prepare_write will
881 * add a refcount to the transaction, and each commit_write will
882 * remove a refcount. The transaction will be closed when all of
883 * the pages have been written.
885 static void *ext3_filter_journal_start(struct filter_obd *filter,
886 int objcount, struct obd_ioobj *obj,
887 int niocount, struct niobuf_remote *nb)
889 journal_t *journal = NULL;
890 handle_t *handle = NULL;
893 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
894 * with having extN built properly before filterobd for now.
896 journal = EXT3_SB(filter->fo_sb)->s_journal;
897 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
899 /* The number of blocks we could _possibly_ dirty can very large.
900 * We reduce our request if it is absurd (and we couldn't get that
901 * many credits for a single handle anyways).
903 * At some point we have to limit the size of I/Os sent at one time,
904 * increase the size of the journal, or we have to calculate the
905 * actual journal requirements more carefully by checking all of
906 * the blocks instead of being maximally pessimistic. It remains to
907 * be seen if this is a real problem or not.
909 if (needed > journal->j_max_transaction_buffers) {
910 CERROR("want too many journal credits (%d) using %d instead\n",
911 needed, journal->j_max_transaction_buffers);
912 needed = journal->j_max_transaction_buffers;
915 handle = journal_start(journal, needed);
917 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
923 static void *filter_journal_start(void **journal_save,
924 struct filter_obd *filter,
925 int objcount, struct obd_ioobj *obj,
926 int niocount, struct niobuf_remote *nb)
930 /* This may not be necessary - we probably never have a
931 * transaction started when we enter here, so we can
932 * remove the saving of the journal state entirely.
933 * For now leave it in just to see if it ever happens.
935 *journal_save = current->journal_info;
937 CERROR("Already have handle %p???\n", *journal_save);
939 current->journal_info = NULL;
942 if (!strcmp(filter->fo_fstype, "ext3") ||
943 !strcmp(filter->fo_fstype, "extN"))
944 handle = ext3_filter_journal_start(filter, objcount, obj,
949 static int ext3_filter_journal_stop(void *handle)
953 /* We got a refcount on the handle for each call to prepare_write,
954 * so we can drop the "parent" handle here to avoid the need for
955 * osc to call back into filterobd to close the handle. The
956 * remaining references will be dropped in commit_write.
958 rc = journal_stop((handle_t *)handle);
963 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
968 if (!strcmp(filter->fo_fstype, "ext3") ||
969 !strcmp(filter->fo_fstype, "extN"))
970 rc = ext3_filter_journal_stop(handle);
973 CERROR("error on journal stop: rc = %d\n", rc);
975 current->journal_info = journal_save;
980 static inline void lustre_put_page(struct page *page)
983 page_cache_release(page);
987 lustre_get_page_read(struct inode *inode, unsigned long index)
989 struct address_space *mapping = inode->i_mapping;
993 page = read_cache_page(mapping, index,
994 (filler_t*)mapping->a_ops->readpage, NULL);
998 if (!Page_Uptodate(page)) {
999 CERROR("page index %lu not uptodate\n", index);
1000 GOTO(err_page, rc = -EIO);
1002 if (PageError(page)) {
1003 CERROR("page index %lu has error\n", index);
1004 GOTO(err_page, rc = -EIO);
1010 lustre_put_page(page);
1014 static struct page *
1015 lustre_get_page_write(struct inode *inode, unsigned long index)
1017 struct address_space *mapping = inode->i_mapping;
1021 page = grab_cache_page(mapping, index); /* locked page */
1023 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.
1029 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1031 CERROR("page index %lu, rc = %d\n", index, rc);
1034 GOTO(err_unlock, rc);
1036 /* XXX not sure if we need this if we are overwriting page */
1037 if (PageError(page)) {
1038 CERROR("error on page index %lu, rc = %d\n", index, rc);
1040 GOTO(err_unlock, rc = -EIO);
1047 lustre_put_page(page);
1051 static int lustre_commit_write(struct page *page, unsigned from, unsigned to)
1053 struct inode *inode = page->mapping->host;
1056 err = page->mapping->a_ops->commit_write(NULL, page, from, to);
1057 if (!err && IS_SYNC(inode))
1058 err = waitfor_one_page(page);
1060 //SetPageUptodate(page); // the client commit_write will do this
1062 SetPageReferenced(page);
1064 lustre_put_page(page);
1068 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1069 struct niobuf_local *lnb, int *pglocked)
1071 struct address_space *mapping = inode->i_mapping;
1075 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1077 page = grab_cache_page_nowait(mapping, index); /* locked page */
1079 page = grab_cache_page(mapping, index); /* locked page */
1082 /* This page is currently locked, so get a temporary page instead. */
1083 /* XXX I believe this is a very dangerous thing to do - consider if
1084 * we had multiple writers for the same file (definitely the case
1085 * if we are using this codepath). If writer A locks the page,
1086 * writer B writes to a copy (as here), writer A drops the page
1087 * lock, and writer C grabs the lock before B does, then B will
1088 * later overwrite the data from C, even if C had LDLM locked
1089 * and initiated the write after B did.
1093 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
1094 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1096 CERROR("no memory for a temp page\n");
1098 GOTO(err, rc = -ENOMEM);
1100 page = virt_to_page(addr);
1102 page->index = index;
1103 lnb->flags |= N_LOCAL_TEMP_PAGE;
1104 } else if (!IS_ERR(page)) {
1108 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1109 * a no-op for most filesystems, because we write the whole
1110 * page. For partial-page I/O this will read in the page.
1112 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1114 CERROR("page index %lu, rc = %d\n", index, rc);
1117 GOTO(err_unlock, rc);
1119 /* XXX not sure if we need this if we are overwriting page */
1120 if (PageError(page)) {
1121 CERROR("error on page index %lu, rc = %d\n", index, rc);
1123 GOTO(err_unlock, rc = -EIO);
1130 lustre_put_page(page);
1136 * We need to balance prepare_write() calls with commit_write() calls.
1137 * If the page has been prepared, but we have no data for it, we don't
1138 * want to overwrite valid data on disk, but we still need to zero out
1139 * data for space which was newly allocated. Like part of what happens
1140 * in __block_prepare_write() for newly allocated blocks.
1142 * XXX currently __block_prepare_write() creates buffers for all the
1143 * pages, and the filesystems mark these buffers as BH_New if they
1144 * were newly allocated from disk. We use the BH_New flag similarly.
1146 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1150 unsigned block_start, block_end;
1151 struct buffer_head *bh, *head = page->buffers;
1152 unsigned blocksize = head->b_size;
1153 void *addr = page_address(page);
1155 /* debugging: just seeing if this ever happens */
1156 CERROR("called filter_commit_write for obj %ld:%ld on err %d\n",
1157 page->index, page->mapping->host->i_ino, err);
1159 /* Currently one buffer per page, but in the future... */
1160 for (bh = head, block_start = 0; bh != head || !block_start;
1161 block_start = block_end, bh = bh->b_this_page) {
1162 block_end = block_start + blocksize;
1164 memset(addr + block_start, 0, blocksize);
1168 return lustre_commit_write(page, from, to);
1171 static int filter_preprw(int cmd, struct lustre_handle *conn,
1172 int objcount, struct obd_ioobj *obj,
1173 int niocount, struct niobuf_remote *nb,
1174 struct niobuf_local *res, void **desc_private)
1176 struct obd_run_ctxt saved;
1177 struct obd_device *obd;
1178 struct obd_ioobj *o = obj;
1179 struct niobuf_remote *b = nb;
1180 struct niobuf_local *r = res;
1181 void *journal_save = NULL;
1187 obd = class_conn2obd(conn);
1189 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1192 memset(res, 0, sizeof(*res) * niocount);
1194 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1196 if (cmd & OBD_BRW_WRITE) {
1197 *desc_private = filter_journal_start(&journal_save,
1199 objcount, obj, niocount,
1201 if (IS_ERR(*desc_private))
1202 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1205 for (i = 0; i < objcount; i++, o++) {
1206 struct dentry *dentry;
1207 struct inode *inode;
1210 dentry = filter_fid2dentry(obd, filter_parent(obd, S_IFREG),
1211 o->ioo_id, S_IFREG);
1213 GOTO(out_clean, rc = PTR_ERR(dentry));
1214 inode = dentry->d_inode;
1216 CERROR("trying to BRW to non-existent file "LPU64"\n",
1219 GOTO(out_clean, rc = -ENOENT);
1222 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1223 unsigned long index = b->offset >> PAGE_SHIFT;
1229 r->dentry = dget(dentry);
1231 if (cmd & OBD_BRW_WRITE)
1232 page = filter_get_page_write(inode, index, r,
1235 page = lustre_get_page_read(inode, index);
1239 GOTO(out_clean, rc = PTR_ERR(page));
1242 r->addr = page_address(page);
1243 r->offset = b->offset;
1250 if (cmd & OBD_BRW_WRITE) {
1251 int err = filter_journal_stop(journal_save, &obd->u.filter,
1261 CERROR("error cleanup on brw\n");
1263 if (cmd & OBD_BRW_WRITE)
1264 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1266 lustre_put_page(r->page);
1271 static int filter_write_locked_page(struct niobuf_local *lnb)
1276 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1277 if (IS_ERR(lpage)) {
1278 /* It is highly unlikely that we would ever get an error here.
1279 * The page we want to get was previously locked, so it had to
1280 * have already allocated the space, and we were just writing
1281 * over the same data, so there would be no hole in the file.
1283 * XXX: possibility of a race with truncate could exist, need
1284 * to check that. There are no guarantees w.r.t.
1285 * write order even on a local filesystem, although the
1286 * normal response would be to return the number of bytes
1287 * successfully written and leave the rest to the app.
1289 rc = PTR_ERR(lpage);
1290 CERROR("error getting locked page index %ld: rc = %d\n",
1291 lnb->page->index, rc);
1295 /* lpage is kmapped in lustre_get_page_write() above and kunmapped in
1296 * lustre_commit_write() below, lnb->page was kmapped previously in
1297 * filter_get_page_write() and kunmapped in lustre_put_page() below.
1299 memcpy(page_address(lpage), page_address(lnb->page), PAGE_SIZE);
1300 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1302 CERROR("error committing locked page %ld: rc = %d\n",
1303 lnb->page->index, rc);
1305 lustre_put_page(lnb->page);
1310 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1311 int objcount, struct obd_ioobj *obj,
1312 int niocount, struct niobuf_local *res,
1315 struct obd_run_ctxt saved;
1316 struct obd_ioobj *o;
1317 struct niobuf_local *r;
1318 struct obd_device *obd = class_conn2obd(conn);
1320 int found_locked = 0;
1325 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1326 journal_save = current->journal_info;
1327 LASSERT(!journal_save);
1329 current->journal_info = private;
1330 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1332 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1333 struct page *page = r->page;
1338 if (r->flags & N_LOCAL_TEMP_PAGE) {
1343 if (cmd & OBD_BRW_WRITE) {
1344 int err = filter_commit_write(page, 0,
1350 lustre_put_page(page);
1355 current->journal_info = journal_save;
1360 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1362 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1364 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1367 err = filter_write_locked_page(r);
1379 static int filter_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
1381 struct obd_device *obd = class_conn2obd(conn);
1386 rc = vfs_statfs(obd->u.filter.fo_sb, &sfs);
1388 statfs_pack(osfs, &sfs);
1393 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1394 void *key, obd_count *vallen, void **val)
1396 struct obd_device *obd;
1399 obd = class_conn2obd(conn);
1401 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1405 if ( keylen == strlen("blocksize") &&
1406 memcmp(key, "blocksize", keylen) == 0 ) {
1407 *vallen = sizeof(long);
1408 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1412 if ( keylen == strlen("blocksize_bits") &&
1413 memcmp(key, "blocksize_bits", keylen) == 0 ){
1414 *vallen = sizeof(long);
1415 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1419 if ( keylen == strlen("root_ino") &&
1420 memcmp(key, "root_ino", keylen) == 0 ){
1421 *vallen = sizeof(obd_id);
1422 *val = (void *)(obd_id)FILTER_ROOTINO;
1426 CDEBUG(D_IOCTL, "invalid key\n");
1430 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1431 struct lustre_handle *src_conn, struct obdo *src,
1432 obd_size count, obd_off offset)
1435 struct lov_stripe_md srcmd, dstmd;
1436 unsigned long index = 0;
1439 memset(&srcmd, 0, sizeof(srcmd));
1440 memset(&dstmd, 0, sizeof(dstmd));
1441 srcmd.lsm_object_id = src->o_id;
1442 dstmd.lsm_object_id = dst->o_id;
1445 CDEBUG(D_INFO, "src: ino "LPU64" blocks "LPU64", size "LPU64
1446 ", dst: ino "LPU64"\n",
1447 src->o_id, src->o_blocks, src->o_size, dst->o_id);
1448 page = alloc_page(GFP_USER);
1452 while (TryLockPage(page))
1453 ___wait_on_page(page);
1455 /* XXX with brw vector I/O, we could batch up reads and writes here,
1456 * all we need to do is allocate multiple pages to handle the I/Os
1457 * and arrays to handle the request parameters.
1459 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1461 struct io_cb_data *cbd = ll_init_cb();
1470 pg.count = PAGE_SIZE;
1471 pg.off = (page->index) << PAGE_SHIFT;
1474 page->index = index;
1475 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &pg,
1476 ll_sync_io_cb, cbd);
1489 pg.flag = OBD_BRW_CREATE;
1490 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1492 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &pg,
1493 ll_sync_io_cb, cbd);
1495 /* XXX should handle dst->o_size, dst->o_blocks here */
1501 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1505 dst->o_size = src->o_size;
1506 dst->o_blocks = src->o_blocks;
1507 dst->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1515 static struct obd_ops filter_obd_ops = {
1516 o_get_info: filter_get_info,
1517 o_setup: filter_setup,
1518 o_cleanup: filter_cleanup,
1519 o_connect: filter_connect,
1520 o_disconnect: filter_disconnect,
1521 o_statfs: filter_statfs,
1522 o_getattr: filter_getattr,
1523 o_create: filter_create,
1524 o_setattr: filter_setattr,
1525 o_destroy: filter_destroy,
1526 o_open: filter_open,
1527 o_close: filter_close,
1528 o_brw: filter_pgcache_brw,
1529 o_punch: filter_truncate,
1530 o_preprw: filter_preprw,
1531 o_commitrw: filter_commitrw
1533 o_preallocate: filter_preallocate_inodes,
1534 o_migrate: filter_migrate,
1535 o_copy: filter_copy_data,
1536 o_iterate: filter_iterate
1541 static int __init obdfilter_init(void)
1543 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1544 return class_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1547 static void __exit obdfilter_exit(void)
1549 class_unregister_type(OBD_FILTER_DEVICENAME);
1552 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1553 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1554 MODULE_LICENSE("GPL");
1556 module_init(obdfilter_init);
1557 module_exit(obdfilter_exit);