Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/dcache.c |
| 3 | * |
| 4 | * Complete reimplementation |
| 5 | * (C) 1997 Thomas Schoebel-Theuer, |
| 6 | * with heavy changes by Linus Torvalds |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Notes on the allocation strategy: |
| 11 | * |
| 12 | * The dcache is a master of the icache - whenever a dcache entry |
| 13 | * exists, the inode will always exist. "iput()" is done either when |
| 14 | * the dcache entry is deleted or garbage collected. |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/syscalls.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/fs.h> |
John McCutchan | 7a91bf7 | 2005-08-08 13:52:16 -0400 | [diff] [blame] | 21 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/hash.h> |
| 25 | #include <linux/cache.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include <linux/file.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <linux/security.h> |
| 31 | #include <linux/seqlock.h> |
| 32 | #include <linux/swap.h> |
| 33 | #include <linux/bootmem.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 34 | #include <linux/fs_struct.h> |
Frederic Weisbecker | 613afbf | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 35 | #include <linux/hardirq.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 36 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 38 | int sysctl_vfs_cache_pressure __read_mostly = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure); |
| 40 | |
| 41 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 42 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | EXPORT_SYMBOL(dcache_lock); |
| 45 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 46 | static struct kmem_cache *dentry_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname)) |
| 49 | |
| 50 | /* |
| 51 | * This is the single most critical data structure when it comes |
| 52 | * to the dcache: the hashtable for lookups. Somebody should try |
| 53 | * to make this good - I've just made it work. |
| 54 | * |
| 55 | * This hash-function tries to avoid losing too many bits of hash |
| 56 | * information, yet avoid using a prime hash-size or similar. |
| 57 | */ |
| 58 | #define D_HASHBITS d_hash_shift |
| 59 | #define D_HASHMASK d_hash_mask |
| 60 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 61 | static unsigned int d_hash_mask __read_mostly; |
| 62 | static unsigned int d_hash_shift __read_mostly; |
| 63 | static struct hlist_head *dentry_hashtable __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* Statistics gathering. */ |
| 66 | struct dentry_stat_t dentry_stat = { |
| 67 | .age_limit = 45, |
| 68 | }; |
| 69 | |
Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 70 | static DEFINE_PER_CPU(unsigned int, nr_dentry); |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 71 | |
| 72 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) |
Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 73 | static int get_nr_dentry(void) |
| 74 | { |
| 75 | int i; |
| 76 | int sum = 0; |
| 77 | for_each_possible_cpu(i) |
| 78 | sum += per_cpu(nr_dentry, i); |
| 79 | return sum < 0 ? 0 : sum; |
| 80 | } |
| 81 | |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 82 | int proc_nr_dentry(ctl_table *table, int write, void __user *buffer, |
| 83 | size_t *lenp, loff_t *ppos) |
| 84 | { |
Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 85 | dentry_stat.nr_dentry = get_nr_dentry(); |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 86 | return proc_dointvec(table, write, buffer, lenp, ppos); |
| 87 | } |
| 88 | #endif |
| 89 | |
Christoph Hellwig | 9c82ab9 | 2010-10-10 05:36:22 -0400 | [diff] [blame] | 90 | static void __d_free(struct rcu_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Christoph Hellwig | 9c82ab9 | 2010-10-10 05:36:22 -0400 | [diff] [blame] | 92 | struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); |
| 93 | |
Arjan van de Ven | fd217f4 | 2008-10-21 06:47:33 -0700 | [diff] [blame] | 94 | WARN_ON(!list_empty(&dentry->d_alias)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (dname_external(dentry)) |
| 96 | kfree(dentry->d_name.name); |
| 97 | kmem_cache_free(dentry_cache, dentry); |
| 98 | } |
| 99 | |
| 100 | /* |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 101 | * no dcache_lock, please. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | */ |
| 103 | static void d_free(struct dentry *dentry) |
| 104 | { |
Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 105 | this_cpu_dec(nr_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (dentry->d_op && dentry->d_op->d_release) |
| 107 | dentry->d_op->d_release(dentry); |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 108 | |
Eric Dumazet | b342341 | 2006-12-06 20:38:48 -0800 | [diff] [blame] | 109 | /* if dentry was never inserted into hash, immediate free is OK */ |
Akinobu Mita | e8462ca | 2008-02-06 01:37:07 -0800 | [diff] [blame] | 110 | if (hlist_unhashed(&dentry->d_hash)) |
Christoph Hellwig | 9c82ab9 | 2010-10-10 05:36:22 -0400 | [diff] [blame] | 111 | __d_free(&dentry->d_u.d_rcu); |
Eric Dumazet | b342341 | 2006-12-06 20:38:48 -0800 | [diff] [blame] | 112 | else |
Christoph Hellwig | 9c82ab9 | 2010-10-10 05:36:22 -0400 | [diff] [blame] | 113 | call_rcu(&dentry->d_u.d_rcu, __d_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Release the dentry's inode, using the filesystem |
| 118 | * d_iput() operation if defined. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 120 | static void dentry_iput(struct dentry * dentry) |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 121 | __releases(dentry->d_lock) |
| 122 | __releases(dcache_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | struct inode *inode = dentry->d_inode; |
| 125 | if (inode) { |
| 126 | dentry->d_inode = NULL; |
| 127 | list_del_init(&dentry->d_alias); |
| 128 | spin_unlock(&dentry->d_lock); |
| 129 | spin_unlock(&dcache_lock); |
Linus Torvalds | f805fbd | 2005-09-19 19:54:29 -0700 | [diff] [blame] | 130 | if (!inode->i_nlink) |
| 131 | fsnotify_inoderemove(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (dentry->d_op && dentry->d_op->d_iput) |
| 133 | dentry->d_op->d_iput(dentry, inode); |
| 134 | else |
| 135 | iput(inode); |
| 136 | } else { |
| 137 | spin_unlock(&dentry->d_lock); |
| 138 | spin_unlock(&dcache_lock); |
| 139 | } |
| 140 | } |
| 141 | |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 142 | /* |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 143 | * dentry_lru_(add|del|move_tail) must be called with dcache_lock held. |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 144 | */ |
| 145 | static void dentry_lru_add(struct dentry *dentry) |
| 146 | { |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 147 | if (list_empty(&dentry->d_lru)) { |
| 148 | list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru); |
| 149 | dentry->d_sb->s_nr_dentry_unused++; |
Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 150 | dentry_stat.nr_unused++; |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 151 | } |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void dentry_lru_del(struct dentry *dentry) |
| 155 | { |
| 156 | if (!list_empty(&dentry->d_lru)) { |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 157 | list_del_init(&dentry->d_lru); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 158 | dentry->d_sb->s_nr_dentry_unused--; |
Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 159 | dentry_stat.nr_unused--; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 163 | static void dentry_lru_move_tail(struct dentry *dentry) |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 164 | { |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 165 | if (list_empty(&dentry->d_lru)) { |
| 166 | list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru); |
| 167 | dentry->d_sb->s_nr_dentry_unused++; |
Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 168 | dentry_stat.nr_unused++; |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 169 | } else { |
| 170 | list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 174 | /** |
| 175 | * d_kill - kill dentry and return parent |
| 176 | * @dentry: dentry to kill |
| 177 | * |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 178 | * The dentry must already be unhashed and removed from the LRU. |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 179 | * |
| 180 | * If this is the root of the dentry tree, return NULL. |
| 181 | */ |
| 182 | static struct dentry *d_kill(struct dentry *dentry) |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 183 | __releases(dentry->d_lock) |
| 184 | __releases(dcache_lock) |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 185 | { |
| 186 | struct dentry *parent; |
| 187 | |
| 188 | list_del(&dentry->d_u.d_child); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 189 | /*drops the locks, at that point nobody can reach this dentry */ |
| 190 | dentry_iput(dentry); |
OGAWA Hirofumi | 871c006 | 2008-10-16 07:50:27 +0900 | [diff] [blame] | 191 | if (IS_ROOT(dentry)) |
| 192 | parent = NULL; |
| 193 | else |
| 194 | parent = dentry->d_parent; |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 195 | d_free(dentry); |
OGAWA Hirofumi | 871c006 | 2008-10-16 07:50:27 +0900 | [diff] [blame] | 196 | return parent; |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* |
| 200 | * This is dput |
| 201 | * |
| 202 | * This is complicated by the fact that we do not want to put |
| 203 | * dentries that are no longer on any hash chain on the unused |
| 204 | * list: we'd much rather just get rid of them immediately. |
| 205 | * |
| 206 | * However, that implies that we have to traverse the dentry |
| 207 | * tree upwards to the parents which might _also_ now be |
| 208 | * scheduled for deletion (it may have been only waiting for |
| 209 | * its last child to go away). |
| 210 | * |
| 211 | * This tail recursion is done by hand as we don't want to depend |
| 212 | * on the compiler to always get this right (gcc generally doesn't). |
| 213 | * Real recursion would eat up our stack space. |
| 214 | */ |
| 215 | |
| 216 | /* |
| 217 | * dput - release a dentry |
| 218 | * @dentry: dentry to release |
| 219 | * |
| 220 | * Release a dentry. This will drop the usage count and if appropriate |
| 221 | * call the dentry unlink method as well as removing it from the queues and |
| 222 | * releasing its resources. If the parent dentries were scheduled for release |
| 223 | * they too may now get deleted. |
| 224 | * |
| 225 | * no dcache lock, please. |
| 226 | */ |
| 227 | |
| 228 | void dput(struct dentry *dentry) |
| 229 | { |
| 230 | if (!dentry) |
| 231 | return; |
| 232 | |
| 233 | repeat: |
| 234 | if (atomic_read(&dentry->d_count) == 1) |
| 235 | might_sleep(); |
| 236 | if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock)) |
| 237 | return; |
| 238 | |
| 239 | spin_lock(&dentry->d_lock); |
| 240 | if (atomic_read(&dentry->d_count)) { |
| 241 | spin_unlock(&dentry->d_lock); |
| 242 | spin_unlock(&dcache_lock); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * AV: ->d_delete() is _NOT_ allowed to block now. |
| 248 | */ |
| 249 | if (dentry->d_op && dentry->d_op->d_delete) { |
| 250 | if (dentry->d_op->d_delete(dentry)) |
| 251 | goto unhash_it; |
| 252 | } |
Nick Piggin | 265ac90 | 2010-10-10 05:36:24 -0400 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /* Unreachable? Get rid of it */ |
| 255 | if (d_unhashed(dentry)) |
| 256 | goto kill_it; |
Nick Piggin | 265ac90 | 2010-10-10 05:36:24 -0400 | [diff] [blame] | 257 | |
| 258 | /* Otherwise leave it cached and ensure it's on the LRU */ |
| 259 | dentry->d_flags |= DCACHE_REFERENCED; |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 260 | dentry_lru_add(dentry); |
Nick Piggin | 265ac90 | 2010-10-10 05:36:24 -0400 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | spin_unlock(&dentry->d_lock); |
| 263 | spin_unlock(&dcache_lock); |
| 264 | return; |
| 265 | |
| 266 | unhash_it: |
| 267 | __d_drop(dentry); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 268 | kill_it: |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 269 | /* if dentry was on the d_lru list delete it from there */ |
| 270 | dentry_lru_del(dentry); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 271 | dentry = d_kill(dentry); |
| 272 | if (dentry) |
| 273 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 275 | EXPORT_SYMBOL(dput); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * d_invalidate - invalidate a dentry |
| 279 | * @dentry: dentry to invalidate |
| 280 | * |
| 281 | * Try to invalidate the dentry if it turns out to be |
| 282 | * possible. If there are other dentries that can be |
| 283 | * reached through this one we can't delete it and we |
| 284 | * return -EBUSY. On success we return 0. |
| 285 | * |
| 286 | * no dcache lock. |
| 287 | */ |
| 288 | |
| 289 | int d_invalidate(struct dentry * dentry) |
| 290 | { |
| 291 | /* |
| 292 | * If it's already been dropped, return OK. |
| 293 | */ |
| 294 | spin_lock(&dcache_lock); |
| 295 | if (d_unhashed(dentry)) { |
| 296 | spin_unlock(&dcache_lock); |
| 297 | return 0; |
| 298 | } |
| 299 | /* |
| 300 | * Check whether to do a partial shrink_dcache |
| 301 | * to get rid of unused child entries. |
| 302 | */ |
| 303 | if (!list_empty(&dentry->d_subdirs)) { |
| 304 | spin_unlock(&dcache_lock); |
| 305 | shrink_dcache_parent(dentry); |
| 306 | spin_lock(&dcache_lock); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Somebody else still using it? |
| 311 | * |
| 312 | * If it's a directory, we can't drop it |
| 313 | * for fear of somebody re-populating it |
| 314 | * with children (even though dropping it |
| 315 | * would make it unreachable from the root, |
| 316 | * we might still populate it if it was a |
| 317 | * working directory or similar). |
| 318 | */ |
| 319 | spin_lock(&dentry->d_lock); |
| 320 | if (atomic_read(&dentry->d_count) > 1) { |
| 321 | if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) { |
| 322 | spin_unlock(&dentry->d_lock); |
| 323 | spin_unlock(&dcache_lock); |
| 324 | return -EBUSY; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | __d_drop(dentry); |
| 329 | spin_unlock(&dentry->d_lock); |
| 330 | spin_unlock(&dcache_lock); |
| 331 | return 0; |
| 332 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 333 | EXPORT_SYMBOL(d_invalidate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | /* This should be called _only_ with dcache_lock held */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | static inline struct dentry * __dget_locked(struct dentry *dentry) |
| 337 | { |
| 338 | atomic_inc(&dentry->d_count); |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 339 | dentry_lru_del(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return dentry; |
| 341 | } |
| 342 | |
| 343 | struct dentry * dget_locked(struct dentry *dentry) |
| 344 | { |
| 345 | return __dget_locked(dentry); |
| 346 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 347 | EXPORT_SYMBOL(dget_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | /** |
| 350 | * d_find_alias - grab a hashed alias of inode |
| 351 | * @inode: inode in question |
| 352 | * @want_discon: flag, used by d_splice_alias, to request |
| 353 | * that only a DISCONNECTED alias be returned. |
| 354 | * |
| 355 | * If inode has a hashed alias, or is a directory and has any alias, |
| 356 | * acquire the reference to alias and return it. Otherwise return NULL. |
| 357 | * Notice that if inode is a directory there can be only one alias and |
| 358 | * it can be unhashed only if it has no children, or if it is the root |
| 359 | * of a filesystem. |
| 360 | * |
NeilBrown | 21c0d8f | 2006-10-04 02:16:16 -0700 | [diff] [blame] | 361 | * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | * any other hashed alias over that one unless @want_discon is set, |
NeilBrown | 21c0d8f | 2006-10-04 02:16:16 -0700 | [diff] [blame] | 363 | * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | */ |
| 365 | |
| 366 | static struct dentry * __d_find_alias(struct inode *inode, int want_discon) |
| 367 | { |
| 368 | struct list_head *head, *next, *tmp; |
| 369 | struct dentry *alias, *discon_alias=NULL; |
| 370 | |
| 371 | head = &inode->i_dentry; |
| 372 | next = inode->i_dentry.next; |
| 373 | while (next != head) { |
| 374 | tmp = next; |
| 375 | next = tmp->next; |
| 376 | prefetch(next); |
| 377 | alias = list_entry(tmp, struct dentry, d_alias); |
| 378 | if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) { |
NeilBrown | 21c0d8f | 2006-10-04 02:16:16 -0700 | [diff] [blame] | 379 | if (IS_ROOT(alias) && |
| 380 | (alias->d_flags & DCACHE_DISCONNECTED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | discon_alias = alias; |
| 382 | else if (!want_discon) { |
| 383 | __dget_locked(alias); |
| 384 | return alias; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | if (discon_alias) |
| 389 | __dget_locked(discon_alias); |
| 390 | return discon_alias; |
| 391 | } |
| 392 | |
| 393 | struct dentry * d_find_alias(struct inode *inode) |
| 394 | { |
David Howells | 214fda1 | 2006-03-25 03:06:36 -0800 | [diff] [blame] | 395 | struct dentry *de = NULL; |
| 396 | |
| 397 | if (!list_empty(&inode->i_dentry)) { |
| 398 | spin_lock(&dcache_lock); |
| 399 | de = __d_find_alias(inode, 0); |
| 400 | spin_unlock(&dcache_lock); |
| 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return de; |
| 403 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 404 | EXPORT_SYMBOL(d_find_alias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Try to kill dentries associated with this inode. |
| 408 | * WARNING: you must own a reference to inode. |
| 409 | */ |
| 410 | void d_prune_aliases(struct inode *inode) |
| 411 | { |
Domen Puncer | 0cdca3f | 2005-09-10 00:27:07 -0700 | [diff] [blame] | 412 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | restart: |
| 414 | spin_lock(&dcache_lock); |
Domen Puncer | 0cdca3f | 2005-09-10 00:27:07 -0700 | [diff] [blame] | 415 | list_for_each_entry(dentry, &inode->i_dentry, d_alias) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | spin_lock(&dentry->d_lock); |
| 417 | if (!atomic_read(&dentry->d_count)) { |
| 418 | __dget_locked(dentry); |
| 419 | __d_drop(dentry); |
| 420 | spin_unlock(&dentry->d_lock); |
| 421 | spin_unlock(&dcache_lock); |
| 422 | dput(dentry); |
| 423 | goto restart; |
| 424 | } |
| 425 | spin_unlock(&dentry->d_lock); |
| 426 | } |
| 427 | spin_unlock(&dcache_lock); |
| 428 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 429 | EXPORT_SYMBOL(d_prune_aliases); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | /* |
Andrew Morton | d702ccb | 2006-06-22 14:47:31 -0700 | [diff] [blame] | 432 | * Throw away a dentry - free the inode, dput the parent. This requires that |
| 433 | * the LRU list has already been removed. |
| 434 | * |
Miklos Szeredi | 85864e1 | 2007-10-16 23:27:09 -0700 | [diff] [blame] | 435 | * Try to prune ancestors as well. This is necessary to prevent |
| 436 | * quadratic behavior of shrink_dcache_parent(), but is also expected |
| 437 | * to be beneficial in reducing dentry cache fragmentation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
Miklos Szeredi | 85864e1 | 2007-10-16 23:27:09 -0700 | [diff] [blame] | 439 | static void prune_one_dentry(struct dentry * dentry) |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 440 | __releases(dentry->d_lock) |
| 441 | __releases(dcache_lock) |
| 442 | __acquires(dcache_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | __d_drop(dentry); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 445 | dentry = d_kill(dentry); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * Prune ancestors. Locking is simpler than in dput(), |
| 449 | * because dcache_lock needs to be taken anyway. |
| 450 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | spin_lock(&dcache_lock); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 452 | while (dentry) { |
| 453 | if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock)) |
| 454 | return; |
| 455 | |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 456 | dentry_lru_del(dentry); |
Miklos Szeredi | d52b908 | 2007-05-08 00:23:46 -0700 | [diff] [blame] | 457 | __d_drop(dentry); |
| 458 | dentry = d_kill(dentry); |
| 459 | spin_lock(&dcache_lock); |
| 460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 463 | static void shrink_dentry_list(struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 465 | struct dentry *dentry; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 466 | |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 467 | while (!list_empty(list)) { |
| 468 | dentry = list_entry(list->prev, struct dentry, d_lru); |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 469 | dentry_lru_del(dentry); |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* |
| 472 | * We found an inuse dentry which was not removed from |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 473 | * the LRU because of laziness during lookup. Do not free |
| 474 | * it - just keep it off the LRU list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | */ |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 476 | spin_lock(&dentry->d_lock); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 477 | if (atomic_read(&dentry->d_count)) { |
| 478 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | continue; |
| 480 | } |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 481 | prune_one_dentry(dentry); |
| 482 | /* dentry->d_lock was dropped in prune_one_dentry() */ |
| 483 | cond_resched_lock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
| 488 | * __shrink_dcache_sb - shrink the dentry LRU on a given superblock |
| 489 | * @sb: superblock to shrink dentry LRU. |
| 490 | * @count: number of entries to prune |
| 491 | * @flags: flags to control the dentry processing |
| 492 | * |
| 493 | * If flags contains DCACHE_REFERENCED reference dentries will not be pruned. |
| 494 | */ |
| 495 | static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags) |
| 496 | { |
| 497 | /* called from prune_dcache() and shrink_dcache_parent() */ |
| 498 | struct dentry *dentry; |
| 499 | LIST_HEAD(referenced); |
| 500 | LIST_HEAD(tmp); |
| 501 | int cnt = *count; |
| 502 | |
| 503 | spin_lock(&dcache_lock); |
| 504 | while (!list_empty(&sb->s_dentry_lru)) { |
| 505 | dentry = list_entry(sb->s_dentry_lru.prev, |
| 506 | struct dentry, d_lru); |
| 507 | BUG_ON(dentry->d_sb != sb); |
| 508 | |
| 509 | /* |
| 510 | * If we are honouring the DCACHE_REFERENCED flag and the |
| 511 | * dentry has this flag set, don't free it. Clear the flag |
| 512 | * and put it back on the LRU. |
| 513 | */ |
| 514 | if (flags & DCACHE_REFERENCED) { |
| 515 | spin_lock(&dentry->d_lock); |
| 516 | if (dentry->d_flags & DCACHE_REFERENCED) { |
| 517 | dentry->d_flags &= ~DCACHE_REFERENCED; |
| 518 | list_move(&dentry->d_lru, &referenced); |
| 519 | spin_unlock(&dentry->d_lock); |
| 520 | cond_resched_lock(&dcache_lock); |
| 521 | continue; |
| 522 | } |
| 523 | spin_unlock(&dentry->d_lock); |
| 524 | } |
| 525 | |
| 526 | list_move_tail(&dentry->d_lru, &tmp); |
| 527 | if (!--cnt) |
| 528 | break; |
| 529 | cond_resched_lock(&dcache_lock); |
| 530 | } |
| 531 | |
| 532 | *count = cnt; |
| 533 | shrink_dentry_list(&tmp); |
| 534 | |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 535 | if (!list_empty(&referenced)) |
| 536 | list_splice(&referenced, &sb->s_dentry_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | spin_unlock(&dcache_lock); |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 541 | /** |
| 542 | * prune_dcache - shrink the dcache |
| 543 | * @count: number of entries to try to free |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | * |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 545 | * Shrink the dcache. This is done when we need more memory, or simply when we |
| 546 | * need to unmount something (at which point we need to unuse all dentries). |
| 547 | * |
| 548 | * This function may fail to free any resources if all the dentries are in use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | */ |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 550 | static void prune_dcache(int count) |
| 551 | { |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 552 | struct super_block *sb, *p = NULL; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 553 | int w_count; |
Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 554 | int unused = dentry_stat.nr_unused; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 555 | int prune_ratio; |
| 556 | int pruned; |
| 557 | |
| 558 | if (unused == 0 || count == 0) |
| 559 | return; |
| 560 | spin_lock(&dcache_lock); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 561 | if (count >= unused) |
| 562 | prune_ratio = 1; |
| 563 | else |
| 564 | prune_ratio = unused / count; |
| 565 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 566 | list_for_each_entry(sb, &super_blocks, s_list) { |
Al Viro | 551de6f | 2010-03-22 19:36:35 -0400 | [diff] [blame] | 567 | if (list_empty(&sb->s_instances)) |
| 568 | continue; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 569 | if (sb->s_nr_dentry_unused == 0) |
| 570 | continue; |
| 571 | sb->s_count++; |
| 572 | /* Now, we reclaim unused dentrins with fairness. |
| 573 | * We reclaim them same percentage from each superblock. |
| 574 | * We calculate number of dentries to scan on this sb |
| 575 | * as follows, but the implementation is arranged to avoid |
| 576 | * overflows: |
| 577 | * number of dentries to scan on this sb = |
| 578 | * count * (number of dentries on this sb / |
| 579 | * number of dentries in the machine) |
| 580 | */ |
| 581 | spin_unlock(&sb_lock); |
| 582 | if (prune_ratio != 1) |
| 583 | w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1; |
| 584 | else |
| 585 | w_count = sb->s_nr_dentry_unused; |
| 586 | pruned = w_count; |
| 587 | /* |
| 588 | * We need to be sure this filesystem isn't being unmounted, |
| 589 | * otherwise we could race with generic_shutdown_super(), and |
| 590 | * end up holding a reference to an inode while the filesystem |
| 591 | * is unmounted. So we try to get s_umount, and make sure |
| 592 | * s_root isn't NULL. |
| 593 | */ |
| 594 | if (down_read_trylock(&sb->s_umount)) { |
| 595 | if ((sb->s_root != NULL) && |
| 596 | (!list_empty(&sb->s_dentry_lru))) { |
| 597 | spin_unlock(&dcache_lock); |
| 598 | __shrink_dcache_sb(sb, &w_count, |
| 599 | DCACHE_REFERENCED); |
| 600 | pruned -= w_count; |
| 601 | spin_lock(&dcache_lock); |
| 602 | } |
| 603 | up_read(&sb->s_umount); |
| 604 | } |
| 605 | spin_lock(&sb_lock); |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 606 | if (p) |
| 607 | __put_super(p); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 608 | count -= pruned; |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 609 | p = sb; |
Al Viro | 79893c1 | 2010-03-22 20:27:55 -0400 | [diff] [blame] | 610 | /* more work left to do? */ |
| 611 | if (count <= 0) |
| 612 | break; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 613 | } |
Al Viro | dca3325 | 2010-07-25 02:31:46 +0400 | [diff] [blame] | 614 | if (p) |
| 615 | __put_super(p); |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 616 | spin_unlock(&sb_lock); |
| 617 | spin_unlock(&dcache_lock); |
| 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | /** |
| 621 | * shrink_dcache_sb - shrink dcache for a superblock |
| 622 | * @sb: superblock |
| 623 | * |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 624 | * Shrink the dcache for the specified super block. This is used to free |
| 625 | * the dcache before unmounting a file system. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | */ |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 627 | void shrink_dcache_sb(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Christoph Hellwig | 3049cfe | 2010-10-10 05:36:25 -0400 | [diff] [blame] | 629 | LIST_HEAD(tmp); |
| 630 | |
| 631 | spin_lock(&dcache_lock); |
| 632 | while (!list_empty(&sb->s_dentry_lru)) { |
| 633 | list_splice_init(&sb->s_dentry_lru, &tmp); |
| 634 | shrink_dentry_list(&tmp); |
| 635 | } |
| 636 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 638 | EXPORT_SYMBOL(shrink_dcache_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | /* |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 641 | * destroy a single subtree of dentries for unmount |
| 642 | * - see the comments on shrink_dcache_for_umount() for a description of the |
| 643 | * locking |
| 644 | */ |
| 645 | static void shrink_dcache_for_umount_subtree(struct dentry *dentry) |
| 646 | { |
| 647 | struct dentry *parent; |
David Howells | f871357 | 2006-10-28 10:38:46 -0700 | [diff] [blame] | 648 | unsigned detached = 0; |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 649 | |
| 650 | BUG_ON(!IS_ROOT(dentry)); |
| 651 | |
| 652 | /* detach this root from the system */ |
| 653 | spin_lock(&dcache_lock); |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 654 | dentry_lru_del(dentry); |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 655 | __d_drop(dentry); |
| 656 | spin_unlock(&dcache_lock); |
| 657 | |
| 658 | for (;;) { |
| 659 | /* descend to the first leaf in the current subtree */ |
| 660 | while (!list_empty(&dentry->d_subdirs)) { |
| 661 | struct dentry *loop; |
| 662 | |
| 663 | /* this is a branch with children - detach all of them |
| 664 | * from the system in one go */ |
| 665 | spin_lock(&dcache_lock); |
| 666 | list_for_each_entry(loop, &dentry->d_subdirs, |
| 667 | d_u.d_child) { |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 668 | dentry_lru_del(loop); |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 669 | __d_drop(loop); |
| 670 | cond_resched_lock(&dcache_lock); |
| 671 | } |
| 672 | spin_unlock(&dcache_lock); |
| 673 | |
| 674 | /* move to the first child */ |
| 675 | dentry = list_entry(dentry->d_subdirs.next, |
| 676 | struct dentry, d_u.d_child); |
| 677 | } |
| 678 | |
| 679 | /* consume the dentries from this leaf up through its parents |
| 680 | * until we find one with children or run out altogether */ |
| 681 | do { |
| 682 | struct inode *inode; |
| 683 | |
| 684 | if (atomic_read(&dentry->d_count) != 0) { |
| 685 | printk(KERN_ERR |
| 686 | "BUG: Dentry %p{i=%lx,n=%s}" |
| 687 | " still in use (%d)" |
| 688 | " [unmount of %s %s]\n", |
| 689 | dentry, |
| 690 | dentry->d_inode ? |
| 691 | dentry->d_inode->i_ino : 0UL, |
| 692 | dentry->d_name.name, |
| 693 | atomic_read(&dentry->d_count), |
| 694 | dentry->d_sb->s_type->name, |
| 695 | dentry->d_sb->s_id); |
| 696 | BUG(); |
| 697 | } |
| 698 | |
OGAWA Hirofumi | 871c006 | 2008-10-16 07:50:27 +0900 | [diff] [blame] | 699 | if (IS_ROOT(dentry)) |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 700 | parent = NULL; |
OGAWA Hirofumi | 871c006 | 2008-10-16 07:50:27 +0900 | [diff] [blame] | 701 | else { |
| 702 | parent = dentry->d_parent; |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 703 | atomic_dec(&parent->d_count); |
OGAWA Hirofumi | 871c006 | 2008-10-16 07:50:27 +0900 | [diff] [blame] | 704 | } |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 705 | |
| 706 | list_del(&dentry->d_u.d_child); |
David Howells | f871357 | 2006-10-28 10:38:46 -0700 | [diff] [blame] | 707 | detached++; |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 708 | |
| 709 | inode = dentry->d_inode; |
| 710 | if (inode) { |
| 711 | dentry->d_inode = NULL; |
| 712 | list_del_init(&dentry->d_alias); |
| 713 | if (dentry->d_op && dentry->d_op->d_iput) |
| 714 | dentry->d_op->d_iput(dentry, inode); |
| 715 | else |
| 716 | iput(inode); |
| 717 | } |
| 718 | |
| 719 | d_free(dentry); |
| 720 | |
| 721 | /* finished when we fall off the top of the tree, |
| 722 | * otherwise we ascend to the parent and move to the |
| 723 | * next sibling if there is one */ |
| 724 | if (!parent) |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 725 | return; |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 726 | dentry = parent; |
David Howells | c636ebd | 2006-10-11 01:22:19 -0700 | [diff] [blame] | 727 | } while (list_empty(&dentry->d_subdirs)); |
| 728 | |
| 729 | dentry = list_entry(dentry->d_subdirs.next, |
| 730 | struct dentry, d_u.d_child); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * destroy the dentries attached to a superblock on unmounting |
| 736 | * - we don't need to use dentry->d_lock, and only need dcache_lock when |
| 737 | * removing the dentry from the system lists and hashes because: |
| 738 | * - the superblock is detached from all mountings and open files, so the |
| 739 | * dentry trees will not be rearranged by the VFS |
| 740 | * - s_umount is write-locked, so the memory pressure shrinker will ignore |
| 741 | * any dentries belonging to this superblock that it comes across |
| 742 | * - the filesystem itself is no longer permitted to rearrange the dentries |
| 743 | * in this superblock |
| 744 | */ |
| 745 | void shrink_dcache_for_umount(struct super_block *sb) |
| 746 | { |
| 747 | struct dentry *dentry; |
| 748 | |
| 749 | if (down_read_trylock(&sb->s_umount)) |
| 750 | BUG(); |
| 751 | |
| 752 | dentry = sb->s_root; |
| 753 | sb->s_root = NULL; |
| 754 | atomic_dec(&dentry->d_count); |
| 755 | shrink_dcache_for_umount_subtree(dentry); |
| 756 | |
| 757 | while (!hlist_empty(&sb->s_anon)) { |
| 758 | dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash); |
| 759 | shrink_dcache_for_umount_subtree(dentry); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | * Search for at least 1 mount point in the dentry's subdirs. |
| 765 | * We descend to the next level whenever the d_subdirs |
| 766 | * list is non-empty and continue searching. |
| 767 | */ |
| 768 | |
| 769 | /** |
| 770 | * have_submounts - check for mounts over a dentry |
| 771 | * @parent: dentry to check. |
| 772 | * |
| 773 | * Return true if the parent or its subdirectories contain |
| 774 | * a mount point |
| 775 | */ |
| 776 | |
| 777 | int have_submounts(struct dentry *parent) |
| 778 | { |
| 779 | struct dentry *this_parent = parent; |
| 780 | struct list_head *next; |
| 781 | |
| 782 | spin_lock(&dcache_lock); |
| 783 | if (d_mountpoint(parent)) |
| 784 | goto positive; |
| 785 | repeat: |
| 786 | next = this_parent->d_subdirs.next; |
| 787 | resume: |
| 788 | while (next != &this_parent->d_subdirs) { |
| 789 | struct list_head *tmp = next; |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 790 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | next = tmp->next; |
| 792 | /* Have we found a mount point ? */ |
| 793 | if (d_mountpoint(dentry)) |
| 794 | goto positive; |
| 795 | if (!list_empty(&dentry->d_subdirs)) { |
| 796 | this_parent = dentry; |
| 797 | goto repeat; |
| 798 | } |
| 799 | } |
| 800 | /* |
| 801 | * All done at this level ... ascend and resume the search. |
| 802 | */ |
| 803 | if (this_parent != parent) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 804 | next = this_parent->d_u.d_child.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | this_parent = this_parent->d_parent; |
| 806 | goto resume; |
| 807 | } |
| 808 | spin_unlock(&dcache_lock); |
| 809 | return 0; /* No mount points found in tree */ |
| 810 | positive: |
| 811 | spin_unlock(&dcache_lock); |
| 812 | return 1; |
| 813 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 814 | EXPORT_SYMBOL(have_submounts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * Search the dentry child list for the specified parent, |
| 818 | * and move any unused dentries to the end of the unused |
| 819 | * list for prune_dcache(). We descend to the next level |
| 820 | * whenever the d_subdirs list is non-empty and continue |
| 821 | * searching. |
| 822 | * |
| 823 | * It returns zero iff there are no unused children, |
| 824 | * otherwise it returns the number of children moved to |
| 825 | * the end of the unused list. This may not be the total |
| 826 | * number of unused children, because select_parent can |
| 827 | * drop the lock and return early due to latency |
| 828 | * constraints. |
| 829 | */ |
| 830 | static int select_parent(struct dentry * parent) |
| 831 | { |
| 832 | struct dentry *this_parent = parent; |
| 833 | struct list_head *next; |
| 834 | int found = 0; |
| 835 | |
| 836 | spin_lock(&dcache_lock); |
| 837 | repeat: |
| 838 | next = this_parent->d_subdirs.next; |
| 839 | resume: |
| 840 | while (next != &this_parent->d_subdirs) { |
| 841 | struct list_head *tmp = next; |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 842 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | next = tmp->next; |
| 844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | /* |
| 846 | * move only zero ref count dentries to the end |
| 847 | * of the unused list for prune_dcache |
| 848 | */ |
| 849 | if (!atomic_read(&dentry->d_count)) { |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 850 | dentry_lru_move_tail(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | found++; |
Christoph Hellwig | a463335 | 2010-10-10 05:36:26 -0400 | [diff] [blame] | 852 | } else { |
| 853 | dentry_lru_del(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* |
| 857 | * We can return to the caller if we have found some (this |
| 858 | * ensures forward progress). We'll be coming back to find |
| 859 | * the rest. |
| 860 | */ |
| 861 | if (found && need_resched()) |
| 862 | goto out; |
| 863 | |
| 864 | /* |
| 865 | * Descend a level if the d_subdirs list is non-empty. |
| 866 | */ |
| 867 | if (!list_empty(&dentry->d_subdirs)) { |
| 868 | this_parent = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | goto repeat; |
| 870 | } |
| 871 | } |
| 872 | /* |
| 873 | * All done at this level ... ascend and resume the search. |
| 874 | */ |
| 875 | if (this_parent != parent) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 876 | next = this_parent->d_u.d_child.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | this_parent = this_parent->d_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | goto resume; |
| 879 | } |
| 880 | out: |
| 881 | spin_unlock(&dcache_lock); |
| 882 | return found; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * shrink_dcache_parent - prune dcache |
| 887 | * @parent: parent of entries to prune |
| 888 | * |
| 889 | * Prune the dcache to remove unused children of the parent dentry. |
| 890 | */ |
| 891 | |
| 892 | void shrink_dcache_parent(struct dentry * parent) |
| 893 | { |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 894 | struct super_block *sb = parent->d_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | int found; |
| 896 | |
| 897 | while ((found = select_parent(parent)) != 0) |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 898 | __shrink_dcache_sb(sb, &found, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 900 | EXPORT_SYMBOL(shrink_dcache_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | /* |
| 903 | * Scan `nr' dentries and return the number which remain. |
| 904 | * |
| 905 | * We need to avoid reentering the filesystem if the caller is performing a |
| 906 | * GFP_NOFS allocation attempt. One example deadlock is: |
| 907 | * |
| 908 | * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache-> |
| 909 | * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode-> |
| 910 | * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK. |
| 911 | * |
| 912 | * In this case we return -1 to tell the caller that we baled. |
| 913 | */ |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 914 | static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { |
| 916 | if (nr) { |
| 917 | if (!(gfp_mask & __GFP_FS)) |
| 918 | return -1; |
Kentaro Makita | da3bbdd | 2008-07-23 21:27:13 -0700 | [diff] [blame] | 919 | prune_dcache(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 921 | |
Nick Piggin | 86c8749 | 2011-01-07 17:49:18 +1100 | [diff] [blame] | 922 | return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 925 | static struct shrinker dcache_shrinker = { |
| 926 | .shrink = shrink_dcache_memory, |
| 927 | .seeks = DEFAULT_SEEKS, |
| 928 | }; |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | /** |
| 931 | * d_alloc - allocate a dcache entry |
| 932 | * @parent: parent of entry to allocate |
| 933 | * @name: qstr of the name |
| 934 | * |
| 935 | * Allocates a dentry. It returns %NULL if there is insufficient memory |
| 936 | * available. On a success the dentry is returned. The name passed in is |
| 937 | * copied and the copy passed in may be reused after this call. |
| 938 | */ |
| 939 | |
| 940 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) |
| 941 | { |
| 942 | struct dentry *dentry; |
| 943 | char *dname; |
| 944 | |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 945 | dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if (!dentry) |
| 947 | return NULL; |
| 948 | |
| 949 | if (name->len > DNAME_INLINE_LEN-1) { |
| 950 | dname = kmalloc(name->len + 1, GFP_KERNEL); |
| 951 | if (!dname) { |
| 952 | kmem_cache_free(dentry_cache, dentry); |
| 953 | return NULL; |
| 954 | } |
| 955 | } else { |
| 956 | dname = dentry->d_iname; |
| 957 | } |
| 958 | dentry->d_name.name = dname; |
| 959 | |
| 960 | dentry->d_name.len = name->len; |
| 961 | dentry->d_name.hash = name->hash; |
| 962 | memcpy(dname, name->name, name->len); |
| 963 | dname[name->len] = 0; |
| 964 | |
| 965 | atomic_set(&dentry->d_count, 1); |
| 966 | dentry->d_flags = DCACHE_UNHASHED; |
| 967 | spin_lock_init(&dentry->d_lock); |
| 968 | dentry->d_inode = NULL; |
| 969 | dentry->d_parent = NULL; |
| 970 | dentry->d_sb = NULL; |
| 971 | dentry->d_op = NULL; |
| 972 | dentry->d_fsdata = NULL; |
| 973 | dentry->d_mounted = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | INIT_HLIST_NODE(&dentry->d_hash); |
| 975 | INIT_LIST_HEAD(&dentry->d_lru); |
| 976 | INIT_LIST_HEAD(&dentry->d_subdirs); |
| 977 | INIT_LIST_HEAD(&dentry->d_alias); |
| 978 | |
| 979 | if (parent) { |
| 980 | dentry->d_parent = dget(parent); |
| 981 | dentry->d_sb = parent->d_sb; |
| 982 | } else { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 983 | INIT_LIST_HEAD(&dentry->d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | spin_lock(&dcache_lock); |
| 987 | if (parent) |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 988 | list_add(&dentry->d_u.d_child, &parent->d_subdirs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | spin_unlock(&dcache_lock); |
| 990 | |
Nick Piggin | 3e880fb | 2011-01-07 17:49:19 +1100 | [diff] [blame] | 991 | this_cpu_inc(nr_dentry); |
Christoph Hellwig | 312d3ca | 2010-10-10 05:36:23 -0400 | [diff] [blame] | 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return dentry; |
| 994 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 995 | EXPORT_SYMBOL(d_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
| 997 | struct dentry *d_alloc_name(struct dentry *parent, const char *name) |
| 998 | { |
| 999 | struct qstr q; |
| 1000 | |
| 1001 | q.name = name; |
| 1002 | q.len = strlen(name); |
| 1003 | q.hash = full_name_hash(q.name, q.len); |
| 1004 | return d_alloc(parent, &q); |
| 1005 | } |
H Hartley Sweeten | ef26ca9 | 2009-09-29 20:09:42 -0400 | [diff] [blame] | 1006 | EXPORT_SYMBOL(d_alloc_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1008 | /* the caller must hold dcache_lock */ |
| 1009 | static void __d_instantiate(struct dentry *dentry, struct inode *inode) |
| 1010 | { |
| 1011 | if (inode) |
| 1012 | list_add(&dentry->d_alias, &inode->i_dentry); |
| 1013 | dentry->d_inode = inode; |
| 1014 | fsnotify_d_instantiate(dentry, inode); |
| 1015 | } |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | /** |
| 1018 | * d_instantiate - fill in inode information for a dentry |
| 1019 | * @entry: dentry to complete |
| 1020 | * @inode: inode to attach to this dentry |
| 1021 | * |
| 1022 | * Fill in inode information in the entry. |
| 1023 | * |
| 1024 | * This turns negative dentries into productive full members |
| 1025 | * of society. |
| 1026 | * |
| 1027 | * NOTE! This assumes that the inode count has been incremented |
| 1028 | * (or otherwise set) by the caller to indicate that it is now |
| 1029 | * in use by the dcache. |
| 1030 | */ |
| 1031 | |
| 1032 | void d_instantiate(struct dentry *entry, struct inode * inode) |
| 1033 | { |
Eric Sesterhenn | 28133c7 | 2006-03-26 18:25:39 +0200 | [diff] [blame] | 1034 | BUG_ON(!list_empty(&entry->d_alias)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | spin_lock(&dcache_lock); |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1036 | __d_instantiate(entry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | spin_unlock(&dcache_lock); |
| 1038 | security_d_instantiate(entry, inode); |
| 1039 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1040 | EXPORT_SYMBOL(d_instantiate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | /** |
| 1043 | * d_instantiate_unique - instantiate a non-aliased dentry |
| 1044 | * @entry: dentry to instantiate |
| 1045 | * @inode: inode to attach to this dentry |
| 1046 | * |
| 1047 | * Fill in inode information in the entry. On success, it returns NULL. |
| 1048 | * If an unhashed alias of "entry" already exists, then we return the |
Oleg Drokin | e866cfa | 2006-01-09 20:52:51 -0800 | [diff] [blame] | 1049 | * aliased dentry instead and drop one reference to inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | * |
| 1051 | * Note that in order to avoid conflicts with rename() etc, the caller |
| 1052 | * had better be holding the parent directory semaphore. |
Oleg Drokin | e866cfa | 2006-01-09 20:52:51 -0800 | [diff] [blame] | 1053 | * |
| 1054 | * This also assumes that the inode count has been incremented |
| 1055 | * (or otherwise set) by the caller to indicate that it is now |
| 1056 | * in use by the dcache. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | */ |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1058 | static struct dentry *__d_instantiate_unique(struct dentry *entry, |
| 1059 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
| 1061 | struct dentry *alias; |
| 1062 | int len = entry->d_name.len; |
| 1063 | const char *name = entry->d_name.name; |
| 1064 | unsigned int hash = entry->d_name.hash; |
| 1065 | |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1066 | if (!inode) { |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1067 | __d_instantiate(entry, NULL); |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1068 | return NULL; |
| 1069 | } |
| 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | list_for_each_entry(alias, &inode->i_dentry, d_alias) { |
| 1072 | struct qstr *qstr = &alias->d_name; |
| 1073 | |
| 1074 | if (qstr->hash != hash) |
| 1075 | continue; |
| 1076 | if (alias->d_parent != entry->d_parent) |
| 1077 | continue; |
| 1078 | if (qstr->len != len) |
| 1079 | continue; |
| 1080 | if (memcmp(qstr->name, name, len)) |
| 1081 | continue; |
| 1082 | dget_locked(alias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | return alias; |
| 1084 | } |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1085 | |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1086 | __d_instantiate(entry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1089 | |
| 1090 | struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode) |
| 1091 | { |
| 1092 | struct dentry *result; |
| 1093 | |
| 1094 | BUG_ON(!list_empty(&entry->d_alias)); |
| 1095 | |
| 1096 | spin_lock(&dcache_lock); |
| 1097 | result = __d_instantiate_unique(entry, inode); |
| 1098 | spin_unlock(&dcache_lock); |
| 1099 | |
| 1100 | if (!result) { |
| 1101 | security_d_instantiate(entry, inode); |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | |
| 1105 | BUG_ON(!d_unhashed(result)); |
| 1106 | iput(inode); |
| 1107 | return result; |
| 1108 | } |
| 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | EXPORT_SYMBOL(d_instantiate_unique); |
| 1111 | |
| 1112 | /** |
| 1113 | * d_alloc_root - allocate root dentry |
| 1114 | * @root_inode: inode to allocate the root for |
| 1115 | * |
| 1116 | * Allocate a root ("/") dentry for the inode given. The inode is |
| 1117 | * instantiated and returned. %NULL is returned if there is insufficient |
| 1118 | * memory or the inode passed is %NULL. |
| 1119 | */ |
| 1120 | |
| 1121 | struct dentry * d_alloc_root(struct inode * root_inode) |
| 1122 | { |
| 1123 | struct dentry *res = NULL; |
| 1124 | |
| 1125 | if (root_inode) { |
| 1126 | static const struct qstr name = { .name = "/", .len = 1 }; |
| 1127 | |
| 1128 | res = d_alloc(NULL, &name); |
| 1129 | if (res) { |
| 1130 | res->d_sb = root_inode->i_sb; |
| 1131 | res->d_parent = res; |
| 1132 | d_instantiate(res, root_inode); |
| 1133 | } |
| 1134 | } |
| 1135 | return res; |
| 1136 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1137 | EXPORT_SYMBOL(d_alloc_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
| 1139 | static inline struct hlist_head *d_hash(struct dentry *parent, |
| 1140 | unsigned long hash) |
| 1141 | { |
| 1142 | hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES; |
| 1143 | hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS); |
| 1144 | return dentry_hashtable + (hash & D_HASHMASK); |
| 1145 | } |
| 1146 | |
| 1147 | /** |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 1148 | * d_obtain_alias - find or allocate a dentry for a given inode |
| 1149 | * @inode: inode to allocate the dentry for |
| 1150 | * |
| 1151 | * Obtain a dentry for an inode resulting from NFS filehandle conversion or |
| 1152 | * similar open by handle operations. The returned dentry may be anonymous, |
| 1153 | * or may have a full name (if the inode was already in the cache). |
| 1154 | * |
| 1155 | * When called on a directory inode, we must ensure that the inode only ever |
| 1156 | * has one dentry. If a dentry is found, that is returned instead of |
| 1157 | * allocating a new one. |
| 1158 | * |
| 1159 | * On successful return, the reference to the inode has been transferred |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 1160 | * to the dentry. In case of an error the reference on the inode is released. |
| 1161 | * To make it easier to use in export operations a %NULL or IS_ERR inode may |
| 1162 | * be passed in and will be the error will be propagate to the return value, |
| 1163 | * with a %NULL @inode replaced by ERR_PTR(-ESTALE). |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 1164 | */ |
| 1165 | struct dentry *d_obtain_alias(struct inode *inode) |
| 1166 | { |
Christoph Hellwig | 9308a61 | 2008-08-11 15:49:12 +0200 | [diff] [blame] | 1167 | static const struct qstr anonstring = { .name = "" }; |
| 1168 | struct dentry *tmp; |
| 1169 | struct dentry *res; |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 1170 | |
| 1171 | if (!inode) |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 1172 | return ERR_PTR(-ESTALE); |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 1173 | if (IS_ERR(inode)) |
| 1174 | return ERR_CAST(inode); |
| 1175 | |
Christoph Hellwig | 9308a61 | 2008-08-11 15:49:12 +0200 | [diff] [blame] | 1176 | res = d_find_alias(inode); |
| 1177 | if (res) |
| 1178 | goto out_iput; |
| 1179 | |
| 1180 | tmp = d_alloc(NULL, &anonstring); |
| 1181 | if (!tmp) { |
| 1182 | res = ERR_PTR(-ENOMEM); |
| 1183 | goto out_iput; |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 1184 | } |
Christoph Hellwig | 9308a61 | 2008-08-11 15:49:12 +0200 | [diff] [blame] | 1185 | tmp->d_parent = tmp; /* make sure dput doesn't croak */ |
| 1186 | |
| 1187 | spin_lock(&dcache_lock); |
| 1188 | res = __d_find_alias(inode, 0); |
| 1189 | if (res) { |
| 1190 | spin_unlock(&dcache_lock); |
| 1191 | dput(tmp); |
| 1192 | goto out_iput; |
| 1193 | } |
| 1194 | |
| 1195 | /* attach a disconnected dentry */ |
| 1196 | spin_lock(&tmp->d_lock); |
| 1197 | tmp->d_sb = inode->i_sb; |
| 1198 | tmp->d_inode = inode; |
| 1199 | tmp->d_flags |= DCACHE_DISCONNECTED; |
| 1200 | tmp->d_flags &= ~DCACHE_UNHASHED; |
| 1201 | list_add(&tmp->d_alias, &inode->i_dentry); |
| 1202 | hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon); |
| 1203 | spin_unlock(&tmp->d_lock); |
| 1204 | |
| 1205 | spin_unlock(&dcache_lock); |
| 1206 | return tmp; |
| 1207 | |
| 1208 | out_iput: |
| 1209 | iput(inode); |
| 1210 | return res; |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 1211 | } |
Benny Halevy | adc4872 | 2009-02-27 14:02:59 -0800 | [diff] [blame] | 1212 | EXPORT_SYMBOL(d_obtain_alias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
| 1214 | /** |
| 1215 | * d_splice_alias - splice a disconnected dentry into the tree if one exists |
| 1216 | * @inode: the inode which may have a disconnected dentry |
| 1217 | * @dentry: a negative dentry which we want to point to the inode. |
| 1218 | * |
| 1219 | * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and |
| 1220 | * DCACHE_DISCONNECTED), then d_move that in place of the given dentry |
| 1221 | * and return it, else simply d_add the inode to the dentry and return NULL. |
| 1222 | * |
| 1223 | * This is needed in the lookup routine of any filesystem that is exportable |
| 1224 | * (via knfsd) so that we can build dcache paths to directories effectively. |
| 1225 | * |
| 1226 | * If a dentry was found and moved, then it is returned. Otherwise NULL |
| 1227 | * is returned. This matches the expected return value of ->lookup. |
| 1228 | * |
| 1229 | */ |
| 1230 | struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) |
| 1231 | { |
| 1232 | struct dentry *new = NULL; |
| 1233 | |
NeilBrown | 21c0d8f | 2006-10-04 02:16:16 -0700 | [diff] [blame] | 1234 | if (inode && S_ISDIR(inode->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | spin_lock(&dcache_lock); |
| 1236 | new = __d_find_alias(inode, 1); |
| 1237 | if (new) { |
| 1238 | BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED)); |
| 1239 | spin_unlock(&dcache_lock); |
| 1240 | security_d_instantiate(new, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | d_move(new, dentry); |
| 1242 | iput(inode); |
| 1243 | } else { |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1244 | /* already taking dcache_lock, so d_add() by hand */ |
| 1245 | __d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | spin_unlock(&dcache_lock); |
| 1247 | security_d_instantiate(dentry, inode); |
| 1248 | d_rehash(dentry); |
| 1249 | } |
| 1250 | } else |
| 1251 | d_add(dentry, inode); |
| 1252 | return new; |
| 1253 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1254 | EXPORT_SYMBOL(d_splice_alias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1256 | /** |
| 1257 | * d_add_ci - lookup or allocate new dentry with case-exact name |
| 1258 | * @inode: the inode case-insensitive lookup has found |
| 1259 | * @dentry: the negative dentry that was passed to the parent's lookup func |
| 1260 | * @name: the case-exact name to be associated with the returned dentry |
| 1261 | * |
| 1262 | * This is to avoid filling the dcache with case-insensitive names to the |
| 1263 | * same inode, only the actual correct case is stored in the dcache for |
| 1264 | * case-insensitive filesystems. |
| 1265 | * |
| 1266 | * For a case-insensitive lookup match and if the the case-exact dentry |
| 1267 | * already exists in in the dcache, use it and return it. |
| 1268 | * |
| 1269 | * If no entry exists with the exact case name, allocate new dentry with |
| 1270 | * the exact case, and return the spliced entry. |
| 1271 | */ |
Christoph Hellwig | e45b590 | 2008-08-07 23:49:07 +0200 | [diff] [blame] | 1272 | struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1273 | struct qstr *name) |
| 1274 | { |
| 1275 | int error; |
| 1276 | struct dentry *found; |
| 1277 | struct dentry *new; |
| 1278 | |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1279 | /* |
| 1280 | * First check if a dentry matching the name already exists, |
| 1281 | * if not go ahead and create it now. |
| 1282 | */ |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1283 | found = d_hash_and_lookup(dentry->d_parent, name); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1284 | if (!found) { |
| 1285 | new = d_alloc(dentry->d_parent, name); |
| 1286 | if (!new) { |
| 1287 | error = -ENOMEM; |
| 1288 | goto err_out; |
| 1289 | } |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1290 | |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1291 | found = d_splice_alias(inode, new); |
| 1292 | if (found) { |
| 1293 | dput(new); |
| 1294 | return found; |
| 1295 | } |
| 1296 | return new; |
| 1297 | } |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1298 | |
| 1299 | /* |
| 1300 | * If a matching dentry exists, and it's not negative use it. |
| 1301 | * |
| 1302 | * Decrement the reference count to balance the iget() done |
| 1303 | * earlier on. |
| 1304 | */ |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1305 | if (found->d_inode) { |
| 1306 | if (unlikely(found->d_inode != inode)) { |
| 1307 | /* This can't happen because bad inodes are unhashed. */ |
| 1308 | BUG_ON(!is_bad_inode(inode)); |
| 1309 | BUG_ON(!is_bad_inode(found->d_inode)); |
| 1310 | } |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1311 | iput(inode); |
| 1312 | return found; |
| 1313 | } |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1314 | |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1315 | /* |
| 1316 | * Negative dentry: instantiate it unless the inode is a directory and |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1317 | * already has a dentry. |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1318 | */ |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1319 | spin_lock(&dcache_lock); |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1320 | if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) { |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1321 | __d_instantiate(found, inode); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1322 | spin_unlock(&dcache_lock); |
| 1323 | security_d_instantiate(found, inode); |
| 1324 | return found; |
| 1325 | } |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1326 | |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1327 | /* |
Christoph Hellwig | b6520c8 | 2009-01-05 19:10:37 +0100 | [diff] [blame] | 1328 | * In case a directory already has a (disconnected) entry grab a |
| 1329 | * reference to it, move it in place and use it. |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1330 | */ |
| 1331 | new = list_entry(inode->i_dentry.next, struct dentry, d_alias); |
| 1332 | dget_locked(new); |
| 1333 | spin_unlock(&dcache_lock); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1334 | security_d_instantiate(found, inode); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1335 | d_move(new, found); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1336 | iput(inode); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1337 | dput(found); |
Barry Naujok | 9403540 | 2008-05-21 16:50:46 +1000 | [diff] [blame] | 1338 | return new; |
| 1339 | |
| 1340 | err_out: |
| 1341 | iput(inode); |
| 1342 | return ERR_PTR(error); |
| 1343 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1344 | EXPORT_SYMBOL(d_add_ci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | /** |
| 1347 | * d_lookup - search for a dentry |
| 1348 | * @parent: parent dentry |
| 1349 | * @name: qstr of name we wish to find |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1350 | * Returns: dentry, or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | * |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1352 | * d_lookup searches the children of the parent dentry for the name in |
| 1353 | * question. If the dentry is found its reference count is incremented and the |
| 1354 | * dentry is returned. The caller must use dput to free the entry when it has |
| 1355 | * finished using it. %NULL is returned if the dentry does not exist. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | struct dentry * d_lookup(struct dentry * parent, struct qstr * name) |
| 1358 | { |
| 1359 | struct dentry * dentry = NULL; |
| 1360 | unsigned long seq; |
| 1361 | |
| 1362 | do { |
| 1363 | seq = read_seqbegin(&rename_lock); |
| 1364 | dentry = __d_lookup(parent, name); |
| 1365 | if (dentry) |
| 1366 | break; |
| 1367 | } while (read_seqretry(&rename_lock, seq)); |
| 1368 | return dentry; |
| 1369 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1370 | EXPORT_SYMBOL(d_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1372 | /* |
| 1373 | * __d_lookup - search for a dentry (racy) |
| 1374 | * @parent: parent dentry |
| 1375 | * @name: qstr of name we wish to find |
| 1376 | * Returns: dentry, or NULL |
| 1377 | * |
| 1378 | * __d_lookup is like d_lookup, however it may (rarely) return a |
| 1379 | * false-negative result due to unrelated rename activity. |
| 1380 | * |
| 1381 | * __d_lookup is slightly faster by avoiding rename_lock read seqlock, |
| 1382 | * however it must be used carefully, eg. with a following d_lookup in |
| 1383 | * the case of failure. |
| 1384 | * |
| 1385 | * __d_lookup callers must be commented. |
| 1386 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | struct dentry * __d_lookup(struct dentry * parent, struct qstr * name) |
| 1388 | { |
| 1389 | unsigned int len = name->len; |
| 1390 | unsigned int hash = name->hash; |
| 1391 | const unsigned char *str = name->name; |
| 1392 | struct hlist_head *head = d_hash(parent,hash); |
| 1393 | struct dentry *found = NULL; |
| 1394 | struct hlist_node *node; |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 1395 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1397 | /* |
| 1398 | * The hash list is protected using RCU. |
| 1399 | * |
| 1400 | * Take d_lock when comparing a candidate dentry, to avoid races |
| 1401 | * with d_move(). |
| 1402 | * |
| 1403 | * It is possible that concurrent renames can mess up our list |
| 1404 | * walk here and result in missing our dentry, resulting in the |
| 1405 | * false-negative result. d_lookup() protects against concurrent |
| 1406 | * renames using rename_lock seqlock. |
| 1407 | * |
| 1408 | * See Documentation/vfs/dcache-locking.txt for more details. |
| 1409 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | rcu_read_lock(); |
| 1411 | |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 1412 | hlist_for_each_entry_rcu(dentry, node, head, d_hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | struct qstr *qstr; |
| 1414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | if (dentry->d_name.hash != hash) |
| 1416 | continue; |
| 1417 | if (dentry->d_parent != parent) |
| 1418 | continue; |
| 1419 | |
| 1420 | spin_lock(&dentry->d_lock); |
| 1421 | |
| 1422 | /* |
| 1423 | * Recheck the dentry after taking the lock - d_move may have |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1424 | * changed things. Don't bother checking the hash because |
| 1425 | * we're about to compare the whole name anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | */ |
| 1427 | if (dentry->d_parent != parent) |
| 1428 | goto next; |
| 1429 | |
Linus Torvalds | d0185c0 | 2008-09-29 07:42:57 -0700 | [diff] [blame] | 1430 | /* non-existing due to RCU? */ |
| 1431 | if (d_unhashed(dentry)) |
| 1432 | goto next; |
| 1433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | /* |
| 1435 | * It is safe to compare names since d_move() cannot |
| 1436 | * change the qstr (protected by d_lock). |
| 1437 | */ |
| 1438 | qstr = &dentry->d_name; |
| 1439 | if (parent->d_op && parent->d_op->d_compare) { |
| 1440 | if (parent->d_op->d_compare(parent, qstr, name)) |
| 1441 | goto next; |
| 1442 | } else { |
| 1443 | if (qstr->len != len) |
| 1444 | goto next; |
| 1445 | if (memcmp(qstr->name, str, len)) |
| 1446 | goto next; |
| 1447 | } |
| 1448 | |
Linus Torvalds | d0185c0 | 2008-09-29 07:42:57 -0700 | [diff] [blame] | 1449 | atomic_inc(&dentry->d_count); |
| 1450 | found = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | spin_unlock(&dentry->d_lock); |
| 1452 | break; |
| 1453 | next: |
| 1454 | spin_unlock(&dentry->d_lock); |
| 1455 | } |
| 1456 | rcu_read_unlock(); |
| 1457 | |
| 1458 | return found; |
| 1459 | } |
| 1460 | |
| 1461 | /** |
Eric W. Biederman | 3e7e241 | 2006-03-31 02:31:43 -0800 | [diff] [blame] | 1462 | * d_hash_and_lookup - hash the qstr then search for a dentry |
| 1463 | * @dir: Directory to search in |
| 1464 | * @name: qstr of name we wish to find |
| 1465 | * |
| 1466 | * On hash failure or on lookup failure NULL is returned. |
| 1467 | */ |
| 1468 | struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) |
| 1469 | { |
| 1470 | struct dentry *dentry = NULL; |
| 1471 | |
| 1472 | /* |
| 1473 | * Check for a fs-specific hash function. Note that we must |
| 1474 | * calculate the standard hash first, as the d_op->d_hash() |
| 1475 | * routine may choose to leave the hash value unchanged. |
| 1476 | */ |
| 1477 | name->hash = full_name_hash(name->name, name->len); |
| 1478 | if (dir->d_op && dir->d_op->d_hash) { |
| 1479 | if (dir->d_op->d_hash(dir, name) < 0) |
| 1480 | goto out; |
| 1481 | } |
| 1482 | dentry = d_lookup(dir, name); |
| 1483 | out: |
| 1484 | return dentry; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
Nick Piggin | 786a5e1 | 2011-01-07 17:49:16 +1100 | [diff] [blame] | 1488 | * d_validate - verify dentry provided from insecure source (deprecated) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | * @dentry: The dentry alleged to be valid child of @dparent |
| 1490 | * @dparent: The parent dentry (known to be valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | * |
| 1492 | * An insecure source has sent us a dentry, here we verify it and dget() it. |
| 1493 | * This is used by ncpfs in its readdir implementation. |
| 1494 | * Zero is returned in the dentry is invalid. |
Nick Piggin | 786a5e1 | 2011-01-07 17:49:16 +1100 | [diff] [blame] | 1495 | * |
| 1496 | * This function is slow for big directories, and deprecated, do not use it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | */ |
Nick Piggin | d3a23e1 | 2011-01-05 20:01:21 +1100 | [diff] [blame] | 1498 | int d_validate(struct dentry *dentry, struct dentry *dparent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | { |
Nick Piggin | 786a5e1 | 2011-01-07 17:49:16 +1100 | [diff] [blame] | 1500 | struct dentry *child; |
Nick Piggin | d3a23e1 | 2011-01-05 20:01:21 +1100 | [diff] [blame] | 1501 | |
| 1502 | spin_lock(&dcache_lock); |
Nick Piggin | 786a5e1 | 2011-01-07 17:49:16 +1100 | [diff] [blame] | 1503 | list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) { |
| 1504 | if (dentry == child) { |
Nick Piggin | d3a23e1 | 2011-01-05 20:01:21 +1100 | [diff] [blame] | 1505 | __dget_locked(dentry); |
| 1506 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | return 1; |
| 1508 | } |
| 1509 | } |
Nick Piggin | d3a23e1 | 2011-01-05 20:01:21 +1100 | [diff] [blame] | 1510 | spin_unlock(&dcache_lock); |
Nick Piggin | 786a5e1 | 2011-01-07 17:49:16 +1100 | [diff] [blame] | 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | return 0; |
| 1513 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1514 | EXPORT_SYMBOL(d_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
| 1516 | /* |
| 1517 | * When a file is deleted, we have two options: |
| 1518 | * - turn this dentry into a negative dentry |
| 1519 | * - unhash this dentry and free it. |
| 1520 | * |
| 1521 | * Usually, we want to just turn this into |
| 1522 | * a negative dentry, but if anybody else is |
| 1523 | * currently using the dentry or the inode |
| 1524 | * we can't do that and we fall back on removing |
| 1525 | * it from the hash queues and waiting for |
| 1526 | * it to be deleted later when it has no users |
| 1527 | */ |
| 1528 | |
| 1529 | /** |
| 1530 | * d_delete - delete a dentry |
| 1531 | * @dentry: The dentry to delete |
| 1532 | * |
| 1533 | * Turn the dentry into a negative dentry if possible, otherwise |
| 1534 | * remove it from the hash queues so it can be deleted later |
| 1535 | */ |
| 1536 | |
| 1537 | void d_delete(struct dentry * dentry) |
| 1538 | { |
John McCutchan | 7a91bf7 | 2005-08-08 13:52:16 -0400 | [diff] [blame] | 1539 | int isdir = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | /* |
| 1541 | * Are we the only user? |
| 1542 | */ |
| 1543 | spin_lock(&dcache_lock); |
| 1544 | spin_lock(&dentry->d_lock); |
John McCutchan | 7a91bf7 | 2005-08-08 13:52:16 -0400 | [diff] [blame] | 1545 | isdir = S_ISDIR(dentry->d_inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | if (atomic_read(&dentry->d_count) == 1) { |
Al Viro | 13e3c5e | 2010-05-21 16:11:04 -0400 | [diff] [blame] | 1547 | dentry->d_flags &= ~DCACHE_CANT_MOUNT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | dentry_iput(dentry); |
John McCutchan | 7a91bf7 | 2005-08-08 13:52:16 -0400 | [diff] [blame] | 1549 | fsnotify_nameremove(dentry, isdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | if (!d_unhashed(dentry)) |
| 1554 | __d_drop(dentry); |
| 1555 | |
| 1556 | spin_unlock(&dentry->d_lock); |
| 1557 | spin_unlock(&dcache_lock); |
John McCutchan | 7a91bf7 | 2005-08-08 13:52:16 -0400 | [diff] [blame] | 1558 | |
| 1559 | fsnotify_nameremove(dentry, isdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1561 | EXPORT_SYMBOL(d_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
| 1563 | static void __d_rehash(struct dentry * entry, struct hlist_head *list) |
| 1564 | { |
| 1565 | |
| 1566 | entry->d_flags &= ~DCACHE_UNHASHED; |
| 1567 | hlist_add_head_rcu(&entry->d_hash, list); |
| 1568 | } |
| 1569 | |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1570 | static void _d_rehash(struct dentry * entry) |
| 1571 | { |
| 1572 | __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash)); |
| 1573 | } |
| 1574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | /** |
| 1576 | * d_rehash - add an entry back to the hash |
| 1577 | * @entry: dentry to add to the hash |
| 1578 | * |
| 1579 | * Adds a dentry to the hash according to its name. |
| 1580 | */ |
| 1581 | |
| 1582 | void d_rehash(struct dentry * entry) |
| 1583 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | spin_lock(&dcache_lock); |
| 1585 | spin_lock(&entry->d_lock); |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1586 | _d_rehash(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | spin_unlock(&entry->d_lock); |
| 1588 | spin_unlock(&dcache_lock); |
| 1589 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1590 | EXPORT_SYMBOL(d_rehash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
Nick Piggin | fb2d5b8 | 2011-01-07 17:49:26 +1100 | [diff] [blame^] | 1592 | /** |
| 1593 | * dentry_update_name_case - update case insensitive dentry with a new name |
| 1594 | * @dentry: dentry to be updated |
| 1595 | * @name: new name |
| 1596 | * |
| 1597 | * Update a case insensitive dentry with new case of name. |
| 1598 | * |
| 1599 | * dentry must have been returned by d_lookup with name @name. Old and new |
| 1600 | * name lengths must match (ie. no d_compare which allows mismatched name |
| 1601 | * lengths). |
| 1602 | * |
| 1603 | * Parent inode i_mutex must be held over d_lookup and into this call (to |
| 1604 | * keep renames and concurrent inserts, and readdir(2) away). |
| 1605 | */ |
| 1606 | void dentry_update_name_case(struct dentry *dentry, struct qstr *name) |
| 1607 | { |
| 1608 | BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 1609 | BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */ |
| 1610 | |
| 1611 | spin_lock(&dcache_lock); |
| 1612 | spin_lock(&dentry->d_lock); |
| 1613 | memcpy((unsigned char *)dentry->d_name.name, name->name, name->len); |
| 1614 | spin_unlock(&dentry->d_lock); |
| 1615 | spin_unlock(&dcache_lock); |
| 1616 | } |
| 1617 | EXPORT_SYMBOL(dentry_update_name_case); |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | /* |
| 1620 | * When switching names, the actual string doesn't strictly have to |
| 1621 | * be preserved in the target - because we're dropping the target |
| 1622 | * anyway. As such, we can just do a simple memcpy() to copy over |
| 1623 | * the new name before we switch. |
| 1624 | * |
| 1625 | * Note that we have to be a lot more careful about getting the hash |
| 1626 | * switched - we have to switch the hash value properly even if it |
| 1627 | * then no longer matches the actual (corrupted) string of the target. |
| 1628 | * The hash value has to match the hash queue that the dentry is on.. |
| 1629 | */ |
| 1630 | static void switch_names(struct dentry *dentry, struct dentry *target) |
| 1631 | { |
| 1632 | if (dname_external(target)) { |
| 1633 | if (dname_external(dentry)) { |
| 1634 | /* |
| 1635 | * Both external: swap the pointers |
| 1636 | */ |
Wu Fengguang | 9a8d5bb | 2009-01-07 18:09:14 -0800 | [diff] [blame] | 1637 | swap(target->d_name.name, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } else { |
| 1639 | /* |
| 1640 | * dentry:internal, target:external. Steal target's |
| 1641 | * storage and make target internal. |
| 1642 | */ |
J. Bruce Fields | 321bcf9 | 2007-10-21 16:41:38 -0700 | [diff] [blame] | 1643 | memcpy(target->d_iname, dentry->d_name.name, |
| 1644 | dentry->d_name.len + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | dentry->d_name.name = target->d_name.name; |
| 1646 | target->d_name.name = target->d_iname; |
| 1647 | } |
| 1648 | } else { |
| 1649 | if (dname_external(dentry)) { |
| 1650 | /* |
| 1651 | * dentry:external, target:internal. Give dentry's |
| 1652 | * storage to target and make dentry internal |
| 1653 | */ |
| 1654 | memcpy(dentry->d_iname, target->d_name.name, |
| 1655 | target->d_name.len + 1); |
| 1656 | target->d_name.name = dentry->d_name.name; |
| 1657 | dentry->d_name.name = dentry->d_iname; |
| 1658 | } else { |
| 1659 | /* |
| 1660 | * Both are internal. Just copy target to dentry |
| 1661 | */ |
| 1662 | memcpy(dentry->d_iname, target->d_name.name, |
| 1663 | target->d_name.len + 1); |
Al Viro | dc711ca | 2008-11-03 15:03:50 -0500 | [diff] [blame] | 1664 | dentry->d_name.len = target->d_name.len; |
| 1665 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | } |
| 1667 | } |
Wu Fengguang | 9a8d5bb | 2009-01-07 18:09:14 -0800 | [diff] [blame] | 1668 | swap(dentry->d_name.len, target->d_name.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * We cannibalize "target" when moving dentry on top of it, |
| 1673 | * because it's going to be thrown away anyway. We could be more |
| 1674 | * polite about it, though. |
| 1675 | * |
| 1676 | * This forceful removal will result in ugly /proc output if |
| 1677 | * somebody holds a file open that got deleted due to a rename. |
| 1678 | * We could be nicer about the deleted file, and let it show |
J. Bruce Fields | bc154b1 | 2007-10-16 23:29:42 -0700 | [diff] [blame] | 1679 | * up under the name it had before it was deleted rather than |
| 1680 | * under the original name of the file that was moved on top of it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | */ |
| 1682 | |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1683 | /* |
| 1684 | * d_move_locked - move a dentry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | * @dentry: entry to move |
| 1686 | * @target: new dentry |
| 1687 | * |
| 1688 | * Update the dcache to reflect the move of a file name. Negative |
| 1689 | * dcache entries should not be moved in this way. |
| 1690 | */ |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1691 | static void d_move_locked(struct dentry * dentry, struct dentry * target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | { |
| 1693 | struct hlist_head *list; |
| 1694 | |
| 1695 | if (!dentry->d_inode) |
| 1696 | printk(KERN_WARNING "VFS: moving negative dcache entry\n"); |
| 1697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | write_seqlock(&rename_lock); |
| 1699 | /* |
| 1700 | * XXXX: do we really need to take target->d_lock? |
| 1701 | */ |
| 1702 | if (target < dentry) { |
| 1703 | spin_lock(&target->d_lock); |
Ingo Molnar | a90b9c0 | 2006-07-03 00:25:04 -0700 | [diff] [blame] | 1704 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } else { |
| 1706 | spin_lock(&dentry->d_lock); |
Ingo Molnar | a90b9c0 | 2006-07-03 00:25:04 -0700 | [diff] [blame] | 1707 | spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /* Move the dentry to the target hash queue, if on different bucket */ |
Denis Cheng | f77e349 | 2007-10-16 23:30:11 -0700 | [diff] [blame] | 1711 | if (d_unhashed(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | goto already_unhashed; |
| 1713 | |
| 1714 | hlist_del_rcu(&dentry->d_hash); |
| 1715 | |
| 1716 | already_unhashed: |
| 1717 | list = d_hash(target->d_parent, target->d_name.hash); |
| 1718 | __d_rehash(dentry, list); |
| 1719 | |
| 1720 | /* Unhash the target: dput() will then get rid of it */ |
| 1721 | __d_drop(target); |
| 1722 | |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 1723 | list_del(&dentry->d_u.d_child); |
| 1724 | list_del(&target->d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
| 1726 | /* Switch the names.. */ |
| 1727 | switch_names(dentry, target); |
Wu Fengguang | 9a8d5bb | 2009-01-07 18:09:14 -0800 | [diff] [blame] | 1728 | swap(dentry->d_name.hash, target->d_name.hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
| 1730 | /* ... and switch the parents */ |
| 1731 | if (IS_ROOT(dentry)) { |
| 1732 | dentry->d_parent = target->d_parent; |
| 1733 | target->d_parent = target; |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 1734 | INIT_LIST_HEAD(&target->d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | } else { |
Wu Fengguang | 9a8d5bb | 2009-01-07 18:09:14 -0800 | [diff] [blame] | 1736 | swap(dentry->d_parent, target->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | |
| 1738 | /* And add them back to the (new) parent lists */ |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 1739 | list_add(&target->d_u.d_child, &target->d_parent->d_subdirs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 1742 | list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | spin_unlock(&target->d_lock); |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 1744 | fsnotify_d_move(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | spin_unlock(&dentry->d_lock); |
| 1746 | write_sequnlock(&rename_lock); |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | /** |
| 1750 | * d_move - move a dentry |
| 1751 | * @dentry: entry to move |
| 1752 | * @target: new dentry |
| 1753 | * |
| 1754 | * Update the dcache to reflect the move of a file name. Negative |
| 1755 | * dcache entries should not be moved in this way. |
| 1756 | */ |
| 1757 | |
| 1758 | void d_move(struct dentry * dentry, struct dentry * target) |
| 1759 | { |
| 1760 | spin_lock(&dcache_lock); |
| 1761 | d_move_locked(dentry, target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | spin_unlock(&dcache_lock); |
| 1763 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1764 | EXPORT_SYMBOL(d_move); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1766 | /** |
| 1767 | * d_ancestor - search for an ancestor |
| 1768 | * @p1: ancestor dentry |
| 1769 | * @p2: child dentry |
| 1770 | * |
| 1771 | * Returns the ancestor dentry of p2 which is a child of p1, if p1 is |
| 1772 | * an ancestor of p2, else NULL. |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1773 | */ |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1774 | struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2) |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1775 | { |
| 1776 | struct dentry *p; |
| 1777 | |
OGAWA Hirofumi | 871c006 | 2008-10-16 07:50:27 +0900 | [diff] [blame] | 1778 | for (p = p2; !IS_ROOT(p); p = p->d_parent) { |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1779 | if (p->d_parent == p1) |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1780 | return p; |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1781 | } |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1782 | return NULL; |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * This helper attempts to cope with remotely renamed directories |
| 1787 | * |
| 1788 | * It assumes that the caller is already holding |
| 1789 | * dentry->d_parent->d_inode->i_mutex and the dcache_lock |
| 1790 | * |
| 1791 | * Note: If ever the locking in lock_rename() changes, then please |
| 1792 | * remember to update this too... |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1793 | */ |
| 1794 | static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias) |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 1795 | __releases(dcache_lock) |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1796 | { |
| 1797 | struct mutex *m1 = NULL, *m2 = NULL; |
| 1798 | struct dentry *ret; |
| 1799 | |
| 1800 | /* If alias and dentry share a parent, then no extra locks required */ |
| 1801 | if (alias->d_parent == dentry->d_parent) |
| 1802 | goto out_unalias; |
| 1803 | |
| 1804 | /* Check for loops */ |
| 1805 | ret = ERR_PTR(-ELOOP); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1806 | if (d_ancestor(alias, dentry)) |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1807 | goto out_err; |
| 1808 | |
| 1809 | /* See lock_rename() */ |
| 1810 | ret = ERR_PTR(-EBUSY); |
| 1811 | if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex)) |
| 1812 | goto out_err; |
| 1813 | m1 = &dentry->d_sb->s_vfs_rename_mutex; |
| 1814 | if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex)) |
| 1815 | goto out_err; |
| 1816 | m2 = &alias->d_parent->d_inode->i_mutex; |
| 1817 | out_unalias: |
| 1818 | d_move_locked(alias, dentry); |
| 1819 | ret = alias; |
| 1820 | out_err: |
| 1821 | spin_unlock(&dcache_lock); |
| 1822 | if (m2) |
| 1823 | mutex_unlock(m2); |
| 1824 | if (m1) |
| 1825 | mutex_unlock(m1); |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1830 | * Prepare an anonymous dentry for life in the superblock's dentry tree as a |
| 1831 | * named dentry in place of the dentry to be replaced. |
| 1832 | */ |
| 1833 | static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon) |
| 1834 | { |
| 1835 | struct dentry *dparent, *aparent; |
| 1836 | |
| 1837 | switch_names(dentry, anon); |
Wu Fengguang | 9a8d5bb | 2009-01-07 18:09:14 -0800 | [diff] [blame] | 1838 | swap(dentry->d_name.hash, anon->d_name.hash); |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1839 | |
| 1840 | dparent = dentry->d_parent; |
| 1841 | aparent = anon->d_parent; |
| 1842 | |
| 1843 | dentry->d_parent = (aparent == anon) ? dentry : aparent; |
| 1844 | list_del(&dentry->d_u.d_child); |
| 1845 | if (!IS_ROOT(dentry)) |
| 1846 | list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs); |
| 1847 | else |
| 1848 | INIT_LIST_HEAD(&dentry->d_u.d_child); |
| 1849 | |
| 1850 | anon->d_parent = (dparent == dentry) ? anon : dparent; |
| 1851 | list_del(&anon->d_u.d_child); |
| 1852 | if (!IS_ROOT(anon)) |
| 1853 | list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs); |
| 1854 | else |
| 1855 | INIT_LIST_HEAD(&anon->d_u.d_child); |
| 1856 | |
| 1857 | anon->d_flags &= ~DCACHE_DISCONNECTED; |
| 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * d_materialise_unique - introduce an inode into the tree |
| 1862 | * @dentry: candidate dentry |
| 1863 | * @inode: inode to bind to the dentry, to which aliases may be attached |
| 1864 | * |
| 1865 | * Introduces an dentry into the tree, substituting an extant disconnected |
| 1866 | * root directory alias in its place if there is one |
| 1867 | */ |
| 1868 | struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode) |
| 1869 | { |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1870 | struct dentry *actual; |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1871 | |
| 1872 | BUG_ON(!d_unhashed(dentry)); |
| 1873 | |
| 1874 | spin_lock(&dcache_lock); |
| 1875 | |
| 1876 | if (!inode) { |
| 1877 | actual = dentry; |
OGAWA Hirofumi | 360da90 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 1878 | __d_instantiate(dentry, NULL); |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1879 | goto found_lock; |
| 1880 | } |
| 1881 | |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1882 | if (S_ISDIR(inode->i_mode)) { |
| 1883 | struct dentry *alias; |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1884 | |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1885 | /* Does an aliased dentry already exist? */ |
| 1886 | alias = __d_find_alias(inode, 0); |
| 1887 | if (alias) { |
| 1888 | actual = alias; |
| 1889 | /* Is this an anonymous mountpoint that we could splice |
| 1890 | * into our tree? */ |
| 1891 | if (IS_ROOT(alias)) { |
| 1892 | spin_lock(&alias->d_lock); |
| 1893 | __d_materialise_dentry(dentry, alias); |
| 1894 | __d_drop(alias); |
| 1895 | goto found; |
| 1896 | } |
| 1897 | /* Nope, but we must(!) avoid directory aliasing */ |
| 1898 | actual = __d_unalias(dentry, alias); |
| 1899 | if (IS_ERR(actual)) |
| 1900 | dput(alias); |
| 1901 | goto out_nolock; |
| 1902 | } |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | /* Add a unique reference */ |
| 1906 | actual = __d_instantiate_unique(dentry, inode); |
| 1907 | if (!actual) |
| 1908 | actual = dentry; |
| 1909 | else if (unlikely(!d_unhashed(actual))) |
| 1910 | goto shouldnt_be_hashed; |
| 1911 | |
| 1912 | found_lock: |
| 1913 | spin_lock(&actual->d_lock); |
| 1914 | found: |
| 1915 | _d_rehash(actual); |
| 1916 | spin_unlock(&actual->d_lock); |
| 1917 | spin_unlock(&dcache_lock); |
Trond Myklebust | 9eaef27 | 2006-10-21 10:24:20 -0700 | [diff] [blame] | 1918 | out_nolock: |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1919 | if (actual == dentry) { |
| 1920 | security_d_instantiate(dentry, inode); |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | |
| 1924 | iput(inode); |
| 1925 | return actual; |
| 1926 | |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1927 | shouldnt_be_hashed: |
| 1928 | spin_unlock(&dcache_lock); |
| 1929 | BUG(); |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1930 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 1931 | EXPORT_SYMBOL_GPL(d_materialise_unique); |
David Howells | 770bfad | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 1932 | |
Miklos Szeredi | cdd16d0 | 2008-06-23 18:11:53 +0200 | [diff] [blame] | 1933 | static int prepend(char **buffer, int *buflen, const char *str, int namelen) |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 1934 | { |
| 1935 | *buflen -= namelen; |
| 1936 | if (*buflen < 0) |
| 1937 | return -ENAMETOOLONG; |
| 1938 | *buffer -= namelen; |
| 1939 | memcpy(*buffer, str, namelen); |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
Miklos Szeredi | cdd16d0 | 2008-06-23 18:11:53 +0200 | [diff] [blame] | 1943 | static int prepend_name(char **buffer, int *buflen, struct qstr *name) |
| 1944 | { |
| 1945 | return prepend(buffer, buflen, name->name, name->len); |
| 1946 | } |
| 1947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | /** |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 1949 | * Prepend path string to a buffer |
| 1950 | * |
| 1951 | * @path: the dentry/vfsmount to report |
| 1952 | * @root: root vfsmnt/dentry (may be modified by this function) |
| 1953 | * @buffer: pointer to the end of the buffer |
| 1954 | * @buflen: pointer to buffer length |
| 1955 | * |
| 1956 | * Caller holds the dcache_lock. |
| 1957 | * |
| 1958 | * If path is not reachable from the supplied root, then the value of |
| 1959 | * root is changed (without modifying refcounts). |
| 1960 | */ |
| 1961 | static int prepend_path(const struct path *path, struct path *root, |
| 1962 | char **buffer, int *buflen) |
| 1963 | { |
| 1964 | struct dentry *dentry = path->dentry; |
| 1965 | struct vfsmount *vfsmnt = path->mnt; |
| 1966 | bool slash = false; |
| 1967 | int error = 0; |
| 1968 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1969 | br_read_lock(vfsmount_lock); |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 1970 | while (dentry != root->dentry || vfsmnt != root->mnt) { |
| 1971 | struct dentry * parent; |
| 1972 | |
| 1973 | if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
| 1974 | /* Global root? */ |
| 1975 | if (vfsmnt->mnt_parent == vfsmnt) { |
| 1976 | goto global_root; |
| 1977 | } |
| 1978 | dentry = vfsmnt->mnt_mountpoint; |
| 1979 | vfsmnt = vfsmnt->mnt_parent; |
| 1980 | continue; |
| 1981 | } |
| 1982 | parent = dentry->d_parent; |
| 1983 | prefetch(parent); |
| 1984 | error = prepend_name(buffer, buflen, &dentry->d_name); |
| 1985 | if (!error) |
| 1986 | error = prepend(buffer, buflen, "/", 1); |
| 1987 | if (error) |
| 1988 | break; |
| 1989 | |
| 1990 | slash = true; |
| 1991 | dentry = parent; |
| 1992 | } |
| 1993 | |
| 1994 | out: |
| 1995 | if (!error && !slash) |
| 1996 | error = prepend(buffer, buflen, "/", 1); |
| 1997 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1998 | br_read_unlock(vfsmount_lock); |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 1999 | return error; |
| 2000 | |
| 2001 | global_root: |
| 2002 | /* |
| 2003 | * Filesystems needing to implement special "root names" |
| 2004 | * should do so with ->d_dname() |
| 2005 | */ |
| 2006 | if (IS_ROOT(dentry) && |
| 2007 | (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) { |
| 2008 | WARN(1, "Root dentry has weird name <%.*s>\n", |
| 2009 | (int) dentry->d_name.len, dentry->d_name.name); |
| 2010 | } |
| 2011 | root->mnt = vfsmnt; |
| 2012 | root->dentry = dentry; |
| 2013 | goto out; |
| 2014 | } |
| 2015 | |
| 2016 | /** |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 2017 | * __d_path - return the path of a dentry |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 2018 | * @path: the dentry/vfsmount to report |
| 2019 | * @root: root vfsmnt/dentry (may be modified by this function) |
Randy Dunlap | cd956a1 | 2010-08-14 13:05:31 -0700 | [diff] [blame] | 2020 | * @buf: buffer to return value in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | * @buflen: buffer length |
| 2022 | * |
Miklos Szeredi | ffd1f4e | 2010-08-10 11:41:40 +0200 | [diff] [blame] | 2023 | * Convert a dentry into an ASCII path name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | * |
Arjan van de Ven | 52afeef | 2008-12-01 14:35:00 -0800 | [diff] [blame] | 2025 | * Returns a pointer into the buffer or an error code if the |
| 2026 | * path was too long. |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2027 | * |
Christoph Hellwig | be14824 | 2010-10-10 05:36:21 -0400 | [diff] [blame] | 2028 | * "buflen" should be positive. |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 2029 | * |
| 2030 | * If path is not reachable from the supplied root, then the value of |
| 2031 | * root is changed (without modifying refcounts). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | */ |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 2033 | char *__d_path(const struct path *path, struct path *root, |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 2034 | char *buf, int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | { |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 2036 | char *res = buf + buflen; |
| 2037 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 2039 | prepend(&res, &buflen, "\0", 1); |
Christoph Hellwig | be14824 | 2010-10-10 05:36:21 -0400 | [diff] [blame] | 2040 | spin_lock(&dcache_lock); |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 2041 | error = prepend_path(path, root, &res, &buflen); |
Christoph Hellwig | be14824 | 2010-10-10 05:36:21 -0400 | [diff] [blame] | 2042 | spin_unlock(&dcache_lock); |
| 2043 | |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 2044 | if (error) |
| 2045 | return ERR_PTR(error); |
Miklos Szeredi | f2eb657 | 2010-08-10 11:41:39 +0200 | [diff] [blame] | 2046 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Miklos Szeredi | ffd1f4e | 2010-08-10 11:41:40 +0200 | [diff] [blame] | 2049 | /* |
| 2050 | * same as __d_path but appends "(deleted)" for unlinked files. |
| 2051 | */ |
| 2052 | static int path_with_deleted(const struct path *path, struct path *root, |
| 2053 | char **buf, int *buflen) |
| 2054 | { |
| 2055 | prepend(buf, buflen, "\0", 1); |
| 2056 | if (d_unlinked(path->dentry)) { |
| 2057 | int error = prepend(buf, buflen, " (deleted)", 10); |
| 2058 | if (error) |
| 2059 | return error; |
| 2060 | } |
| 2061 | |
| 2062 | return prepend_path(path, root, buf, buflen); |
| 2063 | } |
| 2064 | |
Miklos Szeredi | 8df9d1a | 2010-08-10 11:41:41 +0200 | [diff] [blame] | 2065 | static int prepend_unreachable(char **buffer, int *buflen) |
| 2066 | { |
| 2067 | return prepend(buffer, buflen, "(unreachable)", 13); |
| 2068 | } |
| 2069 | |
Jan Blunck | a03a8a70 | 2008-02-14 19:38:32 -0800 | [diff] [blame] | 2070 | /** |
| 2071 | * d_path - return the path of a dentry |
Jan Blunck | cf28b48 | 2008-02-14 19:38:44 -0800 | [diff] [blame] | 2072 | * @path: path to report |
Jan Blunck | a03a8a70 | 2008-02-14 19:38:32 -0800 | [diff] [blame] | 2073 | * @buf: buffer to return value in |
| 2074 | * @buflen: buffer length |
| 2075 | * |
| 2076 | * Convert a dentry into an ASCII path name. If the entry has been deleted |
| 2077 | * the string " (deleted)" is appended. Note that this is ambiguous. |
| 2078 | * |
Arjan van de Ven | 52afeef | 2008-12-01 14:35:00 -0800 | [diff] [blame] | 2079 | * Returns a pointer into the buffer or an error code if the path was |
| 2080 | * too long. Note: Callers should use the returned pointer, not the passed |
| 2081 | * in buffer, to use the name! The implementation often starts at an offset |
| 2082 | * into the buffer, and may leave 0 bytes at the start. |
Jan Blunck | a03a8a70 | 2008-02-14 19:38:32 -0800 | [diff] [blame] | 2083 | * |
Miklos Szeredi | 31f3e0b | 2008-06-23 18:11:52 +0200 | [diff] [blame] | 2084 | * "buflen" should be positive. |
Jan Blunck | a03a8a70 | 2008-02-14 19:38:32 -0800 | [diff] [blame] | 2085 | */ |
Jan Engelhardt | 20d4fdc | 2008-06-09 16:40:36 -0700 | [diff] [blame] | 2086 | char *d_path(const struct path *path, char *buf, int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | { |
Miklos Szeredi | ffd1f4e | 2010-08-10 11:41:40 +0200 | [diff] [blame] | 2088 | char *res = buf + buflen; |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2089 | struct path root; |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 2090 | struct path tmp; |
Miklos Szeredi | ffd1f4e | 2010-08-10 11:41:40 +0200 | [diff] [blame] | 2091 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 2093 | /* |
| 2094 | * We have various synthetic filesystems that never get mounted. On |
| 2095 | * these filesystems dentries are never used for lookup purposes, and |
| 2096 | * thus don't need to be hashed. They also don't need a name until a |
| 2097 | * user wants to identify the object in /proc/pid/fd/. The little hack |
| 2098 | * below allows us to generate a name for these objects on demand: |
| 2099 | */ |
Jan Blunck | cf28b48 | 2008-02-14 19:38:44 -0800 | [diff] [blame] | 2100 | if (path->dentry->d_op && path->dentry->d_op->d_dname) |
| 2101 | return path->dentry->d_op->d_dname(path->dentry, buf, buflen); |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 2102 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 2103 | get_fs_root(current->fs, &root); |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2104 | spin_lock(&dcache_lock); |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 2105 | tmp = root; |
Miklos Szeredi | ffd1f4e | 2010-08-10 11:41:40 +0200 | [diff] [blame] | 2106 | error = path_with_deleted(path, &tmp, &res, &buflen); |
| 2107 | if (error) |
| 2108 | res = ERR_PTR(error); |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2109 | spin_unlock(&dcache_lock); |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2110 | path_put(&root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | return res; |
| 2112 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 2113 | EXPORT_SYMBOL(d_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | |
Miklos Szeredi | 8df9d1a | 2010-08-10 11:41:41 +0200 | [diff] [blame] | 2115 | /** |
| 2116 | * d_path_with_unreachable - return the path of a dentry |
| 2117 | * @path: path to report |
| 2118 | * @buf: buffer to return value in |
| 2119 | * @buflen: buffer length |
| 2120 | * |
| 2121 | * The difference from d_path() is that this prepends "(unreachable)" |
| 2122 | * to paths which are unreachable from the current process' root. |
| 2123 | */ |
| 2124 | char *d_path_with_unreachable(const struct path *path, char *buf, int buflen) |
| 2125 | { |
| 2126 | char *res = buf + buflen; |
| 2127 | struct path root; |
| 2128 | struct path tmp; |
| 2129 | int error; |
| 2130 | |
| 2131 | if (path->dentry->d_op && path->dentry->d_op->d_dname) |
| 2132 | return path->dentry->d_op->d_dname(path->dentry, buf, buflen); |
| 2133 | |
| 2134 | get_fs_root(current->fs, &root); |
| 2135 | spin_lock(&dcache_lock); |
| 2136 | tmp = root; |
| 2137 | error = path_with_deleted(path, &tmp, &res, &buflen); |
| 2138 | if (!error && !path_equal(&tmp, &root)) |
| 2139 | error = prepend_unreachable(&res, &buflen); |
| 2140 | spin_unlock(&dcache_lock); |
| 2141 | path_put(&root); |
| 2142 | if (error) |
| 2143 | res = ERR_PTR(error); |
| 2144 | |
| 2145 | return res; |
| 2146 | } |
| 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | /* |
Eric Dumazet | c23fbb6 | 2007-05-08 00:26:18 -0700 | [diff] [blame] | 2149 | * Helper function for dentry_operations.d_dname() members |
| 2150 | */ |
| 2151 | char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen, |
| 2152 | const char *fmt, ...) |
| 2153 | { |
| 2154 | va_list args; |
| 2155 | char temp[64]; |
| 2156 | int sz; |
| 2157 | |
| 2158 | va_start(args, fmt); |
| 2159 | sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1; |
| 2160 | va_end(args); |
| 2161 | |
| 2162 | if (sz > sizeof(temp) || sz > buflen) |
| 2163 | return ERR_PTR(-ENAMETOOLONG); |
| 2164 | |
| 2165 | buffer += buflen - sz; |
| 2166 | return memcpy(buffer, temp, sz); |
| 2167 | } |
| 2168 | |
| 2169 | /* |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2170 | * Write full pathname from the root of the filesystem into the buffer. |
| 2171 | */ |
Al Viro | c103135 | 2010-06-06 22:31:14 -0400 | [diff] [blame] | 2172 | char *__dentry_path(struct dentry *dentry, char *buf, int buflen) |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2173 | { |
| 2174 | char *end = buf + buflen; |
| 2175 | char *retval; |
| 2176 | |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2177 | prepend(&end, &buflen, "\0", 1); |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2178 | if (buflen < 1) |
| 2179 | goto Elong; |
| 2180 | /* Get '/' right */ |
| 2181 | retval = end-1; |
| 2182 | *retval = '/'; |
| 2183 | |
Miklos Szeredi | cdd16d0 | 2008-06-23 18:11:53 +0200 | [diff] [blame] | 2184 | while (!IS_ROOT(dentry)) { |
| 2185 | struct dentry *parent = dentry->d_parent; |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2186 | |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2187 | prefetch(parent); |
Miklos Szeredi | cdd16d0 | 2008-06-23 18:11:53 +0200 | [diff] [blame] | 2188 | if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) || |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2189 | (prepend(&end, &buflen, "/", 1) != 0)) |
| 2190 | goto Elong; |
| 2191 | |
| 2192 | retval = end; |
| 2193 | dentry = parent; |
| 2194 | } |
Al Viro | c103135 | 2010-06-06 22:31:14 -0400 | [diff] [blame] | 2195 | return retval; |
| 2196 | Elong: |
| 2197 | return ERR_PTR(-ENAMETOOLONG); |
| 2198 | } |
| 2199 | EXPORT_SYMBOL(__dentry_path); |
| 2200 | |
| 2201 | char *dentry_path(struct dentry *dentry, char *buf, int buflen) |
| 2202 | { |
| 2203 | char *p = NULL; |
| 2204 | char *retval; |
| 2205 | |
| 2206 | spin_lock(&dcache_lock); |
| 2207 | if (d_unlinked(dentry)) { |
| 2208 | p = buf + buflen; |
| 2209 | if (prepend(&p, &buflen, "//deleted", 10) != 0) |
| 2210 | goto Elong; |
| 2211 | buflen++; |
| 2212 | } |
| 2213 | retval = __dentry_path(dentry, buf, buflen); |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2214 | spin_unlock(&dcache_lock); |
Al Viro | c103135 | 2010-06-06 22:31:14 -0400 | [diff] [blame] | 2215 | if (!IS_ERR(retval) && p) |
| 2216 | *p = '/'; /* restore '/' overriden with '\0' */ |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 2217 | return retval; |
| 2218 | Elong: |
| 2219 | spin_unlock(&dcache_lock); |
| 2220 | return ERR_PTR(-ENAMETOOLONG); |
| 2221 | } |
| 2222 | |
| 2223 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | * NOTE! The user-level library version returns a |
| 2225 | * character pointer. The kernel system call just |
| 2226 | * returns the length of the buffer filled (which |
| 2227 | * includes the ending '\0' character), or a negative |
| 2228 | * error value. So libc would do something like |
| 2229 | * |
| 2230 | * char *getcwd(char * buf, size_t size) |
| 2231 | * { |
| 2232 | * int retval; |
| 2233 | * |
| 2234 | * retval = sys_getcwd(buf, size); |
| 2235 | * if (retval >= 0) |
| 2236 | * return buf; |
| 2237 | * errno = -retval; |
| 2238 | * return NULL; |
| 2239 | * } |
| 2240 | */ |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 2241 | SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | { |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2243 | int error; |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2244 | struct path pwd, root; |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2245 | char *page = (char *) __get_free_page(GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
| 2247 | if (!page) |
| 2248 | return -ENOMEM; |
| 2249 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 2250 | get_fs_root_and_pwd(current->fs, &root, &pwd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2252 | error = -ENOENT; |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2253 | spin_lock(&dcache_lock); |
Alexey Dobriyan | f3da392 | 2009-05-04 03:32:03 +0400 | [diff] [blame] | 2254 | if (!d_unlinked(pwd.dentry)) { |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2255 | unsigned long len; |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 2256 | struct path tmp = root; |
Miklos Szeredi | 8df9d1a | 2010-08-10 11:41:41 +0200 | [diff] [blame] | 2257 | char *cwd = page + PAGE_SIZE; |
| 2258 | int buflen = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | |
Miklos Szeredi | 8df9d1a | 2010-08-10 11:41:41 +0200 | [diff] [blame] | 2260 | prepend(&cwd, &buflen, "\0", 1); |
| 2261 | error = prepend_path(&pwd, &tmp, &cwd, &buflen); |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2262 | spin_unlock(&dcache_lock); |
| 2263 | |
Miklos Szeredi | 8df9d1a | 2010-08-10 11:41:41 +0200 | [diff] [blame] | 2264 | if (error) |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2265 | goto out; |
| 2266 | |
Miklos Szeredi | 8df9d1a | 2010-08-10 11:41:41 +0200 | [diff] [blame] | 2267 | /* Unreachable from current root */ |
| 2268 | if (!path_equal(&tmp, &root)) { |
| 2269 | error = prepend_unreachable(&cwd, &buflen); |
| 2270 | if (error) |
| 2271 | goto out; |
| 2272 | } |
| 2273 | |
Linus Torvalds | 552ce54 | 2007-02-13 12:08:18 -0800 | [diff] [blame] | 2274 | error = -ERANGE; |
| 2275 | len = PAGE_SIZE + page - cwd; |
| 2276 | if (len <= size) { |
| 2277 | error = len; |
| 2278 | if (copy_to_user(buf, cwd, len)) |
| 2279 | error = -EFAULT; |
| 2280 | } |
| 2281 | } else |
| 2282 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | |
| 2284 | out: |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2285 | path_put(&pwd); |
| 2286 | path_put(&root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | free_page((unsigned long) page); |
| 2288 | return error; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Test whether new_dentry is a subdirectory of old_dentry. |
| 2293 | * |
| 2294 | * Trivially implemented using the dcache structure |
| 2295 | */ |
| 2296 | |
| 2297 | /** |
| 2298 | * is_subdir - is new dentry a subdirectory of old_dentry |
| 2299 | * @new_dentry: new dentry |
| 2300 | * @old_dentry: old dentry |
| 2301 | * |
| 2302 | * Returns 1 if new_dentry is a subdirectory of the parent (at any depth). |
| 2303 | * Returns 0 otherwise. |
| 2304 | * Caller must ensure that "new_dentry" is pinned before calling is_subdir() |
| 2305 | */ |
| 2306 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2307 | int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | { |
| 2309 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | unsigned long seq; |
| 2311 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2312 | if (new_dentry == old_dentry) |
| 2313 | return 1; |
| 2314 | |
| 2315 | /* |
| 2316 | * Need rcu_readlock to protect against the d_parent trashing |
| 2317 | * due to d_move |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | */ |
| 2319 | rcu_read_lock(); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2320 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | /* for restarting inner loop in case of seq retry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | seq = read_seqbegin(&rename_lock); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2323 | if (d_ancestor(old_dentry, new_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | result = 1; |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2325 | else |
| 2326 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | } while (read_seqretry(&rename_lock, seq)); |
| 2328 | rcu_read_unlock(); |
| 2329 | |
| 2330 | return result; |
| 2331 | } |
| 2332 | |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 2333 | int path_is_under(struct path *path1, struct path *path2) |
| 2334 | { |
| 2335 | struct vfsmount *mnt = path1->mnt; |
| 2336 | struct dentry *dentry = path1->dentry; |
| 2337 | int res; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2338 | |
| 2339 | br_read_lock(vfsmount_lock); |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 2340 | if (mnt != path2->mnt) { |
| 2341 | for (;;) { |
| 2342 | if (mnt->mnt_parent == mnt) { |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2343 | br_read_unlock(vfsmount_lock); |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 2344 | return 0; |
| 2345 | } |
| 2346 | if (mnt->mnt_parent == path2->mnt) |
| 2347 | break; |
| 2348 | mnt = mnt->mnt_parent; |
| 2349 | } |
| 2350 | dentry = mnt->mnt_mountpoint; |
| 2351 | } |
| 2352 | res = is_subdir(dentry, path2->dentry); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2353 | br_read_unlock(vfsmount_lock); |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 2354 | return res; |
| 2355 | } |
| 2356 | EXPORT_SYMBOL(path_is_under); |
| 2357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | void d_genocide(struct dentry *root) |
| 2359 | { |
| 2360 | struct dentry *this_parent = root; |
| 2361 | struct list_head *next; |
| 2362 | |
| 2363 | spin_lock(&dcache_lock); |
| 2364 | repeat: |
| 2365 | next = this_parent->d_subdirs.next; |
| 2366 | resume: |
| 2367 | while (next != &this_parent->d_subdirs) { |
| 2368 | struct list_head *tmp = next; |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 2369 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | next = tmp->next; |
| 2371 | if (d_unhashed(dentry)||!dentry->d_inode) |
| 2372 | continue; |
| 2373 | if (!list_empty(&dentry->d_subdirs)) { |
| 2374 | this_parent = dentry; |
| 2375 | goto repeat; |
| 2376 | } |
| 2377 | atomic_dec(&dentry->d_count); |
| 2378 | } |
| 2379 | if (this_parent != root) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 2380 | next = this_parent->d_u.d_child.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | atomic_dec(&this_parent->d_count); |
| 2382 | this_parent = this_parent->d_parent; |
| 2383 | goto resume; |
| 2384 | } |
| 2385 | spin_unlock(&dcache_lock); |
| 2386 | } |
| 2387 | |
| 2388 | /** |
| 2389 | * find_inode_number - check for dentry with name |
| 2390 | * @dir: directory to check |
| 2391 | * @name: Name to find. |
| 2392 | * |
| 2393 | * Check whether a dentry already exists for the given name, |
| 2394 | * and return the inode number if it has an inode. Otherwise |
| 2395 | * 0 is returned. |
| 2396 | * |
| 2397 | * This routine is used to post-process directory listings for |
| 2398 | * filesystems using synthetic inode numbers, and is necessary |
| 2399 | * to keep getcwd() working. |
| 2400 | */ |
| 2401 | |
| 2402 | ino_t find_inode_number(struct dentry *dir, struct qstr *name) |
| 2403 | { |
| 2404 | struct dentry * dentry; |
| 2405 | ino_t ino = 0; |
| 2406 | |
Eric W. Biederman | 3e7e241 | 2006-03-31 02:31:43 -0800 | [diff] [blame] | 2407 | dentry = d_hash_and_lookup(dir, name); |
| 2408 | if (dentry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | if (dentry->d_inode) |
| 2410 | ino = dentry->d_inode->i_ino; |
| 2411 | dput(dentry); |
| 2412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | return ino; |
| 2414 | } |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 2415 | EXPORT_SYMBOL(find_inode_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
| 2417 | static __initdata unsigned long dhash_entries; |
| 2418 | static int __init set_dhash_entries(char *str) |
| 2419 | { |
| 2420 | if (!str) |
| 2421 | return 0; |
| 2422 | dhash_entries = simple_strtoul(str, &str, 0); |
| 2423 | return 1; |
| 2424 | } |
| 2425 | __setup("dhash_entries=", set_dhash_entries); |
| 2426 | |
| 2427 | static void __init dcache_init_early(void) |
| 2428 | { |
| 2429 | int loop; |
| 2430 | |
| 2431 | /* If hashes are distributed across NUMA nodes, defer |
| 2432 | * hash allocation until vmalloc space is available. |
| 2433 | */ |
| 2434 | if (hashdist) |
| 2435 | return; |
| 2436 | |
| 2437 | dentry_hashtable = |
| 2438 | alloc_large_system_hash("Dentry cache", |
| 2439 | sizeof(struct hlist_head), |
| 2440 | dhash_entries, |
| 2441 | 13, |
| 2442 | HASH_EARLY, |
| 2443 | &d_hash_shift, |
| 2444 | &d_hash_mask, |
| 2445 | 0); |
| 2446 | |
| 2447 | for (loop = 0; loop < (1 << d_hash_shift); loop++) |
| 2448 | INIT_HLIST_HEAD(&dentry_hashtable[loop]); |
| 2449 | } |
| 2450 | |
Denis Cheng | 74bf17c | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 2451 | static void __init dcache_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | { |
| 2453 | int loop; |
| 2454 | |
| 2455 | /* |
| 2456 | * A constructor could be added for stable state like the lists, |
| 2457 | * but it is probably not worth it because of the cache nature |
| 2458 | * of the dcache. |
| 2459 | */ |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 2460 | dentry_cache = KMEM_CACHE(dentry, |
| 2461 | SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 2463 | register_shrinker(&dcache_shrinker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | |
| 2465 | /* Hash may have been set up in dcache_init_early */ |
| 2466 | if (!hashdist) |
| 2467 | return; |
| 2468 | |
| 2469 | dentry_hashtable = |
| 2470 | alloc_large_system_hash("Dentry cache", |
| 2471 | sizeof(struct hlist_head), |
| 2472 | dhash_entries, |
| 2473 | 13, |
| 2474 | 0, |
| 2475 | &d_hash_shift, |
| 2476 | &d_hash_mask, |
| 2477 | 0); |
| 2478 | |
| 2479 | for (loop = 0; loop < (1 << d_hash_shift); loop++) |
| 2480 | INIT_HLIST_HEAD(&dentry_hashtable[loop]); |
| 2481 | } |
| 2482 | |
| 2483 | /* SLAB cache for __getname() consumers */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2484 | struct kmem_cache *names_cachep __read_mostly; |
H Hartley Sweeten | ec4f860 | 2010-01-05 13:45:18 -0700 | [diff] [blame] | 2485 | EXPORT_SYMBOL(names_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | EXPORT_SYMBOL(d_genocide); |
| 2488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | void __init vfs_caches_init_early(void) |
| 2490 | { |
| 2491 | dcache_init_early(); |
| 2492 | inode_init_early(); |
| 2493 | } |
| 2494 | |
| 2495 | void __init vfs_caches_init(unsigned long mempages) |
| 2496 | { |
| 2497 | unsigned long reserve; |
| 2498 | |
| 2499 | /* Base hash sizes on available memory, with a reserve equal to |
| 2500 | 150% of current kernel size */ |
| 2501 | |
| 2502 | reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1); |
| 2503 | mempages -= reserve; |
| 2504 | |
| 2505 | names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2506 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | |
Denis Cheng | 74bf17c | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 2508 | dcache_init(); |
| 2509 | inode_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | files_init(mempages); |
Denis Cheng | 74bf17c | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 2511 | mnt_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | bdev_cache_init(); |
| 2513 | chrdev_init(); |
| 2514 | } |