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