xref: /linux/fs/f2fs/inode.c (revision 109d59b900e78834c66657dd4748fcedb9a1fe8d)
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 
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 
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 & FS_SYNC_FL)
40 		new_fl |= S_SYNC;
41 	if (flags & FS_APPEND_FL)
42 		new_fl |= S_APPEND;
43 	if (flags & FS_IMMUTABLE_FL)
44 		new_fl |= S_IMMUTABLE;
45 	if (flags & FS_NOATIME_FL)
46 		new_fl |= S_NOATIME;
47 	if (flags & FS_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 
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 
71 static bool __written_first_block(struct f2fs_inode *ri)
72 {
73 	block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
74 
75 	if (addr != NEW_ADDR && addr != NULL_ADDR)
76 		return true;
77 	return false;
78 }
79 
80 static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
81 {
82 	int extra_size = get_extra_isize(inode);
83 
84 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
85 		if (old_valid_dev(inode->i_rdev)) {
86 			ri->i_addr[extra_size] =
87 				cpu_to_le32(old_encode_dev(inode->i_rdev));
88 			ri->i_addr[extra_size + 1] = 0;
89 		} else {
90 			ri->i_addr[extra_size] = 0;
91 			ri->i_addr[extra_size + 1] =
92 				cpu_to_le32(new_encode_dev(inode->i_rdev));
93 			ri->i_addr[extra_size + 2] = 0;
94 		}
95 	}
96 }
97 
98 static void __recover_inline_status(struct inode *inode, struct page *ipage)
99 {
100 	void *inline_data = inline_data_addr(inode, ipage);
101 	__le32 *start = inline_data;
102 	__le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
103 
104 	while (start < end) {
105 		if (*start++) {
106 			f2fs_wait_on_page_writeback(ipage, NODE, true);
107 
108 			set_inode_flag(inode, FI_DATA_EXIST);
109 			set_raw_inline(inode, F2FS_INODE(ipage));
110 			set_page_dirty(ipage);
111 			return;
112 		}
113 	}
114 	return;
115 }
116 
117 static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
118 {
119 	struct f2fs_inode *ri = &F2FS_NODE(page)->i;
120 	int extra_isize = le32_to_cpu(ri->i_extra_isize);
121 
122 	if (!f2fs_sb_has_inode_chksum(sbi->sb))
123 		return false;
124 
125 	if (!RAW_IS_INODE(F2FS_NODE(page)) || !(ri->i_inline & F2FS_EXTRA_ATTR))
126 		return false;
127 
128 	if (!F2FS_FITS_IN_INODE(ri, extra_isize, i_inode_checksum))
129 		return false;
130 
131 	return true;
132 }
133 
134 static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
135 {
136 	struct f2fs_node *node = F2FS_NODE(page);
137 	struct f2fs_inode *ri = &node->i;
138 	__le32 ino = node->footer.ino;
139 	__le32 gen = ri->i_generation;
140 	__u32 chksum, chksum_seed;
141 	__u32 dummy_cs = 0;
142 	unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
143 	unsigned int cs_size = sizeof(dummy_cs);
144 
145 	chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
146 							sizeof(ino));
147 	chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
148 
149 	chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
150 	chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
151 	offset += cs_size;
152 	chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
153 						F2FS_BLKSIZE - offset);
154 	return chksum;
155 }
156 
157 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
158 {
159 	struct f2fs_inode *ri;
160 	__u32 provided, calculated;
161 
162 	if (!f2fs_enable_inode_chksum(sbi, page) ||
163 			PageDirty(page) || PageWriteback(page))
164 		return true;
165 
166 	ri = &F2FS_NODE(page)->i;
167 	provided = le32_to_cpu(ri->i_inode_checksum);
168 	calculated = f2fs_inode_chksum(sbi, page);
169 
170 	if (provided != calculated)
171 		f2fs_msg(sbi->sb, KERN_WARNING,
172 			"checksum invalid, ino = %x, %x vs. %x",
173 			ino_of_node(page), provided, calculated);
174 
175 	return provided == calculated;
176 }
177 
178 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
179 {
180 	struct f2fs_inode *ri = &F2FS_NODE(page)->i;
181 
182 	if (!f2fs_enable_inode_chksum(sbi, page))
183 		return;
184 
185 	ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
186 }
187 
188 static int do_read_inode(struct inode *inode)
189 {
190 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
191 	struct f2fs_inode_info *fi = F2FS_I(inode);
192 	struct page *node_page;
193 	struct f2fs_inode *ri;
194 	projid_t i_projid;
195 
196 	/* Check if ino is within scope */
197 	if (check_nid_range(sbi, inode->i_ino)) {
198 		f2fs_msg(inode->i_sb, KERN_ERR, "bad inode number: %lu",
199 			 (unsigned long) inode->i_ino);
200 		WARN_ON(1);
201 		return -EINVAL;
202 	}
203 
204 	node_page = get_node_page(sbi, inode->i_ino);
205 	if (IS_ERR(node_page))
206 		return PTR_ERR(node_page);
207 
208 	ri = F2FS_INODE(node_page);
209 
210 	inode->i_mode = le16_to_cpu(ri->i_mode);
211 	i_uid_write(inode, le32_to_cpu(ri->i_uid));
212 	i_gid_write(inode, le32_to_cpu(ri->i_gid));
213 	set_nlink(inode, le32_to_cpu(ri->i_links));
214 	inode->i_size = le64_to_cpu(ri->i_size);
215 	inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
216 
217 	inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
218 	inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
219 	inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
220 	inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
221 	inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
222 	inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
223 	inode->i_generation = le32_to_cpu(ri->i_generation);
224 
225 	fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
226 	fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
227 	fi->i_flags = le32_to_cpu(ri->i_flags);
228 	fi->flags = 0;
229 	fi->i_advise = ri->i_advise;
230 	fi->i_pino = le32_to_cpu(ri->i_pino);
231 	fi->i_dir_level = ri->i_dir_level;
232 
233 	if (f2fs_init_extent_tree(inode, &ri->i_ext))
234 		set_page_dirty(node_page);
235 
236 	get_inline_info(inode, ri);
237 
238 	fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
239 					le16_to_cpu(ri->i_extra_isize) : 0;
240 
241 	if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
242 		f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
243 		fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
244 	} else if (f2fs_has_inline_xattr(inode) ||
245 				f2fs_has_inline_dentry(inode)) {
246 		fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
247 	} else {
248 
249 		/*
250 		 * Previous inline data or directory always reserved 200 bytes
251 		 * in inode layout, even if inline_xattr is disabled. In order
252 		 * to keep inline_dentry's structure for backward compatibility,
253 		 * we get the space back only from inline_data.
254 		 */
255 		fi->i_inline_xattr_size = 0;
256 	}
257 
258 	/* check data exist */
259 	if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
260 		__recover_inline_status(inode, node_page);
261 
262 	/* get rdev by using inline_info */
263 	__get_inode_rdev(inode, ri);
264 
265 	if (__written_first_block(ri))
266 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
267 
268 	if (!need_inode_block_update(sbi, inode->i_ino))
269 		fi->last_disk_size = inode->i_size;
270 
271 	if (fi->i_flags & FS_PROJINHERIT_FL)
272 		set_inode_flag(inode, FI_PROJ_INHERIT);
273 
274 	if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) &&
275 			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
276 		i_projid = (projid_t)le32_to_cpu(ri->i_projid);
277 	else
278 		i_projid = F2FS_DEF_PROJID;
279 	fi->i_projid = make_kprojid(&init_user_ns, i_projid);
280 
281 	if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi->sb) &&
282 			F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
283 		fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
284 		fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
285 	}
286 
287 	f2fs_put_page(node_page, 1);
288 
289 	stat_inc_inline_xattr(inode);
290 	stat_inc_inline_inode(inode);
291 	stat_inc_inline_dir(inode);
292 
293 	return 0;
294 }
295 
296 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
297 {
298 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
299 	struct inode *inode;
300 	int ret = 0;
301 
302 	inode = iget_locked(sb, ino);
303 	if (!inode)
304 		return ERR_PTR(-ENOMEM);
305 
306 	if (!(inode->i_state & I_NEW)) {
307 		trace_f2fs_iget(inode);
308 		return inode;
309 	}
310 	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
311 		goto make_now;
312 
313 	ret = do_read_inode(inode);
314 	if (ret)
315 		goto bad_inode;
316 make_now:
317 	if (ino == F2FS_NODE_INO(sbi)) {
318 		inode->i_mapping->a_ops = &f2fs_node_aops;
319 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
320 	} else if (ino == F2FS_META_INO(sbi)) {
321 		inode->i_mapping->a_ops = &f2fs_meta_aops;
322 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
323 	} else if (S_ISREG(inode->i_mode)) {
324 		inode->i_op = &f2fs_file_inode_operations;
325 		inode->i_fop = &f2fs_file_operations;
326 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
327 	} else if (S_ISDIR(inode->i_mode)) {
328 		inode->i_op = &f2fs_dir_inode_operations;
329 		inode->i_fop = &f2fs_dir_operations;
330 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
331 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
332 	} else if (S_ISLNK(inode->i_mode)) {
333 		if (f2fs_encrypted_inode(inode))
334 			inode->i_op = &f2fs_encrypted_symlink_inode_operations;
335 		else
336 			inode->i_op = &f2fs_symlink_inode_operations;
337 		inode_nohighmem(inode);
338 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
339 	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
340 			S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
341 		inode->i_op = &f2fs_special_inode_operations;
342 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
343 	} else {
344 		ret = -EIO;
345 		goto bad_inode;
346 	}
347 	f2fs_set_inode_flags(inode);
348 	unlock_new_inode(inode);
349 	trace_f2fs_iget(inode);
350 	return inode;
351 
352 bad_inode:
353 	iget_failed(inode);
354 	trace_f2fs_iget_exit(inode, ret);
355 	return ERR_PTR(ret);
356 }
357 
358 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
359 {
360 	struct inode *inode;
361 retry:
362 	inode = f2fs_iget(sb, ino);
363 	if (IS_ERR(inode)) {
364 		if (PTR_ERR(inode) == -ENOMEM) {
365 			congestion_wait(BLK_RW_ASYNC, HZ/50);
366 			goto retry;
367 		}
368 	}
369 	return inode;
370 }
371 
372 void update_inode(struct inode *inode, struct page *node_page)
373 {
374 	struct f2fs_inode *ri;
375 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
376 
377 	f2fs_wait_on_page_writeback(node_page, NODE, true);
378 	set_page_dirty(node_page);
379 
380 	f2fs_inode_synced(inode);
381 
382 	ri = F2FS_INODE(node_page);
383 
384 	ri->i_mode = cpu_to_le16(inode->i_mode);
385 	ri->i_advise = F2FS_I(inode)->i_advise;
386 	ri->i_uid = cpu_to_le32(i_uid_read(inode));
387 	ri->i_gid = cpu_to_le32(i_gid_read(inode));
388 	ri->i_links = cpu_to_le32(inode->i_nlink);
389 	ri->i_size = cpu_to_le64(i_size_read(inode));
390 	ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
391 
392 	if (et) {
393 		read_lock(&et->lock);
394 		set_raw_extent(&et->largest, &ri->i_ext);
395 		read_unlock(&et->lock);
396 	} else {
397 		memset(&ri->i_ext, 0, sizeof(ri->i_ext));
398 	}
399 	set_raw_inline(inode, ri);
400 
401 	ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
402 	ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
403 	ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
404 	ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
405 	ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
406 	ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
407 	ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
408 	ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
409 	ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
410 	ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
411 	ri->i_generation = cpu_to_le32(inode->i_generation);
412 	ri->i_dir_level = F2FS_I(inode)->i_dir_level;
413 
414 	if (f2fs_has_extra_attr(inode)) {
415 		ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
416 
417 		if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb))
418 			ri->i_inline_xattr_size =
419 				cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
420 
421 		if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
422 			F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
423 								i_projid)) {
424 			projid_t i_projid;
425 
426 			i_projid = from_kprojid(&init_user_ns,
427 						F2FS_I(inode)->i_projid);
428 			ri->i_projid = cpu_to_le32(i_projid);
429 		}
430 
431 		if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) &&
432 			F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
433 								i_crtime)) {
434 			ri->i_crtime =
435 				cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
436 			ri->i_crtime_nsec =
437 				cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
438 		}
439 	}
440 
441 	__set_inode_rdev(inode, ri);
442 	set_cold_node(inode, node_page);
443 
444 	/* deleted inode */
445 	if (inode->i_nlink == 0)
446 		clear_inline_node(node_page);
447 
448 }
449 
450 void update_inode_page(struct inode *inode)
451 {
452 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
453 	struct page *node_page;
454 retry:
455 	node_page = get_node_page(sbi, inode->i_ino);
456 	if (IS_ERR(node_page)) {
457 		int err = PTR_ERR(node_page);
458 		if (err == -ENOMEM) {
459 			cond_resched();
460 			goto retry;
461 		} else if (err != -ENOENT) {
462 			f2fs_stop_checkpoint(sbi, false);
463 		}
464 		return;
465 	}
466 	update_inode(inode, node_page);
467 	f2fs_put_page(node_page, 1);
468 }
469 
470 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
471 {
472 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
473 
474 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
475 			inode->i_ino == F2FS_META_INO(sbi))
476 		return 0;
477 
478 	if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
479 		return 0;
480 
481 	/*
482 	 * We need to balance fs here to prevent from producing dirty node pages
483 	 * during the urgent cleaning time when runing out of free sections.
484 	 */
485 	update_inode_page(inode);
486 	if (wbc && wbc->nr_to_write)
487 		f2fs_balance_fs(sbi, true);
488 	return 0;
489 }
490 
491 /*
492  * Called at the last iput() if i_nlink is zero
493  */
494 void f2fs_evict_inode(struct inode *inode)
495 {
496 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
497 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
498 	int err = 0;
499 
500 	/* some remained atomic pages should discarded */
501 	if (f2fs_is_atomic_file(inode))
502 		drop_inmem_pages(inode);
503 
504 	trace_f2fs_evict_inode(inode);
505 	truncate_inode_pages_final(&inode->i_data);
506 
507 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
508 			inode->i_ino == F2FS_META_INO(sbi))
509 		goto out_clear;
510 
511 	f2fs_bug_on(sbi, get_dirty_pages(inode));
512 	remove_dirty_inode(inode);
513 
514 	f2fs_destroy_extent_tree(inode);
515 
516 	if (inode->i_nlink || is_bad_inode(inode))
517 		goto no_delete;
518 
519 	dquot_initialize(inode);
520 
521 	remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
522 	remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
523 	remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
524 
525 	sb_start_intwrite(inode->i_sb);
526 	set_inode_flag(inode, FI_NO_ALLOC);
527 	i_size_write(inode, 0);
528 retry:
529 	if (F2FS_HAS_BLOCKS(inode))
530 		err = f2fs_truncate(inode);
531 
532 #ifdef CONFIG_F2FS_FAULT_INJECTION
533 	if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
534 		f2fs_show_injection_info(FAULT_EVICT_INODE);
535 		err = -EIO;
536 	}
537 #endif
538 	if (!err) {
539 		f2fs_lock_op(sbi);
540 		err = remove_inode_page(inode);
541 		f2fs_unlock_op(sbi);
542 		if (err == -ENOENT)
543 			err = 0;
544 	}
545 
546 	/* give more chances, if ENOMEM case */
547 	if (err == -ENOMEM) {
548 		err = 0;
549 		goto retry;
550 	}
551 
552 	if (err)
553 		update_inode_page(inode);
554 	dquot_free_inode(inode);
555 	sb_end_intwrite(inode->i_sb);
556 no_delete:
557 	dquot_drop(inode);
558 
559 	stat_dec_inline_xattr(inode);
560 	stat_dec_inline_dir(inode);
561 	stat_dec_inline_inode(inode);
562 
563 	if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG)))
564 		f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
565 	else
566 		f2fs_inode_synced(inode);
567 
568 	/* ino == 0, if f2fs_new_inode() was failed t*/
569 	if (inode->i_ino)
570 		invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
571 							inode->i_ino);
572 	if (xnid)
573 		invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
574 	if (inode->i_nlink) {
575 		if (is_inode_flag_set(inode, FI_APPEND_WRITE))
576 			add_ino_entry(sbi, inode->i_ino, APPEND_INO);
577 		if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
578 			add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
579 	}
580 	if (is_inode_flag_set(inode, FI_FREE_NID)) {
581 		alloc_nid_failed(sbi, inode->i_ino);
582 		clear_inode_flag(inode, FI_FREE_NID);
583 	} else {
584 		f2fs_bug_on(sbi, err &&
585 			!exist_written_data(sbi, inode->i_ino, ORPHAN_INO));
586 	}
587 out_clear:
588 	fscrypt_put_encryption_info(inode);
589 	clear_inode(inode);
590 }
591 
592 /* caller should call f2fs_lock_op() */
593 void handle_failed_inode(struct inode *inode)
594 {
595 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
596 	struct node_info ni;
597 
598 	/*
599 	 * clear nlink of inode in order to release resource of inode
600 	 * immediately.
601 	 */
602 	clear_nlink(inode);
603 
604 	/*
605 	 * we must call this to avoid inode being remained as dirty, resulting
606 	 * in a panic when flushing dirty inodes in gdirty_list.
607 	 */
608 	update_inode_page(inode);
609 	f2fs_inode_synced(inode);
610 
611 	/* don't make bad inode, since it becomes a regular file. */
612 	unlock_new_inode(inode);
613 
614 	/*
615 	 * Note: we should add inode to orphan list before f2fs_unlock_op()
616 	 * so we can prevent losing this orphan when encoutering checkpoint
617 	 * and following suddenly power-off.
618 	 */
619 	get_node_info(sbi, inode->i_ino, &ni);
620 
621 	if (ni.blk_addr != NULL_ADDR) {
622 		int err = acquire_orphan_inode(sbi);
623 		if (err) {
624 			set_sbi_flag(sbi, SBI_NEED_FSCK);
625 			f2fs_msg(sbi->sb, KERN_WARNING,
626 				"Too many orphan inodes, run fsck to fix.");
627 		} else {
628 			add_orphan_inode(inode);
629 		}
630 		alloc_nid_done(sbi, inode->i_ino);
631 	} else {
632 		set_inode_flag(inode, FI_FREE_NID);
633 	}
634 
635 	f2fs_unlock_op(sbi);
636 
637 	/* iput will drop the inode object */
638 	iput(inode);
639 }
640