blob: 22a5607a051837266c9c9d1722f65c3e36cd3758 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +09002 * 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>
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -070014#include <linux/backing-dev.h>
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090015#include <linux/writeback.h>
16
17#include "f2fs.h"
18#include "node.h"
Chao Yu0eb0ada2017-06-14 23:00:56 +080019#include "segment.h"
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090020
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090021#include <trace/events/f2fs.h>
22
Jaegeuk Kim7c457292016-10-14 11:51:23 -070023void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070024{
Jaegeuk Kim7c457292016-10-14 11:51:23 -070025 if (f2fs_inode_dirtied(inode, sync))
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070026 return;
Jaegeuk Kim7c457292016-10-14 11:51:23 -070027
Jaegeuk Kimb56ab832016-06-30 19:09:37 -070028 mark_inode_dirty_sync(inode);
29}
30
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090031void f2fs_set_inode_flags(struct inode *inode)
32{
33 unsigned int flags = F2FS_I(inode)->i_flags;
Zhang Zhen8abfb362014-04-15 14:19:38 +080034 unsigned int new_fl = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090035
36 if (flags & FS_SYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080037 new_fl |= S_SYNC;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090038 if (flags & FS_APPEND_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080039 new_fl |= S_APPEND;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090040 if (flags & FS_IMMUTABLE_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080041 new_fl |= S_IMMUTABLE;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090042 if (flags & FS_NOATIME_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080043 new_fl |= S_NOATIME;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090044 if (flags & FS_DIRSYNC_FL)
Zhang Zhen8abfb362014-04-15 14:19:38 +080045 new_fl |= S_DIRSYNC;
Eric Biggers2ee6a572017-10-09 12:15:35 -070046 if (f2fs_encrypted_inode(inode))
47 new_fl |= S_ENCRYPTED;
Zhang Zhen6a678852015-08-24 10:41:32 +080048 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -070049 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
50 S_ENCRYPTED);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090051}
52
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090053static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
54{
Chao Yu7a2af762017-07-19 00:19:06 +080055 int extra_size = get_extra_isize(inode);
56
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090057 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
58 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Chao Yu7a2af762017-07-19 00:19:06 +080059 if (ri->i_addr[extra_size])
60 inode->i_rdev = old_decode_dev(
61 le32_to_cpu(ri->i_addr[extra_size]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090062 else
Chao Yu7a2af762017-07-19 00:19:06 +080063 inode->i_rdev = new_decode_dev(
64 le32_to_cpu(ri->i_addr[extra_size + 1]));
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090065 }
66}
67
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070068static bool __written_first_block(struct f2fs_inode *ri)
69{
Chao Yu7a2af762017-07-19 00:19:06 +080070 block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
Jaegeuk Kimadad81e2015-03-24 12:04:20 -070071
72 if (addr != NEW_ADDR && addr != NULL_ADDR)
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -070073 return true;
74 return false;
75}
76
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090077static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
78{
Chao Yu7a2af762017-07-19 00:19:06 +080079 int extra_size = get_extra_isize(inode);
80
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090081 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
82 if (old_valid_dev(inode->i_rdev)) {
Chao Yu7a2af762017-07-19 00:19:06 +080083 ri->i_addr[extra_size] =
Chris Fries6c311ec2014-01-17 14:44:39 -060084 cpu_to_le32(old_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080085 ri->i_addr[extra_size + 1] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090086 } else {
Chao Yu7a2af762017-07-19 00:19:06 +080087 ri->i_addr[extra_size] = 0;
88 ri->i_addr[extra_size + 1] =
Chris Fries6c311ec2014-01-17 14:44:39 -060089 cpu_to_le32(new_encode_dev(inode->i_rdev));
Chao Yu7a2af762017-07-19 00:19:06 +080090 ri->i_addr[extra_size + 2] = 0;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +090091 }
92 }
93}
94
Chao Yu9e5ba772015-01-06 14:28:43 +080095static void __recover_inline_status(struct inode *inode, struct page *ipage)
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -070096{
Chao Yuf2470372017-07-19 00:19:05 +080097 void *inline_data = inline_data_addr(inode, ipage);
Chao Yu9e5ba772015-01-06 14:28:43 +080098 __le32 *start = inline_data;
Chao Yuf2470372017-07-19 00:19:05 +080099 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700100
Chao Yu9e5ba772015-01-06 14:28:43 +0800101 while (start < end) {
102 if (*start++) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800103 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700104
Jaegeuk Kim91942322016-05-20 10:13:22 -0700105 set_inode_flag(inode, FI_DATA_EXIST);
106 set_raw_inline(inode, F2FS_INODE(ipage));
Chao Yu9e5ba772015-01-06 14:28:43 +0800107 set_page_dirty(ipage);
108 return;
109 }
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700110 }
Chao Yu9e5ba772015-01-06 14:28:43 +0800111 return;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700112}
113
Chao Yu704956e2017-07-31 20:19:09 +0800114static 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
131static __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
154bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
155{
156 struct f2fs_inode *ri;
157 __u32 provided, calculated;
158
Jaegeuk Kimee605232017-08-31 16:54:51 -0700159 if (!f2fs_enable_inode_chksum(sbi, page) ||
160 PageDirty(page) || PageWriteback(page))
Chao Yu704956e2017-07-31 20:19:09 +0800161 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
175void 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
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900185static int do_read_inode(struct inode *inode)
186{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700187 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900188 struct f2fs_inode_info *fi = F2FS_I(inode);
189 struct page *node_page;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900190 struct f2fs_inode *ri;
Chao Yu5c571322017-07-26 00:01:41 +0800191 projid_t i_projid;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900192
193 /* Check if ino is within scope */
Namjae Jeon064e0822013-03-17 17:27:20 +0900194 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);
Chao Yud6b7d4b2014-06-12 13:23:41 +0800197 WARN_ON(1);
Namjae Jeon064e0822013-03-17 17:27:20 +0900198 return -EINVAL;
199 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900200
201 node_page = get_node_page(sbi, inode->i_ino);
202 if (IS_ERR(node_page))
203 return PTR_ERR(node_page);
204
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900205 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900206
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);
Chao Yu000519f2017-07-06 01:11:31 +0800212 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900213
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;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900226 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900227 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900228 fi->i_dir_level = ri->i_dir_level;
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900229
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800230 if (f2fs_init_extent_tree(inode, &ri->i_ext))
231 set_page_dirty(node_page);
Chao Yu0c872e22015-02-05 17:46:29 +0800232
Jaegeuk Kim91942322016-05-20 10:13:22 -0700233 get_inline_info(inode, ri);
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900234
Chao Yu7a2af762017-07-19 00:19:06 +0800235 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
236 le16_to_cpu(ri->i_extra_isize) : 0;
237
Chao Yu6afc6622017-09-06 21:59:50 +0800238 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
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700255 /* check data exist */
256 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
Chao Yu9e5ba772015-01-06 14:28:43 +0800257 __recover_inline_status(inode, node_page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700258
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900259 /* get rdev by using inline_info */
260 __get_inode_rdev(inode, ri);
261
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700262 if (__written_first_block(ri))
Jaegeuk Kim91942322016-05-20 10:13:22 -0700263 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -0700264
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700265 if (!need_inode_block_update(sbi, inode->i_ino))
266 fi->last_disk_size = inode->i_size;
267
Chao Yu5c571322017-07-26 00:01:41 +0800268 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
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900278 f2fs_put_page(node_page, 1);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800279
Chao Yud5e8f6c2015-07-15 17:28:53 +0800280 stat_inc_inline_xattr(inode);
Jaegeuk Kim9d1015d2014-12-05 10:51:50 -0800281 stat_inc_inline_inode(inode);
282 stat_inc_inline_dir(inode);
283
Chao Yu9e5ba772015-01-06 14:28:43 +0800284 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900285}
286
287struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
288{
289 struct f2fs_sb_info *sbi = F2FS_SB(sb);
290 struct inode *inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900291 int ret = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900292
293 inode = iget_locked(sb, ino);
294 if (!inode)
295 return ERR_PTR(-ENOMEM);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900296
297 if (!(inode->i_state & I_NEW)) {
298 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900299 return inode;
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900300 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900301 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;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900307make_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;
Jaegeuk Kima78186e2014-10-17 17:57:29 -0700322 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900323 } else if (S_ISLNK(inode->i_mode)) {
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -0700324 if (f2fs_encrypted_inode(inode))
325 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
326 else
327 inode->i_op = &f2fs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500328 inode_nohighmem(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900329 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 }
Jaegeuk Kim93607122017-05-16 13:20:16 -0700338 f2fs_set_inode_flags(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900339 unlock_new_inode(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900340 trace_f2fs_iget(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900341 return inode;
342
343bad_inode:
344 iget_failed(inode);
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900345 trace_f2fs_iget_exit(inode, ret);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900346 return ERR_PTR(ret);
347}
348
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -0700349struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
350{
351 struct inode *inode;
352retry:
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
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800363int update_inode(struct inode *inode, struct page *node_page)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900364{
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900365 struct f2fs_inode *ri;
Chao Yub691d982016-10-11 22:57:05 +0800366 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900367
Jaegeuk Kim1e7c48f2016-06-02 14:15:56 -0700368 f2fs_inode_synced(inode);
369
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800370 f2fs_wait_on_page_writeback(node_page, NODE, true);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900371
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900372 ri = F2FS_INODE(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900373
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));
Chao Yu000519f2017-07-06 01:11:31 +0800380 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
Chao Yu0c872e22015-02-05 17:46:29 +0800381
Chao Yub691d982016-10-11 22:57:05 +0800382 if (et) {
383 read_lock(&et->lock);
384 set_raw_extent(&et->largest, &ri->i_ext);
385 read_unlock(&et->lock);
386 } else {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700387 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
Chao Yub691d982016-10-11 22:57:05 +0800388 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700389 set_raw_inline(inode, ri);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900390
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);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900400 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900401 ri->i_generation = cpu_to_le32(inode->i_generation);
Jaegeuk Kim38431542014-02-27 18:20:00 +0900402 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
Changman Lee7d79e752013-01-23 09:40:23 +0900403
Chao Yu5c571322017-07-26 00:01:41 +0800404 if (f2fs_has_extra_attr(inode)) {
Chao Yu7a2af762017-07-19 00:19:06 +0800405 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
406
Chao Yu6afc6622017-09-06 21:59:50 +0800407 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
Chao Yu5c571322017-07-26 00:01:41 +0800411 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
Jaegeuk Kim3d1e3802013-10-08 18:01:51 +0900422 __set_inode_rdev(inode, ri);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900423 set_cold_node(inode, node_page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800424
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -0800425 /* deleted inode */
426 if (inode->i_nlink == 0)
427 clear_inline_node(node_page);
428
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800429 return set_page_dirty(node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900430}
431
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800432int update_inode_page(struct inode *inode)
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900433{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700434 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900435 struct page *node_page;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800436 int ret = 0;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900437retry:
Jaegeuk Kim39936832012-11-22 16:21:29 +0900438 node_page = get_node_page(sbi, inode->i_ino);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900439 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) {
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700445 f2fs_stop_checkpoint(sbi, false);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900446 }
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800447 return 0;
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900448 }
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800449 ret = update_inode(inode, node_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900450 f2fs_put_page(node_page, 1);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800451 return ret;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900452}
453
454int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
455{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700456 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900457
458 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
459 inode->i_ino == F2FS_META_INO(sbi))
460 return 0;
461
Jaegeuk Kim91942322016-05-20 10:13:22 -0700462 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900463 return 0;
464
Jaegeuk Kim39936832012-11-22 16:21:29 +0900465 /*
Jaegeuk Kimc5cd29d2015-09-12 11:25:30 -0700466 * We need to balance fs here to prevent from producing dirty node pages
Jaegeuk Kim39936832012-11-22 16:21:29 +0900467 * during the urgent cleaning time when runing out of free sections.
468 */
Jaegeuk Kima7881892017-04-20 13:51:57 -0700469 update_inode_page(inode);
470 if (wbc && wbc->nr_to_write)
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800471 f2fs_balance_fs(sbi, true);
Jaegeuk Kim744602c2014-01-24 09:42:16 +0900472 return 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900473}
474
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900475/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900476 * Called at the last iput() if i_nlink is zero
477 */
478void f2fs_evict_inode(struct inode *inode)
479{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700480 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700481 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu13ec7292015-08-24 17:40:45 +0800482 int err = 0;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900483
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700484 /* some remained atomic pages should discarded */
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800485 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800486 drop_inmem_pages(inode);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700487
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900488 trace_f2fs_evict_inode(inode);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700489 truncate_inode_pages_final(&inode->i_data);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900490
491 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
492 inode->i_ino == F2FS_META_INO(sbi))
Chao Yudbf20cb2014-07-25 12:00:57 +0800493 goto out_clear;
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900494
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700495 f2fs_bug_on(sbi, get_dirty_pages(inode));
Chao Yuc227f912015-12-16 13:09:20 +0800496 remove_dirty_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900497
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700498 f2fs_destroy_extent_tree(inode);
499
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900500 if (inode->i_nlink || is_bad_inode(inode))
501 goto no_delete;
502
Chao Yu0abd6752017-07-09 00:13:07 +0800503 dquot_initialize(inode);
504
Chao Yu60dcedc2016-11-02 20:43:21 +0800505 remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
506 remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
Chao Yu39d787b2017-09-29 13:59:38 +0800507 remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
Chao Yu60dcedc2016-11-02 20:43:21 +0800508
Changman Leed6212a52013-01-29 18:30:07 +0900509 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700510 set_inode_flag(inode, FI_NO_ALLOC);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900511 i_size_write(inode, 0);
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700512retry:
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900513 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700514 err = f2fs_truncate(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900515
Jaegeuk Kim8c1b3c02017-03-07 13:32:20 -0800516#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
Chao Yu13ec7292015-08-24 17:40:45 +0800522 if (!err) {
523 f2fs_lock_op(sbi);
524 err = remove_inode_page(inode);
525 f2fs_unlock_op(sbi);
Chao Yua11b9f62016-10-11 22:56:59 +0800526 if (err == -ENOENT)
527 err = 0;
Chao Yu13ec7292015-08-24 17:40:45 +0800528 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900529
Jaegeuk Kim4c0c2942016-05-03 09:22:18 -0700530 /* give more chances, if ENOMEM case */
531 if (err == -ENOMEM) {
532 err = 0;
533 goto retry;
534 }
535
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700536 if (err)
537 update_inode_page(inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800538 dquot_free_inode(inode);
Changman Leed6212a52013-01-29 18:30:07 +0900539 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900540no_delete:
Chao Yu0abd6752017-07-09 00:13:07 +0800541 dquot_drop(inode);
542
Chao Yud5e8f6c2015-07-15 17:28:53 +0800543 stat_dec_inline_xattr(inode);
Jaegeuk Kim3289c062014-10-13 20:00:16 -0700544 stat_dec_inline_dir(inode);
Jaegeuk Kime7a2bf22014-10-14 10:29:50 -0700545 stat_dec_inline_inode(inode);
Chao Yu0bdee482015-03-19 19:27:51 +0800546
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700547 if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG)))
Chao Yuca7d8022017-09-12 14:04:05 +0800548 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
Jaegeuk Kim943973c2017-10-12 19:12:53 -0700549 else
550 f2fs_inode_synced(inode);
Chao Yuca7d8022017-09-12 14:04:05 +0800551
Jaegeuk Kim4f295442017-03-04 13:56:10 -0800552 /* 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);
Chao Yu002a41c2014-08-04 09:54:58 +0800556 if (xnid)
557 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
Chao Yu60dcedc2016-11-02 20:43:21 +0800558 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 }
Jaegeuk Kim91942322016-05-20 10:13:22 -0700564 if (is_inode_flag_set(inode, FI_FREE_NID)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700565 alloc_nid_failed(sbi, inode->i_ino);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700566 clear_inode_flag(inode, FI_FREE_NID);
Jaegeuk Kimd8c42562017-06-01 15:39:27 -0700567 } else {
568 f2fs_bug_on(sbi, err &&
569 !exist_written_data(sbi, inode->i_ino, ORPHAN_INO));
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -0700570 }
Chao Yudbf20cb2014-07-25 12:00:57 +0800571out_clear:
Eric Biggers3d204e22018-01-11 23:30:13 -0500572 fscrypt_put_encryption_info(inode);
Chao Yudbf20cb2014-07-25 12:00:57 +0800573 clear_inode(inode);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900574}
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700575
576/* caller should call f2fs_lock_op() */
577void handle_failed_inode(struct inode *inode)
578{
579 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700580 struct node_info ni;
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700581
Chao Yua11b9f62016-10-11 22:56:59 +0800582 /*
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);
Jaegeuk Kim9bb02c32017-04-11 19:01:26 -0700593 f2fs_inode_synced(inode);
Chao Yua11b9f62016-10-11 22:56:59 +0800594
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700595 /* don't make bad inode, since it becomes a regular file. */
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700596 unlock_new_inode(inode);
597
Chao Yu13ec7292015-08-24 17:40:45 +0800598 /*
Chao Yu13ec7292015-08-24 17:40:45 +0800599 * 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 */
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700603 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 {
Jaegeuk Kim67c37582016-06-13 18:27:02 -0700612 add_orphan_inode(inode);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700613 }
614 alloc_nid_done(sbi, inode->i_ino);
615 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700616 set_inode_flag(inode, FI_FREE_NID);
Chao Yu13ec7292015-08-24 17:40:45 +0800617 }
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700618
Jaegeuk Kim44c16152014-09-25 11:55:53 -0700619 f2fs_unlock_op(sbi);
620
621 /* iput will drop the inode object */
622 iput(inode);
623}