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