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.
7 * Author: Peter Braam <braam@clusterfs.com>
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Invariant: get O/R i_sem for lookup, if needed, before any journal ops
28 * (which need to get journal_lock, may block if journal full).
32 #define DEBUG_SUBSYSTEM S_FILTER
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/pagemap.h> // XXX kill me soon
38 #include <linux/dcache.h>
39 #include <linux/obd_class.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/obd_filter.h>
42 #include <linux/init.h>
43 #include <linux/random.h>
44 #include <linux/lustre_fsfilt.h>
45 #include <linux/lprocfs_status.h>
47 extern struct lprocfs_vars status_class_var[];
48 extern struct lprocfs_vars status_var_nm_1[];
50 static kmem_cache_t *filter_open_cache;
51 static kmem_cache_t *filter_dentry_cache;
54 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
56 [S_IFREG >> S_SHIFT] "R",
57 [S_IFDIR >> S_SHIFT] "D",
58 [S_IFCHR >> S_SHIFT] "C",
59 [S_IFBLK >> S_SHIFT] "B",
60 [S_IFIFO >> S_SHIFT] "F",
61 [S_IFSOCK >> S_SHIFT] "S",
62 [S_IFLNK >> S_SHIFT] "L"
65 static inline const char *obd_mode_to_type(int mode)
67 return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
70 /* write the pathname into the string */
71 static int filter_id(char *buf, obd_id id, obd_mode mode)
73 return sprintf(buf, "O/%s/"LPU64, obd_mode_to_type(mode), id);
76 static inline void f_dput(struct dentry *dentry)
78 /* Can't go inside filter_ddelete because it can block */
79 CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
80 dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
81 LASSERT(atomic_read(&dentry->d_count) > 0);
86 /* Not racy w.r.t. others, because we are the only user of this dentry */
87 static void filter_drelease(struct dentry *dentry)
90 kmem_cache_free(filter_dentry_cache, dentry->d_fsdata);
93 struct dentry_operations filter_dops = {
94 .d_release = filter_drelease,
97 #define LAST_RCVD "last_rcvd"
99 /* setup the object store with correct subdirectories */
100 static int filter_prep(struct obd_device *obd)
102 struct obd_run_ctxt saved;
103 struct filter_obd *filter = &obd->u.filter;
104 struct dentry *dentry;
111 push_ctxt(&saved, &filter->fo_ctxt, NULL);
112 dentry = simple_mkdir(current->fs->pwd, "O", 0700);
113 CDEBUG(D_INODE, "got/created O: %p\n", dentry);
114 if (IS_ERR(dentry)) {
115 rc = PTR_ERR(dentry);
116 CERROR("cannot open/create O: rc = %d\n", rc);
119 filter->fo_dentry_O = dentry;
122 * Create directories and/or get dentries for each object type.
123 * This saves us from having to do multiple lookups for each one.
125 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
126 char *name = obd_type_by_mode[mode];
129 filter->fo_dentry_O_mode[mode] = NULL;
132 dentry = simple_mkdir(filter->fo_dentry_O, name, 0700);
133 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
134 if (IS_ERR(dentry)) {
135 rc = PTR_ERR(dentry);
136 CERROR("cannot create O/%s: rc = %d\n", name, rc);
137 GOTO(out_O_mode, rc);
139 filter->fo_dentry_O_mode[mode] = dentry;
142 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0700);
143 if ( !file || IS_ERR(file) ) {
145 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
147 GOTO(out_O_mode, rc);
150 /* steal operations */
151 inode = file->f_dentry->d_inode;
152 filter->fo_fop = file->f_op;
153 filter->fo_iop = inode->i_op;
154 filter->fo_aops = inode->i_mapping->a_ops;
156 if (inode->i_size == 0) {
157 __u64 disk_lastobjid = cpu_to_le64(lastobjid);
158 ssize_t retval = file->f_op->write(file,(char *)&disk_lastobjid,
159 sizeof(disk_lastobjid),
161 if (retval != sizeof(disk_lastobjid)) {
162 CDEBUG(D_INODE,"OBD filter: error writing lastobjid\n");
164 GOTO(out_O_mode, rc = -EIO);
167 __u64 disk_lastobjid;
168 ssize_t retval = file->f_op->read(file, (char *)&disk_lastobjid,
169 sizeof(disk_lastobjid),
171 if (retval != sizeof(disk_lastobjid)) {
172 CDEBUG(D_INODE,"OBD filter: error reading lastobjid\n");
174 GOTO(out_O_mode, rc = -EIO);
176 lastobjid = le64_to_cpu(disk_lastobjid);
178 filter->fo_lastobjid = lastobjid;
183 pop_ctxt(&saved, &filter->fo_ctxt, NULL);
189 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
192 filter->fo_dentry_O_mode[mode] = NULL;
195 f_dput(filter->fo_dentry_O);
196 filter->fo_dentry_O = NULL;
200 /* cleanup the filter: write last used object id to status file */
201 static void filter_post(struct obd_device *obd)
203 struct obd_run_ctxt saved;
204 struct filter_obd *filter = &obd->u.filter;
205 __u64 disk_lastobjid;
210 push_ctxt(&saved, &filter->fo_ctxt, NULL);
211 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0700);
213 CERROR("OBD filter: cannot create %s\n", LAST_RCVD);
218 disk_lastobjid = cpu_to_le64(filter->fo_lastobjid);
219 rc = file->f_op->write(file, (char *)&disk_lastobjid,
220 sizeof(disk_lastobjid), &file->f_pos);
221 if (rc != sizeof(disk_lastobjid))
222 CERROR("OBD filter: error writing lastobjid: rc = %ld\n", rc);
224 rc = filp_close(file, NULL);
226 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
228 for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
229 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
232 filter->fo_dentry_O_mode[mode] = NULL;
235 f_dput(filter->fo_dentry_O);
237 pop_ctxt(&saved, &filter->fo_ctxt, NULL);
241 static __u64 filter_next_id(struct obd_device *obd)
245 spin_lock(&obd->u.filter.fo_objidlock);
246 id = ++obd->u.filter.fo_lastobjid;
247 spin_unlock(&obd->u.filter.fo_objidlock);
252 /* how to get files, dentries, inodes from object id's */
253 /* parent i_sem is already held if needed for exclusivity */
254 static struct dentry *filter_fid2dentry(struct obd_device *obd,
255 struct dentry *dparent,
256 __u64 id, int locked)
258 struct super_block *sb = obd->u.filter.fo_sb;
259 struct dentry *dchild;
264 if (!sb || !sb->s_dev) {
265 CERROR("fatal: device not initialized.\n");
266 RETURN(ERR_PTR(-ENXIO));
270 CERROR("fatal: invalid object id 0\n");
272 RETURN(ERR_PTR(-ESTALE));
275 len = sprintf(name, LPU64, id);
276 CDEBUG(D_INODE, "opening object O/%*s/%s\n",
277 dparent->d_name.len, dparent->d_name.name, name);
279 down(&dparent->d_inode->i_sem);
280 dchild = lookup_one_len(name, dparent, len);
282 up(&dparent->d_inode->i_sem);
283 if (IS_ERR(dchild)) {
284 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
288 CDEBUG(D_INODE, "got child obj O/%*s/%s: %p, count = %d\n",
289 dparent->d_name.len, dparent->d_name.name, name, dchild,
290 atomic_read(&dchild->d_count));
292 LASSERT(atomic_read(&dchild->d_count) > 0);
297 static inline struct dentry *filter_parent(struct obd_device *obd,
300 struct filter_obd *filter = &obd->u.filter;
302 LASSERT((mode & S_IFMT) == S_IFREG); /* only regular files for now */
303 return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
306 static struct file *filter_obj_open(struct obd_export *export,
307 __u64 id, __u32 type)
309 struct filter_obd *filter = &export->exp_obd->u.filter;
310 struct super_block *sb = filter->fo_sb;
311 struct dentry *dentry;
312 struct filter_export_data *fed = &export->exp_filter_data;
313 struct filter_dentry_data *fdd;
314 struct filter_file_data *ffd;
315 struct obd_run_ctxt saved;
320 if (!sb || !sb->s_dev) {
321 CERROR("fatal: device not initialized.\n");
322 RETURN(ERR_PTR(-ENXIO));
326 CERROR("fatal: invalid obdo "LPU64"\n", id);
327 RETURN(ERR_PTR(-ESTALE));
330 if (!(type & S_IFMT)) {
331 CERROR("OBD %s, object "LPU64" has bad type: %o\n",
332 __FUNCTION__, id, type);
333 RETURN(ERR_PTR(-EINVAL));
336 ffd = kmem_cache_alloc(filter_open_cache, SLAB_KERNEL);
338 CERROR("obdfilter: out of memory\n");
339 RETURN(ERR_PTR(-ENOMEM));
342 /* We preallocate this to avoid blocking while holding fo_fddlock */
343 fdd = kmem_cache_alloc(filter_dentry_cache, SLAB_KERNEL);
345 CERROR("obdfilter: out of memory\n");
346 GOTO(out_ffd, file = ERR_PTR(-ENOMEM));
349 filter_id(name, id, type);
350 push_ctxt(&saved, &filter->fo_ctxt, NULL);
351 file = filp_open(name, O_RDWR | O_LARGEFILE, 0 /* type? */);
352 pop_ctxt(&saved, &filter->fo_ctxt, NULL);
355 CERROR("error opening %s: rc %d\n", name, PTR_ERR(file));
359 dentry = file->f_dentry;
360 spin_lock(&filter->fo_fddlock);
361 if (dentry->d_fsdata) {
362 spin_unlock(&filter->fo_fddlock);
363 kmem_cache_free(filter_dentry_cache, fdd);
364 fdd = dentry->d_fsdata;
365 LASSERT(kmem_cache_validate(filter_dentry_cache, fdd));
366 /* should only happen during client recovery */
367 if (fdd->fdd_flags & FILTER_FLAG_DESTROY)
368 CDEBUG(D_INODE,"opening destroyed object "LPX64"\n",id);
369 atomic_inc(&fdd->fdd_open_count);
371 atomic_set(&fdd->fdd_open_count, 1);
373 /* If this is racy, then we can use {cmp}xchg and atomic_add */
374 dentry->d_fsdata = fdd;
375 spin_unlock(&filter->fo_fddlock);
378 get_random_bytes(&ffd->ffd_servercookie, sizeof(ffd->ffd_servercookie));
379 ffd->ffd_file = file;
380 file->private_data = ffd;
383 dentry->d_op = &filter_dops;
385 LASSERT(dentry->d_op == &filter_dops);
387 spin_lock(&fed->fed_lock);
388 list_add(&ffd->ffd_export_list, &fed->fed_open_head);
389 spin_unlock(&fed->fed_lock);
391 CDEBUG(D_INODE, "opened objid "LPX64": rc = %p\n", id, file);
397 kmem_cache_free(filter_dentry_cache, fdd);
399 ffd->ffd_servercookie = DEAD_HANDLE_MAGIC;
400 kmem_cache_free(filter_open_cache, ffd);
404 /* Caller must hold i_sem on dir_dentry->d_inode */
405 static int filter_destroy_internal(struct obd_device *obd,
406 struct dentry *dir_dentry,
407 struct dentry *object_dentry)
409 struct obd_run_ctxt saved;
410 struct inode *inode = object_dentry->d_inode;
414 if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
415 CERROR("destroying objid %*s nlink = %d, count = %d\n",
416 object_dentry->d_name.len,
417 object_dentry->d_name.name,
418 inode->i_nlink, atomic_read(&inode->i_count));
421 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
422 rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
423 pop_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
426 CERROR("error unlinking objid %*s: rc %d\n",
427 object_dentry->d_name.len,
428 object_dentry->d_name.name, rc);
433 static int filter_close_internal(struct obd_device *obd,
434 struct filter_file_data *ffd)
436 struct file *filp = ffd->ffd_file;
437 struct dentry *object_dentry = dget(filp->f_dentry);
438 struct filter_dentry_data *fdd = object_dentry->d_fsdata;
442 LASSERT(filp->private_data == ffd);
445 rc = filp_close(filp, 0);
447 if (atomic_dec_and_test(&fdd->fdd_open_count) &&
448 fdd->fdd_flags & FILTER_FLAG_DESTROY) {
449 struct dentry *dir_dentry = filter_parent(obd, S_IFREG);
451 down(&dir_dentry->d_inode->i_sem);
452 /* XXX start transaction */
453 /* XXX unlink from PENDING directory now too */
454 rc2 = filter_destroy_internal(obd, dir_dentry, object_dentry);
455 /* XXX finish transaction */
458 up(&dir_dentry->d_inode->i_sem);
461 f_dput(object_dentry);
462 kmem_cache_free(filter_open_cache, ffd);
468 /* mount the file system (secretly) */
469 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
471 struct obd_ioctl_data* data = buf;
472 struct filter_obd *filter;
473 struct vfsmount *mnt;
478 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
479 GOTO(err_dec, rc = -EINVAL);
481 obd->obd_fsops = fsfilt_get_ops(data->ioc_inlbuf2);
482 if (IS_ERR(obd->obd_fsops))
483 GOTO(err_dec, rc = PTR_ERR(obd->obd_fsops));
485 mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
490 filter = &obd->u.filter;;
491 filter->fo_vfsmnt = mnt;
492 filter->fo_fstype = strdup(data->ioc_inlbuf2);
493 filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
494 CDEBUG(D_SUPER, "%s: mnt = %p\n", data->ioc_inlbuf1, mnt);
496 OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
497 filter->fo_ctxt.pwdmnt = mnt;
498 filter->fo_ctxt.pwd = mnt->mnt_root;
499 filter->fo_ctxt.fs = get_ds();
501 rc = filter_prep(obd);
505 spin_lock_init(&filter->fo_fddlock);
506 spin_lock_init(&filter->fo_objidlock);
507 INIT_LIST_HEAD(&filter->fo_export_list);
510 ldlm_namespace_new("filter-tgt", LDLM_NAMESPACE_SERVER);
511 if (!obd->obd_namespace)
512 GOTO(err_post, rc = -ENOMEM);
514 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
515 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
522 kfree(filter->fo_fstype);
524 mntput(filter->fo_vfsmnt);
528 fsfilt_put_ops(obd->obd_fsops);
535 static int filter_cleanup(struct obd_device *obd)
537 struct super_block *sb;
540 if (!list_empty(&obd->obd_exports)) {
541 CERROR("still has clients!\n");
542 class_disconnect_all(obd);
543 if (!list_empty(&obd->obd_exports)) {
544 CERROR("still has exports after forced cleanup?\n");
549 ldlm_namespace_free(obd->obd_namespace);
551 sb = obd->u.filter.fo_sb;
552 if (!obd->u.filter.fo_sb)
557 shrink_dcache_parent(sb->s_root);
559 mntput(obd->u.filter.fo_vfsmnt);
560 obd->u.filter.fo_sb = 0;
561 kfree(obd->u.filter.fo_fstype);
562 fsfilt_put_ops(obd->obd_fsops);
570 int filter_attach(struct obd_device *dev, obd_count len, void *data)
572 return lprocfs_reg_obd(dev, status_var_nm_1, dev);
575 int filter_detach(struct obd_device *dev)
577 return lprocfs_dereg_obd(dev);
580 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
581 obd_uuid_t cluuid, struct recovd_obd *recovd,
582 ptlrpc_recovery_cb_t recover)
584 struct obd_export *exp;
589 rc = class_connect(conn, obd, cluuid);
592 exp = class_conn2export(conn);
595 INIT_LIST_HEAD(&exp->exp_filter_data.fed_open_head);
596 spin_lock_init(&exp->exp_filter_data.fed_lock);
605 static int filter_disconnect(struct lustre_handle *conn)
607 struct obd_export *exp = class_conn2export(conn);
608 struct filter_export_data *fed;
613 fed = &exp->exp_filter_data;
614 spin_lock(&fed->fed_lock);
615 while (!list_empty(&fed->fed_open_head)) {
616 struct filter_file_data *ffd;
618 ffd = list_entry(fed->fed_open_head.next, typeof(*ffd),
620 list_del(&ffd->ffd_export_list);
621 spin_unlock(&fed->fed_lock);
623 CERROR("force closing file %*s on disconnect\n",
624 ffd->ffd_file->f_dentry->d_name.len,
625 ffd->ffd_file->f_dentry->d_name.name);
627 filter_close_internal(exp->exp_obd, ffd);
628 spin_lock(&fed->fed_lock);
630 spin_unlock(&fed->fed_lock);
632 ldlm_cancel_locks_for_export(exp);
633 rc = class_disconnect(conn);
637 /* XXX cleanup preallocated inodes */
641 static void filter_from_inode(struct obdo *oa, struct inode *inode, int valid)
643 int type = oa->o_mode & S_IFMT;
646 CDEBUG(D_INFO, "src inode %lu (%p), dst obdo "LPX64" valid 0x%08x\n",
647 inode->i_ino, inode, oa->o_id, valid);
648 /* Don't copy the inode number in place of the object ID */
649 obdo_from_inode(oa, inode, valid);
650 oa->o_mode &= ~S_IFMT;
653 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
654 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
656 oa->o_valid |= OBD_MD_FLRDEV;
662 static struct filter_file_data *filter_handle2ffd(struct lustre_handle *handle)
664 struct filter_file_data *ffd = NULL;
667 if (!handle || !handle->addr)
670 ffd = (struct filter_file_data *)(unsigned long)(handle->addr);
671 if (!kmem_cache_validate(filter_open_cache, (void *)ffd))
674 if (ffd->ffd_servercookie != handle->cookie)
677 LASSERT(ffd->ffd_file->private_data == ffd);
681 static struct dentry *__filter_oa2dentry(struct lustre_handle *conn,
682 struct obdo *oa, int locked,char *what)
684 struct dentry *dentry = NULL;
686 if (oa->o_valid & OBD_MD_FLHANDLE) {
687 struct lustre_handle *ost_handle = obdo_handle(oa);
688 struct filter_file_data *ffd = filter_handle2ffd(ost_handle);
691 dentry = dget(ffd->ffd_file->f_dentry);
695 struct obd_device *obd = class_conn2obd(conn);
697 CERROR("invalid client "LPX64"\n", conn->addr);
698 RETURN(ERR_PTR(-EINVAL));
700 dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
704 if (IS_ERR(dentry)) {
705 CERROR("%s error looking up object: "LPX64"\n", what, oa->o_id);
709 if (!dentry->d_inode) {
710 CERROR("%s on non-existent object: "LPX64"\n", what, oa->o_id);
712 RETURN(ERR_PTR(-ENOENT));
718 #define filter_oa2dentry(conn, oa, locked) __filter_oa2dentry(conn, oa, locked,\
721 static int filter_getattr(struct lustre_handle *conn, struct obdo *oa,
722 struct lov_stripe_md *md)
724 struct dentry *dentry = NULL;
728 dentry = filter_oa2dentry(conn, oa, 0);
730 RETURN(PTR_ERR(dentry));
732 filter_from_inode(oa, dentry->d_inode, oa->o_valid);
738 /* this is called from filter_truncate() until we have filter_punch() */
739 static int filter_setattr(struct lustre_handle *conn, struct obdo *oa,
740 struct lov_stripe_md *md)
742 struct obd_run_ctxt saved;
743 struct obd_device *obd = class_conn2obd(conn);
744 struct dentry *dentry;
750 dentry = filter_oa2dentry(conn, oa, 0);
753 RETURN(PTR_ERR(dentry));
755 iattr_from_obdo(&iattr, oa, oa->o_valid);
756 iattr.ia_mode = (iattr.ia_mode & ~S_IFMT) | S_IFREG;
757 inode = dentry->d_inode;
759 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
761 if (iattr.ia_valid & ATTR_SIZE)
764 /* XXX start transaction */
765 if (inode->i_op->setattr)
766 rc = inode->i_op->setattr(dentry, &iattr);
768 rc = inode_setattr(inode, &iattr);
769 /* XXX update last_rcvd, finish transaction */
771 if (iattr.ia_valid & ATTR_SIZE) {
773 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
774 obdo_from_inode(oa, inode, oa->o_valid);
778 pop_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
784 static int filter_open(struct lustre_handle *conn, struct obdo *oa,
785 struct lov_stripe_md *ea)
787 struct obd_export *export;
788 struct lustre_handle *handle;
789 struct filter_file_data *ffd;
794 export = class_conn2export(conn);
796 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
800 filp = filter_obj_open(export, oa->o_id, oa->o_mode);
802 GOTO(out, rc = PTR_ERR(filp));
804 filter_from_inode(oa, filp->f_dentry->d_inode, oa->o_valid);
806 ffd = filp->private_data;
807 handle = obdo_handle(oa);
808 handle->addr = (__u64)(unsigned long)ffd;
809 handle->cookie = ffd->ffd_servercookie;
810 oa->o_valid |= OBD_MD_FLHANDLE;
816 static int filter_close(struct lustre_handle *conn, struct obdo *oa,
817 struct lov_stripe_md *ea)
819 struct obd_export *exp;
820 struct filter_file_data *ffd;
821 struct filter_export_data *fed;
825 exp = class_conn2export(conn);
827 CDEBUG(D_IOCTL, "fatal: invalid client "LPX64"\n", conn->addr);
831 if (!(oa->o_valid & OBD_MD_FLHANDLE)) {
832 CERROR("no handle for close of objid "LPX64"\n", oa->o_id);
836 ffd = filter_handle2ffd(obdo_handle(oa));
838 struct lustre_handle *handle = obdo_handle(oa);
839 CERROR("bad handle ("LPX64") or cookie ("LPX64") for close\n",
840 handle->addr, handle->cookie);
844 fed = &exp->exp_filter_data;
845 spin_lock(&fed->fed_lock);
846 list_del(&ffd->ffd_export_list);
847 spin_unlock(&fed->fed_lock);
849 rc = filter_close_internal(exp->exp_obd, ffd);
854 static int filter_create(struct lustre_handle *conn, struct obdo *oa,
855 struct lov_stripe_md **ea)
857 struct obd_device *obd = class_conn2obd(conn);
858 struct obd_run_ctxt saved;
859 struct dentry *dir_dentry;
866 CERROR("invalid client "LPX64"\n", conn->addr);
870 oa->o_id = filter_next_id(obd);
872 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
873 dir_dentry = filter_parent(obd, oa->o_mode);
874 down(&dir_dentry->d_inode->i_sem);
875 new = filter_fid2dentry(obd, dir_dentry, oa->o_id, 1);
877 GOTO(out, rc = PTR_ERR(new));
880 /* This would only happen if lastobjid was bad on disk */
881 CERROR("objid O/%*s/"LPU64" already exists\n",
882 dir_dentry->d_name.len, dir_dentry->d_name.name,
885 GOTO(out, rc = -EEXIST);
888 /* XXX start transaction */
889 rc = vfs_create(dir_dentry->d_inode, new, oa->o_mode);
892 /* XXX update last_rcvd+lastobjid on disk, finish transaction */
894 /* Set flags for fields we have set in the inode struct */
895 oa->o_valid = OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
896 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME;
897 filter_from_inode(oa, new->d_inode, oa->o_valid);
903 up(&dir_dentry->d_inode->i_sem);
904 pop_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
908 static int filter_destroy(struct lustre_handle *conn, struct obdo *oa,
909 struct lov_stripe_md *ea)
911 struct obd_device *obd = class_conn2obd(conn);
912 struct dentry *dir_dentry, *object_dentry;
913 struct filter_dentry_data *fdd;
918 CERROR("invalid client "LPX64"\n", conn->addr);
922 CDEBUG(D_INODE, "destroying objid "LPX64"\n", oa->o_id);
924 dir_dentry = filter_parent(obd, oa->o_mode);
925 down(&dir_dentry->d_inode->i_sem);
927 object_dentry = filter_oa2dentry(conn, oa, 1);
928 if (IS_ERR(object_dentry))
929 GOTO(out, rc = -ENOENT);
931 fdd = object_dentry->d_fsdata;
932 /* XXX start transaction */
933 if (fdd && atomic_read(&fdd->fdd_open_count)) {
934 if (!(fdd->fdd_flags & FILTER_FLAG_DESTROY)) {
935 fdd->fdd_flags |= FILTER_FLAG_DESTROY;
936 /* XXX put into PENDING directory in case of crash */
938 "defer destroy of %dx open objid "LPX64"\n",
939 atomic_read(&fdd->fdd_open_count), oa->o_id);
942 "repeat destroy of %dx open objid "LPX64"\n",
943 atomic_read(&fdd->fdd_open_count), oa->o_id);
944 GOTO(out_dput, rc = 0);
947 rc = filter_destroy_internal(obd, dir_dentry, object_dentry);
949 /* XXX update last_rcvd on disk, finish transaction */
950 f_dput(object_dentry);
954 up(&dir_dentry->d_inode->i_sem);
958 /* NB start and end are used for punch, but not truncate */
959 static int filter_truncate(struct lustre_handle *conn, struct obdo *oa,
960 struct lov_stripe_md *lsm,
961 obd_off start, obd_off end)
966 if (end != OBD_OBJECT_EOF)
967 CERROR("PUNCH not supported, only truncate works\n");
969 CDEBUG(D_INODE, "calling truncate for object "LPX64", valid = %x, "
970 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
972 error = filter_setattr(conn, oa, NULL);
976 static inline void lustre_put_page(struct page *page)
979 page_cache_release(page);
984 lustre_get_page_read(struct inode *inode, struct niobuf_remote *rnb)
986 unsigned long index = rnb->offset >> PAGE_SHIFT;
987 struct address_space *mapping = inode->i_mapping;
991 page = read_cache_page(mapping, index,
992 (filler_t*)mapping->a_ops->readpage, NULL);
996 if (!PageUptodate(page)) {
997 CERROR("page index %lu not uptodate\n", index);
998 GOTO(err_page, rc = -EIO);
1000 if (PageError(page)) {
1001 CERROR("page index %lu has error\n", index);
1002 GOTO(err_page, rc = -EIO);
1008 lustre_put_page(page);
1012 static struct page *
1013 lustre_get_page_write(struct inode *inode, unsigned long index)
1015 struct address_space *mapping = inode->i_mapping;
1019 page = grab_cache_page(mapping, index); /* locked page */
1021 if (!IS_ERR(page)) {
1023 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1024 * a no-op for most filesystems, because we write the whole
1025 * page. For partial-page I/O this will read in the page.
1027 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1029 CERROR("page index %lu, rc = %d\n", index, rc);
1032 GOTO(err_unlock, rc);
1034 /* XXX not sure if we need this if we are overwriting page */
1035 if (PageError(page)) {
1036 CERROR("error on page index %lu, rc = %d\n", index, rc);
1038 GOTO(err_unlock, rc = -EIO);
1045 lustre_put_page(page);
1049 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1050 int waitfor_one_page(struct page *page)
1052 wait_on_page_locked(page);
1057 static int lustre_commit_write(struct page *page, unsigned from, unsigned to)
1059 struct inode *inode = page->mapping->host;
1062 err = page->mapping->a_ops->commit_write(NULL, page, from, to);
1063 if (!err && IS_SYNC(inode))
1064 err = waitfor_one_page(page);
1065 //SetPageUptodate(page); // the client commit_write will do this
1067 SetPageReferenced(page);
1069 lustre_put_page(page);
1073 struct page *filter_get_page_write(struct inode *inode,
1074 struct niobuf_remote *rnb,
1075 struct niobuf_local *lnb, int *pglocked)
1077 unsigned long index = rnb->offset >> PAGE_SHIFT;
1078 struct address_space *mapping = inode->i_mapping;
1083 //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1085 page = grab_cache_page_nowait(mapping, index); /* locked page */
1087 page = grab_cache_page(mapping, index); /* locked page */
1090 /* This page is currently locked, so get a temporary page instead. */
1091 /* XXX I believe this is a very dangerous thing to do - consider if
1092 * we had multiple writers for the same file (definitely the case
1093 * if we are using this codepath). If writer A locks the page,
1094 * writer B writes to a copy (as here), writer A drops the page
1095 * lock, and writer C grabs the lock before B does, then B will
1096 * later overwrite the data from C, even if C had LDLM locked
1097 * and initiated the write after B did.
1101 CDEBUG(D_PAGE, "ino %lu page %ld locked\n", inode->i_ino,index);
1102 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1104 CERROR("no memory for a temp page\n");
1106 GOTO(err, rc = -ENOMEM);
1109 memset((void *)addr, 0xBA, PAGE_SIZE);
1110 page = virt_to_page(addr);
1112 page->index = index;
1113 lnb->flags |= N_LOCAL_TEMP_PAGE;
1114 } else if (!IS_ERR(page)) {
1118 rc = mapping->a_ops->prepare_write(NULL, page,
1119 rnb->offset % PAGE_SIZE,
1122 CERROR("page index %lu, rc = %d\n", index, rc);
1125 GOTO(err_unlock, rc);
1127 /* XXX not sure if we need this if we are overwriting page */
1128 if (PageError(page)) {
1129 CERROR("error on page index %lu, rc = %d\n", index, rc);
1131 GOTO(err_unlock, rc = -EIO);
1138 lustre_put_page(page);
1144 * We need to balance prepare_write() calls with commit_write() calls.
1145 * If the page has been prepared, but we have no data for it, we don't
1146 * want to overwrite valid data on disk, but we still need to zero out
1147 * data for space which was newly allocated. Like part of what happens
1148 * in __block_prepare_write() for newly allocated blocks.
1150 * XXX currently __block_prepare_write() creates buffers for all the
1151 * pages, and the filesystems mark these buffers as BH_New if they
1152 * were newly allocated from disk. We use the BH_New flag similarly.
1154 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1157 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1159 unsigned block_start, block_end;
1160 struct buffer_head *bh, *head = page->buffers;
1161 unsigned blocksize = head->b_size;
1162 void *addr = page_address(page);
1164 /* debugging: just seeing if this ever happens */
1165 CERROR("called filter_commit_write for ino %lu:%lu on err %d\n",
1166 page->mapping->host->i_ino, page->index, err);
1168 /* Currently one buffer per page, but in the future... */
1169 for (bh = head, block_start = 0; bh != head || !block_start;
1170 block_start = block_end, bh = bh->b_this_page) {
1171 block_end = block_start + blocksize;
1173 memset(addr + block_start, 0, blocksize);
1177 return lustre_commit_write(page, from, to);
1180 static int filter_preprw(int cmd, struct lustre_handle *conn,
1181 int objcount, struct obd_ioobj *obj,
1182 int niocount, struct niobuf_remote *nb,
1183 struct niobuf_local *res, void **desc_private)
1185 struct obd_run_ctxt saved;
1186 struct obd_device *obd;
1187 struct obd_ioobj *o;
1188 struct niobuf_remote *rnb = nb;
1189 struct niobuf_local *lnb = res;
1190 struct dentry *dir_dentry;
1191 struct fsfilt_objinfo *fso;
1197 memset(res, 0, niocount * sizeof(*res));
1199 obd = class_conn2obd(conn);
1201 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1205 LASSERT(objcount < 16); // theoretically we support multi-obj BRW
1207 OBD_ALLOC(fso, objcount * sizeof(*fso));
1211 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
1212 dir_dentry = filter_parent(obd, S_IFREG);
1214 for (i = 0, o = obj; i < objcount; i++, o++) {
1215 struct filter_dentry_data *fdd;
1216 struct dentry *dentry;
1218 LASSERT(o->ioo_bufcnt);
1220 dentry = filter_fid2dentry(obd, dir_dentry, o->ioo_id, 0);
1223 GOTO(out_objinfo, rc = PTR_ERR(dentry));
1225 fso[i].fso_dentry = dentry;
1226 fso[i].fso_bufcnt = o->ioo_bufcnt;
1228 if (!dentry->d_inode) {
1229 CERROR("trying to BRW to non-existent file "LPU64"\n",
1231 GOTO(out_objinfo, rc = -ENOENT);
1234 fdd = dentry->d_fsdata;
1235 if (!fdd || !atomic_read(&fdd->fdd_open_count))
1236 CDEBUG(D_PAGE, "I/O to unopened object "LPX64"\n",
1240 if (cmd & OBD_BRW_WRITE) {
1241 *desc_private = fsfilt_brw_start(obd, objcount, fso,
1243 if (IS_ERR(*desc_private))
1244 GOTO(out_objinfo, rc = PTR_ERR(*desc_private));
1247 obd_kmap_get(niocount, 1);
1249 for (i = 0, o = obj; i < objcount; i++, o++) {
1250 struct dentry *dentry = fso->fso_dentry;
1251 struct inode *inode = dentry->d_inode;
1254 for (j = 0; j < o->ioo_bufcnt; j++, rnb++, lnb++) {
1258 lnb->dentry = dentry;
1260 lnb->dentry = dget(dentry);
1262 if (cmd & OBD_BRW_WRITE)
1263 page = filter_get_page_write(inode, rnb, lnb,
1266 page = lustre_get_page_read(inode, rnb);
1269 if (cmd & OBD_BRW_WRITE)
1270 fsfilt_commit(obd, dir_dentry->d_inode,
1273 GOTO(out_pages, rc = PTR_ERR(page));
1276 lnb->addr = page_address(page);
1277 lnb->offset = rnb->offset;
1279 lnb->len = rnb->len;
1283 if (cmd & OBD_BRW_WRITE) {
1284 int err = fsfilt_commit(obd, dir_dentry->d_inode,
1287 GOTO(out_pages, rc = err);
1292 OBD_FREE(fso, objcount * sizeof(*fso));
1293 current->journal_info = NULL;
1294 pop_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
1298 while (lnb-- > res) {
1299 CERROR("error cleanup on brw\n");
1300 if (cmd & OBD_BRW_WRITE)
1301 filter_commit_write(lnb->page, 0, PAGE_SIZE, rc);
1303 lustre_put_page(lnb->page);
1305 obd_kmap_put(niocount);
1307 for (i = 0; i < objcount && fso[i].fso_dentry; i++)
1308 f_dput(fso[i].fso_dentry);
1313 static int filter_write_locked_page(struct niobuf_local *lnb)
1318 lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1319 if (IS_ERR(lpage)) {
1320 /* It is highly unlikely that we would ever get an error here.
1321 * The page we want to get was previously locked, so it had to
1322 * have already allocated the space, and we were just writing
1323 * over the same data, so there would be no hole in the file.
1325 * XXX: possibility of a race with truncate could exist, need
1326 * to check that. There are no guarantees w.r.t.
1327 * write order even on a local filesystem, although the
1328 * normal response would be to return the number of bytes
1329 * successfully written and leave the rest to the app.
1331 rc = PTR_ERR(lpage);
1332 CERROR("error getting locked page index %ld: rc = %d\n",
1333 lnb->page->index, rc);
1337 /* lpage is kmapped in lustre_get_page_write() above and kunmapped in
1338 * lustre_commit_write() below, lnb->page was kmapped previously in
1339 * filter_get_page_write() and kunmapped in lustre_put_page() below.
1341 memcpy(page_address(lpage), page_address(lnb->page), PAGE_SIZE);
1342 rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1344 CERROR("error committing locked page %ld: rc = %d\n",
1345 lnb->page->index, rc);
1347 lustre_put_page(lnb->page);
1352 static int filter_commitrw(int cmd, struct lustre_handle *conn,
1353 int objcount, struct obd_ioobj *obj,
1354 int niocount, struct niobuf_local *res,
1357 struct obd_run_ctxt saved;
1358 struct obd_ioobj *o;
1359 struct niobuf_local *r;
1360 struct obd_device *obd = class_conn2obd(conn);
1361 int found_locked = 0;
1366 push_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
1368 LASSERT(!current->journal_info);
1369 current->journal_info = private;
1371 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1374 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1375 struct page *page = r->page;
1380 if (r->flags & N_LOCAL_TEMP_PAGE) {
1385 if (cmd & OBD_BRW_WRITE) {
1386 int err = filter_commit_write(page, 0,
1392 lustre_put_page(page);
1402 for (i = 0, o = obj, r = res; i < objcount; i++, o++) {
1404 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1406 if (!(r->flags & N_LOCAL_TEMP_PAGE))
1409 err = filter_write_locked_page(r);
1418 LASSERT(!current->journal_info);
1419 current->journal_info = NULL;
1421 pop_ctxt(&saved, &obd->u.filter.fo_ctxt, NULL);
1425 static int filter_brw(int cmd, struct lustre_handle *conn,
1426 struct lov_stripe_md *lsm, obd_count oa_bufs,
1427 struct brw_page *pga, struct obd_brw_set *set)
1429 struct obd_ioobj ioo;
1430 struct niobuf_local *lnb;
1431 struct niobuf_remote *rnb;
1437 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
1438 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
1440 if ( lnb == NULL || rnb == NULL )
1441 GOTO(out, ret = -ENOMEM);
1443 for ( i = 0 ; i < oa_bufs ; i++ ) {
1444 rnb[i].offset = pga[i].off;
1445 rnb[i].len = pga[i].count;
1448 ioo.ioo_id = lsm->lsm_object_id;
1450 ioo.ioo_type = S_IFREG;
1451 ioo.ioo_bufcnt = oa_bufs;
1453 ret = filter_preprw(cmd, conn, 1, &ioo, oa_bufs, rnb, lnb,
1458 for ( i = 0; i < oa_bufs ; i++ ) {
1459 void *virt = kmap(pga[i].pg);
1460 obd_off off = pga[i].off & ~PAGE_MASK;
1462 if ( cmd & OBD_BRW_WRITE )
1463 memcpy(lnb[i].addr + off, virt + off, pga[i].count);
1465 memcpy(virt + off, lnb[i].addr + off, pga[i].count);
1470 ret = filter_commitrw(cmd, conn, 1, &ioo, oa_bufs, lnb, desc_private);
1474 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
1476 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));
1480 static int filter_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
1482 struct obd_device *obd;
1485 obd = class_conn2obd(conn);
1487 RETURN(fsfilt_statfs(obd, obd->u.filter.fo_sb, osfs));
1490 static int filter_get_info(struct lustre_handle *conn, obd_count keylen,
1491 void *key, obd_count *vallen, void **val)
1493 struct obd_device *obd;
1496 obd = class_conn2obd(conn);
1498 CDEBUG(D_IOCTL, "invalid client "LPX64"\n", conn->addr);
1502 if ( keylen == strlen("blocksize") &&
1503 memcmp(key, "blocksize", keylen) == 0 ) {
1504 *vallen = sizeof(long);
1505 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1509 if ( keylen == strlen("blocksize_bits") &&
1510 memcmp(key, "blocksize_bits", keylen) == 0 ){
1511 *vallen = sizeof(long);
1512 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1516 CDEBUG(D_IOCTL, "invalid key\n");
1520 int filter_copy_data(struct lustre_handle *dst_conn, struct obdo *dst,
1521 struct lustre_handle *src_conn, struct obdo *src,
1522 obd_size count, obd_off offset)
1525 struct lov_stripe_md srcmd, dstmd;
1526 unsigned long index = 0;
1529 memset(&srcmd, 0, sizeof(srcmd));
1530 memset(&dstmd, 0, sizeof(dstmd));
1531 srcmd.lsm_object_id = src->o_id;
1532 dstmd.lsm_object_id = dst->o_id;
1535 CDEBUG(D_INFO, "src: ino "LPU64" blocks "LPU64", size "LPU64
1536 ", dst: ino "LPU64"\n",
1537 src->o_id, src->o_blocks, src->o_size, dst->o_id);
1538 page = alloc_page(GFP_USER);
1542 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1543 while (TryLockPage(page))
1544 ___wait_on_page(page);
1546 wait_on_page_locked(page);
1549 /* XXX with brw vector I/O, we could batch up reads and writes here,
1550 * all we need to do is allocate multiple pages to handle the I/Os
1551 * and arrays to handle the request parameters.
1553 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
1555 struct obd_brw_set *set;
1557 set = obd_brw_set_new();
1565 pg.count = PAGE_SIZE;
1566 pg.off = (page->index) << PAGE_SHIFT;
1569 page->index = index;
1570 set->brw_callback = ll_brw_sync_wait;
1571 err = obd_brw(OBD_BRW_READ, src_conn, &srcmd, 1, &pg, set);
1572 obd_brw_set_free(set);
1578 set = obd_brw_set_new();
1584 pg.flag = OBD_BRW_CREATE;
1585 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
1587 set->brw_callback = ll_brw_sync_wait;
1588 err = obd_brw(OBD_BRW_WRITE, dst_conn, &dstmd, 1, &pg, set);
1589 obd_brw_set_free(set);
1591 /* XXX should handle dst->o_size, dst->o_blocks here */
1597 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
1601 dst->o_size = src->o_size;
1602 dst->o_blocks = src->o_blocks;
1603 dst->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1610 static struct obd_ops filter_obd_ops = {
1611 o_attach: filter_attach,
1612 o_detach: filter_detach,
1613 o_get_info: filter_get_info,
1614 o_setup: filter_setup,
1615 o_cleanup: filter_cleanup,
1616 o_connect: filter_connect,
1617 o_disconnect: filter_disconnect,
1618 o_statfs: filter_statfs,
1619 o_getattr: filter_getattr,
1620 o_create: filter_create,
1621 o_setattr: filter_setattr,
1622 o_destroy: filter_destroy,
1623 o_open: filter_open,
1624 o_close: filter_close,
1626 o_punch: filter_truncate,
1627 o_preprw: filter_preprw,
1628 o_commitrw: filter_commitrw
1630 o_preallocate: filter_preallocate_inodes,
1631 o_migrate: filter_migrate,
1632 o_copy: filter_copy_data,
1633 o_iterate: filter_iterate
1638 static int __init obdfilter_init(void)
1640 printk(KERN_INFO "Filtering OBD driver v0.001, info@clusterfs.com\n");
1641 filter_open_cache = kmem_cache_create("ll_filter_fdata",
1642 sizeof(struct filter_file_data),
1644 if (!filter_open_cache)
1647 filter_dentry_cache = kmem_cache_create("ll_filter_dentry",
1648 sizeof(struct filter_dentry_data),
1650 if (!filter_dentry_cache) {
1651 kmem_cache_destroy(filter_open_cache);
1655 return class_register_type(&filter_obd_ops, status_class_var,
1656 OBD_FILTER_DEVICENAME);
1659 static void __exit obdfilter_exit(void)
1661 class_unregister_type(OBD_FILTER_DEVICENAME);
1662 if (kmem_cache_destroy(filter_dentry_cache))
1663 CERROR("couldn't free obdfilter dentry cache\n");
1664 if (kmem_cache_destroy(filter_open_cache))
1665 CERROR("couldn't free obdfilter open cache\n");
1668 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1669 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1670 MODULE_LICENSE("GPL");
1672 module_init(obdfilter_init);
1673 module_exit(obdfilter_exit);