xref: /openbmc/linux/fs/f2fs/inode.c (revision d2ba09c1)
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_I(inode)->i_disk_time[0] = inode->i_atime;
288 	F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
289 	F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
290 	F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
291 	f2fs_put_page(node_page, 1);
292 
293 	stat_inc_inline_xattr(inode);
294 	stat_inc_inline_inode(inode);
295 	stat_inc_inline_dir(inode);
296 
297 	return 0;
298 }
299 
300 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
301 {
302 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
303 	struct inode *inode;
304 	int ret = 0;
305 
306 	inode = iget_locked(sb, ino);
307 	if (!inode)
308 		return ERR_PTR(-ENOMEM);
309 
310 	if (!(inode->i_state & I_NEW)) {
311 		trace_f2fs_iget(inode);
312 		return inode;
313 	}
314 	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
315 		goto make_now;
316 
317 	ret = do_read_inode(inode);
318 	if (ret)
319 		goto bad_inode;
320 make_now:
321 	if (ino == F2FS_NODE_INO(sbi)) {
322 		inode->i_mapping->a_ops = &f2fs_node_aops;
323 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
324 	} else if (ino == F2FS_META_INO(sbi)) {
325 		inode->i_mapping->a_ops = &f2fs_meta_aops;
326 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
327 	} else if (S_ISREG(inode->i_mode)) {
328 		inode->i_op = &f2fs_file_inode_operations;
329 		inode->i_fop = &f2fs_file_operations;
330 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
331 	} else if (S_ISDIR(inode->i_mode)) {
332 		inode->i_op = &f2fs_dir_inode_operations;
333 		inode->i_fop = &f2fs_dir_operations;
334 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
335 		inode_nohighmem(inode);
336 	} else if (S_ISLNK(inode->i_mode)) {
337 		if (f2fs_encrypted_inode(inode))
338 			inode->i_op = &f2fs_encrypted_symlink_inode_operations;
339 		else
340 			inode->i_op = &f2fs_symlink_inode_operations;
341 		inode_nohighmem(inode);
342 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
343 	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
344 			S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
345 		inode->i_op = &f2fs_special_inode_operations;
346 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
347 	} else {
348 		ret = -EIO;
349 		goto bad_inode;
350 	}
351 	f2fs_set_inode_flags(inode);
352 	unlock_new_inode(inode);
353 	trace_f2fs_iget(inode);
354 	return inode;
355 
356 bad_inode:
357 	iget_failed(inode);
358 	trace_f2fs_iget_exit(inode, ret);
359 	return ERR_PTR(ret);
360 }
361 
362 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
363 {
364 	struct inode *inode;
365 retry:
366 	inode = f2fs_iget(sb, ino);
367 	if (IS_ERR(inode)) {
368 		if (PTR_ERR(inode) == -ENOMEM) {
369 			congestion_wait(BLK_RW_ASYNC, HZ/50);
370 			goto retry;
371 		}
372 	}
373 	return inode;
374 }
375 
376 void update_inode(struct inode *inode, struct page *node_page)
377 {
378 	struct f2fs_inode *ri;
379 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
380 
381 	f2fs_wait_on_page_writeback(node_page, NODE, true);
382 	set_page_dirty(node_page);
383 
384 	f2fs_inode_synced(inode);
385 
386 	ri = F2FS_INODE(node_page);
387 
388 	ri->i_mode = cpu_to_le16(inode->i_mode);
389 	ri->i_advise = F2FS_I(inode)->i_advise;
390 	ri->i_uid = cpu_to_le32(i_uid_read(inode));
391 	ri->i_gid = cpu_to_le32(i_gid_read(inode));
392 	ri->i_links = cpu_to_le32(inode->i_nlink);
393 	ri->i_size = cpu_to_le64(i_size_read(inode));
394 	ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
395 
396 	if (et) {
397 		read_lock(&et->lock);
398 		set_raw_extent(&et->largest, &ri->i_ext);
399 		read_unlock(&et->lock);
400 	} else {
401 		memset(&ri->i_ext, 0, sizeof(ri->i_ext));
402 	}
403 	set_raw_inline(inode, ri);
404 
405 	ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
406 	ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
407 	ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
408 	ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
409 	ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
410 	ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
411 	ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
412 	ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
413 	ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
414 	ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
415 	ri->i_generation = cpu_to_le32(inode->i_generation);
416 	ri->i_dir_level = F2FS_I(inode)->i_dir_level;
417 
418 	if (f2fs_has_extra_attr(inode)) {
419 		ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
420 
421 		if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb))
422 			ri->i_inline_xattr_size =
423 				cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
424 
425 		if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
426 			F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
427 								i_projid)) {
428 			projid_t i_projid;
429 
430 			i_projid = from_kprojid(&init_user_ns,
431 						F2FS_I(inode)->i_projid);
432 			ri->i_projid = cpu_to_le32(i_projid);
433 		}
434 
435 		if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) &&
436 			F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
437 								i_crtime)) {
438 			ri->i_crtime =
439 				cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
440 			ri->i_crtime_nsec =
441 				cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
442 		}
443 	}
444 
445 	__set_inode_rdev(inode, ri);
446 
447 	/* deleted inode */
448 	if (inode->i_nlink == 0)
449 		clear_inline_node(node_page);
450 
451 	F2FS_I(inode)->i_disk_time[0] = inode->i_atime;
452 	F2FS_I(inode)->i_disk_time[1] = inode->i_ctime;
453 	F2FS_I(inode)->i_disk_time[2] = inode->i_mtime;
454 	F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
455 }
456 
457 void update_inode_page(struct inode *inode)
458 {
459 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
460 	struct page *node_page;
461 retry:
462 	node_page = get_node_page(sbi, inode->i_ino);
463 	if (IS_ERR(node_page)) {
464 		int err = PTR_ERR(node_page);
465 		if (err == -ENOMEM) {
466 			cond_resched();
467 			goto retry;
468 		} else if (err != -ENOENT) {
469 			f2fs_stop_checkpoint(sbi, false);
470 		}
471 		return;
472 	}
473 	update_inode(inode, node_page);
474 	f2fs_put_page(node_page, 1);
475 }
476 
477 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
478 {
479 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
480 
481 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
482 			inode->i_ino == F2FS_META_INO(sbi))
483 		return 0;
484 
485 	if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
486 		return 0;
487 
488 	/*
489 	 * We need to balance fs here to prevent from producing dirty node pages
490 	 * during the urgent cleaning time when runing out of free sections.
491 	 */
492 	update_inode_page(inode);
493 	if (wbc && wbc->nr_to_write)
494 		f2fs_balance_fs(sbi, true);
495 	return 0;
496 }
497 
498 /*
499  * Called at the last iput() if i_nlink is zero
500  */
501 void f2fs_evict_inode(struct inode *inode)
502 {
503 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
504 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
505 	int err = 0;
506 
507 	/* some remained atomic pages should discarded */
508 	if (f2fs_is_atomic_file(inode))
509 		drop_inmem_pages(inode);
510 
511 	trace_f2fs_evict_inode(inode);
512 	truncate_inode_pages_final(&inode->i_data);
513 
514 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
515 			inode->i_ino == F2FS_META_INO(sbi))
516 		goto out_clear;
517 
518 	f2fs_bug_on(sbi, get_dirty_pages(inode));
519 	remove_dirty_inode(inode);
520 
521 	f2fs_destroy_extent_tree(inode);
522 
523 	if (inode->i_nlink || is_bad_inode(inode))
524 		goto no_delete;
525 
526 	dquot_initialize(inode);
527 
528 	remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
529 	remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
530 	remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
531 
532 	sb_start_intwrite(inode->i_sb);
533 	set_inode_flag(inode, FI_NO_ALLOC);
534 	i_size_write(inode, 0);
535 retry:
536 	if (F2FS_HAS_BLOCKS(inode))
537 		err = f2fs_truncate(inode);
538 
539 #ifdef CONFIG_F2FS_FAULT_INJECTION
540 	if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
541 		f2fs_show_injection_info(FAULT_EVICT_INODE);
542 		err = -EIO;
543 	}
544 #endif
545 	if (!err) {
546 		f2fs_lock_op(sbi);
547 		err = remove_inode_page(inode);
548 		f2fs_unlock_op(sbi);
549 		if (err == -ENOENT)
550 			err = 0;
551 	}
552 
553 	/* give more chances, if ENOMEM case */
554 	if (err == -ENOMEM) {
555 		err = 0;
556 		goto retry;
557 	}
558 
559 	if (err)
560 		update_inode_page(inode);
561 	dquot_free_inode(inode);
562 	sb_end_intwrite(inode->i_sb);
563 no_delete:
564 	dquot_drop(inode);
565 
566 	stat_dec_inline_xattr(inode);
567 	stat_dec_inline_dir(inode);
568 	stat_dec_inline_inode(inode);
569 
570 	if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG)))
571 		f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
572 	else
573 		f2fs_inode_synced(inode);
574 
575 	/* ino == 0, if f2fs_new_inode() was failed t*/
576 	if (inode->i_ino)
577 		invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
578 							inode->i_ino);
579 	if (xnid)
580 		invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
581 	if (inode->i_nlink) {
582 		if (is_inode_flag_set(inode, FI_APPEND_WRITE))
583 			add_ino_entry(sbi, inode->i_ino, APPEND_INO);
584 		if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
585 			add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
586 	}
587 	if (is_inode_flag_set(inode, FI_FREE_NID)) {
588 		alloc_nid_failed(sbi, inode->i_ino);
589 		clear_inode_flag(inode, FI_FREE_NID);
590 	} else {
591 		f2fs_bug_on(sbi, err &&
592 			!exist_written_data(sbi, inode->i_ino, ORPHAN_INO));
593 	}
594 out_clear:
595 	fscrypt_put_encryption_info(inode);
596 	clear_inode(inode);
597 }
598 
599 /* caller should call f2fs_lock_op() */
600 void handle_failed_inode(struct inode *inode)
601 {
602 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
603 	struct node_info ni;
604 
605 	/*
606 	 * clear nlink of inode in order to release resource of inode
607 	 * immediately.
608 	 */
609 	clear_nlink(inode);
610 
611 	/*
612 	 * we must call this to avoid inode being remained as dirty, resulting
613 	 * in a panic when flushing dirty inodes in gdirty_list.
614 	 */
615 	update_inode_page(inode);
616 	f2fs_inode_synced(inode);
617 
618 	/* don't make bad inode, since it becomes a regular file. */
619 	unlock_new_inode(inode);
620 
621 	/*
622 	 * Note: we should add inode to orphan list before f2fs_unlock_op()
623 	 * so we can prevent losing this orphan when encoutering checkpoint
624 	 * and following suddenly power-off.
625 	 */
626 	get_node_info(sbi, inode->i_ino, &ni);
627 
628 	if (ni.blk_addr != NULL_ADDR) {
629 		int err = acquire_orphan_inode(sbi);
630 		if (err) {
631 			set_sbi_flag(sbi, SBI_NEED_FSCK);
632 			f2fs_msg(sbi->sb, KERN_WARNING,
633 				"Too many orphan inodes, run fsck to fix.");
634 		} else {
635 			add_orphan_inode(inode);
636 		}
637 		alloc_nid_done(sbi, inode->i_ino);
638 	} else {
639 		set_inode_flag(inode, FI_FREE_NID);
640 	}
641 
642 	f2fs_unlock_op(sbi);
643 
644 	/* iput will drop the inode object */
645 	iput(inode);
646 }
647