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 static inline void f_dput(struct dentry *dentry)
55 CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
56 dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
57 LASSERT(atomic_read(&dentry->d_count) > 0);
61 /* setup the object store with correct subdirectories */
62 static int filter_prep(struct obd_device *obddev)
64 struct obd_run_ctxt saved;
65 struct filter_obd *filter = &obddev->u.filter;
66 struct dentry *dentry;
74 push_ctxt(&saved, &filter->fo_ctxt);
75 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
76 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
79 CERROR("cannot open/create O: rc = %d\n", rc);
82 filter->fo_dentry_O = dentry;
83 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
84 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
87 CERROR("cannot open/create P: rc = %d\n", rc);
90 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
91 atomic_read(&dentry->d_count) - 1);
93 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
94 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
97 CERROR("cannot open/create D: rc = %d\n", rc);
100 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
101 atomic_read(&dentry->d_count) - 1);
105 * Create directories and/or get dentries for each object type.
106 * This saves us from having to do multiple lookups for each one.
108 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
109 char *type = obd_type_by_mode[mode];
112 filter->fo_dentry_O_mode[mode] = NULL;
115 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
116 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
117 if (IS_ERR(dentry)) {
118 rc = PTR_ERR(dentry);
119 CERROR("cannot create O/%s: rc = %d\n", type, rc);
120 GOTO(out_O_mode, rc);
122 filter->fo_dentry_O_mode[mode] = dentry;
125 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
126 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
129 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
131 GOTO(out_O_mode, rc);
135 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
136 if ( !file || IS_ERR(file) ) {
138 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
140 GOTO(out_O_mode, rc);
143 /* steal operations */
144 inode = file->f_dentry->d_inode;
145 filter->fo_fop = file->f_op;
146 filter->fo_iop = inode->i_op;
147 filter->fo_aops = inode->i_mapping->a_ops;
149 if (inode->i_size == 0) {
150 __u64 disk_lastino = cpu_to_le64(lastino);
151 ssize_t retval = file->f_op->write(file, (char *)&disk_lastino,
152 sizeof(disk_lastino),
154 if (retval != sizeof(disk_lastino)) {
155 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
157 GOTO(out_O_mode, rc = -EIO);
161 ssize_t retval = file->f_op->read(file, (char *)&disk_lastino,
162 sizeof(disk_lastino),
164 if (retval != sizeof(disk_lastino)) {
165 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
167 GOTO(out_O_mode, rc = -EIO);
169 lastino = le64_to_cpu(disk_lastino);
171 filter->fo_lastino = lastino;
182 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
185 filter->fo_dentry_O_mode[mode] = NULL;
189 f_dput(filter->fo_dentry_O);
190 filter->fo_dentry_O = NULL;
194 /* cleanup the filter: write last used object id to status file */
195 static void filter_post(struct obd_device *obddev)
197 struct obd_run_ctxt saved;
198 struct filter_obd *filter = &obddev->u.filter;
204 push_ctxt(&saved, &filter->fo_ctxt);
205 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
207 CERROR("OBD filter: cannot create status file\n");
212 disk_lastino = cpu_to_le64(filter->fo_lastino);
213 rc = file->f_op->write(file, (char *)&disk_lastino,
214 sizeof(disk_lastino), &file->f_pos);
215 if (rc != sizeof(disk_lastino))
216 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
218 rc = filp_close(file, NULL);
220 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
222 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
223 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
226 filter->fo_dentry_O_mode[mode] = NULL;
229 f_dput(filter->fo_dentry_O);
235 static __u64 filter_next_id(struct obd_device *obddev)
239 spin_lock(&obddev->u.filter.fo_lock);
240 id = ++obddev->u.filter.fo_lastino;
241 spin_unlock(&obddev->u.filter.fo_lock);
243 /* FIXME: write the lastino to disk here */
247 /* how to get files, dentries, inodes from object id's */
248 /* parent i_sem is already held if needed for exclusivity */
249 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
250 struct dentry *dparent,
251 __u64 id, __u32 type)
253 struct super_block *sb = obddev->u.filter.fo_sb;
254 struct dentry *dchild;
259 if (!sb || !sb->s_dev) {
260 CERROR("fatal: device not initialized.\n");
261 RETURN(ERR_PTR(-ENXIO));
265 CERROR("fatal: invalid object #0\n");
267 RETURN(ERR_PTR(-ESTALE));
270 if (!(type & S_IFMT)) {
271 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
272 (unsigned long long)id, type);
273 RETURN(ERR_PTR(-EINVAL));
276 len = sprintf(name, "%Ld", id);
277 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
279 dchild = lookup_one_len(name, dparent, len);
280 if (IS_ERR(dchild)) {
281 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
285 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
286 obd_mode_to_type(type), name, dchild,
287 atomic_read(&dchild->d_count));
289 LASSERT(atomic_read(&dchild->d_count) > 0);
294 static struct file *filter_obj_open(struct obd_device *obddev,
295 __u64 id, __u32 type)
297 struct super_block *sb = obddev->u.filter.fo_sb;
298 struct obd_run_ctxt saved;
303 if (!sb || !sb->s_dev) {
304 CERROR("fatal: device not initialized.\n");
305 RETURN(ERR_PTR(-ENXIO));
309 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
310 RETURN(ERR_PTR(-ESTALE));
313 if (!(type & S_IFMT)) {
314 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
315 (unsigned long long)id, type);
316 RETURN(ERR_PTR(-EINVAL));
319 filter_id(name, id, type);
320 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
321 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
324 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
331 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
333 struct filter_obd *filter = &obddev->u.filter;
335 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
340 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
346 rc = class_connect(conn, obd, cluuid);
352 static int filter_disconnect(struct lustre_handle *conn)
357 rc = class_disconnect(conn);
361 /* XXX cleanup preallocated inodes */
365 /* mount the file system (secretly) */
366 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
368 struct obd_ioctl_data* data = buf;
369 struct filter_obd *filter;
370 struct vfsmount *mnt;
374 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
378 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
383 filter = &obddev->u.filter;;
384 filter->fo_vfsmnt = mnt;
385 filter->fo_fstype = strdup(data->ioc_inlbuf2);
386 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
387 /* XXX is this even possible if do_kern_mount succeeded? */
389 GOTO(err_kfree, err = -ENODEV);
391 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
392 filter->fo_ctxt.pwdmnt = mnt;
393 filter->fo_ctxt.pwd = mnt->mnt_root;
394 filter->fo_ctxt.fs = get_ds();
396 err = filter_prep(obddev);
398 GOTO(err_kfree, err);
399 spin_lock_init(&filter->fo_lock);
401 obddev->obd_namespace =
402 ldlm_namespace_new("filter-tgt", LDLM_NAMESPACE_SERVER);
403 if (obddev->obd_namespace == NULL)
409 kfree(filter->fo_fstype);
411 mntput(filter->fo_vfsmnt);
421 static int filter_cleanup(struct obd_device * obddev)
423 struct super_block *sb;
426 if (!list_empty(&obddev->obd_exports)) {
427 CERROR("still has clients!\n");
428 class_disconnect_all(obddev);
429 if (!list_empty(&obddev->obd_exports)) {
430 CERROR("still has exports after forced cleanup?\n");
435 ldlm_namespace_free(obddev->obd_namespace);
437 sb = obddev->u.filter.fo_sb;
438 if (!obddev->u.filter.fo_sb)
443 shrink_dcache_parent(sb->s_root);
445 mntput(obddev->u.filter.fo_vfsmnt);
446 obddev->u.filter.fo_sb = 0;
447 kfree(obddev->u.filter.fo_fstype);
456 static inline void filter_from_inode(struct obdo *oa, struct inode *inode,
459 int type = oa->o_mode & S_IFMT;
462 CDEBUG(D_INFO, "src inode %ld (%p), dst obdo %ld valid 0x%08x\n",
463 inode->i_ino, inode, (long)oa->o_id, valid);
464 /* Don't copy the inode number in place of the object ID */
465 obdo_from_inode(oa, inode, valid);
466 oa->o_mode &= ~S_IFMT;
469 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
470 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
472 oa->o_valid |= OBD_MD_FLRDEV;
478 static int filter_getattr(struct lustre_handle *conn, struct obdo *oa,
479 struct lov_stripe_md *md)
481 struct obd_device *obddev = class_conn2obd(conn);
482 struct dentry *dentry;
485 if (!class_conn2export(conn)) {
486 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\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 filter_from_inode(oa, dentry->d_inode, oa->o_valid);
502 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
503 struct lov_stripe_md *md)
505 struct obd_run_ctxt saved;
506 struct obd_device *obd = class_conn2obd(conn);
507 struct dentry *dentry;
513 iattr_from_obdo(&iattr, oa, oa->o_valid);
514 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
515 dentry = filter_fid2dentry(obd, filter_parent(obd, iattr.ia_mode),
516 oa->o_id, iattr.ia_mode);
518 RETURN(PTR_ERR(dentry));
520 inode = dentry->d_inode;
522 CERROR("setattr on non-existent object: "LPU64"\n", oa->o_id);
523 GOTO(out_setattr, rc = -ENOENT);
527 if (iattr.ia_valid & ATTR_SIZE)
529 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
530 if (inode->i_op->setattr)
531 rc = inode->i_op->setattr(dentry, &iattr);
533 rc = inode_setattr(inode, &iattr);
535 if (iattr.ia_valid & ATTR_SIZE) {
537 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
538 obdo_from_inode(oa, inode, oa->o_valid);
547 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
548 struct lov_stripe_md *ea)
550 struct obd_export *export;
551 struct obd_device *obd;
552 struct dentry *dentry;
555 export = class_conn2export(conn);
557 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
561 obd = export->exp_obd;
562 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
563 oa->o_id, oa->o_mode);
565 RETURN(PTR_ERR(dentry));
567 filter_from_inode(oa, dentry->d_inode, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
568 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
573 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
574 struct lov_stripe_md *ea)
576 struct obd_device *obd;
577 struct dentry *dentry;
580 obd = class_conn2obd(conn);
582 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
586 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
587 oa->o_id, oa->o_mode);
589 RETURN(PTR_ERR(dentry));
590 LASSERT(atomic_read(&dentry->d_count) > 1);
592 f_dput(dentry); /* for the close */
593 f_dput(dentry); /* for this call */
597 static int filter_create(struct lustre_handle* conn, struct obdo *oa,
598 struct lov_stripe_md **ea)
601 struct obd_run_ctxt saved;
604 struct obd_device *obd = class_conn2obd(conn);
609 CERROR("invalid client %Lx\n", conn->addr);
613 if (!(oa->o_mode && S_IFMT)) {
614 CERROR("filter obd: no type!\n");
618 oa->o_id = filter_next_id(obd);
620 filter_id(name, oa->o_id, oa->o_mode);
621 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
622 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
623 file = filp_open(name, O_RDONLY | O_CREAT, mode);
626 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
630 /* Set flags for fields we have set in the inode struct */
631 oa->o_valid = OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
632 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME;
633 filter_from_inode(oa, file->f_dentry->d_inode, oa->o_valid);
639 static int filter_destroy(struct lustre_handle *conn, struct obdo *oa,
640 struct lov_stripe_md *ea)
642 struct obd_device *obd;
643 struct filter_obd *filter;
644 struct obd_run_ctxt saved;
646 struct dentry *dir_dentry, *object_dentry;
650 obd = class_conn2obd(conn);
652 CERROR("invalid client %Lx\n", conn->addr);
656 CDEBUG(D_INODE, "destroying object %Ld\n", oa->o_id);
658 dir_dentry = filter_parent(obd, oa->o_mode);
659 down(&dir_dentry->d_inode->i_sem);
661 object_dentry = filter_fid2dentry(obd, dir_dentry, oa->o_id,
663 if (IS_ERR(object_dentry))
664 GOTO(out, rc = -ENOENT);
666 inode = object_dentry->d_inode;
668 CERROR("trying to destroy negative inode %Ld!\n", oa->o_id);
669 GOTO(out, rc = -ENOENT);
672 if (inode->i_nlink != 1) {
673 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
677 inode->i_mode = S_IFREG;
679 filter = &obd->u.filter;
680 push_ctxt(&saved, &filter->fo_ctxt);
682 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
684 f_dput(object_dentry);
688 up(&dir_dentry->d_inode->i_sem);
692 /* NB count and offset are used for punch, but not truncate */
693 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
694 struct lov_stripe_md *md,
695 obd_off start, obd_off end)
700 if (end != 0xffffffffffffffff)
701 CERROR("PUNCH not supported, only truncate works\n");
703 CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
704 "o_size = %Ld\n", oa->o_id, oa->o_valid, start);
706 error = filter_setattr(conn, oa, NULL);
710 static int filter_pgcache_brw(int cmd, struct lustre_handle *conn,
711 struct lov_stripe_md *md, obd_count oa_bufs,
712 struct brw_page *pga, brw_callback_t callback,
713 struct io_cb_data *data)
715 struct obd_run_ctxt saved;
716 struct super_block *sb;
717 int pnum; /* index to pages (bufs) */
718 unsigned long retval;
721 struct obd_device *obd = class_conn2obd(conn);
726 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
730 sb = obd->u.filter.fo_sb;
731 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
732 pnum = 0; /* pnum indexes buf 0..num_pages */
734 file = filter_obj_open(obd, md->lmd_object_id, S_IFREG);
736 GOTO(out, retval = PTR_ERR(file));
738 /* count doubles as retval */
739 for (pg = 0; pg < oa_bufs; pg++) {
740 CDEBUG(D_INODE, "OP %d obdo pgno: (%d) (%ld,"LPU64
741 ") off count ("LPU64",%d)\n",
742 cmd, pnum, file->f_dentry->d_inode->i_ino,
743 pga[pnum].off >> PAGE_CACHE_SHIFT, pga[pnum].off,
744 (int)pga[pnum].count);
745 if (cmd & OBD_BRW_WRITE) {
749 buffer = kmap(pga[pnum].pg);
750 retval = file->f_op->write(file, buffer,
753 kunmap(pga[pnum].pg);
754 CDEBUG(D_INODE, "retval %ld\n", retval);
756 loff_t off = pga[pnum].off;
757 char *buffer = kmap(pga[pnum].pg);
759 if (off >= file->f_dentry->d_inode->i_size) {
760 memset(buffer, 0, pga[pnum].count);
761 retval = pga[pnum].count;
763 retval = file->f_op->read(file, buffer,
764 pga[pnum].count, &off);
766 kunmap(pga[pnum].pg);
768 if (retval != pga[pnum].count) {
770 GOTO(out, retval = -EIO);
772 CDEBUG(D_INODE, "retval %ld\n", retval);
776 /* sizes and blocks are set by generic_file_write */
777 /* ctimes/mtimes will follow with a setattr call */
780 /* XXX: do something with callback if it is set? */
785 error = (retval >= 0) ? 0 : retval;
790 * Calculate the number of buffer credits needed to write multiple pages in
791 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
792 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
794 * See comment above ext3_writepage_trans_blocks for details. We assume
795 * no data journaling is being done, but it does allow for all of the pages
796 * being non-contiguous. If we are guaranteed contiguous pages we could
797 * reduce the number of (d)indirect blocks a lot.
799 * With N blocks per page and P pages, for each inode we have at most:
801 * min(N*P, blocksize/4 + 1) dindirect blocks
804 * For the entire filesystem, we have at most:
805 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
806 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
809 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
811 static int ext3_credits_needed(struct super_block *sb, int objcount,
812 struct obd_ioobj *obj)
814 struct obd_ioobj *o = obj;
815 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
816 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
819 int needed = objcount + 1;
822 for (i = 0; i < objcount; i++, o++) {
823 int nblocks = o->ioo_bufcnt * blockpp;
824 int ndindirect = min(nblocks, addrpp + 1);
825 int nindir = nblocks + ndindirect + 1;
827 nbitmaps += nindir + nblocks;
828 ngdblocks += nindir + nblocks;
833 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
834 nbitmaps = EXT3_SB(sb)->s_groups_count;
835 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
836 ngdblocks = EXT3_SB(sb)->s_gdb_count;
838 needed += nbitmaps + ngdblocks;
841 /* We assume that there will be 1 bit set in s_dquot.flags for each
842 * quota file that is active. This is at least true for now.
844 needed += hweight32(sb_any_quota_enabled(sb)) *
845 EXT3_SINGLEDATA_TRANS_BLOCKS;
851 /* We have to start a huge journal transaction here to hold all of the
852 * metadata for the pages being written here. This is necessitated by
853 * the fact that we do lots of prepare_write operations before we do
854 * any of the matching commit_write operations, so even if we split
855 * up to use "smaller" transactions none of them could complete until
856 * all of them were opened. By having a single journal transaction,
857 * we eliminate duplicate reservations for common blocks like the
858 * superblock and group descriptors or bitmaps.
860 * We will start the transaction here, but each prepare_write will
861 * add a refcount to the transaction, and each commit_write will
862 * remove a refcount. The transaction will be closed when all of
863 * the pages have been written.
865 static void *ext3_filter_journal_start(struct filter_obd *filter,
866 int objcount, struct obd_ioobj *obj,
867 int niocount, struct niobuf_remote *nb)
869 journal_t *journal = NULL;
870 handle_t *handle = NULL;
873 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
874 * with having extN built properly before filterobd for now.
876 journal = EXT3_SB(filter->fo_sb)->s_journal;
877 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
879 /* The number of blocks we could _possibly_ dirty can very large.
880 * We reduce our request if it is absurd (and we couldn't get that
881 * many credits for a single handle anyways).
883 * At some point we have to limit the size of I/Os sent at one time,
884 * increase the size of the journal, or we have to calculate the
885 * actual journal requirements more carefully by checking all of
886 * the blocks instead of being maximally pessimistic. It remains to
887 * be seen if this is a real problem or not.
889 if (needed > journal->j_max_transaction_buffers) {
890 CERROR("want too many journal credits (%d) using %d instead\n",
891 needed, journal->j_max_transaction_buffers);
892 needed = journal->j_max_transaction_buffers;
895 handle = journal_start(journal, needed);
897 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
903 static void *filter_journal_start(void **journal_save,
904 struct filter_obd *filter,
905 int objcount, struct obd_ioobj *obj,
906 int niocount, struct niobuf_remote *nb)
910 /* This may not be necessary - we probably never have a
911 * transaction started when we enter here, so we can
912 * remove the saving of the journal state entirely.
913 * For now leave it in just to see if it ever happens.
915 *journal_save = current->journal_info;
917 CERROR("Already have handle %p???\n", *journal_save);
919 current->journal_info = NULL;
922 if (!strcmp(filter->fo_fstype, "ext3") ||
923 !strcmp(filter->fo_fstype, "extN"))
924 handle = ext3_filter_journal_start(filter, objcount, obj,
929 static int ext3_filter_journal_stop(void *handle)
933 /* We got a refcount on the handle for each call to prepare_write,
934 * so we can drop the "parent" handle here to avoid the need for
935 * osc to call back into filterobd to close the handle. The
936 * remaining references will be dropped in commit_write.
938 rc = journal_stop((handle_t *)handle);
943 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
948 if (!strcmp(filter->fo_fstype, "ext3") ||
949 !strcmp(filter->fo_fstype, "extN"))
950 rc = ext3_filter_journal_stop(handle);
953 CERROR("error on journal stop: rc = %d\n", rc);
955 current->journal_info = journal_save;
960 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
961 struct niobuf_local *lnb, int *pglocked)
963 struct address_space *mapping = inode->i_mapping;
967 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
969 page = grab_cache_page_nowait(mapping, index); /* locked page */
971 page = grab_cache_page(mapping, index); /* locked page */
974 /* This page is currently locked, so get a temporary page instead. */
975 /* XXX I believe this is a very dangerous thing to do - consider if
976 * we had multiple writers for the same file (definitely the case
977 * if we are using this codepath). If writer A locks the page,
978 * writer B writes to a copy (as here), writer A drops the page
979 * lock, and writer C grabs the lock before B does, then B will
980 * later overwrite the data from C, even if C had LDLM locked
981 * and initiated the write after B did.
985 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
986 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
988 CERROR("no memory for a temp page\n");
990 GOTO(err, rc = -ENOMEM);
992 page = virt_to_page(addr);
995 lnb->flags |= N_LOCAL_TEMP_PAGE;
996 } else if (!IS_ERR(page)) {
1000 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1001 * a no-op for most filesystems, because we write the whole
1002 * page. For partial-page I/O this will read in the page.
1004 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1006 CERROR("page index %lu, rc = %d\n", index, rc);
1009 GOTO(err_unlock, rc);
1011 /* XXX not sure if we need this if we are overwriting page */
1012 if (PageError(page)) {
1013 CERROR("error on page index %lu, rc = %d\n", index, rc);
1015 GOTO(err_unlock, rc = -EIO);
1022 lustre_put_page(page);
1028 * We need to balance prepare_write() calls with commit_write() calls.
1029 * If the page has been prepared, but we have no data for it, we don't
1030 * want to overwrite valid data on disk, but we still need to zero out
1031 * data for space which was newly allocated. Like part of what happens
1032 * in __block_prepare_write() for newly allocated blocks.
1034 * XXX currently __block_prepare_write() creates buffers for all the
1035 * pages, and the filesystems mark these buffers as BH_New if they
1036 * were newly allocated from disk. We use the BH_New flag similarly.
1038 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1042 unsigned block_start, block_end;
1043 struct buffer_head *bh, *head = page->buffers;
1044 unsigned blocksize = head->b_size;
1045 void *addr = page_address(page);
1047 /* debugging: just seeing if this ever happens */
1048 CERROR("called filter_commit_write for obj %ld:%ld on err %d\n",
1049 page->index, page->mapping->host->i_ino, err);
1051 /* Currently one buffer per page, but in the future... */
1052 for (bh = head, block_start = 0; bh != head || !block_start;
1053 block_start = block_end, bh = bh->b_this_page) {
1054 block_end = block_start + blocksize;
1056 memset(addr + block_start, 0, blocksize);
1060 return lustre_commit_write(page, from, to);
1063 static int filter_preprw(int cmd, struct lustre_handle *conn,
1064 int objcount, struct obd_ioobj *obj,
1065 int niocount, struct niobuf_remote *nb,
1066 struct niobuf_local *res, void **desc_private)
1068 struct obd_run_ctxt saved;
1069 struct obd_device *obd;
1070 struct obd_ioobj *o = obj;
1071 struct niobuf_remote *b = nb;
1072 struct niobuf_local *r = res;
1073 void *journal_save = NULL;
1079 obd = class_conn2obd(conn);
1081 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1084 memset(res, 0, sizeof(*res) * niocount);
1086 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1088 if (cmd & OBD_BRW_WRITE) {
1089 *desc_private = filter_journal_start(&journal_save,
1091 objcount, obj, niocount,
1093 if (IS_ERR(*desc_private))
1094 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1097 for (i = 0; i < objcount; i++, o++) {
1098 struct dentry *dentry;
1099 struct inode *inode;
1102 dentry = filter_fid2dentry(obd, filter_parent(obd, S_IFREG),
1103 o->ioo_id, S_IFREG);
1105 GOTO(out_clean, rc = PTR_ERR(dentry));
1106 inode = dentry->d_inode;
1108 CERROR("trying to BRW to non-existent file %Ld\n",
1109 (unsigned long long)o->ioo_id);
1111 GOTO(out_clean, rc = -ENOENT);
1114 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1115 unsigned long index = b->offset >> PAGE_SHIFT;
1121 r->dentry = dget(dentry);
1123 if (cmd & OBD_BRW_WRITE)
1124 page = filter_get_page_write(inode, index, r,
1127 page = lustre_get_page_read(inode, index);
1131 GOTO(out_clean, rc = PTR_ERR(page));
1134 r->addr = page_address(page);
1135 r->offset = b->offset;
1142 if (cmd & OBD_BRW_WRITE) {
1143 int err = filter_journal_stop(journal_save, &obd->u.filter,
1153 CERROR("error cleanup on brw\n");
1155 if (cmd & OBD_BRW_WRITE)
1156 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1158 lustre_put_page(r->page);
1163 static int filter_write_locked_page(struct niobuf_local *lnb)
1168 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1169 if (IS_ERR(lpage)) {
1170 /* It is highly unlikely that we would ever get an error here.
1171 * The page we want to get was previously locked, so it had to
1172 * have already allocated the space, and we were just writing
1173 * over the same data, so there would be no hole in the file.
1175 * XXX: possibility of a race with truncate could exist, need
1176 * to check that. There are no guarantees w.r.t.
1177 * write order even on a local filesystem, although the
1178 * normal response would be to return the number of bytes
1179 * successfully written and leave the rest to the app.
1181 rc = PTR_ERR(lpage);
1182 CERROR("error getting locked page index %ld: rc = %d\n",
1183 lnb->page->index, rc);
1187 /* lpage is kmapped in lustre_get_page_write() above and kunmapped in
1188 * lustre_commit_write() below, lnb->page was kmapped previously in
1189 * filter_get_page_write() and kunmapped in lustre_put_page() below.
1191 memcpy(page_address(lpage), page_address(lnb->page), PAGE_SIZE);
1192 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1194 CERROR("error committing locked page %ld: rc = %d\n",
1195 lnb->page->index, rc);
1197 lustre_put_page(lnb->page);
1202 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1203 int objcount, struct obd_ioobj *obj,
1204 int niocount, struct niobuf_local *res,
1207 struct obd_run_ctxt saved;
1208 struct obd_ioobj *o;
1209 struct niobuf_local *r;
1210 struct obd_device *obd = class_conn2obd(conn);
1212 int found_locked = 0;
1217 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1218 journal_save = current->journal_info;
1219 LASSERT(!journal_save);
1221 current->journal_info = private;
1222 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1224 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1225 struct page *page = r->page;
1230 if (r->flags & N_LOCAL_TEMP_PAGE) {
1235 if (cmd & OBD_BRW_WRITE) {
1236 int err = filter_commit_write(page, 0,
1242 lustre_put_page(page);
1247 current->journal_info = journal_save;
1252 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1254 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1256 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1259 err = filter_write_locked_page(r);
1271 static int filter_statfs(struct lustre_handle *conn, struct statfs *statfs)
1273 struct obd_device *obd = class_conn2obd(conn);
1276 RETURN(vfs_statfs(obd->u.filter.fo_sb, statfs));
1279 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1280 void *key, obd_count *vallen, void **val)
1282 struct obd_device *obd;
1283 struct obd_export * export;
1286 if (!(export = class_conn2export(conn))) {
1287 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
1291 obd = class_conn2obd(conn);
1293 if ( keylen == strlen("blocksize") &&
1294 memcmp(key, "blocksize", keylen) == 0 ) {
1295 *vallen = sizeof(long);
1296 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1300 if ( keylen == strlen("blocksize_bits") &&
1301 memcmp(key, "blocksize_bits", keylen) == 0 ){
1302 *vallen = sizeof(long);
1303 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1307 if ( keylen == strlen("root_ino") &&
1308 memcmp(key, "root_ino", keylen) == 0 ){
1309 *vallen = sizeof(obd_id);
1310 *val = (void *)(obd_id)FILTER_ROOTINO;
1314 CDEBUG(D_IOCTL, "invalid key\n");
1318 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1319 struct lustre_handle *src_conn, struct obdo *src,
1320 obd_size count, obd_off offset)
1323 struct lov_stripe_md srcmd, dstmd;
1324 unsigned long index = 0;
1327 memset(&srcmd, 0, sizeof(srcmd));
1328 memset(&dstmd, 0, sizeof(dstmd));
1329 srcmd.lmd_object_id = src->o_id;
1330 dstmd.lmd_object_id = dst->o_id;
1333 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
1334 (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
1335 (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
1336 page = alloc_page(GFP_USER);
1340 while (TryLockPage(page))
1341 ___wait_on_page(page);
1343 /* XXX with brw vector I/O, we could batch up reads and writes here,
1344 * all we need to do is allocate multiple pages to handle the I/Os
1345 * and arrays to handle the request parameters.
1347 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1349 struct io_cb_data *cbd = ll_init_cb();
1358 pg.count = PAGE_SIZE;
1359 pg.off = (page->index) << PAGE_SHIFT;
1362 page->index = index;
1363 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &pg,
1364 ll_sync_io_cb, cbd);
1377 pg.flag = OBD_BRW_CREATE;
1378 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1380 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &pg,
1381 ll_sync_io_cb, cbd);
1383 /* XXX should handle dst->o_size, dst->o_blocks here */
1389 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1393 dst->o_size = src->o_size;
1394 dst->o_blocks = src->o_blocks;
1395 dst->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1403 static struct obd_ops filter_obd_ops = {
1404 o_get_info: filter_get_info,
1405 o_setup: filter_setup,
1406 o_cleanup: filter_cleanup,
1407 o_connect: filter_connect,
1408 o_disconnect: filter_disconnect,
1409 o_statfs: filter_statfs,
1410 o_getattr: filter_getattr,
1411 o_create: filter_create,
1412 o_setattr: filter_setattr,
1413 o_destroy: filter_destroy,
1414 o_open: filter_open,
1415 o_close: filter_close,
1416 o_brw: filter_pgcache_brw,
1417 o_punch: filter_truncate,
1418 o_preprw: filter_preprw,
1419 o_commitrw: filter_commitrw
1421 o_preallocate: filter_preallocate_inodes,
1422 o_migrate: filter_migrate,
1423 o_copy: filter_copy_data,
1424 o_iterate: filter_iterate
1429 static int __init obdfilter_init(void)
1431 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1432 return class_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1435 static void __exit obdfilter_exit(void)
1437 class_unregister_type(OBD_FILTER_DEVICENAME);
1440 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1441 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1442 MODULE_LICENSE("GPL");
1444 module_init(obdfilter_init);
1445 module_exit(obdfilter_exit);