Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 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); |
Changman Lee | 7d79e75 | 2013-01-23 09:40:23 +0900 | [diff] [blame^] | 103 | if (ri->i_addr[0]) |
| 104 | inode->i_rdev = old_decode_dev(le32_to_cpu(ri->i_addr[0])); |
| 105 | else |
| 106 | inode->i_rdev = new_decode_dev(le32_to_cpu(ri->i_addr[1])); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 107 | |
| 108 | fi->i_current_depth = le32_to_cpu(ri->i_current_depth); |
| 109 | fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid); |
| 110 | fi->i_flags = le32_to_cpu(ri->i_flags); |
| 111 | fi->flags = 0; |
| 112 | fi->data_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver) - 1; |
| 113 | fi->i_advise = ri->i_advise; |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 114 | fi->i_pino = le32_to_cpu(ri->i_pino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 115 | get_extent_info(&fi->ext, ri->i_ext); |
| 116 | f2fs_put_page(node_page, 1); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) |
| 121 | { |
| 122 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 123 | struct inode *inode; |
| 124 | int ret; |
| 125 | |
| 126 | inode = iget_locked(sb, ino); |
| 127 | if (!inode) |
| 128 | return ERR_PTR(-ENOMEM); |
| 129 | if (!(inode->i_state & I_NEW)) |
| 130 | return inode; |
| 131 | if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi)) |
| 132 | goto make_now; |
| 133 | |
| 134 | ret = do_read_inode(inode); |
| 135 | if (ret) |
| 136 | goto bad_inode; |
| 137 | |
| 138 | if (!sbi->por_doing && inode->i_nlink == 0) { |
| 139 | ret = -ENOENT; |
| 140 | goto bad_inode; |
| 141 | } |
| 142 | |
| 143 | make_now: |
| 144 | if (ino == F2FS_NODE_INO(sbi)) { |
| 145 | inode->i_mapping->a_ops = &f2fs_node_aops; |
| 146 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); |
| 147 | } else if (ino == F2FS_META_INO(sbi)) { |
| 148 | inode->i_mapping->a_ops = &f2fs_meta_aops; |
| 149 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); |
| 150 | } else if (S_ISREG(inode->i_mode)) { |
| 151 | inode->i_op = &f2fs_file_inode_operations; |
| 152 | inode->i_fop = &f2fs_file_operations; |
| 153 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 154 | } else if (S_ISDIR(inode->i_mode)) { |
| 155 | inode->i_op = &f2fs_dir_inode_operations; |
| 156 | inode->i_fop = &f2fs_dir_operations; |
| 157 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 158 | mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER_MOVABLE | |
| 159 | __GFP_ZERO); |
| 160 | } else if (S_ISLNK(inode->i_mode)) { |
| 161 | inode->i_op = &f2fs_symlink_inode_operations; |
| 162 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 163 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 164 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 165 | inode->i_op = &f2fs_special_inode_operations; |
| 166 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 167 | } else { |
| 168 | ret = -EIO; |
| 169 | goto bad_inode; |
| 170 | } |
| 171 | unlock_new_inode(inode); |
| 172 | |
| 173 | return inode; |
| 174 | |
| 175 | bad_inode: |
| 176 | iget_failed(inode); |
| 177 | return ERR_PTR(ret); |
| 178 | } |
| 179 | |
| 180 | void update_inode(struct inode *inode, struct page *node_page) |
| 181 | { |
| 182 | struct f2fs_node *rn; |
| 183 | struct f2fs_inode *ri; |
| 184 | |
| 185 | wait_on_page_writeback(node_page); |
| 186 | |
| 187 | rn = page_address(node_page); |
| 188 | ri = &(rn->i); |
| 189 | |
| 190 | ri->i_mode = cpu_to_le16(inode->i_mode); |
| 191 | ri->i_advise = F2FS_I(inode)->i_advise; |
| 192 | ri->i_uid = cpu_to_le32(i_uid_read(inode)); |
| 193 | ri->i_gid = cpu_to_le32(i_gid_read(inode)); |
| 194 | ri->i_links = cpu_to_le32(inode->i_nlink); |
| 195 | ri->i_size = cpu_to_le64(i_size_read(inode)); |
| 196 | ri->i_blocks = cpu_to_le64(inode->i_blocks); |
| 197 | set_raw_extent(&F2FS_I(inode)->ext, &ri->i_ext); |
| 198 | |
| 199 | ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
| 200 | ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 201 | ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); |
| 202 | ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); |
| 203 | ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 204 | ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); |
| 205 | ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth); |
| 206 | ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid); |
| 207 | ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 208 | ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 209 | ri->i_generation = cpu_to_le32(inode->i_generation); |
Changman Lee | 7d79e75 | 2013-01-23 09:40:23 +0900 | [diff] [blame^] | 210 | |
| 211 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 212 | if (old_valid_dev(inode->i_rdev)) { |
| 213 | ri->i_addr[0] = |
| 214 | cpu_to_le32(old_encode_dev(inode->i_rdev)); |
| 215 | ri->i_addr[1] = 0; |
| 216 | } else { |
| 217 | ri->i_addr[0] = 0; |
| 218 | ri->i_addr[1] = |
| 219 | cpu_to_le32(new_encode_dev(inode->i_rdev)); |
| 220 | ri->i_addr[2] = 0; |
| 221 | } |
| 222 | } |
| 223 | |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 224 | set_cold_node(inode, node_page); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 225 | set_page_dirty(node_page); |
| 226 | } |
| 227 | |
| 228 | int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 229 | { |
| 230 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 231 | struct page *node_page; |
| 232 | bool need_lock = false; |
| 233 | |
| 234 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 235 | inode->i_ino == F2FS_META_INO(sbi)) |
| 236 | return 0; |
| 237 | |
Jaegeuk Kim | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 238 | if (wbc) |
| 239 | f2fs_balance_fs(sbi); |
| 240 | |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 241 | node_page = get_node_page(sbi, inode->i_ino); |
| 242 | if (IS_ERR(node_page)) |
| 243 | return PTR_ERR(node_page); |
| 244 | |
| 245 | if (!PageDirty(node_page)) { |
| 246 | need_lock = true; |
| 247 | f2fs_put_page(node_page, 1); |
| 248 | mutex_lock(&sbi->write_inode); |
| 249 | node_page = get_node_page(sbi, inode->i_ino); |
| 250 | if (IS_ERR(node_page)) { |
| 251 | mutex_unlock(&sbi->write_inode); |
| 252 | return PTR_ERR(node_page); |
| 253 | } |
| 254 | } |
| 255 | update_inode(inode, node_page); |
| 256 | f2fs_put_page(node_page, 1); |
| 257 | if (need_lock) |
| 258 | mutex_unlock(&sbi->write_inode); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 262 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 263 | * Called at the last iput() if i_nlink is zero |
| 264 | */ |
| 265 | void f2fs_evict_inode(struct inode *inode) |
| 266 | { |
| 267 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 268 | |
| 269 | truncate_inode_pages(&inode->i_data, 0); |
| 270 | |
| 271 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 272 | inode->i_ino == F2FS_META_INO(sbi)) |
| 273 | goto no_delete; |
| 274 | |
| 275 | BUG_ON(atomic_read(&F2FS_I(inode)->dirty_dents)); |
| 276 | remove_dirty_dir_inode(inode); |
| 277 | |
| 278 | if (inode->i_nlink || is_bad_inode(inode)) |
| 279 | goto no_delete; |
| 280 | |
| 281 | set_inode_flag(F2FS_I(inode), FI_NO_ALLOC); |
| 282 | i_size_write(inode, 0); |
| 283 | |
| 284 | if (F2FS_HAS_BLOCKS(inode)) |
| 285 | f2fs_truncate(inode); |
| 286 | |
| 287 | remove_inode_page(inode); |
| 288 | no_delete: |
| 289 | clear_inode(inode); |
| 290 | } |