5 #include <linux/export.h>
9 #include <linux/hash.h>
17 #include <linux/prefetch.h>
78 static int get_nr_inodes(
void)
87 static
inline int get_nr_inodes_unused(
void)
99 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
100 return nr_dirty > 0 ? nr_dirty : 0;
108 void __user *
buffer,
size_t *lenp, loff_t *ppos)
134 inode->
i_op = &empty_iops;
135 inode->
i_fop = &empty_fops;
138 i_uid_write(inode, 0);
139 i_gid_write(inode, 0);
146 memset(&inode->i_dquot, 0,
sizeof(inode->i_dquot));
180 bdi = sb->
s_bdev->bd_inode->i_mapping->backing_dev_info;
186 #ifdef CONFIG_FS_POSIX_ACL
190 #ifdef CONFIG_FSNOTIFY
191 inode->i_fsnotify_mask = 0;
206 if (sb->
s_op->alloc_inode)
207 inode = sb->
s_op->alloc_inode(sb);
215 if (inode->
i_sb->s_op->destroy_inode)
216 inode->
i_sb->s_op->destroy_inode(inode);
235 fsnotify_inode_delete(inode);
237 WARN_ON(atomic_long_read(&inode->
i_sb->s_remove_count) == 0);
238 atomic_long_dec(&inode->
i_sb->s_remove_count);
241 #ifdef CONFIG_FS_POSIX_ACL
243 posix_acl_release(inode->i_acl);
244 if (inode->i_default_acl && inode->i_default_acl !=
ACL_NOT_CACHED)
245 posix_acl_release(inode->i_default_acl);
257 static void destroy_inode(
struct inode *inode)
261 if (inode->
i_sb->s_op->destroy_inode)
262 inode->
i_sb->s_op->destroy_inode(inode);
283 atomic_long_inc(&inode->
i_sb->s_remove_count);
299 atomic_long_inc(&inode->
i_sb->s_remove_count);
319 atomic_long_dec(&inode->
i_sb->s_remove_count);
337 atomic_long_dec(&inode->
i_sb->s_remove_count);
345 memset(mapping, 0,
sizeof(*mapping));
363 memset(inode, 0,
sizeof(*inode));
364 INIT_HLIST_NODE(&inode->
i_hash);
367 INIT_LIST_HEAD(&inode->
i_lru);
370 #ifdef CONFIG_FSNOTIFY
376 static void init_once(
void *
foo)
378 struct inode *inode = (
struct inode *) foo;
400 static void inode_lru_list_add(
struct inode *inode)
402 spin_lock(&inode->
i_sb->s_inode_lru_lock);
403 if (list_empty(&inode->
i_lru)) {
404 list_add(&inode->
i_lru, &inode->
i_sb->s_inode_lru);
405 inode->
i_sb->s_nr_inodes_unused++;
408 spin_unlock(&inode->
i_sb->s_inode_lru_lock);
420 inode_lru_list_add(inode);
424 static void inode_lru_list_del(
struct inode *inode)
426 spin_lock(&inode->
i_sb->s_inode_lru_lock);
427 if (!list_empty(&inode->
i_lru)) {
428 list_del_init(&inode->
i_lru);
429 inode->
i_sb->s_nr_inodes_unused--;
432 spin_unlock(&inode->
i_sb->s_inode_lru_lock);
447 static inline void inode_sb_list_del(
struct inode *inode)
450 spin_lock(&inode_sb_list_lock);
452 spin_unlock(&inode_sb_list_lock);
456 static unsigned long hash(
struct super_block *sb,
unsigned long hashval)
460 tmp = (hashval * (
unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
462 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
463 return tmp & i_hash_mask;
478 spin_lock(&inode_hash_lock);
479 spin_lock(&inode->
i_lock);
480 hlist_add_head(&inode->
i_hash, b);
481 spin_unlock(&inode->
i_lock);
482 spin_unlock(&inode_hash_lock);
494 spin_lock(&inode_hash_lock);
495 spin_lock(&inode->
i_lock);
496 hlist_del_init(&inode->
i_hash);
497 spin_unlock(&inode->
i_lock);
498 spin_unlock(&inode_hash_lock);
510 spin_lock_irq(&inode->
i_data.tree_lock);
512 spin_unlock_irq(&inode->
i_data.tree_lock);
534 static void evict(
struct inode *inode)
544 inode_sb_list_del(inode);
557 if (inode->
i_data.nrpages)
566 remove_inode_hash(inode);
568 spin_lock(&inode->
i_lock);
571 spin_unlock(&inode->
i_lock);
585 while (!list_empty(head)) {
589 list_del_init(&inode->
i_lru);
614 spin_lock(&inode->
i_lock);
616 spin_unlock(&inode->
i_lock);
621 inode_lru_list_del(inode);
622 spin_unlock(&inode->
i_lock);
623 list_add(&inode->
i_lru, &dispose);
627 dispose_list(&dispose);
648 spin_lock(&inode->
i_lock);
650 spin_unlock(&inode->
i_lock);
654 spin_unlock(&inode->
i_lock);
659 spin_unlock(&inode->
i_lock);
665 inode_lru_list_del(inode);
666 spin_unlock(&inode->
i_lock);
667 list_add(&inode->
i_lru, &dispose);
671 dispose_list(&dispose);
676 static int can_unuse(
struct inode *inode)
684 if (inode->
i_data.nrpages)
711 unsigned long reap = 0;
713 spin_lock(&sb->s_inode_lru_lock);
714 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
727 if (!spin_trylock(&inode->
i_lock)) {
738 list_del_init(&inode->
i_lru);
739 spin_unlock(&inode->
i_lock);
749 spin_unlock(&inode->
i_lock);
754 spin_unlock(&inode->
i_lock);
755 spin_unlock(&sb->s_inode_lru_lock);
760 spin_lock(&sb->s_inode_lru_lock);
763 struct inode,
i_lru))
766 if (!spin_trylock(&inode->
i_lock))
768 if (!can_unuse(inode)) {
769 spin_unlock(&inode->
i_lock);
775 spin_unlock(&inode->
i_lock);
777 list_move(&inode->
i_lru, &freeable);
781 if (current_is_kswapd())
785 spin_unlock(&sb->s_inode_lru_lock);
787 current->reclaim_state->reclaimed_slab += reap;
789 dispose_list(&freeable);
792 static void __wait_on_freeing_inode(
struct inode *inode);
796 static struct inode *find_inode(
struct super_block *sb,
798 int (*
test)(
struct inode *,
void *),
802 struct inode *inode =
NULL;
806 spin_lock(&inode->
i_lock);
807 if (inode->
i_sb != sb) {
808 spin_unlock(&inode->
i_lock);
811 if (!
test(inode, data)) {
812 spin_unlock(&inode->
i_lock);
816 __wait_on_freeing_inode(inode);
820 spin_unlock(&inode->
i_lock);
830 static struct inode *find_inode_fast(
struct super_block *sb,
834 struct inode *inode =
NULL;
838 spin_lock(&inode->
i_lock);
839 if (inode->
i_ino != ino) {
840 spin_unlock(&inode->
i_lock);
843 if (inode->
i_sb != sb) {
844 spin_unlock(&inode->
i_lock);
848 __wait_on_freeing_inode(inode);
852 spin_unlock(&inode->
i_lock);
873 #define LAST_INO_BATCH 1024
879 unsigned int res = *
p;
908 struct inode *inode = alloc_inode(sb);
911 spin_lock(&inode->
i_lock);
913 spin_unlock(&inode->
i_lock);
944 #ifdef CONFIG_DEBUG_LOCK_ALLOC
945 void lockdep_annotate_inode_mutex_key(
struct inode *inode)
974 lockdep_annotate_inode_mutex_key(inode);
975 spin_lock(&inode->
i_lock);
980 spin_unlock(&inode->
i_lock);
1005 int (*
test)(
struct inode *,
void *),
1006 int (*
set)(
struct inode *,
void *),
void *data)
1009 struct inode *
inode;
1011 spin_lock(&inode_hash_lock);
1012 inode = find_inode(sb, head,
test, data);
1013 spin_unlock(&inode_hash_lock);
1016 wait_on_inode(inode);
1020 inode = alloc_inode(sb);
1024 spin_lock(&inode_hash_lock);
1026 old = find_inode(sb, head,
test, data);
1028 if (
set(inode, data))
1031 spin_lock(&inode->
i_lock);
1033 hlist_add_head(&inode->
i_hash, head);
1034 spin_unlock(&inode->
i_lock);
1036 spin_unlock(&inode_hash_lock);
1049 spin_unlock(&inode_hash_lock);
1050 destroy_inode(inode);
1052 wait_on_inode(inode);
1057 spin_unlock(&inode_hash_lock);
1058 destroy_inode(inode);
1079 struct inode *
inode;
1081 spin_lock(&inode_hash_lock);
1082 inode = find_inode_fast(sb, head, ino);
1083 spin_unlock(&inode_hash_lock);
1085 wait_on_inode(inode);
1089 inode = alloc_inode(sb);
1093 spin_lock(&inode_hash_lock);
1095 old = find_inode_fast(sb, head, ino);
1098 spin_lock(&inode->
i_lock);
1100 hlist_add_head(&inode->
i_hash, head);
1101 spin_unlock(&inode->
i_lock);
1103 spin_unlock(&inode_hash_lock);
1116 spin_unlock(&inode_hash_lock);
1117 destroy_inode(inode);
1119 wait_on_inode(inode);
1132 static int test_inode_iunique(
struct super_block *sb,
unsigned long ino)
1136 struct inode *
inode;
1138 spin_lock(&inode_hash_lock);
1140 if (inode->
i_ino == ino && inode->
i_sb == sb) {
1141 spin_unlock(&inode_hash_lock);
1145 spin_unlock(&inode_hash_lock);
1175 spin_lock(&iunique_lock);
1177 if (counter <= max_reserved)
1178 counter = max_reserved + 1;
1180 }
while (!test_inode_iunique(sb, res));
1181 spin_unlock(&iunique_lock);
1187 struct inode *
igrab(
struct inode *inode)
1189 spin_lock(&inode->
i_lock);
1192 spin_unlock(&inode->
i_lock);
1194 spin_unlock(&inode->
i_lock);
1223 int (*
test)(
struct inode *,
void *),
void *data)
1226 struct inode *
inode;
1228 spin_lock(&inode_hash_lock);
1229 inode = find_inode(sb, head,
test, data);
1230 spin_unlock(&inode_hash_lock);
1254 int (*
test)(
struct inode *,
void *),
void *data)
1259 wait_on_inode(inode);
1275 struct inode *
inode;
1277 spin_lock(&inode_hash_lock);
1278 inode = find_inode_fast(sb, head, ino);
1279 spin_unlock(&inode_hash_lock);
1282 wait_on_inode(inode);
1295 struct inode *old =
NULL;
1296 spin_lock(&inode_hash_lock);
1298 if (old->
i_ino != ino)
1300 if (old->
i_sb != sb)
1304 spin_unlock(&old->
i_lock);
1310 spin_lock(&inode->
i_lock);
1312 hlist_add_head(&inode->
i_hash, head);
1313 spin_unlock(&inode->
i_lock);
1314 spin_unlock(&inode_hash_lock);
1318 spin_unlock(&old->
i_lock);
1319 spin_unlock(&inode_hash_lock);
1321 if (
unlikely(!inode_unhashed(old))) {
1331 int (*
test)(
struct inode *,
void *),
void *data)
1338 struct inode *old =
NULL;
1340 spin_lock(&inode_hash_lock);
1342 if (old->
i_sb != sb)
1344 if (!
test(old, data))
1348 spin_unlock(&old->
i_lock);
1354 spin_lock(&inode->
i_lock);
1356 hlist_add_head(&inode->
i_hash, head);
1357 spin_unlock(&inode->
i_lock);
1358 spin_unlock(&inode_hash_lock);
1362 spin_unlock(&old->
i_lock);
1363 spin_unlock(&inode_hash_lock);
1365 if (
unlikely(!inode_unhashed(old))) {
1391 static void iput_final(
struct inode *inode)
1402 drop = generic_drop_inode(inode);
1407 spin_unlock(&inode->
i_lock);
1413 spin_unlock(&inode->
i_lock);
1415 spin_lock(&inode->
i_lock);
1421 if (!list_empty(&inode->
i_lru))
1422 inode_lru_list_del(inode);
1423 spin_unlock(&inode->
i_lock);
1473 static int relatime_need_update(
struct vfsmount *mnt,
struct inode *inode,
1508 if (inode->
i_op->update_time)
1509 return inode->
i_op->update_time(inode, time, flags);
1514 inode_inc_iversion(inode);
1519 mark_inode_dirty_sync(inode);
1534 struct inode *inode = path->
dentry->d_inode;
1551 if (!relatime_need_update(mnt, inode, now))
1554 if (timespec_equal(&inode->
i_atime, &now))
1557 if (!sb_start_write_trylock(inode->
i_sb))
1571 update_time(inode, &now, S_ATIME);
1574 sb_end_write(inode->
i_sb);
1607 static int __remove_suid(
struct dentry *
dentry,
int kill)
1609 struct iattr newattrs;
1618 struct inode *inode = dentry->
d_inode;
1634 if (!error && killsuid)
1635 error = __remove_suid(dentry, killsuid);
1658 struct inode *inode = file->
f_path.dentry->d_inode;
1668 if (!timespec_equal(&inode->
i_mtime, &now))
1671 if (!timespec_equal(&inode->
i_ctime, &now))
1684 ret = update_time(inode, &now, sync_it);
1719 static void __wait_on_freeing_inode(
struct inode *inode)
1725 spin_unlock(&inode->
i_lock);
1726 spin_unlock(&inode_hash_lock);
1729 spin_lock(&inode_hash_lock);
1732 static __initdata unsigned long ihash_entries;
1733 static int __init set_ihash_entries(
char *
str)
1740 __setup(
"ihash_entries=", set_ihash_entries);
1766 for (loop = 0; loop < (1
U << i_hash_shift); loop++)
1776 sizeof(
struct inode),
1797 for (loop = 0; loop < (1
U << i_hash_shift); loop++)
1816 " inode %s:%lu\n", mode, inode->
i_sb->s_id,
1861 static void __inode_dio_wait(
struct inode *inode)
1887 __inode_dio_wait(inode);