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 | { |
Chao Yu | bfec07d | 2014-05-28 08:56:09 +0800 | [diff] [blame] | 26 | if (level + dir_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 |
Chao Yu | bfec07d | 2014-05-28 08:56:09 +0800 | [diff] [blame] | 29 | return MAX_DIR_BUCKETS; |
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 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 40 | unsigned char f2fs_filetype_table[F2FS_FT_MAX] = { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 62 | void set_de_type(struct f2fs_dir_entry *de, umode_t mode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 63 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 64 | de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT]; |
| 65 | } |
| 66 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 67 | static unsigned long dir_block_index(unsigned int level, |
| 68 | int dir_level, unsigned int idx) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 69 | { |
| 70 | unsigned long i; |
| 71 | unsigned long bidx = 0; |
| 72 | |
| 73 | for (i = 0; i < level; i++) |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 74 | bidx += dir_buckets(i, dir_level) * bucket_blocks(i); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 75 | bidx += idx * bucket_blocks(level); |
| 76 | return bidx; |
| 77 | } |
| 78 | |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 79 | static bool early_match_name(size_t namelen, f2fs_hash_t namehash, |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 80 | struct f2fs_dir_entry *de) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 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, |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 92 | struct qstr *name, int *max_slots, |
| 93 | struct page **res_page) |
| 94 | { |
| 95 | struct f2fs_dentry_block *dentry_blk; |
| 96 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 97 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 98 | |
| 99 | dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page); |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 100 | |
| 101 | make_dentry_ptr(&d, (void *)dentry_blk, 1); |
| 102 | de = find_target_dentry(name, max_slots, &d); |
| 103 | |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 104 | if (de) |
| 105 | *res_page = dentry_page; |
| 106 | else |
| 107 | kunmap(dentry_page); |
| 108 | |
| 109 | /* |
| 110 | * For the most part, it should be a bug when name_len is zero. |
| 111 | * We stop here for figuring out where the bugs has occurred. |
| 112 | */ |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 113 | f2fs_bug_on(F2FS_P_SB(dentry_page), d.max < 0); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 114 | return de; |
| 115 | } |
| 116 | |
| 117 | struct f2fs_dir_entry *find_target_dentry(struct qstr *name, int *max_slots, |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 118 | struct f2fs_dentry_ptr *d) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 119 | { |
| 120 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 121 | unsigned long bit_pos = 0; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 122 | f2fs_hash_t namehash = f2fs_dentry_hash(name); |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 123 | int max_len = 0; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 124 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 125 | if (max_slots) |
| 126 | *max_slots = 0; |
| 127 | while (bit_pos < d->max) { |
| 128 | if (!test_bit_le(bit_pos, d->bitmap)) { |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 129 | bit_pos++; |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 130 | max_len++; |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 131 | continue; |
| 132 | } |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 133 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 134 | de = &d->dentry[bit_pos]; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 135 | if (early_match_name(name->len, namehash, de) && |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 136 | !memcmp(d->filename[bit_pos], name->name, name->len)) |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 137 | goto found; |
| 138 | |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 139 | if (max_slots && max_len > *max_slots) |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 140 | *max_slots = max_len; |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 141 | max_len = 0; |
Chao Yu | 81e366f8 | 2014-07-10 12:37:46 +0800 | [diff] [blame] | 142 | |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 143 | /* remain bug on condition */ |
| 144 | if (unlikely(!de->name_len)) |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 145 | d->max = -1; |
Chao Yu | 81e366f8 | 2014-07-10 12:37:46 +0800 | [diff] [blame] | 146 | |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 147 | bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | de = NULL; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 151 | found: |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 152 | if (max_slots && max_len > *max_slots) |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 153 | *max_slots = max_len; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 154 | return de; |
| 155 | } |
| 156 | |
| 157 | static struct f2fs_dir_entry *find_in_level(struct inode *dir, |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 158 | unsigned int level, struct qstr *name, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 159 | f2fs_hash_t namehash, struct page **res_page) |
| 160 | { |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 161 | int s = GET_DENTRY_SLOTS(name->len); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 162 | unsigned int nbucket, nblock; |
| 163 | unsigned int bidx, end_block; |
| 164 | struct page *dentry_page; |
| 165 | struct f2fs_dir_entry *de = NULL; |
| 166 | bool room = false; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 167 | int max_slots; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 168 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 169 | f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 170 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 171 | nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 172 | nblock = bucket_blocks(level); |
| 173 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 174 | bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level, |
| 175 | le32_to_cpu(namehash) % nbucket); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 176 | end_block = bidx + nblock; |
| 177 | |
| 178 | for (; bidx < end_block; bidx++) { |
| 179 | /* no need to allocate new dentry pages to all the indices */ |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 180 | dentry_page = find_data_page(dir, bidx, true); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 181 | if (IS_ERR(dentry_page)) { |
| 182 | room = true; |
| 183 | continue; |
| 184 | } |
| 185 | |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 186 | de = find_in_block(dentry_page, name, &max_slots, res_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 187 | if (de) |
| 188 | break; |
| 189 | |
| 190 | if (max_slots >= s) |
| 191 | room = true; |
| 192 | f2fs_put_page(dentry_page, 0); |
| 193 | } |
| 194 | |
| 195 | if (!de && room && F2FS_I(dir)->chash != namehash) { |
| 196 | F2FS_I(dir)->chash = namehash; |
| 197 | F2FS_I(dir)->clevel = level; |
| 198 | } |
| 199 | |
| 200 | return de; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Find an entry in the specified directory with the wanted name. |
| 205 | * It returns the page where the entry was found (as a parameter - res_page), |
| 206 | * and the entry itself. Page is returned mapped and unlocked. |
| 207 | * Entry is guaranteed to be valid. |
| 208 | */ |
| 209 | struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, |
| 210 | struct qstr *child, struct page **res_page) |
| 211 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 212 | unsigned long npages = dir_blocks(dir); |
| 213 | struct f2fs_dir_entry *de = NULL; |
| 214 | f2fs_hash_t name_hash; |
| 215 | unsigned int max_depth; |
| 216 | unsigned int level; |
| 217 | |
Yuan Zhong | b1f73b7 | 2015-03-07 10:07:42 +0000 | [diff] [blame] | 218 | *res_page = NULL; |
| 219 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 220 | if (f2fs_has_inline_dentry(dir)) |
| 221 | return find_in_inline_dir(dir, child, res_page); |
| 222 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 223 | if (npages == 0) |
| 224 | return NULL; |
| 225 | |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 226 | name_hash = f2fs_dentry_hash(child); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 227 | max_depth = F2FS_I(dir)->i_current_depth; |
| 228 | |
| 229 | for (level = 0; level < max_depth; level++) { |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 230 | de = find_in_level(dir, level, child, name_hash, res_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 231 | if (de) |
| 232 | break; |
| 233 | } |
| 234 | if (!de && F2FS_I(dir)->chash != name_hash) { |
| 235 | F2FS_I(dir)->chash = name_hash; |
| 236 | F2FS_I(dir)->clevel = level - 1; |
| 237 | } |
| 238 | return de; |
| 239 | } |
| 240 | |
| 241 | struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p) |
| 242 | { |
Namjae Jeon | 4777f86 | 2013-05-23 22:58:07 +0900 | [diff] [blame] | 243 | struct page *page; |
| 244 | struct f2fs_dir_entry *de; |
| 245 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 246 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 247 | if (f2fs_has_inline_dentry(dir)) |
| 248 | return f2fs_parent_inline_dir(dir, p); |
| 249 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 250 | page = get_lock_data_page(dir, 0); |
| 251 | if (IS_ERR(page)) |
| 252 | return NULL; |
| 253 | |
| 254 | dentry_blk = kmap(page); |
| 255 | de = &dentry_blk->dentry[1]; |
| 256 | *p = page; |
| 257 | unlock_page(page); |
| 258 | return de; |
| 259 | } |
| 260 | |
| 261 | ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr) |
| 262 | { |
| 263 | ino_t res = 0; |
| 264 | struct f2fs_dir_entry *de; |
| 265 | struct page *page; |
| 266 | |
| 267 | de = f2fs_find_entry(dir, qstr, &page); |
| 268 | if (de) { |
| 269 | res = le32_to_cpu(de->ino); |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 270 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 271 | f2fs_put_page(page, 0); |
| 272 | } |
| 273 | |
| 274 | return res; |
| 275 | } |
| 276 | |
| 277 | void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, |
| 278 | struct page *page, struct inode *inode) |
| 279 | { |
Jaegeuk Kim | 59a0615 | 2014-10-13 19:34:26 -0700 | [diff] [blame] | 280 | enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 281 | lock_page(page); |
Jaegeuk Kim | 59a0615 | 2014-10-13 19:34:26 -0700 | [diff] [blame] | 282 | f2fs_wait_on_page_writeback(page, type); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 283 | de->ino = cpu_to_le32(inode->i_ino); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 284 | set_de_type(de, inode->i_mode); |
Jaegeuk Kim | 5df1f1d | 2014-12-13 13:13:11 -0800 | [diff] [blame] | 285 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 286 | set_page_dirty(page); |
| 287 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 288 | mark_inode_dirty(dir); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 289 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 290 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 293 | static void init_dent_inode(const struct qstr *name, struct page *ipage) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 294 | { |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 295 | struct f2fs_inode *ri; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 296 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 297 | f2fs_wait_on_page_writeback(ipage, NODE); |
| 298 | |
Al Viro | 53dc9a6 | 2013-01-25 16:01:21 -0500 | [diff] [blame] | 299 | /* copy name info. to this inode page */ |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 300 | ri = F2FS_INODE(ipage); |
| 301 | ri->i_namelen = cpu_to_le32(name->len); |
| 302 | memcpy(ri->i_name, name->name, name->len); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 303 | set_page_dirty(ipage); |
| 304 | } |
| 305 | |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 306 | int update_dent_inode(struct inode *inode, const struct qstr *name) |
| 307 | { |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 308 | struct page *page; |
| 309 | |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 310 | page = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 311 | if (IS_ERR(page)) |
| 312 | return PTR_ERR(page); |
| 313 | |
| 314 | init_dent_inode(name, page); |
| 315 | f2fs_put_page(page, 1); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 320 | void do_make_empty_dir(struct inode *inode, struct inode *parent, |
| 321 | struct f2fs_dentry_ptr *d) |
| 322 | { |
| 323 | struct f2fs_dir_entry *de; |
| 324 | |
| 325 | de = &d->dentry[0]; |
| 326 | de->name_len = cpu_to_le16(1); |
| 327 | de->hash_code = 0; |
| 328 | de->ino = cpu_to_le32(inode->i_ino); |
| 329 | memcpy(d->filename[0], ".", 1); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 330 | set_de_type(de, inode->i_mode); |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 331 | |
| 332 | de = &d->dentry[1]; |
| 333 | de->hash_code = 0; |
| 334 | de->name_len = cpu_to_le16(2); |
| 335 | de->ino = cpu_to_le32(parent->i_ino); |
| 336 | memcpy(d->filename[1], "..", 2); |
Jaegeuk Kim | cb58463 | 2015-03-30 15:23:45 -0700 | [diff] [blame] | 337 | set_de_type(de, parent->i_mode); |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 338 | |
| 339 | test_and_set_bit_le(0, (void *)d->bitmap); |
| 340 | test_and_set_bit_le(1, (void *)d->bitmap); |
| 341 | } |
| 342 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 343 | static int make_empty_dir(struct inode *inode, |
| 344 | struct inode *parent, struct page *page) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 345 | { |
| 346 | struct page *dentry_page; |
| 347 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 348 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 349 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 350 | if (f2fs_has_inline_dentry(inode)) |
| 351 | return make_empty_inline_dir(inode, parent, page); |
| 352 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 353 | dentry_page = get_new_data_page(inode, page, 0, true); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 354 | if (IS_ERR(dentry_page)) |
| 355 | return PTR_ERR(dentry_page); |
| 356 | |
Gu Zheng | 1c3bb97 | 2014-06-27 17:57:04 +0800 | [diff] [blame] | 357 | dentry_blk = kmap_atomic(dentry_page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 358 | |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 359 | make_dentry_ptr(&d, (void *)dentry_blk, 1); |
| 360 | do_make_empty_dir(inode, parent, &d); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 361 | |
Gu Zheng | 1c3bb97 | 2014-06-27 17:57:04 +0800 | [diff] [blame] | 362 | kunmap_atomic(dentry_blk); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 363 | |
| 364 | set_page_dirty(dentry_page); |
| 365 | f2fs_put_page(dentry_page, 1); |
| 366 | return 0; |
| 367 | } |
| 368 | |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 369 | struct page *init_inode_metadata(struct inode *inode, struct inode *dir, |
| 370 | const struct qstr *name, struct page *dpage) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 371 | { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 372 | struct page *page; |
| 373 | int err; |
| 374 | |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 375 | if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { |
Jaegeuk Kim | a014e03 | 2014-06-20 21:44:02 -0700 | [diff] [blame] | 376 | page = new_inode_page(inode); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 377 | if (IS_ERR(page)) |
| 378 | return page; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 379 | |
| 380 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 381 | err = make_empty_dir(inode, dir, page); |
| 382 | if (err) |
| 383 | goto error; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 384 | } |
| 385 | |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 386 | err = f2fs_init_acl(inode, dir, page, dpage); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 387 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 388 | goto put_error; |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 389 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 390 | err = f2fs_init_security(inode, dir, name, page); |
| 391 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 392 | goto put_error; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 393 | } else { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 394 | page = get_node_page(F2FS_I_SB(dir), inode->i_ino); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 395 | if (IS_ERR(page)) |
| 396 | return page; |
| 397 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 398 | set_cold_node(inode, page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 399 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 400 | |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 401 | if (name) |
| 402 | init_dent_inode(name, page); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 403 | |
Jaegeuk Kim | 83d5d6f | 2013-05-28 12:25:47 +0900 | [diff] [blame] | 404 | /* |
| 405 | * This file should be checkpointed during fsync. |
| 406 | * We lost i_pino from now on. |
| 407 | */ |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 408 | if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 409 | file_lost_pino(inode); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 410 | /* |
| 411 | * If link the tmpfile to alias through linkat path, |
| 412 | * we should remove this inode from orphan list. |
| 413 | */ |
| 414 | if (inode->i_nlink == 0) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 415 | remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 416 | inc_nlink(inode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 417 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 418 | return page; |
| 419 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 420 | put_error: |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 421 | f2fs_put_page(page, 1); |
Chao Yu | 90d7245 | 2014-06-24 10:34:00 +0800 | [diff] [blame] | 422 | error: |
Jaegeuk Kim | bd859c6 | 2014-02-05 11:16:39 +0900 | [diff] [blame] | 423 | /* once the failed inode becomes a bad inode, i_mode is S_IFREG */ |
| 424 | truncate_inode_pages(&inode->i_data, 0); |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 425 | truncate_blocks(inode, 0, false); |
Jaegeuk Kim | bd859c6 | 2014-02-05 11:16:39 +0900 | [diff] [blame] | 426 | remove_dirty_dir_inode(inode); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 427 | remove_inode_page(inode); |
| 428 | return ERR_PTR(err); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 429 | } |
| 430 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 431 | void update_parent_metadata(struct inode *dir, struct inode *inode, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 432 | unsigned int current_depth) |
| 433 | { |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 434 | if (inode && is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 435 | if (S_ISDIR(inode->i_mode)) { |
| 436 | inc_nlink(dir); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 437 | set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 438 | } |
| 439 | clear_inode_flag(F2FS_I(inode), FI_NEW_INODE); |
| 440 | } |
| 441 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 442 | mark_inode_dirty(dir); |
| 443 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 444 | if (F2FS_I(dir)->i_current_depth != current_depth) { |
| 445 | F2FS_I(dir)->i_current_depth = current_depth; |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 446 | set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 447 | } |
| 448 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 449 | if (inode && is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 450 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 451 | } |
| 452 | |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 453 | int room_for_filename(const void *bitmap, int slots, int max_slots) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 454 | { |
| 455 | int bit_start = 0; |
| 456 | int zero_start, zero_end; |
| 457 | next: |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 458 | zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start); |
| 459 | if (zero_start >= max_slots) |
| 460 | return max_slots; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 461 | |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 462 | zero_end = find_next_bit_le(bitmap, max_slots, zero_start); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 463 | if (zero_end - zero_start >= slots) |
| 464 | return zero_start; |
| 465 | |
| 466 | bit_start = zero_end + 1; |
| 467 | |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 468 | if (zero_end + 1 >= max_slots) |
| 469 | return max_slots; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 470 | goto next; |
| 471 | } |
| 472 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 473 | void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d, |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 474 | const struct qstr *name, f2fs_hash_t name_hash, |
| 475 | unsigned int bit_pos) |
| 476 | { |
| 477 | struct f2fs_dir_entry *de; |
| 478 | int slots = GET_DENTRY_SLOTS(name->len); |
| 479 | int i; |
| 480 | |
| 481 | de = &d->dentry[bit_pos]; |
| 482 | de->hash_code = name_hash; |
| 483 | de->name_len = cpu_to_le16(name->len); |
| 484 | memcpy(d->filename[bit_pos], name->name, name->len); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 485 | de->ino = cpu_to_le32(ino); |
| 486 | set_de_type(de, mode); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 487 | for (i = 0; i < slots; i++) |
| 488 | test_and_set_bit_le(bit_pos + i, (void *)d->bitmap); |
| 489 | } |
| 490 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 491 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 492 | * Caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 493 | * f2fs_unlock_op(). |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 494 | */ |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 495 | int __f2fs_add_link(struct inode *dir, const struct qstr *name, |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 496 | struct inode *inode, nid_t ino, umode_t mode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 497 | { |
| 498 | unsigned int bit_pos; |
| 499 | unsigned int level; |
| 500 | unsigned int current_depth; |
| 501 | unsigned long bidx, block; |
| 502 | f2fs_hash_t dentry_hash; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 503 | unsigned int nbucket, nblock; |
Al Viro | b7f7a5e | 2013-01-25 16:15:43 -0500 | [diff] [blame] | 504 | size_t namelen = name->len; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 505 | struct page *dentry_page = NULL; |
| 506 | struct f2fs_dentry_block *dentry_blk = NULL; |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 507 | struct f2fs_dentry_ptr d; |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 508 | int slots = GET_DENTRY_SLOTS(namelen); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 509 | struct page *page = NULL; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 510 | int err = 0; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 511 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 512 | if (f2fs_has_inline_dentry(dir)) { |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 513 | err = f2fs_add_inline_entry(dir, name, inode, ino, mode); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 514 | if (!err || err != -EAGAIN) |
| 515 | return err; |
| 516 | else |
| 517 | err = 0; |
| 518 | } |
| 519 | |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 520 | dentry_hash = f2fs_dentry_hash(name); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 521 | level = 0; |
| 522 | current_depth = F2FS_I(dir)->i_current_depth; |
| 523 | if (F2FS_I(dir)->chash == dentry_hash) { |
| 524 | level = F2FS_I(dir)->clevel; |
| 525 | F2FS_I(dir)->chash = 0; |
| 526 | } |
| 527 | |
| 528 | start: |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 529 | if (unlikely(current_depth == MAX_DIR_HASH_DEPTH)) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 530 | return -ENOSPC; |
| 531 | |
| 532 | /* Increase the depth, if required */ |
| 533 | if (level == current_depth) |
| 534 | ++current_depth; |
| 535 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 536 | nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 537 | nblock = bucket_blocks(level); |
| 538 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 539 | bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level, |
| 540 | (le32_to_cpu(dentry_hash) % nbucket)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 541 | |
| 542 | for (block = bidx; block <= (bidx + nblock - 1); block++) { |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 543 | dentry_page = get_new_data_page(dir, NULL, block, true); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 544 | if (IS_ERR(dentry_page)) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 545 | return PTR_ERR(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 546 | |
| 547 | dentry_blk = kmap(dentry_page); |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 548 | bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, |
| 549 | slots, NR_DENTRY_IN_BLOCK); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 550 | if (bit_pos < NR_DENTRY_IN_BLOCK) |
| 551 | goto add_dentry; |
| 552 | |
| 553 | kunmap(dentry_page); |
| 554 | f2fs_put_page(dentry_page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* Move to next level to find the empty slot for new dentry */ |
| 558 | ++level; |
| 559 | goto start; |
| 560 | add_dentry: |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 561 | f2fs_wait_on_page_writeback(dentry_page, DATA); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 562 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 563 | if (inode) { |
| 564 | down_write(&F2FS_I(inode)->i_sem); |
| 565 | page = init_inode_metadata(inode, dir, name, NULL); |
| 566 | if (IS_ERR(page)) { |
| 567 | err = PTR_ERR(page); |
| 568 | goto fail; |
| 569 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 570 | } |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 571 | |
| 572 | make_dentry_ptr(&d, (void *)dentry_blk, 1); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 573 | f2fs_update_dentry(ino, mode, &d, name, dentry_hash, bit_pos); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 574 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 575 | set_page_dirty(dentry_page); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 576 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 577 | if (inode) { |
| 578 | /* we don't need to mark_inode_dirty now */ |
| 579 | F2FS_I(inode)->i_pino = dir->i_ino; |
| 580 | update_inode(inode, page); |
| 581 | f2fs_put_page(page, 1); |
| 582 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 583 | |
| 584 | update_parent_metadata(dir, inode, current_depth); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 585 | fail: |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 586 | if (inode) |
| 587 | up_write(&F2FS_I(inode)->i_sem); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 588 | |
Jaegeuk Kim | 924a2dd | 2014-02-03 17:24:51 +0900 | [diff] [blame] | 589 | if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { |
| 590 | update_inode_page(dir); |
| 591 | clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); |
| 592 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 593 | kunmap(dentry_page); |
| 594 | f2fs_put_page(dentry_page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 595 | return err; |
| 596 | } |
| 597 | |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 598 | int f2fs_do_tmpfile(struct inode *inode, struct inode *dir) |
| 599 | { |
| 600 | struct page *page; |
| 601 | int err = 0; |
| 602 | |
| 603 | down_write(&F2FS_I(inode)->i_sem); |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 604 | page = init_inode_metadata(inode, dir, NULL, NULL); |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 605 | if (IS_ERR(page)) { |
| 606 | err = PTR_ERR(page); |
| 607 | goto fail; |
| 608 | } |
| 609 | /* we don't need to mark_inode_dirty now */ |
| 610 | update_inode(inode, page); |
| 611 | f2fs_put_page(page, 1); |
| 612 | |
| 613 | clear_inode_flag(F2FS_I(inode), FI_NEW_INODE); |
| 614 | fail: |
| 615 | up_write(&F2FS_I(inode)->i_sem); |
| 616 | return err; |
| 617 | } |
| 618 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 619 | void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page) |
| 620 | { |
| 621 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 622 | |
| 623 | down_write(&F2FS_I(inode)->i_sem); |
| 624 | |
| 625 | if (S_ISDIR(inode->i_mode)) { |
| 626 | drop_nlink(dir); |
| 627 | if (page) |
| 628 | update_inode(dir, page); |
| 629 | else |
| 630 | update_inode_page(dir); |
| 631 | } |
| 632 | inode->i_ctime = CURRENT_TIME; |
| 633 | |
| 634 | drop_nlink(inode); |
| 635 | if (S_ISDIR(inode->i_mode)) { |
| 636 | drop_nlink(inode); |
| 637 | i_size_write(inode, 0); |
| 638 | } |
| 639 | up_write(&F2FS_I(inode)->i_sem); |
| 640 | update_inode_page(inode); |
| 641 | |
| 642 | if (inode->i_nlink == 0) |
| 643 | add_orphan_inode(sbi, inode->i_ino); |
| 644 | else |
| 645 | release_orphan_inode(sbi); |
| 646 | } |
| 647 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 648 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 649 | * It only removes the dentry from the dentry page, corresponding name |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 650 | * entry in name page does not need to be touched during deletion. |
| 651 | */ |
| 652 | void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page, |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 653 | struct inode *dir, struct inode *inode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 654 | { |
| 655 | struct f2fs_dentry_block *dentry_blk; |
| 656 | unsigned int bit_pos; |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 657 | int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 658 | int i; |
| 659 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 660 | if (f2fs_has_inline_dentry(dir)) |
| 661 | return f2fs_delete_inline_entry(dentry, page, dir, inode); |
| 662 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 663 | lock_page(page); |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 664 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 665 | |
Gu Zheng | 1c3bb97 | 2014-06-27 17:57:04 +0800 | [diff] [blame] | 666 | dentry_blk = page_address(page); |
| 667 | bit_pos = dentry - dentry_blk->dentry; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 668 | for (i = 0; i < slots; i++) |
Jan Kara | 1f7732f | 2014-10-23 14:40:20 +0200 | [diff] [blame] | 669 | clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 670 | |
| 671 | /* Let's check and deallocate this dentry page */ |
| 672 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 673 | NR_DENTRY_IN_BLOCK, |
| 674 | 0); |
| 675 | kunmap(page); /* kunmap - pair of f2fs_find_entry */ |
| 676 | set_page_dirty(page); |
| 677 | |
| 678 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 679 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 680 | if (inode) |
| 681 | f2fs_drop_nlink(dir, inode, NULL); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 682 | |
| 683 | if (bit_pos == NR_DENTRY_IN_BLOCK) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 684 | truncate_hole(dir, page->index, page->index + 1); |
| 685 | clear_page_dirty_for_io(page); |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 686 | ClearPagePrivate(page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 687 | ClearPageUptodate(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 688 | inode_dec_dirty_pages(dir); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 689 | } |
Namjae Jeon | 508198b | 2012-12-08 14:54:35 +0900 | [diff] [blame] | 690 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | bool f2fs_empty_dir(struct inode *dir) |
| 694 | { |
| 695 | unsigned long bidx; |
| 696 | struct page *dentry_page; |
| 697 | unsigned int bit_pos; |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 698 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 699 | unsigned long nblock = dir_blocks(dir); |
| 700 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 701 | if (f2fs_has_inline_dentry(dir)) |
| 702 | return f2fs_empty_inline_dir(dir); |
| 703 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 704 | for (bidx = 0; bidx < nblock; bidx++) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 705 | dentry_page = get_lock_data_page(dir, bidx); |
| 706 | if (IS_ERR(dentry_page)) { |
| 707 | if (PTR_ERR(dentry_page) == -ENOENT) |
| 708 | continue; |
| 709 | else |
| 710 | return false; |
| 711 | } |
| 712 | |
Gu Zheng | 1c3bb97 | 2014-06-27 17:57:04 +0800 | [diff] [blame] | 713 | dentry_blk = kmap_atomic(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 714 | if (bidx == 0) |
| 715 | bit_pos = 2; |
| 716 | else |
| 717 | bit_pos = 0; |
| 718 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 719 | NR_DENTRY_IN_BLOCK, |
| 720 | bit_pos); |
Gu Zheng | 1c3bb97 | 2014-06-27 17:57:04 +0800 | [diff] [blame] | 721 | kunmap_atomic(dentry_blk); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 722 | |
| 723 | f2fs_put_page(dentry_page, 1); |
| 724 | |
| 725 | if (bit_pos < NR_DENTRY_IN_BLOCK) |
| 726 | return false; |
| 727 | } |
| 728 | return true; |
| 729 | } |
| 730 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 731 | bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, |
| 732 | unsigned int start_pos) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 733 | { |
| 734 | unsigned char d_type = DT_UNKNOWN; |
| 735 | unsigned int bit_pos; |
| 736 | struct f2fs_dir_entry *de = NULL; |
| 737 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 738 | bit_pos = ((unsigned long)ctx->pos % d->max); |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 739 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 740 | while (bit_pos < d->max) { |
| 741 | bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos); |
| 742 | if (bit_pos >= d->max) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 743 | break; |
| 744 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 745 | de = &d->dentry[bit_pos]; |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 746 | if (de->file_type < F2FS_FT_MAX) |
| 747 | d_type = f2fs_filetype_table[de->file_type]; |
| 748 | else |
| 749 | d_type = DT_UNKNOWN; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 750 | if (!dir_emit(ctx, d->filename[bit_pos], |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 751 | le16_to_cpu(de->name_len), |
| 752 | le32_to_cpu(de->ino), d_type)) |
| 753 | return true; |
| 754 | |
| 755 | bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
| 756 | ctx->pos = start_pos + bit_pos; |
| 757 | } |
| 758 | return false; |
| 759 | } |
| 760 | |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 761 | static int f2fs_readdir(struct file *file, struct dir_context *ctx) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 762 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 763 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 764 | unsigned long npages = dir_blocks(inode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 765 | struct f2fs_dentry_block *dentry_blk = NULL; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 766 | struct page *dentry_page = NULL; |
Chao Yu | 817202d9 | 2014-04-28 17:59:43 +0800 | [diff] [blame] | 767 | struct file_ra_state *ra = &file->f_ra; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 768 | unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK); |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 769 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 770 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 771 | if (f2fs_has_inline_dentry(inode)) |
| 772 | return f2fs_read_inline_dir(file, ctx); |
| 773 | |
Chao Yu | 817202d9 | 2014-04-28 17:59:43 +0800 | [diff] [blame] | 774 | /* readahead for multi pages of dir */ |
| 775 | if (npages - n > 1 && !ra_has_index(ra, n)) |
| 776 | page_cache_sync_readahead(inode->i_mapping, ra, file, n, |
| 777 | min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES)); |
| 778 | |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 779 | for (; n < npages; n++) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 780 | dentry_page = get_lock_data_page(inode, n); |
| 781 | if (IS_ERR(dentry_page)) |
| 782 | continue; |
| 783 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 784 | dentry_blk = kmap(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 785 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 786 | make_dentry_ptr(&d, (void *)dentry_blk, 1); |
| 787 | |
| 788 | if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK)) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 789 | goto stop; |
Jaegeuk Kim | 99b072bb | 2013-07-05 17:28:12 +0900 | [diff] [blame] | 790 | |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 791 | ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 792 | kunmap(dentry_page); |
| 793 | f2fs_put_page(dentry_page, 1); |
| 794 | dentry_page = NULL; |
| 795 | } |
Jaegeuk Kim | 99b072bb | 2013-07-05 17:28:12 +0900 | [diff] [blame] | 796 | stop: |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 797 | if (dentry_page && !IS_ERR(dentry_page)) { |
| 798 | kunmap(dentry_page); |
| 799 | f2fs_put_page(dentry_page, 1); |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | const struct file_operations f2fs_dir_operations = { |
| 806 | .llseek = generic_file_llseek, |
| 807 | .read = generic_read_dir, |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 808 | .iterate = f2fs_readdir, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 809 | .fsync = f2fs_sync_file, |
| 810 | .unlocked_ioctl = f2fs_ioctl, |
| 811 | }; |