Linux Audio

Check our new training course

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