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