blob: e0e8308594a5554dc161fd08cc93f6176484b171 [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>
14#include <linux/writeback.h>
15
16#include "f2fs.h"
17#include "node.h"
18
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090019void f2fs_set_inode_flags(struct inode *inode)
20{
21 unsigned int flags = F2FS_I(inode)->i_flags;
22
23 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE |
24 S_NOATIME | S_DIRSYNC);
25
26 if (flags & FS_SYNC_FL)
27 inode->i_flags |= S_SYNC;
28 if (flags & FS_APPEND_FL)
29 inode->i_flags |= S_APPEND;
30 if (flags & FS_IMMUTABLE_FL)
31 inode->i_flags |= S_IMMUTABLE;
32 if (flags & FS_NOATIME_FL)
33 inode->i_flags |= S_NOATIME;
34 if (flags & FS_DIRSYNC_FL)
35 inode->i_flags |= S_DIRSYNC;
36}
37
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090038static int do_read_inode(struct inode *inode)
39{
40 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
41 struct f2fs_inode_info *fi = F2FS_I(inode);
42 struct page *node_page;
43 struct f2fs_node *rn;
44 struct f2fs_inode *ri;
45
46 /* Check if ino is within scope */
Namjae Jeon064e0822013-03-17 17:27:20 +090047 if (check_nid_range(sbi, inode->i_ino)) {
48 f2fs_msg(inode->i_sb, KERN_ERR, "bad inode number: %lu",
49 (unsigned long) inode->i_ino);
50 return -EINVAL;
51 }
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090052
53 node_page = get_node_page(sbi, inode->i_ino);
54 if (IS_ERR(node_page))
55 return PTR_ERR(node_page);
56
57 rn = page_address(node_page);
58 ri = &(rn->i);
59
60 inode->i_mode = le16_to_cpu(ri->i_mode);
61 i_uid_write(inode, le32_to_cpu(ri->i_uid));
62 i_gid_write(inode, le32_to_cpu(ri->i_gid));
63 set_nlink(inode, le32_to_cpu(ri->i_links));
64 inode->i_size = le64_to_cpu(ri->i_size);
65 inode->i_blocks = le64_to_cpu(ri->i_blocks);
66
67 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
68 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
69 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
70 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
71 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
72 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
73 inode->i_generation = le32_to_cpu(ri->i_generation);
Changman Lee7d79e752013-01-23 09:40:23 +090074 if (ri->i_addr[0])
75 inode->i_rdev = old_decode_dev(le32_to_cpu(ri->i_addr[0]));
76 else
77 inode->i_rdev = new_decode_dev(le32_to_cpu(ri->i_addr[1]));
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090078
79 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
80 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
81 fi->i_flags = le32_to_cpu(ri->i_flags);
82 fi->flags = 0;
83 fi->data_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver) - 1;
84 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +090085 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090086 get_extent_info(&fi->ext, ri->i_ext);
87 f2fs_put_page(node_page, 1);
88 return 0;
89}
90
91struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
92{
93 struct f2fs_sb_info *sbi = F2FS_SB(sb);
94 struct inode *inode;
95 int ret;
96
97 inode = iget_locked(sb, ino);
98 if (!inode)
99 return ERR_PTR(-ENOMEM);
100 if (!(inode->i_state & I_NEW))
101 return inode;
102 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
103 goto make_now;
104
105 ret = do_read_inode(inode);
106 if (ret)
107 goto bad_inode;
108
109 if (!sbi->por_doing && inode->i_nlink == 0) {
110 ret = -ENOENT;
111 goto bad_inode;
112 }
113
114make_now:
115 if (ino == F2FS_NODE_INO(sbi)) {
116 inode->i_mapping->a_ops = &f2fs_node_aops;
117 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
118 } else if (ino == F2FS_META_INO(sbi)) {
119 inode->i_mapping->a_ops = &f2fs_meta_aops;
120 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
121 } else if (S_ISREG(inode->i_mode)) {
122 inode->i_op = &f2fs_file_inode_operations;
123 inode->i_fop = &f2fs_file_operations;
124 inode->i_mapping->a_ops = &f2fs_dblock_aops;
125 } else if (S_ISDIR(inode->i_mode)) {
126 inode->i_op = &f2fs_dir_inode_operations;
127 inode->i_fop = &f2fs_dir_operations;
128 inode->i_mapping->a_ops = &f2fs_dblock_aops;
129 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER_MOVABLE |
130 __GFP_ZERO);
131 } else if (S_ISLNK(inode->i_mode)) {
132 inode->i_op = &f2fs_symlink_inode_operations;
133 inode->i_mapping->a_ops = &f2fs_dblock_aops;
134 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
135 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
136 inode->i_op = &f2fs_special_inode_operations;
137 init_special_inode(inode, inode->i_mode, inode->i_rdev);
138 } else {
139 ret = -EIO;
140 goto bad_inode;
141 }
142 unlock_new_inode(inode);
143
144 return inode;
145
146bad_inode:
147 iget_failed(inode);
148 return ERR_PTR(ret);
149}
150
151void update_inode(struct inode *inode, struct page *node_page)
152{
153 struct f2fs_node *rn;
154 struct f2fs_inode *ri;
155
156 wait_on_page_writeback(node_page);
157
158 rn = page_address(node_page);
159 ri = &(rn->i);
160
161 ri->i_mode = cpu_to_le16(inode->i_mode);
162 ri->i_advise = F2FS_I(inode)->i_advise;
163 ri->i_uid = cpu_to_le32(i_uid_read(inode));
164 ri->i_gid = cpu_to_le32(i_gid_read(inode));
165 ri->i_links = cpu_to_le32(inode->i_nlink);
166 ri->i_size = cpu_to_le64(i_size_read(inode));
167 ri->i_blocks = cpu_to_le64(inode->i_blocks);
168 set_raw_extent(&F2FS_I(inode)->ext, &ri->i_ext);
169
170 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
171 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
172 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
173 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
174 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
175 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
176 ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
177 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
178 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900179 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900180 ri->i_generation = cpu_to_le32(inode->i_generation);
Changman Lee7d79e752013-01-23 09:40:23 +0900181
182 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
183 if (old_valid_dev(inode->i_rdev)) {
184 ri->i_addr[0] =
185 cpu_to_le32(old_encode_dev(inode->i_rdev));
186 ri->i_addr[1] = 0;
187 } else {
188 ri->i_addr[0] = 0;
189 ri->i_addr[1] =
190 cpu_to_le32(new_encode_dev(inode->i_rdev));
191 ri->i_addr[2] = 0;
192 }
193 }
194
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900195 set_cold_node(inode, node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900196 set_page_dirty(node_page);
197}
198
199int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
200{
201 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
202 struct page *node_page;
203 bool need_lock = false;
204
205 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
206 inode->i_ino == F2FS_META_INO(sbi))
207 return 0;
208
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900209 if (wbc)
210 f2fs_balance_fs(sbi);
211
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900212 node_page = get_node_page(sbi, inode->i_ino);
213 if (IS_ERR(node_page))
214 return PTR_ERR(node_page);
215
216 if (!PageDirty(node_page)) {
217 need_lock = true;
218 f2fs_put_page(node_page, 1);
219 mutex_lock(&sbi->write_inode);
220 node_page = get_node_page(sbi, inode->i_ino);
221 if (IS_ERR(node_page)) {
222 mutex_unlock(&sbi->write_inode);
223 return PTR_ERR(node_page);
224 }
225 }
226 update_inode(inode, node_page);
227 f2fs_put_page(node_page, 1);
228 if (need_lock)
229 mutex_unlock(&sbi->write_inode);
230 return 0;
231}
232
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900233/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900234 * Called at the last iput() if i_nlink is zero
235 */
236void f2fs_evict_inode(struct inode *inode)
237{
238 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
239
240 truncate_inode_pages(&inode->i_data, 0);
241
242 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
243 inode->i_ino == F2FS_META_INO(sbi))
244 goto no_delete;
245
246 BUG_ON(atomic_read(&F2FS_I(inode)->dirty_dents));
247 remove_dirty_dir_inode(inode);
248
249 if (inode->i_nlink || is_bad_inode(inode))
250 goto no_delete;
251
Changman Leed6212a52013-01-29 18:30:07 +0900252 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900253 set_inode_flag(F2FS_I(inode), FI_NO_ALLOC);
254 i_size_write(inode, 0);
255
256 if (F2FS_HAS_BLOCKS(inode))
257 f2fs_truncate(inode);
258
259 remove_inode_page(inode);
Changman Leed6212a52013-01-29 18:30:07 +0900260 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900261no_delete:
262 clear_inode(inode);
263}