Whamcloud - gitweb
b=3643
[fs/lustre-release.git] / lustre / llite / dcache.c
index 9dba881..a338e40 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/lustre_lite.h>
 #include <linux/lustre_idl.h>
 #include <linux/lustre_dlm.h>
+#include <linux/lustre_version.h>
 
 #include "llite_internal.h"
 
@@ -48,11 +49,27 @@ static void ll_release(struct dentry *de)
         EXIT;
 }
 
+/* should NOT be called with the dcache lock, see fs/dcache.c */
+static int ll_ddelete(struct dentry *de)
+{
+        ENTRY;
+        LASSERT(de);
+        CDEBUG(D_DENTRY, "%s dentry %*s (%p, parent %p, inode %p) %s%s\n",
+               (de->d_flags & DCACHE_LUSTRE_INVALID ? "keeping" : "deleting"),
+               de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
+               d_unhashed(de) ? "" : "hashed,",
+               list_empty(&de->d_subdirs) ? "" : "subdirs");
+        RETURN(0);
+}
+
 void ll_set_dd(struct dentry *de)
 {
         ENTRY;
         LASSERT(de != NULL);
 
+        CDEBUG(D_DENTRY, "ldd on dentry %*s (%p) parent %p inode %p refc %d\n",
+               de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
+               atomic_read(&de->d_count));
         lock_kernel();
         if (de->d_fsdata == NULL) {
                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
@@ -62,10 +79,9 @@ void ll_set_dd(struct dentry *de)
         EXIT;
 }
 
-void ll_intent_release(struct lookup_intent *it)
+void ll_intent_drop_lock(struct lookup_intent *it)
 {
         struct lustre_handle *handle;
-        ENTRY;
 
         if (it->it_op && it->d.lustre.it_lock_mode) {
                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
@@ -73,11 +89,17 @@ void ll_intent_release(struct lookup_intent *it)
                        " from it %p\n", handle->cookie, it);
                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
 
-                /* intent_release may be called multiple times, from
-                   this thread and we don't want to double-decref this
-                   lock (see bug 494) */
+                /* bug 494: intent_release may be called multiple times, from
+                 * this thread and we don't want to double-decref this lock */
                 it->d.lustre.it_lock_mode = 0;
         }
+}
+
+void ll_intent_release(struct lookup_intent *it)
+{
+        ENTRY;
+
+        ll_intent_drop_lock(it);
         it->it_magic = 0;
         it->it_op_release = 0;
         it->d.lustre.it_disposition = 0;
@@ -87,46 +109,54 @@ void ll_intent_release(struct lookup_intent *it)
 
 void ll_unhash_aliases(struct inode *inode)
 {
-       struct list_head *tmp, *head;
+        struct list_head *tmp, *head;
         struct ll_sb_info *sbi;
         ENTRY;
 
-        sbi = ll_i2sbi(inode);
-
-        CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
-               inode->i_ino, inode->i_generation, inode);
-
         if (inode == NULL) {
                 CERROR("unexpected NULL inode, tell phil\n");
                 return;
         }
+
+        CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
+               inode->i_ino, inode->i_generation, inode);
+
+        sbi = ll_i2sbi(inode);
         head = &inode->i_dentry;
 restart:
-       spin_lock(&dcache_lock);
-       tmp = head;
-       while ((tmp = tmp->next) != head) {
-               struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
-               if (!atomic_read(&dentry->d_count)) {
-                       dget_locked(dentry);
-                       __d_drop(dentry);
-                       spin_unlock(&dcache_lock);
-                       dput(dentry);
-                       goto restart;
-               } else {
+        spin_lock(&dcache_lock);
+        tmp = head;
+        while ((tmp = tmp->next) != head) {
+                struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
+                if (atomic_read(&dentry->d_count) == 0) {
+                        CDEBUG(D_DENTRY, "deleting dentry %*s (%p) parent %p "
+                               "inode %p\n", dentry->d_name.len,
+                               dentry->d_name.name, dentry, dentry->d_parent,
+                               dentry->d_inode);
+                        dget_locked(dentry);
+                        __d_drop(dentry);
+                        spin_unlock(&dcache_lock);
+                        dput(dentry);
+                        goto restart;
+                } else if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
+                        CDEBUG(D_DENTRY, "unhashing dentry %*s (%p) parent %p "
+                               "inode %p refc %d\n", dentry->d_name.len,
+                               dentry->d_name.name, dentry, dentry->d_parent,
+                               dentry->d_inode, atomic_read(&dentry->d_count));
                         hlist_del_init(&dentry->d_hash);
                         dentry->d_flags |= DCACHE_LUSTRE_INVALID;
-                        hlist_add_head(&dentry->d_hash, 
+                        hlist_add_head(&dentry->d_hash,
                                        &sbi->ll_orphan_dentry_list);
                 }
-       }
-       spin_unlock(&dcache_lock);
+        }
+        spin_unlock(&dcache_lock);
         EXIT;
 }
 
 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
 
