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, int * flags)
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;
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)
197 SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
199 /* perform lookup in backing fs. */
200 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
203 rc = PTR_ERR(rdentry);
205 rdentry = dget(cache_dentry);
208 if (!rc && rdentry->d_inode) {
209 *inode = smfs_get_inode(dir->i_sb, rdentry->d_inode->i_ino,
216 SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
219 smfs_update_dentry(dentry, rdentry);
220 *flags = rdentry->d_flags;
223 post_smfs_dentry(cache_dentry);
224 post_smfs_dentry(cache_parent);
229 static struct dentry * smfs_iopen_lookup(struct inode * dir,
230 struct dentry *dentry,
231 struct nameidata *nd)
233 struct dentry * alternate = NULL;
234 struct inode *inode = NULL;
239 rc = smfs_do_lookup(dir, dentry, nd, &inode, &flags);
244 /* preferrably return a connected dentry */
245 spin_lock(&dcache_lock);
246 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
247 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
250 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
251 dget_locked(alternate);
252 spin_lock(&alternate->d_lock);
253 alternate->d_flags |= DCACHE_REFERENCED;
254 spin_unlock(&alternate->d_lock);
256 spin_unlock(&dcache_lock);
260 dentry->d_flags |= DCACHE_DISCONNECTED;
262 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
263 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
264 dentry->d_inode = inode;
266 __d_rehash(dentry); /* d_rehash */
267 spin_unlock(&dcache_lock);
272 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
273 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
275 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
276 struct nameidata *nd)
279 struct dentry * rdentry = NULL;
280 struct inode * inode = NULL;
284 rc = smfs_do_lookup(dir, dentry, nd, &inode, &flags);
288 //lmv stuff. Special dentry that has no inode.
289 if (flags & DCACHE_CROSS_REF) {
293 //TODO: should flags be checked and copied before?
294 rdentry = iopen_connect_dentry(dentry, inode, 1);
299 static int smfs_link(struct dentry *old_dentry,
300 struct inode *dir, struct dentry *dentry)
302 struct inode *parent = I2CI(dentry->d_parent->d_inode);
303 struct inode *cache_dir = I2CI(dir);
304 struct inode *old_inode = old_dentry->d_inode;
305 struct inode *cache_old_inode = I2CI(old_inode);
306 struct dentry *cache_old_dentry = NULL;
307 struct dentry *cache_dentry = NULL;
308 struct dentry *cache_parent = NULL;
311 struct hook_link_msg msg = {
312 .dentry = old_dentry,
321 if (!cache_old_inode)
324 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
325 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
326 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
327 if (!cache_old_dentry || !cache_dentry || !cache_parent) {
332 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
333 if (IS_ERR(handle)) {
338 pre_smfs_inode(dir, cache_dir);
339 pre_smfs_inode(old_inode, cache_old_inode);
342 SMFS_PRE_HOOK(dir, HOOK_LINK, &msg);
344 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
346 atomic_inc(&old_inode->i_count);
347 dput(iopen_connect_dentry(dentry, old_inode, 0));
350 SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc);
352 post_smfs_inode(old_inode, cache_old_inode);
353 post_smfs_inode(dir, cache_dir);
355 smfs_trans_commit(dir, handle, 0);
359 post_smfs_dentry(cache_dentry);
360 post_smfs_dentry(cache_parent);
361 post_smfs_dentry(cache_old_dentry);
366 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
368 struct inode *cache_dir = I2CI(dir);
369 struct inode *cache_inode = I2CI(dentry->d_inode);
370 struct inode *parent = I2CI(dentry->d_parent->d_inode);
371 struct dentry *cache_dentry = NULL;
372 struct dentry *cache_parent = NULL;
376 struct hook_unlink_msg msg = {
384 LASSERT(cache_inode);
385 LASSERT(cache_dir->i_op->unlink);
388 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
389 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
390 if (!cache_dentry || !cache_parent) {
396 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
397 if (IS_ERR(handle)) {
402 pre_smfs_inode(dir, cache_dir);
403 pre_smfs_inode(dentry->d_inode, cache_inode);
405 SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg);
407 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
409 SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc);
411 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
412 post_smfs_inode(dir, cache_dir);
415 smfs_trans_commit(dir, handle, 0);
417 post_smfs_dentry(cache_dentry);
418 post_smfs_dentry(cache_parent);
422 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
425 struct inode *cache_dir = I2CI(dir);
426 struct inode *inode = NULL;
427 struct inode *parent = I2CI(dentry->d_parent->d_inode);
428 struct dentry *cache_dentry = NULL;
429 struct dentry *cache_parent = NULL;
432 struct hook_symlink_msg msg = {
434 .tgt_len = strlen(symname) + 1,
435 .symname = (char*)symname
441 LASSERT(cache_dir->i_op->symlink);
444 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
445 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
446 if (!cache_parent || !cache_dentry) {
451 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
452 if (IS_ERR(handle)) {
458 pre_smfs_inode(dir, cache_dir);
460 SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg);
462 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
464 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
467 //smfs_update_dentry(dentry, cache_dentry);
468 d_instantiate(dentry, inode);
474 SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
476 post_smfs_inode(dir, cache_dir);
477 smfs_trans_commit(dir, handle, 0);
481 post_smfs_dentry(cache_dentry);
482 post_smfs_dentry(cache_parent);
486 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
488 struct inode *cache_dir = I2CI(dir);
489 struct inode *parent = I2CI(dentry->d_parent->d_inode);
490 struct inode *inode = NULL;
491 struct dentry *cache_dentry = NULL;
492 struct dentry *cache_parent = NULL;
495 struct hook_msg msg = {
504 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
505 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
506 if (!cache_parent || !cache_dentry) {
511 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
512 if (IS_ERR(handle)) {
517 pre_smfs_inode(dir, cache_dir);
518 SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg);
520 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
522 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
525 //smsf_update_dentry(dentry, cache_dentry);
526 d_instantiate(dentry, inode);
532 SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc);
533 post_smfs_inode(dir, cache_dir);
534 smfs_trans_commit(dir, handle, 0);
537 post_smfs_dentry(cache_dentry);
538 post_smfs_dentry(cache_parent);
542 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
544 struct inode *cache_dir = I2CI(dir);
545 struct inode *cache_inode = I2CI(dentry->d_inode);
546 struct inode *parent = I2CI(dentry->d_parent->d_inode);
547 struct dentry *cache_dentry = NULL;
548 struct dentry *cache_parent = NULL;
551 struct hook_unlink_msg msg = {
559 LASSERT(cache_dir->i_op->rmdir);
562 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
563 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
564 if (!cache_parent || !cache_dentry) {
569 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
570 if (IS_ERR(handle) ) {
575 pre_smfs_inode(dir, cache_dir);
576 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
578 SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg);
580 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
582 SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc);
584 post_smfs_inode(dir, cache_dir);
585 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
586 //like vfs_rmdir is doing with inode
588 cache_dentry->d_inode->i_flags |= S_DEAD;
590 smfs_trans_commit(dir, handle, 0);
593 post_smfs_dentry(cache_dentry);
594 post_smfs_dentry(cache_parent);
598 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
599 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
602 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
603 int mode, dev_t rdev)
606 struct inode *cache_dir = I2CI(dir);
607 struct inode *inode = NULL;
608 struct inode *parent = I2CI(dentry->d_parent->d_inode);
609 struct dentry *cache_dentry = NULL;
610 struct dentry *cache_parent = NULL;
613 struct hook_msg msg = {
621 LASSERT(cache_dir->i_op->mknod);
623 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
624 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
625 if (!cache_parent || !cache_dentry) {
630 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
631 if (IS_ERR(handle)) {
636 pre_smfs_inode(dir, cache_dir);
638 SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg);
640 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
642 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
645 //smsf_update_dentry(dentry, cache_dentry);
646 d_instantiate(dentry, inode);
652 SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc);
654 post_smfs_inode(dir, cache_dir);
656 smfs_trans_commit(dir, handle, 0);
659 post_smfs_dentry(cache_dentry);
660 post_smfs_dentry(cache_parent);
664 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
665 struct inode *new_dir,struct dentry *new_dentry)
667 struct inode *cache_old_dir = I2CI(old_dir);
668 struct inode *cache_new_dir = I2CI(new_dir);
669 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
670 struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
671 struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
672 struct inode *cache_new_inode = NULL;
673 struct dentry *cache_old_dentry = NULL;
674 struct dentry *cache_new_dentry = NULL;
675 struct dentry *cache_new_parent = NULL;
676 struct dentry *cache_old_parent = NULL;
679 struct hook_rename_msg msg = {
680 .dentry = old_dentry,
682 .new_dentry = new_dentry
687 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
690 if (new_dentry->d_inode) {
691 cache_new_inode = I2CI(new_dentry->d_inode);
692 if (!cache_new_inode)
696 cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
697 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
699 if (!cache_old_parent || !cache_old_dentry) {
704 cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
705 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
707 if (!cache_new_parent || !cache_new_dentry) {
712 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
713 if (IS_ERR(handle)) {
718 pre_smfs_inode(old_dir, cache_old_dir);
719 pre_smfs_inode(new_dir, cache_new_dir);
720 if (new_dentry->d_inode)
721 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
723 SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg);
725 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
726 cache_new_dir, cache_new_dentry);
728 SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc);
730 post_smfs_inode(old_dir, cache_old_dir);
731 post_smfs_inode(new_dir, cache_new_dir);
732 if (new_dentry->d_inode)
733 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
735 smfs_trans_commit(old_dir, handle, 0);
738 post_smfs_dentry(cache_old_dentry);
739 post_smfs_dentry(cache_old_parent);
740 post_smfs_dentry(cache_new_dentry);
741 post_smfs_dentry(cache_new_parent);
745 struct inode_operations smfs_dir_iops = {
748 link: smfs_link, /* BKL held */
749 unlink: smfs_unlink, /* BKL held */
750 symlink: smfs_symlink, /* BKL held */
751 mkdir: smfs_mkdir, /* BKL held */
752 rmdir: smfs_rmdir, /* BKL held */
753 mknod: smfs_mknod, /* BKL held */
754 rename: smfs_rename, /* BKL held */
755 setxattr: smfs_setxattr, /* BKL held */
756 getxattr: smfs_getxattr, /* BKL held */
757 listxattr: smfs_listxattr, /* BKL held */
758 removexattr: smfs_removexattr, /* BKL held */
761 struct inode_operations smfs_iopen_iops = {
762 lookup: smfs_iopen_lookup,
765 static ssize_t smfs_read_dir(struct file *filp, char *buf,
766 size_t size, loff_t *ppos)
768 struct dentry *dentry = filp->f_dentry;
769 struct inode *cache_inode = NULL;
770 struct smfs_file_info *sfi = NULL;
772 loff_t *cache_ppos = NULL;
777 cache_inode = I2CI(dentry->d_inode);
779 if (!cache_inode || !cache_inode->i_fop->read)
783 if (sfi->magic != SMFS_FILE_MAGIC)
786 if (ppos != &(filp->f_pos))
787 cache_ppos = &tmp_ppos;
789 cache_ppos = &sfi->c_file->f_pos;
793 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
799 duplicate_file(filp, sfi->c_file);
804 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
806 struct dentry *dentry = filp->f_dentry;
807 struct inode *cache_inode = NULL;
808 struct smfs_file_info *sfi = NULL;
810 struct hook_readdir_msg msg = {
819 cache_inode = I2CI(dentry->d_inode);
820 if (!cache_inode || !cache_inode->i_fop->readdir)
824 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
826 SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg);
828 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
830 SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
831 duplicate_file(filp, sfi->c_file);
836 struct file_operations smfs_dir_fops = {
837 .read = smfs_read_dir,
838 .readdir = smfs_readdir, /* BKL held */
839 .ioctl = smfs_ioctl, /* BKL held */
840 .fsync = smfs_fsync, /* BKL held */
842 .release = smfs_release,
845 struct file_operations smfs_iopen_fops = {
846 .read = smfs_read_dir,