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