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