blob: 3a3302ab78713a6535296252f4ac35f9966beeee [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 Kimc718379b2013-04-24 13:19:56 +0900180 dentry_page = find_data_page(dir, bidx, true);
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 Kim9486ba4422014-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 Kim6b4ea012012-11-14 16:59:04 +0900393 } else {
Jaegeuk Kim40813632014-09-02 15:31:18 -0700394 page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900395 if (IS_ERR(page))
396 return page;
397
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900398 set_cold_node(inode, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900399 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900400
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700401 if (name)
402 init_dent_inode(name, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900403
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900404 /*
405 * This file should be checkpointed during fsync.
406 * We lost i_pino from now on.
407 */
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900408 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900409 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800410 /*
411 * If link the tmpfile to alias through linkat path,
412 * we should remove this inode from orphan list.
413 */
414 if (inode->i_nlink == 0)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700415 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900416 inc_nlink(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900417 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900418 return page;
419
Jaegeuk Kima8865372013-12-27 17:04:17 +0900420put_error:
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900421 f2fs_put_page(page, 1);
Chao Yu90d72452014-06-24 10:34:00 +0800422error:
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900423 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
424 truncate_inode_pages(&inode->i_data, 0);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700425 truncate_blocks(inode, 0, false);
Jaegeuk Kimbd859c62014-02-05 11:16:39 +0900426 remove_dirty_dir_inode(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900427 remove_inode_page(inode);
428 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900429}
430
Chao Yudbeacf02014-09-24 18:17:04 +0800431void update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900432 unsigned int current_depth)
433{
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700434 if (inode && is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900435 if (S_ISDIR(inode->i_mode)) {
436 inc_nlink(dir);
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900437 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900438 }
439 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
440 }
441 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900442 mark_inode_dirty(dir);
443
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900444 if (F2FS_I(dir)->i_current_depth != current_depth) {
445 F2FS_I(dir)->i_current_depth = current_depth;
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900446 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900447 }
448
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700449 if (inode && is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900450 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
451}
452
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700453int room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900454{
455 int bit_start = 0;
456 int zero_start, zero_end;
457next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700458 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
459 if (zero_start >= max_slots)
460 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900461
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700462 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900463 if (zero_end - zero_start >= slots)
464 return zero_start;
465
466 bit_start = zero_end + 1;
467
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700468 if (zero_end + 1 >= max_slots)
469 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900470 goto next;
471}
472
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700473void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Chao Yu3b4d7322015-02-16 16:17:20 +0800474 const struct qstr *name, f2fs_hash_t name_hash,
475 unsigned int bit_pos)
476{
477 struct f2fs_dir_entry *de;
478 int slots = GET_DENTRY_SLOTS(name->len);
479 int i;
480
481 de = &d->dentry[bit_pos];
482 de->hash_code = name_hash;
483 de->name_len = cpu_to_le16(name->len);
484 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700485 de->ino = cpu_to_le32(ino);
486 set_de_type(de, mode);
Chao Yu3b4d7322015-02-16 16:17:20 +0800487 for (i = 0; i < slots; i++)
488 test_and_set_bit_le(bit_pos + i, (void *)d->bitmap);
489}
490
Jaegeuk Kim39936832012-11-22 16:21:29 +0900491/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800492 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
493 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900494 */
Chris Fries6c311ec2014-01-17 14:44:39 -0600495int __f2fs_add_link(struct inode *dir, const struct qstr *name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700496 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900497{
498 unsigned int bit_pos;
499 unsigned int level;
500 unsigned int current_depth;
501 unsigned long bidx, block;
502 f2fs_hash_t dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900503 unsigned int nbucket, nblock;
Al Virob7f7a5e2013-01-25 16:15:43 -0500504 size_t namelen = name->len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900505 struct page *dentry_page = NULL;
506 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800507 struct f2fs_dentry_ptr d;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900508 int slots = GET_DENTRY_SLOTS(namelen);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700509 struct page *page = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900510 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900511
Chao Yu622f28a2014-09-24 18:19:10 +0800512 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700513 err = f2fs_add_inline_entry(dir, name, inode, ino, mode);
Chao Yu622f28a2014-09-24 18:19:10 +0800514 if (!err || err != -EAGAIN)
515 return err;
516 else
517 err = 0;
518 }
519
Gu Zhengeee61602014-06-24 18:21:23 +0800520 dentry_hash = f2fs_dentry_hash(name);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900521 level = 0;
522 current_depth = F2FS_I(dir)->i_current_depth;
523 if (F2FS_I(dir)->chash == dentry_hash) {
524 level = F2FS_I(dir)->clevel;
525 F2FS_I(dir)->chash = 0;
526 }
527
528start:
Chao Yucfb271d2013-12-05 17:15:22 +0800529 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900530 return -ENOSPC;
531
532 /* Increase the depth, if required */
533 if (level == current_depth)
534 ++current_depth;
535
Jaegeuk Kim38431542014-02-27 18:20:00 +0900536 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900537 nblock = bucket_blocks(level);
538
Jaegeuk Kim38431542014-02-27 18:20:00 +0900539 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
540 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900541
542 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900543 dentry_page = get_new_data_page(dir, NULL, block, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900544 if (IS_ERR(dentry_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900545 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900546
547 dentry_blk = kmap(dentry_page);
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700548 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
549 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900550 if (bit_pos < NR_DENTRY_IN_BLOCK)
551 goto add_dentry;
552
553 kunmap(dentry_page);
554 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900555 }
556
557 /* Move to next level to find the empty slot for new dentry */
558 ++level;
559 goto start;
560add_dentry:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900561 f2fs_wait_on_page_writeback(dentry_page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900562
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700563 if (inode) {
564 down_write(&F2FS_I(inode)->i_sem);
565 page = init_inode_metadata(inode, dir, name, NULL);
566 if (IS_ERR(page)) {
567 err = PTR_ERR(page);
568 goto fail;
569 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900570 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800571
572 make_dentry_ptr(&d, (void *)dentry_blk, 1);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700573 f2fs_update_dentry(ino, mode, &d, name, dentry_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800574
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900575 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900576
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700577 if (inode) {
578 /* we don't need to mark_inode_dirty now */
579 F2FS_I(inode)->i_pino = dir->i_ino;
580 update_inode(inode, page);
581 f2fs_put_page(page, 1);
582 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900583
584 update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900585fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700586 if (inode)
587 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900588
Jaegeuk Kim924a2dd2014-02-03 17:24:51 +0900589 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
590 update_inode_page(dir);
591 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
592 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900593 kunmap(dentry_page);
594 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900595 return err;
596}
597
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700598int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
599{
600 struct page *page;
601 int err = 0;
602
603 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700604 page = init_inode_metadata(inode, dir, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700605 if (IS_ERR(page)) {
606 err = PTR_ERR(page);
607 goto fail;
608 }
609 /* we don't need to mark_inode_dirty now */
610 update_inode(inode, page);
611 f2fs_put_page(page, 1);
612
613 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
614fail:
615 up_write(&F2FS_I(inode)->i_sem);
616 return err;
617}
618
Chao Yudbeacf02014-09-24 18:17:04 +0800619void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
620{
621 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
622
623 down_write(&F2FS_I(inode)->i_sem);
624
625 if (S_ISDIR(inode->i_mode)) {
626 drop_nlink(dir);
627 if (page)
628 update_inode(dir, page);
629 else
630 update_inode_page(dir);
631 }
632 inode->i_ctime = CURRENT_TIME;
633
634 drop_nlink(inode);
635 if (S_ISDIR(inode->i_mode)) {
636 drop_nlink(inode);
637 i_size_write(inode, 0);
638 }
639 up_write(&F2FS_I(inode)->i_sem);
640 update_inode_page(inode);
641
642 if (inode->i_nlink == 0)
643 add_orphan_inode(sbi, inode->i_ino);
644 else
645 release_orphan_inode(sbi);
646}
647
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900648/*
arter97e1c42042014-08-06 23:22:50 +0900649 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900650 * entry in name page does not need to be touched during deletion.
651 */
652void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800653 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900654{
655 struct f2fs_dentry_block *dentry_blk;
656 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900657 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900658 int i;
659
Chao Yu622f28a2014-09-24 18:19:10 +0800660 if (f2fs_has_inline_dentry(dir))
661 return f2fs_delete_inline_entry(dentry, page, dir, inode);
662
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900663 lock_page(page);
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900664 f2fs_wait_on_page_writeback(page, DATA);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900665
Gu Zheng1c3bb972014-06-27 17:57:04 +0800666 dentry_blk = page_address(page);
667 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900668 for (i = 0; i < slots; i++)
Jan Kara1f7732f2014-10-23 14:40:20 +0200669 clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900670
671 /* Let's check and deallocate this dentry page */
672 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
673 NR_DENTRY_IN_BLOCK,
674 0);
675 kunmap(page); /* kunmap - pair of f2fs_find_entry */
676 set_page_dirty(page);
677
678 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
679
Chao Yudbeacf02014-09-24 18:17:04 +0800680 if (inode)
681 f2fs_drop_nlink(dir, inode, NULL);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900682
683 if (bit_pos == NR_DENTRY_IN_BLOCK) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900684 truncate_hole(dir, page->index, page->index + 1);
685 clear_page_dirty_for_io(page);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -0800686 ClearPagePrivate(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900687 ClearPageUptodate(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700688 inode_dec_dirty_pages(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900689 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900690 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900691}
692
693bool f2fs_empty_dir(struct inode *dir)
694{
695 unsigned long bidx;
696 struct page *dentry_page;
697 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800698 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900699 unsigned long nblock = dir_blocks(dir);
700
Chao Yu622f28a2014-09-24 18:19:10 +0800701 if (f2fs_has_inline_dentry(dir))
702 return f2fs_empty_inline_dir(dir);
703
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900704 for (bidx = 0; bidx < nblock; bidx++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900705 dentry_page = get_lock_data_page(dir, bidx);
706 if (IS_ERR(dentry_page)) {
707 if (PTR_ERR(dentry_page) == -ENOENT)
708 continue;
709 else
710 return false;
711 }
712
Gu Zheng1c3bb972014-06-27 17:57:04 +0800713 dentry_blk = kmap_atomic(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900714 if (bidx == 0)
715 bit_pos = 2;
716 else
717 bit_pos = 0;
718 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
719 NR_DENTRY_IN_BLOCK,
720 bit_pos);
Gu Zheng1c3bb972014-06-27 17:57:04 +0800721 kunmap_atomic(dentry_blk);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900722
723 f2fs_put_page(dentry_page, 1);
724
725 if (bit_pos < NR_DENTRY_IN_BLOCK)
726 return false;
727 }
728 return true;
729}
730
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700731bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
732 unsigned int start_pos)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700733{
734 unsigned char d_type = DT_UNKNOWN;
735 unsigned int bit_pos;
736 struct f2fs_dir_entry *de = NULL;
737
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700738 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700739
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700740 while (bit_pos < d->max) {
741 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
742 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700743 break;
744
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700745 de = &d->dentry[bit_pos];
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700746 if (de->file_type < F2FS_FT_MAX)
747 d_type = f2fs_filetype_table[de->file_type];
748 else
749 d_type = DT_UNKNOWN;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700750 if (!dir_emit(ctx, d->filename[bit_pos],
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700751 le16_to_cpu(de->name_len),
752 le32_to_cpu(de->ino), d_type))
753 return true;
754
755 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
756 ctx->pos = start_pos + bit_pos;
757 }
758 return false;
759}
760
Al Viro6f7f2312013-05-17 18:02:17 -0400761static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900762{
Al Viro496ad9a2013-01-23 17:07:38 -0500763 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900764 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900765 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900766 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800767 struct file_ra_state *ra = &file->f_ra;
Al Viro6f7f2312013-05-17 18:02:17 -0400768 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700769 struct f2fs_dentry_ptr d;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900770
Chao Yu622f28a2014-09-24 18:19:10 +0800771 if (f2fs_has_inline_dentry(inode))
772 return f2fs_read_inline_dir(file, ctx);
773
Chao Yu817202d92014-04-28 17:59:43 +0800774 /* readahead for multi pages of dir */
775 if (npages - n > 1 && !ra_has_index(ra, n))
776 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
777 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
778
Chris Fries6c311ec2014-01-17 14:44:39 -0600779 for (; n < npages; n++) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900780 dentry_page = get_lock_data_page(inode, n);
781 if (IS_ERR(dentry_page))
782 continue;
783
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900784 dentry_blk = kmap(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900785
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700786 make_dentry_ptr(&d, (void *)dentry_blk, 1);
787
788 if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK))
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700789 goto stop;
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900790
Al Viro6f7f2312013-05-17 18:02:17 -0400791 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900792 kunmap(dentry_page);
793 f2fs_put_page(dentry_page, 1);
794 dentry_page = NULL;
795 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900796stop:
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900797 if (dentry_page && !IS_ERR(dentry_page)) {
798 kunmap(dentry_page);
799 f2fs_put_page(dentry_page, 1);
800 }
801
802 return 0;
803}
804
805const struct file_operations f2fs_dir_operations = {
806 .llseek = generic_file_llseek,
807 .read = generic_read_dir,
Al Viro6f7f2312013-05-17 18:02:17 -0400808 .iterate = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900809 .fsync = f2fs_sync_file,
810 .unlocked_ioctl = f2fs_ioctl,
811};