blob: ddae412d30c8519d7b11c4fe396f548b3a7b713a [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 */
47 check_nid_range(sbi, inode->i_ino);
48
49 node_page = get_node_page(sbi, inode->i_ino);
50 if (IS_ERR(node_page))
51 return PTR_ERR(node_page);
52
53 rn = page_address(node_page);
54 ri = &(rn->i);
55
56 inode->i_mode = le16_to_cpu(ri->i_mode);
57 i_uid_write(inode, le32_to_cpu(ri->i_uid));
58 i_gid_write(inode, le32_to_cpu(ri->i_gid));
59 set_nlink(inode, le32_to_cpu(ri->i_links));
60 inode->i_size = le64_to_cpu(ri->i_size);
61 inode->i_blocks = le64_to_cpu(ri->i_blocks);
62
63 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
64 inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
65 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
66 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
67 inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
68 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
69 inode->i_generation = le32_to_cpu(ri->i_generation);
Changman Lee7d79e752013-01-23 09:40:23 +090070 if (ri->i_addr[0])
71 inode->i_rdev = old_decode_dev(le32_to_cpu(ri->i_addr[0]));
72 else
73 inode->i_rdev = new_decode_dev(le32_to_cpu(ri->i_addr[1]));
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090074
75 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
76 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
77 fi->i_flags = le32_to_cpu(ri->i_flags);
78 fi->flags = 0;
79 fi->data_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver) - 1;
80 fi->i_advise = ri->i_advise;
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +090081 fi->i_pino = le32_to_cpu(ri->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +090082 get_extent_info(&fi->ext, ri->i_ext);
83 f2fs_put_page(node_page, 1);
84 return 0;
85}
86
87struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
88{
89 struct f2fs_sb_info *sbi = F2FS_SB(sb);
90 struct inode *inode;
91 int ret;
92
93 inode = iget_locked(sb, ino);
94 if (!inode)
95 return ERR_PTR(-ENOMEM);
96 if (!(inode->i_state & I_NEW))
97 return inode;
98 if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
99 goto make_now;
100
101 ret = do_read_inode(inode);
102 if (ret)
103 goto bad_inode;
104
105 if (!sbi->por_doing && inode->i_nlink == 0) {
106 ret = -ENOENT;
107 goto bad_inode;
108 }
109
110make_now:
111 if (ino == F2FS_NODE_INO(sbi)) {
112 inode->i_mapping->a_ops = &f2fs_node_aops;
113 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
114 } else if (ino == F2FS_META_INO(sbi)) {
115 inode->i_mapping->a_ops = &f2fs_meta_aops;
116 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
117 } else if (S_ISREG(inode->i_mode)) {
118 inode->i_op = &f2fs_file_inode_operations;
119 inode->i_fop = &f2fs_file_operations;
120 inode->i_mapping->a_ops = &f2fs_dblock_aops;
121 } else if (S_ISDIR(inode->i_mode)) {
122 inode->i_op = &f2fs_dir_inode_operations;
123 inode->i_fop = &f2fs_dir_operations;
124 inode->i_mapping->a_ops = &f2fs_dblock_aops;
125 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER_MOVABLE |
126 __GFP_ZERO);
127 } else if (S_ISLNK(inode->i_mode)) {
128 inode->i_op = &f2fs_symlink_inode_operations;
129 inode->i_mapping->a_ops = &f2fs_dblock_aops;
130 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
131 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
132 inode->i_op = &f2fs_special_inode_operations;
133 init_special_inode(inode, inode->i_mode, inode->i_rdev);
134 } else {
135 ret = -EIO;
136 goto bad_inode;
137 }
138 unlock_new_inode(inode);
139
140 return inode;
141
142bad_inode:
143 iget_failed(inode);
144 return ERR_PTR(ret);
145}
146
147void update_inode(struct inode *inode, struct page *node_page)
148{
149 struct f2fs_node *rn;
150 struct f2fs_inode *ri;
151
152 wait_on_page_writeback(node_page);
153
154 rn = page_address(node_page);
155 ri = &(rn->i);
156
157 ri->i_mode = cpu_to_le16(inode->i_mode);
158 ri->i_advise = F2FS_I(inode)->i_advise;
159 ri->i_uid = cpu_to_le32(i_uid_read(inode));
160 ri->i_gid = cpu_to_le32(i_gid_read(inode));
161 ri->i_links = cpu_to_le32(inode->i_nlink);
162 ri->i_size = cpu_to_le64(i_size_read(inode));
163 ri->i_blocks = cpu_to_le64(inode->i_blocks);
164 set_raw_extent(&F2FS_I(inode)->ext, &ri->i_ext);
165
166 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
167 ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
168 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
169 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
170 ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
171 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
172 ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
173 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
174 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900175 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900176 ri->i_generation = cpu_to_le32(inode->i_generation);
Changman Lee7d79e752013-01-23 09:40:23 +0900177
178 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
179 if (old_valid_dev(inode->i_rdev)) {
180 ri->i_addr[0] =
181 cpu_to_le32(old_encode_dev(inode->i_rdev));
182 ri->i_addr[1] = 0;
183 } else {
184 ri->i_addr[0] = 0;
185 ri->i_addr[1] =
186 cpu_to_le32(new_encode_dev(inode->i_rdev));
187 ri->i_addr[2] = 0;
188 }
189 }
190
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900191 set_cold_node(inode, node_page);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900192 set_page_dirty(node_page);
193}
194
195int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
196{
197 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
198 struct page *node_page;
199 bool need_lock = false;
200
201 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
202 inode->i_ino == F2FS_META_INO(sbi))
203 return 0;
204
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900205 if (wbc)
206 f2fs_balance_fs(sbi);
207
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900208 node_page = get_node_page(sbi, inode->i_ino);
209 if (IS_ERR(node_page))
210 return PTR_ERR(node_page);
211
212 if (!PageDirty(node_page)) {
213 need_lock = true;
214 f2fs_put_page(node_page, 1);
215 mutex_lock(&sbi->write_inode);
216 node_page = get_node_page(sbi, inode->i_ino);
217 if (IS_ERR(node_page)) {
218 mutex_unlock(&sbi->write_inode);
219 return PTR_ERR(node_page);
220 }
221 }
222 update_inode(inode, node_page);
223 f2fs_put_page(node_page, 1);
224 if (need_lock)
225 mutex_unlock(&sbi->write_inode);
226 return 0;
227}
228
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900229/*
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900230 * Called at the last iput() if i_nlink is zero
231 */
232void f2fs_evict_inode(struct inode *inode)
233{
234 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
235
236 truncate_inode_pages(&inode->i_data, 0);
237
238 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
239 inode->i_ino == F2FS_META_INO(sbi))
240 goto no_delete;
241
242 BUG_ON(atomic_read(&F2FS_I(inode)->dirty_dents));
243 remove_dirty_dir_inode(inode);
244
245 if (inode->i_nlink || is_bad_inode(inode))
246 goto no_delete;
247
Changman Leed6212a52013-01-29 18:30:07 +0900248 sb_start_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900249 set_inode_flag(F2FS_I(inode), FI_NO_ALLOC);
250 i_size_write(inode, 0);
251
252 if (F2FS_HAS_BLOCKS(inode))
253 f2fs_truncate(inode);
254
255 remove_inode_page(inode);
Changman Leed6212a52013-01-29 18:30:07 +0900256 sb_end_intwrite(inode->i_sb);
Jaegeuk Kim19f99ce2012-11-02 17:10:40 +0900257no_delete:
258 clear_inode(inode);
259}