Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[pandora-kernel.git] / fs / dcache.c
index 170efbc..6068c25 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/syscalls.h>
 #include <linux/string.h>
 #include <linux/mm.h>
+#include <linux/fdtable.h>
 #include <linux/fs.h>
 #include <linux/fsnotify.h>
 #include <linux/slab.h>
@@ -95,12 +96,21 @@ static void d_free(struct dentry *dentry)
                call_rcu(&dentry->d_u.d_rcu, d_callback);
 }
 
+static void dentry_lru_remove(struct dentry *dentry)
+{
+       if (!list_empty(&dentry->d_lru)) {
+               list_del_init(&dentry->d_lru);
+               dentry_stat.nr_unused--;
+       }
+}
+
 /*
  * Release the dentry's inode, using the filesystem
  * d_iput() operation if defined.
- * Called with dcache_lock and per dentry lock held, drops both.
  */
 static void dentry_iput(struct dentry * dentry)
+       __releases(dentry->d_lock)
+       __releases(dcache_lock)
 {
        struct inode *inode = dentry->d_inode;
        if (inode) {
@@ -124,12 +134,13 @@ static void dentry_iput(struct dentry * dentry)
  * d_kill - kill dentry and return parent
  * @dentry: dentry to kill
  *
- * Called with dcache_lock and d_lock, releases both.  The dentry must
- * already be unhashed and removed from the LRU.
+ * The dentry must already be unhashed and removed from the LRU.
  *
  * If this is the root of the dentry tree, return NULL.
  */
 static struct dentry *d_kill(struct dentry *dentry)
+       __releases(dentry->d_lock)
+       __releases(dcache_lock)
 {
        struct dentry *parent;
 
@@ -211,13 +222,7 @@ repeat:
 unhash_it:
        __d_drop(dentry);
 kill_it:
-       /* If dentry was on d_lru list
-        * delete it from there
-        */
-       if (!list_empty(&dentry->d_lru)) {
-               list_del(&dentry->d_lru);
-               dentry_stat.nr_unused--;
-       }
+       dentry_lru_remove(dentry);
        dentry = d_kill(dentry);
        if (dentry)
                goto repeat;
@@ -285,10 +290,7 @@ int d_invalidate(struct dentry * dentry)
 static inline struct dentry * __dget_locked(struct dentry *dentry)
 {
        atomic_inc(&dentry->d_count);
-       if (!list_empty(&dentry->d_lru)) {
-               dentry_stat.nr_unused--;
-               list_del_init(&dentry->d_lru);
-       }
+       dentry_lru_remove(dentry);
        return dentry;
 }
 
@@ -384,11 +386,11 @@ restart:
  * Try to prune ancestors as well.  This is necessary to prevent
  * quadratic behavior of shrink_dcache_parent(), but is also expected
  * to be beneficial in reducing dentry cache fragmentation.
- *
- * Called with dcache_lock, drops it and then regains.
- * Called with dentry->d_lock held, drops it.
  */
 static void prune_one_dentry(struct dentry * dentry)
+       __releases(dentry->d_lock)
+       __releases(dcache_lock)
+       __acquires(dcache_lock)
 {
        __d_drop(dentry);
        dentry = d_kill(dentry);
@@ -404,10 +406,7 @@ static void prune_one_dentry(struct dentry * dentry)
 
                if (dentry->d_op && dentry->d_op->d_delete)
                        dentry->d_op->d_delete(dentry);
-               if (!list_empty(&dentry->d_lru)) {
-                       list_del(&dentry->d_lru);
-                       dentry_stat.nr_unused--;
-               }
+               dentry_lru_remove(dentry);
                __d_drop(dentry);
                dentry = d_kill(dentry);
                spin_lock(&dcache_lock);
@@ -596,10 +595,7 @@ static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
 
        /* detach this root from the system */
        spin_lock(&dcache_lock);
-       if (!list_empty(&dentry->d_lru)) {
-               dentry_stat.nr_unused--;
-               list_del_init(&dentry->d_lru);
-       }
+       dentry_lru_remove(dentry);
        __d_drop(dentry);
        spin_unlock(&dcache_lock);
 
@@ -613,11 +609,7 @@ static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
                        spin_lock(&dcache_lock);
                        list_for_each_entry(loop, &dentry->d_subdirs,
                                            d_u.d_child) {
-                               if (!list_empty(&loop->d_lru)) {
-                                       dentry_stat.nr_unused--;
-                                       list_del_init(&loop->d_lru);
-                               }
-
+                               dentry_lru_remove(loop);
                                __d_drop(loop);
                                cond_resched_lock(&dcache_lock);
                        }
@@ -799,10 +791,7 @@ resume:
                struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
                next = tmp->next;
 
-               if (!list_empty(&dentry->d_lru)) {
-                       dentry_stat.nr_unused--;
-                       list_del_init(&dentry->d_lru);
-               }
+               dentry_lru_remove(dentry);
                /* 
                 * move only zero ref count dentries to the end 
                 * of the unused list for prune_dcache
@@ -1618,10 +1607,9 @@ static int d_isparent(struct dentry *p1, struct dentry *p2)
  *
  * Note: If ever the locking in lock_rename() changes, then please
  * remember to update this too...
- *
- * On return, dcache_lock will have been unlocked.
  */
 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
+       __releases(dcache_lock)
 {
        struct mutex *m1 = NULL, *m2 = NULL;
        struct dentry *ret;
@@ -1757,15 +1745,27 @@ out_nolock:
 shouldnt_be_hashed:
        spin_unlock(&dcache_lock);
        BUG();
-       goto shouldnt_be_hashed;
+}
+
+static int prepend(char **buffer, int *buflen, const char *str, int namelen)
+{
+       *buflen -= namelen;
+       if (*buflen < 0)
+               return -ENAMETOOLONG;
+       *buffer -= namelen;
+       memcpy(*buffer, str, namelen);
+       return 0;
+}
+
+static int prepend_name(char **buffer, int *buflen, struct qstr *name)
+{
+       return prepend(buffer, buflen, name->name, name->len);
 }
 
 /**
- * d_path - return the path of a dentry
- * @dentry: dentry to report
- * @vfsmnt: vfsmnt to which the dentry belongs
- * @root: root dentry
- * @rootmnt: vfsmnt to which the root dentry belongs
+ * __d_path - return the path of a dentry
+ * @path: the dentry/vfsmount to report
+ * @root: root vfsmnt/dentry (may be modified by this function)
  * @buffer: buffer to return value in
  * @buflen: buffer length
  *
@@ -1775,23 +1775,23 @@ shouldnt_be_hashed:
  * Returns the buffer or an error code if the path was too long.
  *
  * "buflen" should be positive. Caller holds the dcache_lock.
+ *
+ * If path is not reachable from the supplied root, then the value of
+ * root is changed (without modifying refcounts).
  */
-static char *__d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
-                      struct path *root, char *buffer, int buflen)
+char *__d_path(const struct path *path, struct path *root,
+              char *buffer, int buflen)
 {
-       char * end = buffer+buflen;
-       char * retval;
-       int namelen;
-
-       *--end = '\0';
-       buflen--;
-       if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
-               buflen -= 10;
-               end -= 10;
-               if (buflen < 0)
+       struct dentry *dentry = path->dentry;
+       struct vfsmount *vfsmnt = path->mnt;
+       char *end = buffer + buflen;
+       char *retval;
+
+       spin_lock(&vfsmount_lock);
+       prepend(&end, &buflen, "\0", 1);
+       if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
+               (prepend(&end, &buflen, " (deleted)", 10) != 0))
                        goto Elong;
-               memcpy(end, " (deleted)", 10);
-       }
 
        if (buflen < 1)
                goto Elong;
@@ -1806,47 +1806,42 @@ static char *__d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
                        break;
                if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
                        /* Global root? */
-                       spin_lock(&vfsmount_lock);
                        if (vfsmnt->mnt_parent == vfsmnt) {
-                               spin_unlock(&vfsmount_lock);
                                goto global_root;
                        }
                        dentry = vfsmnt->mnt_mountpoint;
                        vfsmnt = vfsmnt->mnt_parent;
-                       spin_unlock(&vfsmount_lock);
                        continue;
                }
                parent = dentry->d_parent;
                prefetch(parent);
-               namelen = dentry->d_name.len;
-               buflen -= namelen + 1;
-               if (buflen < 0)
+               if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
+                   (prepend(&end, &buflen, "/", 1) != 0))
                        goto Elong;
