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