Whamcloud - gitweb
- landed b_hd_mdref (mostly WB cache fixes)
[fs/lustre-release.git] / lustre / smfs / dir.c
index 423aab8..bea797e 100644 (file)
 #include <linux/lustre_fsfilt.h>
 #include <linux/lustre_smfs.h>
 #include <linux/lustre_snap.h>
-
+#include <linux/security.h>
 #include "smfs_internal.h"
 
-#define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
+//#define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
 
 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
 static int smfs_create(struct inode *dir, struct dentry *dentry,
@@ -49,8 +49,9 @@ static int smfs_create(struct inode *dir, struct dentry *dentry,
                        int mode, struct nameidata *nd)
 #endif
 {
-        struct inode  *inode = NULL;
-        struct inode  *cache_dir = NULL;
+        struct inode *inode = NULL;
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
+        struct inode *cache_dir = I2CI(dir);
         struct dentry *cache_dentry = NULL;
         struct dentry *cache_parent = NULL;
         void *handle = NULL;
@@ -60,13 +61,15 @@ static int smfs_create(struct inode *dir, struct dentry *dentry,
         int rc = 0;
         
         ENTRY;
-
-        cache_dir = I2CI(dir);
+        
+        CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s (%p),dir=%lu/%u\n",
+               dentry->d_name.len, dentry->d_name.name, dentry,
+               dir->i_ino, dir->i_generation);
+        
         LASSERT(cache_dir);
         LASSERT(cache_dir->i_op->create);
-
-        //lock_kernel();
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
+        
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
         if (!cache_dentry || !cache_parent) {
                 rc = -ENOMEM;
@@ -88,10 +91,12 @@ static int smfs_create(struct inode *dir, struct dentry *dentry,
 #else
         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
 #endif
-        if (!rc) {        
-                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
-                if (inode)
+        if (!rc) {
+                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
+                                       I2SMI(dir), 0);
+                if (inode) {
                         d_instantiate(dentry, inode);
+                }
                 else
                         rc = -ENOENT;
         }
@@ -100,27 +105,78 @@ static int smfs_create(struct inode *dir, struct dentry *dentry,
 
         post_smfs_inode(dir, cache_dir);
         smfs_trans_commit(dir, handle, 0);
-
+        
 exit:
-        //unlock_kernel();
         post_smfs_dentry(cache_dentry);
         post_smfs_dentry(cache_parent);
         RETURN(rc);
 }
 
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
-#else
-static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
-                                  struct nameidata *nd)
-#endif
+static struct dentry * iopen_connect_dentry(struct dentry * dentry,
+                                     struct inode *inode, int rehash)
 {
-        struct inode *cache_dir;
-        struct inode *inode = NULL;
-        struct inode * cache_inode = NULL;
+       struct dentry *tmp, *goal = NULL;
+       struct list_head *lp;
+
+       /* verify this dentry is really new */
+       LASSERT(dentry->d_inode == NULL);
+       LASSERT(list_empty(&dentry->d_alias));          /* d_instantiate */
+       if (rehash)
+               LASSERT(d_unhashed(dentry));    /* d_rehash */
+       LASSERT(list_empty(&dentry->d_subdirs));
+
+       spin_lock(&dcache_lock);
+       if (!inode)
+               goto do_rehash;
+
+       /* preferrably return a connected dentry */
+       list_for_each(lp, &inode->i_dentry) {
+               tmp = list_entry(lp, struct dentry, d_alias);
+               if (tmp->d_flags & DCACHE_DISCONNECTED) {
+                       LASSERT(tmp->d_alias.next == &inode->i_dentry);
+                       LASSERT(tmp->d_alias.prev == &inode->i_dentry);
+                       goal = tmp;
+                       dget_locked(goal);
+                       break;
+               }
+       }
+
+       if (!goal)
+               goto do_instantiate;
+
+       /* Move the goal to the de hash queue */
+       goal->d_flags &= ~ DCACHE_DISCONNECTED;
+       security_d_instantiate(goal, inode);
+        __d_rehash(dentry);
+        __d_move(goal, dentry);
+       spin_unlock(&dcache_lock);
+       iput(inode);
+
+       RETURN(goal);
+
+       /* d_add(), but don't drop dcache_lock before adding dentry to inode */
+do_instantiate:
+       list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
+       dentry->d_inode = inode;
+do_rehash:
+       if (rehash)
+                __d_rehash(dentry);
+       spin_unlock(&dcache_lock);
+
+       RETURN(NULL);
+
+}
+
+static int smfs_do_lookup (struct inode * dir, 
+                           struct dentry * dentry,
+                           struct nameidata *nd,
+                           struct inode **inode)
+{
+        struct inode *cache_dir = I2CI(dir);
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
         struct dentry *cache_dentry = NULL;
         struct dentry *cache_parent = NULL;
-        struct dentry *rdentry = NULL;
+        struct dentry *rdentry = NULL, *tmp = NULL;
         int rc = 0;
         struct hook_msg msg = {
                 .dentry = dentry,
@@ -128,79 +184,193 @@ static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
 
         ENTRY;
         
-        cache_dir = I2CI(dir);
+        CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s (%p),dir=%lu/%u\n",
+               dentry->d_name.len, dentry->d_name.name, dentry,
+               dir->i_ino, dir->i_generation);
+
         if (!cache_dir)
-                RETURN(ERR_PTR(-ENOENT));
+                RETURN(-ENOENT);
 
         LASSERT(cache_dir->i_op->lookup);
 
         /* preparing artificial backing fs dentries. */
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
-        if (!cache_dentry || !cache_parent) 
-                RETURN(ERR_PTR(-ENOMEM));
+        if (!cache_dentry || !cache_parent) {
+                rc = -ENOMEM;
+                goto exit;
+        }
+        
+        SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
         
-        SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg); 
-
         /* perform lookup in backing fs. */
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-        rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry);
-#else
         rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
-#endif
         if (rdentry) {
-                if (IS_ERR(rdentry))
+                if (IS_ERR(rdentry)) {
                         rc = PTR_ERR(rdentry);
-                else {
-                        cache_inode = rdentry->d_inode;
-                        dput(rdentry);
+                        rdentry = NULL;
+                } else {
+                        tmp = rdentry;
                 }
         } else {
-                cache_inode = cache_dentry->d_inode;
+                tmp = cache_dentry;
+        }
+
+        SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
+     
+        if (tmp) {
+                //copy fields if DCACHE_CROSS_REF
+                smfs_update_dentry(dentry, tmp);         
+                
+                if (tmp->d_inode) {
+                        *inode = smfs_get_inode(dir->i_sb, tmp->d_inode, 
+                                                I2SMI(dir), 0); 
+                        if (!(*inode))
+                                rc = -ENOENT;
+                        else {
+                                if (!tmp->d_inode->i_nlink) {
+                                        struct inode * ind = tmp->d_inode;
+                                
+                                        CWARN("inode #%lu (%*s) nlink is %i/%i\n",
+                                              ind->i_ino, tmp->d_name.len,
+                                              tmp->d_name.name, ind->i_nlink,
+                                              (*inode)->i_nlink);
+                                        CWARN("parent #%lu (%*s) nlink is %i\n",
+                                              dir->i_ino, tmp->d_parent->d_name.len,
+                                              tmp->d_parent->d_name.name,
+                                              cache_dir->i_nlink);
+                                }
+                        }
+                }
         }
         
-        if (cache_inode) { 
-                inode = smfs_get_inode(dir->i_sb, cache_inode->i_ino, dir,0);
-                if (!inode)
-                        rc = -ENOENT;
+        if (rdentry) {
+                dput(rdentry);
         }
-
-        if (!rc)
-                d_add(dentry, inode);
         
-        SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
+exit:
         post_smfs_dentry(cache_dentry);
         post_smfs_dentry(cache_parent);
-        RETURN(ERR_PTR(rc));
+        
+        RETURN(rc);
+}
+
+static struct dentry * smfs_iopen_lookup(struct inode * dir, 
+                                         struct dentry *dentry,
+                                        struct nameidata *nd)
+{
+        struct dentry * alternate = NULL;
+       struct inode *inode = NULL;
+        int rc = 0;
+        ENTRY;
+        
+        rc = smfs_do_lookup(dir, dentry, nd, &inode);
+       if (rc)
+                RETURN(ERR_PTR(rc));
+        
+        LASSERT(inode);
+        /* preferrably return a connected dentry */
+       spin_lock(&dcache_lock);
+       list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
+               LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
+       }
+
+       list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
+               dget_locked(alternate);
+               spin_lock(&alternate->d_lock);
+               alternate->d_flags |= DCACHE_REFERENCED;
+               spin_unlock(&alternate->d_lock);
+               iput(inode);
+               spin_unlock(&dcache_lock);
+               RETURN(alternate);
+       }
+        
+       dentry->d_flags |= DCACHE_DISCONNECTED;
+
+       /* d_add(), but don't drop dcache_lock before adding dentry to inode */
+       list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
+       dentry->d_inode = inode;
+
+       __d_rehash(dentry);                             /* d_rehash */
+       spin_unlock(&dcache_lock);
+
+       return NULL;
 }
 
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
+static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
+#else
+static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
+                                  struct nameidata *nd)
+#endif
+{
+        struct dentry * rdentry = NULL;
+        struct inode * inode = NULL;
+        int rc;
+
+        ENTRY;
+        
+        rc = smfs_do_lookup(dir, dentry, nd, &inode);
+        if (rc)
+                RETURN(ERR_PTR(rc));
+        
+        //lmv stuff. Special dentry that has no inode.
+        if (dentry->d_flags & DCACHE_CROSS_REF) {
+                d_add(dentry, NULL);
+                RETURN(NULL);
+        }
+        //TODO: should flags be checked and copied before?        
+        rdentry = iopen_connect_dentry(dentry, inode, 1);
+        
+        RETURN(rdentry);
+}
+
+#if HAVE_LOOKUP_RAW
+static int smfs_lookup_raw(struct inode *dir, const char *name,
+                           int len, ino_t *data)
+{
+        struct inode *cache_dir = I2CI(dir);
+        int rc = 0;
+
+        if (!cache_dir)
+                RETURN(-ENOENT);
+        
+        if (cache_dir->i_op->lookup_raw) {
+                rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
+        } else {
+                CWARN("do not have raw lookup ops in bottom fs\n");
+        }
+
+        RETURN(rc);
+}
+#endif
+
 static int smfs_link(struct dentry *old_dentry,
                      struct inode *dir, struct dentry *dentry)
 {
-        struct inode *cache_old_inode = NULL;
-        struct inode *cache_dir = NULL;
-        struct inode *old_inode = NULL;
-        struct dentry *cache_dentry = NULL;
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
+        struct inode *cache_dir = I2CI(dir);
+        struct inode *old_inode = old_dentry->d_inode;
+        struct inode *cache_old_inode = I2CI(old_inode);
         struct dentry *cache_old_dentry = NULL;
+        struct dentry *cache_dentry = NULL;
         struct dentry *cache_parent = NULL;
         void *handle = NULL;
         int rc = 0;
-        struct hook_msg msg = {
+        struct hook_link_msg msg = {
                 .dentry = old_dentry,
+                .new_dentry = dentry
         };
 
         ENTRY;
 
-        cache_dir = I2CI(dir);
         if (!cache_dir)
                 RETURN(-ENOENT);
         
-        old_inode = old_dentry->d_inode;        
-        cache_old_inode = I2CI(old_inode);
         if (!cache_old_inode)
                 RETURN(-ENOENT);
         
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
         cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
         if (!cache_old_dentry || !cache_dentry || !cache_parent) {
@@ -223,7 +393,8 @@ static int smfs_link(struct dentry *old_dentry,
         rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
         if (!rc) {
                 atomic_inc(&old_inode->i_count);
-                d_instantiate(dentry, old_inode);
+                old_inode->i_nlink++;
+                dput(iopen_connect_dentry(dentry, old_inode, 0));
         }
 
         SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc); 
@@ -246,35 +417,41 @@ static int smfs_unlink(struct inode * dir, struct dentry *dentry)
 {
         struct inode *cache_dir = I2CI(dir);
         struct inode *cache_inode = I2CI(dentry->d_inode);
-        struct dentry *cache_dentry;
-        struct dentry *cache_parent;
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
+        struct dentry *cache_dentry = NULL;
+        struct dentry *cache_parent = NULL;
         void   *handle = NULL;
         int    rc = 0;
         //int    mode = 0;
         struct hook_unlink_msg msg = {
                 .dentry = dentry,
-                .mode = 0
+                .mode = dentry->d_inode->i_mode
         };
 
         ENTRY;
         
-        if (!cache_dir || !cache_inode || !cache_dir->i_op->unlink)
-                RETURN(-ENOENT);
+        LASSERT(cache_dir);
+        LASSERT(cache_inode);
+        LASSERT(cache_dir->i_op->unlink);
+        LASSERT(parent);
+        
+        CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s (%p),dir=%lu/%u\n",
+               dentry->d_name.len, dentry->d_name.name, dentry,
+               dir->i_ino, dir->i_generation);
 
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
         if (!cache_dentry || !cache_parent) {
                 rc = -ENOMEM;
                 goto exit;
         }
                 
-        //lock_kernel();
         handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
         if (IS_ERR(handle)) {
                 rc = -ENOSPC;
                 goto exit;
         }
-
+        
         pre_smfs_inode(dir, cache_dir);
         pre_smfs_inode(dentry->d_inode, cache_inode);
 
@@ -283,10 +460,11 @@ static int smfs_unlink(struct inode * dir, struct dentry *dentry)
         rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
                 
         SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc); 
-
-        post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
-        post_smfs_inode(dir, cache_dir);
-        //unlock_kernel();
+        if (!rc) {
+                post_smfs_inode(dentry->d_inode, cache_inode);
+                dentry->d_inode->i_nlink--;
+                post_smfs_inode(dir, cache_dir);
+        }
         
         smfs_trans_commit(dir, handle, 0);
 exit:
@@ -300,8 +478,9 @@ static int smfs_symlink(struct inode *dir, struct dentry *dentry,
 {
         struct inode *cache_dir = I2CI(dir);
         struct inode *inode = NULL;
-        struct dentry *cache_dentry;
-        struct dentry *cache_parent;
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
+        struct dentry *cache_dentry = NULL;
+        struct dentry *cache_parent = NULL;
         void   *handle = NULL;
         int    rc = 0;
         struct hook_symlink_msg msg = {
@@ -312,10 +491,11 @@ static int smfs_symlink(struct inode *dir, struct dentry *dentry,
 
         ENTRY;
         
-        if (!cache_dir || !cache_dir->i_op->symlink)
-                RETURN(-ENOENT);
+        LASSERT(cache_dir);
+        LASSERT(cache_dir->i_op->symlink);
+        LASSERT(parent);
 
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
         if (!cache_parent || !cache_dentry) {
                 rc = -ENOMEM;
@@ -328,17 +508,17 @@ static int smfs_symlink(struct inode *dir, struct dentry *dentry,
                 goto exit;
         }
         
-        //lock_kernel();
         pre_smfs_inode(dir, cache_dir);
 
         SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg); 
         
         rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
         if (!rc) {        
-                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
-                                       dir, 0);
-                if (inode)
+                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
+                                       I2SMI(dir), 0);
+                if (inode) {
                         d_instantiate(dentry, inode);
+                }
                 else
                         rc = -ENOENT;
         }
@@ -349,19 +529,18 @@ static int smfs_symlink(struct inode *dir, struct dentry *dentry,
         smfs_trans_commit(dir, handle, 0);
 
 exit:
-        //unlock_kernel();
         post_smfs_dentry(cache_dentry);
         post_smfs_dentry(cache_parent);
         RETURN(rc);
 }
 
-static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
-                      int mode)
+static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 {
         struct inode *cache_dir = I2CI(dir);
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
         struct inode *inode = NULL;
-        struct dentry *cache_dentry;
-        struct dentry *cache_parent;
+        struct dentry *cache_dentry = NULL;
+        struct dentry *cache_parent = NULL;
         void   *handle = NULL;
         int    rc = 0;
         struct hook_msg msg = {
@@ -370,12 +549,15 @@ static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
 
         ENTRY;
         
-        if (!cache_dir)
-                RETURN(-ENOENT);
-
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
+        LASSERT(cache_dir);
+        LASSERT(parent);
+        
+        CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s (%p),dir=%lu/%u\n",
+               dentry->d_name.len, dentry->d_name.name, dentry,
+               dir->i_ino, dir->i_generation);
+        
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
-
         if (!cache_parent || !cache_dentry) {
                 rc = -ENOMEM;
                 goto exit;
@@ -392,10 +574,12 @@ static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
         
         rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
         if (!rc) {
-                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
-                                       dir, 0);
-                if (inode)
+                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
+                                       I2SMI(dir), 0);
+                if (inode) {
+                        dir->i_nlink++;
                         d_instantiate(dentry, inode);
+                }
                 else
                         rc = -ENOENT;
         }
@@ -414,8 +598,10 @@ static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
 {
         struct inode *cache_dir = I2CI(dir);
         struct inode *cache_inode = I2CI(dentry->d_inode);
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
         struct dentry *cache_dentry = NULL;
         struct dentry *cache_parent = NULL;
+        struct inode * inode = dentry->d_inode;
         void *handle = NULL;
         int    rc = 0;
         struct hook_unlink_msg msg = {
@@ -425,10 +611,15 @@ static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
 
         ENTRY;
         
-        if (!cache_dir || !cache_dir->i_op->rmdir)
-                RETURN(-ENOENT);
+        CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s (%p),dir=%lu/%u\n",
+               dentry->d_name.len, dentry->d_name.name, dentry,
+               dir->i_ino, dir->i_generation);
+
+        LASSERT(cache_dir);
+        LASSERT(cache_dir->i_op->rmdir);
+        LASSERT(parent);
 
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
         if (!cache_parent || !cache_dentry) {
                 rc = -ENOMEM;
@@ -441,20 +632,29 @@ static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
                 goto exit;
         }
 
+        dentry_unhash(cache_dentry);
+
         pre_smfs_inode(dir, cache_dir);
-        pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
+        pre_smfs_inode(inode, cache_inode);
         
         SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg); 
-
+        
         rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
               
         SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc); 
-        
-        post_smfs_inode(dir, cache_dir);
-        post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
-
+        if (!rc) {
+                if (inode->i_nlink != 2)
+                        CWARN("Directory #%lu under rmdir has %i nlinks\n",
+                                inode->i_ino, inode->i_nlink);
+                inode->i_nlink = 0;
+                dir->i_nlink--;
+                post_smfs_inode(dir, cache_dir);
+                post_smfs_inode(inode, cache_inode);
+                //like vfs_rmdir is doing with inode
+                cache_inode->i_flags |= S_DEAD;
+        }
         smfs_trans_commit(dir, handle, 0);
-
+        dput(cache_dentry);
 exit:
         post_smfs_dentry(cache_dentry);
         post_smfs_dentry(cache_parent);
@@ -471,6 +671,7 @@ static int smfs_mknod(struct inode *dir, struct dentry *dentry,
 {
         struct inode *cache_dir = I2CI(dir);
         struct inode *inode = NULL;
+        struct inode *parent = I2CI(dentry->d_parent->d_inode);
         struct dentry *cache_dentry = NULL;
         struct dentry *cache_parent = NULL;
         void *handle = NULL;
@@ -481,10 +682,11 @@ static int smfs_mknod(struct inode *dir, struct dentry *dentry,
  
         ENTRY;
         
-        if (!cache_dir || !cache_dir->i_op->mknod)
-                RETURN(-ENOENT);
+        LASSERT(parent);
+        LASSERT(cache_dir);
+        LASSERT(cache_dir->i_op->mknod);
 
-        cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
+        cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
         if (!cache_parent || !cache_dentry) {
                 rc = -ENOMEM;
@@ -498,16 +700,17 @@ static int smfs_mknod(struct inode *dir, struct dentry *dentry,
         }
         
         pre_smfs_inode(dir, cache_dir);
-        pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
-
+        
         SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg); 
         
         rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
         if (!rc) {
-                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
-                                       dir, 0);
-                if (inode)
+                inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
+                                       I2SMI(dir), 0);
+                if (inode) {
+                        //smsf_update_dentry(dentry, cache_dentry);
                         d_instantiate(dentry, inode);
+                }
                 else
                         rc = -ENOENT;
         }
@@ -515,7 +718,7 @@ static int smfs_mknod(struct inode *dir, struct dentry *dentry,
         SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc); 
         
         post_smfs_inode(dir, cache_dir);
-        post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
+        
         smfs_trans_commit(dir, handle, 0);
 
 exit:
@@ -529,7 +732,10 @@ static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 {
         struct inode *cache_old_dir = I2CI(old_dir);
         struct inode *cache_new_dir = I2CI(new_dir);
+        struct inode *new_inode = new_dentry->d_inode;
         struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
+        struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
+        struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
         struct inode *cache_new_inode = NULL;
         struct dentry *cache_old_dentry = NULL;
         struct dentry *cache_new_dentry = NULL;
@@ -540,7 +746,8 @@ static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
         struct hook_rename_msg msg = {
                 .dentry = old_dentry,
                 .new_dir = new_dir,
-                .new_dentry = new_dentry
+                .old_dir = old_dir,
+                .new_dentry = new_dentry,
         };
 
         ENTRY;
@@ -548,13 +755,13 @@ static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
         if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
                 RETURN(-ENOENT);
 
-        if (new_dentry->d_inode) {
-                cache_new_inode = I2CI(new_dentry->d_inode);
+        if (new_inode) {
+                cache_new_inode = I2CI(new_inode);
                 if (!cache_new_inode)
                         RETURN(-ENOENT);
         }
         
-        cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
+        cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
                                            old_dentry);
         if (!cache_old_parent || !cache_old_dentry) {
@@ -562,7 +769,7 @@ static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
                 goto exit;
         }
         
-        cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
+        cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
         cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
                                            new_dentry);
         if (!cache_new_parent || !cache_new_dentry) {
@@ -578,8 +785,9 @@ static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
         
         pre_smfs_inode(old_dir, cache_old_dir);
         pre_smfs_inode(new_dir, cache_new_dir);
-        if (new_dentry->d_inode)
-                pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
+        pre_smfs_inode(old_dentry->d_inode, cache_old_inode);
+        if (new_inode)
+                pre_smfs_inode(new_inode, cache_new_inode);
 
         SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg); 
         
@@ -587,12 +795,24 @@ static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
                                          cache_new_dir, cache_new_dentry);
         
         SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc); 
-
-        post_smfs_inode(old_dir, cache_old_dir);
-        post_smfs_inode(new_dir, cache_new_dir);
-        if (new_dentry->d_inode)
-                post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
-        
+        if (!rc) {
+                post_smfs_inode(old_dir, cache_old_dir);
+                post_smfs_inode(new_dir, cache_new_dir);
+                post_smfs_inode(old_dentry->d_inode, cache_old_inode);
+                if (new_inode) {
+                        post_smfs_inode(new_inode, cache_new_inode);
+                        new_inode->i_nlink--;
+                }
+                //directory is renamed
+                if (S_ISDIR(old_dentry->d_inode->i_mode)) {
+                        old_dir->i_nlink--;
+                        if (new_inode) {
+                                new_inode->i_nlink--;
+                        } else {
+                                new_dir->i_nlink++;
+                        }
+                }
+        }
         smfs_trans_commit(old_dir, handle, 0);
         
 exit:
@@ -604,19 +824,27 @@ exit:
 }
 
 struct inode_operations smfs_dir_iops = {
-        create:         smfs_create,
-        lookup:         smfs_lookup,
-        link:           smfs_link,              /* BKL held */
-        unlink:         smfs_unlink,            /* BKL held */
-        symlink:        smfs_symlink,           /* BKL held */
-        mkdir:          smfs_mkdir,             /* BKL held */
-        rmdir:          smfs_rmdir,             /* BKL held */
-        mknod:          smfs_mknod,             /* BKL held */
-        rename:         smfs_rename,            /* BKL held */
-        setxattr:       smfs_setxattr,          /* BKL held */
-        getxattr:       smfs_getxattr,          /* BKL held */
-        listxattr:      smfs_listxattr,         /* BKL held */
-        removexattr:    smfs_removexattr,       /* BKL held */
+        .create         = smfs_create,
+        .lookup         = smfs_lookup,
+#if HAVE_LOOKUP_RAW
+        .lookup_raw     = smfs_lookup_raw,
+#endif
+        .link           = smfs_link,              
+        .unlink         = smfs_unlink,            
+        .symlink        = smfs_symlink,           
+        .mkdir          = smfs_mkdir,             
+        .rmdir          = smfs_rmdir,             
+        .mknod          = smfs_mknod,             
+        .rename         = smfs_rename,            
+        .setxattr       = smfs_setxattr,
+        .getxattr       = smfs_getxattr,
+        .listxattr      = smfs_listxattr,
+        .removexattr    = smfs_removexattr,
+        .permission     = smfs_permission,
+};
+
+struct inode_operations smfs_iopen_iops = {
+        .lookup         = smfs_iopen_lookup,
 };
 
 static ssize_t smfs_read_dir(struct file *filp, char *buf,
@@ -664,12 +892,6 @@ static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
         struct inode *cache_inode = NULL;
         struct smfs_file_info *sfi = NULL;
         int    rc = 0;
-        struct hook_readdir_msg msg = {
-                .dentry = dentry,
-                .filp = filp,
-                .dirent = dirent,
-                .filldir = filldir
-        };
 
         ENTRY;
         
@@ -680,16 +902,10 @@ static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
         sfi = F2SMFI(filp);
         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
 
-        SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg); 
-        
         rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
         
-        SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
         duplicate_file(filp, sfi->c_file);
 
-        if (rc > 0)
-                rc = 0;
-
         RETURN(rc);
 }
 
@@ -701,3 +917,8 @@ struct file_operations smfs_dir_fops = {
         .open           = smfs_open,
         .release        = smfs_release,
 };
+
+struct file_operations smfs_iopen_fops = {
+        .read           = smfs_read_dir,
+};
+