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); |
| 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; |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 110 | fi->i_pino = le32_to_cpu(ri->i_pino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 111 | get_extent_info(&fi->ext, ri->i_ext); |
| 112 | f2fs_put_page(node_page, 1); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) |
| 117 | { |
| 118 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 119 | struct inode *inode; |
| 120 | int ret; |
| 121 | |
| 122 | inode = iget_locked(sb, ino); |
| 123 | if (!inode) |
| 124 | return ERR_PTR(-ENOMEM); |
| 125 | if (!(inode->i_state & I_NEW)) |
| 126 | return inode; |
| 127 | if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi)) |
| 128 | goto make_now; |
| 129 | |
| 130 | ret = do_read_inode(inode); |
| 131 | if (ret) |
| 132 | goto bad_inode; |
| 133 | |
| 134 | if (!sbi->por_doing && inode->i_nlink == 0) { |
| 135 | ret = -ENOENT; |
| 136 | goto bad_inode; |
| 137 | } |
| 138 | |
| 139 | make_now: |
| 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 | |
| 155 | __GFP_ZERO); |
| 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); |
| 163 | } else { |
| 164 | ret = -EIO; |
| 165 | goto bad_inode; |
| 166 | } |
| 167 | unlock_new_inode(inode); |
| 168 | |
| 169 | return inode; |
| 170 | |
| 171 | bad_inode: |
| 172 | iget_failed(inode); |
| 173 | return ERR_PTR(ret); |
| 174 | } |
| 175 | |
| 176 | void update_inode(struct inode *inode, struct page *node_page) |
| 177 | { |
| 178 | struct f2fs_node *rn; |
| 179 | struct f2fs_inode *ri; |
| 180 | |
| 181 | wait_on_page_writeback(node_page); |
| 182 | |
| 183 | rn = page_address(node_page); |
| 184 | ri = &(rn->i); |
| 185 | |
| 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); |
| 194 | |
| 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); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 204 | ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino); |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 205 | ri->i_generation = cpu_to_le32(inode->i_generation); |
| 206 | set_page_dirty(node_page); |
| 207 | } |
| 208 | |
| 209 | int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 210 | { |
| 211 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 212 | struct page *node_page; |
| 213 | bool need_lock = false; |
| 214 | |
| 215 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 216 | inode->i_ino == F2FS_META_INO(sbi)) |
| 217 | return 0; |
| 218 | |
| 219 | node_page = get_node_page(sbi, inode->i_ino); |
| 220 | if (IS_ERR(node_page)) |
| 221 | return PTR_ERR(node_page); |
| 222 | |
| 223 | if (!PageDirty(node_page)) { |
| 224 | need_lock = true; |
| 225 | f2fs_put_page(node_page, 1); |
| 226 | mutex_lock(&sbi->write_inode); |
| 227 | node_page = get_node_page(sbi, inode->i_ino); |
| 228 | if (IS_ERR(node_page)) { |
| 229 | mutex_unlock(&sbi->write_inode); |
| 230 | return PTR_ERR(node_page); |
| 231 | } |
| 232 | } |
| 233 | update_inode(inode, node_page); |
| 234 | f2fs_put_page(node_page, 1); |
| 235 | if (need_lock) |
| 236 | mutex_unlock(&sbi->write_inode); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 240 | /* |
Jaegeuk Kim | 19f99ce | 2012-11-02 17:10:40 +0900 | [diff] [blame] | 241 | * Called at the last iput() if i_nlink is zero |
| 242 | */ |
| 243 | void f2fs_evict_inode(struct inode *inode) |
| 244 | { |
| 245 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 246 | |
| 247 | truncate_inode_pages(&inode->i_data, 0); |
| 248 | |
| 249 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 250 | inode->i_ino == F2FS_META_INO(sbi)) |
| 251 | goto no_delete; |
| 252 | |
| 253 | BUG_ON(atomic_read(&F2FS_I(inode)->dirty_dents)); |
| 254 | remove_dirty_dir_inode(inode); |
| 255 | |
| 256 | if (inode->i_nlink || is_bad_inode(inode)) |
| 257 | goto no_delete; |
| 258 | |
| 259 | set_inode_flag(F2FS_I(inode), FI_NO_ALLOC); |
| 260 | i_size_write(inode, 0); |
| 261 | |
| 262 | if (F2FS_HAS_BLOCKS(inode)) |
| 263 | f2fs_truncate(inode); |
| 264 | |
| 265 | remove_inode_page(inode); |
| 266 | no_delete: |
| 267 | clear_inode(inode); |
| 268 | } |