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