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/obd_class.h>
21 #include <linux/lustre_dlm.h>
22 #include <linux/obd_filter.h>
23 #include <linux/ext3_jbd.h>
24 #include <linux/extN_jbd.h>
25 #include <linux/quotaops.h>
26 #include <linux/init.h>
27 #include <linux/stringify.h>
31 #define FILTER_ROOTINO 2
32 #define FILTER_ROOTINO_STR __stringify(FILTER_ROOTINO)
35 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
37 [S_IFREG >> S_SHIFT] "R",
38 [S_IFDIR >> S_SHIFT] "D",
39 [S_IFCHR >> S_SHIFT] "C",
40 [S_IFBLK >> S_SHIFT] "B",
41 [S_IFIFO >> S_SHIFT] "F",
42 [S_IFSOCK >> S_SHIFT] "S",
43 [S_IFLNK >> S_SHIFT] "L"
46 static inline const char *obd_mode_to_type(int mode)
48 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
51 /* write the pathname into the string */
52 static int filter_id(char *buf, obd_id id, obd_mode mode)
54 return sprintf(buf, "O/%s/"LPU64, obd_mode_to_type(mode), id);
57 static inline void f_dput(struct dentry *dentry)
59 CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
60 dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
61 LASSERT(atomic_read(&dentry->d_count) > 0);
65 /* setup the object store with correct subdirectories */
66 static int filter_prep(struct obd_device *obddev)
68 struct obd_run_ctxt saved;
69 struct filter_obd *filter = &obddev->u.filter;
70 struct dentry *dentry;
78 push_ctxt(&saved, &filter->fo_ctxt, NULL);
79 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
80 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
83 CERROR("cannot open/create O: rc = %d\n", rc);
86 filter->fo_dentry_O = dentry;
87 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
88 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
91 CERROR("cannot open/create P: rc = %d\n", rc);
95 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
96 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
99 CERROR("cannot open/create D: rc = %d\n", rc);
103 root = simple_mknod(dentry, FILTER_ROOTINO_STR, S_IFREG | 0755);
107 CERROR("OBD filter: cannot open/create root %d: rc = %d\n",
114 * Create directories and/or get dentries for each object type.
115 * This saves us from having to do multiple lookups for each one.
117 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
118 char *type = obd_type_by_mode[mode];
121 filter->fo_dentry_O_mode[mode] = NULL;
124 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
125 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
126 if (IS_ERR(dentry)) {
127 rc = PTR_ERR(dentry);
128 CERROR("cannot create O/%s: rc = %d\n", type, rc);
129 GOTO(out_O_mode, rc);
131 filter->fo_dentry_O_mode[mode] = dentry;
134 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
135 if ( !file || IS_ERR(file) ) {
137 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
139 GOTO(out_O_mode, rc);
142 /* steal operations */
143 inode = file->f_dentry->d_inode;
144 filter->fo_fop = file->f_op;
145 filter->fo_iop = inode->i_op;
146 filter->fo_aops = inode->i_mapping->a_ops;
148 if (inode->i_size == 0) {
149 __u64 disk_lastino = cpu_to_le64(lastino);
150 ssize_t retval = file->f_op->write(file, (char *)&disk_lastino,
151 sizeof(disk_lastino),
153 if (retval != sizeof(disk_lastino)) {
154 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
156 GOTO(out_O_mode, rc = -EIO);
160 ssize_t retval = file->f_op->read(file, (char *)&disk_lastino,
161 sizeof(disk_lastino),
163 if (retval != sizeof(disk_lastino)) {
164 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
166 GOTO(out_O_mode, rc = -EIO);
168 lastino = le64_to_cpu(disk_lastino);
170 filter->fo_lastino = lastino;
181 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
184 filter->fo_dentry_O_mode[mode] = NULL;
188 f_dput(filter->fo_dentry_O);
189 filter->fo_dentry_O = NULL;
193 /* cleanup the filter: write last used object id to status file */
194 static void filter_post(struct obd_device *obddev)
196 struct obd_run_ctxt saved;
197 struct filter_obd *filter = &obddev->u.filter;
203 push_ctxt(&saved, &filter->fo_ctxt, NULL);
204 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
206 CERROR("OBD filter: cannot create status file\n");
211 disk_lastino = cpu_to_le64(filter->fo_lastino);
212 rc = file->f_op->write(file, (char *)&disk_lastino,
213 sizeof(disk_lastino), &file->f_pos);
214 if (rc != sizeof(disk_lastino))
215 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
217 rc = filp_close(file, NULL);
219 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
221 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
222 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
225 filter->fo_dentry_O_mode[mode] = NULL;
228 f_dput(filter->fo_dentry_O);
234 static __u64 filter_next_id(struct obd_device *obddev)
238 spin_lock(&obddev->u.filter.fo_lock);
239 id = ++obddev->u.filter.fo_lastino;
240 spin_unlock(&obddev->u.filter.fo_lock);
242 /* FIXME: write the lastino to disk here */
246 /* how to get files, dentries, inodes from object id's */
247 /* parent i_sem is already held if needed for exclusivity */
248 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
249 struct dentry *dparent,
250 __u64 id, __u32 type)
252 struct super_block *sb = obddev->u.filter.fo_sb;
253 struct dentry *dchild;
258 if (!sb || !sb->s_dev) {
259 CERROR("fatal: device not initialized.\n");
260 RETURN(ERR_PTR(-ENXIO));
264 CERROR("fatal: invalid object #0\n");
266 RETURN(ERR_PTR(-ESTALE));
269 if (!(type & S_IFMT)) {
270 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
271 __FUNCTION__, id, type);
272 RETURN(ERR_PTR(-EINVAL));
275 len = sprintf(name, LPU64, id);
276 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
278 dchild = lookup_one_len(name, dparent, len);
279 if (IS_ERR(dchild)) {
280 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
284 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
285 obd_mode_to_type(type), name, dchild,
286 atomic_read(&dchild->d_count));
288 LASSERT(atomic_read(&dchild->d_count) > 0);
293 static struct file *filter_obj_open(struct obd_device *obddev,
294 __u64 id, __u32 type)
296 struct super_block *sb = obddev->u.filter.fo_sb;
297 struct obd_run_ctxt saved;
302 if (!sb || !sb->s_dev) {
303 CERROR("fatal: device not initialized.\n");
304 RETURN(ERR_PTR(-ENXIO));
308 CERROR("fatal: invalid obdo "LPU64"\n", id);
309 RETURN(ERR_PTR(-ESTALE));
312 if (!(type & S_IFMT)) {
313 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
314 __FUNCTION__, id, type);
315 RETURN(ERR_PTR(-EINVAL));
318 filter_id(name, id, type);
319 push_ctxt(&saved, &obddev->u.filter.fo_ctxt, NULL);
320 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
323 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
330 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
332 struct filter_obd *filter = &obddev->u.filter;
334 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
339 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
340 obd_uuid_t cluuid, struct recovd_obd *recovd,
341 ptlrpc_recovery_cb_t recover)
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)
406 ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
407 "filter_ldlm_client", &obddev->obd_ldlm_client);
412 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;
489 if (!class_conn2export(conn)) {
490 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
494 obddev = class_conn2obd(conn);
495 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
496 oa->o_id, oa->o_mode);
498 RETURN(PTR_ERR(dentry));
500 if (!dentry->d_inode) {
501 CERROR("getattr on non-existent object: "LPU64"\n", oa->o_id);
502 GOTO(out_getattr, rc = -ENOENT);
505 filter_from_inode(oa, dentry->d_inode, oa->o_valid);
512 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
513 struct lov_stripe_md *md)
515 struct obd_run_ctxt saved;
516 struct obd_device *obd = class_conn2obd(conn);
517 struct dentry *dentry;
523 iattr_from_obdo(&iattr, oa, oa->o_valid);
524 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
525 dentry = filter_fid2dentry(obd, filter_parent(obd, iattr.ia_mode),
526 oa->o_id, iattr.ia_mode);
528 RETURN(PTR_ERR(dentry));
530 inode = dentry->d_inode;
532 CERROR("setattr on non-existent object: "LPU64"\n", oa->o_id);
533 GOTO(out_setattr, rc = -ENOENT);
537 if (iattr.ia_valid & ATTR_SIZE)
539 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
540 if (inode->i_op->setattr)
541 rc = inode->i_op->setattr(dentry, &iattr);
543 rc = inode_setattr(inode, &iattr);
545 if (iattr.ia_valid & ATTR_SIZE) {
547 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
548 obdo_from_inode(oa, inode, oa->o_valid);
557 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
558 struct lov_stripe_md *ea)
560 struct obd_export *export;
561 struct obd_device *obd;
562 struct dentry *dentry;
565 export = class_conn2export(conn);
567 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
571 obd = export->exp_obd;
572 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
573 oa->o_id, oa->o_mode);
575 RETURN(PTR_ERR(dentry));
577 if (!dentry->d_inode) {
578 CERROR("opening non-existent objid "LPX64"\n", oa->o_id);
582 filter_from_inode(oa, dentry->d_inode, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
583 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
588 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
589 struct lov_stripe_md *ea)
591 struct obd_device *obd;
592 struct dentry *dentry;
595 obd = class_conn2obd(conn);
597 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
601 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
602 oa->o_id, oa->o_mode);
604 RETURN(PTR_ERR(dentry));
605 LASSERT(atomic_read(&dentry->d_count) > 1);
607 f_dput(dentry); /* for the close */
608 f_dput(dentry); /* for this call */
612 static int filter_create(struct lustre_handle* conn, struct obdo *oa,
613 struct lov_stripe_md **ea)
616 struct obd_run_ctxt saved;
619 struct obd_device *obd = class_conn2obd(conn);
620 struct filter_obd *filter = &obd->u.filter;
625 CERROR("invalid client "LPX64"\n", conn->addr);
629 if (!(oa->o_mode && S_IFMT)) {
630 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
631 __FUNCTION__, oa->o_id, oa->o_mode);
635 oa->o_id = filter_next_id(obd);
637 //filter_id(name, oa->o_id, oa->o_mode);
638 sprintf(name, LPU64, oa->o_id);
639 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
640 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
641 new = simple_mknod(filter->fo_dentry_O_mode[S_IFREG >> S_SHIFT], name,
645 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(new));
649 /* Set flags for fields we have set in the inode struct */
650 oa->o_valid = OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
651 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME;
652 filter_from_inode(oa, new->d_inode, oa->o_valid);
658 static int filter_destroy(struct lustre_handle *conn, struct obdo *oa,
659 struct lov_stripe_md *ea)
661 struct obd_device *obd;
662 struct filter_obd *filter;
663 struct obd_run_ctxt saved;
665 struct dentry *dir_dentry, *object_dentry;
669 obd = class_conn2obd(conn);
671 CERROR("invalid client "LPX64"\n", conn->addr);
675 CDEBUG(D_INODE, "destroying object "LPD64"\n", oa->o_id);
677 dir_dentry = filter_parent(obd, oa->o_mode);
678 down(&dir_dentry->d_inode->i_sem);
680 object_dentry = filter_fid2dentry(obd, dir_dentry, oa->o_id,
682 if (IS_ERR(object_dentry))
683 GOTO(out, rc = -ENOENT);
685 inode = object_dentry->d_inode;
687 CERROR("trying to destroy negative inode "LPX64"!\n", oa->o_id);
688 GOTO(out, rc = -ENOENT);
691 if (inode->i_nlink != 1) {
692 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
696 inode->i_mode = S_IFREG;
698 if (atomic_read(&inode->i_count) > 1) {
699 #warning FIXME: need to handle open-unlinked case and move to hold dir
700 CERROR("inode has count %d\n", atomic_read(&inode->i_count));
703 filter = &obd->u.filter;
704 push_ctxt(&saved, &filter->fo_ctxt, NULL);
706 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
708 f_dput(object_dentry);
712 up(&dir_dentry->d_inode->i_sem);
716 /* NB count and offset are used for punch, but not truncate */
717 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
718 struct lov_stripe_md *md,
719 obd_off start, obd_off end)
724 if (end != OBD_OBJECT_EOF)
725 CERROR("PUNCH not supported, only truncate works\n");
727 CDEBUG(D_INODE, "calling truncate for object "LPX64", valid = %x, "
728 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
730 error = filter_setattr(conn, oa, NULL);
734 static int filter_pgcache_brw(int cmd, struct lustre_handle *conn,
735 struct lov_stripe_md *lsm, obd_count oa_bufs,
736 struct brw_page *pga, brw_callback_t callback,
737 struct io_cb_data *data)
739 struct obd_run_ctxt saved;
740 struct super_block *sb;
741 int pnum; /* index to pages (bufs) */
742 unsigned long retval;
745 struct obd_device *obd = class_conn2obd(conn);
750 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
754 sb = obd->u.filter.fo_sb;
755 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
756 pnum = 0; /* pnum indexes buf 0..num_pages */
758 file = filter_obj_open(obd, lsm->lsm_object_id, S_IFREG);
760 GOTO(out, retval = PTR_ERR(file));
762 /* count doubles as retval */
763 for (pg = 0; pg < oa_bufs; pg++) {
764 CDEBUG(D_INODE, "OP %d obdo pgno: (%d) (%ld,"LPU64
765 ") off count ("LPU64",%d)\n",
766 cmd, pnum, file->f_dentry->d_inode->i_ino,
767 pga[pnum].off >> PAGE_CACHE_SHIFT, pga[pnum].off,
768 (int)pga[pnum].count);
769 if (cmd & OBD_BRW_WRITE) {
773 buffer = kmap(pga[pnum].pg);
774 retval = file->f_op->write(file, buffer,
777 kunmap(pga[pnum].pg);
778 CDEBUG(D_INODE, "retval %ld\n", retval);
780 loff_t off = pga[pnum].off;
781 char *buffer = kmap(pga[pnum].pg);
783 if (off >= file->f_dentry->d_inode->i_size) {
784 memset(buffer, 0, pga[pnum].count);
785 retval = pga[pnum].count;
787 retval = file->f_op->read(file, buffer,
788 pga[pnum].count, &off);
790 kunmap(pga[pnum].pg);
792 if (retval != pga[pnum].count) {
794 GOTO(out, retval = -EIO);
796 CDEBUG(D_INODE, "retval %ld\n", retval);
800 /* sizes and blocks are set by generic_file_write */
801 /* ctimes/mtimes will follow with a setattr call */
804 /* XXX: do something with callback if it is set? */
809 error = (retval >= 0) ? 0 : retval;
814 * Calculate the number of buffer credits needed to write multiple pages in
815 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
816 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
818 * See comment above ext3_writepage_trans_blocks for details. We assume
819 * no data journaling is being done, but it does allow for all of the pages
820 * being non-contiguous. If we are guaranteed contiguous pages we could
821 * reduce the number of (d)indirect blocks a lot.
823 * With N blocks per page and P pages, for each inode we have at most:
825 * min(N*P, blocksize/4 + 1) dindirect blocks
828 * For the entire filesystem, we have at most:
829 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
830 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
833 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
835 static int ext3_credits_needed(struct super_block *sb, int objcount,
836 struct obd_ioobj *obj)
838 struct obd_ioobj *o = obj;
839 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
840 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
843 int needed = objcount + 1;
846 for (i = 0; i < objcount; i++, o++) {
847 int nblocks = o->ioo_bufcnt * blockpp;
848 int ndindirect = min(nblocks, addrpp + 1);
849 int nindir = nblocks + ndindirect + 1;
851 nbitmaps += nindir + nblocks;
852 ngdblocks += nindir + nblocks;
857 /* Assumes ext3 and extN have same sb_info layout at the start. */
858 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
859 nbitmaps = EXT3_SB(sb)->s_groups_count;
860 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
861 ngdblocks = EXT3_SB(sb)->s_gdb_count;
863 needed += nbitmaps + ngdblocks;
866 /* We assume that there will be 1 bit set in s_dquot.flags for each
867 * quota file that is active. This is at least true for now.
869 needed += hweight32(sb_any_quota_enabled(sb)) *
870 EXT3_SINGLEDATA_TRANS_BLOCKS;
876 /* We have to start a huge journal transaction here to hold all of the
877 * metadata for the pages being written here. This is necessitated by
878 * the fact that we do lots of prepare_write operations before we do
879 * any of the matching commit_write operations, so even if we split
880 * up to use "smaller" transactions none of them could complete until
881 * all of them were opened. By having a single journal transaction,
882 * we eliminate duplicate reservations for common blocks like the
883 * superblock and group descriptors or bitmaps.
885 * We will start the transaction here, but each prepare_write will
886 * add a refcount to the transaction, and each commit_write will
887 * remove a refcount. The transaction will be closed when all of
888 * the pages have been written.
890 static void *ext3_filter_journal_start(struct filter_obd *filter,
891 int objcount, struct obd_ioobj *obj,
892 int niocount, struct niobuf_remote *nb)
894 journal_t *journal = NULL;
895 handle_t *handle = NULL;
898 /* It appears that some kernels have different values for
899 * EXT*_MAX_GROUP_LOADED (either 8 or 32), so we cannot
900 * assume anything after s_inode_bitmap_number is the same.
902 if (!strcmp(filter->fo_fstype, "ext3"))
903 journal = EXT3_SB(filter->fo_sb)->s_journal;
904 else if (!strcmp(filter->fo_fstype, "extN"))
905 journal = EXTN_SB(filter->fo_sb)->s_journal;
906 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
908 /* The number of blocks we could _possibly_ dirty can very large.
909 * We reduce our request if it is absurd (and we couldn't get that
910 * many credits for a single handle anyways).
912 * At some point we have to limit the size of I/Os sent at one time,
913 * increase the size of the journal, or we have to calculate the
914 * actual journal requirements more carefully by checking all of
915 * the blocks instead of being maximally pessimistic. It remains to
916 * be seen if this is a real problem or not.
918 if (needed > journal->j_max_transaction_buffers) {
919 CERROR("want too many journal credits (%d) using %d instead\n",
920 needed, journal->j_max_transaction_buffers);
921 needed = journal->j_max_transaction_buffers;
925 handle = journal_start(journal, needed);
928 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
934 static void *filter_journal_start(void **journal_save,
935 struct filter_obd *filter,
936 int objcount, struct obd_ioobj *obj,
937 int niocount, struct niobuf_remote *nb)
941 /* This may not be necessary - we probably never have a
942 * transaction started when we enter here, so we can
943 * remove the saving of the journal state entirely.
944 * For now leave it in just to see if it ever happens.
946 *journal_save = current->journal_info;
948 CERROR("Already have handle %p???\n", *journal_save);
950 current->journal_info = NULL;
953 if (!strcmp(filter->fo_fstype, "ext3") ||
954 !strcmp(filter->fo_fstype, "extN"))
955 handle = ext3_filter_journal_start(filter, objcount, obj,
960 static int ext3_filter_journal_stop(void *handle)
964 /* We got a refcount on the handle for each call to prepare_write,
965 * so we can drop the "parent" handle here to avoid the need for
966 * osc to call back into filterobd to close the handle. The
967 * remaining references will be dropped in commit_write.
970 rc = journal_stop((handle_t *)handle);
976 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
981 if (!strcmp(filter->fo_fstype, "ext3") ||
982 !strcmp(filter->fo_fstype, "extN"))
983 rc = ext3_filter_journal_stop(handle);
986 CERROR("error on journal stop: rc = %d\n", rc);
988 current->journal_info = journal_save;
993 static inline void lustre_put_page(struct page *page)
996 page_cache_release(page);
1000 lustre_get_page_read(struct inode *inode, unsigned long index)
1002 struct address_space *mapping = inode->i_mapping;
1006 page = read_cache_page(mapping, index,
1007 (filler_t*)mapping->a_ops->readpage, NULL);
1008 if (!IS_ERR(page)) {
1011 if (!Page_Uptodate(page)) {
1012 CERROR("page index %lu not uptodate\n", index);
1013 GOTO(err_page, rc = -EIO);
1015 if (PageError(page)) {
1016 CERROR("page index %lu has error\n", index);
1017 GOTO(err_page, rc = -EIO);
1023 lustre_put_page(page);
1027 static struct page *
1028 lustre_get_page_write(struct inode *inode, unsigned long index)
1030 struct address_space *mapping = inode->i_mapping;
1034 page = grab_cache_page(mapping, index); /* locked page */
1036 if (!IS_ERR(page)) {
1038 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1039 * a no-op for most filesystems, because we write the whole
1040 * page. For partial-page I/O this will read in the page.
1042 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1044 CERROR("page index %lu, rc = %d\n", index, rc);
1047 GOTO(err_unlock, rc);
1049 /* XXX not sure if we need this if we are overwriting page */
1050 if (PageError(page)) {
1051 CERROR("error on page index %lu, rc = %d\n", index, rc);
1053 GOTO(err_unlock, rc = -EIO);
1060 lustre_put_page(page);
1064 static int lustre_commit_write(struct page *page, unsigned from, unsigned to)
1066 struct inode *inode = page->mapping->host;
1069 err = page->mapping->a_ops->commit_write(NULL, page, from, to);
1070 if (!err && IS_SYNC(inode))
1071 err = waitfor_one_page(page);
1073 //SetPageUptodate(page); // the client commit_write will do this
1075 SetPageReferenced(page);
1077 lustre_put_page(page);
1081 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1082 struct niobuf_local *lnb, int *pglocked)
1084 struct address_space *mapping = inode->i_mapping;
1088 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1090 page = grab_cache_page_nowait(mapping, index); /* locked page */
1092 page = grab_cache_page(mapping, index); /* locked page */
1095 /* This page is currently locked, so get a temporary page instead. */
1096 /* XXX I believe this is a very dangerous thing to do - consider if
1097 * we had multiple writers for the same file (definitely the case
1098 * if we are using this codepath). If writer A locks the page,
1099 * writer B writes to a copy (as here), writer A drops the page
1100 * lock, and writer C grabs the lock before B does, then B will
1101 * later overwrite the data from C, even if C had LDLM locked
1102 * and initiated the write after B did.
1106 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
1107 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1109 CERROR("no memory for a temp page\n");
1111 GOTO(err, rc = -ENOMEM);
1113 page = virt_to_page(addr);
1115 page->index = index;
1116 lnb->flags |= N_LOCAL_TEMP_PAGE;
1117 } else if (!IS_ERR(page)) {
1121 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1122 * a no-op for most filesystems, because we write the whole
1123 * page. For partial-page I/O this will read in the page.
1125 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1127 CERROR("page index %lu, rc = %d\n", index, rc);
1130 GOTO(err_unlock, rc);
1132 /* XXX not sure if we need this if we are overwriting page */
1133 if (PageError(page)) {
1134 CERROR("error on page index %lu, rc = %d\n", index, rc);
1136 GOTO(err_unlock, rc = -EIO);
1143 lustre_put_page(page);
1149 * We need to balance prepare_write() calls with commit_write() calls.
1150 * If the page has been prepared, but we have no data for it, we don't
1151 * want to overwrite valid data on disk, but we still need to zero out
1152 * data for space which was newly allocated. Like part of what happens
1153 * in __block_prepare_write() for newly allocated blocks.
1155 * XXX currently __block_prepare_write() creates buffers for all the
1156 * pages, and the filesystems mark these buffers as BH_New if they
1157 * were newly allocated from disk. We use the BH_New flag similarly.
1159 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1163 unsigned block_start, block_end;
1164 struct buffer_head *bh, *head = page->buffers;
1165 unsigned blocksize = head->b_size;
1166 void *addr = page_address(page);
1168 /* debugging: just seeing if this ever happens */
1169 CERROR("called filter_commit_write for obj %ld:%ld on err %d\n",
1170 page->index, page->mapping->host->i_ino, err);
1172 /* Currently one buffer per page, but in the future... */
1173 for (bh = head, block_start = 0; bh != head || !block_start;
1174 block_start = block_end, bh = bh->b_this_page) {
1175 block_end = block_start + blocksize;
1177 memset(addr + block_start, 0, blocksize);
1181 return lustre_commit_write(page, from, to);
1184 static int filter_preprw(int cmd, struct lustre_handle *conn,
1185 int objcount, struct obd_ioobj *obj,
1186 int niocount, struct niobuf_remote *nb,
1187 struct niobuf_local *res, void **desc_private)
1189 struct obd_run_ctxt saved;
1190 struct obd_device *obd;
1191 struct obd_ioobj *o = obj;
1192 struct niobuf_remote *b = nb;
1193 struct niobuf_local *r = res;
1194 void *journal_save = NULL;
1200 obd = class_conn2obd(conn);
1202 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1205 memset(res, 0, sizeof(*res) * niocount);
1207 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
1209 if (cmd & OBD_BRW_WRITE) {
1210 *desc_private = filter_journal_start(&journal_save,
1212 objcount, obj, niocount,
1214 if (IS_ERR(*desc_private))
1215 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1218 for (i = 0; i < objcount; i++, o++) {
1219 struct dentry *dentry;
1220 struct inode *inode;
1223 dentry = filter_fid2dentry(obd, filter_parent(obd, S_IFREG),
1224 o->ioo_id, S_IFREG);
1226 GOTO(out_clean, rc = PTR_ERR(dentry));
1227 inode = dentry->d_inode;
1229 CERROR("trying to BRW to non-existent file "LPU64"\n",
1232 GOTO(out_clean, rc = -ENOENT);
1235 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1236 unsigned long index = b->offset >> PAGE_SHIFT;
1242 r->dentry = dget(dentry);
1244 if (cmd & OBD_BRW_WRITE)
1245 page = filter_get_page_write(inode, index, r,
1248 page = lustre_get_page_read(inode, index);
1252 GOTO(out_clean, rc = PTR_ERR(page));
1255 r->addr = page_address(page);
1256 r->offset = b->offset;
1263 if (cmd & OBD_BRW_WRITE) {
1264 int err = filter_journal_stop(journal_save, &obd->u.filter,
1274 CERROR("error cleanup on brw\n");
1276 if (cmd & OBD_BRW_WRITE)
1277 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1279 lustre_put_page(r->page);
1284 static int filter_write_locked_page(struct niobuf_local *lnb)
1289 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1290 if (IS_ERR(lpage)) {
1291 /* It is highly unlikely that we would ever get an error here.
1292 * The page we want to get was previously locked, so it had to
1293 * have already allocated the space, and we were just writing
1294 * over the same data, so there would be no hole in the file.
1296 * XXX: possibility of a race with truncate could exist, need
1297 * to check that. There are no guarantees w.r.t.
1298 * write order even on a local filesystem, although the
1299 * normal response would be to return the number of bytes
1300 * successfully written and leave the rest to the app.
1302 rc = PTR_ERR(lpage);
1303 CERROR("error getting locked page index %ld: rc = %d\n",
1304 lnb->page->index, rc);
1308 /* lpage is kmapped in lustre_get_page_write() above and kunmapped in
1309 * lustre_commit_write() below, lnb->page was kmapped previously in
1310 * filter_get_page_write() and kunmapped in lustre_put_page() below.
1312 memcpy(page_address(lpage), page_address(lnb->page), PAGE_SIZE);
1313 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1315 CERROR("error committing locked page %ld: rc = %d\n",
1316 lnb->page->index, rc);
1318 lustre_put_page(lnb->page);
1323 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1324 int objcount, struct obd_ioobj *obj,
1325 int niocount, struct niobuf_local *res,
1328 struct obd_run_ctxt saved;
1329 struct obd_ioobj *o;
1330 struct niobuf_local *r;
1331 struct obd_device *obd = class_conn2obd(conn);
1333 int found_locked = 0;
1338 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
1339 journal_save = current->journal_info;
1340 LASSERT(!journal_save);
1342 current->journal_info = private;
1343 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1345 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1346 struct page *page = r->page;
1351 if (r->flags & N_LOCAL_TEMP_PAGE) {
1356 if (cmd & OBD_BRW_WRITE) {
1357 int err = filter_commit_write(page, 0,
1363 lustre_put_page(page);
1368 current->journal_info = journal_save;
1373 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1375 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1377 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1380 err = filter_write_locked_page(r);
1392 static int filter_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
1394 struct obd_device *obd = class_conn2obd(conn);
1399 rc = vfs_statfs(obd->u.filter.fo_sb, &sfs);
1401 statfs_pack(osfs, &sfs);
1406 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1407 void *key, obd_count *vallen, void **val)
1409 struct obd_device *obd;
1412 obd = class_conn2obd(conn);
1414 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1418 if ( keylen == strlen("blocksize") &&
1419 memcmp(key, "blocksize", keylen) == 0 ) {
1420 *vallen = sizeof(long);
1421 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1425 if ( keylen == strlen("blocksize_bits") &&
1426 memcmp(key, "blocksize_bits", keylen) == 0 ){
1427 *vallen = sizeof(long);
1428 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1432 if ( keylen == strlen("root_ino") &&
1433 memcmp(key, "root_ino", keylen) == 0 ){
1434 *vallen = sizeof(obd_id);
1435 *val = (void *)(obd_id)FILTER_ROOTINO;
1439 CDEBUG(D_IOCTL, "invalid key\n");
1443 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1444 struct lustre_handle *src_conn, struct obdo *src,
1445 obd_size count, obd_off offset)
1448 struct lov_stripe_md srcmd, dstmd;
1449 unsigned long index = 0;
1452 memset(&srcmd, 0, sizeof(srcmd));
1453 memset(&dstmd, 0, sizeof(dstmd));
1454 srcmd.lsm_object_id = src->o_id;
1455 dstmd.lsm_object_id = dst->o_id;
1458 CDEBUG(D_INFO, "src: ino "LPU64" blocks "LPU64", size "LPU64
1459 ", dst: ino "LPU64"\n",
1460 src->o_id, src->o_blocks, src->o_size, dst->o_id);
1461 page = alloc_page(GFP_USER);
1465 while (TryLockPage(page))
1466 ___wait_on_page(page);
1468 /* XXX with brw vector I/O, we could batch up reads and writes here,
1469 * all we need to do is allocate multiple pages to handle the I/Os
1470 * and arrays to handle the request parameters.
1472 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1474 struct io_cb_data *cbd = ll_init_cb();
1483 pg.count = PAGE_SIZE;
1484 pg.off = (page->index) << PAGE_SHIFT;
1487 page->index = index;
1488 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &pg,
1489 ll_sync_io_cb, cbd);
1502 pg.flag = OBD_BRW_CREATE;
1503 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1505 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &pg,
1506 ll_sync_io_cb, cbd);
1508 /* XXX should handle dst->o_size, dst->o_blocks here */
1514 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1518 dst->o_size = src->o_size;
1519 dst->o_blocks = src->o_blocks;
1520 dst->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1528 static struct obd_ops filter_obd_ops = {
1529 o_get_info: filter_get_info,
1530 o_setup: filter_setup,
1531 o_cleanup: filter_cleanup,
1532 o_connect: filter_connect,
1533 o_disconnect: filter_disconnect,
1534 o_statfs: filter_statfs,
1535 o_getattr: filter_getattr,
1536 o_create: filter_create,
1537 o_setattr: filter_setattr,
1538 o_destroy: filter_destroy,
1539 o_open: filter_open,
1540 o_close: filter_close,
1541 o_brw: filter_pgcache_brw,
1542 o_punch: filter_truncate,
1543 o_preprw: filter_preprw,
1544 o_commitrw: filter_commitrw
1546 o_preallocate: filter_preallocate_inodes,
1547 o_migrate: filter_migrate,
1548 o_copy: filter_copy_data,
1549 o_iterate: filter_iterate
1554 static int __init obdfilter_init(void)
1556 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1557 return class_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1560 static void __exit obdfilter_exit(void)
1562 class_unregister_type(OBD_FILTER_DEVICENAME);
1565 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1566 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1567 MODULE_LICENSE("GPL");
1569 module_init(obdfilter_init);
1570 module_exit(obdfilter_exit);