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