Whamcloud - gitweb
LU-6142 llite: use d_is_symlink to test if dentry is a symlink
[fs/lustre-release.git] / lustre / llite / dcache.c
index cc03462..0faaaea 100644 (file)
@@ -66,6 +66,55 @@ static void ll_release(struct dentry *de)
        EXIT;
 }
 
+/* Compare if two dentries are the same.  Don't match if the existing dentry
+ * is marked invalid.  Returns 1 if different, 0 if the same.
+ *
+ * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
+ * an AST before calling d_revalidate_it().  The dentry still exists (marked
+ * INVALID) so d_lookup() matches it, but we have no lock on it (so
+ * lock_match() fails) and we spin around real_lookup().
+ *
+ * This race doesn't apply to lookups in d_alloc_parallel(), and for
+ * those we want to ensure that only one dentry with a given name is
+ * in ll_lookup_nd() at a time.  So allow invalid dentries to match
+ * while d_in_lookup().  We will be called again when the lookup
+ * completes, and can give a different answer then.
+ */
+#if defined(HAVE_D_COMPARE_5ARGS)
+static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
+                      unsigned int len, const char *str,
+                      const struct qstr *name)
+#elif defined(HAVE_D_COMPARE_4ARGS)
+static int ll_dcompare(const struct dentry *dentry, unsigned int len,
+                      const char *str, const struct qstr *name)
+#endif
+{
+       ENTRY;
+
+       if (len != name->len)
+               RETURN(1);
+
+       if (memcmp(str, name->name, len))
+               RETURN(1);
+
+       CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
+              name->len, name->name, dentry, dentry->d_flags,
+              ll_d_count(dentry));
+
+       /* mountpoint is always valid */
+       if (d_mountpoint((struct dentry *)dentry))
+               RETURN(0);
+
+       /* ensure exclusion against parallel lookup of the same name */
+       if (d_in_lookup((struct dentry *)dentry))
+               return 0;
+
+       if (d_lustre_invalid(dentry))
+               RETURN(1);
+
+       RETURN(0);
+}
+
 /**
  * Called when last reference to a dentry is dropped and dcache wants to know
  * whether or not it should cache it:
@@ -78,16 +127,16 @@ static int ll_ddelete(const struct dentry *de)
        ENTRY;
        LASSERT(de);
 
-       CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
-              d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
-              de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
+       CDEBUG(D_DENTRY, "%s dentry %pd (%p, parent %p, inode %p) %s%s\n",
+              d_lustre_invalid(de) ? "deleting" : "keeping",
+              de, de, de->d_parent, de->d_inode,
               d_unhashed((struct dentry *)de) ? "" : "hashed,",
               list_empty(&de->d_subdirs) ? "" : "subdirs");
 
        /* kernel >= 2.6.38 last refcount is decreased after this function. */
        LASSERT(ll_d_count(de) == 1);
 
-       if (d_lustre_invalid((struct dentry *)de))
+       if (d_lustre_invalid(de))
                RETURN(1);
        RETURN(0);
 }
@@ -97,9 +146,9 @@ int ll_d_init(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,
-               ll_d_count(de));
+       CDEBUG(D_DENTRY, "ldd on dentry %pd (%p) parent %p inode %p refc %d\n",
+              de, de, de->d_parent, de->d_inode,
+              ll_d_count(de));
 
        if (de->d_fsdata == NULL) {
                struct ll_dentry_data *lld;
@@ -168,7 +217,8 @@ void ll_intent_release(struct lookup_intent *it)
        EXIT;
 }
 
-void ll_invalidate_aliases(struct inode *inode)
+/* mark aliases invalid and prune unused aliases */
+void ll_prune_aliases(struct inode *inode)
 {
        struct dentry *dentry;
        ENTRY;
@@ -179,16 +229,12 @@ void ll_invalidate_aliases(struct inode *inode)
               PFID(ll_inode2fid(inode)), inode);
 
        spin_lock(&inode->i_lock);
-       hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
-               CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
-                      "inode %p flags %d\n", dentry->d_name.len,
-                      dentry->d_name.name, dentry, dentry->d_parent,
-                      dentry->d_inode, dentry->d_flags);
-
-               d_lustre_invalidate(dentry, 0);
-       }
+       hlist_for_each_entry(dentry, &inode->i_dentry, d_alias)
+               d_lustre_invalidate(dentry);
        spin_unlock(&inode->i_lock);
 
+       d_prune_aliases(inode);
+
         EXIT;
 }
 
@@ -212,25 +258,21 @@ int ll_revalidate_it_finish(struct ptlrpc_request *request,
 
 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
 {
-        LASSERT(it != NULL);
-        LASSERT(dentry != NULL);
+       LASSERT(it != NULL);
+       LASSERT(dentry != NULL);
 
        if (it->it_lock_mode && dentry->d_inode != NULL) {
-                struct inode *inode = dentry->d_inode;
-                struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
+               struct inode *inode = dentry->d_inode;
+               struct ll_sb_info *sbi = ll_i2sbi(inode);
 
                CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
                       PFID(ll_inode2fid(inode)), inode);
-                ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
-        }
-
-        /* drop lookup or getattr locks immediately */
-        if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
-                /* 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);
-        }
+               ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
+       }
+
+       /* drop lookup or getattr locks immediately */
+       if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
+               ll_intent_drop_lock(it);
 }
 
 static int ll_revalidate_dentry(struct dentry *dentry,
@@ -241,14 +283,6 @@ static int ll_revalidate_dentry(struct dentry *dentry,
        CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
               dentry->d_name.name, lookup_flags);
 
-       /* mountpoint is always valid */
-       if (d_mountpoint((struct dentry *)dentry))
-               return 1;
-
-       /* No lock -> invalid dentry */
-       if (d_lustre_invalid(dentry))
-               return 0;
-
        /* If this is intermediate component path lookup and we were able to get
         * to this dentry, then its lock has not been revoked and the
         * path component is valid. */
@@ -256,12 +290,18 @@ static int ll_revalidate_dentry(struct dentry *dentry,
                return 1;
 
        /* Symlink - always valid as long as the dentry was found */
-#ifdef HAVE_IOP_GET_LINK
-       if (dentry->d_inode && dentry->d_inode->i_op->get_link)
-#else
-       if (dentry->d_inode && dentry->d_inode->i_op->follow_link)
-#endif
-               return 1;
+       /* only special case is to prevent ELOOP error from VFS during open
+        * of a foreign symlink file/dir with O_NOFOLLOW, like it happens for
+        * real symlinks. This will allow to open foreign symlink file/dir
+        * for get[dir]stripe/unlock ioctl()s.
+        */
+       if (d_is_symlink(dentry)) {
+               if (!S_ISLNK(dentry->d_inode->i_mode) &&
+                   !(lookup_flags & LOOKUP_FOLLOW))
+                       return 0;
+               else
+                       return 1;
+       }
 
        /*
         * VFS warns us that this is the second go around and previous
@@ -276,7 +316,7 @@ static int ll_revalidate_dentry(struct dentry *dentry,
                return -ECHILD;
 
        if (dentry_may_statahead(dir, dentry))
-               ll_statahead(dir, &dentry, dentry->d_inode == NULL);
+               ll_revalidate_statahead(dir, &dentry, dentry->d_inode == NULL);
 
        return 1;
 }
@@ -285,4 +325,5 @@ const struct dentry_operations ll_d_ops = {
        .d_revalidate   = ll_revalidate_dentry,
         .d_release = ll_release,
         .d_delete  = ll_ddelete,
+        .d_compare = ll_dcompare,
 };