1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/inode.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/writeback.h>
11 #include <linux/sched/mm.h>
12 #include <linux/lz4.h>
13 #include <linux/zstd.h>
14
15 #include "f2fs.h"
16 #include "node.h"
17 #include "segment.h"
18 #include "xattr.h"
19
20 #include <trace/events/f2fs.h>
21
22 #ifdef CONFIG_F2FS_FS_COMPRESSION
23 extern const struct address_space_operations f2fs_compress_aops;
24 #endif
25
f2fs_mark_inode_dirty_sync(struct inode * inode,bool sync)26 void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
27 {
28 if (is_inode_flag_set(inode, FI_NEW_INODE))
29 return;
30
31 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
32 return;
33
34 if (f2fs_inode_dirtied(inode, sync))
35 return;
36
37 if (f2fs_is_atomic_file(inode))
38 return;
39
40 mark_inode_dirty_sync(inode);
41 }
42
f2fs_set_inode_flags(struct inode * inode)43 void f2fs_set_inode_flags(struct inode *inode)
44 {
45 unsigned int flags = F2FS_I(inode)->i_flags;
46 unsigned int new_fl = 0;
47
48 if (flags & F2FS_SYNC_FL)
49 new_fl |= S_SYNC;
50 if (flags & F2FS_APPEND_FL)
51 new_fl |= S_APPEND;
52 if (flags & F2FS_IMMUTABLE_FL)
53 new_fl |= S_IMMUTABLE;
54 if (flags & F2FS_NOATIME_FL)
55 new_fl |= S_NOATIME;
56 if (flags & F2FS_DIRSYNC_FL)
57 new_fl |= S_DIRSYNC;
58 if (file_is_encrypt(inode))
59 new_fl |= S_ENCRYPTED;
60 if (file_is_verity(inode))
61 new_fl |= S_VERITY;
62 if (flags & F2FS_CASEFOLD_FL)
63 new_fl |= S_CASEFOLD;
64 inode_set_flags(inode, new_fl,
65 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
66 S_ENCRYPTED|S_VERITY|S_CASEFOLD);
67 }
68
__get_inode_rdev(struct inode * inode,struct page * node_page)69 static void __get_inode_rdev(struct inode *inode, struct page *node_page)
70 {
71 __le32 *addr = get_dnode_addr(inode, node_page);
72
73 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
74 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
75 if (addr[0])
76 inode->i_rdev = old_decode_dev(le32_to_cpu(addr[0]));
77 else
78 inode->i_rdev = new_decode_dev(le32_to_cpu(addr[1]));
79 }
80 }
81
__set_inode_rdev(struct inode * inode,struct page * node_page)82 static void __set_inode_rdev(struct inode *inode, struct page *node_page)
83 {
84 __le32 *addr = get_dnode_addr(inode, node_page);
85
86 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
87 if (old_valid_dev(inode->i_rdev)) {
88 addr[0] = cpu_to_le32(old_encode_dev(inode->i_rdev));
89 addr[1] = 0;
90 } else {
91 addr[0] = 0;
92 addr[1] = cpu_to_le32(new_encode_dev(inode->i_rdev));
93 addr[2] = 0;
94 }
95 }
96 }
97
__recover_inline_status(struct inode * inode,struct page * ipage)98 static void __recover_inline_status(struct inode *inode, struct page *ipage)
99 {
100 void *inline_data = inline_data_addr(inode, ipage);
101 __le32 *start = inline_data;
102 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
103
104 while (start < end) {
105 if (*start++) {
106 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
107
108 set_inode_flag(inode, FI_DATA_EXIST);
109 set_raw_inline(inode, F2FS_INODE(ipage));
110 set_page_dirty(ipage);
111 return;
112 }
113 }
114 return;
115 }
116
f2fs_enable_inode_chksum(struct f2fs_sb_info * sbi,struct page * page)117 static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
118 {
119 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
120
121 if (!f2fs_sb_has_inode_chksum(sbi))
122 return false;
123
124 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
125 return false;
126
127 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
128 i_inode_checksum))
129 return false;
130
131 return true;
132 }
133
f2fs_inode_chksum(struct f2fs_sb_info * sbi,struct page * page)134 static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
135 {
136 struct f2fs_node *node = F2FS_NODE(page);
137 struct f2fs_inode *ri = &node->i;
138 __le32 ino = node->footer.ino;
139 __le32 gen = ri->i_generation;
140 __u32 chksum, chksum_seed;
141 __u32 dummy_cs = 0;
142 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
143 unsigned int cs_size = sizeof(dummy_cs);
144
145 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
146 sizeof(ino));
147 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
148
149 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
150 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
151 offset += cs_size;
152 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
153 F2FS_BLKSIZE - offset);
154 return chksum;
155 }
156
f2fs_inode_chksum_verify(struct f2fs_sb_info * sbi,struct page * page)157 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
158 {
159 struct f2fs_inode *ri;
160 __u32 provided, calculated;
161
162 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
163 return true;
164
165 #ifdef CONFIG_F2FS_CHECK_FS
166 if (!f2fs_enable_inode_chksum(sbi, page))
167 #else
168 if (!f2fs_enable_inode_chksum(sbi, page) ||
169 PageDirty(page) ||
170 folio_test_writeback(page_folio(page)))
171 #endif
172 return true;
173
174 ri = &F2FS_NODE(page)->i;
175 provided = le32_to_cpu(ri->i_inode_checksum);
176 calculated = f2fs_inode_chksum(sbi, page);
177
178 if (provided != calculated)
179 f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x",
180 page_folio(page)->index, ino_of_node(page),
181 provided, calculated);
182
183 return provided == calculated;
184 }
185
f2fs_inode_chksum_set(struct f2fs_sb_info * sbi,struct page * page)186 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
187 {
188 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
189
190 if (!f2fs_enable_inode_chksum(sbi, page))
191 return;
192
193 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
194 }
195
sanity_check_compress_inode(struct inode * inode,struct f2fs_inode * ri)196 static bool sanity_check_compress_inode(struct inode *inode,
197 struct f2fs_inode *ri)
198 {
199 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
200 unsigned char clevel;
201
202 if (ri->i_compress_algorithm >= COMPRESS_MAX) {
203 f2fs_warn(sbi,
204 "%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix",
205 __func__, inode->i_ino, ri->i_compress_algorithm);
206 return false;
207 }
208 if (le64_to_cpu(ri->i_compr_blocks) >
209 SECTOR_TO_BLOCK(inode->i_blocks)) {
210 f2fs_warn(sbi,
211 "%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
212 __func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks),
213 SECTOR_TO_BLOCK(inode->i_blocks));
214 return false;
215 }
216 if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
217 ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
218 f2fs_warn(sbi,
219 "%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix",
220 __func__, inode->i_ino, ri->i_log_cluster_size);
221 return false;
222 }
223
224 clevel = le16_to_cpu(ri->i_compress_flag) >>
225 COMPRESS_LEVEL_OFFSET;
226 switch (ri->i_compress_algorithm) {
227 case COMPRESS_LZO:
228 #ifdef CONFIG_F2FS_FS_LZO
229 if (clevel)
230 goto err_level;
231 #endif
232 break;
233 case COMPRESS_LZORLE:
234 #ifdef CONFIG_F2FS_FS_LZORLE
235 if (clevel)
236 goto err_level;
237 #endif
238 break;
239 case COMPRESS_LZ4:
240 #ifdef CONFIG_F2FS_FS_LZ4
241 #ifdef CONFIG_F2FS_FS_LZ4HC
242 if (clevel &&
243 (clevel < LZ4HC_MIN_CLEVEL || clevel > LZ4HC_MAX_CLEVEL))
244 goto err_level;
245 #else
246 if (clevel)
247 goto err_level;
248 #endif
249 #endif
250 break;
251 case COMPRESS_ZSTD:
252 #ifdef CONFIG_F2FS_FS_ZSTD
253 if (clevel < zstd_min_clevel() || clevel > zstd_max_clevel())
254 goto err_level;
255 #endif
256 break;
257 default:
258 goto err_level;
259 }
260
261 return true;
262 err_level:
263 f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix",
264 __func__, inode->i_ino, clevel);
265 return false;
266 }
267
sanity_check_inode(struct inode * inode,struct page * node_page)268 static bool sanity_check_inode(struct inode *inode, struct page *node_page)
269 {
270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
271 struct f2fs_inode_info *fi = F2FS_I(inode);
272 struct f2fs_inode *ri = F2FS_INODE(node_page);
273 unsigned long long iblocks;
274
275 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
276 if (!iblocks) {
277 f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.",
278 __func__, inode->i_ino, iblocks);
279 return false;
280 }
281
282 if (ino_of_node(node_page) != nid_of_node(node_page)) {
283 f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.",
284 __func__, inode->i_ino,
285 ino_of_node(node_page), nid_of_node(node_page));
286 return false;
287 }
288
289 if (f2fs_has_extra_attr(inode)) {
290 if (!f2fs_sb_has_extra_attr(sbi)) {
291 f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
292 __func__, inode->i_ino);
293 return false;
294 }
295 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
296 fi->i_extra_isize < F2FS_MIN_EXTRA_ATTR_SIZE ||
297 fi->i_extra_isize % sizeof(__le32)) {
298 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu",
299 __func__, inode->i_ino, fi->i_extra_isize,
300 F2FS_TOTAL_EXTRA_ATTR_SIZE);
301 return false;
302 }
303 if (f2fs_sb_has_compression(sbi) &&
304 fi->i_flags & F2FS_COMPR_FL &&
305 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
306 i_compress_flag)) {
307 if (!sanity_check_compress_inode(inode, ri))
308 return false;
309 }
310 }
311
312 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
313 f2fs_has_inline_xattr(inode) &&
314 (fi->i_inline_xattr_size < MIN_INLINE_XATTR_SIZE ||
315 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
316 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, min: %zu, max: %lu",
317 __func__, inode->i_ino, fi->i_inline_xattr_size,
318 MIN_INLINE_XATTR_SIZE, MAX_INLINE_XATTR_SIZE);
319 return false;
320 }
321
322 if (!f2fs_sb_has_extra_attr(sbi)) {
323 if (f2fs_sb_has_project_quota(sbi)) {
324 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
325 __func__, inode->i_ino, F2FS_FEATURE_PRJQUOTA);
326 return false;
327 }
328 if (f2fs_sb_has_inode_chksum(sbi)) {
329 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
330 __func__, inode->i_ino, F2FS_FEATURE_INODE_CHKSUM);
331 return false;
332 }
333 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
334 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
335 __func__, inode->i_ino, F2FS_FEATURE_FLEXIBLE_INLINE_XATTR);
336 return false;
337 }
338 if (f2fs_sb_has_inode_crtime(sbi)) {
339 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
340 __func__, inode->i_ino, F2FS_FEATURE_INODE_CRTIME);
341 return false;
342 }
343 if (f2fs_sb_has_compression(sbi)) {
344 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
345 __func__, inode->i_ino, F2FS_FEATURE_COMPRESSION);
346 return false;
347 }
348 }
349
350 if (f2fs_sanity_check_inline_data(inode, node_page)) {
351 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
352 __func__, inode->i_ino, inode->i_mode);
353 return false;
354 }
355
356 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
357 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix",
358 __func__, inode->i_ino, inode->i_mode);
359 return false;
360 }
361
362 if ((fi->i_flags & F2FS_CASEFOLD_FL) && !f2fs_sb_has_casefold(sbi)) {
363 f2fs_warn(sbi, "%s: inode (ino=%lx) has casefold flag, but casefold feature is off",
364 __func__, inode->i_ino);
365 return false;
366 }
367
368 if (fi->i_xattr_nid && f2fs_check_nid_range(sbi, fi->i_xattr_nid)) {
369 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_xattr_nid: %u, run fsck to fix.",
370 __func__, inode->i_ino, fi->i_xattr_nid);
371 return false;
372 }
373
374 if (IS_DEVICE_ALIASING(inode)) {
375 if (!f2fs_sb_has_device_alias(sbi)) {
376 f2fs_warn(sbi, "%s: inode (ino=%lx) has device alias flag, but the feature is off",
377 __func__, inode->i_ino);
378 return false;
379 }
380 if (!f2fs_is_pinned_file(inode)) {
381 f2fs_warn(sbi, "%s: inode (ino=%lx) has device alias flag, but is not pinned",
382 __func__, inode->i_ino);
383 return false;
384 }
385 }
386
387 return true;
388 }
389
init_idisk_time(struct inode * inode)390 static void init_idisk_time(struct inode *inode)
391 {
392 struct f2fs_inode_info *fi = F2FS_I(inode);
393
394 fi->i_disk_time[0] = inode_get_atime(inode);
395 fi->i_disk_time[1] = inode_get_ctime(inode);
396 fi->i_disk_time[2] = inode_get_mtime(inode);
397 }
398
do_read_inode(struct inode * inode)399 static int do_read_inode(struct inode *inode)
400 {
401 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
402 struct f2fs_inode_info *fi = F2FS_I(inode);
403 struct page *node_page;
404 struct f2fs_inode *ri;
405 projid_t i_projid;
406
407 /* Check if ino is within scope */
408 if (f2fs_check_nid_range(sbi, inode->i_ino))
409 return -EINVAL;
410
411 node_page = f2fs_get_node_page(sbi, inode->i_ino);
412 if (IS_ERR(node_page))
413 return PTR_ERR(node_page);
414
415 ri = F2FS_INODE(node_page);
416
417 inode->i_mode = le16_to_cpu(ri->i_mode);
418 i_uid_write(inode, le32_to_cpu(ri->i_uid));
419 i_gid_write(inode, le32_to_cpu(ri->i_gid));
420 set_nlink(inode, le32_to_cpu(ri->i_links));
421 inode->i_size = le64_to_cpu(ri->i_size);
422 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
423
424 inode_set_atime(inode, le64_to_cpu(ri->i_atime),
425 le32_to_cpu(ri->i_atime_nsec));
426 inode_set_ctime(inode, le64_to_cpu(ri->i_ctime),
427 le32_to_cpu(ri->i_ctime_nsec));
428 inode_set_mtime(inode, le64_to_cpu(ri->i_mtime),
429 le32_to_cpu(ri->i_mtime_nsec));
430 inode->i_generation = le32_to_cpu(ri->i_generation);
431 if (S_ISDIR(inode->i_mode))
432 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
433 else if (S_ISREG(inode->i_mode))
434 fi->i_gc_failures = le16_to_cpu(ri->i_gc_failures);
435 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
436 fi->i_flags = le32_to_cpu(ri->i_flags);
437 if (S_ISREG(inode->i_mode))
438 fi->i_flags &= ~F2FS_PROJINHERIT_FL;
439 bitmap_zero(fi->flags, FI_MAX);
440 fi->i_advise = ri->i_advise;
441 fi->i_pino = le32_to_cpu(ri->i_pino);
442 fi->i_dir_level = ri->i_dir_level;
443
444 get_inline_info(inode, ri);
445
446 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
447 le16_to_cpu(ri->i_extra_isize) : 0;
448
449 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
450 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
451 } else if (f2fs_has_inline_xattr(inode) ||
452 f2fs_has_inline_dentry(inode)) {
453 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
454 } else {
455
456 /*
457 * Previous inline data or directory always reserved 200 bytes
458 * in inode layout, even if inline_xattr is disabled. In order
459 * to keep inline_dentry's structure for backward compatibility,
460 * we get the space back only from inline_data.
461 */
462 fi->i_inline_xattr_size = 0;
463 }
464
465 if (!sanity_check_inode(inode, node_page)) {
466 f2fs_put_page(node_page, 1);
467 set_sbi_flag(sbi, SBI_NEED_FSCK);
468 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
469 return -EFSCORRUPTED;
470 }
471
472 /* check data exist */
473 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
474 __recover_inline_status(inode, node_page);
475
476 /* try to recover cold bit for non-dir inode */
477 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
478 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
479 set_cold_node(node_page, false);
480 set_page_dirty(node_page);
481 }
482
483 /* get rdev by using inline_info */
484 __get_inode_rdev(inode, node_page);
485
486 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
487 fi->last_disk_size = inode->i_size;
488
489 if (fi->i_flags & F2FS_PROJINHERIT_FL)
490 set_inode_flag(inode, FI_PROJ_INHERIT);
491
492 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
493 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
494 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
495 else
496 i_projid = F2FS_DEF_PROJID;
497 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
498
499 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
500 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
501 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
502 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
503 }
504
505 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
506 (fi->i_flags & F2FS_COMPR_FL)) {
507 if (F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
508 i_compress_flag)) {
509 unsigned short compress_flag;
510
511 atomic_set(&fi->i_compr_blocks,
512 le64_to_cpu(ri->i_compr_blocks));
513 fi->i_compress_algorithm = ri->i_compress_algorithm;
514 fi->i_log_cluster_size = ri->i_log_cluster_size;
515 compress_flag = le16_to_cpu(ri->i_compress_flag);
516 fi->i_compress_level = compress_flag >>
517 COMPRESS_LEVEL_OFFSET;
518 fi->i_compress_flag = compress_flag &
519 GENMASK(COMPRESS_LEVEL_OFFSET - 1, 0);
520 fi->i_cluster_size = BIT(fi->i_log_cluster_size);
521 set_inode_flag(inode, FI_COMPRESSED_FILE);
522 }
523 }
524
525 init_idisk_time(inode);
526
527 if (!sanity_check_extent_cache(inode, node_page)) {
528 f2fs_put_page(node_page, 1);
529 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
530 return -EFSCORRUPTED;
531 }
532
533 /* Need all the flag bits */
534 f2fs_init_read_extent_tree(inode, node_page);
535 f2fs_init_age_extent_tree(inode);
536
537 f2fs_put_page(node_page, 1);
538
539 stat_inc_inline_xattr(inode);
540 stat_inc_inline_inode(inode);
541 stat_inc_inline_dir(inode);
542 stat_inc_compr_inode(inode);
543 stat_add_compr_blocks(inode, atomic_read(&fi->i_compr_blocks));
544
545 return 0;
546 }
547
is_meta_ino(struct f2fs_sb_info * sbi,unsigned int ino)548 static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
549 {
550 return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
551 ino == F2FS_COMPRESS_INO(sbi);
552 }
553
f2fs_iget(struct super_block * sb,unsigned long ino)554 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
555 {
556 struct f2fs_sb_info *sbi = F2FS_SB(sb);
557 struct inode *inode;
558 int ret = 0;
559
560 inode = iget_locked(sb, ino);
561 if (!inode)
562 return ERR_PTR(-ENOMEM);
563
564 if (!(inode->i_state & I_NEW)) {
565 if (is_meta_ino(sbi, ino)) {
566 f2fs_err(sbi, "inaccessible inode: %lu, run fsck to repair", ino);
567 set_sbi_flag(sbi, SBI_NEED_FSCK);
568 ret = -EFSCORRUPTED;
569 trace_f2fs_iget_exit(inode, ret);
570 iput(inode);
571 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
572 return ERR_PTR(ret);
573 }
574
575 trace_f2fs_iget(inode);
576 return inode;
577 }
578
579 if (is_meta_ino(sbi, ino))
580 goto make_now;
581
582 ret = do_read_inode(inode);
583 if (ret)
584 goto bad_inode;
585 make_now:
586 if (ino == F2FS_NODE_INO(sbi)) {
587 inode->i_mapping->a_ops = &f2fs_node_aops;
588 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
589 } else if (ino == F2FS_META_INO(sbi)) {
590 inode->i_mapping->a_ops = &f2fs_meta_aops;
591 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
592 } else if (ino == F2FS_COMPRESS_INO(sbi)) {
593 #ifdef CONFIG_F2FS_FS_COMPRESSION
594 inode->i_mapping->a_ops = &f2fs_compress_aops;
595 /*
596 * generic_error_remove_folio only truncates pages of regular
597 * inode
598 */
599 inode->i_mode |= S_IFREG;
600 #endif
601 mapping_set_gfp_mask(inode->i_mapping,
602 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
603 } else if (S_ISREG(inode->i_mode)) {
604 inode->i_op = &f2fs_file_inode_operations;
605 inode->i_fop = &f2fs_file_operations;
606 inode->i_mapping->a_ops = &f2fs_dblock_aops;
607 } else if (S_ISDIR(inode->i_mode)) {
608 inode->i_op = &f2fs_dir_inode_operations;
609 inode->i_fop = &f2fs_dir_operations;
610 inode->i_mapping->a_ops = &f2fs_dblock_aops;
611 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
612 } else if (S_ISLNK(inode->i_mode)) {
613 if (file_is_encrypt(inode))
614 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
615 else
616 inode->i_op = &f2fs_symlink_inode_operations;
617 inode_nohighmem(inode);
618 inode->i_mapping->a_ops = &f2fs_dblock_aops;
619 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
620 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
621 inode->i_op = &f2fs_special_inode_operations;
622 init_special_inode(inode, inode->i_mode, inode->i_rdev);
623 } else {
624 ret = -EIO;
625 goto bad_inode;
626 }
627 f2fs_set_inode_flags(inode);
628
629 unlock_new_inode(inode);
630 trace_f2fs_iget(inode);
631 return inode;
632
633 bad_inode:
634 f2fs_inode_synced(inode);
635 iget_failed(inode);
636 trace_f2fs_iget_exit(inode, ret);
637 return ERR_PTR(ret);
638 }
639
f2fs_iget_retry(struct super_block * sb,unsigned long ino)640 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
641 {
642 struct inode *inode;
643 retry:
644 inode = f2fs_iget(sb, ino);
645 if (IS_ERR(inode)) {
646 if (PTR_ERR(inode) == -ENOMEM) {
647 memalloc_retry_wait(GFP_NOFS);
648 goto retry;
649 }
650 }
651 return inode;
652 }
653
f2fs_update_inode(struct inode * inode,struct page * node_page)654 void f2fs_update_inode(struct inode *inode, struct page *node_page)
655 {
656 struct f2fs_inode_info *fi = F2FS_I(inode);
657 struct f2fs_inode *ri;
658 struct extent_tree *et = fi->extent_tree[EX_READ];
659
660 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
661 set_page_dirty(node_page);
662
663 f2fs_inode_synced(inode);
664
665 ri = F2FS_INODE(node_page);
666
667 ri->i_mode = cpu_to_le16(inode->i_mode);
668 ri->i_advise = fi->i_advise;
669 ri->i_uid = cpu_to_le32(i_uid_read(inode));
670 ri->i_gid = cpu_to_le32(i_gid_read(inode));
671 ri->i_links = cpu_to_le32(inode->i_nlink);
672 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
673
674 if (!f2fs_is_atomic_file(inode) ||
675 is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
676 ri->i_size = cpu_to_le64(i_size_read(inode));
677
678 if (et) {
679 read_lock(&et->lock);
680 set_raw_read_extent(&et->largest, &ri->i_ext);
681 read_unlock(&et->lock);
682 } else {
683 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
684 }
685 set_raw_inline(inode, ri);
686
687 ri->i_atime = cpu_to_le64(inode_get_atime_sec(inode));
688 ri->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
689 ri->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode));
690 ri->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode));
691 ri->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
692 ri->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
693 if (S_ISDIR(inode->i_mode))
694 ri->i_current_depth = cpu_to_le32(fi->i_current_depth);
695 else if (S_ISREG(inode->i_mode))
696 ri->i_gc_failures = cpu_to_le16(fi->i_gc_failures);
697 ri->i_xattr_nid = cpu_to_le32(fi->i_xattr_nid);
698 ri->i_flags = cpu_to_le32(fi->i_flags);
699 ri->i_pino = cpu_to_le32(fi->i_pino);
700 ri->i_generation = cpu_to_le32(inode->i_generation);
701 ri->i_dir_level = fi->i_dir_level;
702
703 if (f2fs_has_extra_attr(inode)) {
704 ri->i_extra_isize = cpu_to_le16(fi->i_extra_isize);
705
706 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
707 ri->i_inline_xattr_size =
708 cpu_to_le16(fi->i_inline_xattr_size);
709
710 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
711 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) {
712 projid_t i_projid;
713
714 i_projid = from_kprojid(&init_user_ns, fi->i_projid);
715 ri->i_projid = cpu_to_le32(i_projid);
716 }
717
718 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
719 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
720 ri->i_crtime = cpu_to_le64(fi->i_crtime.tv_sec);
721 ri->i_crtime_nsec = cpu_to_le32(fi->i_crtime.tv_nsec);
722 }
723
724 if (f2fs_sb_has_compression(F2FS_I_SB(inode)) &&
725 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
726 i_compress_flag)) {
727 unsigned short compress_flag;
728
729 ri->i_compr_blocks = cpu_to_le64(
730 atomic_read(&fi->i_compr_blocks));
731 ri->i_compress_algorithm = fi->i_compress_algorithm;
732 compress_flag = fi->i_compress_flag |
733 fi->i_compress_level <<
734 COMPRESS_LEVEL_OFFSET;
735 ri->i_compress_flag = cpu_to_le16(compress_flag);
736 ri->i_log_cluster_size = fi->i_log_cluster_size;
737 }
738 }
739
740 __set_inode_rdev(inode, node_page);
741
742 /* deleted inode */
743 if (inode->i_nlink == 0)
744 clear_page_private_inline(node_page);
745
746 init_idisk_time(inode);
747 #ifdef CONFIG_F2FS_CHECK_FS
748 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
749 #endif
750 }
751
f2fs_update_inode_page(struct inode * inode)752 void f2fs_update_inode_page(struct inode *inode)
753 {
754 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
755 struct page *node_page;
756 int count = 0;
757 retry:
758 node_page = f2fs_get_node_page(sbi, inode->i_ino);
759 if (IS_ERR(node_page)) {
760 int err = PTR_ERR(node_page);
761
762 /* The node block was truncated. */
763 if (err == -ENOENT)
764 return;
765
766 if (err == -EFSCORRUPTED)
767 goto stop_checkpoint;
768
769 if (err == -ENOMEM || ++count <= DEFAULT_RETRY_IO_COUNT)
770 goto retry;
771 stop_checkpoint:
772 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_UPDATE_INODE);
773 return;
774 }
775 f2fs_update_inode(inode, node_page);
776 f2fs_put_page(node_page, 1);
777 }
778
f2fs_write_inode(struct inode * inode,struct writeback_control * wbc)779 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
780 {
781 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
782
783 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
784 inode->i_ino == F2FS_META_INO(sbi))
785 return 0;
786
787 /*
788 * atime could be updated without dirtying f2fs inode in lazytime mode
789 */
790 if (f2fs_is_time_consistent(inode) &&
791 !is_inode_flag_set(inode, FI_DIRTY_INODE))
792 return 0;
793
794 /*
795 * no need to update inode page, ultimately f2fs_evict_inode() will
796 * clear dirty status of inode.
797 */
798 if (f2fs_cp_error(sbi))
799 return -EIO;
800
801 if (!f2fs_is_checkpoint_ready(sbi)) {
802 f2fs_mark_inode_dirty_sync(inode, true);
803 return -ENOSPC;
804 }
805
806 /*
807 * We need to balance fs here to prevent from producing dirty node pages
808 * during the urgent cleaning time when running out of free sections.
809 */
810 f2fs_update_inode_page(inode);
811 if (wbc && wbc->nr_to_write)
812 f2fs_balance_fs(sbi, true);
813 return 0;
814 }
815
816 /*
817 * Called at the last iput() if i_nlink is zero
818 */
f2fs_evict_inode(struct inode * inode)819 void f2fs_evict_inode(struct inode *inode)
820 {
821 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
822 struct f2fs_inode_info *fi = F2FS_I(inode);
823 nid_t xnid = fi->i_xattr_nid;
824 int err = 0;
825 bool freeze_protected = false;
826
827 f2fs_abort_atomic_write(inode, true);
828
829 if (fi->cow_inode && f2fs_is_cow_file(fi->cow_inode)) {
830 clear_inode_flag(fi->cow_inode, FI_COW_FILE);
831 F2FS_I(fi->cow_inode)->atomic_inode = NULL;
832 iput(fi->cow_inode);
833 fi->cow_inode = NULL;
834 }
835
836 trace_f2fs_evict_inode(inode);
837 truncate_inode_pages_final(&inode->i_data);
838
839 if ((inode->i_nlink || is_bad_inode(inode)) &&
840 test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))
841 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
842
843 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
844 inode->i_ino == F2FS_META_INO(sbi) ||
845 inode->i_ino == F2FS_COMPRESS_INO(sbi))
846 goto out_clear;
847
848 f2fs_bug_on(sbi, get_dirty_pages(inode));
849 f2fs_remove_dirty_inode(inode);
850
851 if (!IS_DEVICE_ALIASING(inode))
852 f2fs_destroy_extent_tree(inode);
853
854 if (inode->i_nlink || is_bad_inode(inode))
855 goto no_delete;
856
857 err = f2fs_dquot_initialize(inode);
858 if (err) {
859 err = 0;
860 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
861 }
862
863 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
864 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
865 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
866
867 if (!is_sbi_flag_set(sbi, SBI_IS_FREEZING)) {
868 sb_start_intwrite(inode->i_sb);
869 freeze_protected = true;
870 }
871 set_inode_flag(inode, FI_NO_ALLOC);
872 i_size_write(inode, 0);
873 retry:
874 if (F2FS_HAS_BLOCKS(inode))
875 err = f2fs_truncate(inode);
876
877 if (time_to_inject(sbi, FAULT_EVICT_INODE))
878 err = -EIO;
879
880 if (!err) {
881 f2fs_lock_op(sbi);
882 err = f2fs_remove_inode_page(inode);
883 f2fs_unlock_op(sbi);
884 if (err == -ENOENT) {
885 err = 0;
886
887 /*
888 * in fuzzed image, another node may has the same
889 * block address as inode's, if it was truncated
890 * previously, truncation of inode node will fail.
891 */
892 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
893 f2fs_warn(F2FS_I_SB(inode),
894 "f2fs_evict_inode: inconsistent node id, ino:%lu",
895 inode->i_ino);
896 f2fs_inode_synced(inode);
897 set_sbi_flag(sbi, SBI_NEED_FSCK);
898 }
899 }
900 }
901
902 /* give more chances, if ENOMEM case */
903 if (err == -ENOMEM) {
904 err = 0;
905 goto retry;
906 }
907
908 if (IS_DEVICE_ALIASING(inode))
909 f2fs_destroy_extent_tree(inode);
910
911 if (err) {
912 f2fs_update_inode_page(inode);
913 if (dquot_initialize_needed(inode))
914 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
915 }
916 if (freeze_protected)
917 sb_end_intwrite(inode->i_sb);
918 no_delete:
919 dquot_drop(inode);
920
921 stat_dec_inline_xattr(inode);
922 stat_dec_inline_dir(inode);
923 stat_dec_inline_inode(inode);
924 stat_dec_compr_inode(inode);
925 stat_sub_compr_blocks(inode,
926 atomic_read(&fi->i_compr_blocks));
927
928 if (likely(!f2fs_cp_error(sbi) &&
929 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
930 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
931 else
932 f2fs_inode_synced(inode);
933
934 /* for the case f2fs_new_inode() was failed, .i_ino is zero, skip it */
935 if (inode->i_ino)
936 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
937 inode->i_ino);
938 if (xnid)
939 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
940 if (inode->i_nlink) {
941 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
942 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
943 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
944 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
945 }
946 if (is_inode_flag_set(inode, FI_FREE_NID)) {
947 f2fs_alloc_nid_failed(sbi, inode->i_ino);
948 clear_inode_flag(inode, FI_FREE_NID);
949 } else {
950 /*
951 * If xattr nid is corrupted, we can reach out error condition,
952 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
953 * In that case, f2fs_check_nid_range() is enough to give a clue.
954 */
955 }
956 out_clear:
957 fscrypt_put_encryption_info(inode);
958 fsverity_cleanup_inode(inode);
959 clear_inode(inode);
960 }
961
962 /* caller should call f2fs_lock_op() */
f2fs_handle_failed_inode(struct inode * inode)963 void f2fs_handle_failed_inode(struct inode *inode)
964 {
965 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
966 struct node_info ni;
967 int err;
968
969 /*
970 * clear nlink of inode in order to release resource of inode
971 * immediately.
972 */
973 clear_nlink(inode);
974
975 /*
976 * we must call this to avoid inode being remained as dirty, resulting
977 * in a panic when flushing dirty inodes in gdirty_list.
978 */
979 f2fs_update_inode_page(inode);
980 f2fs_inode_synced(inode);
981
982 /* don't make bad inode, since it becomes a regular file. */
983 unlock_new_inode(inode);
984
985 /*
986 * Note: we should add inode to orphan list before f2fs_unlock_op()
987 * so we can prevent losing this orphan when encoutering checkpoint
988 * and following suddenly power-off.
989 */
990 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
991 if (err) {
992 set_sbi_flag(sbi, SBI_NEED_FSCK);
993 set_inode_flag(inode, FI_FREE_NID);
994 f2fs_warn(sbi, "May loss orphan inode, run fsck to fix.");
995 goto out;
996 }
997
998 if (ni.blk_addr != NULL_ADDR) {
999 err = f2fs_acquire_orphan_inode(sbi);
1000 if (err) {
1001 set_sbi_flag(sbi, SBI_NEED_FSCK);
1002 f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix.");
1003 } else {
1004 f2fs_add_orphan_inode(inode);
1005 }
1006 f2fs_alloc_nid_done(sbi, inode->i_ino);
1007 } else {
1008 set_inode_flag(inode, FI_FREE_NID);
1009 }
1010
1011 out:
1012 f2fs_unlock_op(sbi);
1013
1014 /* iput will drop the inode object */
1015 iput(inode);
1016 }
1017