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