-               end -= namelen;
-               memcpy(end, dentry->d_name.name, namelen);
-               *--end = '/';
                retval = end;
                dentry = parent;
        }
 
+out:
+       spin_unlock(&vfsmount_lock);
        return retval;
 
 global_root:
-       namelen = dentry->d_name.len;
-       buflen -= namelen;
-       if (buflen < 0)
+       retval += 1;    /* hit the slash */
+       if (prepend_name(&retval, &buflen, &dentry->d_name) != 0)
                goto Elong;
-       retval -= namelen-1;    /* hit the slash */
-       memcpy(retval, dentry->d_name.name, namelen);
-       return retval;
+       root->mnt = vfsmnt;
+       root->dentry = dentry;
+       goto out;
+
 Elong:
-       return ERR_PTR(-ENAMETOOLONG);
+       retval = ERR_PTR(-ENAMETOOLONG);
+       goto out;
 }
 
 /**
  * d_path - return the path of a dentry
- * @dentry: dentry to report
- * @vfsmnt: vfsmnt to which the dentry belongs
+ * @path: path to report
  * @buf: buffer to return value in
  * @buflen: buffer length
  *
@@ -1855,13 +1850,13 @@ Elong:
  *
  * Returns the buffer or an error code if the path was too long.
  *
- * "buflen" should be positive. Caller holds the dcache_lock.
+ * "buflen" should be positive.
  */
