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