blob: bcf893c3d9036d0eb743ff8c34bae2eda23e9582 [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{
Chao Yubfec07d2014-05-28 08:56:09 +080026 if (level + dir_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
Chao Yubfec07d2014-05-28 08:56:09 +080029 return MAX_DIR_BUCKETS;
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
Gu Zhengeee61602014-06-24 18:21:23 +080080static bool early_match_name(size_t namelen, f2fs_hash_t namehash,
81 struct f2fs_dir_entry *de)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090082{
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,
Gu Zhengeee61602014-06-24 18:21:23 +080093 struct qstr *name, 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];
Gu Zhengeee61602014-06-24 18:21:23 +0800112 if (early_match_name(name->len, namehash, de)) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900113 if (!memcmp(dentry_blk->filename[bit_pos],
Gu Zhengeee61602014-06-24 18:21:23 +0800114 name->name,
115 name->len)) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900116 *res_page = dentry_page;
117 goto found;
118 }
119 }
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900120 if (max_len > *max_slots) {
121 *max_slots = max_len;
122 max_len = 0;
123 }
Chao Yu81e366f82014-07-10 12:37:46 +0800124
125 /*
126 * For the most part, it should be a bug when name_len is zero.
127 * We stop here for figuring out where the bugs are occurred.
128 */
129 f2fs_bug_on(!de->name_len);
130
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900131 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900132 }
133
134 de = NULL;
135 kunmap(dentry_page);
136found:
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900137 if (max_len > *max_slots)
138 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900139 return de;
140}
141
142static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Gu Zhengeee61602014-06-24 18:21:23 +0800143 unsigned int level, struct qstr *name,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900144 f2fs_hash_t namehash, struct page **res_page)
145{
Gu Zhengeee61602014-06-24 18:21:23 +0800146 int s = GET_DENTRY_SLOTS(name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900147 unsigned int nbucket, nblock;
148 unsigned int bidx, end_block;
149 struct page *dentry_page;
150 struct f2fs_dir_entry *de = NULL;
151 bool room = false;
152 int max_slots = 0;
153
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900154 f2fs_bug_on(level > MAX_DIR_HASH_DEPTH);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900155
Jaegeuk Kim38431542014-02-27 18:20:00 +0900156 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900157 nblock = bucket_blocks(level);
158
Jaegeuk Kim38431542014-02-27 18:20:00 +0900159 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
160 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900161 end_block = bidx + nblock;
162
163 for (; bidx < end_block; bidx++) {
164 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900165 dentry_page = find_data_page(dir, bidx, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900166 if (IS_ERR(dentry_page)) {
167 room = true;
168 continue;
169 }
170
Gu Zhengeee61602014-06-24 18:21:23 +0800171 de = find_in_block(dentry_page, name, &max_slots,
172 namehash, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900173 if (de)
174 break;
175
176 if (max_slots >= s)
177 room = true;
178 f2fs_put_page(dentry_page, 0);
179 }
180
181 if (!de && room && F2FS_I(dir)->chash != namehash) {
182 F2FS_I(dir)->chash = namehash;
183 F2FS_I(dir)->clevel = level;
184 }
185
186 return de;
187}
188
189/*
190 * Find an entry in the specified directory with the wanted name.
191 * It returns the page where the entry was found (as a parameter - res_page),
192 * and the entry itself. Page is returned mapped and unlocked.
193 * Entry is guaranteed to be valid.
194 */
195struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
196 struct qstr *child, struct page **res_page)
197{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900198 unsigned long npages = dir_blocks(dir);
199 struct f2fs_dir_entry *de = NULL;
200 f2fs_hash_t name_hash;
201 unsigned int max_depth;
202 unsigned int level;
203
204 if (npages == 0)
205 return NULL;
206
207 *res_page = NULL;
208
Gu Zhengeee61602014-06-24 18:21:23 +0800209 name_hash = f2fs_dentry_hash(child);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900210 max_depth = F2FS_I(dir)->i_current_depth;
211
212 for (level = 0; level < max_depth; level++) {
Gu Zhengeee61602014-06-24 18:21:23 +0800213 de = find_in_level(dir, level, child, name_hash, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900214 if (de)
215 break;
216 }
217 if (!de && F2FS_I(dir)->chash != name_hash) {
218 F2FS_I(dir)->chash = name_hash;
219 F2FS_I(dir)->clevel = level - 1;
220 }
221 return de;
222}
223
224struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
225{
Namjae Jeon4777f862013-05-23 22:58:07 +0900226 struct page *page;
227 struct f2fs_dir_entry *de;
228 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900229
230 page = get_lock_data_page(dir, 0);
231 if (IS_ERR(page))
232 return NULL;
233
234 dentry_blk = kmap(page);
235 de = &dentry_blk->dentry[1];
236 *p = page;
237 unlock_page(page);
238 return de;
239}
240
241ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
242{
243 ino_t res = 0;
244 struct f2fs_dir_entry *de;
245 struct page *page;
246
247 de = f2fs_find_entry(dir, qstr, &page);
248 if (de) {
249 res = le32_to_cpu(de->ino);
250 kunmap(page);
251 f2fs_put_page(page, 0);
252 }
253
254 return res;
255}
256
257void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
258 struct page *page, struct inode *inode)
259{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900260 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900261 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900262 de->ino = cpu_to_le32(inode->i_ino);
263 set_de_type(de, inode);
264 kunmap(page);
265 set_page_dirty(page);
266 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
267 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900268
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900269 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900270}
271
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900272static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900273{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900274 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900275
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900276 f2fs_wait_on_page_writeback(ipage, NODE);
277
Al Viro53dc9a62013-01-25 16:01:21 -0500278 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900279 ri = F2FS_INODE(ipage);
280 ri->i_namelen = cpu_to_le32(name->len);
281 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900282 set_page_dirty(ipage);
283}
284
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900285int update_dent_inode(struct inode *inode, const struct qstr *name)
286{
287 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
288 struct page *page;
289
290 page = get_node_page(sbi, inode->i_ino);
291 if (IS_ERR(page))
292 return PTR_ERR(page);
293
294 init_dent_inode(name, page);
295 f2fs_put_page(page, 1);
296
297 return 0;
298}
299
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900300static int make_empty_dir(struct inode *inode,
301 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900302{
303 struct page *dentry_page;
304 struct f2fs_dentry_block *dentry_blk;
305 struct f2fs_dir_entry *de;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900306
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900307 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900308 if (IS_ERR(dentry_page))
309 return PTR_ERR(dentry_page);
310
Gu Zheng1c3bb972014-06-27 17:57:04 +0800311
312 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900313
314 de = &dentry_blk->dentry[0];
315 de->name_len = cpu_to_le16(1);
316 de->hash_code = 0;
317 de->ino = cpu_to_le32(inode->i_ino);
318 memcpy(dentry_blk->filename[0], ".", 1);
319 set_de_type(de, inode);
320
321 de = &dentry_blk->dentry[1];
322 de->hash_code = 0;
323 de->name_len = cpu_to_le16(2);
324 de->ino = cpu_to_le32(parent->i_ino);
325 memcpy(dentry_blk->filename[1], "..", 2);
326 set_de_type(de, inode);
327
328 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
329 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800330 kunmap_atomic(dentry_blk);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900331
332 set_page_dirty(dentry_page);
333 f2fs_put_page(dentry_page, 1);
334 return 0;
335}
336
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900337static struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500338 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900339{
Chao Yu50732df2014-06-19 16:23:19 +0800340 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900341 struct page *page;
342 int err;
343
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700344 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kima014e032014-06-20 21:44:02 -0700345 page = new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900346 if (IS_ERR(page))
347 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900348
349 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900350 err = make_empty_dir(inode, dir, page);
351 if (err)
352 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900353 }
354
Jaegeuk Kim2ed2d5b2013-10-28 13:17:54 +0900355 err = f2fs_init_acl(inode, dir, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900356 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900357 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900358
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900359 err = f2fs_init_security(inode, dir, name, page);
360 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900361 goto put_error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900362 } else {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900363 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
364 if (IS_ERR(page))
365 return page;
366
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900367 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900368 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900369
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700370 if (name)
371 init_dent_inode(name, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900372
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900373 /*
374 * This file should be checkpointed during fsync.
375 * We lost i_pino from now on.
376 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900377 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900378 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800379 /*
380 * If link the tmpfile to alias through linkat path,
381 * we should remove this inode from orphan list.
382 */
383 if (inode->i_nlink == 0)
384 remove_orphan_inode(sbi, inode->i_ino);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900385 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900386 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900387 return page;
388
Jaegeuk Kima8865372013-12-27 17:04:17 +0900389put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900390 f2fs_put_page(page, 1);
Chao Yu90d72452014-06-24 10:34:00 +0800391error:
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900392 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
393 truncate_inode_pages(&inode->i_data, 0);
394 truncate_blocks(inode, 0);
395 remove_dirty_dir_inode(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900396 remove_inode_page(inode);
397 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900398}
399
400static void update_parent_metadata(struct inode *dir, struct inode *inode,
401 unsigned int current_depth)
402{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900403 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
404 if (S_ISDIR(inode->i_mode)) {
405 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900406 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900407 }
408 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
409 }
410 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900411 mark_inode_dirty(dir);
412
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900413 if (F2FS_I(dir)->i_current_depth != current_depth) {
414 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900415 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900416 }
417
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900418 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
419 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
420}
421
422static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
423{
424 int bit_start = 0;
425 int zero_start, zero_end;
426next:
427 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
428 NR_DENTRY_IN_BLOCK,
429 bit_start);
430 if (zero_start >= NR_DENTRY_IN_BLOCK)
431 return NR_DENTRY_IN_BLOCK;
432
433 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
434 NR_DENTRY_IN_BLOCK,
435 zero_start);
436 if (zero_end - zero_start >= slots)
437 return zero_start;
438
439 bit_start = zero_end + 1;
440
441 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
442 return NR_DENTRY_IN_BLOCK;
443 goto next;
444}
445
Jaegeuk Kim39936832012-11-22 16:21:29 +0900446/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800447 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
448 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900449 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600450int __f2fs_add_link(struct inode *dir, const struct qstr *name,
451 struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900452{
453 unsigned int bit_pos;
454 unsigned int level;
455 unsigned int current_depth;
456 unsigned long bidx, block;
457 f2fs_hash_t dentry_hash;
458 struct f2fs_dir_entry *de;
459 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500460 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900461 struct page *dentry_page = NULL;
462 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900463 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900464 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900465 int err = 0;
466 int i;
467
Gu Zhengeee61602014-06-24 18:21:23 +0800468 dentry_hash = f2fs_dentry_hash(name);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900469 level = 0;
470 current_depth = F2FS_I(dir)->i_current_depth;
471 if (F2FS_I(dir)->chash == dentry_hash) {
472 level = F2FS_I(dir)->clevel;
473 F2FS_I(dir)->chash = 0;
474 }
475
476start:
Chao Yucfb271d2013-12-05 17:15:22 +0800477 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900478 return -ENOSPC;
479
480 /* Increase the depth, if required */
481 if (level == current_depth)
482 ++current_depth;
483
Jaegeuk Kim38431542014-02-27 18:20:00 +0900484 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900485 nblock = bucket_blocks(level);
486
Jaegeuk Kim38431542014-02-27 18:20:00 +0900487 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
488 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900489
490 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900491 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900492 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900493 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900494
495 dentry_blk = kmap(dentry_page);
496 bit_pos = room_for_filename(dentry_blk, slots);
497 if (bit_pos < NR_DENTRY_IN_BLOCK)
498 goto add_dentry;
499
500 kunmap(dentry_page);
501 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900502 }
503
504 /* Move to next level to find the empty slot for new dentry */
505 ++level;
506 goto start;
507add_dentry:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900508 f2fs_wait_on_page_writeback(dentry_page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900509
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900510 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900511 page = init_inode_metadata(inode, dir, name);
512 if (IS_ERR(page)) {
513 err = PTR_ERR(page);
514 goto fail;
515 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900516 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900517 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900518 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500519 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900520 de->ino = cpu_to_le32(inode->i_ino);
521 set_de_type(de, inode);
522 for (i = 0; i < slots; i++)
523 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
524 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900525
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900526 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900527 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900528 update_inode(inode, page);
529 f2fs_put_page(page, 1);
530
531 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900532fail:
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900533 up_write(&F2FS_I(inode)->i_sem);
534
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900535 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
536 update_inode_page(dir);
537 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
538 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900539 kunmap(dentry_page);
540 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900541 return err;
542}
543
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700544int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
545{
546 struct page *page;
547 int err = 0;
548
549 down_write(&F2FS_I(inode)->i_sem);
550 page = init_inode_metadata(inode, dir, NULL);
551 if (IS_ERR(page)) {
552 err = PTR_ERR(page);
553 goto fail;
554 }
555 /* we don't need to mark_inode_dirty now */
556 update_inode(inode, page);
557 f2fs_put_page(page, 1);
558
559 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
560fail:
561 up_write(&F2FS_I(inode)->i_sem);
562 return err;
563}
564
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900565/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900566 * It only removes the dentry from the dentry page,corresponding name
567 * entry in name page does not need to be touched during deletion.
568 */
569void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
570 struct inode *inode)
571{
572 struct f2fs_dentry_block *dentry_blk;
573 unsigned int bit_pos;
574 struct address_space *mapping = page->mapping;
575 struct inode *dir = mapping->host;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900576 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900577 int i;
578
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900579 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900580 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581
Gu Zheng1c3bb972014-06-27 17:57:04 +0800582 dentry_blk = page_address(page);
583 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900584 for (i = 0; i < slots; i++)
585 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
586
587 /* Let's check and deallocate this dentry page */
588 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
589 NR_DENTRY_IN_BLOCK,
590 0);
591 kunmap(page); /* kunmap - pair of f2fs_find_entry */
592 set_page_dirty(page);
593
594 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
595
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900596 if (inode) {
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +0900597 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
598
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900599 down_write(&F2FS_I(inode)->i_sem);
600
Jaegeuk Kime8dae602014-01-21 18:31:38 +0900601 if (S_ISDIR(inode->i_mode)) {
602 drop_nlink(dir);
603 update_inode_page(dir);
604 }
Namjae Jeon16379982013-01-12 14:41:33 +0900605 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900606 drop_nlink(inode);
607 if (S_ISDIR(inode->i_mode)) {
608 drop_nlink(inode);
609 i_size_write(inode, 0);
610 }
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900611 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900612 update_inode_page(inode);
613
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900614 if (inode->i_nlink == 0)
615 add_orphan_inode(sbi, inode->i_ino);
Jaegeuk Kimcbd56e72013-07-30 11:36:53 +0900616 else
617 release_orphan_inode(sbi);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900618 }
619
620 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900621 truncate_hole(dir, page->index, page->index + 1);
622 clear_page_dirty_for_io(page);
623 ClearPageUptodate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900624 inode_dec_dirty_dents(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900625 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900626 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900627}
628
629bool f2fs_empty_dir(struct inode *dir)
630{
631 unsigned long bidx;
632 struct page *dentry_page;
633 unsigned int bit_pos;
634 struct f2fs_dentry_block *dentry_blk;
635 unsigned long nblock = dir_blocks(dir);
636
637 for (bidx = 0; bidx < nblock; bidx++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900638 dentry_page = get_lock_data_page(dir, bidx);
639 if (IS_ERR(dentry_page)) {
640 if (PTR_ERR(dentry_page) == -ENOENT)
641 continue;
642 else
643 return false;
644 }
645
Gu Zheng1c3bb972014-06-27 17:57:04 +0800646
647 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900648 if (bidx == 0)
649 bit_pos = 2;
650 else
651 bit_pos = 0;
652 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
653 NR_DENTRY_IN_BLOCK,
654 bit_pos);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800655 kunmap_atomic(dentry_blk);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900656
657 f2fs_put_page(dentry_page, 1);
658
659 if (bit_pos < NR_DENTRY_IN_BLOCK)
660 return false;
661 }
662 return true;
663}
664
Al Viro6f7f2312013-05-17 18:02:17 -0400665static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900666{
Al Viro496ad9a2013-01-23 17:07:38 -0500667 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900668 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900669 unsigned int bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900670 struct f2fs_dentry_block *dentry_blk = NULL;
671 struct f2fs_dir_entry *de = NULL;
672 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800673 struct file_ra_state *ra = &file->f_ra;
Al Viro6f7f2312013-05-17 18:02:17 -0400674 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900675 unsigned char d_type = DT_UNKNOWN;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900676
Al Viro6f7f2312013-05-17 18:02:17 -0400677 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900678
Chao Yu817202d92014-04-28 17:59:43 +0800679 /* readahead for multi pages of dir */
680 if (npages - n > 1 && !ra_has_index(ra, n))
681 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
682 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
683
Chris Fries6c311ec2014-01-17 14:44:39 -0600684 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900685 dentry_page = get_lock_data_page(inode, n);
686 if (IS_ERR(dentry_page))
687 continue;
688
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900689 dentry_blk = kmap(dentry_page);
690 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900691 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
692 NR_DENTRY_IN_BLOCK,
693 bit_pos);
694 if (bit_pos >= NR_DENTRY_IN_BLOCK)
695 break;
696
697 de = &dentry_blk->dentry[bit_pos];
Al Viro6f7f2312013-05-17 18:02:17 -0400698 if (de->file_type < F2FS_FT_MAX)
699 d_type = f2fs_filetype_table[de->file_type];
700 else
701 d_type = DT_UNKNOWN;
702 if (!dir_emit(ctx,
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900703 dentry_blk->filename[bit_pos],
704 le16_to_cpu(de->name_len),
705 le32_to_cpu(de->ino), d_type))
706 goto stop;
707
708 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
709 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900710 }
711 bit_pos = 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400712 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900713 kunmap(dentry_page);
714 f2fs_put_page(dentry_page, 1);
715 dentry_page = NULL;
716 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900717stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900718 if (dentry_page && !IS_ERR(dentry_page)) {
719 kunmap(dentry_page);
720 f2fs_put_page(dentry_page, 1);
721 }
722
723 return 0;
724}
725
726const struct file_operations f2fs_dir_operations = {
727 .llseek = generic_file_llseek,
728 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400729 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900730 .fsync = f2fs_sync_file,
731 .unlocked_ioctl = f2fs_ioctl,
732};