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