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