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