blob: f7293a2674b23070784af011b63eac9c5a31dc74 [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;
Al Virob7f7a5e2013-01-25 16:15:43 -0500510 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900511 struct page *dentry_page = NULL;
512 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800513 struct f2fs_dentry_ptr d;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900514 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700515 struct page *page = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900516 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900517
Chao Yu622f28a2014-09-24 18:19:10 +0800518 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700519 err = f2fs_add_inline_entry(dir, name, inode, ino, mode);
Chao Yu622f28a2014-09-24 18:19:10 +0800520 if (!err || err != -EAGAIN)
521 return err;
522 else
523 err = 0;
524 }
525
Gu Zhengeee61602014-06-24 18:21:23 +0800526 dentry_hash = f2fs_dentry_hash(name);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900527 level = 0;
528 current_depth = F2FS_I(dir)->i_current_depth;
529 if (F2FS_I(dir)->chash == dentry_hash) {
530 level = F2FS_I(dir)->clevel;
531 F2FS_I(dir)->chash = 0;
532 }
533
534start:
Chao Yucfb271d2013-12-05 17:15:22 +0800535 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900536 return -ENOSPC;
537
538 /* Increase the depth, if required */
539 if (level == current_depth)
540 ++current_depth;
541
Jaegeuk Kim38431542014-02-27 18:20:00 +0900542 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900543 nblock = bucket_blocks(level);
544
Jaegeuk Kim38431542014-02-27 18:20:00 +0900545 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
546 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900547
548 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900549 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900550 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900551 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900552
553 dentry_blk = kmap(dentry_page);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700554 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
555 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900556 if (bit_pos < NR_DENTRY_IN_BLOCK)
557 goto add_dentry;
558
559 kunmap(dentry_page);
560 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900561 }
562
563 /* Move to next level to find the empty slot for new dentry */
564 ++level;
565 goto start;
566add_dentry:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900567 f2fs_wait_on_page_writeback(dentry_page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900568
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700569 if (inode) {
570 down_write(&F2FS_I(inode)->i_sem);
571 page = init_inode_metadata(inode, dir, name, NULL);
572 if (IS_ERR(page)) {
573 err = PTR_ERR(page);
574 goto fail;
575 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900576 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800577
578 make_dentry_ptr(&d, (void *)dentry_blk, 1);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700579 f2fs_update_dentry(ino, mode, &d, name, dentry_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800580
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900582
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700583 if (inode) {
584 /* we don't need to mark_inode_dirty now */
585 F2FS_I(inode)->i_pino = dir->i_ino;
586 update_inode(inode, page);
587 f2fs_put_page(page, 1);
588 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900589
590 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900591fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700592 if (inode)
593 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900594
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900595 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
596 update_inode_page(dir);
597 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
598 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900599 kunmap(dentry_page);
600 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900601 return err;
602}
603
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700604int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
605{
606 struct page *page;
607 int err = 0;
608
609 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700610 page = init_inode_metadata(inode, dir, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700611 if (IS_ERR(page)) {
612 err = PTR_ERR(page);
613 goto fail;
614 }
615 /* we don't need to mark_inode_dirty now */
616 update_inode(inode, page);
617 f2fs_put_page(page, 1);
618
619 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
620fail:
621 up_write(&F2FS_I(inode)->i_sem);
622 return err;
623}
624
Chao Yudbeacf02014-09-24 18:17:04 +0800625void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
626{
627 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
628
629 down_write(&F2FS_I(inode)->i_sem);
630
631 if (S_ISDIR(inode->i_mode)) {
632 drop_nlink(dir);
633 if (page)
634 update_inode(dir, page);
635 else
636 update_inode_page(dir);
637 }
638 inode->i_ctime = CURRENT_TIME;
639
640 drop_nlink(inode);
641 if (S_ISDIR(inode->i_mode)) {
642 drop_nlink(inode);
643 i_size_write(inode, 0);
644 }
645 up_write(&F2FS_I(inode)->i_sem);
646 update_inode_page(inode);
647
648 if (inode->i_nlink == 0)
649 add_orphan_inode(sbi, inode->i_ino);
650 else
651 release_orphan_inode(sbi);
652}
653
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900654/*
arter97e1c42042014-08-06 23:22:50 +0900655 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900656 * entry in name page does not need to be touched during deletion.
657 */
658void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800659 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900660{
661 struct f2fs_dentry_block *dentry_blk;
662 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900663 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900664 int i;
665
Chao Yu622f28a2014-09-24 18:19:10 +0800666 if (f2fs_has_inline_dentry(dir))
667 return f2fs_delete_inline_entry(dentry, page, dir, inode);
668
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900669 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900670 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900671
Gu Zheng1c3bb972014-06-27 17:57:04 +0800672 dentry_blk = page_address(page);
673 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900674 for (i = 0; i < slots; i++)
Jan Kara1f7732f2014-10-23 14:40:20 +0200675 clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900676
677 /* Let's check and deallocate this dentry page */
678 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
679 NR_DENTRY_IN_BLOCK,
680 0);
681 kunmap(page); /* kunmap - pair of f2fs_find_entry */
682 set_page_dirty(page);
683
684 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
685
Chao Yudbeacf02014-09-24 18:17:04 +0800686 if (inode)
687 f2fs_drop_nlink(dir, inode, NULL);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900688
689 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900690 truncate_hole(dir, page->index, page->index + 1);
691 clear_page_dirty_for_io(page);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -0800692 ClearPagePrivate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900693 ClearPageUptodate(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700694 inode_dec_dirty_pages(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900695 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900696 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900697}
698
699bool f2fs_empty_dir(struct inode *dir)
700{
701 unsigned long bidx;
702 struct page *dentry_page;
703 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800704 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900705 unsigned long nblock = dir_blocks(dir);
706
Chao Yu622f28a2014-09-24 18:19:10 +0800707 if (f2fs_has_inline_dentry(dir))
708 return f2fs_empty_inline_dir(dir);
709
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900710 for (bidx = 0; bidx < nblock; bidx++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900711 dentry_page = get_lock_data_page(dir, bidx);
712 if (IS_ERR(dentry_page)) {
713 if (PTR_ERR(dentry_page) == -ENOENT)
714 continue;
715 else
716 return false;
717 }
718
Gu Zheng1c3bb972014-06-27 17:57:04 +0800719 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900720 if (bidx == 0)
721 bit_pos = 2;
722 else
723 bit_pos = 0;
724 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
725 NR_DENTRY_IN_BLOCK,
726 bit_pos);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800727 kunmap_atomic(dentry_blk);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900728
729 f2fs_put_page(dentry_page, 1);
730
731 if (bit_pos < NR_DENTRY_IN_BLOCK)
732 return false;
733 }
734 return true;
735}
736
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700737bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
738 unsigned int start_pos)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700739{
740 unsigned char d_type = DT_UNKNOWN;
741 unsigned int bit_pos;
742 struct f2fs_dir_entry *de = NULL;
743
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700744 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700745
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700746 while (bit_pos < d->max) {
747 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
748 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700749 break;
750
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700751 de = &d->dentry[bit_pos];
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700752 if (de->file_type < F2FS_FT_MAX)
753 d_type = f2fs_filetype_table[de->file_type];
754 else
755 d_type = DT_UNKNOWN;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700756 if (!dir_emit(ctx, d->filename[bit_pos],
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700757 le16_to_cpu(de->name_len),
758 le32_to_cpu(de->ino), d_type))
759 return true;
760
761 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
762 ctx->pos = start_pos + bit_pos;
763 }
764 return false;
765}
766
Al Viro6f7f2312013-05-17 18:02:17 -0400767static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900768{
Al Viro496ad9a2013-01-23 17:07:38 -0500769 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900770 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900771 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900772 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800773 struct file_ra_state *ra = &file->f_ra;
Al Viro6f7f2312013-05-17 18:02:17 -0400774 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700775 struct f2fs_dentry_ptr d;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900776
Chao Yu622f28a2014-09-24 18:19:10 +0800777 if (f2fs_has_inline_dentry(inode))
778 return f2fs_read_inline_dir(file, ctx);
779
Chao Yu817202d92014-04-28 17:59:43 +0800780 /* readahead for multi pages of dir */
781 if (npages - n > 1 && !ra_has_index(ra, n))
782 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
783 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
784
Chris Fries6c311ec2014-01-17 14:44:39 -0600785 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900786 dentry_page = get_lock_data_page(inode, n);
787 if (IS_ERR(dentry_page))
788 continue;
789
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900790 dentry_blk = kmap(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900791
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700792 make_dentry_ptr(&d, (void *)dentry_blk, 1);
793
794 if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700795 goto stop;
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900796
Al Viro6f7f2312013-05-17 18:02:17 -0400797 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900798 kunmap(dentry_page);
799 f2fs_put_page(dentry_page, 1);
800 dentry_page = NULL;
801 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900802stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900803 if (dentry_page && !IS_ERR(dentry_page)) {
804 kunmap(dentry_page);
805 f2fs_put_page(dentry_page, 1);
806 }
807
808 return 0;
809}
810
811const struct file_operations f2fs_dir_operations = {
812 .llseek = generic_file_llseek,
813 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400814 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900815 .fsync = f2fs_sync_file,
816 .unlocked_ioctl = f2fs_ioctl,
817};