1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_SM
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/unistd.h>
31 #include <linux/smp_lock.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd_support.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_fsfilt.h>
37 #include <linux/lustre_smfs.h>
38 #include <linux/lustre_snap.h>
39 #include <linux/security.h>
40 #include "smfs_internal.h"
42 //#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 static int smfs_create(struct inode *dir, struct dentry *dentry,
48 static int smfs_create(struct inode *dir, struct dentry *dentry,
49 int mode, struct nameidata *nd)
52 struct inode *inode = NULL;
53 struct inode *parent = I2CI(dentry->d_parent->d_inode);
54 struct inode *cache_dir = I2CI(dir);
55 struct dentry *cache_dentry = NULL;
56 struct dentry *cache_parent = NULL;
58 struct hook_msg msg = {
66 LASSERT(cache_dir->i_op->create);
69 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
70 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
71 if (!cache_dentry || !cache_parent) {
76 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
82 SMFS_PRE_HOOK(dir, HOOK_CREATE, &msg);
84 pre_smfs_inode(dir, cache_dir);
86 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
87 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
89 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
92 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
95 d_instantiate(dentry, inode);
101 SMFS_POST_HOOK(dir, HOOK_CREATE, &msg, rc);
103 post_smfs_inode(dir, cache_dir);
104 smfs_trans_commit(dir, handle, 0);
108 post_smfs_dentry(cache_dentry);
109 post_smfs_dentry(cache_parent);
113 static struct dentry * iopen_connect_dentry(struct dentry * dentry,
114 struct inode *inode, int rehash)
116 struct dentry *tmp, *goal = NULL;
117 struct list_head *lp;
119 /* verify this dentry is really new */
120 LASSERT(dentry->d_inode == NULL);
121 LASSERT(list_empty(&dentry->d_alias)); /* d_instantiate */
123 LASSERT(d_unhashed(dentry)); /* d_rehash */
124 LASSERT(list_empty(&dentry->d_subdirs));
126 spin_lock(&dcache_lock);
130 /* preferrably return a connected dentry */
131 list_for_each(lp, &inode->i_dentry) {
132 tmp = list_entry(lp, struct dentry, d_alias);
133 if (tmp->d_flags & DCACHE_DISCONNECTED) {
134 LASSERT(tmp->d_alias.next == &inode->i_dentry);
135 LASSERT(tmp->d_alias.prev == &inode->i_dentry);
145 /* Move the goal to the de hash queue */
146 goal->d_flags &= ~ DCACHE_DISCONNECTED;
147 security_d_instantiate(goal, inode);
149 __d_move(goal, dentry);
150 spin_unlock(&dcache_lock);
155 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
157 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
158 dentry->d_inode = inode;
162 spin_unlock(&dcache_lock);
168 static int smfs_do_lookup (struct inode * dir,
169 struct dentry * dentry,
170 struct nameidata *nd,
171 struct inode **inode)
173 struct inode *cache_dir = I2CI(dir);
174 struct inode *parent = I2CI(dentry->d_parent->d_inode);
175 struct dentry *cache_dentry = NULL;
176 struct dentry *cache_parent = NULL;
177 struct dentry *rdentry = NULL, *tmp = NULL;
179 struct hook_msg msg = {
185 cache_dir = I2CI(dir);
189 LASSERT(cache_dir->i_op->lookup);
191 /* preparing artificial backing fs dentries. */
192 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
193 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
194 if (!cache_dentry || !cache_parent) {
199 SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
201 /* perform lookup in backing fs. */
202 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
204 if (IS_ERR(rdentry)) {
205 rc = PTR_ERR(rdentry);
210 //copy fields if DCACHE_CROSS_REF
211 smfs_update_dentry(dentry, tmp);
216 SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
218 if (tmp && tmp->d_inode) {
219 *inode = smfs_get_inode(dir->i_sb, tmp->d_inode->i_ino,
226 LASSERT(atomic_read(&rdentry->d_count) > 0);
231 post_smfs_dentry(cache_dentry);
232 post_smfs_dentry(cache_parent);
237 static struct dentry * smfs_iopen_lookup(struct inode * dir,
238 struct dentry *dentry,
239 struct nameidata *nd)
241 struct dentry * alternate = NULL;
242 struct inode *inode = NULL;
246 rc = smfs_do_lookup(dir, dentry, nd, &inode);
251 /* preferrably return a connected dentry */
252 spin_lock(&dcache_lock);
253 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
254 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
257 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
258 dget_locked(alternate);
259 spin_lock(&alternate->d_lock);
260 alternate->d_flags |= DCACHE_REFERENCED;
261 spin_unlock(&alternate->d_lock);
263 spin_unlock(&dcache_lock);
267 dentry->d_flags |= DCACHE_DISCONNECTED;
269 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
270 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
271 dentry->d_inode = inode;
273 __d_rehash(dentry); /* d_rehash */
274 spin_unlock(&dcache_lock);
279 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
280 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
282 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
283 struct nameidata *nd)
286 struct dentry * rdentry = NULL;
287 struct inode * inode = NULL;
290 rc = smfs_do_lookup(dir, dentry, nd, &inode);
294 //lmv stuff. Special dentry that has no inode.
295 if (dentry->d_flags & DCACHE_CROSS_REF) {
299 //TODO: should flags be checked and copied before?
300 rdentry = iopen_connect_dentry(dentry, inode, 1);
305 static int smfs_link(struct dentry *old_dentry,
306 struct inode *dir, struct dentry *dentry)
308 struct inode *parent = I2CI(dentry->d_parent->d_inode);
309 struct inode *cache_dir = I2CI(dir);
310 struct inode *old_inode = old_dentry->d_inode;
311 struct inode *cache_old_inode = I2CI(old_inode);
312 struct dentry *cache_old_dentry = NULL;
313 struct dentry *cache_dentry = NULL;
314 struct dentry *cache_parent = NULL;
317 struct hook_link_msg msg = {
318 .dentry = old_dentry,
327 if (!cache_old_inode)
330 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
331 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
332 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
333 if (!cache_old_dentry || !cache_dentry || !cache_parent) {
338 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
339 if (IS_ERR(handle)) {
344 pre_smfs_inode(dir, cache_dir);
345 pre_smfs_inode(old_inode, cache_old_inode);
348 SMFS_PRE_HOOK(dir, HOOK_LINK, &msg);
350 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
352 atomic_inc(&old_inode->i_count);
353 dput(iopen_connect_dentry(dentry, old_inode, 0));
356 SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc);
358 post_smfs_inode(old_inode, cache_old_inode);
359 post_smfs_inode(dir, cache_dir);
361 smfs_trans_commit(dir, handle, 0);
365 post_smfs_dentry(cache_dentry);
366 post_smfs_dentry(cache_parent);
367 post_smfs_dentry(cache_old_dentry);
372 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
374 struct inode *cache_dir = I2CI(dir);
375 struct inode *cache_inode = I2CI(dentry->d_inode);
376 struct inode *parent = I2CI(dentry->d_parent->d_inode);
377 struct dentry *cache_dentry = NULL;
378 struct dentry *cache_parent = NULL;
382 struct hook_unlink_msg msg = {
390 LASSERT(cache_inode);
391 LASSERT(cache_dir->i_op->unlink);
394 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
395 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
396 if (!cache_dentry || !cache_parent) {
402 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
403 if (IS_ERR(handle)) {
408 pre_smfs_inode(dir, cache_dir);
409 pre_smfs_inode(dentry->d_inode, cache_inode);
411 SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg);
413 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
415 SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc);
417 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
418 post_smfs_inode(dir, cache_dir);
421 smfs_trans_commit(dir, handle, 0);
423 post_smfs_dentry(cache_dentry);
424 post_smfs_dentry(cache_parent);
428 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
431 struct inode *cache_dir = I2CI(dir);
432 struct inode *inode = NULL;
433 struct inode *parent = I2CI(dentry->d_parent->d_inode);
434 struct dentry *cache_dentry = NULL;
435 struct dentry *cache_parent = NULL;
438 struct hook_symlink_msg msg = {
440 .tgt_len = strlen(symname) + 1,
441 .symname = (char*)symname
447 LASSERT(cache_dir->i_op->symlink);
450 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
451 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
452 if (!cache_parent || !cache_dentry) {
457 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
458 if (IS_ERR(handle)) {
464 pre_smfs_inode(dir, cache_dir);
466 SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg);
468 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
470 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
473 //smfs_update_dentry(dentry, cache_dentry);
474 d_instantiate(dentry, inode);
480 SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
482 post_smfs_inode(dir, cache_dir);
483 smfs_trans_commit(dir, handle, 0);
487 post_smfs_dentry(cache_dentry);
488 post_smfs_dentry(cache_parent);
492 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
494 struct inode *cache_dir = I2CI(dir);
495 struct inode *parent = I2CI(dentry->d_parent->d_inode);
496 struct inode *inode = NULL;
497 struct dentry *cache_dentry = NULL;
498 struct dentry *cache_parent = NULL;
501 struct hook_msg msg = {
510 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
511 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
512 if (!cache_parent || !cache_dentry) {
517 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
518 if (IS_ERR(handle)) {
523 pre_smfs_inode(dir, cache_dir);
524 SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg);
526 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
528 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
531 //smsf_update_dentry(dentry, cache_dentry);
532 d_instantiate(dentry, inode);
538 SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc);
539 post_smfs_inode(dir, cache_dir);
540 smfs_trans_commit(dir, handle, 0);
543 post_smfs_dentry(cache_dentry);
544 post_smfs_dentry(cache_parent);
548 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
550 struct inode *cache_dir = I2CI(dir);
551 struct inode *cache_inode = I2CI(dentry->d_inode);
552 struct inode *parent = I2CI(dentry->d_parent->d_inode);
553 struct dentry *cache_dentry = NULL;
554 struct dentry *cache_parent = NULL;
557 struct hook_unlink_msg msg = {
565 LASSERT(cache_dir->i_op->rmdir);
568 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
569 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
570 if (!cache_parent || !cache_dentry) {
575 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
576 if (IS_ERR(handle) ) {
581 pre_smfs_inode(dir, cache_dir);
582 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
584 SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg);
586 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
588 SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc);
590 post_smfs_inode(dir, cache_dir);
591 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
592 //like vfs_rmdir is doing with inode
594 cache_dentry->d_inode->i_flags |= S_DEAD;
596 smfs_trans_commit(dir, handle, 0);
599 post_smfs_dentry(cache_dentry);
600 post_smfs_dentry(cache_parent);
604 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
605 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
608 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
609 int mode, dev_t rdev)
612 struct inode *cache_dir = I2CI(dir);
613 struct inode *inode = NULL;
614 struct inode *parent = I2CI(dentry->d_parent->d_inode);
615 struct dentry *cache_dentry = NULL;
616 struct dentry *cache_parent = NULL;
619 struct hook_msg msg = {
627 LASSERT(cache_dir->i_op->mknod);
629 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
630 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
631 if (!cache_parent || !cache_dentry) {
636 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
637 if (IS_ERR(handle)) {
642 pre_smfs_inode(dir, cache_dir);
644 SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg);
646 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
648 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
651 //smsf_update_dentry(dentry, cache_dentry);
652 d_instantiate(dentry, inode);
658 SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc);
660 post_smfs_inode(dir, cache_dir);
662 smfs_trans_commit(dir, handle, 0);
665 post_smfs_dentry(cache_dentry);
666 post_smfs_dentry(cache_parent);
670 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
671 struct inode *new_dir,struct dentry *new_dentry)
673 struct inode *cache_old_dir = I2CI(old_dir);
674 struct inode *cache_new_dir = I2CI(new_dir);
675 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
676 struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
677 struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
678 struct inode *cache_new_inode = NULL;
679 struct dentry *cache_old_dentry = NULL;
680 struct dentry *cache_new_dentry = NULL;
681 struct dentry *cache_new_parent = NULL;
682 struct dentry *cache_old_parent = NULL;
685 struct hook_rename_msg msg = {
686 .dentry = old_dentry,
688 .new_dentry = new_dentry
693 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
696 if (new_dentry->d_inode) {
697 cache_new_inode = I2CI(new_dentry->d_inode);
698 if (!cache_new_inode)
702 cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
703 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
705 if (!cache_old_parent || !cache_old_dentry) {
710 cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
711 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
713 if (!cache_new_parent || !cache_new_dentry) {
718 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
719 if (IS_ERR(handle)) {
724 pre_smfs_inode(old_dir, cache_old_dir);
725 pre_smfs_inode(new_dir, cache_new_dir);
726 if (new_dentry->d_inode)
727 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
729 SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg);
731 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
732 cache_new_dir, cache_new_dentry);
734 SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc);
736 post_smfs_inode(old_dir, cache_old_dir);
737 post_smfs_inode(new_dir, cache_new_dir);
738 if (new_dentry->d_inode)
739 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
741 smfs_trans_commit(old_dir, handle, 0);
744 post_smfs_dentry(cache_old_dentry);
745 post_smfs_dentry(cache_old_parent);
746 post_smfs_dentry(cache_new_dentry);
747 post_smfs_dentry(cache_new_parent);
751 struct inode_operations smfs_dir_iops = {
754 link: smfs_link, /* BKL held */
755 unlink: smfs_unlink, /* BKL held */
756 symlink: smfs_symlink, /* BKL held */
757 mkdir: smfs_mkdir, /* BKL held */
758 rmdir: smfs_rmdir, /* BKL held */
759 mknod: smfs_mknod, /* BKL held */
760 rename: smfs_rename, /* BKL held */
761 setxattr: smfs_setxattr, /* BKL held */
762 getxattr: smfs_getxattr, /* BKL held */
763 listxattr: smfs_listxattr, /* BKL held */
764 removexattr: smfs_removexattr, /* BKL held */
767 struct inode_operations smfs_iopen_iops = {
768 lookup: smfs_iopen_lookup,
771 static ssize_t smfs_read_dir(struct file *filp, char *buf,
772 size_t size, loff_t *ppos)
774 struct dentry *dentry = filp->f_dentry;
775 struct inode *cache_inode = NULL;
776 struct smfs_file_info *sfi = NULL;
778 loff_t *cache_ppos = NULL;
783 cache_inode = I2CI(dentry->d_inode);
785 if (!cache_inode || !cache_inode->i_fop->read)
789 if (sfi->magic != SMFS_FILE_MAGIC)
792 if (ppos != &(filp->f_pos))
793 cache_ppos = &tmp_ppos;
795 cache_ppos = &sfi->c_file->f_pos;
799 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
805 duplicate_file(filp, sfi->c_file);
810 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
812 struct dentry *dentry = filp->f_dentry;
813 struct inode *cache_inode = NULL;
814 struct smfs_file_info *sfi = NULL;
816 struct hook_readdir_msg msg = {
825 cache_inode = I2CI(dentry->d_inode);
826 if (!cache_inode || !cache_inode->i_fop->readdir)
830 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
832 SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg);
834 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
836 SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
837 duplicate_file(filp, sfi->c_file);
842 struct file_operations smfs_dir_fops = {
843 .read = smfs_read_dir,
844 .readdir = smfs_readdir, /* BKL held */
845 .ioctl = smfs_ioctl, /* BKL held */
846 .fsync = smfs_fsync, /* BKL held */
848 .release = smfs_release,
851 struct file_operations smfs_iopen_fops = {
852 .read = smfs_read_dir,