blob: 164c6c911bf8dd249aec0858adf5155739b6517e [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
Chao Yudbeacf02014-09-24 18:17:04 +080040unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090041 [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
Chao Yudbeacf02014-09-24 18:17:04 +080062void set_de_type(struct f2fs_dir_entry *de, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090063{
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
Chao Yudbeacf02014-09-24 18:17:04 +080080bool early_match_name(size_t namelen, f2fs_hash_t namehash,
Gu Zhengeee61602014-06-24 18:21:23 +080081 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 Yu81e366f2014-07-10 12:37:46 +0800124
125 /*
126 * For the most part, it should be a bug when name_len is zero.
arter97e1c42042014-08-06 23:22:50 +0900127 * We stop here for figuring out where the bugs has occurred.
Chao Yu81e366f2014-07-10 12:37:46 +0800128 */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700129 f2fs_bug_on(F2FS_P_SB(dentry_page), !de->name_len);
Chao Yu81e366f2014-07-10 12:37:46 +0800130
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 Kim9850cf42014-09-02 15:52:58 -0700154 f2fs_bug_on(F2FS_I_SB(dir), 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
Chao Yu622f28a2014-09-24 18:19:10 +0800204 if (f2fs_has_inline_dentry(dir))
205 return find_in_inline_dir(dir, child, res_page);
206
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900207 if (npages == 0)
208 return NULL;
209
210 *res_page = NULL;
211
Gu Zhengeee61602014-06-24 18:21:23 +0800212 name_hash = f2fs_dentry_hash(child);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900213 max_depth = F2FS_I(dir)->i_current_depth;
214
215 for (level = 0; level < max_depth; level++) {
Gu Zhengeee61602014-06-24 18:21:23 +0800216 de = find_in_level(dir, level, child, name_hash, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900217 if (de)
218 break;
219 }
220 if (!de && F2FS_I(dir)->chash != name_hash) {
221 F2FS_I(dir)->chash = name_hash;
222 F2FS_I(dir)->clevel = level - 1;
223 }
224 return de;
225}
226
227struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
228{
Namjae Jeon4777f862013-05-23 22:58:07 +0900229 struct page *page;
230 struct f2fs_dir_entry *de;
231 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900232
Chao Yu622f28a2014-09-24 18:19:10 +0800233 if (f2fs_has_inline_dentry(dir))
234 return f2fs_parent_inline_dir(dir, p);
235
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900236 page = get_lock_data_page(dir, 0);
237 if (IS_ERR(page))
238 return NULL;
239
240 dentry_blk = kmap(page);
241 de = &dentry_blk->dentry[1];
242 *p = page;
243 unlock_page(page);
244 return de;
245}
246
247ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
248{
249 ino_t res = 0;
250 struct f2fs_dir_entry *de;
251 struct page *page;
252
253 de = f2fs_find_entry(dir, qstr, &page);
254 if (de) {
255 res = le32_to_cpu(de->ino);
Chao Yu622f28a2014-09-24 18:19:10 +0800256 if (!f2fs_has_inline_dentry(dir))
257 kunmap(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900258 f2fs_put_page(page, 0);
259 }
260
261 return res;
262}
263
264void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
265 struct page *page, struct inode *inode)
266{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900267 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900268 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900269 de->ino = cpu_to_le32(inode->i_ino);
270 set_de_type(de, inode);
Chao Yu622f28a2014-09-24 18:19:10 +0800271 if (!f2fs_has_inline_dentry(dir))
272 kunmap(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900273 set_page_dirty(page);
274 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
275 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900276
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900277 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900278}
279
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900280static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900281{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900282 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900283
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900284 f2fs_wait_on_page_writeback(ipage, NODE);
285
Al Viro53dc9a62013-01-25 16:01:21 -0500286 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900287 ri = F2FS_INODE(ipage);
288 ri->i_namelen = cpu_to_le32(name->len);
289 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900290 set_page_dirty(ipage);
291}
292
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900293int update_dent_inode(struct inode *inode, const struct qstr *name)
294{
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900295 struct page *page;
296
Jaegeuk Kim40813632014-09-02 15:31:18 -0700297 page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900298 if (IS_ERR(page))
299 return PTR_ERR(page);
300
301 init_dent_inode(name, page);
302 f2fs_put_page(page, 1);
303
304 return 0;
305}
306
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900307static int make_empty_dir(struct inode *inode,
308 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900309{
310 struct page *dentry_page;
311 struct f2fs_dentry_block *dentry_blk;
312 struct f2fs_dir_entry *de;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900313
Chao Yu622f28a2014-09-24 18:19:10 +0800314 if (f2fs_has_inline_dentry(inode))
315 return make_empty_inline_dir(inode, parent, page);
316
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900317 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900318 if (IS_ERR(dentry_page))
319 return PTR_ERR(dentry_page);
320
Gu Zheng1c3bb972014-06-27 17:57:04 +0800321 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900322
323 de = &dentry_blk->dentry[0];
324 de->name_len = cpu_to_le16(1);
325 de->hash_code = 0;
326 de->ino = cpu_to_le32(inode->i_ino);
327 memcpy(dentry_blk->filename[0], ".", 1);
328 set_de_type(de, inode);
329
330 de = &dentry_blk->dentry[1];
331 de->hash_code = 0;
332 de->name_len = cpu_to_le16(2);
333 de->ino = cpu_to_le32(parent->i_ino);
334 memcpy(dentry_blk->filename[1], "..", 2);
335 set_de_type(de, inode);
336
337 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
338 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800339 kunmap_atomic(dentry_blk);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900340
341 set_page_dirty(dentry_page);
342 f2fs_put_page(dentry_page, 1);
343 return 0;
344}
345
Chao Yudbeacf02014-09-24 18:17:04 +0800346struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500347 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900348{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900349 struct page *page;
350 int err;
351
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700352 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kima014e032014-06-20 21:44:02 -0700353 page = new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900354 if (IS_ERR(page))
355 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900356
357 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900358 err = make_empty_dir(inode, dir, page);
359 if (err)
360 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900361 }
362
Jaegeuk Kim2ed2d5b2013-10-28 13:17:54 +0900363 err = f2fs_init_acl(inode, dir, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900364 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900365 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900366
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900367 err = f2fs_init_security(inode, dir, name, page);
368 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900369 goto put_error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900370 } else {
Jaegeuk Kim40813632014-09-02 15:31:18 -0700371 page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900372 if (IS_ERR(page))
373 return page;
374
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900375 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900376 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900377
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700378 if (name)
379 init_dent_inode(name, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900380
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900381 /*
382 * This file should be checkpointed during fsync.
383 * We lost i_pino from now on.
384 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900385 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900386 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800387 /*
388 * If link the tmpfile to alias through linkat path,
389 * we should remove this inode from orphan list.
390 */
391 if (inode->i_nlink == 0)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700392 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900393 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900394 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900395 return page;
396
Jaegeuk Kima8865372013-12-27 17:04:17 +0900397put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900398 f2fs_put_page(page, 1);
Chao Yu90d72452014-06-24 10:34:00 +0800399error:
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900400 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
401 truncate_inode_pages(&inode->i_data, 0);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700402 truncate_blocks(inode, 0, false);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900403 remove_dirty_dir_inode(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900404 remove_inode_page(inode);
405 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900406}
407
Chao Yudbeacf02014-09-24 18:17:04 +0800408void update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900409 unsigned int current_depth)
410{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900411 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
412 if (S_ISDIR(inode->i_mode)) {
413 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900414 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900415 }
416 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
417 }
418 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900419 mark_inode_dirty(dir);
420
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900421 if (F2FS_I(dir)->i_current_depth != current_depth) {
422 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900423 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900424 }
425
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900426 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
427 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
428}
429
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700430int room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900431{
432 int bit_start = 0;
433 int zero_start, zero_end;
434next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700435 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
436 if (zero_start >= max_slots)
437 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900438
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700439 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900440 if (zero_end - zero_start >= slots)
441 return zero_start;
442
443 bit_start = zero_end + 1;
444
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700445 if (zero_end + 1 >= max_slots)
446 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900447 goto next;
448}
449
Jaegeuk Kim39936832012-11-22 16:21:29 +0900450/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800451 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
452 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900453 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600454int __f2fs_add_link(struct inode *dir, const struct qstr *name,
455 struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900456{
457 unsigned int bit_pos;
458 unsigned int level;
459 unsigned int current_depth;
460 unsigned long bidx, block;
461 f2fs_hash_t dentry_hash;
462 struct f2fs_dir_entry *de;
463 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500464 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900465 struct page *dentry_page = NULL;
466 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900467 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900468 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900469 int err = 0;
470 int i;
471
Chao Yu622f28a2014-09-24 18:19:10 +0800472 if (f2fs_has_inline_dentry(dir)) {
473 err = f2fs_add_inline_entry(dir, name, inode);
474 if (!err || err != -EAGAIN)
475 return err;
476 else
477 err = 0;
478 }
479
Gu Zhengeee61602014-06-24 18:21:23 +0800480 dentry_hash = f2fs_dentry_hash(name);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900481 level = 0;
482 current_depth = F2FS_I(dir)->i_current_depth;
483 if (F2FS_I(dir)->chash == dentry_hash) {
484 level = F2FS_I(dir)->clevel;
485 F2FS_I(dir)->chash = 0;
486 }
487
488start:
Chao Yucfb271d2013-12-05 17:15:22 +0800489 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900490 return -ENOSPC;
491
492 /* Increase the depth, if required */
493 if (level == current_depth)
494 ++current_depth;
495
Jaegeuk Kim38431542014-02-27 18:20:00 +0900496 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900497 nblock = bucket_blocks(level);
498
Jaegeuk Kim38431542014-02-27 18:20:00 +0900499 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
500 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900501
502 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900503 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900504 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900505 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900506
507 dentry_blk = kmap(dentry_page);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700508 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
509 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900510 if (bit_pos < NR_DENTRY_IN_BLOCK)
511 goto add_dentry;
512
513 kunmap(dentry_page);
514 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900515 }
516
517 /* Move to next level to find the empty slot for new dentry */
518 ++level;
519 goto start;
520add_dentry:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900521 f2fs_wait_on_page_writeback(dentry_page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900522
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900523 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900524 page = init_inode_metadata(inode, dir, name);
525 if (IS_ERR(page)) {
526 err = PTR_ERR(page);
527 goto fail;
528 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900529 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900530 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900531 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500532 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900533 de->ino = cpu_to_le32(inode->i_ino);
534 set_de_type(de, inode);
535 for (i = 0; i < slots; i++)
536 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
537 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900538
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900539 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900540 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900541 update_inode(inode, page);
542 f2fs_put_page(page, 1);
543
544 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900545fail:
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900546 up_write(&F2FS_I(inode)->i_sem);
547
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900548 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
549 update_inode_page(dir);
550 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
551 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900552 kunmap(dentry_page);
553 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900554 return err;
555}
556
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700557int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
558{
559 struct page *page;
560 int err = 0;
561
562 down_write(&F2FS_I(inode)->i_sem);
563 page = init_inode_metadata(inode, dir, NULL);
564 if (IS_ERR(page)) {
565 err = PTR_ERR(page);
566 goto fail;
567 }
568 /* we don't need to mark_inode_dirty now */
569 update_inode(inode, page);
570 f2fs_put_page(page, 1);
571
572 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
573fail:
574 up_write(&F2FS_I(inode)->i_sem);
575 return err;
576}
577
Chao Yudbeacf02014-09-24 18:17:04 +0800578void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
579{
580 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
581
582 down_write(&F2FS_I(inode)->i_sem);
583
584 if (S_ISDIR(inode->i_mode)) {
585 drop_nlink(dir);
586 if (page)
587 update_inode(dir, page);
588 else
589 update_inode_page(dir);
590 }
591 inode->i_ctime = CURRENT_TIME;
592
593 drop_nlink(inode);
594 if (S_ISDIR(inode->i_mode)) {
595 drop_nlink(inode);
596 i_size_write(inode, 0);
597 }
598 up_write(&F2FS_I(inode)->i_sem);
599 update_inode_page(inode);
600
601 if (inode->i_nlink == 0)
602 add_orphan_inode(sbi, inode->i_ino);
603 else
604 release_orphan_inode(sbi);
605}
606
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900607/*
arter97e1c42042014-08-06 23:22:50 +0900608 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900609 * entry in name page does not need to be touched during deletion.
610 */
611void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800612 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900613{
614 struct f2fs_dentry_block *dentry_blk;
615 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900616 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900617 int i;
618
Chao Yu622f28a2014-09-24 18:19:10 +0800619 if (f2fs_has_inline_dentry(dir))
620 return f2fs_delete_inline_entry(dentry, page, dir, inode);
621
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900622 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900623 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900624
Gu Zheng1c3bb972014-06-27 17:57:04 +0800625 dentry_blk = page_address(page);
626 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900627 for (i = 0; i < slots; i++)
628 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
629
630 /* Let's check and deallocate this dentry page */
631 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
632 NR_DENTRY_IN_BLOCK,
633 0);
634 kunmap(page); /* kunmap - pair of f2fs_find_entry */
635 set_page_dirty(page);
636
637 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
638
Chao Yudbeacf02014-09-24 18:17:04 +0800639 if (inode)
640 f2fs_drop_nlink(dir, inode, NULL);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900641
642 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900643 truncate_hole(dir, page->index, page->index + 1);
644 clear_page_dirty_for_io(page);
645 ClearPageUptodate(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700646 inode_dec_dirty_pages(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900647 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900648 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900649}
650
651bool f2fs_empty_dir(struct inode *dir)
652{
653 unsigned long bidx;
654 struct page *dentry_page;
655 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800656 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900657 unsigned long nblock = dir_blocks(dir);
658
Chao Yu622f28a2014-09-24 18:19:10 +0800659 if (f2fs_has_inline_dentry(dir))
660 return f2fs_empty_inline_dir(dir);
661
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900662 for (bidx = 0; bidx < nblock; bidx++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900663 dentry_page = get_lock_data_page(dir, bidx);
664 if (IS_ERR(dentry_page)) {
665 if (PTR_ERR(dentry_page) == -ENOENT)
666 continue;
667 else
668 return false;
669 }
670
Gu Zheng1c3bb972014-06-27 17:57:04 +0800671 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900672 if (bidx == 0)
673 bit_pos = 2;
674 else
675 bit_pos = 0;
676 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
677 NR_DENTRY_IN_BLOCK,
678 bit_pos);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800679 kunmap_atomic(dentry_blk);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900680
681 f2fs_put_page(dentry_page, 1);
682
683 if (bit_pos < NR_DENTRY_IN_BLOCK)
684 return false;
685 }
686 return true;
687}
688
Al Viro6f7f2312013-05-17 18:02:17 -0400689static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900690{
Al Viro496ad9a2013-01-23 17:07:38 -0500691 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900692 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900693 unsigned int bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900694 struct f2fs_dentry_block *dentry_blk = NULL;
695 struct f2fs_dir_entry *de = NULL;
696 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800697 struct file_ra_state *ra = &file->f_ra;
Al Viro6f7f2312013-05-17 18:02:17 -0400698 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900699 unsigned char d_type = DT_UNKNOWN;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900700
Chao Yu622f28a2014-09-24 18:19:10 +0800701 if (f2fs_has_inline_dentry(inode))
702 return f2fs_read_inline_dir(file, ctx);
703
Al Viro6f7f2312013-05-17 18:02:17 -0400704 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900705
Chao Yu817202d92014-04-28 17:59:43 +0800706 /* readahead for multi pages of dir */
707 if (npages - n > 1 && !ra_has_index(ra, n))
708 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
709 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
710
Chris Fries6c311ec2014-01-17 14:44:39 -0600711 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900712 dentry_page = get_lock_data_page(inode, n);
713 if (IS_ERR(dentry_page))
714 continue;
715
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900716 dentry_blk = kmap(dentry_page);
717 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900718 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
719 NR_DENTRY_IN_BLOCK,
720 bit_pos);
721 if (bit_pos >= NR_DENTRY_IN_BLOCK)
722 break;
723
724 de = &dentry_blk->dentry[bit_pos];
Al Viro6f7f2312013-05-17 18:02:17 -0400725 if (de->file_type < F2FS_FT_MAX)
726 d_type = f2fs_filetype_table[de->file_type];
727 else
728 d_type = DT_UNKNOWN;
729 if (!dir_emit(ctx,
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900730 dentry_blk->filename[bit_pos],
731 le16_to_cpu(de->name_len),
732 le32_to_cpu(de->ino), d_type))
733 goto stop;
734
735 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
736 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900737 }
738 bit_pos = 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400739 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900740 kunmap(dentry_page);
741 f2fs_put_page(dentry_page, 1);
742 dentry_page = NULL;
743 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900744stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900745 if (dentry_page && !IS_ERR(dentry_page)) {
746 kunmap(dentry_page);
747 f2fs_put_page(dentry_page, 1);
748 }
749
750 return 0;
751}
752
753const struct file_operations f2fs_dir_operations = {
754 .llseek = generic_file_llseek,
755 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400756 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900757 .fsync = f2fs_sync_file,
758 .unlocked_ioctl = f2fs_ioctl,
759};