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