diff options
Diffstat (limited to 'fs/dcache.c')
-rw-r--r-- | fs/dcache.c | 262 |
1 files changed, 91 insertions, 171 deletions
diff --git a/fs/dcache.c b/fs/dcache.c index fbdcbca40725..be18598c7fd7 100644 --- a/fs/dcache.c +++ b/fs/dcache.c | |||
@@ -344,6 +344,24 @@ void d_drop(struct dentry *dentry) | |||
344 | EXPORT_SYMBOL(d_drop); | 344 | EXPORT_SYMBOL(d_drop); |
345 | 345 | ||
346 | /* | 346 | /* |
347 | * d_clear_need_lookup - drop a dentry from cache and clear the need lookup flag | ||
348 | * @dentry: dentry to drop | ||
349 | * | ||
350 | * This is called when we do a lookup on a placeholder dentry that needed to be | ||
351 | * looked up. The dentry should have been hashed in order for it to be found by | ||
352 | * the lookup code, but now needs to be unhashed while we do the actual lookup | ||
353 | * and clear the DCACHE_NEED_LOOKUP flag. | ||
354 | */ | ||
355 | void d_clear_need_lookup(struct dentry *dentry) | ||
356 | { | ||
357 | spin_lock(&dentry->d_lock); | ||
358 | __d_drop(dentry); | ||
359 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; | ||
360 | spin_unlock(&dentry->d_lock); | ||
361 | } | ||
362 | EXPORT_SYMBOL(d_clear_need_lookup); | ||
363 | |||
364 | /* | ||
347 | * Finish off a dentry we've decided to kill. | 365 | * Finish off a dentry we've decided to kill. |
348 | * dentry->d_lock must be held, returns with it unlocked. | 366 | * dentry->d_lock must be held, returns with it unlocked. |
349 | * If ref is non-zero, then decrement the refcount too. | 367 | * If ref is non-zero, then decrement the refcount too. |
@@ -432,8 +450,13 @@ repeat: | |||
432 | if (d_unhashed(dentry)) | 450 | if (d_unhashed(dentry)) |
433 | goto kill_it; | 451 | goto kill_it; |
434 | 452 | ||
435 | /* Otherwise leave it cached and ensure it's on the LRU */ | 453 | /* |
436 | dentry->d_flags |= DCACHE_REFERENCED; | 454 | * If this dentry needs lookup, don't set the referenced flag so that it |
455 | * is more likely to be cleaned up by the dcache shrinker in case of | ||
456 | * memory pressure. | ||
457 | */ | ||
458 | if (!d_need_lookup(dentry)) | ||
459 | dentry->d_flags |= DCACHE_REFERENCED; | ||
437 | dentry_lru_add(dentry); | 460 | dentry_lru_add(dentry); |
438 | 461 | ||
439 | dentry->d_count--; | 462 | dentry->d_count--; |
@@ -526,10 +549,6 @@ repeat: | |||
526 | */ | 549 | */ |
527 | rcu_read_lock(); | 550 | rcu_read_lock(); |
528 | ret = dentry->d_parent; | 551 | ret = dentry->d_parent; |
529 | if (!ret) { | ||
530 | rcu_read_unlock(); | ||
531 | goto out; | ||
532 | } | ||
533 | spin_lock(&ret->d_lock); | 552 | spin_lock(&ret->d_lock); |
534 | if (unlikely(ret != dentry->d_parent)) { | 553 | if (unlikely(ret != dentry->d_parent)) { |
535 | spin_unlock(&ret->d_lock); | 554 | spin_unlock(&ret->d_lock); |
@@ -540,7 +559,6 @@ repeat: | |||
540 | BUG_ON(!ret->d_count); | 559 | BUG_ON(!ret->d_count); |
541 | ret->d_count++; | 560 | ret->d_count++; |
542 | spin_unlock(&ret->d_lock); | 561 | spin_unlock(&ret->d_lock); |
543 | out: | ||
544 | return ret; | 562 | return ret; |
545 | } | 563 | } |
546 | EXPORT_SYMBOL(dget_parent); | 564 | EXPORT_SYMBOL(dget_parent); |
@@ -720,13 +738,11 @@ static void shrink_dentry_list(struct list_head *list) | |||
720 | * | 738 | * |
721 | * If flags contains DCACHE_REFERENCED reference dentries will not be pruned. | 739 | * If flags contains DCACHE_REFERENCED reference dentries will not be pruned. |
722 | */ | 740 | */ |
723 | static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags) | 741 | static void __shrink_dcache_sb(struct super_block *sb, int count, int flags) |
724 | { | 742 | { |
725 | /* called from prune_dcache() and shrink_dcache_parent() */ | ||
726 | struct dentry *dentry; | 743 | struct dentry *dentry; |
727 | LIST_HEAD(referenced); | 744 | LIST_HEAD(referenced); |
728 | LIST_HEAD(tmp); | 745 | LIST_HEAD(tmp); |
729 | int cnt = *count; | ||
730 | 746 | ||
731 | relock: | 747 | relock: |
732 | spin_lock(&dcache_lru_lock); | 748 | spin_lock(&dcache_lru_lock); |
@@ -754,7 +770,7 @@ relock: | |||
754 | } else { | 770 | } else { |
755 | list_move_tail(&dentry->d_lru, &tmp); | 771 | list_move_tail(&dentry->d_lru, &tmp); |
756 | spin_unlock(&dentry->d_lock); | 772 | spin_unlock(&dentry->d_lock); |
757 | if (!--cnt) | 773 | if (!--count) |
758 | break; | 774 | break; |
759 | } | 775 | } |
760 | cond_resched_lock(&dcache_lru_lock); | 776 | cond_resched_lock(&dcache_lru_lock); |
@@ -764,83 +780,22 @@ relock: | |||
764 | spin_unlock(&dcache_lru_lock); | 780 | spin_unlock(&dcache_lru_lock); |
765 | 781 | ||
766 | shrink_dentry_list(&tmp); | 782 | shrink_dentry_list(&tmp); |
767 | |||
768 | *count = cnt; | ||
769 | } | 783 | } |
770 | 784 | ||
771 | /** | 785 | /** |
772 | * prune_dcache - shrink the dcache | 786 | * prune_dcache_sb - shrink the dcache |
773 | * @count: number of entries to try to free | 787 | * @nr_to_scan: number of entries to try to free |
774 | * | 788 | * |
775 | * Shrink the dcache. This is done when we need more memory, or simply when we | 789 | * Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is |
776 | * need to unmount something (at which point we need to unuse all dentries). | 790 | * done when we need more memory an called from the superblock shrinker |
791 | * function. | ||
777 | * | 792 | * |
778 | * This function may fail to free any resources if all the dentries are in use. | 793 | * This function may fail to free any resources if all the dentries are in |
794 | * use. | ||
779 | */ | 795 | */ |
780 | static void prune_dcache(int count) | 796 | void prune_dcache_sb(struct super_block *sb, int nr_to_scan) |
781 | { | 797 | { |
782 | struct super_block *sb, *p = NULL; | 798 | __shrink_dcache_sb(sb, nr_to_scan, DCACHE_REFERENCED); |
783 | int w_count; | ||
784 | int unused = dentry_stat.nr_unused; | ||
785 | int prune_ratio; | ||
786 | int pruned; | ||
787 | |||
788 | if (unused == 0 || count == 0) | ||
789 | return; | ||
790 | if (count >= unused) | ||
791 | prune_ratio = 1; | ||
792 | else | ||
793 | prune_ratio = unused / count; | ||
794 | spin_lock(&sb_lock); | ||
795 | list_for_each_entry(sb, &super_blocks, s_list) { | ||
796 | if (list_empty(&sb->s_instances)) | ||
797 | continue; | ||
798 | if (sb->s_nr_dentry_unused == 0) | ||
799 | continue; | ||
800 | sb->s_count++; | ||
801 | /* Now, we reclaim unused dentrins with fairness. | ||
802 | * We reclaim them same percentage from each superblock. | ||
803 | * We calculate number of dentries to scan on this sb | ||
804 | * as follows, but the implementation is arranged to avoid | ||
805 | * overflows: | ||
806 | * number of dentries to scan on this sb = | ||
807 | * count * (number of dentries on this sb / | ||
808 | * number of dentries in the machine) | ||
809 | */ | ||
810 | spin_unlock(&sb_lock); | ||
811 | if (prune_ratio != 1) | ||
812 | w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1; | ||
813 | else | ||
814 | w_count = sb->s_nr_dentry_unused; | ||
815 | pruned = w_count; | ||
816 | /* | ||
817 | * We need to be sure this filesystem isn't being unmounted, | ||
818 | * otherwise we could race with generic_shutdown_super(), and | ||
819 | * end up holding a reference to an inode while the filesystem | ||
820 | * is unmounted. So we try to get s_umount, and make sure | ||
821 | * s_root isn't NULL. | ||
822 | */ | ||
823 | if (down_read_trylock(&sb->s_umount)) { | ||
824 | if ((sb->s_root != NULL) && | ||
825 | (!list_empty(&sb->s_dentry_lru))) { | ||
826 | __shrink_dcache_sb(sb, &w_count, | ||
827 | DCACHE_REFERENCED); | ||
828 | pruned -= w_count; | ||
829 | } | ||
830 | up_read(&sb->s_umount); | ||
831 | } | ||
832 | spin_lock(&sb_lock); | ||
833 | if (p) | ||
834 | __put_super(p); | ||
835 | count -= pruned; | ||
836 | p = sb; | ||
837 | /* more work left to do? */ | ||
838 | if (count <= 0) | ||
839 | break; | ||
840 | } | ||
841 | if (p) | ||
842 | __put_super(p); | ||
843 | spin_unlock(&sb_lock); | ||
844 | } | 799 | } |
845 | 800 | ||
846 | /** | 801 | /** |
@@ -1215,45 +1170,13 @@ void shrink_dcache_parent(struct dentry * parent) | |||
1215 | int found; | 1170 | int found; |
1216 | 1171 | ||
1217 | while ((found = select_parent(parent)) != 0) | 1172 | while ((found = select_parent(parent)) != 0) |
1218 | __shrink_dcache_sb(sb, &found, 0); | 1173 | __shrink_dcache_sb(sb, found, 0); |
1219 | } | 1174 | } |
1220 | EXPORT_SYMBOL(shrink_dcache_parent); | 1175 | EXPORT_SYMBOL(shrink_dcache_parent); |
1221 | 1176 | ||
1222 | /* | ||
1223 | * Scan `sc->nr_slab_to_reclaim' dentries and return the number which remain. | ||
1224 | * | ||
1225 | * We need to avoid reentering the filesystem if the caller is performing a | ||
1226 | * GFP_NOFS allocation attempt. One example deadlock is: | ||
1227 | * | ||
1228 | * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache-> | ||
1229 | * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode-> | ||
1230 | * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK. | ||
1231 | * | ||
1232 | * In this case we return -1 to tell the caller that we baled. | ||
1233 | */ | ||
1234 | static int shrink_dcache_memory(struct shrinker *shrink, | ||
1235 | struct shrink_control *sc) | ||
1236 | { | ||
1237 | int nr = sc->nr_to_scan; | ||
1238 | gfp_t gfp_mask = sc->gfp_mask; | ||
1239 | |||
1240 | if (nr) { | ||
1241 | if (!(gfp_mask & __GFP_FS)) | ||
1242 | return -1; | ||
1243 | prune_dcache(nr); | ||
1244 | } | ||
1245 | |||
1246 | return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; | ||
1247 | } | ||
1248 | |||
1249 | static struct shrinker dcache_shrinker = { | ||
1250 | .shrink = shrink_dcache_memory, | ||
1251 | .seeks = DEFAULT_SEEKS, | ||
1252 | }; | ||
1253 | |||
1254 | /** | 1177 | /** |
1255 | * d_alloc - allocate a dcache entry | 1178 | * __d_alloc - allocate a dcache entry |
1256 | * @parent: parent of entry to allocate | 1179 | * @sb: filesystem it will belong to |
1257 | * @name: qstr of the name | 1180 | * @name: qstr of the name |
1258 | * | 1181 | * |
1259 | * Allocates a dentry. It returns %NULL if there is insufficient memory | 1182 | * Allocates a dentry. It returns %NULL if there is insufficient memory |
@@ -1261,7 +1184,7 @@ static struct shrinker dcache_shrinker = { | |||
1261 | * copied and the copy passed in may be reused after this call. | 1184 | * copied and the copy passed in may be reused after this call. |
1262 | */ | 1185 | */ |
1263 | 1186 | ||
1264 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | 1187 | struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) |
1265 | { | 1188 | { |
1266 | struct dentry *dentry; | 1189 | struct dentry *dentry; |
1267 | char *dname; | 1190 | char *dname; |
@@ -1291,8 +1214,8 @@ struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |||
1291 | spin_lock_init(&dentry->d_lock); | 1214 | spin_lock_init(&dentry->d_lock); |
1292 | seqcount_init(&dentry->d_seq); | 1215 | seqcount_init(&dentry->d_seq); |
1293 | dentry->d_inode = NULL; | 1216 | dentry->d_inode = NULL; |
1294 | dentry->d_parent = NULL; | 1217 | dentry->d_parent = dentry; |
1295 | dentry->d_sb = NULL; | 1218 | dentry->d_sb = sb; |
1296 | dentry->d_op = NULL; | 1219 | dentry->d_op = NULL; |
1297 | dentry->d_fsdata = NULL; | 1220 | dentry->d_fsdata = NULL; |
1298 | INIT_HLIST_BL_NODE(&dentry->d_hash); | 1221 | INIT_HLIST_BL_NODE(&dentry->d_hash); |
@@ -1300,36 +1223,47 @@ struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |||
1300 | INIT_LIST_HEAD(&dentry->d_subdirs); | 1223 | INIT_LIST_HEAD(&dentry->d_subdirs); |
1301 | INIT_LIST_HEAD(&dentry->d_alias); | 1224 | INIT_LIST_HEAD(&dentry->d_alias); |
1302 | INIT_LIST_HEAD(&dentry->d_u.d_child); | 1225 | INIT_LIST_HEAD(&dentry->d_u.d_child); |
1303 | 1226 | d_set_d_op(dentry, dentry->d_sb->s_d_op); | |
1304 | if (parent) { | ||
1305 | spin_lock(&parent->d_lock); | ||
1306 | /* | ||
1307 | * don't need child lock because it is not subject | ||
1308 | * to concurrency here | ||
1309 | */ | ||
1310 | __dget_dlock(parent); | ||
1311 | dentry->d_parent = parent; | ||
1312 | dentry->d_sb = parent->d_sb; | ||
1313 | d_set_d_op(dentry, dentry->d_sb->s_d_op); | ||
1314 | list_add(&dentry->d_u.d_child, &parent->d_subdirs); | ||
1315 | spin_unlock(&parent->d_lock); | ||
1316 | } | ||
1317 | 1227 | ||
1318 | this_cpu_inc(nr_dentry); | 1228 | this_cpu_inc(nr_dentry); |
1319 | 1229 | ||
1320 | return dentry; | 1230 | return dentry; |
1321 | } | 1231 | } |
1232 | |||
1233 | /** | ||
1234 | * d_alloc - allocate a dcache entry | ||
1235 | * @parent: parent of entry to allocate | ||
1236 | * @name: qstr of the name | ||
1237 | * | ||
1238 | * Allocates a dentry. It returns %NULL if there is insufficient memory | ||
1239 | * available. On a success the dentry is returned. The name passed in is | ||
1240 | * copied and the copy passed in may be reused after this call. | ||
1241 | */ | ||
1242 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | ||
1243 | { | ||
1244 | struct dentry *dentry = __d_alloc(parent->d_sb, name); | ||
1245 | if (!dentry) | ||
1246 | return NULL; | ||
1247 | |||
1248 | spin_lock(&parent->d_lock); | ||
1249 | /* | ||
1250 | * don't need child lock because it is not subject | ||
1251 | * to concurrency here | ||
1252 | */ | ||
1253 | __dget_dlock(parent); | ||
1254 | dentry->d_parent = parent; | ||
1255 | list_add(&dentry->d_u.d_child, &parent->d_subdirs); | ||
1256 | spin_unlock(&parent->d_lock); | ||
1257 | |||
1258 | return dentry; | ||
1259 | } | ||
1322 | EXPORT_SYMBOL(d_alloc); | 1260 | EXPORT_SYMBOL(d_alloc); |
1323 | 1261 | ||
1324 | struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name) | 1262 | struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name) |
1325 | { | 1263 | { |
1326 | struct dentry *dentry = d_alloc(NULL, name); | 1264 | struct dentry *dentry = __d_alloc(sb, name); |
1327 | if (dentry) { | 1265 | if (dentry) |
1328 | dentry->d_sb = sb; | ||
1329 | d_set_d_op(dentry, dentry->d_sb->s_d_op); | ||
1330 | dentry->d_parent = dentry; | ||
1331 | dentry->d_flags |= DCACHE_DISCONNECTED; | 1266 | dentry->d_flags |= DCACHE_DISCONNECTED; |
1332 | } | ||
1333 | return dentry; | 1267 | return dentry; |
1334 | } | 1268 | } |
1335 | EXPORT_SYMBOL(d_alloc_pseudo); | 1269 | EXPORT_SYMBOL(d_alloc_pseudo); |
@@ -1499,13 +1433,9 @@ struct dentry * d_alloc_root(struct inode * root_inode) | |||
1499 | if (root_inode) { | 1433 | if (root_inode) { |
1500 | static const struct qstr name = { .name = "/", .len = 1 }; | 1434 | static const struct qstr name = { .name = "/", .len = 1 }; |
1501 | 1435 | ||
1502 | res = d_alloc(NULL, &name); | 1436 | res = __d_alloc(root_inode->i_sb, &name); |
1503 | if (res) { | 1437 | if (res) |
1504 | res->d_sb = root_inode->i_sb; | ||
1505 | d_set_d_op(res, res->d_sb->s_d_op); | ||
1506 | res->d_parent = res; | ||
1507 | d_instantiate(res, root_inode); | 1438 | d_instantiate(res, root_inode); |
1508 | } | ||
1509 | } | 1439 | } |
1510 | return res; | 1440 | return res; |
1511 | } | 1441 | } |
@@ -1566,13 +1496,11 @@ struct dentry *d_obtain_alias(struct inode *inode) | |||
1566 | if (res) | 1496 | if (res) |
1567 | goto out_iput; | 1497 | goto out_iput; |
1568 | 1498 | ||
1569 | tmp = d_alloc(NULL, &anonstring); | 1499 | tmp = __d_alloc(inode->i_sb, &anonstring); |
1570 | if (!tmp) { | 1500 | if (!tmp) { |
1571 | res = ERR_PTR(-ENOMEM); | 1501 | res = ERR_PTR(-ENOMEM); |
1572 | goto out_iput; | 1502 | goto out_iput; |
1573 | } | 1503 | } |
1574 | tmp->d_parent = tmp; /* make sure dput doesn't croak */ | ||
1575 | |||
1576 | 1504 | ||
1577 | spin_lock(&inode->i_lock); | 1505 | spin_lock(&inode->i_lock); |
1578 | res = __d_find_any_alias(inode); | 1506 | res = __d_find_any_alias(inode); |
@@ -1584,8 +1512,6 @@ struct dentry *d_obtain_alias(struct inode *inode) | |||
1584 | 1512 | ||
1585 | /* attach a disconnected dentry */ | 1513 | /* attach a disconnected dentry */ |
1586 | spin_lock(&tmp->d_lock); | 1514 | spin_lock(&tmp->d_lock); |
1587 | tmp->d_sb = inode->i_sb; | ||
1588 | d_set_d_op(tmp, tmp->d_sb->s_d_op); | ||
1589 | tmp->d_inode = inode; | 1515 | tmp->d_inode = inode; |
1590 | tmp->d_flags |= DCACHE_DISCONNECTED; | 1516 | tmp->d_flags |= DCACHE_DISCONNECTED; |
1591 | list_add(&tmp->d_alias, &inode->i_dentry); | 1517 | list_add(&tmp->d_alias, &inode->i_dentry); |
@@ -1626,6 +1552,9 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) | |||
1626 | { | 1552 | { |
1627 | struct dentry *new = NULL; | 1553 | struct dentry *new = NULL; |
1628 | 1554 | ||
1555 | if (IS_ERR(inode)) | ||
1556 | return ERR_CAST(inode); | ||
1557 | |||
1629 | if (inode && S_ISDIR(inode->i_mode)) { | 1558 | if (inode && S_ISDIR(inode->i_mode)) { |
1630 | spin_lock(&inode->i_lock); | 1559 | spin_lock(&inode->i_lock); |
1631 | new = __d_find_alias(inode, 1); | 1560 | new = __d_find_alias(inode, 1); |
@@ -1708,29 +1637,22 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, | |||
1708 | } | 1637 | } |
1709 | 1638 | ||
1710 | /* | 1639 | /* |
1711 | * Negative dentry: instantiate it unless the inode is a directory and | 1640 | * We are going to instantiate this dentry, unhash it and clear the |
1712 | * already has a dentry. | 1641 | * lookup flag so we can do that. |
1713 | */ | 1642 | */ |
1714 | spin_lock(&inode->i_lock); | 1643 | if (unlikely(d_need_lookup(found))) |
1715 | if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) { | 1644 | d_clear_need_lookup(found); |
1716 | __d_instantiate(found, inode); | ||
1717 | spin_unlock(&inode->i_lock); | ||
1718 | security_d_instantiate(found, inode); | ||
1719 | return found; | ||
1720 | } | ||
1721 | 1645 | ||
1722 | /* | 1646 | /* |
1723 | * In case a directory already has a (disconnected) entry grab a | 1647 | * Negative dentry: instantiate it unless the inode is a directory and |
1724 | * reference to it, move it in place and use it. | 1648 | * already has a dentry. |
1725 | */ | 1649 | */ |
1726 | new = list_entry(inode->i_dentry.next, struct dentry, d_alias); | 1650 | new = d_splice_alias(inode, found); |
1727 | __dget(new); | 1651 | if (new) { |
1728 | spin_unlock(&inode->i_lock); | 1652 | dput(found); |
1729 | security_d_instantiate(found, inode); | 1653 | found = new; |
1730 | d_move(new, found); | 1654 | } |
1731 | iput(inode); | 1655 | return found; |
1732 | dput(found); | ||
1733 | return new; | ||
1734 | 1656 | ||
1735 | err_out: | 1657 | err_out: |
1736 | iput(inode); | 1658 | iput(inode); |
@@ -3045,8 +2967,6 @@ static void __init dcache_init(void) | |||
3045 | */ | 2967 | */ |
3046 | dentry_cache = KMEM_CACHE(dentry, | 2968 | dentry_cache = KMEM_CACHE(dentry, |
3047 | SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD); | 2969 | SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD); |
3048 | |||
3049 | register_shrinker(&dcache_shrinker); | ||
3050 | 2970 | ||
3051 | /* Hash may have been set up in dcache_init_early */ | 2971 | /* Hash may have been set up in dcache_init_early */ |
3052 | if (!hashdist) | 2972 | if (!hashdist) |