blob: fc1dacf55b3af966d02b64786af7568822c5c1b1 [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"
16
17static unsigned long dir_blocks(struct inode *inode)
18{
19 return ((unsigned long long) (i_size_read(inode) + PAGE_CACHE_SIZE - 1))
20 >> PAGE_CACHE_SHIFT;
21}
22
23static unsigned int dir_buckets(unsigned int level)
24{
25 if (level < MAX_DIR_HASH_DEPTH / 2)
26 return 1 << level;
27 else
28 return 1 << ((MAX_DIR_HASH_DEPTH / 2) - 1);
29}
30
31static unsigned int bucket_blocks(unsigned int level)
32{
33 if (level < MAX_DIR_HASH_DEPTH / 2)
34 return 2;
35 else
36 return 4;
37}
38
39static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
40 [F2FS_FT_UNKNOWN] = DT_UNKNOWN,
41 [F2FS_FT_REG_FILE] = DT_REG,
42 [F2FS_FT_DIR] = DT_DIR,
43 [F2FS_FT_CHRDEV] = DT_CHR,
44 [F2FS_FT_BLKDEV] = DT_BLK,
45 [F2FS_FT_FIFO] = DT_FIFO,
46 [F2FS_FT_SOCK] = DT_SOCK,
47 [F2FS_FT_SYMLINK] = DT_LNK,
48};
49
50#define S_SHIFT 12
51static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
52 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
53 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
54 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
55 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
56 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
57 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
58 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
59};
60
61static void set_de_type(struct f2fs_dir_entry *de, struct inode *inode)
62{
Al Viro0ecc8332013-03-29 12:23:28 -040063 umode_t mode = inode->i_mode;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090064 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
65}
66
67static unsigned long dir_block_index(unsigned int level, unsigned int idx)
68{
69 unsigned long i;
70 unsigned long bidx = 0;
71
72 for (i = 0; i < level; i++)
73 bidx += dir_buckets(i) * bucket_blocks(i);
74 bidx += idx * bucket_blocks(level);
75 return bidx;
76}
77
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020078static bool early_match_name(const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090079 f2fs_hash_t namehash, struct f2fs_dir_entry *de)
80{
81 if (le16_to_cpu(de->name_len) != namelen)
82 return false;
83
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090084 if (de->hash_code != namehash)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090085 return false;
86
87 return true;
88}
89
90static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020091 const char *name, size_t namelen, int *max_slots,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090092 f2fs_hash_t namehash, struct page **res_page)
93{
94 struct f2fs_dir_entry *de;
95 unsigned long bit_pos, end_pos, next_pos;
96 struct f2fs_dentry_block *dentry_blk = kmap(dentry_page);
97 int slots;
98
99 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
100 NR_DENTRY_IN_BLOCK, 0);
101 while (bit_pos < NR_DENTRY_IN_BLOCK) {
102 de = &dentry_blk->dentry[bit_pos];
Namjae Jeon457d08e2012-12-08 14:54:50 +0900103 slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900104
105 if (early_match_name(name, namelen, namehash, de)) {
106 if (!memcmp(dentry_blk->filename[bit_pos],
107 name, namelen)) {
108 *res_page = dentry_page;
109 goto found;
110 }
111 }
112 next_pos = bit_pos + slots;
113 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
114 NR_DENTRY_IN_BLOCK, next_pos);
115 if (bit_pos >= NR_DENTRY_IN_BLOCK)
116 end_pos = NR_DENTRY_IN_BLOCK;
117 else
118 end_pos = bit_pos;
119 if (*max_slots < end_pos - next_pos)
120 *max_slots = end_pos - next_pos;
121 }
122
123 de = NULL;
124 kunmap(dentry_page);
125found:
126 return de;
127}
128
129static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200130 unsigned int level, const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900131 f2fs_hash_t namehash, struct page **res_page)
132{
Namjae Jeon457d08e2012-12-08 14:54:50 +0900133 int s = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900134 unsigned int nbucket, nblock;
135 unsigned int bidx, end_block;
136 struct page *dentry_page;
137 struct f2fs_dir_entry *de = NULL;
138 bool room = false;
139 int max_slots = 0;
140
141 BUG_ON(level > MAX_DIR_HASH_DEPTH);
142
143 nbucket = dir_buckets(level);
144 nblock = bucket_blocks(level);
145
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900146 bidx = dir_block_index(level, le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900147 end_block = bidx + nblock;
148
149 for (; bidx < end_block; bidx++) {
150 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900151 dentry_page = find_data_page(dir, bidx, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900152 if (IS_ERR(dentry_page)) {
153 room = true;
154 continue;
155 }
156
157 de = find_in_block(dentry_page, name, namelen,
158 &max_slots, namehash, res_page);
159 if (de)
160 break;
161
162 if (max_slots >= s)
163 room = true;
164 f2fs_put_page(dentry_page, 0);
165 }
166
167 if (!de && room && F2FS_I(dir)->chash != namehash) {
168 F2FS_I(dir)->chash = namehash;
169 F2FS_I(dir)->clevel = level;
170 }
171
172 return de;
173}
174
175/*
176 * Find an entry in the specified directory with the wanted name.
177 * It returns the page where the entry was found (as a parameter - res_page),
178 * and the entry itself. Page is returned mapped and unlocked.
179 * Entry is guaranteed to be valid.
180 */
181struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
182 struct qstr *child, struct page **res_page)
183{
184 const char *name = child->name;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200185 size_t namelen = child->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900186 unsigned long npages = dir_blocks(dir);
187 struct f2fs_dir_entry *de = NULL;
188 f2fs_hash_t name_hash;
189 unsigned int max_depth;
190 unsigned int level;
191
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900192 if (namelen > F2FS_NAME_LEN)
193 return NULL;
194
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900195 if (npages == 0)
196 return NULL;
197
198 *res_page = NULL;
199
200 name_hash = f2fs_dentry_hash(name, namelen);
201 max_depth = F2FS_I(dir)->i_current_depth;
202
203 for (level = 0; level < max_depth; level++) {
204 de = find_in_level(dir, level, name,
205 namelen, name_hash, res_page);
206 if (de)
207 break;
208 }
209 if (!de && F2FS_I(dir)->chash != name_hash) {
210 F2FS_I(dir)->chash = name_hash;
211 F2FS_I(dir)->clevel = level - 1;
212 }
213 return de;
214}
215
216struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
217{
218 struct page *page = NULL;
219 struct f2fs_dir_entry *de = NULL;
220 struct f2fs_dentry_block *dentry_blk = NULL;
221
222 page = get_lock_data_page(dir, 0);
223 if (IS_ERR(page))
224 return NULL;
225
226 dentry_blk = kmap(page);
227 de = &dentry_blk->dentry[1];
228 *p = page;
229 unlock_page(page);
230 return de;
231}
232
233ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
234{
235 ino_t res = 0;
236 struct f2fs_dir_entry *de;
237 struct page *page;
238
239 de = f2fs_find_entry(dir, qstr, &page);
240 if (de) {
241 res = le32_to_cpu(de->ino);
242 kunmap(page);
243 f2fs_put_page(page, 0);
244 }
245
246 return res;
247}
248
249void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
250 struct page *page, struct inode *inode)
251{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900252 lock_page(page);
253 wait_on_page_writeback(page);
254 de->ino = cpu_to_le32(inode->i_ino);
255 set_de_type(de, inode);
256 kunmap(page);
257 set_page_dirty(page);
258 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
259 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900260
261 /* update parent inode number before releasing dentry page */
262 F2FS_I(inode)->i_pino = dir->i_ino;
263
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900264 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900265}
266
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900267static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900268{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900269 struct f2fs_node *rn;
270
Al Viro53dc9a62013-01-25 16:01:21 -0500271 /* copy name info. to this inode page */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900272 rn = (struct f2fs_node *)page_address(ipage);
Al Viro53dc9a62013-01-25 16:01:21 -0500273 rn->i.i_namelen = cpu_to_le32(name->len);
274 memcpy(rn->i.i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900275 set_page_dirty(ipage);
276}
277
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900278static int make_empty_dir(struct inode *inode,
279 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900280{
281 struct page *dentry_page;
282 struct f2fs_dentry_block *dentry_blk;
283 struct f2fs_dir_entry *de;
284 void *kaddr;
285
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900286 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900287 if (IS_ERR(dentry_page))
288 return PTR_ERR(dentry_page);
289
290 kaddr = kmap_atomic(dentry_page);
291 dentry_blk = (struct f2fs_dentry_block *)kaddr;
292
293 de = &dentry_blk->dentry[0];
294 de->name_len = cpu_to_le16(1);
295 de->hash_code = 0;
296 de->ino = cpu_to_le32(inode->i_ino);
297 memcpy(dentry_blk->filename[0], ".", 1);
298 set_de_type(de, inode);
299
300 de = &dentry_blk->dentry[1];
301 de->hash_code = 0;
302 de->name_len = cpu_to_le16(2);
303 de->ino = cpu_to_le32(parent->i_ino);
304 memcpy(dentry_blk->filename[1], "..", 2);
305 set_de_type(de, inode);
306
307 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
308 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
309 kunmap_atomic(kaddr);
310
311 set_page_dirty(dentry_page);
312 f2fs_put_page(dentry_page, 1);
313 return 0;
314}
315
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900316static struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500317 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900318{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900319 struct page *page;
320 int err;
321
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900322 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900323 page = new_inode_page(inode, name);
324 if (IS_ERR(page))
325 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900326
327 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900328 err = make_empty_dir(inode, dir, page);
329 if (err)
330 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900331 }
332
333 err = f2fs_init_acl(inode, dir);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900334 if (err)
335 goto error;
336
337 wait_on_page_writeback(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900338 } else {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900339 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
340 if (IS_ERR(page))
341 return page;
342
343 wait_on_page_writeback(page);
344 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900345 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900346
347 init_dent_inode(name, page);
348
349 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900350 inc_nlink(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900351 return page;
352
353error:
354 f2fs_put_page(page, 1);
355 remove_inode_page(inode);
356 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900357}
358
359static void update_parent_metadata(struct inode *dir, struct inode *inode,
360 unsigned int current_depth)
361{
362 bool need_dir_update = false;
363
364 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
365 if (S_ISDIR(inode->i_mode)) {
366 inc_nlink(dir);
367 need_dir_update = true;
368 }
369 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
370 }
371 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
372 if (F2FS_I(dir)->i_current_depth != current_depth) {
373 F2FS_I(dir)->i_current_depth = current_depth;
374 need_dir_update = true;
375 }
376
377 if (need_dir_update)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900378 update_inode_page(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900379 else
380 mark_inode_dirty(dir);
381
382 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
383 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
384}
385
386static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
387{
388 int bit_start = 0;
389 int zero_start, zero_end;
390next:
391 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
392 NR_DENTRY_IN_BLOCK,
393 bit_start);
394 if (zero_start >= NR_DENTRY_IN_BLOCK)
395 return NR_DENTRY_IN_BLOCK;
396
397 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
398 NR_DENTRY_IN_BLOCK,
399 zero_start);
400 if (zero_end - zero_start >= slots)
401 return zero_start;
402
403 bit_start = zero_end + 1;
404
405 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
406 return NR_DENTRY_IN_BLOCK;
407 goto next;
408}
409
Jaegeuk Kim39936832012-11-22 16:21:29 +0900410/*
411 * Caller should grab and release a mutex by calling mutex_lock_op() and
412 * mutex_unlock_op().
413 */
Al Virob7f7a5e2013-01-25 16:15:43 -0500414int __f2fs_add_link(struct inode *dir, const struct qstr *name, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900415{
416 unsigned int bit_pos;
417 unsigned int level;
418 unsigned int current_depth;
419 unsigned long bidx, block;
420 f2fs_hash_t dentry_hash;
421 struct f2fs_dir_entry *de;
422 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500423 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900424 struct page *dentry_page = NULL;
425 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900426 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900427 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900428 int err = 0;
429 int i;
430
Al Virob7f7a5e2013-01-25 16:15:43 -0500431 dentry_hash = f2fs_dentry_hash(name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900432 level = 0;
433 current_depth = F2FS_I(dir)->i_current_depth;
434 if (F2FS_I(dir)->chash == dentry_hash) {
435 level = F2FS_I(dir)->clevel;
436 F2FS_I(dir)->chash = 0;
437 }
438
439start:
440 if (current_depth == MAX_DIR_HASH_DEPTH)
441 return -ENOSPC;
442
443 /* Increase the depth, if required */
444 if (level == current_depth)
445 ++current_depth;
446
447 nbucket = dir_buckets(level);
448 nblock = bucket_blocks(level);
449
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900450 bidx = dir_block_index(level, (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900451
452 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900453 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900454 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900455 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900456
457 dentry_blk = kmap(dentry_page);
458 bit_pos = room_for_filename(dentry_blk, slots);
459 if (bit_pos < NR_DENTRY_IN_BLOCK)
460 goto add_dentry;
461
462 kunmap(dentry_page);
463 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900464 }
465
466 /* Move to next level to find the empty slot for new dentry */
467 ++level;
468 goto start;
469add_dentry:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900470 wait_on_page_writeback(dentry_page);
471
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900472 page = init_inode_metadata(inode, dir, name);
473 if (IS_ERR(page)) {
474 err = PTR_ERR(page);
475 goto fail;
476 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900477 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900478 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900479 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500480 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900481 de->ino = cpu_to_le32(inode->i_ino);
482 set_de_type(de, inode);
483 for (i = 0; i < slots; i++)
484 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
485 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900486
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900487 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900488 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900489 update_inode(inode, page);
490 f2fs_put_page(page, 1);
491
492 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900493fail:
494 kunmap(dentry_page);
495 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900496 return err;
497}
498
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900499/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900500 * It only removes the dentry from the dentry page,corresponding name
501 * entry in name page does not need to be touched during deletion.
502 */
503void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
504 struct inode *inode)
505{
506 struct f2fs_dentry_block *dentry_blk;
507 unsigned int bit_pos;
508 struct address_space *mapping = page->mapping;
509 struct inode *dir = mapping->host;
510 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Namjae Jeon457d08e2012-12-08 14:54:50 +0900511 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900512 void *kaddr = page_address(page);
513 int i;
514
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900515 lock_page(page);
516 wait_on_page_writeback(page);
517
518 dentry_blk = (struct f2fs_dentry_block *)kaddr;
519 bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry;
520 for (i = 0; i < slots; i++)
521 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
522
523 /* Let's check and deallocate this dentry page */
524 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
525 NR_DENTRY_IN_BLOCK,
526 0);
527 kunmap(page); /* kunmap - pair of f2fs_find_entry */
528 set_page_dirty(page);
529
530 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
531
532 if (inode && S_ISDIR(inode->i_mode)) {
533 drop_nlink(dir);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900534 update_inode_page(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900535 } else {
536 mark_inode_dirty(dir);
537 }
538
539 if (inode) {
Namjae Jeon16379982013-01-12 14:41:33 +0900540 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900541 drop_nlink(inode);
542 if (S_ISDIR(inode->i_mode)) {
543 drop_nlink(inode);
544 i_size_write(inode, 0);
545 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900546 update_inode_page(inode);
547
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900548 if (inode->i_nlink == 0)
549 add_orphan_inode(sbi, inode->i_ino);
550 }
551
552 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900553 truncate_hole(dir, page->index, page->index + 1);
554 clear_page_dirty_for_io(page);
555 ClearPageUptodate(page);
556 dec_page_count(sbi, F2FS_DIRTY_DENTS);
557 inode_dec_dirty_dents(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900558 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900559 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900560}
561
562bool f2fs_empty_dir(struct inode *dir)
563{
564 unsigned long bidx;
565 struct page *dentry_page;
566 unsigned int bit_pos;
567 struct f2fs_dentry_block *dentry_blk;
568 unsigned long nblock = dir_blocks(dir);
569
570 for (bidx = 0; bidx < nblock; bidx++) {
571 void *kaddr;
572 dentry_page = get_lock_data_page(dir, bidx);
573 if (IS_ERR(dentry_page)) {
574 if (PTR_ERR(dentry_page) == -ENOENT)
575 continue;
576 else
577 return false;
578 }
579
580 kaddr = kmap_atomic(dentry_page);
581 dentry_blk = (struct f2fs_dentry_block *)kaddr;
582 if (bidx == 0)
583 bit_pos = 2;
584 else
585 bit_pos = 0;
586 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
587 NR_DENTRY_IN_BLOCK,
588 bit_pos);
589 kunmap_atomic(kaddr);
590
591 f2fs_put_page(dentry_page, 1);
592
593 if (bit_pos < NR_DENTRY_IN_BLOCK)
594 return false;
595 }
596 return true;
597}
598
599static int f2fs_readdir(struct file *file, void *dirent, filldir_t filldir)
600{
601 unsigned long pos = file->f_pos;
Al Viro496ad9a2013-01-23 17:07:38 -0500602 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900603 unsigned long npages = dir_blocks(inode);
604 unsigned char *types = NULL;
605 unsigned int bit_pos = 0, start_bit_pos = 0;
606 int over = 0;
607 struct f2fs_dentry_block *dentry_blk = NULL;
608 struct f2fs_dir_entry *de = NULL;
609 struct page *dentry_page = NULL;
610 unsigned int n = 0;
611 unsigned char d_type = DT_UNKNOWN;
612 int slots;
613
614 types = f2fs_filetype_table;
615 bit_pos = (pos % NR_DENTRY_IN_BLOCK);
616 n = (pos / NR_DENTRY_IN_BLOCK);
617
618 for ( ; n < npages; n++) {
619 dentry_page = get_lock_data_page(inode, n);
620 if (IS_ERR(dentry_page))
621 continue;
622
623 start_bit_pos = bit_pos;
624 dentry_blk = kmap(dentry_page);
625 while (bit_pos < NR_DENTRY_IN_BLOCK) {
626 d_type = DT_UNKNOWN;
627 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
628 NR_DENTRY_IN_BLOCK,
629 bit_pos);
630 if (bit_pos >= NR_DENTRY_IN_BLOCK)
631 break;
632
633 de = &dentry_blk->dentry[bit_pos];
634 if (types && de->file_type < F2FS_FT_MAX)
635 d_type = types[de->file_type];
636
637 over = filldir(dirent,
638 dentry_blk->filename[bit_pos],
639 le16_to_cpu(de->name_len),
640 (n * NR_DENTRY_IN_BLOCK) + bit_pos,
641 le32_to_cpu(de->ino), d_type);
642 if (over) {
643 file->f_pos += bit_pos - start_bit_pos;
644 goto success;
645 }
Namjae Jeon457d08e2012-12-08 14:54:50 +0900646 slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900647 bit_pos += slots;
648 }
649 bit_pos = 0;
650 file->f_pos = (n + 1) * NR_DENTRY_IN_BLOCK;
651 kunmap(dentry_page);
652 f2fs_put_page(dentry_page, 1);
653 dentry_page = NULL;
654 }
655success:
656 if (dentry_page && !IS_ERR(dentry_page)) {
657 kunmap(dentry_page);
658 f2fs_put_page(dentry_page, 1);
659 }
660
661 return 0;
662}
663
664const struct file_operations f2fs_dir_operations = {
665 .llseek = generic_file_llseek,
666 .read = generic_read_dir,
667 .readdir = f2fs_readdir,
668 .fsync = f2fs_sync_file,
669 .unlocked_ioctl = f2fs_ioctl,
670};