-char *d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
-            char *buf, int buflen)
+char *d_path(const struct path *path, char *buf, int buflen)
 {
        char *res;
        struct path root;
+       struct path tmp;
 
        /*
         * We have various synthetic filesystems that never get mounted.  On
@@ -1870,15 +1865,16 @@ char *d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
         * user wants to identify the object in /proc/pid/fd/.  The little hack
         * below allows us to generate a name for these objects on demand:
         */
-       if (dentry->d_op && dentry->d_op->d_dname)
-               return dentry->d_op->d_dname(dentry, buf, buflen);
+       if (path->dentry->d_op && path->dentry->d_op->d_dname)
+               return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
 
        read_lock(&current->fs->lock);
        root = current->fs->root;
-       path_get(&current->fs->root);
+       path_get(&root);
        read_unlock(&current->fs->lock);
        spin_lock(&dcache_lock);
-       res = __d_path(dentry, vfsmnt, &root, buf, buflen);
+       tmp = root;
+       res = __d_path(path, &tmp, buf, buflen);
        spin_unlock(&dcache_lock);
        path_put(&root);
        return res;
@@ -1905,6 +1901,43 @@ char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
        return memcpy(buffer, temp, sz);
 }
 
+/*
+ * Write full pathname from the root of the filesystem into the buffer.
+ */
+char *dentry_path(struct dentry *dentry, char *buf, int buflen)
+{
+       char *end = buf + buflen;
+       char *retval;
+
+       spin_lock(&dcache_lock);
+       prepend(&end, &buflen, "\0", 1);
+       if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
+               (prepend(&end, &buflen, "//deleted", 9) != 0))
+                       goto Elong;
+       if (buflen < 1)
+               goto Elong;
+       /* Get '/' right */
+       retval = end-1;
+       *retval = '/';
+
+       while (!IS_ROOT(dentry)) {
+               struct dentry *parent = dentry->d_parent;
+
+               prefetch(parent);
+               if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
+                   (prepend(&end, &buflen, "/", 1) != 0))
+                       goto Elong;
+
+               retval = end;
+               dentry = parent;
+       }
+       spin_unlock(&dcache_lock);
+       return retval;
+Elong:
+       spin_unlock(&dcache_lock);
+       return ERR_PTR(-ENAMETOOLONG);
+}
+
 /*
  * NOTE! The user-level library version returns a
  * character pointer. The kernel system call just
@@ -1934,19 +1967,20 @@ asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
 
        read_lock(&current->fs->lock);
        pwd = current->fs->pwd;
-       path_get(&current->fs->pwd);
+       path_get(&pwd);
        root = current->fs->root;
-       path_get(&current->fs->root);
+       path_get(&root);
        read_unlock(&current->fs->lock);
 
        error = -ENOENT;
        /* Has the current directory has been unlinked? */
        spin_lock(&dcache_lock);
-       if (pwd.dentry->d_parent == pwd.dentry || !d_unhashed(pwd.dentry)) {
+       if (IS_ROOT(pwd.dentry) || !d_unhashed(pwd.dentry)) {
                unsigned long len;
+               struct path tmp = root;
                char * cwd;
 
-               cwd = __d_path(pwd.dentry, pwd.mnt, &root, page, PAGE_SIZE);
+               cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
                spin_unlock(&dcache_lock);
 
                error = PTR_ERR(cwd);