blob: 750a6883f04720ed1e9b922920753d05dc998c0a [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
Jaegeuk Kim510022a2015-03-30 15:07:16 -070062void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090063{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090064 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
65}
66
Jaegeuk Kim38431542014-02-27 18:20:00 +090067static unsigned long dir_block_index(unsigned int level,
68 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090069{
70 unsigned long i;
71 unsigned long bidx = 0;
72
73 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +090074 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090075 bidx += idx * bucket_blocks(level);
76 return bidx;
77}
78
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070079static bool early_match_name(size_t namelen, f2fs_hash_t namehash,
Gu Zhengeee61602014-06-24 18:21:23 +080080 struct f2fs_dir_entry *de)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090081{
82 if (le16_to_cpu(de->name_len) != namelen)
83 return false;
84
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090085 if (de->hash_code != namehash)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090086 return false;
87
88 return true;
89}
90
91static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070092 struct qstr *name, int *max_slots,
93 struct page **res_page)
94{
95 struct f2fs_dentry_block *dentry_blk;
96 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -070097 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070098
99 dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700100
101 make_dentry_ptr(&d, (void *)dentry_blk, 1);
102 de = find_target_dentry(name, max_slots, &d);
103
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700104 if (de)
105 *res_page = dentry_page;
106 else
107 kunmap(dentry_page);
108
109 /*
110 * For the most part, it should be a bug when name_len is zero.
111 * We stop here for figuring out where the bugs has occurred.
112 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700113 f2fs_bug_on(F2FS_P_SB(dentry_page), d.max < 0);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700114 return de;
115}
116
117struct f2fs_dir_entry *find_target_dentry(struct qstr *name, int *max_slots,
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700118 struct f2fs_dentry_ptr *d)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900119{
120 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900121 unsigned long bit_pos = 0;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700122 f2fs_hash_t namehash = f2fs_dentry_hash(name);
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900123 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900124
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700125 if (max_slots)
126 *max_slots = 0;
127 while (bit_pos < d->max) {
128 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900129 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800130 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900131 continue;
132 }
Chao Yubda19072015-03-09 17:33:16 +0800133
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700134 de = &d->dentry[bit_pos];
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700135 if (early_match_name(name->len, namehash, de) &&
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700136 !memcmp(d->filename[bit_pos], name->name, name->len))
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700137 goto found;
138
Chao Yubda19072015-03-09 17:33:16 +0800139 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900140 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800141 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800142
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700143 /* remain bug on condition */
144 if (unlikely(!de->name_len))
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700145 d->max = -1;
Chao Yu81e366f2014-07-10 12:37:46 +0800146
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900147 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900148 }
149
150 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900151found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700152 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900153 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900154 return de;
155}
156
157static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Gu Zhengeee61602014-06-24 18:21:23 +0800158 unsigned int level, struct qstr *name,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900159 f2fs_hash_t namehash, struct page **res_page)
160{
Gu Zhengeee61602014-06-24 18:21:23 +0800161 int s = GET_DENTRY_SLOTS(name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900162 unsigned int nbucket, nblock;
163 unsigned int bidx, end_block;
164 struct page *dentry_page;
165 struct f2fs_dir_entry *de = NULL;
166 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700167 int max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900168
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700169 f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900170
Jaegeuk Kim38431542014-02-27 18:20:00 +0900171 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900172 nblock = bucket_blocks(level);
173
Jaegeuk Kim38431542014-02-27 18:20:00 +0900174 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
175 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900176 end_block = bidx + nblock;
177
178 for (; bidx < end_block; bidx++) {
179 /* no need to allocate new dentry pages to all the indices */
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700180 dentry_page = find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900181 if (IS_ERR(dentry_page)) {
182 room = true;
183 continue;
184 }
185
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700186 de = find_in_block(dentry_page, name, &max_slots, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900187 if (de)
188 break;
189
190 if (max_slots >= s)
191 room = true;
192 f2fs_put_page(dentry_page, 0);
193 }
194
195 if (!de && room && F2FS_I(dir)->chash != namehash) {
196 F2FS_I(dir)->chash = namehash;
197 F2FS_I(dir)->clevel = level;
198 }
199
200 return de;
201}
202
203/*
204 * Find an entry in the specified directory with the wanted name.
205 * It returns the page where the entry was found (as a parameter - res_page),
206 * and the entry itself. Page is returned mapped and unlocked.
207 * Entry is guaranteed to be valid.
208 */
209struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
210 struct qstr *child, struct page **res_page)
211{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900212 unsigned long npages = dir_blocks(dir);
213 struct f2fs_dir_entry *de = NULL;
214 f2fs_hash_t name_hash;
215 unsigned int max_depth;
216 unsigned int level;
217
Yuan Zhongb1f73b72015-03-07 10:07:42 +0000218 *res_page = NULL;
219
Chao Yu622f28a2014-09-24 18:19:10 +0800220 if (f2fs_has_inline_dentry(dir))
221 return find_in_inline_dir(dir, child, res_page);
222
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900223 if (npages == 0)
224 return NULL;
225
Gu Zhengeee61602014-06-24 18:21:23 +0800226 name_hash = f2fs_dentry_hash(child);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900227 max_depth = F2FS_I(dir)->i_current_depth;
228
229 for (level = 0; level < max_depth; level++) {
Gu Zhengeee61602014-06-24 18:21:23 +0800230 de = find_in_level(dir, level, child, name_hash, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900231 if (de)
232 break;
233 }
234 if (!de && F2FS_I(dir)->chash != name_hash) {
235 F2FS_I(dir)->chash = name_hash;
236 F2FS_I(dir)->clevel = level - 1;
237 }
238 return de;
239}
240
241struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
242{
Namjae Jeon4777f862013-05-23 22:58:07 +0900243 struct page *page;
244 struct f2fs_dir_entry *de;
245 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900246
Chao Yu622f28a2014-09-24 18:19:10 +0800247 if (f2fs_has_inline_dentry(dir))
248 return f2fs_parent_inline_dir(dir, p);
249
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900250 page = get_lock_data_page(dir, 0);
251 if (IS_ERR(page))
252 return NULL;
253
254 dentry_blk = kmap(page);
255 de = &dentry_blk->dentry[1];
256 *p = page;
257 unlock_page(page);
258 return de;
259}
260
261ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
262{
263 ino_t res = 0;
264 struct f2fs_dir_entry *de;
265 struct page *page;
266
267 de = f2fs_find_entry(dir, qstr, &page);
268 if (de) {
269 res = le32_to_cpu(de->ino);
Jaegeuk Kim9486ba42014-11-21 16:36:28 -0800270 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900271 f2fs_put_page(page, 0);
272 }
273
274 return res;
275}
276
277void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
278 struct page *page, struct inode *inode)
279{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700280 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900281 lock_page(page);
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700282 f2fs_wait_on_page_writeback(page, type);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900283 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700284 set_de_type(de, inode->i_mode);
Jaegeuk Kim5df1f1d2014-12-13 13:13:11 -0800285 f2fs_dentry_kunmap(dir, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900286 set_page_dirty(page);
287 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
288 mark_inode_dirty(dir);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900289
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900290 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900291}
292
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900293static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900294{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900295 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900296
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900297 f2fs_wait_on_page_writeback(ipage, NODE);
298
Al Viro53dc9a62013-01-25 16:01:21 -0500299 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900300 ri = F2FS_INODE(ipage);
301 ri->i_namelen = cpu_to_le32(name->len);
302 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900303 set_page_dirty(ipage);
304}
305
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900306int update_dent_inode(struct inode *inode, const struct qstr *name)
307{
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900308 struct page *page;
309
Jaegeuk Kim40813632014-09-02 15:31:18 -0700310 page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim1cd14ca2013-07-18 18:02:31 +0900311 if (IS_ERR(page))
312 return PTR_ERR(page);
313
314 init_dent_inode(name, page);
315 f2fs_put_page(page, 1);
316
317 return 0;
318}
319
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700320void do_make_empty_dir(struct inode *inode, struct inode *parent,
321 struct f2fs_dentry_ptr *d)
322{
323 struct f2fs_dir_entry *de;
324
325 de = &d->dentry[0];
326 de->name_len = cpu_to_le16(1);
327 de->hash_code = 0;
328 de->ino = cpu_to_le32(inode->i_ino);
329 memcpy(d->filename[0], ".", 1);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700330 set_de_type(de, inode->i_mode);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700331
332 de = &d->dentry[1];
333 de->hash_code = 0;
334 de->name_len = cpu_to_le16(2);
335 de->ino = cpu_to_le32(parent->i_ino);
336 memcpy(d->filename[1], "..", 2);
Jaegeuk Kimcb584632015-03-30 15:23:45 -0700337 set_de_type(de, parent->i_mode);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700338
339 test_and_set_bit_le(0, (void *)d->bitmap);
340 test_and_set_bit_le(1, (void *)d->bitmap);
341}
342
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900343static int make_empty_dir(struct inode *inode,
344 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900345{
346 struct page *dentry_page;
347 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700348 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900349
Chao Yu622f28a2014-09-24 18:19:10 +0800350 if (f2fs_has_inline_dentry(inode))
351 return make_empty_inline_dir(inode, parent, page);
352
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900353 dentry_page = get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900354 if (IS_ERR(dentry_page))
355 return PTR_ERR(dentry_page);
356
Gu Zheng1c3bb972014-06-27 17:57:04 +0800357 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900358
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700359 make_dentry_ptr(&d, (void *)dentry_blk, 1);
360 do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900361
Gu Zheng1c3bb972014-06-27 17:57:04 +0800362 kunmap_atomic(dentry_blk);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900363
364 set_page_dirty(dentry_page);
365 f2fs_put_page(dentry_page, 1);
366 return 0;
367}
368
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700369struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
370 const struct qstr *name, struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900371{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900372 struct page *page;
373 int err;
374
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700375 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kima014e032014-06-20 21:44:02 -0700376 page = new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900377 if (IS_ERR(page))
378 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900379
380 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900381 err = make_empty_dir(inode, dir, page);
382 if (err)
383 goto error;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900384 }
385
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700386 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900387 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900388 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900389
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900390 err = f2fs_init_security(inode, dir, name, page);
391 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900392 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700393
394 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
395 err = f2fs_inherit_context(dir, inode, page);
396 if (err)
397 goto put_error;
398 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900399 } else {
Jaegeuk Kim40813632014-09-02 15:31:18 -0700400 page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900401 if (IS_ERR(page))
402 return page;
403
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900404 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900405 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900406
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700407 if (name)
408 init_dent_inode(name, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900409
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900410 /*
411 * This file should be checkpointed during fsync.
412 * We lost i_pino from now on.
413 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900414 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900415 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800416 /*
417 * If link the tmpfile to alias through linkat path,
418 * we should remove this inode from orphan list.
419 */
420 if (inode->i_nlink == 0)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700421 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900422 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900423 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900424 return page;
425
Jaegeuk Kima8865372013-12-27 17:04:17 +0900426put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900427 f2fs_put_page(page, 1);
Chao Yu90d72452014-06-24 10:34:00 +0800428error:
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900429 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
430 truncate_inode_pages(&inode->i_data, 0);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700431 truncate_blocks(inode, 0, false);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900432 remove_dirty_dir_inode(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900433 remove_inode_page(inode);
434 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900435}
436
Chao Yudbeacf02014-09-24 18:17:04 +0800437void update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900438 unsigned int current_depth)
439{
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700440 if (inode && is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900441 if (S_ISDIR(inode->i_mode)) {
442 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900443 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900444 }
445 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
446 }
447 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900448 mark_inode_dirty(dir);
449
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900450 if (F2FS_I(dir)->i_current_depth != current_depth) {
451 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900452 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900453 }
454
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700455 if (inode && is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900456 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
457}
458
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700459int room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900460{
461 int bit_start = 0;
462 int zero_start, zero_end;
463next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700464 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
465 if (zero_start >= max_slots)
466 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900467
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700468 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900469 if (zero_end - zero_start >= slots)
470 return zero_start;
471
472 bit_start = zero_end + 1;
473
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700474 if (zero_end + 1 >= max_slots)
475 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900476 goto next;
477}
478
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700479void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Chao Yu3b4d7322015-02-16 16:17:20 +0800480 const struct qstr *name, f2fs_hash_t name_hash,
481 unsigned int bit_pos)
482{
483 struct f2fs_dir_entry *de;
484 int slots = GET_DENTRY_SLOTS(name->len);
485 int i;
486
487 de = &d->dentry[bit_pos];
488 de->hash_code = name_hash;
489 de->name_len = cpu_to_le16(name->len);
490 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700491 de->ino = cpu_to_le32(ino);
492 set_de_type(de, mode);
Chao Yu3b4d7322015-02-16 16:17:20 +0800493 for (i = 0; i < slots; i++)
494 test_and_set_bit_le(bit_pos + i, (void *)d->bitmap);
495}
496
Jaegeuk Kim39936832012-11-22 16:21:29 +0900497/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800498 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
499 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900500 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600501int __f2fs_add_link(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700502 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900503{
504 unsigned int bit_pos;
505 unsigned int level;
506 unsigned int current_depth;
507 unsigned long bidx, block;
508 f2fs_hash_t dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900509 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900510 struct page *dentry_page = NULL;
511 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800512 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700513 struct page *page = NULL;
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700514 struct f2fs_filename fname;
515 struct qstr new_name;
516 int slots, err;
517
518 err = f2fs_fname_setup_filename(dir, name, 0, &fname);
519 if (err)
520 return err;
521
522 new_name.name = fname_name(&fname);
523 new_name.len = fname_len(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900524
Chao Yu622f28a2014-09-24 18:19:10 +0800525 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700526 err = f2fs_add_inline_entry(dir, &new_name, inode, ino, mode);
Chao Yu622f28a2014-09-24 18:19:10 +0800527 if (!err || err != -EAGAIN)
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700528 goto out;
Chao Yu622f28a2014-09-24 18:19:10 +0800529 else
530 err = 0;
531 }
532
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900533 level = 0;
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700534 slots = GET_DENTRY_SLOTS(new_name.len);
535 dentry_hash = f2fs_dentry_hash(&new_name);
536
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900537 current_depth = F2FS_I(dir)->i_current_depth;
538 if (F2FS_I(dir)->chash == dentry_hash) {
539 level = F2FS_I(dir)->clevel;
540 F2FS_I(dir)->chash = 0;
541 }
542
543start:
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700544 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH)) {
545 err = -ENOSPC;
546 goto out;
547 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900548
549 /* Increase the depth, if required */
550 if (level == current_depth)
551 ++current_depth;
552
Jaegeuk Kim38431542014-02-27 18:20:00 +0900553 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900554 nblock = bucket_blocks(level);
555
Jaegeuk Kim38431542014-02-27 18:20:00 +0900556 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
557 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900558
559 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900560 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700561 if (IS_ERR(dentry_page)) {
562 err = PTR_ERR(dentry_page);
563 goto out;
564 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900565
566 dentry_blk = kmap(dentry_page);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700567 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
568 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900569 if (bit_pos < NR_DENTRY_IN_BLOCK)
570 goto add_dentry;
571
572 kunmap(dentry_page);
573 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900574 }
575
576 /* Move to next level to find the empty slot for new dentry */
577 ++level;
578 goto start;
579add_dentry:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900580 f2fs_wait_on_page_writeback(dentry_page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700582 if (inode) {
583 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700584 page = init_inode_metadata(inode, dir, &new_name, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700585 if (IS_ERR(page)) {
586 err = PTR_ERR(page);
587 goto fail;
588 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900589 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800590
591 make_dentry_ptr(&d, (void *)dentry_blk, 1);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700592 f2fs_update_dentry(ino, mode, &d, &new_name, dentry_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800593
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900594 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900595
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700596 if (inode) {
597 /* we don't need to mark_inode_dirty now */
598 F2FS_I(inode)->i_pino = dir->i_ino;
599 update_inode(inode, page);
600 f2fs_put_page(page, 1);
601 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900602
603 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900604fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700605 if (inode)
606 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900607
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900608 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
609 update_inode_page(dir);
610 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
611 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900612 kunmap(dentry_page);
613 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700614out:
615 f2fs_fname_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900616 return err;
617}
618
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700619int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
620{
621 struct page *page;
622 int err = 0;
623
624 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700625 page = init_inode_metadata(inode, dir, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700626 if (IS_ERR(page)) {
627 err = PTR_ERR(page);
628 goto fail;
629 }
630 /* we don't need to mark_inode_dirty now */
631 update_inode(inode, page);
632 f2fs_put_page(page, 1);
633
634 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
635fail:
636 up_write(&F2FS_I(inode)->i_sem);
637 return err;
638}
639
Chao Yudbeacf02014-09-24 18:17:04 +0800640void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
641{
642 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
643
644 down_write(&F2FS_I(inode)->i_sem);
645
646 if (S_ISDIR(inode->i_mode)) {
647 drop_nlink(dir);
648 if (page)
649 update_inode(dir, page);
650 else
651 update_inode_page(dir);
652 }
653 inode->i_ctime = CURRENT_TIME;
654
655 drop_nlink(inode);
656 if (S_ISDIR(inode->i_mode)) {
657 drop_nlink(inode);
658 i_size_write(inode, 0);
659 }
660 up_write(&F2FS_I(inode)->i_sem);
661 update_inode_page(inode);
662
663 if (inode->i_nlink == 0)
664 add_orphan_inode(sbi, inode->i_ino);
665 else
666 release_orphan_inode(sbi);
667}
668
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900669/*
arter97e1c42042014-08-06 23:22:50 +0900670 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900671 * entry in name page does not need to be touched during deletion.
672 */
673void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800674 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900675{
676 struct f2fs_dentry_block *dentry_blk;
677 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900678 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900679 int i;
680
Chao Yu622f28a2014-09-24 18:19:10 +0800681 if (f2fs_has_inline_dentry(dir))
682 return f2fs_delete_inline_entry(dentry, page, dir, inode);
683
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900684 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900685 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900686
Gu Zheng1c3bb972014-06-27 17:57:04 +0800687 dentry_blk = page_address(page);
688 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900689 for (i = 0; i < slots; i++)
Jan Kara1f7732f2014-10-23 14:40:20 +0200690 clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900691
692 /* Let's check and deallocate this dentry page */
693 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
694 NR_DENTRY_IN_BLOCK,
695 0);
696 kunmap(page); /* kunmap - pair of f2fs_find_entry */
697 set_page_dirty(page);
698
699 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
700
Chao Yudbeacf02014-09-24 18:17:04 +0800701 if (inode)
702 f2fs_drop_nlink(dir, inode, NULL);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900703
704 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900705 truncate_hole(dir, page->index, page->index + 1);
706 clear_page_dirty_for_io(page);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -0800707 ClearPagePrivate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900708 ClearPageUptodate(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700709 inode_dec_dirty_pages(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900710 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900711 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900712}
713
714bool f2fs_empty_dir(struct inode *dir)
715{
716 unsigned long bidx;
717 struct page *dentry_page;
718 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800719 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900720 unsigned long nblock = dir_blocks(dir);
721
Chao Yu622f28a2014-09-24 18:19:10 +0800722 if (f2fs_has_inline_dentry(dir))
723 return f2fs_empty_inline_dir(dir);
724
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900725 for (bidx = 0; bidx < nblock; bidx++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900726 dentry_page = get_lock_data_page(dir, bidx);
727 if (IS_ERR(dentry_page)) {
728 if (PTR_ERR(dentry_page) == -ENOENT)
729 continue;
730 else
731 return false;
732 }
733
Gu Zheng1c3bb972014-06-27 17:57:04 +0800734 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900735 if (bidx == 0)
736 bit_pos = 2;
737 else
738 bit_pos = 0;
739 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
740 NR_DENTRY_IN_BLOCK,
741 bit_pos);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800742 kunmap_atomic(dentry_blk);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900743
744 f2fs_put_page(dentry_page, 1);
745
746 if (bit_pos < NR_DENTRY_IN_BLOCK)
747 return false;
748 }
749 return true;
750}
751
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700752bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
753 unsigned int start_pos)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700754{
755 unsigned char d_type = DT_UNKNOWN;
756 unsigned int bit_pos;
757 struct f2fs_dir_entry *de = NULL;
758
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700759 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700760
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700761 while (bit_pos < d->max) {
762 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
763 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700764 break;
765
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700766 de = &d->dentry[bit_pos];
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700767 if (de->file_type < F2FS_FT_MAX)
768 d_type = f2fs_filetype_table[de->file_type];
769 else
770 d_type = DT_UNKNOWN;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700771 if (!dir_emit(ctx, d->filename[bit_pos],
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700772 le16_to_cpu(de->name_len),
773 le32_to_cpu(de->ino), d_type))
774 return true;
775
776 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
777 ctx->pos = start_pos + bit_pos;
778 }
779 return false;
780}
781
Al Viro6f7f2312013-05-17 18:02:17 -0400782static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900783{
Al Viro496ad9a2013-01-23 17:07:38 -0500784 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900785 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900786 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900787 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800788 struct file_ra_state *ra = &file->f_ra;
Al Viro6f7f2312013-05-17 18:02:17 -0400789 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700790 struct f2fs_dentry_ptr d;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900791
Chao Yu622f28a2014-09-24 18:19:10 +0800792 if (f2fs_has_inline_dentry(inode))
793 return f2fs_read_inline_dir(file, ctx);
794
Chao Yu817202d92014-04-28 17:59:43 +0800795 /* readahead for multi pages of dir */
796 if (npages - n > 1 && !ra_has_index(ra, n))
797 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
798 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
799
Chris Fries6c311ec2014-01-17 14:44:39 -0600800 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900801 dentry_page = get_lock_data_page(inode, n);
802 if (IS_ERR(dentry_page))
803 continue;
804
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900805 dentry_blk = kmap(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900806
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700807 make_dentry_ptr(&d, (void *)dentry_blk, 1);
808
809 if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700810 goto stop;
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900811
Al Viro6f7f2312013-05-17 18:02:17 -0400812 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900813 kunmap(dentry_page);
814 f2fs_put_page(dentry_page, 1);
815 dentry_page = NULL;
816 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900817stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900818 if (dentry_page && !IS_ERR(dentry_page)) {
819 kunmap(dentry_page);
820 f2fs_put_page(dentry_page, 1);
821 }
822
823 return 0;
824}
825
826const struct file_operations f2fs_dir_operations = {
827 .llseek = generic_file_llseek,
828 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400829 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900830 .fsync = f2fs_sync_file,
831 .unlocked_ioctl = f2fs_ioctl,
832};