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