Commit | Line | Data |
---|---|---|
19f99cee JK |
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 | struct f2fs_iget_args { | |
20 | u64 ino; | |
21 | int on_free; | |
22 | }; | |
23 | ||
24 | void f2fs_set_inode_flags(struct inode *inode) | |
25 | { | |
26 | unsigned int flags = F2FS_I(inode)->i_flags; | |
27 | ||
28 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | | |
29 | S_NOATIME | S_DIRSYNC); | |
30 | ||
31 | if (flags & FS_SYNC_FL) | |
32 | inode->i_flags |= S_SYNC; | |
33 | if (flags & FS_APPEND_FL) | |
34 | inode->i_flags |= S_APPEND; | |
35 | if (flags & FS_IMMUTABLE_FL) | |
36 | inode->i_flags |= S_IMMUTABLE; | |
37 | if (flags & FS_NOATIME_FL) | |
38 | inode->i_flags |= S_NOATIME; | |
39 | if (flags & FS_DIRSYNC_FL) | |
40 | inode->i_flags |= S_DIRSYNC; | |
41 | } | |
42 | ||
43 | static int f2fs_iget_test(struct inode *inode, void *data) | |
44 | { | |
45 | struct f2fs_iget_args *args = data; | |
46 | ||
47 | if (inode->i_ino != args->ino) | |
48 | return 0; | |
49 | if (inode->i_state & (I_FREEING | I_WILL_FREE)) { | |
50 | args->on_free = 1; | |
51 | return 0; | |
52 | } | |
53 | return 1; | |
54 | } | |
55 | ||
56 | struct inode *f2fs_iget_nowait(struct super_block *sb, unsigned long ino) | |
57 | { | |
58 | struct f2fs_iget_args args = { | |
59 | .ino = ino, | |
60 | .on_free = 0 | |
61 | }; | |
62 | struct inode *inode = ilookup5(sb, ino, f2fs_iget_test, &args); | |
63 | ||
64 | if (inode) | |
65 | return inode; | |
66 | if (!args.on_free) | |
67 | return f2fs_iget(sb, ino); | |
68 | return ERR_PTR(-ENOENT); | |
69 | } | |
70 | ||
71 | static int do_read_inode(struct inode *inode) | |
72 | { | |
73 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
74 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
75 | struct page *node_page; | |
76 | struct f2fs_node *rn; | |
77 | struct f2fs_inode *ri; | |
78 | ||
79 | /* Check if ino is within scope */ | |
80 | check_nid_range(sbi, inode->i_ino); | |
81 | ||
82 | node_page = get_node_page(sbi, inode->i_ino); | |
83 | if (IS_ERR(node_page)) | |
84 | return PTR_ERR(node_page); | |
85 | ||
86 | rn = page_address(node_page); | |
87 | ri = &(rn->i); | |
88 | ||
89 | inode->i_mode = le16_to_cpu(ri->i_mode); | |
90 | i_uid_write(inode, le32_to_cpu(ri->i_uid)); | |
91 | i_gid_write(inode, le32_to_cpu(ri->i_gid)); | |
92 | set_nlink(inode, le32_to_cpu(ri->i_links)); | |
93 | inode->i_size = le64_to_cpu(ri->i_size); | |
94 | inode->i_blocks = le64_to_cpu(ri->i_blocks); | |
95 | ||
96 | inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime); | |
97 | inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime); | |
98 | inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime); | |
99 | inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec); | |
100 | inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec); | |
101 | inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec); | |
102 | inode->i_generation = le32_to_cpu(ri->i_generation); | |
103 | ||
104 | fi->i_current_depth = le32_to_cpu(ri->i_current_depth); | |
105 | fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid); | |
106 | fi->i_flags = le32_to_cpu(ri->i_flags); | |
107 | fi->flags = 0; | |
108 | fi->data_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver) - 1; | |
109 | fi->i_advise = ri->i_advise; | |
110 | get_extent_info(&fi->ext, ri->i_ext); | |
111 | f2fs_put_page(node_page, 1); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) | |
116 | { | |
117 | struct f2fs_sb_info *sbi = F2FS_SB(sb); | |
118 | struct inode *inode; | |
119 | int ret; | |
120 | ||
121 | inode = iget_locked(sb, ino); | |
122 | if (!inode) | |
123 | return ERR_PTR(-ENOMEM); | |
124 | if (!(inode->i_state & I_NEW)) | |
125 | return inode; | |
126 | if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi)) | |
127 | goto make_now; | |
128 | ||
129 | ret = do_read_inode(inode); | |
130 | if (ret) | |
131 | goto bad_inode; | |
132 | ||
133 | if (!sbi->por_doing && inode->i_nlink == 0) { | |
134 | ret = -ENOENT; | |
135 | goto bad_inode; | |
136 | } | |
137 | ||
138 | make_now: | |
139 | if (ino == F2FS_NODE_INO(sbi)) { | |
140 | inode->i_mapping->a_ops = &f2fs_node_aops; | |
141 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); | |
142 | } else if (ino == F2FS_META_INO(sbi)) { | |
143 | inode->i_mapping->a_ops = &f2fs_meta_aops; | |
144 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); | |
145 | } else if (S_ISREG(inode->i_mode)) { | |
146 | inode->i_op = &f2fs_file_inode_operations; | |
147 | inode->i_fop = &f2fs_file_operations; | |
148 | inode->i_mapping->a_ops = &f2fs_dblock_aops; | |
149 | } else if (S_ISDIR(inode->i_mode)) { | |
150 | inode->i_op = &f2fs_dir_inode_operations; | |
151 | inode->i_fop = &f2fs_dir_operations; | |
152 | inode->i_mapping->a_ops = &f2fs_dblock_aops; | |
153 | mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER_MOVABLE | | |
154 | __GFP_ZERO); | |
155 | } else if (S_ISLNK(inode->i_mode)) { | |
156 | inode->i_op = &f2fs_symlink_inode_operations; | |
157 | inode->i_mapping->a_ops = &f2fs_dblock_aops; | |
158 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || | |
159 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | |
160 | inode->i_op = &f2fs_special_inode_operations; | |
161 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | |
162 | } else { | |
163 | ret = -EIO; | |
164 | goto bad_inode; | |
165 | } | |
166 | unlock_new_inode(inode); | |
167 | ||
168 | return inode; | |
169 | ||
170 | bad_inode: | |
171 | iget_failed(inode); | |
172 | return ERR_PTR(ret); | |
173 | } | |
174 | ||
175 | void update_inode(struct inode *inode, struct page *node_page) | |
176 | { | |
177 | struct f2fs_node *rn; | |
178 | struct f2fs_inode *ri; | |
179 | ||
180 | wait_on_page_writeback(node_page); | |
181 | ||
182 | rn = page_address(node_page); | |
183 | ri = &(rn->i); | |
184 | ||
185 | ri->i_mode = cpu_to_le16(inode->i_mode); | |
186 | ri->i_advise = F2FS_I(inode)->i_advise; | |
187 | ri->i_uid = cpu_to_le32(i_uid_read(inode)); | |
188 | ri->i_gid = cpu_to_le32(i_gid_read(inode)); | |
189 | ri->i_links = cpu_to_le32(inode->i_nlink); | |
190 | ri->i_size = cpu_to_le64(i_size_read(inode)); | |
191 | ri->i_blocks = cpu_to_le64(inode->i_blocks); | |
192 | set_raw_extent(&F2FS_I(inode)->ext, &ri->i_ext); | |
193 | ||
194 | ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec); | |
195 | ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); | |
196 | ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); | |
197 | ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); | |
198 | ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); | |
199 | ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
200 | ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth); | |
201 | ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid); | |
202 | ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags); | |
203 | ri->i_generation = cpu_to_le32(inode->i_generation); | |
204 | set_page_dirty(node_page); | |
205 | } | |
206 | ||
207 | int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) | |
208 | { | |
209 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
210 | struct page *node_page; | |
211 | bool need_lock = false; | |
212 | ||
213 | if (inode->i_ino == F2FS_NODE_INO(sbi) || | |
214 | inode->i_ino == F2FS_META_INO(sbi)) | |
215 | return 0; | |
216 | ||
217 | node_page = get_node_page(sbi, inode->i_ino); | |
218 | if (IS_ERR(node_page)) | |
219 | return PTR_ERR(node_page); | |
220 | ||
221 | if (!PageDirty(node_page)) { | |
222 | need_lock = true; | |
223 | f2fs_put_page(node_page, 1); | |
224 | mutex_lock(&sbi->write_inode); | |
225 | node_page = get_node_page(sbi, inode->i_ino); | |
226 | if (IS_ERR(node_page)) { | |
227 | mutex_unlock(&sbi->write_inode); | |
228 | return PTR_ERR(node_page); | |
229 | } | |
230 | } | |
231 | update_inode(inode, node_page); | |
232 | f2fs_put_page(node_page, 1); | |
233 | if (need_lock) | |
234 | mutex_unlock(&sbi->write_inode); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Called at the last iput() if i_nlink is zero | |
240 | */ | |
241 | void f2fs_evict_inode(struct inode *inode) | |
242 | { | |
243 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
244 | ||
245 | truncate_inode_pages(&inode->i_data, 0); | |
246 | ||
247 | if (inode->i_ino == F2FS_NODE_INO(sbi) || | |
248 | inode->i_ino == F2FS_META_INO(sbi)) | |
249 | goto no_delete; | |
250 | ||
251 | BUG_ON(atomic_read(&F2FS_I(inode)->dirty_dents)); | |
252 | remove_dirty_dir_inode(inode); | |
253 | ||
254 | if (inode->i_nlink || is_bad_inode(inode)) | |
255 | goto no_delete; | |
256 | ||
257 | set_inode_flag(F2FS_I(inode), FI_NO_ALLOC); | |
258 | i_size_write(inode, 0); | |
259 | ||
260 | if (F2FS_HAS_BLOCKS(inode)) | |
261 | f2fs_truncate(inode); | |
262 | ||
263 | remove_inode_page(inode); | |
264 | no_delete: | |
265 | clear_inode(inode); | |
266 | } |