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/obd_filter.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/quotaops.h>
22 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
25 #define FILTER_ROOTINO 2
28 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
30 [S_IFREG >> S_SHIFT] "R",
31 [S_IFDIR >> S_SHIFT] "D",
32 [S_IFCHR >> S_SHIFT] "C",
33 [S_IFBLK >> S_SHIFT] "B",
34 [S_IFIFO >> S_SHIFT] "F",
35 [S_IFSOCK >> S_SHIFT] "S",
36 [S_IFLNK >> S_SHIFT] "L"
39 static inline const char *obd_mode_to_type(int mode)
41 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
44 /* write the pathname into the string */
45 static int filter_id(char *buf, obd_id id, obd_mode mode)
47 return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
48 (unsigned long long)id);
51 /* setup the object store with correct subdirectories */
52 static int filter_prep(struct obd_device *obddev)
54 struct obd_run_ctxt saved;
55 struct filter_obd *filter = &obddev->u.filter;
56 struct dentry *dentry;
65 push_ctxt(&saved, &filter->fo_ctxt);
66 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
67 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
70 CERROR("cannot open/create O: rc = %d\n", rc);
73 filter->fo_dentry_O = dentry;
74 dentry = simple_mkdir(current->fs->pwd, "P", 0700);
75 CDEBUG(D_INODE, "got/created P: %p\n", dentry);
78 CERROR("cannot open/create P: rc = %d\n", rc);
81 CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
82 atomic_read(&dentry->d_count) - 1);
84 dentry = simple_mkdir(current->fs->pwd, "D", 0700);
85 CDEBUG(D_INODE, "got/created D: %p\n", dentry);
88 CERROR("cannot open/create D: rc = %d\n", rc);
91 CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
92 atomic_read(&dentry->d_count) - 1);
96 * Create directories and/or get dentries for each object type.
97 * This saves us from having to do multiple lookups for each one.
99 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
100 char *type = obd_type_by_mode[mode];
103 filter->fo_dentry_O_mode[mode] = NULL;
106 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
107 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
108 if (IS_ERR(dentry)) {
109 rc = PTR_ERR(dentry);
110 CERROR("cannot create O/%s: rc = %d\n", type, rc);
111 GOTO(out_O_mode, rc);
113 filter->fo_dentry_O_mode[mode] = dentry;
116 filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
117 file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
120 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
122 GOTO(out_O_mode, rc);
126 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
127 if ( !file || IS_ERR(file) ) {
129 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
131 GOTO(out_O_mode, rc);
134 /* steal operations */
135 inode = file->f_dentry->d_inode;
136 filter->fo_fop = file->f_op;
137 filter->fo_iop = inode->i_op;
138 filter->fo_aops = inode->i_mapping->a_ops;
141 if (inode->i_size == 0) {
142 ssize_t retval = file->f_op->write(file, (char *)&lastino,
143 sizeof(lastino), &off);
144 if (retval != sizeof(lastino)) {
145 CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
147 GOTO(out_O_mode, rc = -EIO);
150 ssize_t retval = file->f_op->read(file, (char *)&lastino,
151 sizeof(lastino), &off);
152 if (retval != sizeof(lastino)) {
153 CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
155 GOTO(out_O_mode, rc = -EIO);
158 filter->fo_lastino = lastino;
169 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
171 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
172 obd_type_by_mode[mode], dentry,
173 atomic_read(&dentry->d_count) - 1);
175 filter->fo_dentry_O_mode[mode] = NULL;
179 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
180 atomic_read(&filter->fo_dentry_O->d_count) - 1);
181 dput(filter->fo_dentry_O);
182 filter->fo_dentry_O = NULL;
186 /* cleanup the filter: write last used object id to status file */
187 static void filter_post(struct obd_device *obddev)
189 struct obd_run_ctxt saved;
190 struct filter_obd *filter = &obddev->u.filter;
196 push_ctxt(&saved, &filter->fo_ctxt);
197 file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
199 CERROR("OBD filter: cannot create status file\n");
202 rc = file->f_op->write(file, (char *)&filter->fo_lastino,
203 sizeof(filter->fo_lastino), &off);
204 if (rc != sizeof(filter->fo_lastino))
205 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
207 rc = filp_close(file, NULL);
209 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
211 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
212 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
214 CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
215 obd_type_by_mode[mode], dentry,
216 atomic_read(&dentry->d_count) - 1);
218 filter->fo_dentry_O_mode[mode] = NULL;
221 CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
222 atomic_read(&filter->fo_dentry_O->d_count) - 1);
223 dput(filter->fo_dentry_O);
229 static __u64 filter_next_id(struct obd_device *obddev)
232 spin_lock(&obddev->u.filter.fo_lock);
233 obddev->u.filter.fo_lastino++;
234 id = obddev->u.filter.fo_lastino;
235 spin_unlock(&obddev->u.filter.fo_lock);
239 /* how to get files, dentries, inodes from object id's */
240 /* parent i_sem is already held if needed for exclusivity */
241 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
242 struct dentry *dparent,
243 __u64 id, __u32 type)
245 struct super_block *sb = obddev->u.filter.fo_sb;
246 struct dentry *dchild;
251 if (!sb || !sb->s_dev) {
252 CERROR("fatal: device not initialized.\n");
253 RETURN(ERR_PTR(-ENXIO));
257 CERROR("fatal: invalid object #0\n");
259 RETURN(ERR_PTR(-ESTALE));
262 if (!(type & S_IFMT)) {
263 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
264 (unsigned long long)id, type);
265 RETURN(ERR_PTR(-EINVAL));
268 len = sprintf(name, "%Ld", id);
269 CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
271 dchild = lookup_one_len(name, dparent, len);
272 CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
273 obd_mode_to_type(type), name, dchild,
274 atomic_read(&dchild->d_count));
276 if (IS_ERR(dchild)) {
277 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
284 static struct file *filter_obj_open(struct obd_device *obddev,
285 __u64 id, __u32 type)
287 struct super_block *sb = obddev->u.filter.fo_sb;
288 struct obd_run_ctxt saved;
293 if (!sb || !sb->s_dev) {
294 CERROR("fatal: device not initialized.\n");
295 RETURN(ERR_PTR(-ENXIO));
299 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
300 RETURN(ERR_PTR(-ESTALE));
303 if (!(type & S_IFMT)) {
304 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
305 (unsigned long long)id, type);
306 RETURN(ERR_PTR(-EINVAL));
309 filter_id(name, id, type);
310 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
311 file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
314 CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
321 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
323 struct filter_obd *filter = &obddev->u.filter;
325 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
329 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
330 __u64 id, __u32 type)
332 struct dentry *dentry;
335 dentry = filter_fid2dentry(obddev, filter_parent(obddev, type),
337 if (IS_ERR(dentry)) {
338 CERROR("%s: lookup failed: rc = %ld\n", __FUNCTION__,
344 inode = iget(dentry->d_inode->i_sb, dentry->d_inode->i_ino);
346 CDEBUG(D_INODE, "put child %p, count = %d\n", dentry,
347 atomic_read(&dentry->d_count) - 1);
349 CDEBUG(D_INODE, "got inode %p (%ld), count = %d\n", inode, inode->i_ino,
350 atomic_read(&inode->i_count));
355 static int filter_connect(struct obd_conn *conn)
360 rc = gen_connect(conn);
368 static int filter_disconnect(struct obd_conn *conn)
372 rc = gen_disconnect(conn);
376 /* XXX cleanup preallocated inodes */
380 /* mount the file system (secretly) */
381 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
383 struct obd_ioctl_data* data = buf;
384 struct filter_obd *filter;
385 struct vfsmount *mnt;
389 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
393 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
398 filter = &obddev->u.filter;;
399 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
400 /* XXX is this even possible if do_kern_mount succeeded? */
402 GOTO(err_put, err = -ENODEV);
404 filter->fo_vfsmnt = mnt;
405 filter->fo_fstype = strdup(data->ioc_inlbuf2);
407 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
408 filter->fo_ctxt.pwdmnt = mnt;
409 filter->fo_ctxt.pwd = mnt->mnt_root;
410 filter->fo_ctxt.fs = get_ds();
412 err = filter_prep(obddev);
414 GOTO(err_kfree, err);
415 spin_lock_init(&filter->fo_lock);
420 kfree(filter->fo_fstype);
423 mntput(filter->fo_vfsmnt);
433 static int filter_cleanup(struct obd_device * obddev)
435 struct super_block *sb;
438 if (!(obddev->obd_flags & OBD_SET_UP))
441 if (!list_empty(&obddev->obd_gen_clients)) {
442 CERROR("still has clients!\n");
446 sb = obddev->u.filter.fo_sb;
447 if (!obddev->u.filter.fo_sb)
452 shrink_dcache_parent(sb->s_root);
454 mntput(obddev->u.filter.fo_vfsmnt);
455 obddev->u.filter.fo_sb = 0;
456 kfree(obddev->u.filter.fo_fstype);
465 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
467 int type = oa->o_mode & S_IFMT;
470 CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
471 inode->i_ino, (long)oa->o_id, oa->o_valid);
472 obdo_from_inode(oa, inode);
473 oa->o_mode &= ~S_IFMT;
476 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
477 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
478 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
480 *((obd_rdev *)oa->o_inline) = rdev;
481 oa->o_obdflags |= OBD_FL_INLINEDATA;
482 oa->o_valid |= OBD_MD_FLINLINE;
486 else if (filter_has_inline(inode)) {
487 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
488 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
489 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
490 oa->o_obdflags |= OBD_FL_INLINEDATA;
491 oa->o_valid |= OBD_MD_FLINLINE;
494 if (filter_has_obdmd(inode)) {
495 /* XXX this will change when we don't store the obdmd in data */
496 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
497 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
498 MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
499 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
500 oa->o_valid |= OBD_MD_FLOBDMD;
506 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
508 struct obd_device *obddev;
509 struct dentry *dentry;
512 if (!gen_client(conn)) {
513 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
517 obddev = conn->oc_dev;
518 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
519 oa->o_id, oa->o_mode);
521 RETURN(PTR_ERR(dentry));
523 oa->o_valid &= ~OBD_MD_FLID;
524 filter_from_inode(oa, dentry->d_inode);
530 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
532 struct obd_run_ctxt saved;
533 struct obd_device *obddev;
534 struct dentry *dentry;
540 if (!gen_client(conn)) {
541 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
545 obddev = conn->oc_dev;
546 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
547 oa->o_id, oa->o_mode);
549 RETURN(PTR_ERR(dentry));
551 inode = dentry->d_inode;
552 iattr_from_obdo(&iattr, oa);
553 iattr.ia_mode &= ~S_IFMT;
554 iattr.ia_mode |= S_IFREG;
556 if (iattr.ia_mode & ATTR_SIZE)
558 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
559 if (inode->i_op->setattr)
560 rc = inode->i_op->setattr(dentry, &iattr);
562 rc = inode_setattr(inode, &iattr);
564 if (iattr.ia_mode & ATTR_SIZE) {
566 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
567 obdo_from_inode(oa, inode);
571 CDEBUG(D_INODE, "put dentry %p, count = %d\n", inode,
572 atomic_read(&dentry->d_count) - 1);
577 static int filter_open(struct obd_conn *conn, struct obdo *oa)
579 struct obd_device *obddev;
580 struct dentry *dentry;
583 if (!gen_client(conn)) {
584 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
588 obddev = conn->oc_dev;
589 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
590 oa->o_id, oa->o_mode);
592 RETURN(PTR_ERR(dentry));
597 static int filter_close(struct obd_conn *conn, struct obdo *oa)
599 struct obd_device *obddev;
600 struct dentry *dentry;
603 if (!gen_client(conn)) {
604 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
608 obddev = conn->oc_dev;
609 dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
610 oa->o_id, oa->o_mode);
612 RETURN(PTR_ERR(dentry));
614 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
615 atomic_read(&dentry->d_count) - 1);
616 dput(dentry); /* for the close */
617 CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
618 atomic_read(&dentry->d_count) - 1);
619 dput(dentry); /* for this call */
623 static int filter_create(struct obd_conn* conn, struct obdo *oa)
626 struct obd_run_ctxt saved;
629 struct obd_device *obddev = conn->oc_dev;
633 if (!gen_client(conn)) {
634 CERROR("invalid client %u\n", conn->oc_id);
638 oa->o_id = filter_next_id(conn->oc_dev);
639 if (!(oa->o_mode && S_IFMT)) {
640 CERROR("filter obd: no type!\n");
644 filter_id(name, oa->o_id, oa->o_mode);
645 mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
646 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
647 file = filp_open(name, O_RDONLY | O_CREAT, mode);
650 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
655 /* Set flags for fields we have set in the inode struct */
656 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
657 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
658 OBD_MD_FLUID | OBD_MD_FLGID;
660 /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
664 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
666 struct obd_run_ctxt saved;
667 struct obd_device *obddev;
668 struct obd_client *cli;
670 struct dentry *dir_dentry, *object_dentry;
674 if (!(cli = gen_client(conn))) {
675 CERROR("invalid client %u\n", conn->oc_id);
679 CDEBUG(D_INODE, "destroying object %Ld\n",oa->o_id);
680 obddev = conn->oc_dev;
682 dir_dentry = filter_parent(obddev, oa->o_mode);
683 down(&dir_dentry->d_inode->i_sem);
685 object_dentry = filter_fid2dentry(obddev, dir_dentry, oa->o_id,
687 if (IS_ERR(object_dentry))
688 GOTO(out, rc = -ENOENT);
690 inode = object_dentry->d_inode;
691 if (inode->i_nlink != 1) {
692 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
695 inode->i_mode = S_IFREG;
697 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
699 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
701 CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
702 atomic_read(&object_dentry->d_count) - 1);
707 up(&dir_dentry->d_inode->i_sem);
711 /* NB count and offset are used for punch, but not truncate */
712 static int filter_truncate(struct obd_conn *conn, struct obdo *oa,
713 obd_size count, obd_off offset)
718 CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
719 "o_size = %Ld\n", oa->o_id, oa->o_valid, oa->o_size);
720 error = filter_setattr(conn, oa);
725 /* buffer must lie in user memory here */
726 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
727 obd_size *count, obd_off offset)
730 unsigned long retval;
734 if (!gen_client(conn)) {
735 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
739 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
741 RETURN(PTR_ERR(file));
743 /* count doubles as retval */
744 retval = file->f_op->read(file, buf, *count, (loff_t *)&offset);
759 /* buffer must lie in user memory here */
760 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
761 obd_size *count, obd_off offset)
763 struct obd_run_ctxt saved;
766 unsigned long retval;
769 if (!gen_client(conn)) {
770 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
774 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
776 RETURN(PTR_ERR(file));
778 /* count doubles as retval */
779 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
780 retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
797 static int filter_pgcache_brw(int cmd, struct obd_conn *conn, obd_count num_oa,
798 struct obdo **oa, obd_count *oa_bufs,
799 struct page **pages, obd_size *count,
800 obd_off *offset, obd_flag *flags, void *callback)
802 struct obd_run_ctxt saved;
803 struct super_block *sb;
804 int onum; /* index to oas */
805 int pnum; /* index to pages (bufs) */
806 unsigned long retval;
811 if (!gen_client(conn)) {
812 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
816 sb = conn->oc_dev->u.filter.fo_sb;
817 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
818 pnum = 0; /* pnum indexes buf 0..num_pages */
819 for (onum = 0; onum < num_oa; onum++) {
822 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
825 GOTO(out, retval = PTR_ERR(file));
827 /* count doubles as retval */
828 for (pg = 0; pg < oa_bufs[onum]; pg++) {
829 CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) "
830 "off count (%Ld,%Ld)\n",
831 cmd, onum, pnum, file->f_dentry->d_inode->i_ino,
832 (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
833 (unsigned long long)offset[pnum],
834 (unsigned long long)count[pnum]);
835 if (cmd & OBD_BRW_WRITE) {
839 buffer = kmap(pages[pnum]);
840 retval = file->f_op->write(file, buffer, count[pnum], &off);
842 CDEBUG(D_INODE, "retval %ld\n", retval);
844 loff_t off = offset[pnum];
845 char *buffer = kmap(pages[pnum]);
847 if (off >= file->f_dentry->d_inode->i_size) {
848 memset(buffer, 0, count[pnum]);
849 retval = count[pnum];
851 retval = file->f_op->read(file, buffer, count[pnum], &off);
855 if (retval != count[pnum]) {
857 GOTO(out, retval = -EIO);
859 CDEBUG(D_INODE, "retval %ld\n", retval);
863 /* sizes and blocks are set by generic_file_write */
864 /* ctimes/mtimes will follow with a setattr call */
871 error = (retval >= 0) ? 0 : retval;
876 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
878 struct super_block *sb = conn->oc_dev->u.filter.fo_sb;
879 struct inode *inode = NULL;
882 if (!sb || !sb->s_dev) {
883 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
888 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
892 inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
893 if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
894 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
895 (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
896 "no links" : "NULL");
905 * Calculate the number of buffer credits needed to write multiple pages in
906 * a single ext3/extN transaction. No, this shouldn't be here, but as yet
907 * ext3 doesn't have a nice API for calculating this sort of thing in advance.
909 * See comment above ext3_writepage_trans_blocks for details. We assume
910 * no data journaling is being done, but it does allow for all of the pages
911 * being non-contiguous. If we are guaranteed contiguous pages we could
912 * reduce the number of (d)indirect blocks a lot.
914 * With N blocks per page and P pages, for each inode we have at most:
916 * min(N*P, blocksize/4 + 1) dindirect blocks
919 * For the entire filesystem, we have at most:
920 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
921 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
924 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
926 static int ext3_credits_needed(struct super_block *sb, int objcount,
927 struct obd_ioobj *obj)
929 struct obd_ioobj *o = obj;
930 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
931 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
934 int needed = objcount + 1;
937 for (i = 0; i < objcount; i++, o++) {
938 int nblocks = o->ioo_bufcnt * blockpp;
939 int ndindirect = min(nblocks, addrpp + 1);
940 int nindir = nblocks + ndindirect + 1;
942 nbitmaps += nindir + nblocks;
943 ngdblocks += nindir + nblocks;
948 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
949 nbitmaps = EXT3_SB(sb)->s_groups_count;
950 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
951 ngdblocks = EXT3_SB(sb)->s_gdb_count;
953 needed += nbitmaps + ngdblocks;
956 /* We assume that there will be 1 bit set in s_dquot.flags for each
957 * quota file that is active. This is at least true for now.
959 needed += hweight32(sb_any_quota_enabled(sb)) *
960 EXT3_SINGLEDATA_TRANS_BLOCKS;
966 /* We have to start a huge journal transaction here to hold all of the
967 * metadata for the pages being written here. This is necessitated by
968 * the fact that we do lots of prepare_write operations before we do
969 * any of the matching commit_write operations, so even if we split
970 * up to use "smaller" transactions none of them could complete until
971 * all of them were opened. By having a single journal transaction,
972 * we eliminate duplicate reservations for common blocks like the
973 * superblock and group descriptors or bitmaps.
975 * We will start the transaction here, but each prepare_write will
976 * add a refcount to the transaction, and each commit_write will
977 * remove a refcount. The transaction will be closed when all of
978 * the pages have been written.
980 static void *ext3_filter_journal_start(struct filter_obd *filter,
981 int objcount, struct obd_ioobj *obj,
982 int niocount, struct niobuf_remote *nb)
984 journal_t *journal = NULL;
985 handle_t *handle = NULL;
988 /* Assumes ext3 and extN have same sb_info layout, but avoids issues
989 * with having extN built properly before filterobd for now.
991 journal = EXT3_SB(filter->fo_sb)->s_journal;
992 needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
994 /* The number of blocks we could _possibly_ dirty can very large.
995 * We reduce our request if it is absurd (and we couldn't get that
996 * many credits for a single handle anyways).
998 * At some point we have to limit the size of I/Os sent at one time,
999 * increase the size of the journal, or we have to calculate the
1000 * actual journal requirements more carefully by checking all of
1001 * the blocks instead of being maximally pessimistic. It remains to
1002 * be seen if this is a real problem or not.
1004 if (needed > journal->j_max_transaction_buffers) {
1005 CERROR("want too many journal credits (%d) using %d instead\n",
1006 needed, journal->j_max_transaction_buffers);
1007 needed = journal->j_max_transaction_buffers;
1010 handle = journal_start(journal, needed);
1012 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
1018 static void *filter_journal_start(void **journal_save,
1019 struct filter_obd *filter,
1020 int objcount, struct obd_ioobj *obj,
1021 int niocount, struct niobuf_remote *nb)
1023 void *handle = NULL;
1025 /* This may not be necessary - we probably never have a
1026 * transaction started when we enter here, so we can
1027 * remove the saving of the journal state entirely.
1028 * For now leave it in just to see if it ever happens.
1030 *journal_save = current->journal_info;
1031 if (*journal_save) {
1032 CERROR("Already have handle %p???\n", *journal_save);
1034 current->journal_info = NULL;
1037 if (!strcmp(filter->fo_fstype, "ext3") ||
1038 !strcmp(filter->fo_fstype, "extN"))
1039 handle = ext3_filter_journal_start(filter, objcount, obj,
1044 static int ext3_filter_journal_stop(void *handle)
1048 /* We got a refcount on the handle for each call to prepare_write,
1049 * so we can drop the "parent" handle here to avoid the need for
1050 * osc to call back into filterobd to close the handle. The
1051 * remaining references will be dropped in commit_write.
1053 rc = journal_stop((handle_t *)handle);
1058 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
1063 if (!strcmp(filter->fo_fstype, "ext3") ||
1064 !strcmp(filter->fo_fstype, "extN"))
1065 rc = ext3_filter_journal_stop(handle);
1068 CERROR("error on journal stop: rc = %d\n", rc);
1070 current->journal_info = journal_save;
1075 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1076 struct niobuf_local *lnb)
1078 struct address_space *mapping = inode->i_mapping;
1082 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1083 page = grab_cache_page_nowait(mapping, index); /* locked page */
1085 /* This page is currently locked, so get a temporary page instead */
1088 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
1089 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1091 CERROR("no memory for a temp page\n");
1093 GOTO(err, rc = -ENOMEM);
1095 page = virt_to_page(addr);
1097 page->index = index;
1098 lnb->flags |= N_LOCAL_TEMP_PAGE;
1099 } else if (!IS_ERR(page)) {
1100 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1101 * a no-op for most filesystems, because we write the whole
1102 * page. For partial-page I/O this will read in the page.
1105 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1107 CERROR("page index %lu, rc = %d\n", index, rc);
1110 GOTO(err_unlock, rc);
1112 /* XXX not sure if we need this if we are overwriting page */
1113 if (PageError(page)) {
1114 CERROR("error on page index %lu, rc = %d\n", index, rc);
1116 GOTO(err_unlock, rc = -EIO);
1123 lustre_put_page(page);
1129 * We need to balance prepare_write() calls with commit_write() calls.
1130 * If the page has been prepared, but we have no data for it, we don't
1131 * want to overwrite valid data on disk, but we still need to zero out
1132 * data for space which was newly allocated. Like part of what happens
1133 * in __block_prepare_write() for newly allocated blocks.
1135 * XXX currently __block_prepare_write() creates buffers for all the
1136 * pages, and the filesystems mark these buffers as BH_New if they
1137 * were newly allocated from disk. We use the BH_New flag similarly.
1139 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1143 unsigned block_start, block_end;
1144 struct buffer_head *bh, *head = page->buffers;
1145 unsigned blocksize = head->b_size;
1146 void *addr = page_address(page);
1148 /* Currently one buffer per page, but in the future... */
1149 for (bh = head, block_start = 0; bh != head || !block_start;
1150 block_start = block_end, bh = bh->b_this_page) {
1151 block_end = block_start + blocksize;
1153 memset(addr + block_start, 0, blocksize);
1157 return lustre_commit_write(page, from, to);
1160 static int filter_preprw(int cmd, struct obd_conn *conn,
1161 int objcount, struct obd_ioobj *obj,
1162 int niocount, struct niobuf_remote *nb,
1163 struct niobuf_local *res, void **desc_private)
1165 struct obd_run_ctxt saved;
1166 struct obd_device *obddev;
1167 struct obd_ioobj *o = obj;
1168 struct niobuf_remote *b = nb;
1169 struct niobuf_local *r = res;
1170 void *journal_save = NULL;
1175 memset(res, 0, sizeof(*res) * niocount);
1176 obddev = conn->oc_dev;
1178 push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
1180 if (cmd & OBD_BRW_WRITE) {
1181 *desc_private = filter_journal_start(&journal_save,
1183 objcount, obj, niocount,
1185 if (IS_ERR(*desc_private))
1186 GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1189 for (i = 0; i < objcount; i++, o++) {
1190 struct dentry *dentry;
1191 struct inode *inode;
1194 dentry = filter_fid2dentry(obddev,
1195 filter_parent(obddev, S_IFREG),
1196 o->ioo_id, S_IFREG);
1198 GOTO(out_clean, rc = PTR_ERR(dentry));
1199 inode = dentry->d_inode;
1201 CERROR("trying to BRW to non-existent file %Ld\n",
1202 (unsigned long long)o->ioo_id);
1204 GOTO(out_clean, rc = -ENOENT);
1207 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1208 unsigned long index = b->offset >> PAGE_SHIFT;
1214 r->dentry = dget(dentry);
1216 if (cmd & OBD_BRW_WRITE)
1217 page = filter_get_page_write(inode, index, r);
1219 page = lustre_get_page_read(inode, index);
1223 GOTO(out_clean, rc = PTR_ERR(page));
1226 r->addr = page_address(page);
1227 r->offset = b->offset;
1234 if (cmd & OBD_BRW_WRITE) {
1235 int err = filter_journal_stop(journal_save, &obddev->u.filter,
1246 if (cmd & OBD_BRW_WRITE)
1247 filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1249 lustre_put_page(r->page);
1254 static int filter_write_locked_page(struct niobuf_local *lnb)
1259 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1260 if (IS_ERR(lpage)) {
1261 /* It is highly unlikely that we would ever get an error here.
1262 * The page we want to get was previously locked, so it had to
1263 * have already allocated the space, and we were just writing
1264 * over the same data, so there would be no hole in the file.
1266 * XXX: possibility of a race with truncate could exist, need
1267 * to check that. There are no guarantees w.r.t.
1268 * write order even on a local filesystem, although the
1269 * normal response would be to return the number of bytes
1270 * successfully written and leave the rest to the app.
1272 rc = PTR_ERR(lpage);
1273 CERROR("error getting locked page index %ld: rc = %d\n",
1274 lnb->page->index, rc);
1278 memcpy(page_address(lpage), kmap(lnb->page), PAGE_SIZE);
1279 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1281 CERROR("error committing locked page %ld: rc = %d\n",
1282 lnb->page->index, rc);
1285 __free_pages(lnb->page, 0);
1291 static int filter_commitrw(int cmd, struct obd_conn *conn,
1292 int objcount, struct obd_ioobj *obj,
1293 int niocount, struct niobuf_local *res,
1296 struct obd_run_ctxt saved;
1297 struct obd_ioobj *o = obj;
1298 struct niobuf_local *r = res;
1300 int found_locked = 0;
1305 push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
1306 journal_save = current->journal_info;
1308 CERROR("Existing handle %p???\n", journal_save);
1309 current->journal_info = private;
1310 for (i = 0; i < objcount; i++, obj++) {
1312 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1313 struct page *page = r->page;
1318 if (r->flags & N_LOCAL_TEMP_PAGE) {
1323 if (cmd & OBD_BRW_WRITE) {
1324 int err = filter_commit_write(page, 0,
1330 lustre_put_page(page);
1333 "put inode %p (%ld), count = %d, nlink = %d\n",
1334 page->mapping->host,
1335 page->mapping->host->i_ino,
1336 atomic_read(&page->mapping->host->i_count) - 1,
1337 page->mapping->host->i_nlink);
1344 for (i = 0; i < objcount; i++, obj++) {
1346 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1348 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1351 err = filter_write_locked_page(r);
1358 current->journal_info = journal_save;
1363 static int filter_statfs(struct obd_conn *conn, struct statfs * statfs)
1365 struct super_block *sb;
1369 if (!gen_client(conn)) {
1370 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1374 sb = conn->oc_dev->u.filter.fo_sb;
1376 err = sb->s_op->statfs(sb, statfs);
1378 } /* filter_statfs */
1381 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
1382 void *key, obd_count *vallen, void **val)
1384 struct obd_device *obddev;
1385 struct obd_client * cli;
1388 if (!(cli = gen_client(conn))) {
1389 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1393 obddev = conn->oc_dev;
1395 if ( keylen == strlen("blocksize") &&
1396 memcmp(key, "blocksize", keylen) == 0 ) {
1397 *vallen = sizeof(long);
1398 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize;
1402 if ( keylen == strlen("blocksize_bits") &&
1403 memcmp(key, "blocksize_bits", keylen) == 0 ){
1404 *vallen = sizeof(long);
1405 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize_bits;
1409 if ( keylen == strlen("root_ino") &&
1410 memcmp(key, "root_ino", keylen) == 0 ){
1411 *vallen = sizeof(long);
1412 *val = (void *)(long)FILTER_ROOTINO;
1416 CDEBUG(D_IOCTL, "invalid key\n");
1421 static struct obd_ops filter_obd_ops = {
1422 o_get_info: filter_get_info,
1423 o_setup: filter_setup,
1424 o_cleanup: filter_cleanup,
1425 o_connect: filter_connect,
1426 o_disconnect: filter_disconnect,
1427 o_statfs: filter_statfs,
1428 o_getattr: filter_getattr,
1429 o_create: filter_create,
1430 o_setattr: filter_setattr,
1431 o_destroy: filter_destroy,
1432 o_open: filter_open,
1433 o_close: filter_close,
1434 o_read: filter_read,
1435 o_write: filter_write,
1436 o_brw: filter_pgcache_brw,
1437 o_punch: filter_truncate,
1438 o_preprw: filter_preprw,
1439 o_commitrw: filter_commitrw
1441 o_preallocate: filter_preallocate_inodes,
1442 o_migrate: filter_migrate,
1443 o_copy: gen_copy_data,
1444 o_iterate: filter_iterate
1449 static int __init obdfilter_init(void)
1451 printk(KERN_INFO "Filtering OBD driver v0.001, braam@clusterfs.com\n");
1452 return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1455 static void __exit obdfilter_exit(void)
1457 obd_unregister_type(OBD_FILTER_DEVICENAME);
1460 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
1461 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1462 MODULE_LICENSE("GPL");
1464 module_init(obdfilter_init);
1465 module_exit(obdfilter_exit);