4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/writeback.h>
19 struct f2fs_iget_args
{
24 void f2fs_set_inode_flags(struct inode
*inode
)
26 unsigned int flags
= F2FS_I(inode
)->i_flags
;
28 inode
->i_flags
&= ~(S_SYNC
| S_APPEND
| S_IMMUTABLE
|
29 S_NOATIME
| S_DIRSYNC
);
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
;
43 static int f2fs_iget_test(struct inode
*inode
, void *data
)
45 struct f2fs_iget_args
*args
= data
;
47 if (inode
->i_ino
!= args
->ino
)
49 if (inode
->i_state
& (I_FREEING
| I_WILL_FREE
)) {
56 struct inode
*f2fs_iget_nowait(struct super_block
*sb
, unsigned long ino
)
58 struct f2fs_iget_args args
= {
62 struct inode
*inode
= ilookup5(sb
, ino
, f2fs_iget_test
, &args
);
67 return f2fs_iget(sb
, ino
);
68 return ERR_PTR(-ENOENT
);
71 static int do_read_inode(struct inode
*inode
)
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
;
77 struct f2fs_inode
*ri
;
79 /* Check if ino is within scope */
80 check_nid_range(sbi
, inode
->i_ino
);
82 node_page
= get_node_page(sbi
, inode
->i_ino
);
83 if (IS_ERR(node_page
))
84 return PTR_ERR(node_page
);
86 rn
= page_address(node_page
);
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
);
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
);
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
);
108 fi
->data_version
= le64_to_cpu(F2FS_CKPT(sbi
)->checkpoint_ver
) - 1;
109 fi
->i_advise
= ri
->i_advise
;
110 fi
->i_pino
= le32_to_cpu(ri
->i_pino
);
111 get_extent_info(&fi
->ext
, ri
->i_ext
);
112 f2fs_put_page(node_page
, 1);
116 struct inode
*f2fs_iget(struct super_block
*sb
, unsigned long ino
)
118 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
122 inode
= iget_locked(sb
, ino
);
124 return ERR_PTR(-ENOMEM
);
125 if (!(inode
->i_state
& I_NEW
))
127 if (ino
== F2FS_NODE_INO(sbi
) || ino
== F2FS_META_INO(sbi
))
130 ret
= do_read_inode(inode
);
134 if (!sbi
->por_doing
&& inode
->i_nlink
== 0) {
140 if (ino
== F2FS_NODE_INO(sbi
)) {
141 inode
->i_mapping
->a_ops
= &f2fs_node_aops
;
142 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_ZERO
);
143 } else if (ino
== F2FS_META_INO(sbi
)) {
144 inode
->i_mapping
->a_ops
= &f2fs_meta_aops
;
145 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_ZERO
);
146 } else if (S_ISREG(inode
->i_mode
)) {
147 inode
->i_op
= &f2fs_file_inode_operations
;
148 inode
->i_fop
= &f2fs_file_operations
;
149 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
150 } else if (S_ISDIR(inode
->i_mode
)) {
151 inode
->i_op
= &f2fs_dir_inode_operations
;
152 inode
->i_fop
= &f2fs_dir_operations
;
153 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
154 mapping_set_gfp_mask(inode
->i_mapping
, GFP_HIGHUSER_MOVABLE
|
156 } else if (S_ISLNK(inode
->i_mode
)) {
157 inode
->i_op
= &f2fs_symlink_inode_operations
;
158 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
159 } else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
160 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
161 inode
->i_op
= &f2fs_special_inode_operations
;
162 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
167 unlock_new_inode(inode
);
176 void update_inode(struct inode
*inode
, struct page
*node_page
)
178 struct f2fs_node
*rn
;
179 struct f2fs_inode
*ri
;
181 wait_on_page_writeback(node_page
);
183 rn
= page_address(node_page
);
186 ri
->i_mode
= cpu_to_le16(inode
->i_mode
);
187 ri
->i_advise
= F2FS_I(inode
)->i_advise
;
188 ri
->i_uid
= cpu_to_le32(i_uid_read(inode
));
189 ri
->i_gid
= cpu_to_le32(i_gid_read(inode
));
190 ri
->i_links
= cpu_to_le32(inode
->i_nlink
);
191 ri
->i_size
= cpu_to_le64(i_size_read(inode
));
192 ri
->i_blocks
= cpu_to_le64(inode
->i_blocks
);
193 set_raw_extent(&F2FS_I(inode
)->ext
, &ri
->i_ext
);
195 ri
->i_atime
= cpu_to_le64(inode
->i_atime
.tv_sec
);
196 ri
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
197 ri
->i_mtime
= cpu_to_le64(inode
->i_mtime
.tv_sec
);
198 ri
->i_atime_nsec
= cpu_to_le32(inode
->i_atime
.tv_nsec
);
199 ri
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
200 ri
->i_mtime_nsec
= cpu_to_le32(inode
->i_mtime
.tv_nsec
);
201 ri
->i_current_depth
= cpu_to_le32(F2FS_I(inode
)->i_current_depth
);
202 ri
->i_xattr_nid
= cpu_to_le32(F2FS_I(inode
)->i_xattr_nid
);
203 ri
->i_flags
= cpu_to_le32(F2FS_I(inode
)->i_flags
);
204 ri
->i_pino
= cpu_to_le32(F2FS_I(inode
)->i_pino
);
205 ri
->i_generation
= cpu_to_le32(inode
->i_generation
);
206 set_cold_node(inode
, node_page
);
207 set_page_dirty(node_page
);
210 int f2fs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
212 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
213 struct page
*node_page
;
214 bool need_lock
= false;
216 if (inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
217 inode
->i_ino
== F2FS_META_INO(sbi
))
221 f2fs_balance_fs(sbi
);
223 node_page
= get_node_page(sbi
, inode
->i_ino
);
224 if (IS_ERR(node_page
))
225 return PTR_ERR(node_page
);
227 if (!PageDirty(node_page
)) {
229 f2fs_put_page(node_page
, 1);
230 mutex_lock(&sbi
->write_inode
);
231 node_page
= get_node_page(sbi
, inode
->i_ino
);
232 if (IS_ERR(node_page
)) {
233 mutex_unlock(&sbi
->write_inode
);
234 return PTR_ERR(node_page
);
237 update_inode(inode
, node_page
);
238 f2fs_put_page(node_page
, 1);
240 mutex_unlock(&sbi
->write_inode
);
245 * Called at the last iput() if i_nlink is zero
247 void f2fs_evict_inode(struct inode
*inode
)
249 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
251 truncate_inode_pages(&inode
->i_data
, 0);
253 if (inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
254 inode
->i_ino
== F2FS_META_INO(sbi
))
257 BUG_ON(atomic_read(&F2FS_I(inode
)->dirty_dents
));
258 remove_dirty_dir_inode(inode
);
260 if (inode
->i_nlink
|| is_bad_inode(inode
))
263 set_inode_flag(F2FS_I(inode
), FI_NO_ALLOC
);
264 i_size_write(inode
, 0);
266 if (F2FS_HAS_BLOCKS(inode
))
267 f2fs_truncate(inode
);
269 remove_inode_page(inode
);