blob: 0b97b1921504f89162bdf629f21cf5d9e4640707 [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
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070080static bool 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,
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070093 struct qstr *name, int *max_slots,
94 struct page **res_page)
95{
96 struct f2fs_dentry_block *dentry_blk;
97 struct f2fs_dir_entry *de;
98
99 *max_slots = NR_DENTRY_IN_BLOCK;
100
101 dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page);
102 de = find_target_dentry(name, max_slots, &dentry_blk->dentry_bitmap,
103 dentry_blk->dentry,
104 dentry_blk->filename);
105 if (de)
106 *res_page = dentry_page;
107 else
108 kunmap(dentry_page);
109
110 /*
111 * For the most part, it should be a bug when name_len is zero.
112 * We stop here for figuring out where the bugs has occurred.
113 */
114 f2fs_bug_on(F2FS_P_SB(dentry_page), *max_slots < 0);
115 return de;
116}
117
118struct f2fs_dir_entry *find_target_dentry(struct qstr *name, int *max_slots,
119 const void *bitmap, struct f2fs_dir_entry *dentry,
120 __u8 (*filenames)[F2FS_SLOT_LEN])
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900121{
122 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900123 unsigned long bit_pos = 0;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700124 f2fs_hash_t namehash = f2fs_dentry_hash(name);
125 int max_bits = *max_slots;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900126 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900127
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700128 *max_slots = 0;
129 while (bit_pos < max_bits) {
130 if (!test_bit_le(bit_pos, bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900131 if (bit_pos == 0)
132 max_len = 1;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700133 else if (!test_bit_le(bit_pos - 1, bitmap))
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900134 max_len++;
135 bit_pos++;
136 continue;
137 }
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700138 de = &dentry[bit_pos];
139 if (early_match_name(name->len, namehash, de) &&
140 !memcmp(filenames[bit_pos], name->name, name->len))
141 goto found;
142
143 if (*max_slots >= 0 && max_len > *max_slots) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900144 *max_slots = max_len;
145 max_len = 0;
146 }
Chao Yu81e366f2014-07-10 12:37:46 +0800147
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700148 /* remain bug on condition */
149 if (unlikely(!de->name_len))
150 *max_slots = -1;
Chao Yu81e366f2014-07-10 12:37:46 +0800151
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900152 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900153 }
154
155 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900156found:
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900157 if (max_len > *max_slots)
158 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900159 return de;
160}
161
162static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Gu Zhengeee61602014-06-24 18:21:23 +0800163 unsigned int level, struct qstr *name,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900164 f2fs_hash_t namehash, struct page **res_page)
165{
Gu Zhengeee61602014-06-24 18:21:23 +0800166 int s = GET_DENTRY_SLOTS(name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900167 unsigned int nbucket, nblock;
168 unsigned int bidx, end_block;
169 struct page *dentry_page;
170 struct f2fs_dir_entry *de = NULL;
171 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700172 int max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900173
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700174 f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900175
Jaegeuk Kim38431542014-02-27 18:20:00 +0900176 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900177 nblock = bucket_blocks(level);
178
Jaegeuk Kim38431542014-02-27 18:20:00 +0900179 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
180 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900181 end_block = bidx + nblock;
182
183 for (; bidx < end_block; bidx++) {
184 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900185 dentry_page = find_data_page(dir, bidx, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900186 if (IS_ERR(dentry_page)) {
187 room = true;
188 continue;
189 }
190
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700191 de = find_in_block(dentry_page, name, &max_slots, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900192 if (de)
193 break;
194
195 if (max_slots >= s)
196 room = true;
197 f2fs_put_page(dentry_page, 0);
198 }
199
200 if (!de && room && F2FS_I(dir)->chash != namehash) {
201 F2FS_I(dir)->chash = namehash;
202 F2FS_I(dir)->clevel = level;
203 }
204
205 return de;
206}
207
208/*
209 * Find an entry in the specified directory with the wanted name.
210 * It returns the page where the entry was found (as a parameter - res_page),
211 * and the entry itself. Page is returned mapped and unlocked.
212 * Entry is guaranteed to be valid.
213 */
214struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
215 struct qstr *child, struct page **res_page)
216{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900217 unsigned long npages = dir_blocks(dir);
218 struct f2fs_dir_entry *de = NULL;
219 f2fs_hash_t name_hash;
220 unsigned int max_depth;
221 unsigned int level;
222
Chao Yu622f28a2014-09-24 18:19:10 +0800223 if (f2fs_has_inline_dentry(dir))
224 return find_in_inline_dir(dir, child, res_page);
225
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900226 if (npages == 0)
227 return NULL;
228
229 *res_page = NULL;
230
Gu Zhengeee61602014-06-24 18:21:23 +0800231 name_hash = f2fs_dentry_hash(child);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900232 max_depth = F2FS_I(dir)->i_current_depth;
233
234 for (level = 0; level < max_depth; level++) {
Gu Zhengeee61602014-06-24 18:21:23 +0800235 de = find_in_level(dir, level, child, name_hash, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900236 if (de)
237 break;
238 }
239 if (!de && F2FS_I(dir)->chash != name_hash) {
240 F2FS_I(dir)->chash = name_hash;
241 F2FS_I(dir)->clevel = level - 1;
242 }
243 return de;
244}
245
246struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
247{
Namjae Jeon4777f862013-05-23 22:58:07 +0900248 struct page *page;
249 struct f2fs_dir_entry *de;
250 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900251
Chao Yu622f28a2014-09-24 18:19:10 +0800252 if (f2fs_has_inline_dentry(dir))
253 return f2fs_parent_inline_dir(dir, p);
254
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900255 page = get_lock_data_page(dir, 0);
256 if (IS_ERR(page))
257 return NULL;
258
259 dentry_blk = kmap(page);
260 de = &dentry_blk->dentry[1];
261 *p = page;
262 unlock_page(page);
263 return de;
264}
265
266ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
267{
268 ino_t res = 0;
269 struct f2fs_dir_entry *de;
270 struct page *page;
271
272 de = f2fs_find_entry(dir, qstr, &page);
273 if (de) {
274 res = le32_to_cpu(de->ino);
Chao Yu622f28a2014-09-24 18:19:10 +0800275 if (!f2fs_has_inline_dentry(dir))
276 kunmap(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900277 f2fs_put_page(page, 0);
278 }
279
280 return res;
281}
282
283void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
284 struct page *page, struct inode *inode)
285{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900286 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900287 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900288 de->ino = cpu_to_le32(inode->i_ino);
289 set_de_type(de, inode);
Chao Yu622f28a2014-09-24 18:19:10 +0800290 if (!f2fs_has_inline_dentry(dir))
291 kunmap(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900292 set_page_dirty(page);
293 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
294 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900295
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900296 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900297}
298
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900299static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900300{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900301 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900302
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900303 f2fs_wait_on_page_writeback(ipage, NODE);
304
Al Viro53dc9a62013-01-25 16:01:21 -0500305 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900306 ri = F2FS_INODE(ipage);
307 ri->i_namelen = cpu_to_le32(name->len);
308 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900309 set_page_dirty(ipage);
310}
311
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900312int update_dent_inode(struct inode *inode, const struct qstr *name)
313{
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900314 struct page *page;
315
Jaegeuk Kim40813632014-09-02 15:31:18 -0700316 page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900317 if (IS_ERR(page))
318 return PTR_ERR(page);
319
320 init_dent_inode(name, page);
321 f2fs_put_page(page, 1);
322
323 return 0;
324}
325
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900326static int make_empty_dir(struct inode *inode,
327 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900328{
329 struct page *dentry_page;
330 struct f2fs_dentry_block *dentry_blk;
331 struct f2fs_dir_entry *de;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900332
Chao Yu622f28a2014-09-24 18:19:10 +0800333 if (f2fs_has_inline_dentry(inode))
334 return make_empty_inline_dir(inode, parent, page);
335
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900336 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900337 if (IS_ERR(dentry_page))
338 return PTR_ERR(dentry_page);
339
Gu Zheng1c3bb972014-06-27 17:57:04 +0800340 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900341
342 de = &dentry_blk->dentry[0];
343 de->name_len = cpu_to_le16(1);
344 de->hash_code = 0;
345 de->ino = cpu_to_le32(inode->i_ino);
346 memcpy(dentry_blk->filename[0], ".", 1);
347 set_de_type(de, inode);
348
349 de = &dentry_blk->dentry[1];
350 de->hash_code = 0;
351 de->name_len = cpu_to_le16(2);
352 de->ino = cpu_to_le32(parent->i_ino);
353 memcpy(dentry_blk->filename[1], "..", 2);
354 set_de_type(de, inode);
355
356 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
357 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800358 kunmap_atomic(dentry_blk);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900359
360 set_page_dirty(dentry_page);
361 f2fs_put_page(dentry_page, 1);
362 return 0;
363}
364
Chao Yudbeacf02014-09-24 18:17:04 +0800365struct page *init_inode_metadata(struct inode *inode,
Al Viro69f24ea2013-01-25 16:08:53 -0500366 struct inode *dir, const struct qstr *name)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900367{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900368 struct page *page;
369 int err;
370
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700371 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kima014e032014-06-20 21:44:02 -0700372 page = new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900373 if (IS_ERR(page))
374 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900375
376 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900377 err = make_empty_dir(inode, dir, page);
378 if (err)
379 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900380 }
381
Jaegeuk Kim2ed2d5b2013-10-28 13:17:54 +0900382 err = f2fs_init_acl(inode, dir, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900383 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900384 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900385
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900386 err = f2fs_init_security(inode, dir, name, page);
387 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900388 goto put_error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900389 } else {
Jaegeuk Kim40813632014-09-02 15:31:18 -0700390 page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900391 if (IS_ERR(page))
392 return page;
393
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900394 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900395 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900396
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700397 if (name)
398 init_dent_inode(name, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900399
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900400 /*
401 * This file should be checkpointed during fsync.
402 * We lost i_pino from now on.
403 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900404 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900405 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800406 /*
407 * If link the tmpfile to alias through linkat path,
408 * we should remove this inode from orphan list.
409 */
410 if (inode->i_nlink == 0)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700411 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900412 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900413 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900414 return page;
415
Jaegeuk Kima8865372013-12-27 17:04:17 +0900416put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900417 f2fs_put_page(page, 1);
Chao Yu90d72452014-06-24 10:34:00 +0800418error:
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900419 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
420 truncate_inode_pages(&inode->i_data, 0);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700421 truncate_blocks(inode, 0, false);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900422 remove_dirty_dir_inode(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900423 remove_inode_page(inode);
424 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900425}
426
Chao Yudbeacf02014-09-24 18:17:04 +0800427void update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900428 unsigned int current_depth)
429{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900430 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
431 if (S_ISDIR(inode->i_mode)) {
432 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900433 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900434 }
435 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
436 }
437 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900438 mark_inode_dirty(dir);
439
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900440 if (F2FS_I(dir)->i_current_depth != current_depth) {
441 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900442 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900443 }
444
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900445 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
446 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
447}
448
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700449int room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900450{
451 int bit_start = 0;
452 int zero_start, zero_end;
453next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700454 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
455 if (zero_start >= max_slots)
456 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900457
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700458 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900459 if (zero_end - zero_start >= slots)
460 return zero_start;
461
462 bit_start = zero_end + 1;
463
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700464 if (zero_end + 1 >= max_slots)
465 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900466 goto next;
467}
468
Jaegeuk Kim39936832012-11-22 16:21:29 +0900469/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800470 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
471 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900472 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600473int __f2fs_add_link(struct inode *dir, const struct qstr *name,
474 struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900475{
476 unsigned int bit_pos;
477 unsigned int level;
478 unsigned int current_depth;
479 unsigned long bidx, block;
480 f2fs_hash_t dentry_hash;
481 struct f2fs_dir_entry *de;
482 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500483 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900484 struct page *dentry_page = NULL;
485 struct f2fs_dentry_block *dentry_blk = NULL;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900486 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900487 struct page *page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900488 int err = 0;
489 int i;
490
Chao Yu622f28a2014-09-24 18:19:10 +0800491 if (f2fs_has_inline_dentry(dir)) {
492 err = f2fs_add_inline_entry(dir, name, inode);
493 if (!err || err != -EAGAIN)
494 return err;
495 else
496 err = 0;
497 }
498
Gu Zhengeee61602014-06-24 18:21:23 +0800499 dentry_hash = f2fs_dentry_hash(name);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900500 level = 0;
501 current_depth = F2FS_I(dir)->i_current_depth;
502 if (F2FS_I(dir)->chash == dentry_hash) {
503 level = F2FS_I(dir)->clevel;
504 F2FS_I(dir)->chash = 0;
505 }
506
507start:
Chao Yucfb271d2013-12-05 17:15:22 +0800508 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900509 return -ENOSPC;
510
511 /* Increase the depth, if required */
512 if (level == current_depth)
513 ++current_depth;
514
Jaegeuk Kim38431542014-02-27 18:20:00 +0900515 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900516 nblock = bucket_blocks(level);
517
Jaegeuk Kim38431542014-02-27 18:20:00 +0900518 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
519 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900520
521 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900522 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900523 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900524 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900525
526 dentry_blk = kmap(dentry_page);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700527 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
528 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900529 if (bit_pos < NR_DENTRY_IN_BLOCK)
530 goto add_dentry;
531
532 kunmap(dentry_page);
533 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900534 }
535
536 /* Move to next level to find the empty slot for new dentry */
537 ++level;
538 goto start;
539add_dentry:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900540 f2fs_wait_on_page_writeback(dentry_page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900541
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900542 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900543 page = init_inode_metadata(inode, dir, name);
544 if (IS_ERR(page)) {
545 err = PTR_ERR(page);
546 goto fail;
547 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900548 de = &dentry_blk->dentry[bit_pos];
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900549 de->hash_code = dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900550 de->name_len = cpu_to_le16(namelen);
Al Virob7f7a5e2013-01-25 16:15:43 -0500551 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900552 de->ino = cpu_to_le32(inode->i_ino);
553 set_de_type(de, inode);
554 for (i = 0; i < slots; i++)
555 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
556 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900557
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900558 /* we don't need to mark_inode_dirty now */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900559 F2FS_I(inode)->i_pino = dir->i_ino;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900560 update_inode(inode, page);
561 f2fs_put_page(page, 1);
562
563 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900564fail:
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900565 up_write(&F2FS_I(inode)->i_sem);
566
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900567 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
568 update_inode_page(dir);
569 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
570 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900571 kunmap(dentry_page);
572 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900573 return err;
574}
575
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700576int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
577{
578 struct page *page;
579 int err = 0;
580
581 down_write(&F2FS_I(inode)->i_sem);
582 page = init_inode_metadata(inode, dir, NULL);
583 if (IS_ERR(page)) {
584 err = PTR_ERR(page);
585 goto fail;
586 }
587 /* we don't need to mark_inode_dirty now */
588 update_inode(inode, page);
589 f2fs_put_page(page, 1);
590
591 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
592fail:
593 up_write(&F2FS_I(inode)->i_sem);
594 return err;
595}
596
Chao Yudbeacf02014-09-24 18:17:04 +0800597void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
598{
599 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
600
601 down_write(&F2FS_I(inode)->i_sem);
602
603 if (S_ISDIR(inode->i_mode)) {
604 drop_nlink(dir);
605 if (page)
606 update_inode(dir, page);
607 else
608 update_inode_page(dir);
609 }
610 inode->i_ctime = CURRENT_TIME;
611
612 drop_nlink(inode);
613 if (S_ISDIR(inode->i_mode)) {
614 drop_nlink(inode);
615 i_size_write(inode, 0);
616 }
617 up_write(&F2FS_I(inode)->i_sem);
618 update_inode_page(inode);
619
620 if (inode->i_nlink == 0)
621 add_orphan_inode(sbi, inode->i_ino);
622 else
623 release_orphan_inode(sbi);
624}
625
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900626/*
arter97e1c42042014-08-06 23:22:50 +0900627 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900628 * entry in name page does not need to be touched during deletion.
629 */
630void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800631 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900632{
633 struct f2fs_dentry_block *dentry_blk;
634 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900635 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900636 int i;
637
Chao Yu622f28a2014-09-24 18:19:10 +0800638 if (f2fs_has_inline_dentry(dir))
639 return f2fs_delete_inline_entry(dentry, page, dir, inode);
640
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900641 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900642 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900643
Gu Zheng1c3bb972014-06-27 17:57:04 +0800644 dentry_blk = page_address(page);
645 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900646 for (i = 0; i < slots; i++)
647 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
648
649 /* Let's check and deallocate this dentry page */
650 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
651 NR_DENTRY_IN_BLOCK,
652 0);
653 kunmap(page); /* kunmap - pair of f2fs_find_entry */
654 set_page_dirty(page);
655
656 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
657
Chao Yudbeacf02014-09-24 18:17:04 +0800658 if (inode)
659 f2fs_drop_nlink(dir, inode, NULL);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900660
661 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900662 truncate_hole(dir, page->index, page->index + 1);
663 clear_page_dirty_for_io(page);
664 ClearPageUptodate(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700665 inode_dec_dirty_pages(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900666 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900667 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900668}
669
670bool f2fs_empty_dir(struct inode *dir)
671{
672 unsigned long bidx;
673 struct page *dentry_page;
674 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800675 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900676 unsigned long nblock = dir_blocks(dir);
677
Chao Yu622f28a2014-09-24 18:19:10 +0800678 if (f2fs_has_inline_dentry(dir))
679 return f2fs_empty_inline_dir(dir);
680
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900681 for (bidx = 0; bidx < nblock; bidx++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900682 dentry_page = get_lock_data_page(dir, bidx);
683 if (IS_ERR(dentry_page)) {
684 if (PTR_ERR(dentry_page) == -ENOENT)
685 continue;
686 else
687 return false;
688 }
689
Gu Zheng1c3bb972014-06-27 17:57:04 +0800690 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900691 if (bidx == 0)
692 bit_pos = 2;
693 else
694 bit_pos = 0;
695 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
696 NR_DENTRY_IN_BLOCK,
697 bit_pos);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800698 kunmap_atomic(dentry_blk);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900699
700 f2fs_put_page(dentry_page, 1);
701
702 if (bit_pos < NR_DENTRY_IN_BLOCK)
703 return false;
704 }
705 return true;
706}
707
Al Viro6f7f2312013-05-17 18:02:17 -0400708static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900709{
Al Viro496ad9a2013-01-23 17:07:38 -0500710 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900711 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900712 unsigned int bit_pos = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900713 struct f2fs_dentry_block *dentry_blk = NULL;
714 struct f2fs_dir_entry *de = NULL;
715 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800716 struct file_ra_state *ra = &file->f_ra;
Al Viro6f7f2312013-05-17 18:02:17 -0400717 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900718 unsigned char d_type = DT_UNKNOWN;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900719
Chao Yu622f28a2014-09-24 18:19:10 +0800720 if (f2fs_has_inline_dentry(inode))
721 return f2fs_read_inline_dir(file, ctx);
722
Al Viro6f7f2312013-05-17 18:02:17 -0400723 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900724
Chao Yu817202d92014-04-28 17:59:43 +0800725 /* readahead for multi pages of dir */
726 if (npages - n > 1 && !ra_has_index(ra, n))
727 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
728 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
729
Chris Fries6c311ec2014-01-17 14:44:39 -0600730 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900731 dentry_page = get_lock_data_page(inode, n);
732 if (IS_ERR(dentry_page))
733 continue;
734
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900735 dentry_blk = kmap(dentry_page);
736 while (bit_pos < NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900737 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
738 NR_DENTRY_IN_BLOCK,
739 bit_pos);
740 if (bit_pos >= NR_DENTRY_IN_BLOCK)
741 break;
742
743 de = &dentry_blk->dentry[bit_pos];
Al Viro6f7f2312013-05-17 18:02:17 -0400744 if (de->file_type < F2FS_FT_MAX)
745 d_type = f2fs_filetype_table[de->file_type];
746 else
747 d_type = DT_UNKNOWN;
748 if (!dir_emit(ctx,
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900749 dentry_blk->filename[bit_pos],
750 le16_to_cpu(de->name_len),
751 le32_to_cpu(de->ino), d_type))
752 goto stop;
753
754 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
755 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900756 }
757 bit_pos = 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400758 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900759 kunmap(dentry_page);
760 f2fs_put_page(dentry_page, 1);
761 dentry_page = NULL;
762 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900763stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900764 if (dentry_page && !IS_ERR(dentry_page)) {
765 kunmap(dentry_page);
766 f2fs_put_page(dentry_page, 1);
767 }
768
769 return 0;
770}
771
772const struct file_operations f2fs_dir_operations = {
773 .llseek = generic_file_llseek,
774 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400775 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900776 .fsync = f2fs_sync_file,
777 .unlocked_ioctl = f2fs_ioctl,
778};