fs: Use rename lock and RCU for multi-step operations
[pandora-kernel.git] / drivers / staging / pohmelfs / path_entry.c
1 /*
2  * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/module.h>
17 #include <linux/fs.h>
18 #include <linux/ktime.h>
19 #include <linux/fs_struct.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/mount.h>
23 #include <linux/mm.h>
24
25 #include "netfs.h"
26
27 #define UNHASHED_OBSCURE_STRING_SIZE            sizeof(" (deleted)")
28
29 /*
30  * Create path from root for given inode.
31  * Path is formed as set of stuctures, containing name of the object
32  * and its inode data (mode, permissions and so on).
33  */
34 int pohmelfs_construct_path_string(struct pohmelfs_inode *pi, void *data, int len)
35 {
36         struct path path;
37         struct dentry *d;
38         char *ptr;
39         int err = 0, strlen, reduce = 0;
40
41         d = d_find_alias(&pi->vfs_inode);
42         if (!d) {
43                 printk("%s: no alias, list_empty: %d.\n", __func__, list_empty(&pi->vfs_inode.i_dentry));
44                 return -ENOENT;
45         }
46
47         spin_lock(&current->fs->lock);
48         path.mnt = mntget(current->fs->root.mnt);
49         spin_unlock(&current->fs->lock);
50
51         path.dentry = d;
52
53         if (!IS_ROOT(d) && d_unhashed(d))
54                 reduce = 1;
55
56         ptr = d_path(&path, data, len);
57         if (IS_ERR(ptr)) {
58                 err = PTR_ERR(ptr);
59                 goto out;
60         }
61
62         if (reduce && len >= UNHASHED_OBSCURE_STRING_SIZE) {
63                 char *end = data + len - UNHASHED_OBSCURE_STRING_SIZE;
64                 *end = '\0';
65         }
66
67         strlen = len - (ptr - (char *)data);
68         memmove(data, ptr, strlen);
69         ptr = data;
70
71         err = strlen;
72
73         dprintk("%s: dname: '%s', len: %u, maxlen: %u, name: '%s', strlen: %d.\n",
74                         __func__, d->d_name.name, d->d_name.len, len, ptr, strlen);
75
76 out:
77         dput(d);
78         mntput(path.mnt);
79
80         return err;
81 }
82
83 int pohmelfs_path_length(struct pohmelfs_inode *pi)
84 {
85         struct dentry *d, *root, *first;
86         int len;
87         unsigned seq;
88
89         first = d_find_alias(&pi->vfs_inode);
90         if (!first) {
91                 dprintk("%s: ino: %llu, mode: %o.\n", __func__, pi->ino, pi->vfs_inode.i_mode);
92                 return -ENOENT;
93         }
94
95         spin_lock(&current->fs->lock);
96         root = dget(current->fs->root.dentry);
97         spin_unlock(&current->fs->lock);
98
99 rename_retry:
100         len = 1; /* Root slash */
101         d = first;
102         seq = read_seqbegin(&rename_lock);
103         rcu_read_lock();
104         spin_lock(&dcache_lock);
105
106         if (!IS_ROOT(d) && d_unhashed(d))
107                 len += UNHASHED_OBSCURE_STRING_SIZE; /* Obscure " (deleted)" string */
108
109         while (d && d != root && !IS_ROOT(d)) {
110                 len += d->d_name.len + 1; /* Plus slash */
111                 d = d->d_parent;
112         }
113         spin_unlock(&dcache_lock);
114         rcu_read_unlock();
115         if (read_seqretry(&rename_lock, seq))
116                 goto rename_retry;
117
118         dput(root);
119         dput(first);
120
121         return len + 1; /* Including zero-byte */
122 }