blob: f3a80ce9ddf5801b07f20451977e4557506d21be [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
Jaegeuk Kim38431542014-02-27 18:20:00 +090024static unsigned int dir_buckets(unsigned int level, int dir_level)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090025{
26 if (level < MAX_DIR_HASH_DEPTH / 2)
Jaegeuk Kim38431542014-02-27 18:20:00 +090027 return 1 << (level + dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090028 else
Jaegeuk Kim38431542014-02-27 18:20:00 +090029 return 1 << ((MAX_DIR_HASH_DEPTH / 2 + dir_level) - 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090030}
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
Jaegeuk Kim38431542014-02-27 18:20:00 +090068static unsigned long dir_block_index(unsigned int level,
69 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090070{
71 unsigned long i;
72 unsigned long bidx = 0;
73
74 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +090075 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090076 bidx += idx * bucket_blocks(level);
77 return bidx;
78}
79
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020080static bool early_match_name(const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090081 f2fs_hash_t namehash, struct f2fs_dir_entry *de)
82{
83 if (le16_to_cpu(de->name_len) != namelen)
84 return false;
85
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090086 if (de->hash_code != namehash)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090087 return false;
88
89 return true;
90}
91
92static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +020093 const char *name, size_t namelen, int *max_slots,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090094 f2fs_hash_t namehash, struct page **res_page)
95{
96 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +090097 unsigned long bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090098 struct f2fs_dentry_block *dentry_blk = kmap(dentry_page);
Jaegeuk Kim20f70752014-03-05 10:48:25 +090099 const void *dentry_bits = &dentry_blk->dentry_bitmap;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900100 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900101
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900102 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim20f70752014-03-05 10:48:25 +0900103 if (!test_bit_le(bit_pos, dentry_bits)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900104 if (bit_pos == 0)
105 max_len = 1;
Jaegeuk Kim20f70752014-03-05 10:48:25 +0900106 else if (!test_bit_le(bit_pos - 1, dentry_bits))
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900107 max_len++;
108 bit_pos++;
109 continue;
110 }
Jaegeuk Kim20f70752014-03-05 10:48:25 +0900111 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900112 if (early_match_name(name, namelen, namehash, de)) {
113 if (!memcmp(dentry_blk->filename[bit_pos],
114 name, namelen)) {
115 *res_page = dentry_page;
116 goto found;
117 }
118 }
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900119 if (max_len > *max_slots) {
120 *max_slots = max_len;
121 max_len = 0;
122 }
123 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900124 }
125
126 de = NULL;
127 kunmap(dentry_page);
128found:
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900129 if (max_len > *max_slots)
130 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900131 return de;
132}
133
134static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200135 unsigned int level, const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900136 f2fs_hash_t namehash, struct page **res_page)
137{
Namjae Jeon457d08e2012-12-08 14:54:50 +0900138 int s = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900139 unsigned int nbucket, nblock;
140 unsigned int bidx, end_block;
141 struct page *dentry_page;
142 struct f2fs_dir_entry *de = NULL;
143 bool room = false;
144 int max_slots = 0;
145
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900146 f2fs_bug_on(level > MAX_DIR_HASH_DEPTH);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900147
Jaegeuk Kim38431542014-02-27 18:20:00 +0900148 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900149 nblock = bucket_blocks(level);
150
Jaegeuk Kim38431542014-02-27 18:20:00 +0900151 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
152 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900153 end_block = bidx + nblock;
154
155 for (; bidx < end_block; bidx++) {
156 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900157 dentry_page = find_data_page(dir, bidx, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900158 if (IS_ERR(dentry_page)) {
159 room = true;
160 continue;
161 }
162
163 de = find_in_block(dentry_page, name, namelen,
164 &max_slots, namehash, res_page);
165 if (de)
166 break;
167
168 if (max_slots >= s)
169 room = true;
170 f2fs_put_page(dentry_page, 0);
171 }
172
173 if (!de && room && F2FS_I(dir)->chash != namehash) {
174 F2FS_I(dir)->chash = namehash;
175 F2FS_I(dir)->clevel = level;
176 }
177
178 return de;
179}
180
181/*
182 * Find an entry in the specified directory with the wanted name.
183 * It returns the page where the entry was found (as a parameter - res_page),
184 * and the entry itself. Page is returned mapped and unlocked.
185 * Entry is guaranteed to be valid.
186 */
187struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
188 struct qstr *child, struct page **res_page)
189{
190 const char *name = child->name;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200191 size_t namelen = child->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900192 unsigned long npages = dir_blocks(dir);
193 struct f2fs_dir_entry *de = NULL;
194 f2fs_hash_t name_hash;
195 unsigned int max_depth;
196 unsigned int level;
197
198 if (npages == 0)
199 return NULL;
200
201 *res_page = NULL;
202
203 name_hash = f2fs_dentry_hash(name, namelen);
204 max_depth = F2FS_I(dir)->i_current_depth;
205
206 for (level = 0; level < max_depth; level++) {
207 de = find_in_level(dir, level, name,
208 namelen, name_hash, res_page);
209 if (de)
210 break;
211 }
212 if (!de && F2FS_I(dir)->chash != name_hash) {
213 F2FS_I(dir)->chash = name_hash;
214 F2FS_I(dir)->clevel = level - 1;
215 }
216 return de;
217}
218
219struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
220{
Namjae Jeon4777f862013-05-23 22:58:07 +0900221 struct page *page;
222 struct f2fs_dir_entry *de;
223 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900224
225 page = get_lock_data_page(dir, 0);
226 if (IS_ERR(page))
227 return NULL;
228
229 dentry_blk = kmap(page);
230 de = &dentry_blk->dentry[1];
231 *p = page;
232 unlock_page(page);
233 return de;
234}
235
236ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
237{
238 ino_t res = 0;
239 struct f2fs_dir_entry *de;
240 struct page *page;
241
242 de = f2fs_find_entry(dir, qstr, &page);
243 if (de) {
244 res = le32_to_cpu(de->ino);
245 kunmap(page);
246 f2fs_put_page(page, 0);
247 }
248
249 return res;
250}
251
252void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
253 struct page *page, struct inode *inode)
254{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900255 lock_page(page);
256 wait_on_page_writeback(page);
257 de->ino = cpu_to_le32(inode->i_ino);
258 set_de_type(de, inode);
259 kunmap(page);
260 set_page_dirty(page);
261 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
262 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900263
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 Kim58bfaf42013-12-26 16:30:41 +0900269 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900270
Al Viro53dc9a62013-01-25 16:01:21 -0500271 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900272 ri = F2FS_INODE(ipage);
273 ri->i_namelen = cpu_to_le32(name->len);
274 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900275 set_page_dirty(ipage);
276}
277
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900278int update_dent_inode(struct inode *inode, const struct qstr *name)
279{
280 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
281 struct page *page;
282
283 page = get_node_page(sbi, inode->i_ino);
284 if (IS_ERR(page))
285 return PTR_ERR(page);
286
287 init_dent_inode(name, page);
288 f2fs_put_page(page, 1);
289
290 return 0;
291}
292
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900293static int make_empty_dir(struct inode *inode,
294 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900295{
296 struct page *dentry_page;
297 struct f2fs_dentry_block *dentry_blk;
298 struct f2fs_dir_entry *de;
299 void *kaddr;
300
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900301 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900302 if (IS_ERR(dentry_page))
303 return PTR_ERR(dentry_page);
304
305 kaddr = kmap_atomic(dentry_page);
306 dentry_blk = (struct f2fs_dentry_block *)kaddr;
307
308 de = &dentry_blk->dentry[0];
309 de->name_len = cpu_to_le16(1);
310 de->hash_code = 0;
311 de->ino = cpu_to_le32(inode->i_ino);
312 memcpy(dentry_blk->filename[0], ".", 1);
313 set_de_type(de, inode);
314
315 de = &dentry_blk->dentry[1];
316 de->hash_code = 0;
317 de->name_len = cpu_to_le16(2);
318 de->ino = cpu_to_le32(parent->i_ino);
319 memcpy(dentry_blk->filename[1], "..", 2);
320 set_de_type(de, inode);
321
322 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
323 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
324 kunmap_atomic(kaddr);
325
326 set_page_dirty(dentry_page);
327 f2fs_put_page(dentry_page, 1);
328 return 0;
329}
330
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900331static struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500332 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900333{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900334 struct page *page;
335 int err;
336
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900337 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900338 page = new_inode_page(inode, name);
339 if (IS_ERR(page))
340 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900341
342 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900343 err = make_empty_dir(inode, dir, page);
344 if (err)
345 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900346 }
347
Jaegeuk Kim2ed2d5b2013-10-28 13:17:54 +0900348 err = f2fs_init_acl(inode, dir, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900349 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900350 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900351
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900352 err = f2fs_init_security(inode, dir, name, page);
353 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900354 goto put_error;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900355
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900356 wait_on_page_writeback(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900357 } else {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900358 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
359 if (IS_ERR(page))
360 return page;
361
362 wait_on_page_writeback(page);
363 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900364 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900365
366 init_dent_inode(name, page);
367
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900368 /*
369 * This file should be checkpointed during fsync.
370 * We lost i_pino from now on.
371 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900372 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900373 file_lost_pino(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900374 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900375 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900376 return page;
377
Jaegeuk Kima8865372013-12-27 17:04:17 +0900378put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900379 f2fs_put_page(page, 1);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900380 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
381 truncate_inode_pages(&inode->i_data, 0);
382 truncate_blocks(inode, 0);
383 remove_dirty_dir_inode(inode);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900384error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900385 remove_inode_page(inode);
386 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900387}
388
389static void update_parent_metadata(struct inode *dir, struct inode *inode,
390 unsigned int current_depth)
391{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900392 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
393 if (S_ISDIR(inode->i_mode)) {
394 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900395 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900396 }
397 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
398 }
399 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900400 mark_inode_dirty(dir);
401
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900402 if (F2FS_I(dir)->i_current_depth != current_depth) {
403 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900404 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900405 }
406
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900407 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
408 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
409}
410
411static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
412{
413 int bit_start = 0;
414 int zero_start, zero_end;
415next:
416 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
417 NR_DENTRY_IN_BLOCK,
418 bit_start);
419 if (zero_start >= NR_DENTRY_IN_BLOCK)
420 return NR_DENTRY_IN_BLOCK;
421
422 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
423 NR_DENTRY_IN_BLOCK,
424 zero_start);
425 if (zero_end - zero_start >= slots)
426 return zero_start;
427
428 bit_start = zero_end + 1;
429
430 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
431 return NR_DENTRY_IN_BLOCK;
432 goto next;
433}
434
Jaegeuk Kim39936832012-11-22 16:21:29 +0900435/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800436 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
437 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900438 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600439int __f2fs_add_link(struct inode *dir, const struct qstr *name,
440 struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900441{
442 unsigned int bit_pos;
443 unsigned int level;
444 unsigned int current_depth;
445 unsigned long bidx, block;
446 f2fs_hash_t dentry_hash;
447 struct f2fs_dir_entry *de;
448 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500449 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900450 struct page *dentry_page = NULL;
451 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900452 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900453 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900454 int err = 0;
455 int i;
456
Al Virob7f7a5e2013-01-25 16:15:43 -0500457 dentry_hash = f2fs_dentry_hash(name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900458 level = 0;
459 current_depth = F2FS_I(dir)->i_current_depth;
460 if (F2FS_I(dir)->chash == dentry_hash) {
461 level = F2FS_I(dir)->clevel;
462 F2FS_I(dir)->chash = 0;
463 }
464
465start:
Chao Yucfb271d2013-12-05 17:15:22 +0800466 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900467 return -ENOSPC;
468
469 /* Increase the depth, if required */
470 if (level == current_depth)
471 ++current_depth;
472
Jaegeuk Kim38431542014-02-27 18:20:00 +0900473 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900474 nblock = bucket_blocks(level);
475
Jaegeuk Kim38431542014-02-27 18:20:00 +0900476 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
477 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900478
479 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900480 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900481 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900482 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900483
484 dentry_blk = kmap(dentry_page);
485 bit_pos = room_for_filename(dentry_blk, slots);
486 if (bit_pos < NR_DENTRY_IN_BLOCK)
487 goto add_dentry;
488
489 kunmap(dentry_page);
490 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900491 }
492
493 /* Move to next level to find the empty slot for new dentry */
494 ++level;
495 goto start;
496add_dentry:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900497 wait_on_page_writeback(dentry_page);
498
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900499 page = init_inode_metadata(inode, dir, name);
500 if (IS_ERR(page)) {
501 err = PTR_ERR(page);
502 goto fail;
503 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900504 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900505 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900506 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500507 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900508 de->ino = cpu_to_le32(inode->i_ino);
509 set_de_type(de, inode);
510 for (i = 0; i < slots; i++)
511 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
512 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900513
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900514 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900515 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900516 update_inode(inode, page);
517 f2fs_put_page(page, 1);
518
519 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900520fail:
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900521 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
522 update_inode_page(dir);
523 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
524 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900525 kunmap(dentry_page);
526 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900527 return err;
528}
529
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900530/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900531 * It only removes the dentry from the dentry page,corresponding name
532 * entry in name page does not need to be touched during deletion.
533 */
534void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
535 struct inode *inode)
536{
537 struct f2fs_dentry_block *dentry_blk;
538 unsigned int bit_pos;
539 struct address_space *mapping = page->mapping;
540 struct inode *dir = mapping->host;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900541 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900542 void *kaddr = page_address(page);
543 int i;
544
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900545 lock_page(page);
546 wait_on_page_writeback(page);
547
548 dentry_blk = (struct f2fs_dentry_block *)kaddr;
549 bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry;
550 for (i = 0; i < slots; i++)
551 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
552
553 /* Let's check and deallocate this dentry page */
554 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
555 NR_DENTRY_IN_BLOCK,
556 0);
557 kunmap(page); /* kunmap - pair of f2fs_find_entry */
558 set_page_dirty(page);
559
560 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
561
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900562 if (inode) {
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +0900563 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
564
Jaegeuk Kime8dae602014-01-21 18:31:38 +0900565 if (S_ISDIR(inode->i_mode)) {
566 drop_nlink(dir);
567 update_inode_page(dir);
568 }
Namjae Jeon16379982013-01-12 14:41:33 +0900569 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900570 drop_nlink(inode);
571 if (S_ISDIR(inode->i_mode)) {
572 drop_nlink(inode);
573 i_size_write(inode, 0);
574 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900575 update_inode_page(inode);
576
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900577 if (inode->i_nlink == 0)
578 add_orphan_inode(sbi, inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900579 else
580 release_orphan_inode(sbi);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581 }
582
583 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900584 truncate_hole(dir, page->index, page->index + 1);
585 clear_page_dirty_for_io(page);
586 ClearPageUptodate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900587 inode_dec_dirty_dents(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900588 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900589 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900590}
591
592bool f2fs_empty_dir(struct inode *dir)
593{
594 unsigned long bidx;
595 struct page *dentry_page;
596 unsigned int bit_pos;
597 struct f2fs_dentry_block *dentry_blk;
598 unsigned long nblock = dir_blocks(dir);
599
600 for (bidx = 0; bidx < nblock; bidx++) {
601 void *kaddr;
602 dentry_page = get_lock_data_page(dir, bidx);
603 if (IS_ERR(dentry_page)) {
604 if (PTR_ERR(dentry_page) == -ENOENT)
605 continue;
606 else
607 return false;
608 }
609
610 kaddr = kmap_atomic(dentry_page);
611 dentry_blk = (struct f2fs_dentry_block *)kaddr;
612 if (bidx == 0)
613 bit_pos = 2;
614 else
615 bit_pos = 0;
616 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
617 NR_DENTRY_IN_BLOCK,
618 bit_pos);
619 kunmap_atomic(kaddr);
620
621 f2fs_put_page(dentry_page, 1);
622
623 if (bit_pos < NR_DENTRY_IN_BLOCK)
624 return false;
625 }
626 return true;
627}
628
Al Viro6f7f2312013-05-17 18:02:17 -0400629static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900630{
Al Viro496ad9a2013-01-23 17:07:38 -0500631 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900632 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900633 unsigned int bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900634 struct f2fs_dentry_block *dentry_blk = NULL;
635 struct f2fs_dir_entry *de = NULL;
636 struct page *dentry_page = NULL;
Al Viro6f7f2312013-05-17 18:02:17 -0400637 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900638 unsigned char d_type = DT_UNKNOWN;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900639
Al Viro6f7f2312013-05-17 18:02:17 -0400640 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900641
Chris Fries6c311ec2014-01-17 14:44:39 -0600642 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900643 dentry_page = get_lock_data_page(inode, n);
644 if (IS_ERR(dentry_page))
645 continue;
646
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900647 dentry_blk = kmap(dentry_page);
648 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900649 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
650 NR_DENTRY_IN_BLOCK,
651 bit_pos);
652 if (bit_pos >= NR_DENTRY_IN_BLOCK)
653 break;
654
655 de = &dentry_blk->dentry[bit_pos];
Al Viro6f7f2312013-05-17 18:02:17 -0400656 if (de->file_type < F2FS_FT_MAX)
657 d_type = f2fs_filetype_table[de->file_type];
658 else
659 d_type = DT_UNKNOWN;
660 if (!dir_emit(ctx,
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900661 dentry_blk->filename[bit_pos],
662 le16_to_cpu(de->name_len),
663 le32_to_cpu(de->ino), d_type))
664 goto stop;
665
666 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
667 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900668 }
669 bit_pos = 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400670 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900671 kunmap(dentry_page);
672 f2fs_put_page(dentry_page, 1);
673 dentry_page = NULL;
674 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900675stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900676 if (dentry_page && !IS_ERR(dentry_page)) {
677 kunmap(dentry_page);
678 f2fs_put_page(dentry_page, 1);
679 }
680
681 return 0;
682}
683
684const struct file_operations f2fs_dir_operations = {
685 .llseek = generic_file_llseek,
686 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400687 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900688 .fsync = f2fs_sync_file,
689 .unlocked_ioctl = f2fs_ioctl,
690};