]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
1da177e4 | 3 | * (C) 1997 Linus Torvalds |
4b4563dc | 4 | * (C) 1999 Andrea Arcangeli <[email protected]> (dynamic inode allocation) |
1da177e4 | 5 | */ |
e59cc473 | 6 | #include <linux/export.h> |
1da177e4 | 7 | #include <linux/fs.h> |
5970e15d | 8 | #include <linux/filelock.h> |
1da177e4 | 9 | #include <linux/mm.h> |
1da177e4 | 10 | #include <linux/backing-dev.h> |
1da177e4 LT |
11 | #include <linux/hash.h> |
12 | #include <linux/swap.h> | |
13 | #include <linux/security.h> | |
1da177e4 | 14 | #include <linux/cdev.h> |
57c8a661 | 15 | #include <linux/memblock.h> |
3be25f49 | 16 | #include <linux/fsnotify.h> |
fc33a7bb | 17 | #include <linux/mount.h> |
f19d4a8f | 18 | #include <linux/posix_acl.h> |
4b4563dc | 19 | #include <linux/buffer_head.h> /* for inode_has_buffers */ |
7ada4db8 | 20 | #include <linux/ratelimit.h> |
bc3b14cb | 21 | #include <linux/list_lru.h> |
ae5e165d | 22 | #include <linux/iversion.h> |
fe3944fb | 23 | #include <linux/rw_hint.h> |
0ae45f63 | 24 | #include <trace/events/writeback.h> |
a66979ab | 25 | #include "internal.h" |
1da177e4 | 26 | |
250df6ed | 27 | /* |
4b4563dc | 28 | * Inode locking rules: |
250df6ed DC |
29 | * |
30 | * inode->i_lock protects: | |
10e14073 | 31 | * inode->i_state, inode->i_hash, __iget(), inode->i_io_list |
bc3b14cb | 32 | * Inode LRU list locks protect: |
98b745c6 | 33 | * inode->i_sb->s_inode_lru, inode->i_lru |
74278da9 DC |
34 | * inode->i_sb->s_inode_list_lock protects: |
35 | * inode->i_sb->s_inodes, inode->i_sb_list | |
f758eeab | 36 | * bdi->wb.list_lock protects: |
c7f54084 | 37 | * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list |
67a23c49 DC |
38 | * inode_hash_lock protects: |
39 | * inode_hashtable, inode->i_hash | |
250df6ed DC |
40 | * |
41 | * Lock ordering: | |
55fa6091 | 42 | * |
74278da9 | 43 | * inode->i_sb->s_inode_list_lock |
55fa6091 | 44 | * inode->i_lock |
bc3b14cb | 45 | * Inode LRU list locks |
a66979ab | 46 | * |
f758eeab | 47 | * bdi->wb.list_lock |
a66979ab | 48 | * inode->i_lock |
67a23c49 DC |
49 | * |
50 | * inode_hash_lock | |
74278da9 | 51 | * inode->i_sb->s_inode_list_lock |
67a23c49 DC |
52 | * inode->i_lock |
53 | * | |
54 | * iunique_lock | |
55 | * inode_hash_lock | |
250df6ed DC |
56 | */ |
57 | ||
68279f9c AD |
58 | static unsigned int i_hash_mask __ro_after_init; |
59 | static unsigned int i_hash_shift __ro_after_init; | |
60 | static struct hlist_head *inode_hashtable __ro_after_init; | |
67a23c49 | 61 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock); |
1da177e4 | 62 | |
7dcda1c9 JA |
63 | /* |
64 | * Empty aops. Can be used for the cases where the user does not | |
65 | * define any of the address_space operations. | |
66 | */ | |
67 | const struct address_space_operations empty_aops = { | |
68 | }; | |
69 | EXPORT_SYMBOL(empty_aops); | |
70 | ||
3942c07c GC |
71 | static DEFINE_PER_CPU(unsigned long, nr_inodes); |
72 | static DEFINE_PER_CPU(unsigned long, nr_unused); | |
cffbc8aa | 73 | |
68279f9c | 74 | static struct kmem_cache *inode_cachep __ro_after_init; |
1da177e4 | 75 | |
3942c07c | 76 | static long get_nr_inodes(void) |
cffbc8aa | 77 | { |
3e880fb5 | 78 | int i; |
3942c07c | 79 | long sum = 0; |
3e880fb5 NP |
80 | for_each_possible_cpu(i) |
81 | sum += per_cpu(nr_inodes, i); | |
82 | return sum < 0 ? 0 : sum; | |
cffbc8aa DC |
83 | } |
84 | ||
3942c07c | 85 | static inline long get_nr_inodes_unused(void) |
cffbc8aa | 86 | { |
fcb94f72 | 87 | int i; |
3942c07c | 88 | long sum = 0; |
fcb94f72 DC |
89 | for_each_possible_cpu(i) |
90 | sum += per_cpu(nr_unused, i); | |
91 | return sum < 0 ? 0 : sum; | |
cffbc8aa DC |
92 | } |
93 | ||
3942c07c | 94 | long get_nr_dirty_inodes(void) |
cffbc8aa | 95 | { |
3e880fb5 | 96 | /* not actually dirty inodes, but a wild approximation */ |
3942c07c | 97 | long nr_dirty = get_nr_inodes() - get_nr_inodes_unused(); |
cffbc8aa | 98 | return nr_dirty > 0 ? nr_dirty : 0; |
cffbc8aa DC |
99 | } |
100 | ||
101 | /* | |
102 | * Handle nr_inode sysctl | |
103 | */ | |
104 | #ifdef CONFIG_SYSCTL | |
1d67fe58 LC |
105 | /* |
106 | * Statistics gathering.. | |
107 | */ | |
108 | static struct inodes_stat_t inodes_stat; | |
109 | ||
78eb4ea2 | 110 | static int proc_nr_inodes(const struct ctl_table *table, int write, void *buffer, |
1d67fe58 | 111 | size_t *lenp, loff_t *ppos) |
cffbc8aa DC |
112 | { |
113 | inodes_stat.nr_inodes = get_nr_inodes(); | |
fcb94f72 | 114 | inodes_stat.nr_unused = get_nr_inodes_unused(); |
3942c07c | 115 | return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); |
cffbc8aa | 116 | } |
1d67fe58 LC |
117 | |
118 | static struct ctl_table inodes_sysctls[] = { | |
119 | { | |
120 | .procname = "inode-nr", | |
121 | .data = &inodes_stat, | |
122 | .maxlen = 2*sizeof(long), | |
123 | .mode = 0444, | |
124 | .proc_handler = proc_nr_inodes, | |
125 | }, | |
126 | { | |
127 | .procname = "inode-state", | |
128 | .data = &inodes_stat, | |
129 | .maxlen = 7*sizeof(long), | |
130 | .mode = 0444, | |
131 | .proc_handler = proc_nr_inodes, | |
132 | }, | |
1d67fe58 LC |
133 | }; |
134 | ||
135 | static int __init init_fs_inode_sysctls(void) | |
136 | { | |
137 | register_sysctl_init("fs", inodes_sysctls); | |
138 | return 0; | |
139 | } | |
140 | early_initcall(init_fs_inode_sysctls); | |
cffbc8aa DC |
141 | #endif |
142 | ||
bd9b51e7 AV |
143 | static int no_open(struct inode *inode, struct file *file) |
144 | { | |
145 | return -ENXIO; | |
146 | } | |
147 | ||
2cb1599f | 148 | /** |
6e7c2b4d | 149 | * inode_init_always - perform inode structure initialisation |
0bc02f3f RD |
150 | * @sb: superblock inode belongs to |
151 | * @inode: inode to initialise | |
2cb1599f DC |
152 | * |
153 | * These are initializations that need to be done on every inode | |
154 | * allocation as the fields are not initialised by slab allocation. | |
155 | */ | |
54e34621 | 156 | int inode_init_always(struct super_block *sb, struct inode *inode) |
1da177e4 | 157 | { |
6e1d5dcc | 158 | static const struct inode_operations empty_iops; |
bd9b51e7 | 159 | static const struct file_operations no_open_fops = {.open = no_open}; |
6b3304b5 | 160 | struct address_space *const mapping = &inode->i_data; |
2cb1599f DC |
161 | |
162 | inode->i_sb = sb; | |
163 | inode->i_blkbits = sb->s_blocksize_bits; | |
164 | inode->i_flags = 0; | |
5a9b911b | 165 | inode->i_state = 0; |
8019ad13 | 166 | atomic64_set(&inode->i_sequence, 0); |
2cb1599f DC |
167 | atomic_set(&inode->i_count, 1); |
168 | inode->i_op = &empty_iops; | |
bd9b51e7 | 169 | inode->i_fop = &no_open_fops; |
edbb35cc | 170 | inode->i_ino = 0; |
a78ef704 | 171 | inode->__i_nlink = 1; |
3ddcd056 | 172 | inode->i_opflags = 0; |
d0a5b995 AG |
173 | if (sb->s_xattr) |
174 | inode->i_opflags |= IOP_XATTR; | |
92361636 EB |
175 | i_uid_write(inode, 0); |
176 | i_gid_write(inode, 0); | |
2cb1599f DC |
177 | atomic_set(&inode->i_writecount, 0); |
178 | inode->i_size = 0; | |
c75b1d94 | 179 | inode->i_write_hint = WRITE_LIFE_NOT_SET; |
2cb1599f DC |
180 | inode->i_blocks = 0; |
181 | inode->i_bytes = 0; | |
182 | inode->i_generation = 0; | |
2cb1599f | 183 | inode->i_pipe = NULL; |
2cb1599f | 184 | inode->i_cdev = NULL; |
61ba64fc | 185 | inode->i_link = NULL; |
84e710da | 186 | inode->i_dir_seq = 0; |
2cb1599f DC |
187 | inode->i_rdev = 0; |
188 | inode->dirtied_when = 0; | |
6146f0d5 | 189 | |
3d65ae46 TE |
190 | #ifdef CONFIG_CGROUP_WRITEBACK |
191 | inode->i_wb_frn_winner = 0; | |
192 | inode->i_wb_frn_avg_time = 0; | |
193 | inode->i_wb_frn_history = 0; | |
194 | #endif | |
195 | ||
2cb1599f DC |
196 | spin_lock_init(&inode->i_lock); |
197 | lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); | |
198 | ||
9902af79 AV |
199 | init_rwsem(&inode->i_rwsem); |
200 | lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key); | |
2cb1599f | 201 | |
bd5fe6c5 | 202 | atomic_set(&inode->i_dio_count, 0); |
2cb1599f DC |
203 | |
204 | mapping->a_ops = &empty_aops; | |
205 | mapping->host = inode; | |
206 | mapping->flags = 0; | |
829bc787 | 207 | mapping->wb_err = 0; |
4bb5f5d9 | 208 | atomic_set(&mapping->i_mmap_writable, 0); |
09d91cda SL |
209 | #ifdef CONFIG_READ_ONLY_THP_FOR_FS |
210 | atomic_set(&mapping->nr_thps, 0); | |
211 | #endif | |
3c1d4378 | 212 | mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE); |
600f111e | 213 | mapping->i_private_data = NULL; |
2cb1599f | 214 | mapping->writeback_index = 0; |
23ca067b SAS |
215 | init_rwsem(&mapping->invalidate_lock); |
216 | lockdep_set_class_and_name(&mapping->invalidate_lock, | |
217 | &sb->s_type->invalidate_lock_key, | |
218 | "mapping.invalidate_lock"); | |
762321da CH |
219 | if (sb->s_iflags & SB_I_STABLE_WRITES) |
220 | mapping_set_stable_writes(mapping); | |
2cb1599f DC |
221 | inode->i_private = NULL; |
222 | inode->i_mapping = mapping; | |
b3d9b7a3 | 223 | INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */ |
f19d4a8f AV |
224 | #ifdef CONFIG_FS_POSIX_ACL |
225 | inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED; | |
226 | #endif | |
2cb1599f | 227 | |
3be25f49 EP |
228 | #ifdef CONFIG_FSNOTIFY |
229 | inode->i_fsnotify_mask = 0; | |
230 | #endif | |
4a075e39 | 231 | inode->i_flctx = NULL; |
2e488f13 DM |
232 | |
233 | if (unlikely(security_inode_alloc(inode))) | |
234 | return -ENOMEM; | |
5a9b911b | 235 | |
3e880fb5 | 236 | this_cpu_inc(nr_inodes); |
cffbc8aa | 237 | |
54e34621 | 238 | return 0; |
1da177e4 | 239 | } |
2cb1599f DC |
240 | EXPORT_SYMBOL(inode_init_always); |
241 | ||
fdb0da89 AV |
242 | void free_inode_nonrcu(struct inode *inode) |
243 | { | |
244 | kmem_cache_free(inode_cachep, inode); | |
245 | } | |
246 | EXPORT_SYMBOL(free_inode_nonrcu); | |
247 | ||
248 | static void i_callback(struct rcu_head *head) | |
249 | { | |
250 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
251 | if (inode->free_inode) | |
252 | inode->free_inode(inode); | |
253 | else | |
254 | free_inode_nonrcu(inode); | |
255 | } | |
256 | ||
2cb1599f DC |
257 | static struct inode *alloc_inode(struct super_block *sb) |
258 | { | |
fdb0da89 | 259 | const struct super_operations *ops = sb->s_op; |
2cb1599f DC |
260 | struct inode *inode; |
261 | ||
fdb0da89 AV |
262 | if (ops->alloc_inode) |
263 | inode = ops->alloc_inode(sb); | |
2cb1599f | 264 | else |
8b9f3ac5 | 265 | inode = alloc_inode_sb(sb, inode_cachep, GFP_KERNEL); |
2cb1599f | 266 | |
54e34621 CH |
267 | if (!inode) |
268 | return NULL; | |
269 | ||
270 | if (unlikely(inode_init_always(sb, inode))) { | |
fdb0da89 AV |
271 | if (ops->destroy_inode) { |
272 | ops->destroy_inode(inode); | |
273 | if (!ops->free_inode) | |
274 | return NULL; | |
275 | } | |
276 | inode->free_inode = ops->free_inode; | |
277 | i_callback(&inode->i_rcu); | |
54e34621 CH |
278 | return NULL; |
279 | } | |
280 | ||
281 | return inode; | |
2cb1599f | 282 | } |
1da177e4 | 283 | |
2e00c97e | 284 | void __destroy_inode(struct inode *inode) |
1da177e4 | 285 | { |
b7542f8c | 286 | BUG_ON(inode_has_buffers(inode)); |
52ebea74 | 287 | inode_detach_wb(inode); |
1da177e4 | 288 | security_inode_free(inode); |
3be25f49 | 289 | fsnotify_inode_delete(inode); |
f27a0fe0 | 290 | locks_free_lock_context(inode); |
7ada4db8 MS |
291 | if (!inode->i_nlink) { |
292 | WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0); | |
293 | atomic_long_dec(&inode->i_sb->s_remove_count); | |
294 | } | |
295 | ||
f19d4a8f | 296 | #ifdef CONFIG_FS_POSIX_ACL |
b8a7a3a6 | 297 | if (inode->i_acl && !is_uncached_acl(inode->i_acl)) |
f19d4a8f | 298 | posix_acl_release(inode->i_acl); |
b8a7a3a6 | 299 | if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl)) |
f19d4a8f AV |
300 | posix_acl_release(inode->i_default_acl); |
301 | #endif | |
3e880fb5 | 302 | this_cpu_dec(nr_inodes); |
2e00c97e CH |
303 | } |
304 | EXPORT_SYMBOL(__destroy_inode); | |
305 | ||
56b0dacf | 306 | static void destroy_inode(struct inode *inode) |
2e00c97e | 307 | { |
fdb0da89 AV |
308 | const struct super_operations *ops = inode->i_sb->s_op; |
309 | ||
7ccf19a8 | 310 | BUG_ON(!list_empty(&inode->i_lru)); |
2e00c97e | 311 | __destroy_inode(inode); |
fdb0da89 AV |
312 | if (ops->destroy_inode) { |
313 | ops->destroy_inode(inode); | |
314 | if (!ops->free_inode) | |
315 | return; | |
316 | } | |
317 | inode->free_inode = ops->free_inode; | |
318 | call_rcu(&inode->i_rcu, i_callback); | |
1da177e4 | 319 | } |
1da177e4 | 320 | |
7ada4db8 MS |
321 | /** |
322 | * drop_nlink - directly drop an inode's link count | |
323 | * @inode: inode | |
324 | * | |
325 | * This is a low-level filesystem helper to replace any | |
326 | * direct filesystem manipulation of i_nlink. In cases | |
327 | * where we are attempting to track writes to the | |
328 | * filesystem, a decrement to zero means an imminent | |
329 | * write when the file is truncated and actually unlinked | |
330 | * on the filesystem. | |
331 | */ | |
332 | void drop_nlink(struct inode *inode) | |
333 | { | |
334 | WARN_ON(inode->i_nlink == 0); | |
335 | inode->__i_nlink--; | |
336 | if (!inode->i_nlink) | |
337 | atomic_long_inc(&inode->i_sb->s_remove_count); | |
338 | } | |
339 | EXPORT_SYMBOL(drop_nlink); | |
340 | ||
341 | /** | |
342 | * clear_nlink - directly zero an inode's link count | |
343 | * @inode: inode | |
344 | * | |
345 | * This is a low-level filesystem helper to replace any | |
346 | * direct filesystem manipulation of i_nlink. See | |
347 | * drop_nlink() for why we care about i_nlink hitting zero. | |
348 | */ | |
349 | void clear_nlink(struct inode *inode) | |
350 | { | |
351 | if (inode->i_nlink) { | |
352 | inode->__i_nlink = 0; | |
353 | atomic_long_inc(&inode->i_sb->s_remove_count); | |
354 | } | |
355 | } | |
356 | EXPORT_SYMBOL(clear_nlink); | |
357 | ||
358 | /** | |
359 | * set_nlink - directly set an inode's link count | |
360 | * @inode: inode | |
361 | * @nlink: new nlink (should be non-zero) | |
362 | * | |
363 | * This is a low-level filesystem helper to replace any | |
364 | * direct filesystem manipulation of i_nlink. | |
365 | */ | |
366 | void set_nlink(struct inode *inode, unsigned int nlink) | |
367 | { | |
368 | if (!nlink) { | |
7ada4db8 MS |
369 | clear_nlink(inode); |
370 | } else { | |
371 | /* Yes, some filesystems do change nlink from zero to one */ | |
372 | if (inode->i_nlink == 0) | |
373 | atomic_long_dec(&inode->i_sb->s_remove_count); | |
374 | ||
375 | inode->__i_nlink = nlink; | |
376 | } | |
377 | } | |
378 | EXPORT_SYMBOL(set_nlink); | |
379 | ||
380 | /** | |
381 | * inc_nlink - directly increment an inode's link count | |
382 | * @inode: inode | |
383 | * | |
384 | * This is a low-level filesystem helper to replace any | |
385 | * direct filesystem manipulation of i_nlink. Currently, | |
386 | * it is only here for parity with dec_nlink(). | |
387 | */ | |
388 | void inc_nlink(struct inode *inode) | |
389 | { | |
f4e0c30c AV |
390 | if (unlikely(inode->i_nlink == 0)) { |
391 | WARN_ON(!(inode->i_state & I_LINKABLE)); | |
7ada4db8 | 392 | atomic_long_dec(&inode->i_sb->s_remove_count); |
f4e0c30c | 393 | } |
7ada4db8 MS |
394 | |
395 | inode->__i_nlink++; | |
396 | } | |
397 | EXPORT_SYMBOL(inc_nlink); | |
398 | ||
ae23395d | 399 | static void __address_space_init_once(struct address_space *mapping) |
2aa15890 | 400 | { |
7b785645 | 401 | xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT); |
c8c06efa | 402 | init_rwsem(&mapping->i_mmap_rwsem); |
600f111e MWO |
403 | INIT_LIST_HEAD(&mapping->i_private_list); |
404 | spin_lock_init(&mapping->i_private_lock); | |
f808c13f | 405 | mapping->i_mmap = RB_ROOT_CACHED; |
2aa15890 | 406 | } |
ae23395d DC |
407 | |
408 | void address_space_init_once(struct address_space *mapping) | |
409 | { | |
410 | memset(mapping, 0, sizeof(*mapping)); | |
411 | __address_space_init_once(mapping); | |
412 | } | |
2aa15890 MS |
413 | EXPORT_SYMBOL(address_space_init_once); |
414 | ||
1da177e4 LT |
415 | /* |
416 | * These are initializations that only need to be done | |
417 | * once, because the fields are idempotent across use | |
418 | * of the inode, so let the slab aware of that. | |
419 | */ | |
420 | void inode_init_once(struct inode *inode) | |
421 | { | |
422 | memset(inode, 0, sizeof(*inode)); | |
423 | INIT_HLIST_NODE(&inode->i_hash); | |
1da177e4 | 424 | INIT_LIST_HEAD(&inode->i_devices); |
c7f54084 | 425 | INIT_LIST_HEAD(&inode->i_io_list); |
6c60d2b5 | 426 | INIT_LIST_HEAD(&inode->i_wb_list); |
7ccf19a8 | 427 | INIT_LIST_HEAD(&inode->i_lru); |
18cc912b | 428 | INIT_LIST_HEAD(&inode->i_sb_list); |
ae23395d | 429 | __address_space_init_once(&inode->i_data); |
1da177e4 LT |
430 | i_size_ordered_init(inode); |
431 | } | |
1da177e4 LT |
432 | EXPORT_SYMBOL(inode_init_once); |
433 | ||
51cc5068 | 434 | static void init_once(void *foo) |
1da177e4 | 435 | { |
6b3304b5 | 436 | struct inode *inode = (struct inode *) foo; |
1da177e4 | 437 | |
a35afb83 | 438 | inode_init_once(inode); |
1da177e4 LT |
439 | } |
440 | ||
441 | /* | |
250df6ed | 442 | * inode->i_lock must be held |
1da177e4 | 443 | */ |
6b3304b5 | 444 | void __iget(struct inode *inode) |
1da177e4 | 445 | { |
9e38d86f NP |
446 | atomic_inc(&inode->i_count); |
447 | } | |
2e147f1e | 448 | |
7de9c6ee AV |
449 | /* |
450 | * get additional reference to inode; caller must already hold one. | |
451 | */ | |
452 | void ihold(struct inode *inode) | |
453 | { | |
454 | WARN_ON(atomic_inc_return(&inode->i_count) < 2); | |
455 | } | |
456 | EXPORT_SYMBOL(ihold); | |
457 | ||
51b8c1fe | 458 | static void __inode_add_lru(struct inode *inode, bool rotate) |
9e38d86f | 459 | { |
51b8c1fe JW |
460 | if (inode->i_state & (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE)) |
461 | return; | |
462 | if (atomic_read(&inode->i_count)) | |
463 | return; | |
464 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) | |
465 | return; | |
466 | if (!mapping_shrinkable(&inode->i_data)) | |
467 | return; | |
468 | ||
0a97c01c | 469 | if (list_lru_add_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) |
fcb94f72 | 470 | this_cpu_inc(nr_unused); |
51b8c1fe | 471 | else if (rotate) |
563f4001 | 472 | inode->i_state |= I_REFERENCED; |
9e38d86f | 473 | } |
2e147f1e | 474 | |
4eff96dd JK |
475 | /* |
476 | * Add inode to LRU if needed (inode is unused and clean). | |
477 | * | |
478 | * Needs inode->i_lock held. | |
479 | */ | |
480 | void inode_add_lru(struct inode *inode) | |
481 | { | |
51b8c1fe | 482 | __inode_add_lru(inode, false); |
4eff96dd JK |
483 | } |
484 | ||
9e38d86f NP |
485 | static void inode_lru_list_del(struct inode *inode) |
486 | { | |
0a97c01c | 487 | if (list_lru_del_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) |
fcb94f72 | 488 | this_cpu_dec(nr_unused); |
1da177e4 LT |
489 | } |
490 | ||
2a062983 ZC |
491 | static void inode_pin_lru_isolating(struct inode *inode) |
492 | { | |
493 | lockdep_assert_held(&inode->i_lock); | |
494 | WARN_ON(inode->i_state & (I_LRU_ISOLATING | I_FREEING | I_WILL_FREE)); | |
495 | inode->i_state |= I_LRU_ISOLATING; | |
496 | } | |
497 | ||
498 | static void inode_unpin_lru_isolating(struct inode *inode) | |
499 | { | |
500 | spin_lock(&inode->i_lock); | |
501 | WARN_ON(!(inode->i_state & I_LRU_ISOLATING)); | |
502 | inode->i_state &= ~I_LRU_ISOLATING; | |
503 | smp_mb(); | |
504 | wake_up_bit(&inode->i_state, __I_LRU_ISOLATING); | |
505 | spin_unlock(&inode->i_lock); | |
506 | } | |
507 | ||
508 | static void inode_wait_for_lru_isolating(struct inode *inode) | |
509 | { | |
510 | spin_lock(&inode->i_lock); | |
511 | if (inode->i_state & I_LRU_ISOLATING) { | |
512 | DEFINE_WAIT_BIT(wq, &inode->i_state, __I_LRU_ISOLATING); | |
513 | wait_queue_head_t *wqh; | |
514 | ||
515 | wqh = bit_waitqueue(&inode->i_state, __I_LRU_ISOLATING); | |
516 | spin_unlock(&inode->i_lock); | |
517 | __wait_on_bit(wqh, &wq, bit_wait, TASK_UNINTERRUPTIBLE); | |
518 | spin_lock(&inode->i_lock); | |
519 | WARN_ON(inode->i_state & I_LRU_ISOLATING); | |
520 | } | |
521 | spin_unlock(&inode->i_lock); | |
522 | } | |
523 | ||
646ec461 CH |
524 | /** |
525 | * inode_sb_list_add - add inode to the superblock list of inodes | |
526 | * @inode: inode to add | |
527 | */ | |
528 | void inode_sb_list_add(struct inode *inode) | |
529 | { | |
74278da9 | 530 | spin_lock(&inode->i_sb->s_inode_list_lock); |
55fa6091 | 531 | list_add(&inode->i_sb_list, &inode->i_sb->s_inodes); |
74278da9 | 532 | spin_unlock(&inode->i_sb->s_inode_list_lock); |
646ec461 CH |
533 | } |
534 | EXPORT_SYMBOL_GPL(inode_sb_list_add); | |
535 | ||
55fa6091 | 536 | static inline void inode_sb_list_del(struct inode *inode) |
646ec461 | 537 | { |
a209dfc7 | 538 | if (!list_empty(&inode->i_sb_list)) { |
74278da9 | 539 | spin_lock(&inode->i_sb->s_inode_list_lock); |
a209dfc7 | 540 | list_del_init(&inode->i_sb_list); |
74278da9 | 541 | spin_unlock(&inode->i_sb->s_inode_list_lock); |
a209dfc7 | 542 | } |
646ec461 CH |
543 | } |
544 | ||
4c51acbc DC |
545 | static unsigned long hash(struct super_block *sb, unsigned long hashval) |
546 | { | |
547 | unsigned long tmp; | |
548 | ||
549 | tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / | |
550 | L1_CACHE_BYTES; | |
4b4563dc CH |
551 | tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift); |
552 | return tmp & i_hash_mask; | |
4c51acbc DC |
553 | } |
554 | ||
555 | /** | |
556 | * __insert_inode_hash - hash an inode | |
557 | * @inode: unhashed inode | |
558 | * @hashval: unsigned long value used to locate this object in the | |
559 | * inode_hashtable. | |
560 | * | |
561 | * Add an inode to the inode hash for this superblock. | |
562 | */ | |
563 | void __insert_inode_hash(struct inode *inode, unsigned long hashval) | |
564 | { | |
646ec461 CH |
565 | struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); |
566 | ||
67a23c49 | 567 | spin_lock(&inode_hash_lock); |
250df6ed | 568 | spin_lock(&inode->i_lock); |
3f19b2ab | 569 | hlist_add_head_rcu(&inode->i_hash, b); |
250df6ed | 570 | spin_unlock(&inode->i_lock); |
67a23c49 | 571 | spin_unlock(&inode_hash_lock); |
4c51acbc DC |
572 | } |
573 | EXPORT_SYMBOL(__insert_inode_hash); | |
574 | ||
4c51acbc | 575 | /** |
f2ee7abf | 576 | * __remove_inode_hash - remove an inode from the hash |
4c51acbc DC |
577 | * @inode: inode to unhash |
578 | * | |
579 | * Remove an inode from the superblock. | |
580 | */ | |
f2ee7abf | 581 | void __remove_inode_hash(struct inode *inode) |
4c51acbc | 582 | { |
67a23c49 | 583 | spin_lock(&inode_hash_lock); |
250df6ed | 584 | spin_lock(&inode->i_lock); |
3f19b2ab | 585 | hlist_del_init_rcu(&inode->i_hash); |
250df6ed | 586 | spin_unlock(&inode->i_lock); |
67a23c49 | 587 | spin_unlock(&inode_hash_lock); |
4c51acbc | 588 | } |
f2ee7abf | 589 | EXPORT_SYMBOL(__remove_inode_hash); |
4c51acbc | 590 | |
3e9d80a8 MWO |
591 | void dump_mapping(const struct address_space *mapping) |
592 | { | |
593 | struct inode *host; | |
594 | const struct address_space_operations *a_ops; | |
595 | struct hlist_node *dentry_first; | |
596 | struct dentry *dentry_ptr; | |
597 | struct dentry dentry; | |
598 | unsigned long ino; | |
599 | ||
600 | /* | |
601 | * If mapping is an invalid pointer, we don't want to crash | |
602 | * accessing it, so probe everything depending on it carefully. | |
603 | */ | |
604 | if (get_kernel_nofault(host, &mapping->host) || | |
605 | get_kernel_nofault(a_ops, &mapping->a_ops)) { | |
606 | pr_warn("invalid mapping:%px\n", mapping); | |
607 | return; | |
608 | } | |
609 | ||
610 | if (!host) { | |
611 | pr_warn("aops:%ps\n", a_ops); | |
612 | return; | |
613 | } | |
614 | ||
615 | if (get_kernel_nofault(dentry_first, &host->i_dentry.first) || | |
616 | get_kernel_nofault(ino, &host->i_ino)) { | |
617 | pr_warn("aops:%ps invalid inode:%px\n", a_ops, host); | |
618 | return; | |
619 | } | |
620 | ||
621 | if (!dentry_first) { | |
622 | pr_warn("aops:%ps ino:%lx\n", a_ops, ino); | |
623 | return; | |
624 | } | |
625 | ||
626 | dentry_ptr = container_of(dentry_first, struct dentry, d_u.d_alias); | |
8b3d8381 BW |
627 | if (get_kernel_nofault(dentry, dentry_ptr) || |
628 | !dentry.d_parent || !dentry.d_name.name) { | |
3e9d80a8 MWO |
629 | pr_warn("aops:%ps ino:%lx invalid dentry:%px\n", |
630 | a_ops, ino, dentry_ptr); | |
631 | return; | |
632 | } | |
633 | ||
634 | /* | |
635 | * if dentry is corrupted, the %pd handler may still crash, | |
636 | * but it's unlikely that we reach here with a corrupt mapping | |
637 | */ | |
638 | pr_warn("aops:%ps ino:%lx dentry name:\"%pd\"\n", a_ops, ino, &dentry); | |
639 | } | |
640 | ||
dbd5768f | 641 | void clear_inode(struct inode *inode) |
b0683aa6 | 642 | { |
08142579 | 643 | /* |
b93b0163 | 644 | * We have to cycle the i_pages lock here because reclaim can be in the |
6ffcd825 | 645 | * process of removing the last page (in __filemap_remove_folio()) |
b93b0163 | 646 | * and we must not free the mapping under it. |
08142579 | 647 | */ |
b93b0163 | 648 | xa_lock_irq(&inode->i_data.i_pages); |
b0683aa6 | 649 | BUG_ON(inode->i_data.nrpages); |
786b3112 HD |
650 | /* |
651 | * Almost always, mapping_empty(&inode->i_data) here; but there are | |
652 | * two known and long-standing ways in which nodes may get left behind | |
653 | * (when deep radix-tree node allocation failed partway; or when THP | |
654 | * collapse_file() failed). Until those two known cases are cleaned up, | |
655 | * or a cleanup function is called here, do not BUG_ON(!mapping_empty), | |
656 | * nor even WARN_ON(!mapping_empty). | |
657 | */ | |
b93b0163 | 658 | xa_unlock_irq(&inode->i_data.i_pages); |
600f111e | 659 | BUG_ON(!list_empty(&inode->i_data.i_private_list)); |
b0683aa6 AV |
660 | BUG_ON(!(inode->i_state & I_FREEING)); |
661 | BUG_ON(inode->i_state & I_CLEAR); | |
6c60d2b5 | 662 | BUG_ON(!list_empty(&inode->i_wb_list)); |
fa0d7e3d | 663 | /* don't need i_lock here, no concurrent mods to i_state */ |
b0683aa6 AV |
664 | inode->i_state = I_FREEING | I_CLEAR; |
665 | } | |
dbd5768f | 666 | EXPORT_SYMBOL(clear_inode); |
b0683aa6 | 667 | |
b2b2af8e DC |
668 | /* |
669 | * Free the inode passed in, removing it from the lists it is still connected | |
670 | * to. We remove any pages still attached to the inode and wait for any IO that | |
671 | * is still in progress before finally destroying the inode. | |
672 | * | |
673 | * An inode must already be marked I_FREEING so that we avoid the inode being | |
674 | * moved back onto lists if we race with other code that manipulates the lists | |
675 | * (e.g. writeback_single_inode). The caller is responsible for setting this. | |
676 | * | |
677 | * An inode must already be removed from the LRU list before being evicted from | |
678 | * the cache. This should occur atomically with setting the I_FREEING state | |
679 | * flag, so no inodes here should ever be on the LRU when being evicted. | |
680 | */ | |
644da596 | 681 | static void evict(struct inode *inode) |
b4272d4c AV |
682 | { |
683 | const struct super_operations *op = inode->i_sb->s_op; | |
684 | ||
b2b2af8e DC |
685 | BUG_ON(!(inode->i_state & I_FREEING)); |
686 | BUG_ON(!list_empty(&inode->i_lru)); | |
687 | ||
c7f54084 DC |
688 | if (!list_empty(&inode->i_io_list)) |
689 | inode_io_list_del(inode); | |
b12362bd | 690 | |
55fa6091 DC |
691 | inode_sb_list_del(inode); |
692 | ||
2a062983 ZC |
693 | inode_wait_for_lru_isolating(inode); |
694 | ||
169ebd90 JK |
695 | /* |
696 | * Wait for flusher thread to be done with the inode so that filesystem | |
697 | * does not start destroying it while writeback is still running. Since | |
698 | * the inode has I_FREEING set, flusher thread won't start new work on | |
699 | * the inode. We just have to wait for running writeback to finish. | |
700 | */ | |
701 | inode_wait_for_writeback(inode); | |
7994e6f7 | 702 | |
be7ce416 AV |
703 | if (op->evict_inode) { |
704 | op->evict_inode(inode); | |
b4272d4c | 705 | } else { |
91b0abe3 | 706 | truncate_inode_pages_final(&inode->i_data); |
dbd5768f | 707 | clear_inode(inode); |
b4272d4c | 708 | } |
661074e9 AV |
709 | if (S_ISCHR(inode->i_mode) && inode->i_cdev) |
710 | cd_forget(inode); | |
b2b2af8e DC |
711 | |
712 | remove_inode_hash(inode); | |
713 | ||
5bc9ad78 MG |
714 | /* |
715 | * Wake up waiters in __wait_on_freeing_inode(). | |
716 | * | |
717 | * Lockless hash lookup may end up finding the inode before we removed | |
718 | * it above, but only lock it *after* we are done with the wakeup below. | |
719 | * In this case the potential waiter cannot safely block. | |
720 | * | |
721 | * The inode being unhashed after the call to remove_inode_hash() is | |
722 | * used as an indicator whether blocking on it is safe. | |
723 | */ | |
b2b2af8e DC |
724 | spin_lock(&inode->i_lock); |
725 | wake_up_bit(&inode->i_state, __I_NEW); | |
726 | BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); | |
727 | spin_unlock(&inode->i_lock); | |
728 | ||
729 | destroy_inode(inode); | |
b4272d4c AV |
730 | } |
731 | ||
1da177e4 LT |
732 | /* |
733 | * dispose_list - dispose of the contents of a local list | |
734 | * @head: the head of the list to free | |
735 | * | |
736 | * Dispose-list gets a local list with local inodes in it, so it doesn't | |
737 | * need to worry about list corruption and SMP locks. | |
738 | */ | |
739 | static void dispose_list(struct list_head *head) | |
740 | { | |
1da177e4 LT |
741 | while (!list_empty(head)) { |
742 | struct inode *inode; | |
743 | ||
7ccf19a8 NP |
744 | inode = list_first_entry(head, struct inode, i_lru); |
745 | list_del_init(&inode->i_lru); | |
1da177e4 | 746 | |
644da596 | 747 | evict(inode); |
ac05fbb4 | 748 | cond_resched(); |
1da177e4 | 749 | } |
1da177e4 LT |
750 | } |
751 | ||
63997e98 AV |
752 | /** |
753 | * evict_inodes - evict all evictable inodes for a superblock | |
754 | * @sb: superblock to operate on | |
755 | * | |
756 | * Make sure that no inodes with zero refcount are retained. This is | |
1751e8a6 | 757 | * called by superblock shutdown after having SB_ACTIVE flag removed, |
63997e98 AV |
758 | * so any inode reaching zero refcount during or after that call will |
759 | * be immediately evicted. | |
1da177e4 | 760 | */ |
63997e98 | 761 | void evict_inodes(struct super_block *sb) |
1da177e4 | 762 | { |
63997e98 AV |
763 | struct inode *inode, *next; |
764 | LIST_HEAD(dispose); | |
1da177e4 | 765 | |
ac05fbb4 | 766 | again: |
74278da9 | 767 | spin_lock(&sb->s_inode_list_lock); |
63997e98 AV |
768 | list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { |
769 | if (atomic_read(&inode->i_count)) | |
aabb8fdb | 770 | continue; |
250df6ed DC |
771 | |
772 | spin_lock(&inode->i_lock); | |
773 | if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { | |
774 | spin_unlock(&inode->i_lock); | |
1da177e4 | 775 | continue; |
250df6ed | 776 | } |
63997e98 AV |
777 | |
778 | inode->i_state |= I_FREEING; | |
02afc410 | 779 | inode_lru_list_del(inode); |
250df6ed | 780 | spin_unlock(&inode->i_lock); |
02afc410 | 781 | list_add(&inode->i_lru, &dispose); |
ac05fbb4 JB |
782 | |
783 | /* | |
784 | * We can have a ton of inodes to evict at unmount time given | |
785 | * enough memory, check to see if we need to go to sleep for a | |
786 | * bit so we don't livelock. | |
787 | */ | |
788 | if (need_resched()) { | |
789 | spin_unlock(&sb->s_inode_list_lock); | |
790 | cond_resched(); | |
791 | dispose_list(&dispose); | |
792 | goto again; | |
793 | } | |
1da177e4 | 794 | } |
74278da9 | 795 | spin_unlock(&sb->s_inode_list_lock); |
63997e98 AV |
796 | |
797 | dispose_list(&dispose); | |
1da177e4 | 798 | } |
799ea9e9 | 799 | EXPORT_SYMBOL_GPL(evict_inodes); |
1da177e4 | 800 | |
1da177e4 | 801 | /** |
a0318786 CH |
802 | * invalidate_inodes - attempt to free all inodes on a superblock |
803 | * @sb: superblock to operate on | |
1da177e4 | 804 | * |
e127b9bc | 805 | * Attempts to free all inodes (including dirty inodes) for a given superblock. |
1da177e4 | 806 | */ |
e127b9bc | 807 | void invalidate_inodes(struct super_block *sb) |
1da177e4 | 808 | { |
a0318786 CH |
809 | struct inode *inode, *next; |
810 | LIST_HEAD(dispose); | |
1da177e4 | 811 | |
04646aeb | 812 | again: |
74278da9 | 813 | spin_lock(&sb->s_inode_list_lock); |
a0318786 | 814 | list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { |
250df6ed DC |
815 | spin_lock(&inode->i_lock); |
816 | if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { | |
817 | spin_unlock(&inode->i_lock); | |
aabb8fdb | 818 | continue; |
250df6ed | 819 | } |
99a38919 | 820 | if (atomic_read(&inode->i_count)) { |
250df6ed | 821 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
822 | continue; |
823 | } | |
99a38919 | 824 | |
99a38919 | 825 | inode->i_state |= I_FREEING; |
02afc410 | 826 | inode_lru_list_del(inode); |
250df6ed | 827 | spin_unlock(&inode->i_lock); |
02afc410 | 828 | list_add(&inode->i_lru, &dispose); |
04646aeb ES |
829 | if (need_resched()) { |
830 | spin_unlock(&sb->s_inode_list_lock); | |
831 | cond_resched(); | |
832 | dispose_list(&dispose); | |
833 | goto again; | |
834 | } | |
1da177e4 | 835 | } |
74278da9 | 836 | spin_unlock(&sb->s_inode_list_lock); |
1da177e4 | 837 | |
a0318786 | 838 | dispose_list(&dispose); |
1da177e4 | 839 | } |
1da177e4 | 840 | |
1da177e4 | 841 | /* |
bc3b14cb | 842 | * Isolate the inode from the LRU in preparation for freeing it. |
1da177e4 | 843 | * |
9e38d86f NP |
844 | * If the inode has the I_REFERENCED flag set, then it means that it has been |
845 | * used recently - the flag is set in iput_final(). When we encounter such an | |
846 | * inode, clear the flag and move it to the back of the LRU so it gets another | |
847 | * pass through the LRU before it gets reclaimed. This is necessary because of | |
848 | * the fact we are doing lazy LRU updates to minimise lock contention so the | |
849 | * LRU does not have strict ordering. Hence we don't want to reclaim inodes | |
850 | * with this flag set because they are the inodes that are out of order. | |
1da177e4 | 851 | */ |
3f97b163 VD |
852 | static enum lru_status inode_lru_isolate(struct list_head *item, |
853 | struct list_lru_one *lru, spinlock_t *lru_lock, void *arg) | |
1da177e4 | 854 | { |
bc3b14cb DC |
855 | struct list_head *freeable = arg; |
856 | struct inode *inode = container_of(item, struct inode, i_lru); | |
1da177e4 | 857 | |
bc3b14cb | 858 | /* |
51b8c1fe JW |
859 | * We are inverting the lru lock/inode->i_lock here, so use a |
860 | * trylock. If we fail to get the lock, just skip it. | |
bc3b14cb DC |
861 | */ |
862 | if (!spin_trylock(&inode->i_lock)) | |
863 | return LRU_SKIP; | |
1da177e4 | 864 | |
bc3b14cb | 865 | /* |
51b8c1fe JW |
866 | * Inodes can get referenced, redirtied, or repopulated while |
867 | * they're already on the LRU, and this can make them | |
868 | * unreclaimable for a while. Remove them lazily here; iput, | |
869 | * sync, or the last page cache deletion will requeue them. | |
bc3b14cb DC |
870 | */ |
871 | if (atomic_read(&inode->i_count) || | |
51b8c1fe JW |
872 | (inode->i_state & ~I_REFERENCED) || |
873 | !mapping_shrinkable(&inode->i_data)) { | |
3f97b163 | 874 | list_lru_isolate(lru, &inode->i_lru); |
bc3b14cb DC |
875 | spin_unlock(&inode->i_lock); |
876 | this_cpu_dec(nr_unused); | |
877 | return LRU_REMOVED; | |
878 | } | |
1da177e4 | 879 | |
51b8c1fe | 880 | /* Recently referenced inodes get one more pass */ |
69056ee6 | 881 | if (inode->i_state & I_REFERENCED) { |
bc3b14cb DC |
882 | inode->i_state &= ~I_REFERENCED; |
883 | spin_unlock(&inode->i_lock); | |
884 | return LRU_ROTATE; | |
885 | } | |
1da177e4 | 886 | |
51b8c1fe JW |
887 | /* |
888 | * On highmem systems, mapping_shrinkable() permits dropping | |
889 | * page cache in order to free up struct inodes: lowmem might | |
890 | * be under pressure before the cache inside the highmem zone. | |
891 | */ | |
7ae12c80 | 892 | if (inode_has_buffers(inode) || !mapping_empty(&inode->i_data)) { |
2a062983 | 893 | inode_pin_lru_isolating(inode); |
bc3b14cb DC |
894 | spin_unlock(&inode->i_lock); |
895 | spin_unlock(lru_lock); | |
896 | if (remove_inode_buffers(inode)) { | |
897 | unsigned long reap; | |
898 | reap = invalidate_mapping_pages(&inode->i_data, 0, -1); | |
899 | if (current_is_kswapd()) | |
900 | __count_vm_events(KSWAPD_INODESTEAL, reap); | |
901 | else | |
902 | __count_vm_events(PGINODESTEAL, reap); | |
c7b23b68 | 903 | mm_account_reclaimed_pages(reap); |
02afc410 | 904 | } |
2a062983 | 905 | inode_unpin_lru_isolating(inode); |
bc3b14cb DC |
906 | spin_lock(lru_lock); |
907 | return LRU_RETRY; | |
908 | } | |
02afc410 | 909 | |
bc3b14cb DC |
910 | WARN_ON(inode->i_state & I_NEW); |
911 | inode->i_state |= I_FREEING; | |
3f97b163 | 912 | list_lru_isolate_move(lru, &inode->i_lru, freeable); |
bc3b14cb | 913 | spin_unlock(&inode->i_lock); |
9e38d86f | 914 | |
bc3b14cb DC |
915 | this_cpu_dec(nr_unused); |
916 | return LRU_REMOVED; | |
917 | } | |
7ccf19a8 | 918 | |
bc3b14cb DC |
919 | /* |
920 | * Walk the superblock inode LRU for freeable inodes and attempt to free them. | |
921 | * This is called from the superblock shrinker function with a number of inodes | |
922 | * to trim from the LRU. Inodes to be freed are moved to a temporary list and | |
923 | * then are freed outside inode_lock by dispose_list(). | |
924 | */ | |
503c358c | 925 | long prune_icache_sb(struct super_block *sb, struct shrink_control *sc) |
bc3b14cb DC |
926 | { |
927 | LIST_HEAD(freeable); | |
928 | long freed; | |
1da177e4 | 929 | |
503c358c VD |
930 | freed = list_lru_shrink_walk(&sb->s_inode_lru, sc, |
931 | inode_lru_isolate, &freeable); | |
1da177e4 | 932 | dispose_list(&freeable); |
0a234c6d | 933 | return freed; |
1da177e4 LT |
934 | } |
935 | ||
f5e5e97c | 936 | static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked); |
1da177e4 LT |
937 | /* |
938 | * Called with the inode lock held. | |
1da177e4 | 939 | */ |
6b3304b5 MK |
940 | static struct inode *find_inode(struct super_block *sb, |
941 | struct hlist_head *head, | |
942 | int (*test)(struct inode *, void *), | |
f5e5e97c | 943 | void *data, bool is_inode_hash_locked) |
1da177e4 | 944 | { |
6b3304b5 | 945 | struct inode *inode = NULL; |
1da177e4 | 946 | |
f5e5e97c | 947 | if (is_inode_hash_locked) |
7180f8d9 MG |
948 | lockdep_assert_held(&inode_hash_lock); |
949 | else | |
950 | lockdep_assert_not_held(&inode_hash_lock); | |
951 | ||
952 | rcu_read_lock(); | |
1da177e4 | 953 | repeat: |
7180f8d9 | 954 | hlist_for_each_entry_rcu(inode, head, i_hash) { |
5a3cd992 | 955 | if (inode->i_sb != sb) |
1da177e4 | 956 | continue; |
5a3cd992 | 957 | if (!test(inode, data)) |
1da177e4 | 958 | continue; |
5a3cd992 | 959 | spin_lock(&inode->i_lock); |
a4ffdde6 | 960 | if (inode->i_state & (I_FREEING|I_WILL_FREE)) { |
f5e5e97c | 961 | __wait_on_freeing_inode(inode, is_inode_hash_locked); |
1da177e4 LT |
962 | goto repeat; |
963 | } | |
c2b6d621 AV |
964 | if (unlikely(inode->i_state & I_CREATING)) { |
965 | spin_unlock(&inode->i_lock); | |
7180f8d9 | 966 | rcu_read_unlock(); |
c2b6d621 AV |
967 | return ERR_PTR(-ESTALE); |
968 | } | |
f7899bd5 | 969 | __iget(inode); |
250df6ed | 970 | spin_unlock(&inode->i_lock); |
7180f8d9 | 971 | rcu_read_unlock(); |
f7899bd5 | 972 | return inode; |
1da177e4 | 973 | } |
7180f8d9 | 974 | rcu_read_unlock(); |
f7899bd5 | 975 | return NULL; |
1da177e4 LT |
976 | } |
977 | ||
978 | /* | |
979 | * find_inode_fast is the fast path version of find_inode, see the comment at | |
980 | * iget_locked for details. | |
981 | */ | |
6b3304b5 | 982 | static struct inode *find_inode_fast(struct super_block *sb, |
7180f8d9 | 983 | struct hlist_head *head, unsigned long ino, |
f5e5e97c | 984 | bool is_inode_hash_locked) |
1da177e4 | 985 | { |
6b3304b5 | 986 | struct inode *inode = NULL; |
1da177e4 | 987 | |
f5e5e97c | 988 | if (is_inode_hash_locked) |
7180f8d9 MG |
989 | lockdep_assert_held(&inode_hash_lock); |
990 | else | |
991 | lockdep_assert_not_held(&inode_hash_lock); | |
992 | ||
993 | rcu_read_lock(); | |
1da177e4 | 994 | repeat: |
7180f8d9 | 995 | hlist_for_each_entry_rcu(inode, head, i_hash) { |
5a3cd992 | 996 | if (inode->i_ino != ino) |
1da177e4 | 997 | continue; |
5a3cd992 | 998 | if (inode->i_sb != sb) |
1da177e4 | 999 | continue; |
5a3cd992 | 1000 | spin_lock(&inode->i_lock); |
a4ffdde6 | 1001 | if (inode->i_state & (I_FREEING|I_WILL_FREE)) { |
f5e5e97c | 1002 | __wait_on_freeing_inode(inode, is_inode_hash_locked); |
1da177e4 LT |
1003 | goto repeat; |
1004 | } | |
c2b6d621 AV |
1005 | if (unlikely(inode->i_state & I_CREATING)) { |
1006 | spin_unlock(&inode->i_lock); | |
7180f8d9 | 1007 | rcu_read_unlock(); |
c2b6d621 AV |
1008 | return ERR_PTR(-ESTALE); |
1009 | } | |
f7899bd5 | 1010 | __iget(inode); |
250df6ed | 1011 | spin_unlock(&inode->i_lock); |
7180f8d9 | 1012 | rcu_read_unlock(); |
f7899bd5 | 1013 | return inode; |
1da177e4 | 1014 | } |
7180f8d9 | 1015 | rcu_read_unlock(); |
f7899bd5 | 1016 | return NULL; |
8290c35f DC |
1017 | } |
1018 | ||
f991bd2e ED |
1019 | /* |
1020 | * Each cpu owns a range of LAST_INO_BATCH numbers. | |
1021 | * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations, | |
1022 | * to renew the exhausted range. | |
8290c35f | 1023 | * |
f991bd2e ED |
1024 | * This does not significantly increase overflow rate because every CPU can |
1025 | * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is | |
1026 | * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the | |
1027 | * 2^32 range, and is a worst-case. Even a 50% wastage would only increase | |
1028 | * overflow rate by 2x, which does not seem too significant. | |
1029 | * | |
1030 | * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW | |
1031 | * error if st_ino won't fit in target struct field. Use 32bit counter | |
1032 | * here to attempt to avoid that. | |
8290c35f | 1033 | */ |
f991bd2e ED |
1034 | #define LAST_INO_BATCH 1024 |
1035 | static DEFINE_PER_CPU(unsigned int, last_ino); | |
1036 | ||
85fe4025 | 1037 | unsigned int get_next_ino(void) |
8290c35f | 1038 | { |
f991bd2e ED |
1039 | unsigned int *p = &get_cpu_var(last_ino); |
1040 | unsigned int res = *p; | |
8290c35f | 1041 | |
f991bd2e ED |
1042 | #ifdef CONFIG_SMP |
1043 | if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) { | |
1044 | static atomic_t shared_last_ino; | |
1045 | int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino); | |
1046 | ||
1047 | res = next - LAST_INO_BATCH; | |
1048 | } | |
1049 | #endif | |
1050 | ||
2adc376c CM |
1051 | res++; |
1052 | /* get_next_ino should not provide a 0 inode number */ | |
1053 | if (unlikely(!res)) | |
1054 | res++; | |
1055 | *p = res; | |
f991bd2e ED |
1056 | put_cpu_var(last_ino); |
1057 | return res; | |
8290c35f | 1058 | } |
85fe4025 | 1059 | EXPORT_SYMBOL(get_next_ino); |
8290c35f | 1060 | |
a209dfc7 ED |
1061 | /** |
1062 | * new_inode_pseudo - obtain an inode | |
1063 | * @sb: superblock | |
1064 | * | |
1065 | * Allocates a new inode for given superblock. | |
1066 | * Inode wont be chained in superblock s_inodes list | |
1067 | * This means : | |
1068 | * - fs can't be unmount | |
1069 | * - quotas, fsnotify, writeback can't work | |
1070 | */ | |
1071 | struct inode *new_inode_pseudo(struct super_block *sb) | |
1072 | { | |
5a9b911b | 1073 | return alloc_inode(sb); |
a209dfc7 ED |
1074 | } |
1075 | ||
1da177e4 LT |
1076 | /** |
1077 | * new_inode - obtain an inode | |
1078 | * @sb: superblock | |
1079 | * | |
769848c0 | 1080 | * Allocates a new inode for given superblock. The default gfp_mask |
3c1d4378 | 1081 | * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE. |
769848c0 MG |
1082 | * If HIGHMEM pages are unsuitable or it is known that pages allocated |
1083 | * for the page cache are not reclaimable or migratable, | |
1084 | * mapping_set_gfp_mask() must be called with suitable flags on the | |
1085 | * newly created inode's mapping | |
1086 | * | |
1da177e4 LT |
1087 | */ |
1088 | struct inode *new_inode(struct super_block *sb) | |
1089 | { | |
6b3304b5 | 1090 | struct inode *inode; |
1da177e4 | 1091 | |
a209dfc7 ED |
1092 | inode = new_inode_pseudo(sb); |
1093 | if (inode) | |
55fa6091 | 1094 | inode_sb_list_add(inode); |
1da177e4 LT |
1095 | return inode; |
1096 | } | |
1da177e4 LT |
1097 | EXPORT_SYMBOL(new_inode); |
1098 | ||
14358e6d | 1099 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
e096d0c7 JB |
1100 | void lockdep_annotate_inode_mutex_key(struct inode *inode) |
1101 | { | |
a3314a0e | 1102 | if (S_ISDIR(inode->i_mode)) { |
1e89a5e1 PZ |
1103 | struct file_system_type *type = inode->i_sb->s_type; |
1104 | ||
9a7aa12f | 1105 | /* Set new key only if filesystem hasn't already changed it */ |
9902af79 | 1106 | if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) { |
9a7aa12f JK |
1107 | /* |
1108 | * ensure nobody is actually holding i_mutex | |
1109 | */ | |
9902af79 AV |
1110 | // mutex_destroy(&inode->i_mutex); |
1111 | init_rwsem(&inode->i_rwsem); | |
1112 | lockdep_set_class(&inode->i_rwsem, | |
9a7aa12f JK |
1113 | &type->i_mutex_dir_key); |
1114 | } | |
1e89a5e1 | 1115 | } |
e096d0c7 JB |
1116 | } |
1117 | EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key); | |
14358e6d | 1118 | #endif |
e096d0c7 JB |
1119 | |
1120 | /** | |
1121 | * unlock_new_inode - clear the I_NEW state and wake up any waiters | |
1122 | * @inode: new inode to unlock | |
1123 | * | |
1124 | * Called when the inode is fully initialised to clear the new state of the | |
1125 | * inode and wake up anyone waiting for the inode to finish initialisation. | |
1126 | */ | |
1127 | void unlock_new_inode(struct inode *inode) | |
1128 | { | |
1129 | lockdep_annotate_inode_mutex_key(inode); | |
250df6ed | 1130 | spin_lock(&inode->i_lock); |
eaff8079 | 1131 | WARN_ON(!(inode->i_state & I_NEW)); |
c2b6d621 | 1132 | inode->i_state &= ~I_NEW & ~I_CREATING; |
310fa7a3 | 1133 | smp_mb(); |
250df6ed DC |
1134 | wake_up_bit(&inode->i_state, __I_NEW); |
1135 | spin_unlock(&inode->i_lock); | |
1da177e4 | 1136 | } |
1da177e4 LT |
1137 | EXPORT_SYMBOL(unlock_new_inode); |
1138 | ||
c2b6d621 AV |
1139 | void discard_new_inode(struct inode *inode) |
1140 | { | |
1141 | lockdep_annotate_inode_mutex_key(inode); | |
1142 | spin_lock(&inode->i_lock); | |
1143 | WARN_ON(!(inode->i_state & I_NEW)); | |
1144 | inode->i_state &= ~I_NEW; | |
1145 | smp_mb(); | |
1146 | wake_up_bit(&inode->i_state, __I_NEW); | |
1147 | spin_unlock(&inode->i_lock); | |
1148 | iput(inode); | |
1149 | } | |
1150 | EXPORT_SYMBOL(discard_new_inode); | |
1151 | ||
375e289e BF |
1152 | /** |
1153 | * lock_two_nondirectories - take two i_mutexes on non-directory objects | |
4fd699ae | 1154 | * |
2454ad83 | 1155 | * Lock any non-NULL argument. Passed objects must not be directories. |
4fd699ae BF |
1156 | * Zero, one or two objects may be locked by this function. |
1157 | * | |
375e289e BF |
1158 | * @inode1: first inode to lock |
1159 | * @inode2: second inode to lock | |
1160 | */ | |
1161 | void lock_two_nondirectories(struct inode *inode1, struct inode *inode2) | |
1162 | { | |
33ab231f CB |
1163 | if (inode1) |
1164 | WARN_ON_ONCE(S_ISDIR(inode1->i_mode)); | |
1165 | if (inode2) | |
1166 | WARN_ON_ONCE(S_ISDIR(inode2->i_mode)); | |
dbd4540d AV |
1167 | if (inode1 > inode2) |
1168 | swap(inode1, inode2); | |
1169 | if (inode1) | |
1170 | inode_lock(inode1); | |
1171 | if (inode2 && inode2 != inode1) | |
1172 | inode_lock_nested(inode2, I_MUTEX_NONDIR2); | |
375e289e BF |
1173 | } |
1174 | EXPORT_SYMBOL(lock_two_nondirectories); | |
1175 | ||
1176 | /** | |
1177 | * unlock_two_nondirectories - release locks from lock_two_nondirectories() | |
1178 | * @inode1: first inode to unlock | |
1179 | * @inode2: second inode to unlock | |
1180 | */ | |
1181 | void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2) | |
1182 | { | |
2454ad83 JK |
1183 | if (inode1) { |
1184 | WARN_ON_ONCE(S_ISDIR(inode1->i_mode)); | |
5955102c | 1185 | inode_unlock(inode1); |
2454ad83 JK |
1186 | } |
1187 | if (inode2 && inode2 != inode1) { | |
1188 | WARN_ON_ONCE(S_ISDIR(inode2->i_mode)); | |
5955102c | 1189 | inode_unlock(inode2); |
2454ad83 | 1190 | } |
375e289e BF |
1191 | } |
1192 | EXPORT_SYMBOL(unlock_two_nondirectories); | |
1193 | ||
80ea09a0 MS |
1194 | /** |
1195 | * inode_insert5 - obtain an inode from a mounted file system | |
1196 | * @inode: pre-allocated inode to use for insert to cache | |
1197 | * @hashval: hash value (usually inode number) to get | |
1198 | * @test: callback used for comparisons between inodes | |
1199 | * @set: callback used to initialize a new struct inode | |
1200 | * @data: opaque data pointer to pass to @test and @set | |
1201 | * | |
1202 | * Search for the inode specified by @hashval and @data in the inode cache, | |
1203 | * and if present it is return it with an increased reference count. This is | |
1204 | * a variant of iget5_locked() for callers that don't want to fail on memory | |
1205 | * allocation of inode. | |
1206 | * | |
1207 | * If the inode is not in cache, insert the pre-allocated inode to cache and | |
1208 | * return it locked, hashed, and with the I_NEW flag set. The file system gets | |
1209 | * to fill it in before unlocking it via unlock_new_inode(). | |
1210 | * | |
1211 | * Note both @test and @set are called with the inode_hash_lock held, so can't | |
1212 | * sleep. | |
1213 | */ | |
1214 | struct inode *inode_insert5(struct inode *inode, unsigned long hashval, | |
1215 | int (*test)(struct inode *, void *), | |
1216 | int (*set)(struct inode *, void *), void *data) | |
1217 | { | |
1218 | struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval); | |
1219 | struct inode *old; | |
1220 | ||
1221 | again: | |
1222 | spin_lock(&inode_hash_lock); | |
7180f8d9 | 1223 | old = find_inode(inode->i_sb, head, test, data, true); |
80ea09a0 MS |
1224 | if (unlikely(old)) { |
1225 | /* | |
1226 | * Uhhuh, somebody else created the same inode under us. | |
1227 | * Use the old inode instead of the preallocated one. | |
1228 | */ | |
1229 | spin_unlock(&inode_hash_lock); | |
c2b6d621 AV |
1230 | if (IS_ERR(old)) |
1231 | return NULL; | |
80ea09a0 MS |
1232 | wait_on_inode(old); |
1233 | if (unlikely(inode_unhashed(old))) { | |
1234 | iput(old); | |
1235 | goto again; | |
1236 | } | |
1237 | return old; | |
1238 | } | |
1239 | ||
1240 | if (set && unlikely(set(inode, data))) { | |
1241 | inode = NULL; | |
1242 | goto unlock; | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * Return the locked inode with I_NEW set, the | |
1247 | * caller is responsible for filling in the contents | |
1248 | */ | |
1249 | spin_lock(&inode->i_lock); | |
1250 | inode->i_state |= I_NEW; | |
3f19b2ab | 1251 | hlist_add_head_rcu(&inode->i_hash, head); |
80ea09a0 | 1252 | spin_unlock(&inode->i_lock); |
18cc912b JL |
1253 | |
1254 | /* | |
1255 | * Add inode to the sb list if it's not already. It has I_NEW at this | |
1256 | * point, so it should be safe to test i_sb_list locklessly. | |
1257 | */ | |
1258 | if (list_empty(&inode->i_sb_list)) | |
e950564b | 1259 | inode_sb_list_add(inode); |
80ea09a0 MS |
1260 | unlock: |
1261 | spin_unlock(&inode_hash_lock); | |
1262 | ||
1263 | return inode; | |
1264 | } | |
1265 | EXPORT_SYMBOL(inode_insert5); | |
1266 | ||
0b2d0724 CH |
1267 | /** |
1268 | * iget5_locked - obtain an inode from a mounted file system | |
1269 | * @sb: super block of file system | |
1270 | * @hashval: hash value (usually inode number) to get | |
1271 | * @test: callback used for comparisons between inodes | |
1272 | * @set: callback used to initialize a new struct inode | |
1273 | * @data: opaque data pointer to pass to @test and @set | |
1274 | * | |
1275 | * Search for the inode specified by @hashval and @data in the inode cache, | |
1276 | * and if present it is return it with an increased reference count. This is | |
1277 | * a generalized version of iget_locked() for file systems where the inode | |
1278 | * number is not sufficient for unique identification of an inode. | |
1279 | * | |
1280 | * If the inode is not in cache, allocate a new inode and return it locked, | |
1281 | * hashed, and with the I_NEW flag set. The file system gets to fill it in | |
1282 | * before unlocking it via unlock_new_inode(). | |
1da177e4 | 1283 | * |
0b2d0724 CH |
1284 | * Note both @test and @set are called with the inode_hash_lock held, so can't |
1285 | * sleep. | |
1da177e4 | 1286 | */ |
0b2d0724 CH |
1287 | struct inode *iget5_locked(struct super_block *sb, unsigned long hashval, |
1288 | int (*test)(struct inode *, void *), | |
1289 | int (*set)(struct inode *, void *), void *data) | |
1da177e4 | 1290 | { |
80ea09a0 | 1291 | struct inode *inode = ilookup5(sb, hashval, test, data); |
0b2d0724 | 1292 | |
80ea09a0 | 1293 | if (!inode) { |
e950564b | 1294 | struct inode *new = alloc_inode(sb); |
0b2d0724 | 1295 | |
80ea09a0 MS |
1296 | if (new) { |
1297 | inode = inode_insert5(new, hashval, test, set, data); | |
1298 | if (unlikely(inode != new)) | |
e950564b | 1299 | destroy_inode(new); |
2864f301 | 1300 | } |
1da177e4 LT |
1301 | } |
1302 | return inode; | |
1da177e4 | 1303 | } |
0b2d0724 | 1304 | EXPORT_SYMBOL(iget5_locked); |
1da177e4 | 1305 | |
7180f8d9 MG |
1306 | /** |
1307 | * iget5_locked_rcu - obtain an inode from a mounted file system | |
1308 | * @sb: super block of file system | |
1309 | * @hashval: hash value (usually inode number) to get | |
1310 | * @test: callback used for comparisons between inodes | |
1311 | * @set: callback used to initialize a new struct inode | |
1312 | * @data: opaque data pointer to pass to @test and @set | |
1313 | * | |
1314 | * This is equivalent to iget5_locked, except the @test callback must | |
1315 | * tolerate the inode not being stable, including being mid-teardown. | |
1316 | */ | |
1317 | struct inode *iget5_locked_rcu(struct super_block *sb, unsigned long hashval, | |
1318 | int (*test)(struct inode *, void *), | |
1319 | int (*set)(struct inode *, void *), void *data) | |
1320 | { | |
1321 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); | |
1322 | struct inode *inode, *new; | |
1323 | ||
1324 | again: | |
1325 | inode = find_inode(sb, head, test, data, false); | |
1326 | if (inode) { | |
1327 | if (IS_ERR(inode)) | |
1328 | return NULL; | |
1329 | wait_on_inode(inode); | |
1330 | if (unlikely(inode_unhashed(inode))) { | |
1331 | iput(inode); | |
1332 | goto again; | |
1333 | } | |
1334 | return inode; | |
1335 | } | |
1336 | ||
1337 | new = alloc_inode(sb); | |
1338 | if (new) { | |
7180f8d9 MG |
1339 | inode = inode_insert5(new, hashval, test, set, data); |
1340 | if (unlikely(inode != new)) | |
1341 | destroy_inode(new); | |
1342 | } | |
1343 | return inode; | |
1344 | } | |
1345 | EXPORT_SYMBOL_GPL(iget5_locked_rcu); | |
1346 | ||
0b2d0724 CH |
1347 | /** |
1348 | * iget_locked - obtain an inode from a mounted file system | |
1349 | * @sb: super block of file system | |
1350 | * @ino: inode number to get | |
1351 | * | |
1352 | * Search for the inode specified by @ino in the inode cache and if present | |
1353 | * return it with an increased reference count. This is for file systems | |
1354 | * where the inode number is sufficient for unique identification of an inode. | |
1355 | * | |
1356 | * If the inode is not in cache, allocate a new inode and return it locked, | |
1357 | * hashed, and with the I_NEW flag set. The file system gets to fill it in | |
1358 | * before unlocking it via unlock_new_inode(). | |
1da177e4 | 1359 | */ |
0b2d0724 | 1360 | struct inode *iget_locked(struct super_block *sb, unsigned long ino) |
1da177e4 | 1361 | { |
0b2d0724 | 1362 | struct hlist_head *head = inode_hashtable + hash(sb, ino); |
6b3304b5 | 1363 | struct inode *inode; |
2864f301 | 1364 | again: |
7180f8d9 | 1365 | inode = find_inode_fast(sb, head, ino, false); |
0b2d0724 | 1366 | if (inode) { |
c2b6d621 AV |
1367 | if (IS_ERR(inode)) |
1368 | return NULL; | |
0b2d0724 | 1369 | wait_on_inode(inode); |
2864f301 AV |
1370 | if (unlikely(inode_unhashed(inode))) { |
1371 | iput(inode); | |
1372 | goto again; | |
1373 | } | |
0b2d0724 CH |
1374 | return inode; |
1375 | } | |
1376 | ||
1da177e4 LT |
1377 | inode = alloc_inode(sb); |
1378 | if (inode) { | |
6b3304b5 | 1379 | struct inode *old; |
1da177e4 | 1380 | |
67a23c49 | 1381 | spin_lock(&inode_hash_lock); |
1da177e4 | 1382 | /* We released the lock, so.. */ |
7180f8d9 | 1383 | old = find_inode_fast(sb, head, ino, true); |
1da177e4 LT |
1384 | if (!old) { |
1385 | inode->i_ino = ino; | |
250df6ed DC |
1386 | spin_lock(&inode->i_lock); |
1387 | inode->i_state = I_NEW; | |
3f19b2ab | 1388 | hlist_add_head_rcu(&inode->i_hash, head); |
250df6ed | 1389 | spin_unlock(&inode->i_lock); |
55fa6091 | 1390 | inode_sb_list_add(inode); |
67a23c49 | 1391 | spin_unlock(&inode_hash_lock); |
1da177e4 LT |
1392 | |
1393 | /* Return the locked inode with I_NEW set, the | |
1394 | * caller is responsible for filling in the contents | |
1395 | */ | |
1396 | return inode; | |
1397 | } | |
1398 | ||
1399 | /* | |
1400 | * Uhhuh, somebody else created the same inode under | |
1401 | * us. Use the old inode instead of the one we just | |
1402 | * allocated. | |
1403 | */ | |
67a23c49 | 1404 | spin_unlock(&inode_hash_lock); |
1da177e4 | 1405 | destroy_inode(inode); |
c2b6d621 AV |
1406 | if (IS_ERR(old)) |
1407 | return NULL; | |
1da177e4 LT |
1408 | inode = old; |
1409 | wait_on_inode(inode); | |
2864f301 AV |
1410 | if (unlikely(inode_unhashed(inode))) { |
1411 | iput(inode); | |
1412 | goto again; | |
1413 | } | |
1da177e4 LT |
1414 | } |
1415 | return inode; | |
1416 | } | |
0b2d0724 | 1417 | EXPORT_SYMBOL(iget_locked); |
1da177e4 | 1418 | |
ad5e195a CH |
1419 | /* |
1420 | * search the inode cache for a matching inode number. | |
1421 | * If we find one, then the inode number we are trying to | |
1422 | * allocate is not unique and so we should not use it. | |
1423 | * | |
1424 | * Returns 1 if the inode number is unique, 0 if it is not. | |
1425 | */ | |
1426 | static int test_inode_iunique(struct super_block *sb, unsigned long ino) | |
1427 | { | |
1428 | struct hlist_head *b = inode_hashtable + hash(sb, ino); | |
ad5e195a CH |
1429 | struct inode *inode; |
1430 | ||
3f19b2ab DH |
1431 | hlist_for_each_entry_rcu(inode, b, i_hash) { |
1432 | if (inode->i_ino == ino && inode->i_sb == sb) | |
ad5e195a CH |
1433 | return 0; |
1434 | } | |
ad5e195a CH |
1435 | return 1; |
1436 | } | |
1437 | ||
1da177e4 LT |
1438 | /** |
1439 | * iunique - get a unique inode number | |
1440 | * @sb: superblock | |
1441 | * @max_reserved: highest reserved inode number | |
1442 | * | |
1443 | * Obtain an inode number that is unique on the system for a given | |
1444 | * superblock. This is used by file systems that have no natural | |
1445 | * permanent inode numbering system. An inode number is returned that | |
1446 | * is higher than the reserved limit but unique. | |
1447 | * | |
1448 | * BUGS: | |
1449 | * With a large number of inodes live on the file system this function | |
1450 | * currently becomes quite slow. | |
1451 | */ | |
1452 | ino_t iunique(struct super_block *sb, ino_t max_reserved) | |
1453 | { | |
866b04fc JL |
1454 | /* |
1455 | * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW | |
1456 | * error if st_ino won't fit in target struct field. Use 32bit counter | |
1457 | * here to attempt to avoid that. | |
1458 | */ | |
ad5e195a | 1459 | static DEFINE_SPINLOCK(iunique_lock); |
866b04fc | 1460 | static unsigned int counter; |
1da177e4 | 1461 | ino_t res; |
3361c7be | 1462 | |
3f19b2ab | 1463 | rcu_read_lock(); |
ad5e195a | 1464 | spin_lock(&iunique_lock); |
3361c7be JL |
1465 | do { |
1466 | if (counter <= max_reserved) | |
1467 | counter = max_reserved + 1; | |
1da177e4 | 1468 | res = counter++; |
ad5e195a CH |
1469 | } while (!test_inode_iunique(sb, res)); |
1470 | spin_unlock(&iunique_lock); | |
3f19b2ab | 1471 | rcu_read_unlock(); |
1da177e4 | 1472 | |
3361c7be JL |
1473 | return res; |
1474 | } | |
1da177e4 LT |
1475 | EXPORT_SYMBOL(iunique); |
1476 | ||
1477 | struct inode *igrab(struct inode *inode) | |
1478 | { | |
250df6ed DC |
1479 | spin_lock(&inode->i_lock); |
1480 | if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) { | |
1da177e4 | 1481 | __iget(inode); |
250df6ed DC |
1482 | spin_unlock(&inode->i_lock); |
1483 | } else { | |
1484 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
1485 | /* |
1486 | * Handle the case where s_op->clear_inode is not been | |
1487 | * called yet, and somebody is calling igrab | |
1488 | * while the inode is getting freed. | |
1489 | */ | |
1490 | inode = NULL; | |
250df6ed | 1491 | } |
1da177e4 LT |
1492 | return inode; |
1493 | } | |
1da177e4 LT |
1494 | EXPORT_SYMBOL(igrab); |
1495 | ||
1496 | /** | |
0b2d0724 | 1497 | * ilookup5_nowait - search for an inode in the inode cache |
1da177e4 | 1498 | * @sb: super block of file system to search |
0b2d0724 | 1499 | * @hashval: hash value (usually inode number) to search for |
1da177e4 LT |
1500 | * @test: callback used for comparisons between inodes |
1501 | * @data: opaque data pointer to pass to @test | |
1da177e4 | 1502 | * |
0b2d0724 | 1503 | * Search for the inode specified by @hashval and @data in the inode cache. |
1da177e4 LT |
1504 | * If the inode is in the cache, the inode is returned with an incremented |
1505 | * reference count. | |
1506 | * | |
0b2d0724 CH |
1507 | * Note: I_NEW is not waited upon so you have to be very careful what you do |
1508 | * with the returned inode. You probably should be using ilookup5() instead. | |
1da177e4 | 1509 | * |
b6d0ad68 | 1510 | * Note2: @test is called with the inode_hash_lock held, so can't sleep. |
1da177e4 | 1511 | */ |
0b2d0724 CH |
1512 | struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, |
1513 | int (*test)(struct inode *, void *), void *data) | |
1da177e4 | 1514 | { |
0b2d0724 | 1515 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
1da177e4 LT |
1516 | struct inode *inode; |
1517 | ||
67a23c49 | 1518 | spin_lock(&inode_hash_lock); |
7180f8d9 | 1519 | inode = find_inode(sb, head, test, data, true); |
67a23c49 | 1520 | spin_unlock(&inode_hash_lock); |
88bd5121 | 1521 | |
c2b6d621 | 1522 | return IS_ERR(inode) ? NULL : inode; |
88bd5121 | 1523 | } |
88bd5121 AA |
1524 | EXPORT_SYMBOL(ilookup5_nowait); |
1525 | ||
1526 | /** | |
1527 | * ilookup5 - search for an inode in the inode cache | |
1528 | * @sb: super block of file system to search | |
1529 | * @hashval: hash value (usually inode number) to search for | |
1530 | * @test: callback used for comparisons between inodes | |
1531 | * @data: opaque data pointer to pass to @test | |
1532 | * | |
0b2d0724 CH |
1533 | * Search for the inode specified by @hashval and @data in the inode cache, |
1534 | * and if the inode is in the cache, return the inode with an incremented | |
1535 | * reference count. Waits on I_NEW before returning the inode. | |
88bd5121 | 1536 | * returned with an incremented reference count. |
1da177e4 | 1537 | * |
0b2d0724 CH |
1538 | * This is a generalized version of ilookup() for file systems where the |
1539 | * inode number is not sufficient for unique identification of an inode. | |
1da177e4 | 1540 | * |
0b2d0724 | 1541 | * Note: @test is called with the inode_hash_lock held, so can't sleep. |
1da177e4 LT |
1542 | */ |
1543 | struct inode *ilookup5(struct super_block *sb, unsigned long hashval, | |
1544 | int (*test)(struct inode *, void *), void *data) | |
1545 | { | |
2864f301 AV |
1546 | struct inode *inode; |
1547 | again: | |
1548 | inode = ilookup5_nowait(sb, hashval, test, data); | |
1549 | if (inode) { | |
0b2d0724 | 1550 | wait_on_inode(inode); |
2864f301 AV |
1551 | if (unlikely(inode_unhashed(inode))) { |
1552 | iput(inode); | |
1553 | goto again; | |
1554 | } | |
1555 | } | |
0b2d0724 | 1556 | return inode; |
1da177e4 | 1557 | } |
1da177e4 LT |
1558 | EXPORT_SYMBOL(ilookup5); |
1559 | ||
1560 | /** | |
1561 | * ilookup - search for an inode in the inode cache | |
1562 | * @sb: super block of file system to search | |
1563 | * @ino: inode number to search for | |
1564 | * | |
0b2d0724 CH |
1565 | * Search for the inode @ino in the inode cache, and if the inode is in the |
1566 | * cache, the inode is returned with an incremented reference count. | |
1da177e4 LT |
1567 | */ |
1568 | struct inode *ilookup(struct super_block *sb, unsigned long ino) | |
1569 | { | |
1570 | struct hlist_head *head = inode_hashtable + hash(sb, ino); | |
1da177e4 | 1571 | struct inode *inode; |
2864f301 | 1572 | again: |
0b2d0724 | 1573 | spin_lock(&inode_hash_lock); |
7180f8d9 | 1574 | inode = find_inode_fast(sb, head, ino, true); |
0b2d0724 | 1575 | spin_unlock(&inode_hash_lock); |
1da177e4 | 1576 | |
2864f301 | 1577 | if (inode) { |
c2b6d621 AV |
1578 | if (IS_ERR(inode)) |
1579 | return NULL; | |
0b2d0724 | 1580 | wait_on_inode(inode); |
2864f301 AV |
1581 | if (unlikely(inode_unhashed(inode))) { |
1582 | iput(inode); | |
1583 | goto again; | |
1584 | } | |
1585 | } | |
0b2d0724 | 1586 | return inode; |
1da177e4 | 1587 | } |
0b2d0724 | 1588 | EXPORT_SYMBOL(ilookup); |
1da177e4 | 1589 | |
fe032c42 TT |
1590 | /** |
1591 | * find_inode_nowait - find an inode in the inode cache | |
1592 | * @sb: super block of file system to search | |
1593 | * @hashval: hash value (usually inode number) to search for | |
1594 | * @match: callback used for comparisons between inodes | |
1595 | * @data: opaque data pointer to pass to @match | |
1596 | * | |
1597 | * Search for the inode specified by @hashval and @data in the inode | |
1598 | * cache, where the helper function @match will return 0 if the inode | |
1599 | * does not match, 1 if the inode does match, and -1 if the search | |
1600 | * should be stopped. The @match function must be responsible for | |
1601 | * taking the i_lock spin_lock and checking i_state for an inode being | |
1602 | * freed or being initialized, and incrementing the reference count | |
1603 | * before returning 1. It also must not sleep, since it is called with | |
1604 | * the inode_hash_lock spinlock held. | |
1605 | * | |
1606 | * This is a even more generalized version of ilookup5() when the | |
1607 | * function must never block --- find_inode() can block in | |
1608 | * __wait_on_freeing_inode() --- or when the caller can not increment | |
1609 | * the reference count because the resulting iput() might cause an | |
1610 | * inode eviction. The tradeoff is that the @match funtion must be | |
1611 | * very carefully implemented. | |
1612 | */ | |
1613 | struct inode *find_inode_nowait(struct super_block *sb, | |
1614 | unsigned long hashval, | |
1615 | int (*match)(struct inode *, unsigned long, | |
1616 | void *), | |
1617 | void *data) | |
1618 | { | |
1619 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); | |
1620 | struct inode *inode, *ret_inode = NULL; | |
1621 | int mval; | |
1622 | ||
1623 | spin_lock(&inode_hash_lock); | |
1624 | hlist_for_each_entry(inode, head, i_hash) { | |
1625 | if (inode->i_sb != sb) | |
1626 | continue; | |
1627 | mval = match(inode, hashval, data); | |
1628 | if (mval == 0) | |
1629 | continue; | |
1630 | if (mval == 1) | |
1631 | ret_inode = inode; | |
1632 | goto out; | |
1633 | } | |
1634 | out: | |
1635 | spin_unlock(&inode_hash_lock); | |
1636 | return ret_inode; | |
1637 | } | |
1638 | EXPORT_SYMBOL(find_inode_nowait); | |
1639 | ||
3f19b2ab DH |
1640 | /** |
1641 | * find_inode_rcu - find an inode in the inode cache | |
1642 | * @sb: Super block of file system to search | |
1643 | * @hashval: Key to hash | |
1644 | * @test: Function to test match on an inode | |
1645 | * @data: Data for test function | |
1646 | * | |
1647 | * Search for the inode specified by @hashval and @data in the inode cache, | |
1648 | * where the helper function @test will return 0 if the inode does not match | |
1649 | * and 1 if it does. The @test function must be responsible for taking the | |
1650 | * i_lock spin_lock and checking i_state for an inode being freed or being | |
1651 | * initialized. | |
1652 | * | |
1653 | * If successful, this will return the inode for which the @test function | |
1654 | * returned 1 and NULL otherwise. | |
1655 | * | |
1656 | * The @test function is not permitted to take a ref on any inode presented. | |
1657 | * It is also not permitted to sleep. | |
1658 | * | |
1659 | * The caller must hold the RCU read lock. | |
1660 | */ | |
1661 | struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval, | |
1662 | int (*test)(struct inode *, void *), void *data) | |
1663 | { | |
1664 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); | |
1665 | struct inode *inode; | |
1666 | ||
1667 | RCU_LOCKDEP_WARN(!rcu_read_lock_held(), | |
1668 | "suspicious find_inode_rcu() usage"); | |
1669 | ||
1670 | hlist_for_each_entry_rcu(inode, head, i_hash) { | |
1671 | if (inode->i_sb == sb && | |
1672 | !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) && | |
1673 | test(inode, data)) | |
1674 | return inode; | |
1675 | } | |
1676 | return NULL; | |
1677 | } | |
1678 | EXPORT_SYMBOL(find_inode_rcu); | |
1679 | ||
1680 | /** | |
961f3c89 | 1681 | * find_inode_by_ino_rcu - Find an inode in the inode cache |
3f19b2ab DH |
1682 | * @sb: Super block of file system to search |
1683 | * @ino: The inode number to match | |
1684 | * | |
1685 | * Search for the inode specified by @hashval and @data in the inode cache, | |
1686 | * where the helper function @test will return 0 if the inode does not match | |
1687 | * and 1 if it does. The @test function must be responsible for taking the | |
1688 | * i_lock spin_lock and checking i_state for an inode being freed or being | |
1689 | * initialized. | |
1690 | * | |
1691 | * If successful, this will return the inode for which the @test function | |
1692 | * returned 1 and NULL otherwise. | |
1693 | * | |
1694 | * The @test function is not permitted to take a ref on any inode presented. | |
1695 | * It is also not permitted to sleep. | |
1696 | * | |
1697 | * The caller must hold the RCU read lock. | |
1698 | */ | |
1699 | struct inode *find_inode_by_ino_rcu(struct super_block *sb, | |
1700 | unsigned long ino) | |
1701 | { | |
1702 | struct hlist_head *head = inode_hashtable + hash(sb, ino); | |
1703 | struct inode *inode; | |
1704 | ||
1705 | RCU_LOCKDEP_WARN(!rcu_read_lock_held(), | |
1706 | "suspicious find_inode_by_ino_rcu() usage"); | |
1707 | ||
1708 | hlist_for_each_entry_rcu(inode, head, i_hash) { | |
1709 | if (inode->i_ino == ino && | |
1710 | inode->i_sb == sb && | |
1711 | !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE))) | |
1712 | return inode; | |
1713 | } | |
1714 | return NULL; | |
1715 | } | |
1716 | EXPORT_SYMBOL(find_inode_by_ino_rcu); | |
1717 | ||
261bca86 AV |
1718 | int insert_inode_locked(struct inode *inode) |
1719 | { | |
1720 | struct super_block *sb = inode->i_sb; | |
1721 | ino_t ino = inode->i_ino; | |
1722 | struct hlist_head *head = inode_hashtable + hash(sb, ino); | |
261bca86 | 1723 | |
261bca86 | 1724 | while (1) { |
72a43d63 | 1725 | struct inode *old = NULL; |
67a23c49 | 1726 | spin_lock(&inode_hash_lock); |
b67bfe0d | 1727 | hlist_for_each_entry(old, head, i_hash) { |
72a43d63 AV |
1728 | if (old->i_ino != ino) |
1729 | continue; | |
1730 | if (old->i_sb != sb) | |
1731 | continue; | |
250df6ed DC |
1732 | spin_lock(&old->i_lock); |
1733 | if (old->i_state & (I_FREEING|I_WILL_FREE)) { | |
1734 | spin_unlock(&old->i_lock); | |
72a43d63 | 1735 | continue; |
250df6ed | 1736 | } |
72a43d63 AV |
1737 | break; |
1738 | } | |
b67bfe0d | 1739 | if (likely(!old)) { |
250df6ed | 1740 | spin_lock(&inode->i_lock); |
c2b6d621 | 1741 | inode->i_state |= I_NEW | I_CREATING; |
3f19b2ab | 1742 | hlist_add_head_rcu(&inode->i_hash, head); |
250df6ed | 1743 | spin_unlock(&inode->i_lock); |
67a23c49 | 1744 | spin_unlock(&inode_hash_lock); |
261bca86 AV |
1745 | return 0; |
1746 | } | |
c2b6d621 AV |
1747 | if (unlikely(old->i_state & I_CREATING)) { |
1748 | spin_unlock(&old->i_lock); | |
1749 | spin_unlock(&inode_hash_lock); | |
1750 | return -EBUSY; | |
1751 | } | |
261bca86 | 1752 | __iget(old); |
250df6ed | 1753 | spin_unlock(&old->i_lock); |
67a23c49 | 1754 | spin_unlock(&inode_hash_lock); |
261bca86 | 1755 | wait_on_inode(old); |
1d3382cb | 1756 | if (unlikely(!inode_unhashed(old))) { |
261bca86 AV |
1757 | iput(old); |
1758 | return -EBUSY; | |
1759 | } | |
1760 | iput(old); | |
1761 | } | |
1762 | } | |
261bca86 AV |
1763 | EXPORT_SYMBOL(insert_inode_locked); |
1764 | ||
1765 | int insert_inode_locked4(struct inode *inode, unsigned long hashval, | |
1766 | int (*test)(struct inode *, void *), void *data) | |
1767 | { | |
c2b6d621 AV |
1768 | struct inode *old; |
1769 | ||
1770 | inode->i_state |= I_CREATING; | |
1771 | old = inode_insert5(inode, hashval, test, NULL, data); | |
261bca86 | 1772 | |
80ea09a0 | 1773 | if (old != inode) { |
261bca86 | 1774 | iput(old); |
80ea09a0 | 1775 | return -EBUSY; |
261bca86 | 1776 | } |
80ea09a0 | 1777 | return 0; |
261bca86 | 1778 | } |
261bca86 AV |
1779 | EXPORT_SYMBOL(insert_inode_locked4); |
1780 | ||
1da177e4 | 1781 | |
45321ac5 AV |
1782 | int generic_delete_inode(struct inode *inode) |
1783 | { | |
1784 | return 1; | |
1785 | } | |
1786 | EXPORT_SYMBOL(generic_delete_inode); | |
1787 | ||
45321ac5 AV |
1788 | /* |
1789 | * Called when we're dropping the last reference | |
1790 | * to an inode. | |
22fe4042 | 1791 | * |
45321ac5 AV |
1792 | * Call the FS "drop_inode()" function, defaulting to |
1793 | * the legacy UNIX filesystem behaviour. If it tells | |
1794 | * us to evict inode, do so. Otherwise, retain inode | |
1795 | * in cache if fs is alive, sync and evict if fs is | |
1796 | * shutting down. | |
22fe4042 | 1797 | */ |
45321ac5 | 1798 | static void iput_final(struct inode *inode) |
1da177e4 LT |
1799 | { |
1800 | struct super_block *sb = inode->i_sb; | |
45321ac5 | 1801 | const struct super_operations *op = inode->i_sb->s_op; |
3f19b2ab | 1802 | unsigned long state; |
45321ac5 AV |
1803 | int drop; |
1804 | ||
250df6ed DC |
1805 | WARN_ON(inode->i_state & I_NEW); |
1806 | ||
e7f59097 | 1807 | if (op->drop_inode) |
45321ac5 AV |
1808 | drop = op->drop_inode(inode); |
1809 | else | |
1810 | drop = generic_drop_inode(inode); | |
1da177e4 | 1811 | |
88149082 HL |
1812 | if (!drop && |
1813 | !(inode->i_state & I_DONTCACHE) && | |
1814 | (sb->s_flags & SB_ACTIVE)) { | |
51b8c1fe | 1815 | __inode_add_lru(inode, true); |
b2b2af8e | 1816 | spin_unlock(&inode->i_lock); |
b2b2af8e DC |
1817 | return; |
1818 | } | |
1819 | ||
3f19b2ab | 1820 | state = inode->i_state; |
45321ac5 | 1821 | if (!drop) { |
3f19b2ab | 1822 | WRITE_ONCE(inode->i_state, state | I_WILL_FREE); |
250df6ed | 1823 | spin_unlock(&inode->i_lock); |
3f19b2ab | 1824 | |
1da177e4 | 1825 | write_inode_now(inode, 1); |
3f19b2ab | 1826 | |
250df6ed | 1827 | spin_lock(&inode->i_lock); |
3f19b2ab DH |
1828 | state = inode->i_state; |
1829 | WARN_ON(state & I_NEW); | |
1830 | state &= ~I_WILL_FREE; | |
1da177e4 | 1831 | } |
7ccf19a8 | 1832 | |
3f19b2ab | 1833 | WRITE_ONCE(inode->i_state, state | I_FREEING); |
c4ae0c65 ED |
1834 | if (!list_empty(&inode->i_lru)) |
1835 | inode_lru_list_del(inode); | |
b2b2af8e | 1836 | spin_unlock(&inode->i_lock); |
b2b2af8e | 1837 | |
644da596 | 1838 | evict(inode); |
1da177e4 LT |
1839 | } |
1840 | ||
1da177e4 | 1841 | /** |
6b3304b5 | 1842 | * iput - put an inode |
1da177e4 LT |
1843 | * @inode: inode to put |
1844 | * | |
1845 | * Puts an inode, dropping its usage count. If the inode use count hits | |
1846 | * zero, the inode is then freed and may also be destroyed. | |
1847 | * | |
1848 | * Consequently, iput() can sleep. | |
1849 | */ | |
1850 | void iput(struct inode *inode) | |
1851 | { | |
0ae45f63 TT |
1852 | if (!inode) |
1853 | return; | |
1854 | BUG_ON(inode->i_state & I_CLEAR); | |
1855 | retry: | |
1856 | if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) { | |
1857 | if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) { | |
1858 | atomic_inc(&inode->i_count); | |
0ae45f63 TT |
1859 | spin_unlock(&inode->i_lock); |
1860 | trace_writeback_lazytime_iput(inode); | |
1861 | mark_inode_dirty_sync(inode); | |
1862 | goto retry; | |
1863 | } | |
1864 | iput_final(inode); | |
1da177e4 LT |
1865 | } |
1866 | } | |
1da177e4 LT |
1867 | EXPORT_SYMBOL(iput); |
1868 | ||
30460e1e | 1869 | #ifdef CONFIG_BLOCK |
1da177e4 LT |
1870 | /** |
1871 | * bmap - find a block number in a file | |
30460e1e CM |
1872 | * @inode: inode owning the block number being requested |
1873 | * @block: pointer containing the block to find | |
1da177e4 | 1874 | * |
2b8e8b55 | 1875 | * Replaces the value in ``*block`` with the block number on the device holding |
30460e1e CM |
1876 | * corresponding to the requested block number in the file. |
1877 | * That is, asked for block 4 of inode 1 the function will replace the | |
2b8e8b55 | 1878 | * 4 in ``*block``, with disk block relative to the disk start that holds that |
30460e1e CM |
1879 | * block of the file. |
1880 | * | |
1881 | * Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a | |
2b8e8b55 | 1882 | * hole, returns 0 and ``*block`` is also set to 0. |
1da177e4 | 1883 | */ |
30460e1e | 1884 | int bmap(struct inode *inode, sector_t *block) |
1da177e4 | 1885 | { |
30460e1e CM |
1886 | if (!inode->i_mapping->a_ops->bmap) |
1887 | return -EINVAL; | |
1888 | ||
1889 | *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block); | |
1890 | return 0; | |
1da177e4 | 1891 | } |
1da177e4 | 1892 | EXPORT_SYMBOL(bmap); |
30460e1e | 1893 | #endif |
1da177e4 | 1894 | |
11ff6f05 MG |
1895 | /* |
1896 | * With relative atime, only update atime if the previous atime is | |
d98ffa1a SK |
1897 | * earlier than or equal to either the ctime or mtime, |
1898 | * or if at least a day has passed since the last atime update. | |
11ff6f05 | 1899 | */ |
effa1870 | 1900 | static bool relatime_need_update(struct vfsmount *mnt, struct inode *inode, |
6f22b664 | 1901 | struct timespec64 now) |
11ff6f05 | 1902 | { |
16a94965 | 1903 | struct timespec64 atime, mtime, ctime; |
11ff6f05 | 1904 | |
c6718543 | 1905 | if (!(mnt->mnt_flags & MNT_RELATIME)) |
effa1870 | 1906 | return true; |
11ff6f05 | 1907 | /* |
d98ffa1a | 1908 | * Is mtime younger than or equal to atime? If yes, update atime: |
11ff6f05 | 1909 | */ |
16a94965 JL |
1910 | atime = inode_get_atime(inode); |
1911 | mtime = inode_get_mtime(inode); | |
1912 | if (timespec64_compare(&mtime, &atime) >= 0) | |
effa1870 | 1913 | return true; |
11ff6f05 | 1914 | /* |
d98ffa1a | 1915 | * Is ctime younger than or equal to atime? If yes, update atime: |
11ff6f05 | 1916 | */ |
2276e5ba | 1917 | ctime = inode_get_ctime(inode); |
16a94965 | 1918 | if (timespec64_compare(&ctime, &atime) >= 0) |
effa1870 | 1919 | return true; |
11ff6f05 MG |
1920 | |
1921 | /* | |
1922 | * Is the previous atime value older than a day? If yes, | |
1923 | * update atime: | |
1924 | */ | |
16a94965 | 1925 | if ((long)(now.tv_sec - atime.tv_sec) >= 24*60*60) |
effa1870 | 1926 | return true; |
11ff6f05 MG |
1927 | /* |
1928 | * Good, we can skip the atime update: | |
1929 | */ | |
effa1870 | 1930 | return false; |
11ff6f05 MG |
1931 | } |
1932 | ||
541d4c79 JL |
1933 | /** |
1934 | * inode_update_timestamps - update the timestamps on the inode | |
1935 | * @inode: inode to be updated | |
1936 | * @flags: S_* flags that needed to be updated | |
1937 | * | |
1938 | * The update_time function is called when an inode's timestamps need to be | |
1939 | * updated for a read or write operation. This function handles updating the | |
1940 | * actual timestamps. It's up to the caller to ensure that the inode is marked | |
1941 | * dirty appropriately. | |
1942 | * | |
1943 | * In the case where any of S_MTIME, S_CTIME, or S_VERSION need to be updated, | |
1944 | * attempt to update all three of them. S_ATIME updates can be handled | |
1945 | * independently of the rest. | |
1946 | * | |
1947 | * Returns a set of S_* flags indicating which values changed. | |
1948 | */ | |
1949 | int inode_update_timestamps(struct inode *inode, int flags) | |
c3b2da31 | 1950 | { |
541d4c79 JL |
1951 | int updated = 0; |
1952 | struct timespec64 now; | |
e20b14db | 1953 | |
541d4c79 JL |
1954 | if (flags & (S_MTIME|S_CTIME|S_VERSION)) { |
1955 | struct timespec64 ctime = inode_get_ctime(inode); | |
16a94965 | 1956 | struct timespec64 mtime = inode_get_mtime(inode); |
e20b14db | 1957 | |
541d4c79 JL |
1958 | now = inode_set_ctime_current(inode); |
1959 | if (!timespec64_equal(&now, &ctime)) | |
1960 | updated |= S_CTIME; | |
16a94965 JL |
1961 | if (!timespec64_equal(&now, &mtime)) { |
1962 | inode_set_mtime_to_ts(inode, now); | |
541d4c79 JL |
1963 | updated |= S_MTIME; |
1964 | } | |
1965 | if (IS_I_VERSION(inode) && inode_maybe_inc_iversion(inode, updated)) | |
1966 | updated |= S_VERSION; | |
1967 | } else { | |
1968 | now = current_time(inode); | |
e20b14db EB |
1969 | } |
1970 | ||
541d4c79 | 1971 | if (flags & S_ATIME) { |
16a94965 JL |
1972 | struct timespec64 atime = inode_get_atime(inode); |
1973 | ||
1974 | if (!timespec64_equal(&now, &atime)) { | |
1975 | inode_set_atime_to_ts(inode, now); | |
541d4c79 JL |
1976 | updated |= S_ATIME; |
1977 | } | |
1978 | } | |
1979 | return updated; | |
1980 | } | |
1981 | EXPORT_SYMBOL(inode_update_timestamps); | |
e20b14db | 1982 | |
541d4c79 JL |
1983 | /** |
1984 | * generic_update_time - update the timestamps on the inode | |
1985 | * @inode: inode to be updated | |
1986 | * @flags: S_* flags that needed to be updated | |
1987 | * | |
1988 | * The update_time function is called when an inode's timestamps need to be | |
1989 | * updated for a read or write operation. In the case where any of S_MTIME, S_CTIME, | |
1990 | * or S_VERSION need to be updated we attempt to update all three of them. S_ATIME | |
1991 | * updates can be handled done independently of the rest. | |
1992 | * | |
1993 | * Returns a S_* mask indicating which fields were updated. | |
1994 | */ | |
1995 | int generic_update_time(struct inode *inode, int flags) | |
1996 | { | |
1997 | int updated = inode_update_timestamps(inode, flags); | |
1998 | int dirty_flags = 0; | |
e20b14db | 1999 | |
541d4c79 JL |
2000 | if (updated & (S_ATIME|S_MTIME|S_CTIME)) |
2001 | dirty_flags = inode->i_sb->s_flags & SB_LAZYTIME ? I_DIRTY_TIME : I_DIRTY_SYNC; | |
2002 | if (updated & S_VERSION) | |
2003 | dirty_flags |= I_DIRTY_SYNC; | |
e20b14db | 2004 | __mark_inode_dirty(inode, dirty_flags); |
541d4c79 | 2005 | return updated; |
c3b2da31 | 2006 | } |
0ae45f63 TT |
2007 | EXPORT_SYMBOL(generic_update_time); |
2008 | ||
2009 | /* | |
2010 | * This does the actual work of updating an inodes time or version. Must have | |
2011 | * had called mnt_want_write() before calling this. | |
2012 | */ | |
913e9928 | 2013 | int inode_update_time(struct inode *inode, int flags) |
0ae45f63 | 2014 | { |
23b424d9 | 2015 | if (inode->i_op->update_time) |
913e9928 | 2016 | return inode->i_op->update_time(inode, flags); |
541d4c79 JL |
2017 | generic_update_time(inode, flags); |
2018 | return 0; | |
0ae45f63 | 2019 | } |
e60feb44 | 2020 | EXPORT_SYMBOL(inode_update_time); |
c3b2da31 | 2021 | |
1da177e4 | 2022 | /** |
961f3c89 | 2023 | * atime_needs_update - update the access time |
185553b2 | 2024 | * @path: the &struct path to update |
30fdc8ee | 2025 | * @inode: inode to update |
1da177e4 LT |
2026 | * |
2027 | * Update the accessed time on an inode and mark it for writeback. | |
2028 | * This function automatically handles read only file systems and media, | |
2029 | * as well as the "noatime" flag and inode specific "noatime" markers. | |
2030 | */ | |
c6718543 | 2031 | bool atime_needs_update(const struct path *path, struct inode *inode) |
1da177e4 | 2032 | { |
68ac1234 | 2033 | struct vfsmount *mnt = path->mnt; |
16a94965 | 2034 | struct timespec64 now, atime; |
1da177e4 | 2035 | |
cdb70f3f | 2036 | if (inode->i_flags & S_NOATIME) |
8fa9dd24 | 2037 | return false; |
0bd23d09 EB |
2038 | |
2039 | /* Atime updates will likely cause i_uid and i_gid to be written | |
2040 | * back improprely if their true value is unknown to the vfs. | |
2041 | */ | |
4609e1f1 | 2042 | if (HAS_UNMAPPED_ID(mnt_idmap(mnt), inode)) |
0bd23d09 EB |
2043 | return false; |
2044 | ||
37756ced | 2045 | if (IS_NOATIME(inode)) |
8fa9dd24 | 2046 | return false; |
1751e8a6 | 2047 | if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode)) |
8fa9dd24 | 2048 | return false; |
47ae32d6 | 2049 | |
cdb70f3f | 2050 | if (mnt->mnt_flags & MNT_NOATIME) |
8fa9dd24 | 2051 | return false; |
cdb70f3f | 2052 | if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) |
8fa9dd24 | 2053 | return false; |
1da177e4 | 2054 | |
c2050a45 | 2055 | now = current_time(inode); |
11ff6f05 | 2056 | |
6f22b664 | 2057 | if (!relatime_need_update(mnt, inode, now)) |
8fa9dd24 | 2058 | return false; |
11ff6f05 | 2059 | |
16a94965 JL |
2060 | atime = inode_get_atime(inode); |
2061 | if (timespec64_equal(&atime, &now)) | |
8fa9dd24 N |
2062 | return false; |
2063 | ||
2064 | return true; | |
2065 | } | |
2066 | ||
2067 | void touch_atime(const struct path *path) | |
2068 | { | |
2069 | struct vfsmount *mnt = path->mnt; | |
2070 | struct inode *inode = d_inode(path->dentry); | |
8fa9dd24 | 2071 | |
c6718543 | 2072 | if (!atime_needs_update(path, inode)) |
b12536c2 AK |
2073 | return; |
2074 | ||
5d37e9e6 | 2075 | if (!sb_start_write_trylock(inode->i_sb)) |
b12536c2 | 2076 | return; |
47ae32d6 | 2077 | |
3e15dcf7 | 2078 | if (mnt_get_write_access(mnt) != 0) |
5d37e9e6 | 2079 | goto skip_update; |
c3b2da31 JB |
2080 | /* |
2081 | * File systems can error out when updating inodes if they need to | |
2082 | * allocate new space to modify an inode (such is the case for | |
2083 | * Btrfs), but since we touch atime while walking down the path we | |
2084 | * really don't care if we failed to update the atime of the file, | |
2085 | * so just ignore the return value. | |
2bc55652 AB |
2086 | * We may also fail on filesystems that have the ability to make parts |
2087 | * of the fs read only, e.g. subvolumes in Btrfs. | |
c3b2da31 | 2088 | */ |
913e9928 | 2089 | inode_update_time(inode, S_ATIME); |
3e15dcf7 | 2090 | mnt_put_write_access(mnt); |
5d37e9e6 JK |
2091 | skip_update: |
2092 | sb_end_write(inode->i_sb); | |
1da177e4 | 2093 | } |
869243a0 | 2094 | EXPORT_SYMBOL(touch_atime); |
1da177e4 | 2095 | |
dbfae0cd JK |
2096 | /* |
2097 | * Return mask of changes for notify_change() that need to be done as a | |
2098 | * response to write or truncate. Return 0 if nothing has to be changed. | |
2099 | * Negative value on error (change should be denied). | |
2100 | */ | |
9452e93e | 2101 | int dentry_needs_remove_privs(struct mnt_idmap *idmap, |
ed5a7047 | 2102 | struct dentry *dentry) |
dbfae0cd | 2103 | { |
dbfae0cd JK |
2104 | struct inode *inode = d_inode(dentry); |
2105 | int mask = 0; | |
2106 | int ret; | |
2107 | ||
2108 | if (IS_NOSEC(inode)) | |
2109 | return 0; | |
2110 | ||
9452e93e | 2111 | mask = setattr_should_drop_suidgid(idmap, inode); |
dbfae0cd JK |
2112 | ret = security_inode_need_killpriv(dentry); |
2113 | if (ret < 0) | |
2114 | return ret; | |
2115 | if (ret) | |
2116 | mask |= ATTR_KILL_PRIV; | |
2117 | return mask; | |
2118 | } | |
dbfae0cd | 2119 | |
abf08576 | 2120 | static int __remove_privs(struct mnt_idmap *idmap, |
643fe55a | 2121 | struct dentry *dentry, int kill) |
3ed37648 CW |
2122 | { |
2123 | struct iattr newattrs; | |
2124 | ||
2125 | newattrs.ia_valid = ATTR_FORCE | kill; | |
27ac0ffe BF |
2126 | /* |
2127 | * Note we call this on write, so notify_change will not | |
2128 | * encounter any conflicting delegations: | |
2129 | */ | |
abf08576 | 2130 | return notify_change(idmap, dentry, &newattrs, NULL); |
3ed37648 CW |
2131 | } |
2132 | ||
66a67c86 | 2133 | int file_remove_privs_flags(struct file *file, unsigned int flags) |
3ed37648 | 2134 | { |
c1892c37 MS |
2135 | struct dentry *dentry = file_dentry(file); |
2136 | struct inode *inode = file_inode(file); | |
41191cf6 | 2137 | int error = 0; |
dbfae0cd | 2138 | int kill; |
3ed37648 | 2139 | |
f69e749a | 2140 | if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode)) |
3ed37648 CW |
2141 | return 0; |
2142 | ||
9452e93e | 2143 | kill = dentry_needs_remove_privs(file_mnt_idmap(file), dentry); |
41191cf6 | 2144 | if (kill < 0) |
dbfae0cd | 2145 | return kill; |
faf99b56 | 2146 | |
41191cf6 SR |
2147 | if (kill) { |
2148 | if (flags & IOCB_NOWAIT) | |
2149 | return -EAGAIN; | |
2150 | ||
abf08576 | 2151 | error = __remove_privs(file_mnt_idmap(file), dentry, kill); |
41191cf6 | 2152 | } |
faf99b56 | 2153 | |
2426f391 JK |
2154 | if (!error) |
2155 | inode_has_no_xattr(inode); | |
3ed37648 CW |
2156 | return error; |
2157 | } | |
66a67c86 | 2158 | EXPORT_SYMBOL_GPL(file_remove_privs_flags); |
faf99b56 SR |
2159 | |
2160 | /** | |
2161 | * file_remove_privs - remove special file privileges (suid, capabilities) | |
2162 | * @file: file to remove privileges from | |
2163 | * | |
2164 | * When file is modified by a write or truncation ensure that special | |
2165 | * file privileges are removed. | |
2166 | * | |
2167 | * Return: 0 on success, negative errno on failure. | |
2168 | */ | |
2169 | int file_remove_privs(struct file *file) | |
2170 | { | |
66a67c86 | 2171 | return file_remove_privs_flags(file, 0); |
faf99b56 | 2172 | } |
5fa8e0a1 | 2173 | EXPORT_SYMBOL(file_remove_privs); |
3ed37648 | 2174 | |
913e9928 | 2175 | static int inode_needs_update_time(struct inode *inode) |
1da177e4 | 2176 | { |
c3b2da31 | 2177 | int sync_it = 0; |
647aa768 | 2178 | struct timespec64 now = current_time(inode); |
16a94965 | 2179 | struct timespec64 ts; |
1da177e4 | 2180 | |
ce06e0b2 | 2181 | /* First try to exhaust all avenues to not sync */ |
1da177e4 | 2182 | if (IS_NOCMTIME(inode)) |
c3b2da31 | 2183 | return 0; |
20ddee2c | 2184 | |
16a94965 JL |
2185 | ts = inode_get_mtime(inode); |
2186 | if (!timespec64_equal(&ts, &now)) | |
ce06e0b2 | 2187 | sync_it = S_MTIME; |
1da177e4 | 2188 | |
16a94965 JL |
2189 | ts = inode_get_ctime(inode); |
2190 | if (!timespec64_equal(&ts, &now)) | |
ce06e0b2 | 2191 | sync_it |= S_CTIME; |
870f4817 | 2192 | |
e38cf302 | 2193 | if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode)) |
ce06e0b2 | 2194 | sync_it |= S_VERSION; |
7a224228 | 2195 | |
6a2aa5d8 SR |
2196 | return sync_it; |
2197 | } | |
2198 | ||
913e9928 | 2199 | static int __file_update_time(struct file *file, int sync_mode) |
6a2aa5d8 SR |
2200 | { |
2201 | int ret = 0; | |
2202 | struct inode *inode = file_inode(file); | |
ce06e0b2 | 2203 | |
6a2aa5d8 | 2204 | /* try to update time settings */ |
3e15dcf7 | 2205 | if (!mnt_get_write_access_file(file)) { |
913e9928 | 2206 | ret = inode_update_time(inode, sync_mode); |
3e15dcf7 | 2207 | mnt_put_write_access_file(file); |
6a2aa5d8 | 2208 | } |
c3b2da31 JB |
2209 | |
2210 | return ret; | |
1da177e4 | 2211 | } |
6a2aa5d8 SR |
2212 | |
2213 | /** | |
2214 | * file_update_time - update mtime and ctime time | |
2215 | * @file: file accessed | |
2216 | * | |
2217 | * Update the mtime and ctime members of an inode and mark the inode for | |
2218 | * writeback. Note that this function is meant exclusively for usage in | |
2219 | * the file write path of filesystems, and filesystems may choose to | |
2220 | * explicitly ignore updates via this function with the _NOCMTIME inode | |
2221 | * flag, e.g. for network filesystem where these imestamps are handled | |
2222 | * by the server. This can return an error for file systems who need to | |
2223 | * allocate space in order to update an inode. | |
2224 | * | |
2225 | * Return: 0 on success, negative errno on failure. | |
2226 | */ | |
2227 | int file_update_time(struct file *file) | |
2228 | { | |
2229 | int ret; | |
2230 | struct inode *inode = file_inode(file); | |
6a2aa5d8 | 2231 | |
913e9928 | 2232 | ret = inode_needs_update_time(inode); |
6a2aa5d8 SR |
2233 | if (ret <= 0) |
2234 | return ret; | |
2235 | ||
913e9928 | 2236 | return __file_update_time(file, ret); |
6a2aa5d8 | 2237 | } |
870f4817 | 2238 | EXPORT_SYMBOL(file_update_time); |
1da177e4 | 2239 | |
faf99b56 | 2240 | /** |
66fa3ced | 2241 | * file_modified_flags - handle mandated vfs changes when modifying a file |
faf99b56 | 2242 | * @file: file that was modified |
66fa3ced | 2243 | * @flags: kiocb flags |
faf99b56 SR |
2244 | * |
2245 | * When file has been modified ensure that special | |
2246 | * file privileges are removed and time settings are updated. | |
2247 | * | |
66fa3ced SR |
2248 | * If IOCB_NOWAIT is set, special file privileges will not be removed and |
2249 | * time settings will not be updated. It will return -EAGAIN. | |
2250 | * | |
faf99b56 SR |
2251 | * Context: Caller must hold the file's inode lock. |
2252 | * | |
2253 | * Return: 0 on success, negative errno on failure. | |
2254 | */ | |
66fa3ced | 2255 | static int file_modified_flags(struct file *file, int flags) |
e38f7f53 | 2256 | { |
faf99b56 | 2257 | int ret; |
6a2aa5d8 | 2258 | struct inode *inode = file_inode(file); |
e38f7f53 AG |
2259 | |
2260 | /* | |
2261 | * Clear the security bits if the process is not being run by root. | |
2262 | * This keeps people from modifying setuid and setgid binaries. | |
2263 | */ | |
66a67c86 | 2264 | ret = file_remove_privs_flags(file, flags); |
faf99b56 SR |
2265 | if (ret) |
2266 | return ret; | |
e38f7f53 AG |
2267 | |
2268 | if (unlikely(file->f_mode & FMODE_NOCMTIME)) | |
2269 | return 0; | |
2270 | ||
913e9928 | 2271 | ret = inode_needs_update_time(inode); |
6a2aa5d8 SR |
2272 | if (ret <= 0) |
2273 | return ret; | |
66fa3ced SR |
2274 | if (flags & IOCB_NOWAIT) |
2275 | return -EAGAIN; | |
6a2aa5d8 | 2276 | |
913e9928 | 2277 | return __file_update_time(file, ret); |
e38f7f53 | 2278 | } |
66fa3ced SR |
2279 | |
2280 | /** | |
2281 | * file_modified - handle mandated vfs changes when modifying a file | |
2282 | * @file: file that was modified | |
2283 | * | |
2284 | * When file has been modified ensure that special | |
2285 | * file privileges are removed and time settings are updated. | |
2286 | * | |
2287 | * Context: Caller must hold the file's inode lock. | |
2288 | * | |
2289 | * Return: 0 on success, negative errno on failure. | |
2290 | */ | |
2291 | int file_modified(struct file *file) | |
2292 | { | |
2293 | return file_modified_flags(file, 0); | |
2294 | } | |
e38f7f53 AG |
2295 | EXPORT_SYMBOL(file_modified); |
2296 | ||
66fa3ced SR |
2297 | /** |
2298 | * kiocb_modified - handle mandated vfs changes when modifying a file | |
2299 | * @iocb: iocb that was modified | |
2300 | * | |
2301 | * When file has been modified ensure that special | |
2302 | * file privileges are removed and time settings are updated. | |
2303 | * | |
2304 | * Context: Caller must hold the file's inode lock. | |
2305 | * | |
2306 | * Return: 0 on success, negative errno on failure. | |
2307 | */ | |
2308 | int kiocb_modified(struct kiocb *iocb) | |
2309 | { | |
2310 | return file_modified_flags(iocb->ki_filp, iocb->ki_flags); | |
2311 | } | |
2312 | EXPORT_SYMBOL_GPL(kiocb_modified); | |
2313 | ||
1da177e4 LT |
2314 | int inode_needs_sync(struct inode *inode) |
2315 | { | |
2316 | if (IS_SYNC(inode)) | |
2317 | return 1; | |
2318 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) | |
2319 | return 1; | |
2320 | return 0; | |
2321 | } | |
1da177e4 LT |
2322 | EXPORT_SYMBOL(inode_needs_sync); |
2323 | ||
1da177e4 | 2324 | /* |
168a9fd6 MS |
2325 | * If we try to find an inode in the inode hash while it is being |
2326 | * deleted, we have to wait until the filesystem completes its | |
2327 | * deletion before reporting that it isn't found. This function waits | |
2328 | * until the deletion _might_ have completed. Callers are responsible | |
2329 | * to recheck inode state. | |
2330 | * | |
eaff8079 | 2331 | * It doesn't matter if I_NEW is not set initially, a call to |
250df6ed DC |
2332 | * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list |
2333 | * will DTRT. | |
1da177e4 | 2334 | */ |
f5e5e97c | 2335 | static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked) |
1da177e4 LT |
2336 | { |
2337 | wait_queue_head_t *wq; | |
eaff8079 | 2338 | DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW); |
5bc9ad78 MG |
2339 | |
2340 | /* | |
2341 | * Handle racing against evict(), see that routine for more details. | |
2342 | */ | |
2343 | if (unlikely(inode_unhashed(inode))) { | |
f5e5e97c | 2344 | WARN_ON(is_inode_hash_locked); |
5bc9ad78 MG |
2345 | spin_unlock(&inode->i_lock); |
2346 | return; | |
2347 | } | |
2348 | ||
eaff8079 | 2349 | wq = bit_waitqueue(&inode->i_state, __I_NEW); |
21417136 | 2350 | prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); |
250df6ed | 2351 | spin_unlock(&inode->i_lock); |
7180f8d9 | 2352 | rcu_read_unlock(); |
f5e5e97c | 2353 | if (is_inode_hash_locked) |
7180f8d9 | 2354 | spin_unlock(&inode_hash_lock); |
1da177e4 | 2355 | schedule(); |
21417136 | 2356 | finish_wait(wq, &wait.wq_entry); |
f5e5e97c | 2357 | if (is_inode_hash_locked) |
7180f8d9 MG |
2358 | spin_lock(&inode_hash_lock); |
2359 | rcu_read_lock(); | |
1da177e4 LT |
2360 | } |
2361 | ||
1da177e4 LT |
2362 | static __initdata unsigned long ihash_entries; |
2363 | static int __init set_ihash_entries(char *str) | |
2364 | { | |
2365 | if (!str) | |
2366 | return 0; | |
2367 | ihash_entries = simple_strtoul(str, &str, 0); | |
2368 | return 1; | |
2369 | } | |
2370 | __setup("ihash_entries=", set_ihash_entries); | |
2371 | ||
2372 | /* | |
2373 | * Initialize the waitqueues and inode hash table. | |
2374 | */ | |
2375 | void __init inode_init_early(void) | |
2376 | { | |
1da177e4 LT |
2377 | /* If hashes are distributed across NUMA nodes, defer |
2378 | * hash allocation until vmalloc space is available. | |
2379 | */ | |
2380 | if (hashdist) | |
2381 | return; | |
2382 | ||
2383 | inode_hashtable = | |
2384 | alloc_large_system_hash("Inode-cache", | |
2385 | sizeof(struct hlist_head), | |
2386 | ihash_entries, | |
2387 | 14, | |
3d375d78 | 2388 | HASH_EARLY | HASH_ZERO, |
1da177e4 LT |
2389 | &i_hash_shift, |
2390 | &i_hash_mask, | |
31fe62b9 | 2391 | 0, |
1da177e4 | 2392 | 0); |
1da177e4 LT |
2393 | } |
2394 | ||
74bf17cf | 2395 | void __init inode_init(void) |
1da177e4 | 2396 | { |
1da177e4 | 2397 | /* inode slab cache */ |
b0196009 PJ |
2398 | inode_cachep = kmem_cache_create("inode_cache", |
2399 | sizeof(struct inode), | |
2400 | 0, | |
2401 | (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| | |
c997d683 | 2402 | SLAB_ACCOUNT), |
20c2df83 | 2403 | init_once); |
1da177e4 LT |
2404 | |
2405 | /* Hash may have been set up in inode_init_early */ | |
2406 | if (!hashdist) | |
2407 | return; | |
2408 | ||
2409 | inode_hashtable = | |
2410 | alloc_large_system_hash("Inode-cache", | |
2411 | sizeof(struct hlist_head), | |
2412 | ihash_entries, | |
2413 | 14, | |
3d375d78 | 2414 | HASH_ZERO, |
1da177e4 LT |
2415 | &i_hash_shift, |
2416 | &i_hash_mask, | |
31fe62b9 | 2417 | 0, |
1da177e4 | 2418 | 0); |
1da177e4 LT |
2419 | } |
2420 | ||
2421 | void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) | |
2422 | { | |
2423 | inode->i_mode = mode; | |
2424 | if (S_ISCHR(mode)) { | |
2425 | inode->i_fop = &def_chr_fops; | |
2426 | inode->i_rdev = rdev; | |
2427 | } else if (S_ISBLK(mode)) { | |
bda2795a CH |
2428 | if (IS_ENABLED(CONFIG_BLOCK)) |
2429 | inode->i_fop = &def_blk_fops; | |
1da177e4 LT |
2430 | inode->i_rdev = rdev; |
2431 | } else if (S_ISFIFO(mode)) | |
599a0ac1 | 2432 | inode->i_fop = &pipefifo_fops; |
1da177e4 | 2433 | else if (S_ISSOCK(mode)) |
bd9b51e7 | 2434 | ; /* leave it no_open_fops */ |
1da177e4 | 2435 | else |
af0d9ae8 MK |
2436 | printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for" |
2437 | " inode %s:%lu\n", mode, inode->i_sb->s_id, | |
2438 | inode->i_ino); | |
1da177e4 LT |
2439 | } |
2440 | EXPORT_SYMBOL(init_special_inode); | |
a1bd120d DM |
2441 | |
2442 | /** | |
eaae668d | 2443 | * inode_init_owner - Init uid,gid,mode for new inode according to posix standards |
f2d40141 | 2444 | * @idmap: idmap of the mount the inode was created from |
a1bd120d DM |
2445 | * @inode: New inode |
2446 | * @dir: Directory inode | |
2447 | * @mode: mode of the new inode | |
21cb47be | 2448 | * |
f2d40141 CB |
2449 | * If the inode has been created through an idmapped mount the idmap of |
2450 | * the vfsmount must be passed through @idmap. This function will then take | |
2451 | * care to map the inode according to @idmap before checking permissions | |
21cb47be | 2452 | * and initializing i_uid and i_gid. On non-idmapped mounts or if permission |
f2d40141 | 2453 | * checking is to be performed on the raw inode simply pass @nop_mnt_idmap. |
a1bd120d | 2454 | */ |
f2d40141 | 2455 | void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode, |
21cb47be | 2456 | const struct inode *dir, umode_t mode) |
a1bd120d | 2457 | { |
c14329d3 | 2458 | inode_fsuid_set(inode, idmap); |
a1bd120d DM |
2459 | if (dir && dir->i_mode & S_ISGID) { |
2460 | inode->i_gid = dir->i_gid; | |
0fa3ecd8 LT |
2461 | |
2462 | /* Directories are special, and always inherit S_ISGID */ | |
a1bd120d DM |
2463 | if (S_ISDIR(mode)) |
2464 | mode |= S_ISGID; | |
2465 | } else | |
c14329d3 | 2466 | inode_fsgid_set(inode, idmap); |
a1bd120d DM |
2467 | inode->i_mode = mode; |
2468 | } | |
2469 | EXPORT_SYMBOL(inode_init_owner); | |
e795b717 | 2470 | |
2e149670 SH |
2471 | /** |
2472 | * inode_owner_or_capable - check current task permissions to inode | |
01beba79 | 2473 | * @idmap: idmap of the mount the inode was found from |
2e149670 SH |
2474 | * @inode: inode being checked |
2475 | * | |
23adbe12 AL |
2476 | * Return true if current either has CAP_FOWNER in a namespace with the |
2477 | * inode owner uid mapped, or owns the file. | |
21cb47be | 2478 | * |
01beba79 CB |
2479 | * If the inode has been found through an idmapped mount the idmap of |
2480 | * the vfsmount must be passed through @idmap. This function will then take | |
2481 | * care to map the inode according to @idmap before checking permissions. | |
21cb47be | 2482 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 2483 | * raw inode simply pass @nop_mnt_idmap. |
e795b717 | 2484 | */ |
01beba79 | 2485 | bool inode_owner_or_capable(struct mnt_idmap *idmap, |
21cb47be | 2486 | const struct inode *inode) |
e795b717 | 2487 | { |
a2bd096f | 2488 | vfsuid_t vfsuid; |
23adbe12 AL |
2489 | struct user_namespace *ns; |
2490 | ||
e67fe633 | 2491 | vfsuid = i_uid_into_vfsuid(idmap, inode); |
a2bd096f | 2492 | if (vfsuid_eq_kuid(vfsuid, current_fsuid())) |
e795b717 | 2493 | return true; |
23adbe12 AL |
2494 | |
2495 | ns = current_user_ns(); | |
a2bd096f | 2496 | if (vfsuid_has_mapping(ns, vfsuid) && ns_capable(ns, CAP_FOWNER)) |
e795b717 SH |
2497 | return true; |
2498 | return false; | |
2499 | } | |
2e149670 | 2500 | EXPORT_SYMBOL(inode_owner_or_capable); |
1d59d61f TM |
2501 | |
2502 | /* | |
2503 | * Direct i/o helper functions | |
2504 | */ | |
2505 | static void __inode_dio_wait(struct inode *inode) | |
2506 | { | |
2507 | wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP); | |
2508 | DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP); | |
2509 | ||
2510 | do { | |
21417136 | 2511 | prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE); |
1d59d61f TM |
2512 | if (atomic_read(&inode->i_dio_count)) |
2513 | schedule(); | |
2514 | } while (atomic_read(&inode->i_dio_count)); | |
21417136 | 2515 | finish_wait(wq, &q.wq_entry); |
1d59d61f TM |
2516 | } |
2517 | ||
2518 | /** | |
2519 | * inode_dio_wait - wait for outstanding DIO requests to finish | |
2520 | * @inode: inode to wait for | |
2521 | * | |
2522 | * Waits for all pending direct I/O requests to finish so that we can | |
2523 | * proceed with a truncate or equivalent operation. | |
2524 | * | |
2525 | * Must be called under a lock that serializes taking new references | |
2526 | * to i_dio_count, usually by inode->i_mutex. | |
2527 | */ | |
2528 | void inode_dio_wait(struct inode *inode) | |
2529 | { | |
2530 | if (atomic_read(&inode->i_dio_count)) | |
2531 | __inode_dio_wait(inode); | |
2532 | } | |
2533 | EXPORT_SYMBOL(inode_dio_wait); | |
2534 | ||
5f16f322 TT |
2535 | /* |
2536 | * inode_set_flags - atomically set some inode flags | |
2537 | * | |
2538 | * Note: the caller should be holding i_mutex, or else be sure that | |
2539 | * they have exclusive access to the inode structure (i.e., while the | |
2540 | * inode is being instantiated). The reason for the cmpxchg() loop | |
2541 | * --- which wouldn't be necessary if all code paths which modify | |
2542 | * i_flags actually followed this rule, is that there is at least one | |
5fa8e0a1 JK |
2543 | * code path which doesn't today so we use cmpxchg() out of an abundance |
2544 | * of caution. | |
5f16f322 TT |
2545 | * |
2546 | * In the long run, i_mutex is overkill, and we should probably look | |
2547 | * at using the i_lock spinlock to protect i_flags, and then make sure | |
2548 | * it is so documented in include/linux/fs.h and that all code follows | |
2549 | * the locking convention!! | |
2550 | */ | |
2551 | void inode_set_flags(struct inode *inode, unsigned int flags, | |
2552 | unsigned int mask) | |
2553 | { | |
5f16f322 | 2554 | WARN_ON_ONCE(flags & ~mask); |
a905737f | 2555 | set_mask_bits(&inode->i_flags, mask, flags); |
5f16f322 TT |
2556 | } |
2557 | EXPORT_SYMBOL(inode_set_flags); | |
21fc61c7 AV |
2558 | |
2559 | void inode_nohighmem(struct inode *inode) | |
2560 | { | |
2561 | mapping_set_gfp_mask(inode->i_mapping, GFP_USER); | |
2562 | } | |
2563 | EXPORT_SYMBOL(inode_nohighmem); | |
3cd88666 | 2564 | |
50e17c00 DD |
2565 | /** |
2566 | * timestamp_truncate - Truncate timespec to a granularity | |
2567 | * @t: Timespec | |
2568 | * @inode: inode being updated | |
2569 | * | |
2570 | * Truncate a timespec to the granularity supported by the fs | |
2571 | * containing the inode. Always rounds down. gran must | |
2572 | * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). | |
2573 | */ | |
2574 | struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode) | |
2575 | { | |
2576 | struct super_block *sb = inode->i_sb; | |
2577 | unsigned int gran = sb->s_time_gran; | |
2578 | ||
2579 | t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max); | |
2580 | if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min)) | |
2581 | t.tv_nsec = 0; | |
2582 | ||
2583 | /* Avoid division in the common cases 1 ns and 1 s. */ | |
2584 | if (gran == 1) | |
2585 | ; /* nothing */ | |
2586 | else if (gran == NSEC_PER_SEC) | |
2587 | t.tv_nsec = 0; | |
2588 | else if (gran > 1 && gran < NSEC_PER_SEC) | |
2589 | t.tv_nsec -= t.tv_nsec % gran; | |
2590 | else | |
2591 | WARN(1, "invalid file time granularity: %u", gran); | |
2592 | return t; | |
2593 | } | |
2594 | EXPORT_SYMBOL(timestamp_truncate); | |
2595 | ||
3cd88666 DD |
2596 | /** |
2597 | * current_time - Return FS time | |
2598 | * @inode: inode. | |
2599 | * | |
2600 | * Return the current time truncated to the time granularity supported by | |
2601 | * the fs. | |
2602 | * | |
2603 | * Note that inode and inode->sb cannot be NULL. | |
2604 | * Otherwise, the function warns and returns time without truncation. | |
2605 | */ | |
95582b00 | 2606 | struct timespec64 current_time(struct inode *inode) |
3cd88666 | 2607 | { |
d651d160 AB |
2608 | struct timespec64 now; |
2609 | ||
2610 | ktime_get_coarse_real_ts64(&now); | |
50e17c00 | 2611 | return timestamp_truncate(now, inode); |
3cd88666 DD |
2612 | } |
2613 | EXPORT_SYMBOL(current_time); | |
3cd88666 | 2614 | |
9b6304c1 JL |
2615 | /** |
2616 | * inode_set_ctime_current - set the ctime to current_time | |
2617 | * @inode: inode | |
2618 | * | |
2619 | * Set the inode->i_ctime to the current value for the inode. Returns | |
2620 | * the current value that was assigned to i_ctime. | |
2621 | */ | |
2622 | struct timespec64 inode_set_ctime_current(struct inode *inode) | |
2623 | { | |
647aa768 | 2624 | struct timespec64 now = current_time(inode); |
9b6304c1 | 2625 | |
24a8b7bf | 2626 | inode_set_ctime_to_ts(inode, now); |
9b6304c1 | 2627 | return now; |
3cd88666 | 2628 | } |
9b6304c1 | 2629 | EXPORT_SYMBOL(inode_set_ctime_current); |
2b3416ce | 2630 | |
11c2a870 CB |
2631 | /** |
2632 | * in_group_or_capable - check whether caller is CAP_FSETID privileged | |
9452e93e | 2633 | * @idmap: idmap of the mount @inode was found from |
11c2a870 CB |
2634 | * @inode: inode to check |
2635 | * @vfsgid: the new/current vfsgid of @inode | |
2636 | * | |
2637 | * Check wether @vfsgid is in the caller's group list or if the caller is | |
2638 | * privileged with CAP_FSETID over @inode. This can be used to determine | |
2639 | * whether the setgid bit can be kept or must be dropped. | |
2640 | * | |
2641 | * Return: true if the caller is sufficiently privileged, false if not. | |
2642 | */ | |
9452e93e | 2643 | bool in_group_or_capable(struct mnt_idmap *idmap, |
11c2a870 CB |
2644 | const struct inode *inode, vfsgid_t vfsgid) |
2645 | { | |
2646 | if (vfsgid_in_group_p(vfsgid)) | |
2647 | return true; | |
9452e93e | 2648 | if (capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID)) |
11c2a870 CB |
2649 | return true; |
2650 | return false; | |
2651 | } | |
9b6a14f0 | 2652 | EXPORT_SYMBOL(in_group_or_capable); |
11c2a870 | 2653 | |
2b3416ce YX |
2654 | /** |
2655 | * mode_strip_sgid - handle the sgid bit for non-directories | |
9452e93e | 2656 | * @idmap: idmap of the mount the inode was created from |
2b3416ce YX |
2657 | * @dir: parent directory inode |
2658 | * @mode: mode of the file to be created in @dir | |
2659 | * | |
2660 | * If the @mode of the new file has both the S_ISGID and S_IXGRP bit | |
2661 | * raised and @dir has the S_ISGID bit raised ensure that the caller is | |
2662 | * either in the group of the parent directory or they have CAP_FSETID | |
2663 | * in their user namespace and are privileged over the parent directory. | |
2664 | * In all other cases, strip the S_ISGID bit from @mode. | |
2665 | * | |
2666 | * Return: the new mode to use for the file | |
2667 | */ | |
9452e93e | 2668 | umode_t mode_strip_sgid(struct mnt_idmap *idmap, |
2b3416ce YX |
2669 | const struct inode *dir, umode_t mode) |
2670 | { | |
2671 | if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP)) | |
2672 | return mode; | |
2673 | if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID)) | |
2674 | return mode; | |
e67fe633 | 2675 | if (in_group_or_capable(idmap, dir, i_gid_into_vfsgid(idmap, dir))) |
2b3416ce | 2676 | return mode; |
2b3416ce YX |
2677 | return mode & ~S_ISGID; |
2678 | } | |
2679 | EXPORT_SYMBOL(mode_strip_sgid); |