1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/filter/filter.c
6 * Copyright (C) 2001 Cluster File Systems, Inc.
8 * This code is issued under the GNU General Public License.
9 * See the file COPYING in this distribution
11 * by Peter Braam <braam@clusterfs.com>
15 #define DEBUG_SUBSYSTEM S_FILTER
17 #include <linux/module.h>
18 #include <linux/lustre_dlm.h>
19 #include <linux/obd_filter.h>
20 #include <linux/ext3_jbd.h>
21 #include <linux/quotaops.h>
22 #include <linux/init.h>
24 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
27 #define FILTER_ROOTINO 2
30 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
32 [S_IFREG >> S_SHIFT] "R",
33 [S_IFDIR >> S_SHIFT] "D",
34 [S_IFCHR >> S_SHIFT] "C",
35 [S_IFBLK >> S_SHIFT] "B",
36 [S_IFIFO >> S_SHIFT] "F",
37 [S_IFSOCK >> S_SHIFT] "S",
38 [S_IFLNK >> S_SHIFT] "L"
41 static inline const char *obd_mode_to_type(int mode)
43 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
46 /* write the pathname into the string */
47 static int filter_id(char *buf, obd_id id, obd_mode mode)
49 return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
50 (unsigned long long)id);
53 /* setup the object store with correct subdirectories */
54 static int filter_prep(struct obd_device *obddev)
56 struct obd_run_ctxt saved;
57 struct filter_obd *filter = &obddev->u.filter;
58 struct dentry *dentry;
66 push_ctxt(&saved, &filter->fo_ctxt);
67 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
68 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
71 CERROR("cannot open/create O: rc = %d\n", rc);
74 filter->fo_dentry_O = dentry;
75 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
76 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
79 CERROR("cannot open/create P: rc = %d\n", rc);
82 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
83 atomic_read(&dentry->d_count) - 1);
85 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
86 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
89 CERROR("cannot open/create D: rc = %d\n", rc);
92 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
93 atomic_read(&dentry->d_count) - 1);
97 * Create directories and/or get dentries for each object type.
98 * This saves us from having to do multiple lookups for each one.
100 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
101 char *type = obd_type_by_mode[mode];
104 filter->fo_dentry_O_mode[mode] = NULL;
107 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
108 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
109 if (IS_ERR(dentry)) {
110 rc = PTR_ERR(dentry);
111 CERROR("cannot create O/%s: rc = %d\n", type, rc);
112 GOTO(out_O_mode, rc);
114 filter->fo_dentry_O_mode[mode] = dentry;
117 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
118 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
121 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
123 GOTO(out_O_mode, rc);
127 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
128 if ( !file || IS_ERR(file) ) {
130 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
132 GOTO(out_O_mode, rc);
135 /* steal operations */
136 inode = file->f_dentry->d_inode;
137 filter->fo_fop = file->f_op;
138 filter->fo_iop = inode->i_op;
139 filter->fo_aops = inode->i_mapping->a_ops;
141 if (inode->i_size == 0) {
142 __u64 disk_lastino = cpu_to_le64(lastino);
143 ssize_t retval = file->f_op->write(file, (char *)&disk_lastino,
144 sizeof(disk_lastino),
146 if (retval != sizeof(disk_lastino)) {
147 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
149 GOTO(out_O_mode, rc = -EIO);
153 ssize_t retval = file->f_op->read(file, (char *)&disk_lastino,
154 sizeof(disk_lastino),
156 if (retval != sizeof(disk_lastino)) {
157 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
159 GOTO(out_O_mode, rc = -EIO);
161 lastino = le64_to_cpu(disk_lastino);
163 filter->fo_lastino = lastino;
174 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
176 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
177 obd_type_by_mode[mode], dentry,
178 atomic_read(&dentry->d_count) - 1);
180 filter->fo_dentry_O_mode[mode] = NULL;
184 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
185 atomic_read(&filter->fo_dentry_O->d_count) - 1);
186 dput(filter->fo_dentry_O);
187 filter->fo_dentry_O = NULL;
191 /* cleanup the filter: write last used object id to status file */
192 static void filter_post(struct obd_device *obddev)
194 struct obd_run_ctxt saved;
195 struct filter_obd *filter = &obddev->u.filter;
201 push_ctxt(&saved, &filter->fo_ctxt);
202 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
204 CERROR("OBD filter: cannot create status file\n");
209 disk_lastino = cpu_to_le64(filter->fo_lastino);
210 rc = file->f_op->write(file, (char *)&disk_lastino,
211 sizeof(disk_lastino), &file->f_pos);
212 if (rc != sizeof(disk_lastino))
213 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
215 rc = filp_close(file, NULL);
217 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
219 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
220 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
222 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
223 obd_type_by_mode[mode], dentry,
224 atomic_read(&dentry->d_count) - 1);
226 filter->fo_dentry_O_mode[mode] = NULL;
229 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
230 atomic_read(&filter->fo_dentry_O->d_count) - 1);
231 dput(filter->fo_dentry_O);
237 static __u64 filter_next_id(struct obd_device *obddev)
241 spin_lock(&obddev->u.filter.fo_lock);
242 obddev->u.filter.fo_lastino++;
243 id = obddev->u.filter.fo_lastino;
244 spin_unlock(&obddev->u.filter.fo_lock);
246 /* FIXME: write the lastino to disk here */
250 /* how to get files, dentries, inodes from object id's */
251 /* parent i_sem is already held if needed for exclusivity */
252 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
253 struct dentry *dparent,
254 __u64 id, __u32 type)
256 struct super_block *sb = obddev->u.filter.fo_sb;
257 struct dentry *dchild;
262 if (!sb || !sb->s_dev) {
263 CERROR("fatal: device not initialized.\n");
264 RETURN(ERR_PTR(-ENXIO));
268 CERROR("fatal: invalid object #0\n");
270 RETURN(ERR_PTR(-ESTALE));
273 if (!(type & S_IFMT)) {
274 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
275 (unsigned long long)id, type);
276 RETURN(ERR_PTR(-EINVAL));
279 len = sprintf(name, "%Ld", id);
280 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
282 dchild = lookup_one_len(name, dparent, len);
283 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
284 obd_mode_to_type(type), name, dchild,
285 atomic_read(&dchild->d_count));
287 if (IS_ERR(dchild)) {
288 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
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_sb = mnt->mnt_root->d_inode->i_sb;
386 /* XXX is this even possible if do_kern_mount succeeded? */
388 GOTO(err_put, err = -ENODEV);
390 filter->fo_vfsmnt = mnt;
391 filter->fo_fstype = strdup(data->ioc_inlbuf2);
393 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
394 filter->fo_ctxt.pwdmnt = mnt;
395 filter->fo_ctxt.pwd = mnt->mnt_root;
396 filter->fo_ctxt.fs = get_ds();
398 err = filter_prep(obddev);
400 GOTO(err_kfree, err);
401 spin_lock_init(&filter->fo_lock);
403 obddev->obd_namespace =
404 ldlm_namespace_new("filter-tgt", LDLM_NAMESPACE_SERVER);
405 if (obddev->obd_namespace == NULL)
411 kfree(filter->fo_fstype);
414 mntput(filter->fo_vfsmnt);
424 static int filter_cleanup(struct obd_device * obddev)
426 struct super_block *sb;
429 if (!list_empty(&obddev->obd_exports)) {
430 CERROR("still has clients!\n");
431 class_disconnect_all(obddev);
432 if (!list_empty(&obddev->obd_exports)) {
433 CERROR("still has exports after forced cleanup?\n");
438 ldlm_namespace_free(obddev->obd_namespace);
440 sb = obddev->u.filter.fo_sb;
441 if (!obddev->u.filter.fo_sb)
446 shrink_dcache_parent(sb->s_root);
448 mntput(obddev->u.filter.fo_vfsmnt);
449 obddev->u.filter.fo_sb = 0;
450 kfree(obddev->u.filter.fo_fstype);
459 static inline void filter_from_inode(struct obdo *oa, struct inode *inode,
462 int type = oa->o_mode & S_IFMT;
465 CDEBUG(D_INFO, "src inode %ld (%p), dst obdo %ld valid 0x%08x\n",
466 inode->i_ino, inode, (long)oa->o_id, valid);
467 /* Don't copy the inode number in place of the object ID */
468 obdo_from_inode(oa, inode, valid);
469 oa->o_mode &= ~S_IFMT;
472 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
473 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
475 oa->o_valid |= OBD_MD_FLRDEV;
481 static int filter_getattr(struct lustre_handle *conn, struct obdo *oa,
482 struct lov_stripe_md *md)
484 struct obd_device *obddev = class_conn2obd(conn);
485 struct dentry *dentry;
488 if (!class_conn2export(conn)) {
489 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
493 obddev = class_conn2obd(conn);
494 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
495 oa->o_id, oa->o_mode);
497 RETURN(PTR_ERR(dentry));
499 filter_from_inode(oa, dentry->d_inode, oa->o_valid & ~OBD_MD_FLID);
505 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
506 struct lov_stripe_md *md)
508 struct obd_run_ctxt saved;
509 struct obd_device *obd = class_conn2obd(conn);
510 struct dentry *dentry;
516 iattr_from_obdo(&iattr, oa, oa->o_valid);
517 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
518 dentry = filter_fid2dentry(obd, filter_parent(obd, iattr.ia_mode),
519 oa->o_id, iattr.ia_mode);
521 RETURN(PTR_ERR(dentry));
523 inode = dentry->d_inode;
525 if (iattr.ia_valid & ATTR_SIZE)
527 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
528 if (inode->i_op->setattr)
529 rc = inode->i_op->setattr(dentry, &iattr);
531 rc = inode_setattr(inode, &iattr);
533 if (iattr.ia_valid & ATTR_SIZE) {
535 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
536 obdo_from_inode(oa, inode, oa->o_valid);
540 CDEBUG(D_INODE, "put dentry %p, count = %d\n", inode,
541 atomic_read(&dentry->d_count) - 1);
546 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
547 struct lov_stripe_md *ea)
549 struct obd_device *obd;
550 struct dentry *dentry;
553 if (!class_conn2export(conn)) {
554 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
558 obd = class_conn2obd(conn);
559 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
560 oa->o_id, oa->o_mode);
562 RETURN(PTR_ERR(dentry));
564 oa->o_size = dentry->d_inode->i_size;
569 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
570 struct lov_stripe_md *ea)
572 struct obd_device *obd;
573 struct dentry *dentry;
576 obd = class_conn2obd(conn);
578 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
582 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
583 oa->o_id, oa->o_mode);
585 RETURN(PTR_ERR(dentry));
587 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
588 atomic_read(&dentry->d_count) - 1);
589 dput(dentry); /* for the close */
590 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
591 atomic_read(&dentry->d_count) - 1);
592 dput(dentry); /* for this call */
596 static int filter_create(struct lustre_handle* conn, struct obdo *oa,
597 struct lov_stripe_md **ea)
600 struct obd_run_ctxt saved;
603 struct obd_device *obd = class_conn2obd(conn);
608 CERROR("invalid client %Lx\n", conn->addr);
612 if (!(oa->o_mode && S_IFMT)) {
613 CERROR("filter obd: no type!\n");
617 oa->o_id = filter_next_id(obd);
619 filter_id(name, oa->o_id, oa->o_mode);
620 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
621 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
622 file = filp_open(name, O_RDONLY | O_CREAT, mode);
625 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
629 /* Set flags for fields we have set in the inode struct */
630 oa->o_valid = OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
631 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
632 OBD_MD_FLUID | OBD_MD_FLGID;
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 CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
685 atomic_read(&object_dentry->d_count) - 1);
690 up(&dir_dentry->d_inode->i_sem);
694 /* NB count and offset are used for punch, but not truncate */
695 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
696 struct lov_stripe_md *md,
697 obd_off start, obd_off end)
702 if (end != 0xffffffffffffffff)
703 CERROR("PUNCH not supported, only truncate works\n");
705 CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
706 "o_size = %Ld\n", oa->o_id, oa->o_valid, start);
708 error = filter_setattr(conn, oa, NULL);
712 static int filter_pgcache_brw(int cmd, struct lustre_handle *conn,
713 struct lov_stripe_md *md, obd_count oa_bufs,
714 struct brw_page *pga, brw_callback_t callback,
715 struct io_cb_data *data)
717 struct obd_run_ctxt saved;
718 struct super_block *sb;
719 int pnum; /* index to pages (bufs) */
720 unsigned long retval;
723 struct obd_device *obd = class_conn2obd(conn);
728 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
732 sb = obd->u.filter.fo_sb;
733 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
734 pnum = 0; /* pnum indexes buf 0..num_pages */
736 file = filter_obj_open(obd, md->lmd_object_id, S_IFREG);
738 GOTO(out, retval = PTR_ERR(file));
740 /* count doubles as retval */
741 for (pg = 0; pg < oa_bufs; pg++) {
742 CDEBUG(D_INODE, "OP %d obdo pgno: (%d) (%ld,%ld) "
743 "off count (%Ld,%Ld)\n",
744 cmd, pnum, file->f_dentry->d_inode->i_ino,
745 (unsigned long)pga[pnum].off >> PAGE_CACHE_SHIFT,
746 (unsigned long long)pga[pnum].off,
747 (unsigned long long)pga[pnum].count);
748 if (cmd & OBD_BRW_WRITE) {
752 buffer = kmap(pga[pnum].pg);
753 retval = file->f_op->write(file, buffer,
756 kunmap(pga[pnum].pg);
757 CDEBUG(D_INODE, "retval %ld\n", retval);
759 loff_t off = pga[pnum].off;
760 char *buffer = kmap(pga[pnum].pg);
762 if (off >= file->f_dentry->d_inode->i_size) {
763 memset(buffer, 0, pga[pnum].count);
764 retval = pga[pnum].count;
766 retval = file->f_op->read(file, buffer,
767 pga[pnum].count, &off);
769 kunmap(pga[pnum].pg);
771 if (retval != pga[pnum].count) {
773 GOTO(out, retval = -EIO);
775 CDEBUG(D_INODE, "retval %ld\n", retval);
779 /* sizes and blocks are set by generic_file_write */
780 /* ctimes/mtimes will follow with a setattr call */
783 /* XXX: do something with callback if it is set? */
788 error = (retval >= 0) ? 0 : retval;
793 * Calculate the number of buffer credits needed to write multiple pages in
794 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
795 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
797 * See comment above ext3_writepage_trans_blocks for details. We assume
798 * no data journaling is being done, but it does allow for all of the pages
799 * being non-contiguous. If we are guaranteed contiguous pages we could
800 * reduce the number of (d)indirect blocks a lot.
802 * With N blocks per page and P pages, for each inode we have at most:
804 * min(N*P, blocksize/4 + 1) dindirect blocks
807 * For the entire filesystem, we have at most:
808 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
809 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
812 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
814 static int ext3_credits_needed(struct super_block *sb, int objcount,
815 struct obd_ioobj *obj)
817 struct obd_ioobj *o = obj;
818 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
819 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
822 int needed = objcount + 1;
825 for (i = 0; i < objcount; i++, o++) {
826 int nblocks = o->ioo_bufcnt * blockpp;
827 int ndindirect = min(nblocks, addrpp + 1);
828 int nindir = nblocks + ndindirect + 1;
830 nbitmaps += nindir + nblocks;
831 ngdblocks += nindir + nblocks;
836 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
837 nbitmaps = EXT3_SB(sb)->s_groups_count;
838 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
839 ngdblocks = EXT3_SB(sb)->s_gdb_count;
841 needed += nbitmaps + ngdblocks;
844 /* We assume that there will be 1 bit set in s_dquot.flags for each
845 * quota file that is active. This is at least true for now.
847 needed += hweight32(sb_any_quota_enabled(sb)) *
848 EXT3_SINGLEDATA_TRANS_BLOCKS;
854 /* We have to start a huge journal transaction here to hold all of the
855 * metadata for the pages being written here. This is necessitated by
856 * the fact that we do lots of prepare_write operations before we do
857 * any of the matching commit_write operations, so even if we split
858 * up to use "smaller" transactions none of them could complete until
859 * all of them were opened. By having a single journal transaction,
860 * we eliminate duplicate reservations for common blocks like the
861 * superblock and group descriptors or bitmaps.
863 * We will start the transaction here, but each prepare_write will
864 * add a refcount to the transaction, and each commit_write will
865 * remove a refcount. The transaction will be closed when all of
866 * the pages have been written.
868 static void *ext3_filter_journal_start(struct filter_obd *filter,
869 int objcount, struct obd_ioobj *obj,
870 int niocount, struct niobuf_remote *nb)
872 journal_t *journal = NULL;
873 handle_t *handle = NULL;
876 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
877 * with having extN built properly before filterobd for now.
879 journal = EXT3_SB(filter->fo_sb)->s_journal;
880 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
882 /* The number of blocks we could _possibly_ dirty can very large.
883 * We reduce our request if it is absurd (and we couldn't get that
884 * many credits for a single handle anyways).
886 * At some point we have to limit the size of I/Os sent at one time,
887 * increase the size of the journal, or we have to calculate the
888 * actual journal requirements more carefully by checking all of
889 * the blocks instead of being maximally pessimistic. It remains to
890 * be seen if this is a real problem or not.
892 if (needed > journal->j_max_transaction_buffers) {
893 CERROR("want too many journal credits (%d) using %d instead\n",
894 needed, journal->j_max_transaction_buffers);
895 needed = journal->j_max_transaction_buffers;
898 handle = journal_start(journal, needed);
900 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
906 static void *filter_journal_start(void **journal_save,
907 struct filter_obd *filter,
908 int objcount, struct obd_ioobj *obj,
909 int niocount, struct niobuf_remote *nb)
913 /* This may not be necessary - we probably never have a
914 * transaction started when we enter here, so we can
915 * remove the saving of the journal state entirely.
916 * For now leave it in just to see if it ever happens.
918 *journal_save = current->journal_info;
920 CERROR("Already have handle %p???\n", *journal_save);
922 current->journal_info = NULL;
925 if (!strcmp(filter->fo_fstype, "ext3") ||
926 !strcmp(filter->fo_fstype, "extN"))
927 handle = ext3_filter_journal_start(filter, objcount, obj,
932 static int ext3_filter_journal_stop(void *handle)
936 /* We got a refcount on the handle for each call to prepare_write,
937 * so we can drop the "parent" handle here to avoid the need for
938 * osc to call back into filterobd to close the handle. The
939 * remaining references will be dropped in commit_write.
941 rc = journal_stop((handle_t *)handle);
946 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
951 if (!strcmp(filter->fo_fstype, "ext3") ||
952 !strcmp(filter->fo_fstype, "extN"))
953 rc = ext3_filter_journal_stop(handle);
956 CERROR("error on journal stop: rc = %d\n", rc);
958 current->journal_info = journal_save;
963 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
964 struct niobuf_local *lnb)
966 struct address_space *mapping = inode->i_mapping;
970 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
971 page = grab_cache_page_nowait(mapping, index); /* locked page */
973 /* This page is currently locked, so get a temporary page instead. */
974 /* XXX I believe this is a very dangerous thing to do - consider if
975 * we had multiple writers for the same file (definitely the case
976 * if we are using this codepath). If writer A locks the page,
977 * writer B writes to a copy (as here), writer A drops the page
978 * lock, and writer C grabs the lock before B does, then B will
979 * later overwrite the data from C, even if C had LDLM locked
980 * and initiated the write after B did.
984 /* debugging: just seeing if this ever happens
985 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
987 CERROR("writing ino %ld page %ld locked\n", inode->i_ino,index);
988 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
990 CERROR("no memory for a temp page\n");
992 GOTO(err, rc = -ENOMEM);
994 page = virt_to_page(addr);
997 lnb->flags |= N_LOCAL_TEMP_PAGE;
998 } else if (!IS_ERR(page)) {
999 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1000 * a no-op for most filesystems, because we write the whole
1001 * 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;
1078 memset(res, 0, sizeof(*res) * niocount);
1079 obd = class_conn2obd(conn);
1081 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1083 if (cmd & OBD_BRW_WRITE) {
1084 *desc_private = filter_journal_start(&journal_save,
1086 objcount, obj, niocount,
1088 if (IS_ERR(*desc_private))
1089 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1092 for (i = 0; i < objcount; i++, o++) {
1093 struct dentry *dentry;
1094 struct inode *inode;
1097 dentry = filter_fid2dentry(obd, filter_parent(obd, S_IFREG),
1098 o->ioo_id, S_IFREG);
1100 GOTO(out_clean, rc = PTR_ERR(dentry));
1101 inode = dentry->d_inode;
1103 CERROR("trying to BRW to non-existent file %Ld\n",
1104 (unsigned long long)o->ioo_id);
1106 GOTO(out_clean, rc = -ENOENT);
1109 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1110 unsigned long index = b->offset >> PAGE_SHIFT;
1116 r->dentry = dget(dentry);
1118 if (cmd & OBD_BRW_WRITE)
1119 page = filter_get_page_write(inode, index, r);
1121 page = lustre_get_page_read(inode, index);
1125 GOTO(out_clean, rc = PTR_ERR(page));
1128 r->addr = page_address(page);
1129 r->offset = b->offset;
1136 if (cmd & OBD_BRW_WRITE) {
1137 int err = filter_journal_stop(journal_save, &obd->u.filter,
1148 if (cmd & OBD_BRW_WRITE)
1149 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1151 lustre_put_page(r->page);
1156 static int filter_write_locked_page(struct niobuf_local *lnb)
1161 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1162 if (IS_ERR(lpage)) {
1163 /* It is highly unlikely that we would ever get an error here.
1164 * The page we want to get was previously locked, so it had to
1165 * have already allocated the space, and we were just writing
1166 * over the same data, so there would be no hole in the file.
1168 * XXX: possibility of a race with truncate could exist, need
1169 * to check that. There are no guarantees w.r.t.
1170 * write order even on a local filesystem, although the
1171 * normal response would be to return the number of bytes
1172 * successfully written and leave the rest to the app.
1174 rc = PTR_ERR(lpage);
1175 CERROR("error getting locked page index %ld: rc = %d\n",
1176 lnb->page->index, rc);
1180 /* lpage is kmapped in lustre_get_page_write() above and kunmapped in
1181 * lustre_commit_write() below, lnb->page was kmapped previously in
1182 * filter_get_page_write() and kunmapped in lustre_put_page() below.
1184 memcpy(page_address(lpage), page_address(lnb->page), PAGE_SIZE);
1185 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1187 CERROR("error committing locked page %ld: rc = %d\n",
1188 lnb->page->index, rc);
1190 lustre_put_page(lnb->page);
1195 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1196 int objcount, struct obd_ioobj *obj,
1197 int niocount, struct niobuf_local *res,
1200 struct obd_run_ctxt saved;
1201 struct obd_ioobj *o;
1202 struct niobuf_local *r;
1203 struct obd_device *obd = class_conn2obd(conn);
1205 int found_locked = 0;
1210 push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1211 journal_save = current->journal_info;
1212 LASSERT(!journal_save);
1214 current->journal_info = private;
1215 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1217 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1218 struct page *page = r->page;
1223 if (r->flags & N_LOCAL_TEMP_PAGE) {
1228 if (cmd & OBD_BRW_WRITE) {
1229 int err = filter_commit_write(page, 0,
1235 lustre_put_page(page);
1240 current->journal_info = journal_save;
1245 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1247 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1249 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1252 err = filter_write_locked_page(r);
1264 static int filter_statfs(struct lustre_handle *conn, struct statfs *statfs)
1266 struct obd_device *obd = class_conn2obd(conn);
1269 RETURN(vfs_statfs(obd->u.filter.fo_sb, statfs));
1272 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1273 void *key, obd_count *vallen, void **val)
1275 struct obd_device *obd;
1276 struct obd_export * export;
1279 if (!(export = class_conn2export(conn))) {
1280 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
1284 obd = class_conn2obd(conn);
1286 if ( keylen == strlen("blocksize") &&
1287 memcmp(key, "blocksize", keylen) == 0 ) {
1288 *vallen = sizeof(long);
1289 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1293 if ( keylen == strlen("blocksize_bits") &&
1294 memcmp(key, "blocksize_bits", keylen) == 0 ){
1295 *vallen = sizeof(long);
1296 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1300 if ( keylen == strlen("root_ino") &&
1301 memcmp(key, "root_ino", keylen) == 0 ){
1302 *vallen = sizeof(obd_id);
1303 *val = (void *)(obd_id)FILTER_ROOTINO;
1307 CDEBUG(D_IOCTL, "invalid key\n");
1311 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1312 struct lustre_handle *src_conn, struct obdo *src,
1313 obd_size count, obd_off offset)
1316 struct lov_stripe_md srcmd, dstmd;
1317 unsigned long index = 0;
1320 memset(&srcmd, 0, sizeof(srcmd));
1321 memset(&dstmd, 0, sizeof(dstmd));
1322 srcmd.lmd_object_id = src->o_id;
1323 dstmd.lmd_object_id = dst->o_id;
1326 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
1327 (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
1328 (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
1329 page = alloc_page(GFP_USER);
1333 while (TryLockPage(page))
1334 ___wait_on_page(page);
1336 /* XXX with brw vector I/O, we could batch up reads and writes here,
1337 * all we need to do is allocate multiple pages to handle the I/Os
1338 * and arrays to handle the request parameters.
1340 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1342 struct io_cb_data *cbd = ll_init_cb();
1351 pg.count = PAGE_SIZE;
1352 pg.off = (page->index) << PAGE_SHIFT;
1355 page->index = index;
1356 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &pg,
1357 ll_sync_io_cb, cbd);
1370 pg.flag = OBD_BRW_CREATE;
1371 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1373 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &pg,
1374 ll_sync_io_cb, cbd);
1376 /* XXX should handle dst->o_size, dst->o_blocks here */
1382 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1386 dst->o_size = src->o_size;
1387 dst->o_blocks = src->o_blocks;
1388 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
1396 static struct obd_ops filter_obd_ops = {
1397 o_get_info: filter_get_info,
1398 o_setup: filter_setup,
1399 o_cleanup: filter_cleanup,
1400 o_connect: filter_connect,
1401 o_disconnect: filter_disconnect,
1402 o_statfs: filter_statfs,
1403 o_getattr: filter_getattr,
1404 o_create: filter_create,
1405 o_setattr: filter_setattr,
1406 o_destroy: filter_destroy,
1407 o_open: filter_open,
1408 o_close: filter_close,
1409 o_brw: filter_pgcache_brw,
1410 o_punch: filter_truncate,
1411 o_preprw: filter_preprw,
1412 o_commitrw: filter_commitrw
1414 o_preallocate: filter_preallocate_inodes,
1415 o_migrate: filter_migrate,
1416 o_copy: filter_copy_data,
1417 o_iterate: filter_iterate
1422 static int __init obdfilter_init(void)
1424 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1425 return class_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1428 static void __exit obdfilter_exit(void)
1430 class_unregister_type(OBD_FILTER_DEVICENAME);
1433 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1434 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1435 MODULE_LICENSE("GPL");
1437 module_init(obdfilter_init);
1438 module_exit(obdfilter_exit);