blob: 582fa00f3597d3fc15570781af17a330790d7ff0 [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 Kim5d0c66712014-02-27 13:57:53 +090099 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900100
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900101 while (bit_pos < NR_DENTRY_IN_BLOCK) {
102 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900103 if (!test_bit_le(bit_pos, &dentry_blk->dentry_bitmap)) {
104 if (bit_pos == 0)
105 max_len = 1;
106 else if (!test_bit_le(bit_pos - 1, &dentry_blk->dentry_bitmap))
107 max_len++;
108 bit_pos++;
109 continue;
110 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900111 if (early_match_name(name, namelen, namehash, de)) {
112 if (!memcmp(dentry_blk->filename[bit_pos],
113 name, namelen)) {
114 *res_page = dentry_page;
115 goto found;
116 }
117 }
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900118 if (max_len > *max_slots) {
119 *max_slots = max_len;
120 max_len = 0;
121 }
122 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900123 }
124
125 de = NULL;
126 kunmap(dentry_page);
127found:
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900128 if (max_len > *max_slots)
129 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900130 return de;
131}
132
133static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200134 unsigned int level, const char *name, size_t namelen,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900135 f2fs_hash_t namehash, struct page **res_page)
136{
Namjae Jeon457d08e2012-12-08 14:54:50 +0900137 int s = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900138 unsigned int nbucket, nblock;
139 unsigned int bidx, end_block;
140 struct page *dentry_page;
141 struct f2fs_dir_entry *de = NULL;
142 bool room = false;
143 int max_slots = 0;
144
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900145 f2fs_bug_on(level > MAX_DIR_HASH_DEPTH);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900146
Jaegeuk Kim38431542014-02-27 18:20:00 +0900147 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900148 nblock = bucket_blocks(level);
149
Jaegeuk Kim38431542014-02-27 18:20:00 +0900150 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
151 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900152 end_block = bidx + nblock;
153
154 for (; bidx < end_block; bidx++) {
155 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900156 dentry_page = find_data_page(dir, bidx, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900157 if (IS_ERR(dentry_page)) {
158 room = true;
159 continue;
160 }
161
162 de = find_in_block(dentry_page, name, namelen,
163 &max_slots, namehash, res_page);
164 if (de)
165 break;
166
167 if (max_slots >= s)
168 room = true;
169 f2fs_put_page(dentry_page, 0);
170 }
171
172 if (!de && room && F2FS_I(dir)->chash != namehash) {
173 F2FS_I(dir)->chash = namehash;
174 F2FS_I(dir)->clevel = level;
175 }
176
177 return de;
178}
179
180/*
181 * Find an entry in the specified directory with the wanted name.
182 * It returns the page where the entry was found (as a parameter - res_page),
183 * and the entry itself. Page is returned mapped and unlocked.
184 * Entry is guaranteed to be valid.
185 */
186struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
187 struct qstr *child, struct page **res_page)
188{
189 const char *name = child->name;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200190 size_t namelen = child->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900191 unsigned long npages = dir_blocks(dir);
192 struct f2fs_dir_entry *de = NULL;
193 f2fs_hash_t name_hash;
194 unsigned int max_depth;
195 unsigned int level;
196
197 if (npages == 0)
198 return NULL;
199
200 *res_page = NULL;
201
202 name_hash = f2fs_dentry_hash(name, namelen);
203 max_depth = F2FS_I(dir)->i_current_depth;
204
205 for (level = 0; level < max_depth; level++) {
206 de = find_in_level(dir, level, name,
207 namelen, name_hash, res_page);
208 if (de)
209 break;
210 }
211 if (!de && F2FS_I(dir)->chash != name_hash) {
212 F2FS_I(dir)->chash = name_hash;
213 F2FS_I(dir)->clevel = level - 1;
214 }
215 return de;
216}
217
218struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
219{
Namjae Jeon4777f862013-05-23 22:58:07 +0900220 struct page *page;
221 struct f2fs_dir_entry *de;
222 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900223
224 page = get_lock_data_page(dir, 0);
225 if (IS_ERR(page))
226 return NULL;
227
228 dentry_blk = kmap(page);
229 de = &dentry_blk->dentry[1];
230 *p = page;
231 unlock_page(page);
232 return de;
233}
234
235ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
236{
237 ino_t res = 0;
238 struct f2fs_dir_entry *de;
239 struct page *page;
240
241 de = f2fs_find_entry(dir, qstr, &page);
242 if (de) {
243 res = le32_to_cpu(de->ino);
244 kunmap(page);
245 f2fs_put_page(page, 0);
246 }
247
248 return res;
249}
250
251void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
252 struct page *page, struct inode *inode)
253{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900254 lock_page(page);
255 wait_on_page_writeback(page);
256 de->ino = cpu_to_le32(inode->i_ino);
257 set_de_type(de, inode);
258 kunmap(page);
259 set_page_dirty(page);
260 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
261 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900262
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900263 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900264}
265
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900266static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900267{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900268 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900269
Al Viro53dc9a62013-01-25 16:01:21 -0500270 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900271 ri = F2FS_INODE(ipage);
272 ri->i_namelen = cpu_to_le32(name->len);
273 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900274 set_page_dirty(ipage);
275}
276
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900277int update_dent_inode(struct inode *inode, const struct qstr *name)
278{
279 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
280 struct page *page;
281
282 page = get_node_page(sbi, inode->i_ino);
283 if (IS_ERR(page))
284 return PTR_ERR(page);
285
286 init_dent_inode(name, page);
287 f2fs_put_page(page, 1);
288
289 return 0;
290}
291
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900292static int make_empty_dir(struct inode *inode,
293 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900294{
295 struct page *dentry_page;
296 struct f2fs_dentry_block *dentry_blk;
297 struct f2fs_dir_entry *de;
298 void *kaddr;
299
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900300 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900301 if (IS_ERR(dentry_page))
302 return PTR_ERR(dentry_page);
303
304 kaddr = kmap_atomic(dentry_page);
305 dentry_blk = (struct f2fs_dentry_block *)kaddr;
306
307 de = &dentry_blk->dentry[0];
308 de->name_len = cpu_to_le16(1);
309 de->hash_code = 0;
310 de->ino = cpu_to_le32(inode->i_ino);
311 memcpy(dentry_blk->filename[0], ".", 1);
312 set_de_type(de, inode);
313
314 de = &dentry_blk->dentry[1];
315 de->hash_code = 0;
316 de->name_len = cpu_to_le16(2);
317 de->ino = cpu_to_le32(parent->i_ino);
318 memcpy(dentry_blk->filename[1], "..", 2);
319 set_de_type(de, inode);
320
321 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
322 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
323 kunmap_atomic(kaddr);
324
325 set_page_dirty(dentry_page);
326 f2fs_put_page(dentry_page, 1);
327 return 0;
328}
329
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900330static struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500331 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900332{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900333 struct page *page;
334 int err;
335
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900336 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900337 page = new_inode_page(inode, name);
338 if (IS_ERR(page))
339 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900340
341 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900342 err = make_empty_dir(inode, dir, page);
343 if (err)
344 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900345 }
346
Jaegeuk Kim2ed2d5b2013-10-28 13:17:54 +0900347 err = f2fs_init_acl(inode, dir, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900348 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900349 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900350
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900351 err = f2fs_init_security(inode, dir, name, page);
352 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900353 goto put_error;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900354
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900355 wait_on_page_writeback(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900356 } else {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900357 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
358 if (IS_ERR(page))
359 return page;
360
361 wait_on_page_writeback(page);
362 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900363 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900364
365 init_dent_inode(name, page);
366
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900367 /*
368 * This file should be checkpointed during fsync.
369 * We lost i_pino from now on.
370 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900371 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900372 file_lost_pino(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900373 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900374 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900375 return page;
376
Jaegeuk Kima8865372013-12-27 17:04:17 +0900377put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900378 f2fs_put_page(page, 1);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900379 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
380 truncate_inode_pages(&inode->i_data, 0);
381 truncate_blocks(inode, 0);
382 remove_dirty_dir_inode(inode);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900383error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900384 remove_inode_page(inode);
385 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900386}
387
388static void update_parent_metadata(struct inode *dir, struct inode *inode,
389 unsigned int current_depth)
390{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900391 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
392 if (S_ISDIR(inode->i_mode)) {
393 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900394 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900395 }
396 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
397 }
398 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900399 mark_inode_dirty(dir);
400
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900401 if (F2FS_I(dir)->i_current_depth != current_depth) {
402 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900403 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900404 }
405
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900406 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
407 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
408}
409
410static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
411{
412 int bit_start = 0;
413 int zero_start, zero_end;
414next:
415 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
416 NR_DENTRY_IN_BLOCK,
417 bit_start);
418 if (zero_start >= NR_DENTRY_IN_BLOCK)
419 return NR_DENTRY_IN_BLOCK;
420
421 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
422 NR_DENTRY_IN_BLOCK,
423 zero_start);
424 if (zero_end - zero_start >= slots)
425 return zero_start;
426
427 bit_start = zero_end + 1;
428
429 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
430 return NR_DENTRY_IN_BLOCK;
431 goto next;
432}
433
Jaegeuk Kim39936832012-11-22 16:21:29 +0900434/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800435 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
436 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900437 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600438int __f2fs_add_link(struct inode *dir, const struct qstr *name,
439 struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900440{
441 unsigned int bit_pos;
442 unsigned int level;
443 unsigned int current_depth;
444 unsigned long bidx, block;
445 f2fs_hash_t dentry_hash;
446 struct f2fs_dir_entry *de;
447 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500448 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900449 struct page *dentry_page = NULL;
450 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900451 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900452 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900453 int err = 0;
454 int i;
455
Al Virob7f7a5e2013-01-25 16:15:43 -0500456 dentry_hash = f2fs_dentry_hash(name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900457 level = 0;
458 current_depth = F2FS_I(dir)->i_current_depth;
459 if (F2FS_I(dir)->chash == dentry_hash) {
460 level = F2FS_I(dir)->clevel;
461 F2FS_I(dir)->chash = 0;
462 }
463
464start:
Chao Yucfb271d2013-12-05 17:15:22 +0800465 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900466 return -ENOSPC;
467
468 /* Increase the depth, if required */
469 if (level == current_depth)
470 ++current_depth;
471
Jaegeuk Kim38431542014-02-27 18:20:00 +0900472 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900473 nblock = bucket_blocks(level);
474
Jaegeuk Kim38431542014-02-27 18:20:00 +0900475 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
476 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900477
478 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900479 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900480 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900481 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900482
483 dentry_blk = kmap(dentry_page);
484 bit_pos = room_for_filename(dentry_blk, slots);
485 if (bit_pos < NR_DENTRY_IN_BLOCK)
486 goto add_dentry;
487
488 kunmap(dentry_page);
489 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900490 }
491
492 /* Move to next level to find the empty slot for new dentry */
493 ++level;
494 goto start;
495add_dentry:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900496 wait_on_page_writeback(dentry_page);
497
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900498 page = init_inode_metadata(inode, dir, name);
499 if (IS_ERR(page)) {
500 err = PTR_ERR(page);
501 goto fail;
502 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900503 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900504 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900505 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500506 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900507 de->ino = cpu_to_le32(inode->i_ino);
508 set_de_type(de, inode);
509 for (i = 0; i < slots; i++)
510 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
511 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900512
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900513 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900514 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900515 update_inode(inode, page);
516 f2fs_put_page(page, 1);
517
518 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900519fail:
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900520 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
521 update_inode_page(dir);
522 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
523 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900524 kunmap(dentry_page);
525 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900526 return err;
527}
528
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900529/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900530 * It only removes the dentry from the dentry page,corresponding name
531 * entry in name page does not need to be touched during deletion.
532 */
533void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
534 struct inode *inode)
535{
536 struct f2fs_dentry_block *dentry_blk;
537 unsigned int bit_pos;
538 struct address_space *mapping = page->mapping;
539 struct inode *dir = mapping->host;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900540 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900541 void *kaddr = page_address(page);
542 int i;
543
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900544 lock_page(page);
545 wait_on_page_writeback(page);
546
547 dentry_blk = (struct f2fs_dentry_block *)kaddr;
548 bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry;
549 for (i = 0; i < slots; i++)
550 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
551
552 /* Let's check and deallocate this dentry page */
553 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
554 NR_DENTRY_IN_BLOCK,
555 0);
556 kunmap(page); /* kunmap - pair of f2fs_find_entry */
557 set_page_dirty(page);
558
559 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
560
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900561 if (inode) {
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +0900562 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
563
Jaegeuk Kime8dae602014-01-21 18:31:38 +0900564 if (S_ISDIR(inode->i_mode)) {
565 drop_nlink(dir);
566 update_inode_page(dir);
567 }
Namjae Jeon16379982013-01-12 14:41:33 +0900568 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900569 drop_nlink(inode);
570 if (S_ISDIR(inode->i_mode)) {
571 drop_nlink(inode);
572 i_size_write(inode, 0);
573 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900574 update_inode_page(inode);
575
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900576 if (inode->i_nlink == 0)
577 add_orphan_inode(sbi, inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900578 else
579 release_orphan_inode(sbi);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900580 }
581
582 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900583 truncate_hole(dir, page->index, page->index + 1);
584 clear_page_dirty_for_io(page);
585 ClearPageUptodate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900586 inode_dec_dirty_dents(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900587 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900588 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900589}
590
591bool f2fs_empty_dir(struct inode *dir)
592{
593 unsigned long bidx;
594 struct page *dentry_page;
595 unsigned int bit_pos;
596 struct f2fs_dentry_block *dentry_blk;
597 unsigned long nblock = dir_blocks(dir);
598
599 for (bidx = 0; bidx < nblock; bidx++) {
600 void *kaddr;
601 dentry_page = get_lock_data_page(dir, bidx);
602 if (IS_ERR(dentry_page)) {
603 if (PTR_ERR(dentry_page) == -ENOENT)
604 continue;
605 else
606 return false;
607 }
608
609 kaddr = kmap_atomic(dentry_page);
610 dentry_blk = (struct f2fs_dentry_block *)kaddr;
611 if (bidx == 0)
612 bit_pos = 2;
613 else
614 bit_pos = 0;
615 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
616 NR_DENTRY_IN_BLOCK,
617 bit_pos);
618 kunmap_atomic(kaddr);
619
620 f2fs_put_page(dentry_page, 1);
621
622 if (bit_pos < NR_DENTRY_IN_BLOCK)
623 return false;
624 }
625 return true;
626}
627
Al Viro6f7f2312013-05-17 18:02:17 -0400628static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900629{
Al Viro496ad9a2013-01-23 17:07:38 -0500630 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900631 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900632 unsigned int bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900633 struct f2fs_dentry_block *dentry_blk = NULL;
634 struct f2fs_dir_entry *de = NULL;
635 struct page *dentry_page = NULL;
Al Viro6f7f2312013-05-17 18:02:17 -0400636 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900637 unsigned char d_type = DT_UNKNOWN;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900638
Al Viro6f7f2312013-05-17 18:02:17 -0400639 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900640
Chris Fries6c311ec2014-01-17 14:44:39 -0600641 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900642 dentry_page = get_lock_data_page(inode, n);
643 if (IS_ERR(dentry_page))
644 continue;
645
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900646 dentry_blk = kmap(dentry_page);
647 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900648 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
649 NR_DENTRY_IN_BLOCK,
650 bit_pos);
651 if (bit_pos >= NR_DENTRY_IN_BLOCK)
652 break;
653
654 de = &dentry_blk->dentry[bit_pos];
Al Viro6f7f2312013-05-17 18:02:17 -0400655 if (de->file_type < F2FS_FT_MAX)
656 d_type = f2fs_filetype_table[de->file_type];
657 else
658 d_type = DT_UNKNOWN;
659 if (!dir_emit(ctx,
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900660 dentry_blk->filename[bit_pos],
661 le16_to_cpu(de->name_len),
662 le32_to_cpu(de->ino), d_type))
663 goto stop;
664
665 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
666 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900667 }
668 bit_pos = 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400669 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900670 kunmap(dentry_page);
671 f2fs_put_page(dentry_page, 1);
672 dentry_page = NULL;
673 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900674stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900675 if (dentry_page && !IS_ERR(dentry_page)) {
676 kunmap(dentry_page);
677 f2fs_put_page(dentry_page, 1);
678 }
679
680 return 0;
681}
682
683const struct file_operations f2fs_dir_operations = {
684 .llseek = generic_file_llseek,
685 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400686 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900687 .fsync = f2fs_sync_file,
688 .unlocked_ioctl = f2fs_ioctl,
689};