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