-static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
-                                struct lookup_intent *it,
+static int revalidate_it_finish(struct ptlrpc_request *request, 
+                                int offset, struct lookup_intent *it,
                                 struct dentry *de)
 {
         struct ll_sb_info *sbi;
@@ -140,7 +170,8 @@ static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
                 RETURN(-ENOENT);
 
         sbi = ll_i2sbi(de->d_inode);
-        rc = ll_prep_inode(sbi->ll_osc_exp, &de->d_inode, request, offset,NULL);
+        rc = ll_prep_inode(sbi->ll_osc_exp, sbi->ll_mdc_exp,
+                           &de->d_inode, request, offset, NULL);
 
         RETURN(rc);
 }
@@ -154,33 +185,42 @@ void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
                 struct inode *inode = dentry->d_inode;
                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
                        inode, inode->i_ino, inode->i_generation);
-                mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
+                mdc_set_lock_data(NULL, &it->d.lustre.it_lock_handle, inode);
         }
 
-        /* drop IT_LOOKUP locks */
-        if (it->it_op == IT_LOOKUP)
+        /* drop lookup or getattr locks immediately */
+        if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+                /* on 2.6 there are situation when several lookups and
+                 * revalidations may be requested during single operation.
+                 * therefore, we don't release intent here -bzzz */
+                ll_intent_drop_lock(it);
+#else
                 ll_intent_release(it);
+#endif
+        }
 }
 
 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
 {
         struct lookup_intent *it = *itp;
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
-        if (it && it->it_magic != INTENT_MAGIC) { 
+        if (it && it->it_magic != INTENT_MAGIC) {
                 CERROR("WARNING: uninitialized intent\n");
                 LBUG();
         }
         if (it && (it->it_op == IT_GETATTR || it->it_op == 0))
                 it->it_op = IT_LOOKUP;
-        
 #endif
+
         if (!it || it->it_op == IT_GETXATTR)
                 it = *itp = deft;
 
         it->it_op_release = ll_intent_release;
 }
 
-int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
+int ll_revalidate_it(struct dentry *de, int flags, struct nameidata *nd,
+                     struct lookup_intent *it)
 {
         int rc;
         struct ll_fid pfid, cfid;
@@ -198,27 +238,56 @@ int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
         if (de->d_inode == NULL)
                 RETURN(0);
 
+        CDEBUG(D_INODE, "revalidate 0x%p: %*s -> %lu/%lu\n",
+                        de, de->d_name.len, de->d_name.name,
+                        (unsigned long) de->d_inode->i_ino,
+                        (unsigned long) de->d_inode->i_generation);
+
         exp = ll_i2mdcexp(de->d_inode);
         ll_inode2fid(&pfid, de->d_parent->d_inode);
         ll_inode2fid(&cfid, de->d_inode);
         icbd.icbd_parent = de->d_parent->d_inode;
         icbd.icbd_childp = &de;
 
-        /* 
-         * never execute intents for mount points
-         * - attrs will be fixed up in ll_revalidate_inode
-         */
+        /* Never execute intents for mount points.
+         * Attributes will be fixed up in ll_inode_revalidate_it */
         if (d_mountpoint(de))
                 RETURN(1);
 
+        if (nd != NULL)
+                nd->mnt->mnt_last_used = jiffies;
+
         ll_frob_intent(&it, &lookup_it);
         LASSERT(it);
 
         ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
 
-        rc = mdc_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
-                             NULL, 0,
-                             &cfid, it, flags, &req, ll_mdc_blocking_ast);
+        if (it->it_op == IT_GETATTR) { /* We need to check for LOOKUP lock
+                                          as well */
+                rc = md_intent_lock(exp, &ctxt, &pfid, de->d_name.name,
+                                    de->d_name.len, NULL, 0, &cfid, &lookup_it,
+                                    flags, &req, ll_mdc_blocking_ast);
+                /* If there was no lookup lock, no point in even checking for
+                   UPDATE lock */
+                if (!rc) {
+                        it = &lookup_it;
+                        GOTO(out, rc);
+                }
+                if (it_disposition(&lookup_it, DISP_LOOKUP_NEG)) {
+                        ll_intent_release(&lookup_it);
+                        it = &lookup_it;
+                        GOTO(out, rc = 0);
+                }
+
+                if (req)
+                        ptlrpc_req_finished(req);
+                req = NULL;
+                ll_lookup_finish_locks(&lookup_it, de);
+        }
+
+        rc = md_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
+                            NULL, 0, &cfid, it, flags, &req,
+                            ll_mdc_blocking_ast);
         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
          * if all was well, it will return 1 if it found locks, 0 otherwise. */
         if (req == NULL && rc >= 0)
