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