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
))
220 node_page
= get_node_page(sbi
, inode
->i_ino
);
221 if (IS_ERR(node_page
))
222 return PTR_ERR(node_page
);
224 if (!PageDirty(node_page
)) {
226 f2fs_put_page(node_page
, 1);
227 mutex_lock(&sbi
->write_inode
);
228 node_page
= get_node_page(sbi
, inode
->i_ino
);
229 if (IS_ERR(node_page
)) {
230 mutex_unlock(&sbi
->write_inode
);
231 return PTR_ERR(node_page
);
234 update_inode(inode
, node_page
);
235 f2fs_put_page(node_page
, 1);
237 mutex_unlock(&sbi
->write_inode
);
242 * Called at the last iput() if i_nlink is zero
244 void f2fs_evict_inode(struct inode
*inode
)
246 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
248 truncate_inode_pages(&inode
->i_data
, 0);
250 if (inode
->i_ino
== F2FS_NODE_INO(sbi
) ||
251 inode
->i_ino
== F2FS_META_INO(sbi
))
254 BUG_ON(atomic_read(&F2FS_I(inode
)->dirty_dents
));
255 remove_dirty_dir_inode(inode
);
257 if (inode
->i_nlink
|| is_bad_inode(inode
))
260 set_inode_flag(F2FS_I(inode
), FI_NO_ALLOC
);
261 i_size_write(inode
, 0);
263 if (F2FS_HAS_BLOCKS(inode
))
264 f2fs_truncate(inode
);
266 remove_inode_page(inode
);