@@ -246,17 +315,28 @@ int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
         __d_rehash(de, 0);
         spin_unlock(&dcache_lock);
 
+        GOTO(out, rc);
  out:
         if (req != NULL && rc == 1)
                 ptlrpc_req_finished(req);
-        if (rc == 0) { 
+        if (rc == 0) {
                 ll_unhash_aliases(de->d_inode);
-                de->d_flags |= DCACHE_LUSTRE_INVALID;
-        } else { 
-                ll_lookup_finish_locks(it, de);
-                de->d_flags &= ~DCACHE_LUSTRE_INVALID;
+                return rc;
         }
-        RETURN(rc);
+
+        CDEBUG(D_DENTRY, "revalidated dentry %*s (%p) parent %p "
+               "inode %p refc %d\n", de->d_name.len,
+               de->d_name.name, de, de->d_parent, de->d_inode,
+               atomic_read(&de->d_count));
+        ll_lookup_finish_locks(it, de);
+        de->d_flags &= ~DCACHE_LUSTRE_INVALID;
+        if (!((de->d_inode->i_mode & S_ISUID) &&S_ISDIR(de->d_inode->i_mode)) ||
+            !(flags & LOOKUP_CONTINUE || (it->it_op & (IT_CHDIR | IT_OPEN))))
+                return rc;
+
+        if (nd)
+                (void)ll_dir_process_mount_object(de, nd->mnt);
+        return rc;
 }
 
 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
@@ -349,15 +429,34 @@ static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
         int rc;
         ENTRY;
 
-        if (nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
-                rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
-        else 
-                rc = ll_revalidate_it(dentry, 0, NULL);
+        if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
+                rc = ll_revalidate_it(dentry, nd->flags, nd, &nd->intent);
+        else
+                rc = ll_revalidate_it(dentry, 0, nd, NULL);
 
         RETURN(rc);
 }
 #endif
 
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
+static void ll_dentry_iput(struct dentry *dentry, struct inode *inode)
+{
+        struct ll_sb_info *sbi = ll_i2sbi(inode);
+        struct ll_fid parent, child;
+
+        LASSERT(dentry->d_parent && dentry->d_parent->d_inode);
+        ll_inode2fid(&parent, dentry->d_parent->d_inode);
+        ll_inode2fid(&child, inode);
+        md_change_cbdata_name(sbi->ll_mdc_exp, &parent,
+                              (char *)dentry->d_name.name, 
+                              dentry->d_name.len, &child, 
+                              null_if_equal, inode);
+        iput(inode);
+}
+#else
+#error "implement ->d_iput() for 2.6"
+#endif
+
 struct dentry_operations ll_d_ops = {
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
         .d_revalidate = ll_revalidate_nd,
@@ -365,6 +464,8 @@ struct dentry_operations ll_d_ops = {
         .d_revalidate_it = ll_revalidate_it,
 #endif
         .d_release = ll_release,
+        .d_iput = ll_dentry_iput,
+        .d_delete = ll_ddelete,
 #if 0
         .d_pin = ll_pin,
         .d_unpin = ll_unpin,