xref: /openbmc/linux/fs/f2fs/inode.c (revision 9cfc5c90)
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/writeback.h>
15 
16 #include "f2fs.h"
17 #include "node.h"
18 
19 #include <trace/events/f2fs.h>
20 
21 void f2fs_set_inode_flags(struct inode *inode)
22 {
23 	unsigned int flags = F2FS_I(inode)->i_flags;
24 	unsigned int new_fl = 0;
25 
26 	if (flags & FS_SYNC_FL)
27 		new_fl |= S_SYNC;
28 	if (flags & FS_APPEND_FL)
29 		new_fl |= S_APPEND;
30 	if (flags & FS_IMMUTABLE_FL)
31 		new_fl |= S_IMMUTABLE;
32 	if (flags & FS_NOATIME_FL)
33 		new_fl |= S_NOATIME;
34 	if (flags & FS_DIRSYNC_FL)
35 		new_fl |= S_DIRSYNC;
36 	inode_set_flags(inode, new_fl,
37 			S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
38 }
39 
40 static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
41 {
42 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
43 			S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
44 		if (ri->i_addr[0])
45 			inode->i_rdev =
46 				old_decode_dev(le32_to_cpu(ri->i_addr[0]));
47 		else
48 			inode->i_rdev =
49 				new_decode_dev(le32_to_cpu(ri->i_addr[1]));
50 	}
51 }
52 
53 static bool __written_first_block(struct f2fs_inode *ri)
54 {
55 	block_t addr = le32_to_cpu(ri->i_addr[0]);
56 
57 	if (addr != NEW_ADDR && addr != NULL_ADDR)
58 		return true;
59 	return false;
60 }
61 
62 static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
63 {
64 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
65 		if (old_valid_dev(inode->i_rdev)) {
66 			ri->i_addr[0] =
67 				cpu_to_le32(old_encode_dev(inode->i_rdev));
68 			ri->i_addr[1] = 0;
69 		} else {
70 			ri->i_addr[0] = 0;
71 			ri->i_addr[1] =
72 				cpu_to_le32(new_encode_dev(inode->i_rdev));
73 			ri->i_addr[2] = 0;
74 		}
75 	}
76 }
77 
78 static void __recover_inline_status(struct inode *inode, struct page *ipage)
79 {
80 	void *inline_data = inline_data_addr(ipage);
81 	__le32 *start = inline_data;
82 	__le32 *end = start + MAX_INLINE_DATA / sizeof(__le32);
83 
84 	while (start < end) {
85 		if (*start++) {
86 			f2fs_wait_on_page_writeback(ipage, NODE);
87 
88 			set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
89 			set_raw_inline(F2FS_I(inode), F2FS_INODE(ipage));
90 			set_page_dirty(ipage);
91 			return;
92 		}
93 	}
94 	return;
95 }
96 
97 static int do_read_inode(struct inode *inode)
98 {
99 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
100 	struct f2fs_inode_info *fi = F2FS_I(inode);
101 	struct page *node_page;
102 	struct f2fs_inode *ri;
103 
104 	/* Check if ino is within scope */
105 	if (check_nid_range(sbi, inode->i_ino)) {
106 		f2fs_msg(inode->i_sb, KERN_ERR, "bad inode number: %lu",
107 			 (unsigned long) inode->i_ino);
108 		WARN_ON(1);
109 		return -EINVAL;
110 	}
111 
112 	node_page = get_node_page(sbi, inode->i_ino);
113 	if (IS_ERR(node_page))
114 		return PTR_ERR(node_page);
115 
116 	ri = F2FS_INODE(node_page);
117 
118 	inode->i_mode = le16_to_cpu(ri->i_mode);
119 	i_uid_write(inode, le32_to_cpu(ri->i_uid));
120 	i_gid_write(inode, le32_to_cpu(ri->i_gid));
121 	set_nlink(inode, le32_to_cpu(ri->i_links));
122 	inode->i_size = le64_to_cpu(ri->i_size);
123 	inode->i_blocks = le64_to_cpu(ri->i_blocks);
124 
125 	inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
126 	inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
127 	inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
128 	inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
129 	inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
130 	inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
131 	inode->i_generation = le32_to_cpu(ri->i_generation);
132 
133 	fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
134 	fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
135 	fi->i_flags = le32_to_cpu(ri->i_flags);
136 	fi->flags = 0;
137 	fi->i_advise = ri->i_advise;
138 	fi->i_pino = le32_to_cpu(ri->i_pino);
139 	fi->i_dir_level = ri->i_dir_level;
140 
141 	f2fs_init_extent_tree(inode, &ri->i_ext);
142 
143 	get_inline_info(fi, ri);
144 
145 	/* check data exist */
146 	if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
147 		__recover_inline_status(inode, node_page);
148 
149 	/* get rdev by using inline_info */
150 	__get_inode_rdev(inode, ri);
151 
152 	if (__written_first_block(ri))
153 		set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
154 
155 	f2fs_put_page(node_page, 1);
156 
157 	stat_inc_inline_xattr(inode);
158 	stat_inc_inline_inode(inode);
159 	stat_inc_inline_dir(inode);
160 
161 	return 0;
162 }
163 
164 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
165 {
166 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
167 	struct inode *inode;
168 	int ret = 0;
169 
170 	inode = iget_locked(sb, ino);
171 	if (!inode)
172 		return ERR_PTR(-ENOMEM);
173 
174 	if (!(inode->i_state & I_NEW)) {
175 		trace_f2fs_iget(inode);
176 		return inode;
177 	}
178 	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
179 		goto make_now;
180 
181 	ret = do_read_inode(inode);
182 	if (ret)
183 		goto bad_inode;
184 make_now:
185 	if (ino == F2FS_NODE_INO(sbi)) {
186 		inode->i_mapping->a_ops = &f2fs_node_aops;
187 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
188 	} else if (ino == F2FS_META_INO(sbi)) {
189 		inode->i_mapping->a_ops = &f2fs_meta_aops;
190 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
191 	} else if (S_ISREG(inode->i_mode)) {
192 		inode->i_op = &f2fs_file_inode_operations;
193 		inode->i_fop = &f2fs_file_operations;
194 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
195 	} else if (S_ISDIR(inode->i_mode)) {
196 		inode->i_op = &f2fs_dir_inode_operations;
197 		inode->i_fop = &f2fs_dir_operations;
198 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
199 		mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
200 	} else if (S_ISLNK(inode->i_mode)) {
201 		if (f2fs_encrypted_inode(inode))
202 			inode->i_op = &f2fs_encrypted_symlink_inode_operations;
203 		else
204 			inode->i_op = &f2fs_symlink_inode_operations;
205 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
206 	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
207 			S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
208 		inode->i_op = &f2fs_special_inode_operations;
209 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
210 	} else {
211 		ret = -EIO;
212 		goto bad_inode;
213 	}
214 	unlock_new_inode(inode);
215 	trace_f2fs_iget(inode);
216 	return inode;
217 
218 bad_inode:
219 	iget_failed(inode);
220 	trace_f2fs_iget_exit(inode, ret);
221 	return ERR_PTR(ret);
222 }
223 
224 void update_inode(struct inode *inode, struct page *node_page)
225 {
226 	struct f2fs_inode *ri;
227 
228 	f2fs_wait_on_page_writeback(node_page, NODE);
229 
230 	ri = F2FS_INODE(node_page);
231 
232 	ri->i_mode = cpu_to_le16(inode->i_mode);
233 	ri->i_advise = F2FS_I(inode)->i_advise;
234 	ri->i_uid = cpu_to_le32(i_uid_read(inode));
235 	ri->i_gid = cpu_to_le32(i_gid_read(inode));
236 	ri->i_links = cpu_to_le32(inode->i_nlink);
237 	ri->i_size = cpu_to_le64(i_size_read(inode));
238 	ri->i_blocks = cpu_to_le64(inode->i_blocks);
239 
240 	if (F2FS_I(inode)->extent_tree)
241 		set_raw_extent(&F2FS_I(inode)->extent_tree->largest,
242 							&ri->i_ext);
243 	else
244 		memset(&ri->i_ext, 0, sizeof(ri->i_ext));
245 	set_raw_inline(F2FS_I(inode), ri);
246 
247 	ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
248 	ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
249 	ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
250 	ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
251 	ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
252 	ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
253 	ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
254 	ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
255 	ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
256 	ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
257 	ri->i_generation = cpu_to_le32(inode->i_generation);
258 	ri->i_dir_level = F2FS_I(inode)->i_dir_level;
259 
260 	__set_inode_rdev(inode, ri);
261 	set_cold_node(inode, node_page);
262 	set_page_dirty(node_page);
263 
264 	clear_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
265 }
266 
267 void update_inode_page(struct inode *inode)
268 {
269 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
270 	struct page *node_page;
271 retry:
272 	node_page = get_node_page(sbi, inode->i_ino);
273 	if (IS_ERR(node_page)) {
274 		int err = PTR_ERR(node_page);
275 		if (err == -ENOMEM) {
276 			cond_resched();
277 			goto retry;
278 		} else if (err != -ENOENT) {
279 			f2fs_stop_checkpoint(sbi);
280 		}
281 		return;
282 	}
283 	update_inode(inode, node_page);
284 	f2fs_put_page(node_page, 1);
285 }
286 
287 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
288 {
289 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
290 
291 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
292 			inode->i_ino == F2FS_META_INO(sbi))
293 		return 0;
294 
295 	if (!is_inode_flag_set(F2FS_I(inode), FI_DIRTY_INODE))
296 		return 0;
297 
298 	/*
299 	 * We need to balance fs here to prevent from producing dirty node pages
300 	 * during the urgent cleaning time when runing out of free sections.
301 	 */
302 	update_inode_page(inode);
303 
304 	f2fs_balance_fs(sbi);
305 	return 0;
306 }
307 
308 /*
309  * Called at the last iput() if i_nlink is zero
310  */
311 void f2fs_evict_inode(struct inode *inode)
312 {
313 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
314 	struct f2fs_inode_info *fi = F2FS_I(inode);
315 	nid_t xnid = fi->i_xattr_nid;
316 	int err = 0;
317 
318 	/* some remained atomic pages should discarded */
319 	if (f2fs_is_atomic_file(inode))
320 		commit_inmem_pages(inode, true);
321 
322 	trace_f2fs_evict_inode(inode);
323 	truncate_inode_pages_final(&inode->i_data);
324 
325 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
326 			inode->i_ino == F2FS_META_INO(sbi))
327 		goto out_clear;
328 
329 	f2fs_bug_on(sbi, get_dirty_pages(inode));
330 	remove_dirty_dir_inode(inode);
331 
332 	f2fs_destroy_extent_tree(inode);
333 
334 	if (inode->i_nlink || is_bad_inode(inode))
335 		goto no_delete;
336 
337 	sb_start_intwrite(inode->i_sb);
338 	set_inode_flag(fi, FI_NO_ALLOC);
339 	i_size_write(inode, 0);
340 
341 	if (F2FS_HAS_BLOCKS(inode))
342 		err = f2fs_truncate(inode, true);
343 
344 	if (!err) {
345 		f2fs_lock_op(sbi);
346 		err = remove_inode_page(inode);
347 		f2fs_unlock_op(sbi);
348 	}
349 
350 	sb_end_intwrite(inode->i_sb);
351 no_delete:
352 	stat_dec_inline_xattr(inode);
353 	stat_dec_inline_dir(inode);
354 	stat_dec_inline_inode(inode);
355 
356 	invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, inode->i_ino);
357 	if (xnid)
358 		invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
359 	if (is_inode_flag_set(fi, FI_APPEND_WRITE))
360 		add_dirty_inode(sbi, inode->i_ino, APPEND_INO);
361 	if (is_inode_flag_set(fi, FI_UPDATE_WRITE))
362 		add_dirty_inode(sbi, inode->i_ino, UPDATE_INO);
363 	if (is_inode_flag_set(fi, FI_FREE_NID)) {
364 		if (err && err != -ENOENT)
365 			alloc_nid_done(sbi, inode->i_ino);
366 		else
367 			alloc_nid_failed(sbi, inode->i_ino);
368 		clear_inode_flag(fi, FI_FREE_NID);
369 	}
370 
371 	if (err && err != -ENOENT) {
372 		if (!exist_written_data(sbi, inode->i_ino, ORPHAN_INO)) {
373 			/*
374 			 * get here because we failed to release resource
375 			 * of inode previously, reminder our user to run fsck
376 			 * for fixing.
377 			 */
378 			set_sbi_flag(sbi, SBI_NEED_FSCK);
379 			f2fs_msg(sbi->sb, KERN_WARNING,
380 				"inode (ino:%lu) resource leak, run fsck "
381 				"to fix this issue!", inode->i_ino);
382 		}
383 	}
384 out_clear:
385 #ifdef CONFIG_F2FS_FS_ENCRYPTION
386 	if (fi->i_crypt_info)
387 		f2fs_free_encryption_info(inode, fi->i_crypt_info);
388 #endif
389 	clear_inode(inode);
390 }
391 
392 /* caller should call f2fs_lock_op() */
393 void handle_failed_inode(struct inode *inode)
394 {
395 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
396 	int err = 0;
397 
398 	clear_nlink(inode);
399 	make_bad_inode(inode);
400 	unlock_new_inode(inode);
401 
402 	i_size_write(inode, 0);
403 	if (F2FS_HAS_BLOCKS(inode))
404 		err = f2fs_truncate(inode, false);
405 
406 	if (!err)
407 		err = remove_inode_page(inode);
408 
409 	/*
410 	 * if we skip truncate_node in remove_inode_page bacause we failed
411 	 * before, it's better to find another way to release resource of
412 	 * this inode (e.g. valid block count, node block or nid). Here we
413 	 * choose to add this inode to orphan list, so that we can call iput
414 	 * for releasing in orphan recovery flow.
415 	 *
416 	 * Note: we should add inode to orphan list before f2fs_unlock_op()
417 	 * so we can prevent losing this orphan when encoutering checkpoint
418 	 * and following suddenly power-off.
419 	 */
420 	if (err && err != -ENOENT) {
421 		err = acquire_orphan_inode(sbi);
422 		if (!err)
423 			add_orphan_inode(sbi, inode->i_ino);
424 	}
425 
426 	set_inode_flag(F2FS_I(inode), FI_FREE_NID);
427 	f2fs_unlock_op(sbi);
428 
429 	/* iput will drop the inode object */
430 	iput(inode);
431 }
432