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