Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 2 | * fs/f2fs/dir.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 "f2fs.h" |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 14 | #include "node.h" |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 15 | #include "acl.h" |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 16 | #include "xattr.h" |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 17 | |
| 18 | static unsigned long dir_blocks(struct inode *inode) |
| 19 | { |
| 20 | return ((unsigned long long) (i_size_read(inode) + PAGE_CACHE_SIZE - 1)) |
| 21 | >> PAGE_CACHE_SHIFT; |
| 22 | } |
| 23 | |
| 24 | static unsigned int dir_buckets(unsigned int level) |
| 25 | { |
| 26 | if (level < MAX_DIR_HASH_DEPTH / 2) |
| 27 | return 1 << level; |
| 28 | else |
| 29 | return 1 << ((MAX_DIR_HASH_DEPTH / 2) - 1); |
| 30 | } |
| 31 | |
| 32 | static unsigned int bucket_blocks(unsigned int level) |
| 33 | { |
| 34 | if (level < MAX_DIR_HASH_DEPTH / 2) |
| 35 | return 2; |
| 36 | else |
| 37 | return 4; |
| 38 | } |
| 39 | |
| 40 | static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = { |
| 41 | [F2FS_FT_UNKNOWN] = DT_UNKNOWN, |
| 42 | [F2FS_FT_REG_FILE] = DT_REG, |
| 43 | [F2FS_FT_DIR] = DT_DIR, |
| 44 | [F2FS_FT_CHRDEV] = DT_CHR, |
| 45 | [F2FS_FT_BLKDEV] = DT_BLK, |
| 46 | [F2FS_FT_FIFO] = DT_FIFO, |
| 47 | [F2FS_FT_SOCK] = DT_SOCK, |
| 48 | [F2FS_FT_SYMLINK] = DT_LNK, |
| 49 | }; |
| 50 | |
| 51 | #define S_SHIFT 12 |
| 52 | static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 53 | [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE, |
| 54 | [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR, |
| 55 | [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV, |
| 56 | [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV, |
| 57 | [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO, |
| 58 | [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK, |
| 59 | [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK, |
| 60 | }; |
| 61 | |
| 62 | static void set_de_type(struct f2fs_dir_entry *de, struct inode *inode) |
| 63 | { |
Al Viro | 0ecc833 | 2013-03-29 12:23:28 -0400 | [diff] [blame] | 64 | umode_t mode = inode->i_mode; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 65 | de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT]; |
| 66 | } |
| 67 | |
| 68 | static unsigned long dir_block_index(unsigned int level, unsigned int idx) |
| 69 | { |
| 70 | unsigned long i; |
| 71 | unsigned long bidx = 0; |
| 72 | |
| 73 | for (i = 0; i < level; i++) |
| 74 | bidx += dir_buckets(i) * bucket_blocks(i); |
| 75 | bidx += idx * bucket_blocks(level); |
| 76 | return bidx; |
| 77 | } |
| 78 | |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 79 | static bool early_match_name(const char *name, size_t namelen, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 80 | f2fs_hash_t namehash, struct f2fs_dir_entry *de) |
| 81 | { |
| 82 | if (le16_to_cpu(de->name_len) != namelen) |
| 83 | return false; |
| 84 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 85 | if (de->hash_code != namehash) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 86 | return false; |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | static struct f2fs_dir_entry *find_in_block(struct page *dentry_page, |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 92 | const char *name, size_t namelen, int *max_slots, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 93 | f2fs_hash_t namehash, struct page **res_page) |
| 94 | { |
| 95 | struct f2fs_dir_entry *de; |
| 96 | unsigned long bit_pos, end_pos, next_pos; |
| 97 | struct f2fs_dentry_block *dentry_blk = kmap(dentry_page); |
| 98 | int slots; |
| 99 | |
| 100 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 101 | NR_DENTRY_IN_BLOCK, 0); |
| 102 | while (bit_pos < NR_DENTRY_IN_BLOCK) { |
| 103 | de = &dentry_blk->dentry[bit_pos]; |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 104 | slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 105 | |
| 106 | if (early_match_name(name, namelen, namehash, de)) { |
| 107 | if (!memcmp(dentry_blk->filename[bit_pos], |
| 108 | name, namelen)) { |
| 109 | *res_page = dentry_page; |
| 110 | goto found; |
| 111 | } |
| 112 | } |
| 113 | next_pos = bit_pos + slots; |
| 114 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 115 | NR_DENTRY_IN_BLOCK, next_pos); |
| 116 | if (bit_pos >= NR_DENTRY_IN_BLOCK) |
| 117 | end_pos = NR_DENTRY_IN_BLOCK; |
| 118 | else |
| 119 | end_pos = bit_pos; |
| 120 | if (*max_slots < end_pos - next_pos) |
| 121 | *max_slots = end_pos - next_pos; |
| 122 | } |
| 123 | |
| 124 | de = NULL; |
| 125 | kunmap(dentry_page); |
| 126 | found: |
| 127 | return de; |
| 128 | } |
| 129 | |
| 130 | static struct f2fs_dir_entry *find_in_level(struct inode *dir, |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 131 | unsigned int level, const char *name, size_t namelen, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 132 | f2fs_hash_t namehash, struct page **res_page) |
| 133 | { |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 134 | int s = GET_DENTRY_SLOTS(namelen); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 135 | unsigned int nbucket, nblock; |
| 136 | unsigned int bidx, end_block; |
| 137 | struct page *dentry_page; |
| 138 | struct f2fs_dir_entry *de = NULL; |
| 139 | bool room = false; |
| 140 | int max_slots = 0; |
| 141 | |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 142 | f2fs_bug_on(level > MAX_DIR_HASH_DEPTH); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 143 | |
| 144 | nbucket = dir_buckets(level); |
| 145 | nblock = bucket_blocks(level); |
| 146 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 147 | bidx = dir_block_index(level, le32_to_cpu(namehash) % nbucket); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 148 | end_block = bidx + nblock; |
| 149 | |
| 150 | for (; bidx < end_block; bidx++) { |
| 151 | /* no need to allocate new dentry pages to all the indices */ |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 152 | dentry_page = find_data_page(dir, bidx, true); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 153 | if (IS_ERR(dentry_page)) { |
| 154 | room = true; |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | de = find_in_block(dentry_page, name, namelen, |
| 159 | &max_slots, namehash, res_page); |
| 160 | if (de) |
| 161 | break; |
| 162 | |
| 163 | if (max_slots >= s) |
| 164 | room = true; |
| 165 | f2fs_put_page(dentry_page, 0); |
| 166 | } |
| 167 | |
| 168 | if (!de && room && F2FS_I(dir)->chash != namehash) { |
| 169 | F2FS_I(dir)->chash = namehash; |
| 170 | F2FS_I(dir)->clevel = level; |
| 171 | } |
| 172 | |
| 173 | return de; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Find an entry in the specified directory with the wanted name. |
| 178 | * It returns the page where the entry was found (as a parameter - res_page), |
| 179 | * and the entry itself. Page is returned mapped and unlocked. |
| 180 | * Entry is guaranteed to be valid. |
| 181 | */ |
| 182 | struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, |
| 183 | struct qstr *child, struct page **res_page) |
| 184 | { |
| 185 | const char *name = child->name; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 186 | size_t namelen = child->len; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 187 | unsigned long npages = dir_blocks(dir); |
| 188 | struct f2fs_dir_entry *de = NULL; |
| 189 | f2fs_hash_t name_hash; |
| 190 | unsigned int max_depth; |
| 191 | unsigned int level; |
| 192 | |
| 193 | if (npages == 0) |
| 194 | return NULL; |
| 195 | |
| 196 | *res_page = NULL; |
| 197 | |
| 198 | name_hash = f2fs_dentry_hash(name, namelen); |
| 199 | max_depth = F2FS_I(dir)->i_current_depth; |
| 200 | |
| 201 | for (level = 0; level < max_depth; level++) { |
| 202 | de = find_in_level(dir, level, name, |
| 203 | namelen, name_hash, res_page); |
| 204 | if (de) |
| 205 | break; |
| 206 | } |
| 207 | if (!de && F2FS_I(dir)->chash != name_hash) { |
| 208 | F2FS_I(dir)->chash = name_hash; |
| 209 | F2FS_I(dir)->clevel = level - 1; |
| 210 | } |
| 211 | return de; |
| 212 | } |
| 213 | |
| 214 | struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p) |
| 215 | { |
Namjae Jeon | 4777f86 | 2013-05-23 22:58:07 +0900 | [diff] [blame] | 216 | struct page *page; |
| 217 | struct f2fs_dir_entry *de; |
| 218 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 219 | |
| 220 | page = get_lock_data_page(dir, 0); |
| 221 | if (IS_ERR(page)) |
| 222 | return NULL; |
| 223 | |
| 224 | dentry_blk = kmap(page); |
| 225 | de = &dentry_blk->dentry[1]; |
| 226 | *p = page; |
| 227 | unlock_page(page); |
| 228 | return de; |
| 229 | } |
| 230 | |
| 231 | ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr) |
| 232 | { |
| 233 | ino_t res = 0; |
| 234 | struct f2fs_dir_entry *de; |
| 235 | struct page *page; |
| 236 | |
| 237 | de = f2fs_find_entry(dir, qstr, &page); |
| 238 | if (de) { |
| 239 | res = le32_to_cpu(de->ino); |
| 240 | kunmap(page); |
| 241 | f2fs_put_page(page, 0); |
| 242 | } |
| 243 | |
| 244 | return res; |
| 245 | } |
| 246 | |
| 247 | void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, |
| 248 | struct page *page, struct inode *inode) |
| 249 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 250 | lock_page(page); |
| 251 | wait_on_page_writeback(page); |
| 252 | de->ino = cpu_to_le32(inode->i_ino); |
| 253 | set_de_type(de, inode); |
| 254 | kunmap(page); |
| 255 | set_page_dirty(page); |
| 256 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 257 | mark_inode_dirty(dir); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 258 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 259 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 262 | static void init_dent_inode(const struct qstr *name, struct page *ipage) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 263 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 264 | struct f2fs_node *rn; |
| 265 | |
Al Viro | 53dc9a6 | 2013-01-25 16:01:21 -0500 | [diff] [blame] | 266 | /* copy name info. to this inode page */ |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 267 | rn = F2FS_NODE(ipage); |
Al Viro | 53dc9a6 | 2013-01-25 16:01:21 -0500 | [diff] [blame] | 268 | rn->i.i_namelen = cpu_to_le32(name->len); |
| 269 | memcpy(rn->i.i_name, name->name, name->len); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 270 | set_page_dirty(ipage); |
| 271 | } |
| 272 | |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 273 | int update_dent_inode(struct inode *inode, const struct qstr *name) |
| 274 | { |
| 275 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 276 | struct page *page; |
| 277 | |
| 278 | page = get_node_page(sbi, inode->i_ino); |
| 279 | if (IS_ERR(page)) |
| 280 | return PTR_ERR(page); |
| 281 | |
| 282 | init_dent_inode(name, page); |
| 283 | f2fs_put_page(page, 1); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 288 | static int make_empty_dir(struct inode *inode, |
| 289 | struct inode *parent, struct page *page) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 290 | { |
| 291 | struct page *dentry_page; |
| 292 | struct f2fs_dentry_block *dentry_blk; |
| 293 | struct f2fs_dir_entry *de; |
| 294 | void *kaddr; |
| 295 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 296 | dentry_page = get_new_data_page(inode, page, 0, true); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 297 | if (IS_ERR(dentry_page)) |
| 298 | return PTR_ERR(dentry_page); |
| 299 | |
| 300 | kaddr = kmap_atomic(dentry_page); |
| 301 | dentry_blk = (struct f2fs_dentry_block *)kaddr; |
| 302 | |
| 303 | de = &dentry_blk->dentry[0]; |
| 304 | de->name_len = cpu_to_le16(1); |
| 305 | de->hash_code = 0; |
| 306 | de->ino = cpu_to_le32(inode->i_ino); |
| 307 | memcpy(dentry_blk->filename[0], ".", 1); |
| 308 | set_de_type(de, inode); |
| 309 | |
| 310 | de = &dentry_blk->dentry[1]; |
| 311 | de->hash_code = 0; |
| 312 | de->name_len = cpu_to_le16(2); |
| 313 | de->ino = cpu_to_le32(parent->i_ino); |
| 314 | memcpy(dentry_blk->filename[1], "..", 2); |
| 315 | set_de_type(de, inode); |
| 316 | |
| 317 | test_and_set_bit_le(0, &dentry_blk->dentry_bitmap); |
| 318 | test_and_set_bit_le(1, &dentry_blk->dentry_bitmap); |
| 319 | kunmap_atomic(kaddr); |
| 320 | |
| 321 | set_page_dirty(dentry_page); |
| 322 | f2fs_put_page(dentry_page, 1); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 326 | static struct page *init_inode_metadata(struct inode *inode, |
Al Viro | 69f24ea | 2013-01-25 16:08:53 -0500 | [diff] [blame] | 327 | struct inode *dir, const struct qstr *name) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 328 | { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 329 | struct page *page; |
| 330 | int err; |
| 331 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 332 | if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 333 | page = new_inode_page(inode, name); |
| 334 | if (IS_ERR(page)) |
| 335 | return page; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 336 | |
| 337 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 338 | err = make_empty_dir(inode, dir, page); |
| 339 | if (err) |
| 340 | goto error; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 341 | } |
| 342 | |
Jaegeuk Kim | 2ed2d5b | 2013-10-28 13:17:54 +0900 | [diff] [blame] | 343 | err = f2fs_init_acl(inode, dir, page); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 344 | if (err) |
| 345 | goto error; |
| 346 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 347 | err = f2fs_init_security(inode, dir, name, page); |
| 348 | if (err) |
| 349 | goto error; |
| 350 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 351 | wait_on_page_writeback(page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 352 | } else { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 353 | page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino); |
| 354 | if (IS_ERR(page)) |
| 355 | return page; |
| 356 | |
| 357 | wait_on_page_writeback(page); |
| 358 | set_cold_node(inode, page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 359 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 360 | |
| 361 | init_dent_inode(name, page); |
| 362 | |
Jaegeuk Kim | 83d5d6f | 2013-05-28 12:25:47 +0900 | [diff] [blame] | 363 | /* |
| 364 | * This file should be checkpointed during fsync. |
| 365 | * We lost i_pino from now on. |
| 366 | */ |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 367 | if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 368 | file_lost_pino(inode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 369 | inc_nlink(inode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 370 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 371 | return page; |
| 372 | |
| 373 | error: |
| 374 | f2fs_put_page(page, 1); |
| 375 | remove_inode_page(inode); |
| 376 | return ERR_PTR(err); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void update_parent_metadata(struct inode *dir, struct inode *inode, |
| 380 | unsigned int current_depth) |
| 381 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 382 | if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { |
| 383 | if (S_ISDIR(inode->i_mode)) { |
| 384 | inc_nlink(dir); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 385 | set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 386 | } |
| 387 | clear_inode_flag(F2FS_I(inode), FI_NEW_INODE); |
| 388 | } |
| 389 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 390 | if (F2FS_I(dir)->i_current_depth != current_depth) { |
| 391 | F2FS_I(dir)->i_current_depth = current_depth; |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 392 | set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 395 | if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 396 | update_inode_page(dir); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 397 | else |
| 398 | mark_inode_dirty(dir); |
| 399 | |
| 400 | if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) |
| 401 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 402 | } |
| 403 | |
| 404 | static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots) |
| 405 | { |
| 406 | int bit_start = 0; |
| 407 | int zero_start, zero_end; |
| 408 | next: |
| 409 | zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap, |
| 410 | NR_DENTRY_IN_BLOCK, |
| 411 | bit_start); |
| 412 | if (zero_start >= NR_DENTRY_IN_BLOCK) |
| 413 | return NR_DENTRY_IN_BLOCK; |
| 414 | |
| 415 | zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 416 | NR_DENTRY_IN_BLOCK, |
| 417 | zero_start); |
| 418 | if (zero_end - zero_start >= slots) |
| 419 | return zero_start; |
| 420 | |
| 421 | bit_start = zero_end + 1; |
| 422 | |
| 423 | if (zero_end + 1 >= NR_DENTRY_IN_BLOCK) |
| 424 | return NR_DENTRY_IN_BLOCK; |
| 425 | goto next; |
| 426 | } |
| 427 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 428 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 429 | * Caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 430 | * f2fs_unlock_op(). |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 431 | */ |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 432 | int __f2fs_add_link(struct inode *dir, const struct qstr *name, struct inode *inode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 433 | { |
| 434 | unsigned int bit_pos; |
| 435 | unsigned int level; |
| 436 | unsigned int current_depth; |
| 437 | unsigned long bidx, block; |
| 438 | f2fs_hash_t dentry_hash; |
| 439 | struct f2fs_dir_entry *de; |
| 440 | unsigned int nbucket, nblock; |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 441 | size_t namelen = name->len; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 442 | struct page *dentry_page = NULL; |
| 443 | struct f2fs_dentry_block *dentry_blk = NULL; |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 444 | int slots = GET_DENTRY_SLOTS(namelen); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 445 | struct page *page; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 446 | int err = 0; |
| 447 | int i; |
| 448 | |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 449 | dentry_hash = f2fs_dentry_hash(name->name, name->len); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 450 | level = 0; |
| 451 | current_depth = F2FS_I(dir)->i_current_depth; |
| 452 | if (F2FS_I(dir)->chash == dentry_hash) { |
| 453 | level = F2FS_I(dir)->clevel; |
| 454 | F2FS_I(dir)->chash = 0; |
| 455 | } |
| 456 | |
| 457 | start: |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 458 | if (unlikely(current_depth == MAX_DIR_HASH_DEPTH)) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 459 | return -ENOSPC; |
| 460 | |
| 461 | /* Increase the depth, if required */ |
| 462 | if (level == current_depth) |
| 463 | ++current_depth; |
| 464 | |
| 465 | nbucket = dir_buckets(level); |
| 466 | nblock = bucket_blocks(level); |
| 467 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 468 | bidx = dir_block_index(level, (le32_to_cpu(dentry_hash) % nbucket)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 469 | |
| 470 | for (block = bidx; block <= (bidx + nblock - 1); block++) { |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 471 | dentry_page = get_new_data_page(dir, NULL, block, true); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 472 | if (IS_ERR(dentry_page)) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 473 | return PTR_ERR(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 474 | |
| 475 | dentry_blk = kmap(dentry_page); |
| 476 | bit_pos = room_for_filename(dentry_blk, slots); |
| 477 | if (bit_pos < NR_DENTRY_IN_BLOCK) |
| 478 | goto add_dentry; |
| 479 | |
| 480 | kunmap(dentry_page); |
| 481 | f2fs_put_page(dentry_page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* Move to next level to find the empty slot for new dentry */ |
| 485 | ++level; |
| 486 | goto start; |
| 487 | add_dentry: |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 488 | wait_on_page_writeback(dentry_page); |
| 489 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 490 | page = init_inode_metadata(inode, dir, name); |
| 491 | if (IS_ERR(page)) { |
| 492 | err = PTR_ERR(page); |
| 493 | goto fail; |
| 494 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 495 | de = &dentry_blk->dentry[bit_pos]; |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 496 | de->hash_code = dentry_hash; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 497 | de->name_len = cpu_to_le16(namelen); |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 498 | memcpy(dentry_blk->filename[bit_pos], name->name, name->len); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 499 | de->ino = cpu_to_le32(inode->i_ino); |
| 500 | set_de_type(de, inode); |
| 501 | for (i = 0; i < slots; i++) |
| 502 | test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); |
| 503 | set_page_dirty(dentry_page); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 504 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 505 | /* we don't need to mark_inode_dirty now */ |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 506 | F2FS_I(inode)->i_pino = dir->i_ino; |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 507 | update_inode(inode, page); |
| 508 | f2fs_put_page(page, 1); |
| 509 | |
| 510 | update_parent_metadata(dir, inode, current_depth); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 511 | fail: |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 512 | clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 513 | kunmap(dentry_page); |
| 514 | f2fs_put_page(dentry_page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 515 | return err; |
| 516 | } |
| 517 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 518 | /* |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 519 | * It only removes the dentry from the dentry page,corresponding name |
| 520 | * entry in name page does not need to be touched during deletion. |
| 521 | */ |
| 522 | void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page, |
| 523 | struct inode *inode) |
| 524 | { |
| 525 | struct f2fs_dentry_block *dentry_blk; |
| 526 | unsigned int bit_pos; |
| 527 | struct address_space *mapping = page->mapping; |
| 528 | struct inode *dir = mapping->host; |
| 529 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 530 | int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 531 | void *kaddr = page_address(page); |
| 532 | int i; |
| 533 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 534 | lock_page(page); |
| 535 | wait_on_page_writeback(page); |
| 536 | |
| 537 | dentry_blk = (struct f2fs_dentry_block *)kaddr; |
| 538 | bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry; |
| 539 | for (i = 0; i < slots; i++) |
| 540 | test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); |
| 541 | |
| 542 | /* Let's check and deallocate this dentry page */ |
| 543 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 544 | NR_DENTRY_IN_BLOCK, |
| 545 | 0); |
| 546 | kunmap(page); /* kunmap - pair of f2fs_find_entry */ |
| 547 | set_page_dirty(page); |
| 548 | |
| 549 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 550 | |
| 551 | if (inode && S_ISDIR(inode->i_mode)) { |
| 552 | drop_nlink(dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 553 | update_inode_page(dir); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 554 | } else { |
| 555 | mark_inode_dirty(dir); |
| 556 | } |
| 557 | |
| 558 | if (inode) { |
Namjae Jeon | 1637998 | 2013-01-12 14:41:33 +0900 | [diff] [blame] | 559 | inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 560 | drop_nlink(inode); |
| 561 | if (S_ISDIR(inode->i_mode)) { |
| 562 | drop_nlink(inode); |
| 563 | i_size_write(inode, 0); |
| 564 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 565 | update_inode_page(inode); |
| 566 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 567 | if (inode->i_nlink == 0) |
| 568 | add_orphan_inode(sbi, inode->i_ino); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 569 | else |
| 570 | release_orphan_inode(sbi); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if (bit_pos == NR_DENTRY_IN_BLOCK) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 574 | truncate_hole(dir, page->index, page->index + 1); |
| 575 | clear_page_dirty_for_io(page); |
| 576 | ClearPageUptodate(page); |
| 577 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 578 | inode_dec_dirty_dents(dir); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 579 | } |
Namjae Jeon | 508198b | 2012-12-08 14:54:35 +0900 | [diff] [blame] | 580 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | bool f2fs_empty_dir(struct inode *dir) |
| 584 | { |
| 585 | unsigned long bidx; |
| 586 | struct page *dentry_page; |
| 587 | unsigned int bit_pos; |
| 588 | struct f2fs_dentry_block *dentry_blk; |
| 589 | unsigned long nblock = dir_blocks(dir); |
| 590 | |
| 591 | for (bidx = 0; bidx < nblock; bidx++) { |
| 592 | void *kaddr; |
| 593 | dentry_page = get_lock_data_page(dir, bidx); |
| 594 | if (IS_ERR(dentry_page)) { |
| 595 | if (PTR_ERR(dentry_page) == -ENOENT) |
| 596 | continue; |
| 597 | else |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | kaddr = kmap_atomic(dentry_page); |
| 602 | dentry_blk = (struct f2fs_dentry_block *)kaddr; |
| 603 | if (bidx == 0) |
| 604 | bit_pos = 2; |
| 605 | else |
| 606 | bit_pos = 0; |
| 607 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 608 | NR_DENTRY_IN_BLOCK, |
| 609 | bit_pos); |
| 610 | kunmap_atomic(kaddr); |
| 611 | |
| 612 | f2fs_put_page(dentry_page, 1); |
| 613 | |
| 614 | if (bit_pos < NR_DENTRY_IN_BLOCK) |
| 615 | return false; |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 620 | static int f2fs_readdir(struct file *file, struct dir_context *ctx) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 621 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 622 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 623 | unsigned long npages = dir_blocks(inode); |
Jaegeuk Kim | 99b072bb | 2013-07-05 17:28:12 +0900 | [diff] [blame] | 624 | unsigned int bit_pos = 0; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 625 | struct f2fs_dentry_block *dentry_blk = NULL; |
| 626 | struct f2fs_dir_entry *de = NULL; |
| 627 | struct page *dentry_page = NULL; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 628 | unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 629 | unsigned char d_type = DT_UNKNOWN; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 630 | |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 631 | bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 632 | |
| 633 | for ( ; n < npages; n++) { |
| 634 | dentry_page = get_lock_data_page(inode, n); |
| 635 | if (IS_ERR(dentry_page)) |
| 636 | continue; |
| 637 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 638 | dentry_blk = kmap(dentry_page); |
| 639 | while (bit_pos < NR_DENTRY_IN_BLOCK) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 640 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 641 | NR_DENTRY_IN_BLOCK, |
| 642 | bit_pos); |
| 643 | if (bit_pos >= NR_DENTRY_IN_BLOCK) |
| 644 | break; |
| 645 | |
| 646 | de = &dentry_blk->dentry[bit_pos]; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 647 | if (de->file_type < F2FS_FT_MAX) |
| 648 | d_type = f2fs_filetype_table[de->file_type]; |
| 649 | else |
| 650 | d_type = DT_UNKNOWN; |
| 651 | if (!dir_emit(ctx, |
Jaegeuk Kim | 99b072bb | 2013-07-05 17:28:12 +0900 | [diff] [blame] | 652 | dentry_blk->filename[bit_pos], |
| 653 | le16_to_cpu(de->name_len), |
| 654 | le32_to_cpu(de->ino), d_type)) |
| 655 | goto stop; |
| 656 | |
| 657 | bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
| 658 | ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 659 | } |
| 660 | bit_pos = 0; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 661 | ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 662 | kunmap(dentry_page); |
| 663 | f2fs_put_page(dentry_page, 1); |
| 664 | dentry_page = NULL; |
| 665 | } |
Jaegeuk Kim | 99b072bb | 2013-07-05 17:28:12 +0900 | [diff] [blame] | 666 | stop: |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 667 | if (dentry_page && !IS_ERR(dentry_page)) { |
| 668 | kunmap(dentry_page); |
| 669 | f2fs_put_page(dentry_page, 1); |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | const struct file_operations f2fs_dir_operations = { |
| 676 | .llseek = generic_file_llseek, |
| 677 | .read = generic_read_dir, |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 678 | .iterate = f2fs_readdir, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 679 | .fsync = f2fs_sync_file, |
| 680 | .unlocked_ioctl = f2fs_ioctl, |
| 681 | }; |