blob: d5a2c9ed9aa73d5c118843c91891452e2519af2e [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09002 * 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 Kim398b1ac2012-12-19 15:28:39 +090014#include "node.h"
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090015#include "acl.h"
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090016#include "xattr.h"
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090017
18static 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
24static unsigned int dir_buckets(unsigned int level)
25{
26 if (level < MAX_DIR_HASH_DEPTH / 2)
27 return 1 << level;
28 else
29 return 1 << ((MAX_DIR_HASH_DEPTH / 2) - 1);
30}
31
32static 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
40static 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
52static 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
62static void set_de_type(struct f2fs_dir_entry *de, struct inode *inode)
63{
Al Viro0ecc8332013-03-29 12:23:28 -040064 umode_t mode = inode->i_mode;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090065 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
66}
67
68static unsigned long dir_block_index(unsigned int level, unsigned int idx)
69{
70 unsigned long i;
71 unsigned long bidx = 0;
72
73 for (i = 0; i < level; i++)
74 bidx += dir_buckets(i) * bucket_blocks(i);
75 bidx += idx * bucket_blocks(level);
76 return bidx;
77}
78
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020079static bool early_match_name(const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090080 f2fs_hash_t namehash, struct f2fs_dir_entry *de)
81{
82 if (le16_to_cpu(de->name_len) != namelen)
83 return false;
84
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090085 if (de->hash_code != namehash)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090086 return false;
87
88 return true;
89}
90
91static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020092 const char *name, size_t namelen, int *max_slots,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090093 f2fs_hash_t namehash, struct page **res_page)
94{
95 struct f2fs_dir_entry *de;
96 unsigned long bit_pos, end_pos, next_pos;
97 struct f2fs_dentry_block *dentry_blk = kmap(dentry_page);
98 int slots;
99
100 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
101 NR_DENTRY_IN_BLOCK, 0);
102 while (bit_pos < NR_DENTRY_IN_BLOCK) {
103 de = &dentry_blk->dentry[bit_pos];
Namjae Jeon457d08e2012-12-08 14:54:50 +0900104 slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900105
106 if (early_match_name(name, namelen, namehash, de)) {
107 if (!memcmp(dentry_blk->filename[bit_pos],
108 name, namelen)) {
109 *res_page = dentry_page;
110 goto found;
111 }
112 }
113 next_pos = bit_pos + slots;
114 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
115 NR_DENTRY_IN_BLOCK, next_pos);
116 if (bit_pos >= NR_DENTRY_IN_BLOCK)
117 end_pos = NR_DENTRY_IN_BLOCK;
118 else
119 end_pos = bit_pos;
120 if (*max_slots < end_pos - next_pos)
121 *max_slots = end_pos - next_pos;
122 }
123
124 de = NULL;
125 kunmap(dentry_page);
126found:
127 return de;
128}
129
130static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200131 unsigned int level, const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900132 f2fs_hash_t namehash, struct page **res_page)
133{
Namjae Jeon457d08e2012-12-08 14:54:50 +0900134 int s = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900135 unsigned int nbucket, nblock;
136 unsigned int bidx, end_block;
137 struct page *dentry_page;
138 struct f2fs_dir_entry *de = NULL;
139 bool room = false;
140 int max_slots = 0;
141
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900142 f2fs_bug_on(level > MAX_DIR_HASH_DEPTH);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900143
144 nbucket = dir_buckets(level);
145 nblock = bucket_blocks(level);
146
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900147 bidx = dir_block_index(level, le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900148 end_block = bidx + nblock;
149
150 for (; bidx < end_block; bidx++) {
151 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900152 dentry_page = find_data_page(dir, bidx, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900153 if (IS_ERR(dentry_page)) {
154 room = true;
155 continue;
156 }
157
158 de = find_in_block(dentry_page, name, namelen,
159 &max_slots, namehash, res_page);
160 if (de)
161 break;
162
163 if (max_slots >= s)
164 room = true;
165 f2fs_put_page(dentry_page, 0);
166 }
167
168 if (!de && room && F2FS_I(dir)->chash != namehash) {
169 F2FS_I(dir)->chash = namehash;
170 F2FS_I(dir)->clevel = level;
171 }
172
173 return de;
174}
175
176/*
177 * Find an entry in the specified directory with the wanted name.
178 * It returns the page where the entry was found (as a parameter - res_page),
179 * and the entry itself. Page is returned mapped and unlocked.
180 * Entry is guaranteed to be valid.
181 */
182struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
183 struct qstr *child, struct page **res_page)
184{
185 const char *name = child->name;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200186 size_t namelen = child->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900187 unsigned long npages = dir_blocks(dir);
188 struct f2fs_dir_entry *de = NULL;
189 f2fs_hash_t name_hash;
190 unsigned int max_depth;
191 unsigned int level;
192
193 if (npages == 0)
194 return NULL;
195
196 *res_page = NULL;
197
198 name_hash = f2fs_dentry_hash(name, namelen);
199 max_depth = F2FS_I(dir)->i_current_depth;
200
201 for (level = 0; level < max_depth; level++) {
202 de = find_in_level(dir, level, name,
203 namelen, name_hash, res_page);
204 if (de)
205 break;
206 }
207 if (!de && F2FS_I(dir)->chash != name_hash) {
208 F2FS_I(dir)->chash = name_hash;
209 F2FS_I(dir)->clevel = level - 1;
210 }
211 return de;
212}
213
214struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
215{
Namjae Jeon4777f862013-05-23 22:58:07 +0900216 struct page *page;
217 struct f2fs_dir_entry *de;
218 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900219
220 page = get_lock_data_page(dir, 0);
221 if (IS_ERR(page))
222 return NULL;
223
224 dentry_blk = kmap(page);
225 de = &dentry_blk->dentry[1];
226 *p = page;
227 unlock_page(page);
228 return de;
229}
230
231ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
232{
233 ino_t res = 0;
234 struct f2fs_dir_entry *de;
235 struct page *page;
236
237 de = f2fs_find_entry(dir, qstr, &page);
238 if (de) {
239 res = le32_to_cpu(de->ino);
240 kunmap(page);
241 f2fs_put_page(page, 0);
242 }
243
244 return res;
245}
246
247void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
248 struct page *page, struct inode *inode)
249{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900250 lock_page(page);
251 wait_on_page_writeback(page);
252 de->ino = cpu_to_le32(inode->i_ino);
253 set_de_type(de, inode);
254 kunmap(page);
255 set_page_dirty(page);
256 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
257 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900258
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900259 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900260}
261
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900262static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900263{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900264 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900265
Al Viro53dc9a62013-01-25 16:01:21 -0500266 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900267 ri = F2FS_INODE(ipage);
268 ri->i_namelen = cpu_to_le32(name->len);
269 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900270 set_page_dirty(ipage);
271}
272
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900273int update_dent_inode(struct inode *inode, const struct qstr *name)
274{
275 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
276 struct page *page;
277
278 page = get_node_page(sbi, inode->i_ino);
279 if (IS_ERR(page))
280 return PTR_ERR(page);
281
282 init_dent_inode(name, page);
283 f2fs_put_page(page, 1);
284
285 return 0;
286}
287
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900288static int make_empty_dir(struct inode *inode,
289 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900290{
291 struct page *dentry_page;
292 struct f2fs_dentry_block *dentry_blk;
293 struct f2fs_dir_entry *de;
294 void *kaddr;
295
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900296 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900297 if (IS_ERR(dentry_page))
298 return PTR_ERR(dentry_page);
299
300 kaddr = kmap_atomic(dentry_page);
301 dentry_blk = (struct f2fs_dentry_block *)kaddr;
302
303 de = &dentry_blk->dentry[0];
304 de->name_len = cpu_to_le16(1);
305 de->hash_code = 0;
306 de->ino = cpu_to_le32(inode->i_ino);
307 memcpy(dentry_blk->filename[0], ".", 1);
308 set_de_type(de, inode);
309
310 de = &dentry_blk->dentry[1];
311 de->hash_code = 0;
312 de->name_len = cpu_to_le16(2);
313 de->ino = cpu_to_le32(parent->i_ino);
314 memcpy(dentry_blk->filename[1], "..", 2);
315 set_de_type(de, inode);
316
317 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
318 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
319 kunmap_atomic(kaddr);
320
321 set_page_dirty(dentry_page);
322 f2fs_put_page(dentry_page, 1);
323 return 0;
324}
325
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900326static struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500327 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900328{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900329 struct page *page;
330 int err;
331
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900332 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900333 page = new_inode_page(inode, name);
334 if (IS_ERR(page))
335 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900336
337 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900338 err = make_empty_dir(inode, dir, page);
339 if (err)
340 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900341 }
342
Jaegeuk Kim2ed2d5b2013-10-28 13:17:54 +0900343 err = f2fs_init_acl(inode, dir, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900344 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900345 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900346
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900347 err = f2fs_init_security(inode, dir, name, page);
348 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900349 goto put_error;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900350
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900351 wait_on_page_writeback(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900352 } else {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900353 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
354 if (IS_ERR(page))
355 return page;
356
357 wait_on_page_writeback(page);
358 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900359 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900360
361 init_dent_inode(name, page);
362
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900363 /*
364 * This file should be checkpointed during fsync.
365 * We lost i_pino from now on.
366 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900367 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900368 file_lost_pino(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900369 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900370 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900371 return page;
372
Jaegeuk Kima8865372013-12-27 17:04:17 +0900373put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900374 f2fs_put_page(page, 1);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900375 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
376 truncate_inode_pages(&inode->i_data, 0);
377 truncate_blocks(inode, 0);
378 remove_dirty_dir_inode(inode);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900379error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900380 remove_inode_page(inode);
381 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900382}
383
384static void update_parent_metadata(struct inode *dir, struct inode *inode,
385 unsigned int current_depth)
386{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900387 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
388 if (S_ISDIR(inode->i_mode)) {
389 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900390 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900391 }
392 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
393 }
394 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900395 mark_inode_dirty(dir);
396
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900397 if (F2FS_I(dir)->i_current_depth != current_depth) {
398 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900399 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900400 }
401
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900402 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
403 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
404}
405
406static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
407{
408 int bit_start = 0;
409 int zero_start, zero_end;
410next:
411 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
412 NR_DENTRY_IN_BLOCK,
413 bit_start);
414 if (zero_start >= NR_DENTRY_IN_BLOCK)
415 return NR_DENTRY_IN_BLOCK;
416
417 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
418 NR_DENTRY_IN_BLOCK,
419 zero_start);
420 if (zero_end - zero_start >= slots)
421 return zero_start;
422
423 bit_start = zero_end + 1;
424
425 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
426 return NR_DENTRY_IN_BLOCK;
427 goto next;
428}
429
Jaegeuk Kim39936832012-11-22 16:21:29 +0900430/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800431 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
432 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900433 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600434int __f2fs_add_link(struct inode *dir, const struct qstr *name,
435 struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900436{
437 unsigned int bit_pos;
438 unsigned int level;
439 unsigned int current_depth;
440 unsigned long bidx, block;
441 f2fs_hash_t dentry_hash;
442 struct f2fs_dir_entry *de;
443 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500444 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900445 struct page *dentry_page = NULL;
446 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900447 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900448 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900449 int err = 0;
450 int i;
451
Al Virob7f7a5e2013-01-25 16:15:43 -0500452 dentry_hash = f2fs_dentry_hash(name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900453 level = 0;
454 current_depth = F2FS_I(dir)->i_current_depth;
455 if (F2FS_I(dir)->chash == dentry_hash) {
456 level = F2FS_I(dir)->clevel;
457 F2FS_I(dir)->chash = 0;
458 }
459
460start:
Chao Yucfb271d2013-12-05 17:15:22 +0800461 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900462 return -ENOSPC;
463
464 /* Increase the depth, if required */
465 if (level == current_depth)
466 ++current_depth;
467
468 nbucket = dir_buckets(level);
469 nblock = bucket_blocks(level);
470
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900471 bidx = dir_block_index(level, (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900472
473 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900474 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900475 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900476 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900477
478 dentry_blk = kmap(dentry_page);
479 bit_pos = room_for_filename(dentry_blk, slots);
480 if (bit_pos < NR_DENTRY_IN_BLOCK)
481 goto add_dentry;
482
483 kunmap(dentry_page);
484 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900485 }
486
487 /* Move to next level to find the empty slot for new dentry */
488 ++level;
489 goto start;
490add_dentry:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900491 wait_on_page_writeback(dentry_page);
492
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900493 page = init_inode_metadata(inode, dir, name);
494 if (IS_ERR(page)) {
495 err = PTR_ERR(page);
496 goto fail;
497 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900498 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900499 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900500 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500501 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900502 de->ino = cpu_to_le32(inode->i_ino);
503 set_de_type(de, inode);
504 for (i = 0; i < slots; i++)
505 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
506 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900507
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900508 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900509 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900510 update_inode(inode, page);
511 f2fs_put_page(page, 1);
512
513 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900514fail:
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900515 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
516 update_inode_page(dir);
517 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
518 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900519 kunmap(dentry_page);
520 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900521 return err;
522}
523
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900524/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900525 * It only removes the dentry from the dentry page,corresponding name
526 * entry in name page does not need to be touched during deletion.
527 */
528void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
529 struct inode *inode)
530{
531 struct f2fs_dentry_block *dentry_blk;
532 unsigned int bit_pos;
533 struct address_space *mapping = page->mapping;
534 struct inode *dir = mapping->host;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900535 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900536 void *kaddr = page_address(page);
537 int i;
538
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900539 lock_page(page);
540 wait_on_page_writeback(page);
541
542 dentry_blk = (struct f2fs_dentry_block *)kaddr;
543 bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry;
544 for (i = 0; i < slots; i++)
545 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
546
547 /* Let's check and deallocate this dentry page */
548 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
549 NR_DENTRY_IN_BLOCK,
550 0);
551 kunmap(page); /* kunmap - pair of f2fs_find_entry */
552 set_page_dirty(page);
553
554 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
555
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900556 if (inode) {
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +0900557 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
558
Jaegeuk Kime8dae602014-01-21 18:31:38 +0900559 if (S_ISDIR(inode->i_mode)) {
560 drop_nlink(dir);
561 update_inode_page(dir);
562 }
Namjae Jeon16379982013-01-12 14:41:33 +0900563 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900564 drop_nlink(inode);
565 if (S_ISDIR(inode->i_mode)) {
566 drop_nlink(inode);
567 i_size_write(inode, 0);
568 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900569 update_inode_page(inode);
570
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900571 if (inode->i_nlink == 0)
572 add_orphan_inode(sbi, inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900573 else
574 release_orphan_inode(sbi);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900575 }
576
577 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900578 truncate_hole(dir, page->index, page->index + 1);
579 clear_page_dirty_for_io(page);
580 ClearPageUptodate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581 inode_dec_dirty_dents(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900582 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900583 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900584}
585
586bool f2fs_empty_dir(struct inode *dir)
587{
588 unsigned long bidx;
589 struct page *dentry_page;
590 unsigned int bit_pos;
591 struct f2fs_dentry_block *dentry_blk;
592 unsigned long nblock = dir_blocks(dir);
593
594 for (bidx = 0; bidx < nblock; bidx++) {
595 void *kaddr;
596 dentry_page = get_lock_data_page(dir, bidx);
597 if (IS_ERR(dentry_page)) {
598 if (PTR_ERR(dentry_page) == -ENOENT)
599 continue;
600 else
601 return false;
602 }
603
604 kaddr = kmap_atomic(dentry_page);
605 dentry_blk = (struct f2fs_dentry_block *)kaddr;
606 if (bidx == 0)
607 bit_pos = 2;
608 else
609 bit_pos = 0;
610 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
611 NR_DENTRY_IN_BLOCK,
612 bit_pos);
613 kunmap_atomic(kaddr);
614
615 f2fs_put_page(dentry_page, 1);
616
617 if (bit_pos < NR_DENTRY_IN_BLOCK)
618 return false;
619 }
620 return true;
621}
622
Al Viro6f7f2312013-05-17 18:02:17 -0400623static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900624{
Al Viro496ad9a2013-01-23 17:07:38 -0500625 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900626 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900627 unsigned int bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900628 struct f2fs_dentry_block *dentry_blk = NULL;
629 struct f2fs_dir_entry *de = NULL;
630 struct page *dentry_page = NULL;
Al Viro6f7f2312013-05-17 18:02:17 -0400631 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900632 unsigned char d_type = DT_UNKNOWN;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900633
Al Viro6f7f2312013-05-17 18:02:17 -0400634 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900635
Chris Fries6c311ec2014-01-17 14:44:39 -0600636 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900637 dentry_page = get_lock_data_page(inode, n);
638 if (IS_ERR(dentry_page))
639 continue;
640
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900641 dentry_blk = kmap(dentry_page);
642 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900643 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
644 NR_DENTRY_IN_BLOCK,
645 bit_pos);
646 if (bit_pos >= NR_DENTRY_IN_BLOCK)
647 break;
648
649 de = &dentry_blk->dentry[bit_pos];
Al Viro6f7f2312013-05-17 18:02:17 -0400650 if (de->file_type < F2FS_FT_MAX)
651 d_type = f2fs_filetype_table[de->file_type];
652 else
653 d_type = DT_UNKNOWN;
654 if (!dir_emit(ctx,
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900655 dentry_blk->filename[bit_pos],
656 le16_to_cpu(de->name_len),
657 le32_to_cpu(de->ino), d_type))
658 goto stop;
659
660 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
661 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900662 }
663 bit_pos = 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400664 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900665 kunmap(dentry_page);
666 f2fs_put_page(dentry_page, 1);
667 dentry_page = NULL;
668 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900669stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900670 if (dentry_page && !IS_ERR(dentry_page)) {
671 kunmap(dentry_page);
672 f2fs_put_page(dentry_page, 1);
673 }
674
675 return 0;
676}
677
678const struct file_operations f2fs_dir_operations = {
679 .llseek = generic_file_llseek,
680 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400681 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900682 .fsync = f2fs_sync_file,
683 .unlocked_ioctl = f2fs_ioctl,
684};