blob: 166f086233625167f041b150bcef43cc5751380b [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09003 * fs/f2fs/dir.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09007 */
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +00008#include <asm/unaligned.h>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09009#include <linux/fs.h>
10#include <linux/f2fs_fs.h>
Chao Yu4414dea2017-10-13 18:01:34 +080011#include <linux/sched/signal.h>
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -070012#include <linux/unicode.h>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090013#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"
Chao Yue97a3c42017-10-13 18:01:33 +080017#include <trace/events/f2fs.h>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090018
Christoph Hellwig5298d4b2022-01-18 07:56:14 +010019#if IS_ENABLED(CONFIG_UNICODE)
Chao Yu4d9a2bb2021-06-11 07:46:30 +080020extern struct kmem_cache *f2fs_cf_name_slab;
21#endif
22
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090023static unsigned long dir_blocks(struct inode *inode)
24{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030025 return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
26 >> PAGE_SHIFT;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090027}
28
Jaegeuk Kim38431542014-02-27 18:20:00 +090029static unsigned int dir_buckets(unsigned int level, int dir_level)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090030{
Chao Yubfec07d2014-05-28 08:56:09 +080031 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
Jaegeuk Kim38431542014-02-27 18:20:00 +090032 return 1 << (level + dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090033 else
Chao Yubfec07d2014-05-28 08:56:09 +080034 return MAX_DIR_BUCKETS;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090035}
36
37static unsigned int bucket_blocks(unsigned int level)
38{
39 if (level < MAX_DIR_HASH_DEPTH / 2)
40 return 2;
41 else
42 return 4;
43}
44
Chao Yuebfa7322016-09-18 23:30:03 +080045static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090046 [F2FS_FT_UNKNOWN] = DT_UNKNOWN,
47 [F2FS_FT_REG_FILE] = DT_REG,
48 [F2FS_FT_DIR] = DT_DIR,
49 [F2FS_FT_CHRDEV] = DT_CHR,
50 [F2FS_FT_BLKDEV] = DT_BLK,
51 [F2FS_FT_FIFO] = DT_FIFO,
52 [F2FS_FT_SOCK] = DT_SOCK,
53 [F2FS_FT_SYMLINK] = DT_LNK,
54};
55
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090056static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
57 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
58 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
59 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
60 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
61 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
62 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
63 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
64};
65
Chao Yu2e79d952018-05-30 00:20:40 +080066static void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090067{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090068 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
69}
70
Chao Yu4d57b862018-05-30 00:20:41 +080071unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
Chao Yu675f10b2016-02-22 18:29:18 +080072{
73 if (de->file_type < F2FS_FT_MAX)
74 return f2fs_filetype_table[de->file_type];
75 return DT_UNKNOWN;
76}
77
Eric Biggers43c780b2020-05-07 00:59:04 -070078/* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
79int f2fs_init_casefolded_name(const struct inode *dir,
80 struct f2fs_filename *fname)
81{
Christoph Hellwig5298d4b2022-01-18 07:56:14 +010082#if IS_ENABLED(CONFIG_UNICODE)
Daniel Rosenbergeca48732020-07-08 02:12:36 -070083 struct super_block *sb = dir->i_sb;
Eric Biggers43c780b2020-05-07 00:59:04 -070084
85 if (IS_CASEFOLDED(dir)) {
Chao Yu32410572021-08-09 08:24:48 +080086 fname->cf_name.name = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
87 GFP_NOFS, false, F2FS_SB(sb));
Eric Biggers43c780b2020-05-07 00:59:04 -070088 if (!fname->cf_name.name)
89 return -ENOMEM;
Daniel Rosenbergeca48732020-07-08 02:12:36 -070090 fname->cf_name.len = utf8_casefold(sb->s_encoding,
Eric Biggers43c780b2020-05-07 00:59:04 -070091 fname->usr_fname,
92 fname->cf_name.name,
93 F2FS_NAME_LEN);
94 if ((int)fname->cf_name.len <= 0) {
Chao Yu4d9a2bb2021-06-11 07:46:30 +080095 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
Eric Biggers43c780b2020-05-07 00:59:04 -070096 fname->cf_name.name = NULL;
Daniel Rosenbergeca48732020-07-08 02:12:36 -070097 if (sb_has_strict_encoding(sb))
Eric Biggers43c780b2020-05-07 00:59:04 -070098 return -EINVAL;
99 /* fall back to treating name as opaque byte sequence */
100 }
101 }
102#endif
103 return 0;
104}
105
106static int __f2fs_setup_filename(const struct inode *dir,
107 const struct fscrypt_name *crypt_name,
108 struct f2fs_filename *fname)
109{
110 int err;
111
112 memset(fname, 0, sizeof(*fname));
113
114 fname->usr_fname = crypt_name->usr_fname;
115 fname->disk_name = crypt_name->disk_name;
116#ifdef CONFIG_FS_ENCRYPTION
117 fname->crypto_buf = crypt_name->crypto_buf;
118#endif
Eric Biggers70fb2612020-09-23 21:26:23 -0700119 if (crypt_name->is_nokey_name) {
Eric Biggers43c780b2020-05-07 00:59:04 -0700120 /* hash was decoded from the no-key name */
121 fname->hash = cpu_to_le32(crypt_name->hash);
122 } else {
123 err = f2fs_init_casefolded_name(dir, fname);
124 if (err) {
125 f2fs_free_filename(fname);
126 return err;
127 }
128 f2fs_hash_filename(dir, fname);
129 }
130 return 0;
131}
132
133/*
134 * Prepare to search for @iname in @dir. This is similar to
135 * fscrypt_setup_filename(), but this also handles computing the casefolded name
136 * and the f2fs dirhash if needed, then packing all the information about this
137 * filename up into a 'struct f2fs_filename'.
138 */
139int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
140 int lookup, struct f2fs_filename *fname)
141{
142 struct fscrypt_name crypt_name;
143 int err;
144
145 err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
146 if (err)
147 return err;
148
149 return __f2fs_setup_filename(dir, &crypt_name, fname);
150}
151
152/*
153 * Prepare to look up @dentry in @dir. This is similar to
154 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
155 * and the f2fs dirhash if needed, then packing all the information about this
156 * filename up into a 'struct f2fs_filename'.
157 */
158int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
159 struct f2fs_filename *fname)
160{
161 struct fscrypt_name crypt_name;
162 int err;
163
164 err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
165 if (err)
166 return err;
167
168 return __f2fs_setup_filename(dir, &crypt_name, fname);
169}
170
171void f2fs_free_filename(struct f2fs_filename *fname)
172{
173#ifdef CONFIG_FS_ENCRYPTION
174 kfree(fname->crypto_buf.name);
175 fname->crypto_buf.name = NULL;
176#endif
Christoph Hellwig5298d4b2022-01-18 07:56:14 +0100177#if IS_ENABLED(CONFIG_UNICODE)
Chao Yu4d9a2bb2021-06-11 07:46:30 +0800178 if (fname->cf_name.name) {
179 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
180 fname->cf_name.name = NULL;
181 }
Eric Biggers43c780b2020-05-07 00:59:04 -0700182#endif
183}
184
Jaegeuk Kim38431542014-02-27 18:20:00 +0900185static unsigned long dir_block_index(unsigned int level,
186 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900187{
188 unsigned long i;
189 unsigned long bidx = 0;
190
191 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +0900192 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900193 bidx += idx * bucket_blocks(level);
194 return bidx;
195}
196
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700197static struct f2fs_dir_entry *find_in_block(struct inode *dir,
198 struct page *dentry_page,
Eric Biggers43c780b2020-05-07 00:59:04 -0700199 const struct f2fs_filename *fname,
Chao Yu17f930e2020-09-26 08:20:46 +0800200 int *max_slots)
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700201{
202 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700203 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700204
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800205 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700206
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700207 make_dentry_ptr_block(dir, &d, dentry_blk);
Chao Yu17f930e2020-09-26 08:20:46 +0800208 return f2fs_find_target_dentry(&d, fname, max_slots);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700209}
210
Christoph Hellwig5298d4b2022-01-18 07:56:14 +0100211#if IS_ENABLED(CONFIG_UNICODE)
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700212/*
213 * Test whether a case-insensitive directory entry matches the filename
214 * being searched for.
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000215 *
216 * Returns 1 for a match, 0 for no match, and -errno on an error.
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700217 */
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000218static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
Eric Biggers43c780b2020-05-07 00:59:04 -0700219 const u8 *de_name, u32 de_name_len)
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700220{
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700221 const struct super_block *sb = dir->i_sb;
222 const struct unicode_map *um = sb->s_encoding;
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000223 struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
Eric Biggers43c780b2020-05-07 00:59:04 -0700224 struct qstr entry = QSTR_INIT(de_name, de_name_len);
Eric Biggersf874fa12020-05-07 00:59:03 -0700225 int res;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700226
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000227 if (IS_ENCRYPTED(dir)) {
228 const struct fscrypt_str encrypted_name =
229 FSTR_INIT((u8 *)de_name, de_name_len);
230
231 if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir)))
232 return -EINVAL;
233
234 decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
235 if (!decrypted_name.name)
236 return -ENOMEM;
237 res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name,
238 &decrypted_name);
239 if (res < 0)
240 goto out;
241 entry.name = decrypted_name.name;
242 entry.len = decrypted_name.len;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700243 }
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000244
245 res = utf8_strncasecmp_folded(um, name, &entry);
246 /*
247 * In strict mode, ignore invalid names. In non-strict mode,
248 * fall back to treating them as opaque byte sequences.
249 */
250 if (res < 0 && !sb_has_strict_encoding(sb)) {
251 res = name->len == entry.len &&
252 memcmp(name->name, entry.name, name->len) == 0;
253 } else {
254 /* utf8_strncasecmp_folded returns 0 on match */
255 res = (res == 0);
256 }
257out:
258 kfree(decrypted_name.name);
259 return res;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700260}
Eric Biggers43c780b2020-05-07 00:59:04 -0700261#endif /* CONFIG_UNICODE */
Chao Yu950d47f2019-08-21 23:13:35 +0800262
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000263static inline int f2fs_match_name(const struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700264 const struct f2fs_filename *fname,
265 const u8 *de_name, u32 de_name_len)
Chao Yu950d47f2019-08-21 23:13:35 +0800266{
Eric Biggers43c780b2020-05-07 00:59:04 -0700267 struct fscrypt_name f;
Chao Yufe76a162019-08-21 23:13:34 +0800268
Christoph Hellwig5298d4b2022-01-18 07:56:14 +0100269#if IS_ENABLED(CONFIG_UNICODE)
Eric Biggers43c780b2020-05-07 00:59:04 -0700270 if (fname->cf_name.name) {
271 struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
Chao Yufe76a162019-08-21 23:13:34 +0800272
Eric Biggers43c780b2020-05-07 00:59:04 -0700273 return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
Chao Yu950d47f2019-08-21 23:13:35 +0800274 }
Chao Yufe76a162019-08-21 23:13:34 +0800275#endif
Eric Biggers43c780b2020-05-07 00:59:04 -0700276 f.usr_fname = fname->usr_fname;
277 f.disk_name = fname->disk_name;
278#ifdef CONFIG_FS_ENCRYPTION
279 f.crypto_buf = fname->crypto_buf;
280#endif
281 return fscrypt_match_name(&f, de_name, de_name_len);
Chao Yufe76a162019-08-21 23:13:34 +0800282}
283
Eric Biggers43c780b2020-05-07 00:59:04 -0700284struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
285 const struct f2fs_filename *fname, int *max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900286{
287 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900288 unsigned long bit_pos = 0;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900289 int max_len = 0;
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000290 int res = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900291
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700292 if (max_slots)
293 *max_slots = 0;
294 while (bit_pos < d->max) {
295 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900296 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800297 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900298 continue;
299 }
Chao Yubda19072015-03-09 17:33:16 +0800300
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700301 de = &d->dentry[bit_pos];
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700302
Chao Yua4a13f52016-04-27 22:22:20 +0800303 if (unlikely(!de->name_len)) {
304 bit_pos++;
305 continue;
306 }
307
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000308 if (de->hash_code == fname->hash) {
309 res = f2fs_match_name(d->inode, fname,
310 d->filename[bit_pos],
311 le16_to_cpu(de->name_len));
312 if (res < 0)
313 return ERR_PTR(res);
314 if (res)
315 goto found;
316 }
Eric Biggers1f73d492017-04-24 10:00:12 -0700317
Chao Yubda19072015-03-09 17:33:16 +0800318 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900319 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800320 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800321
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900322 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900323 }
324
325 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900326found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700327 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900328 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900329 return de;
330}
331
332static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700333 unsigned int level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700334 const struct f2fs_filename *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700335 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900336{
Eric Biggers43c780b2020-05-07 00:59:04 -0700337 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900338 unsigned int nbucket, nblock;
339 unsigned int bidx, end_block;
340 struct page *dentry_page;
341 struct f2fs_dir_entry *de = NULL;
342 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700343 int max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900344
Jaegeuk Kim38431542014-02-27 18:20:00 +0900345 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900346 nblock = bucket_blocks(level);
347
Jaegeuk Kim38431542014-02-27 18:20:00 +0900348 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700349 le32_to_cpu(fname->hash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900350 end_block = bidx + nblock;
351
352 for (; bidx < end_block; bidx++) {
353 /* no need to allocate new dentry pages to all the indices */
Chao Yu4d57b862018-05-30 00:20:41 +0800354 dentry_page = f2fs_find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900355 if (IS_ERR(dentry_page)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700356 if (PTR_ERR(dentry_page) == -ENOENT) {
357 room = true;
358 continue;
359 } else {
360 *res_page = dentry_page;
361 break;
362 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900363 }
364
Chao Yu17f930e2020-09-26 08:20:46 +0800365 de = find_in_block(dir, dentry_page, fname, &max_slots);
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000366 if (IS_ERR(de)) {
367 *res_page = ERR_CAST(de);
368 de = NULL;
369 break;
370 } else if (de) {
Chao Yu17f930e2020-09-26 08:20:46 +0800371 *res_page = dentry_page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900372 break;
Chao Yu17f930e2020-09-26 08:20:46 +0800373 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900374
375 if (max_slots >= s)
376 room = true;
377 f2fs_put_page(dentry_page, 0);
378 }
379
Eric Biggers43c780b2020-05-07 00:59:04 -0700380 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
381 F2FS_I(dir)->chash = fname->hash;
Sheng Yongd3bb9102017-04-22 10:39:20 +0800382 F2FS_I(dir)->clevel = level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900383 }
384
385 return de;
386}
387
Shuoran Liue7ba1082016-08-29 11:27:56 +0800388struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700389 const struct f2fs_filename *fname,
390 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900391{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900392 unsigned long npages = dir_blocks(dir);
393 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900394 unsigned int max_depth;
395 unsigned int level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900396
Chao Yu6d7ab882020-09-29 09:22:50 +0800397 *res_page = NULL;
398
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700399 if (f2fs_has_inline_dentry(dir)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800400 de = f2fs_find_in_inline_dir(dir, fname, res_page);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700401 goto out;
402 }
403
Chao Yu6d7ab882020-09-29 09:22:50 +0800404 if (npages == 0)
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700405 goto out;
406
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900407 max_depth = F2FS_I(dir)->i_current_depth;
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800408 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800409 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
410 dir->i_ino, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800411 max_depth = MAX_DIR_HASH_DEPTH;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700412 f2fs_i_depth_write(dir, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800413 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900414
415 for (level = 0; level < max_depth; level++) {
Shuoran Liue7ba1082016-08-29 11:27:56 +0800416 de = find_in_level(dir, level, fname, res_page);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700417 if (de || IS_ERR(*res_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900418 break;
419 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700420out:
Sheng Yongd3bb9102017-04-22 10:39:20 +0800421 /* This is to increase the speed of f2fs_create */
422 if (!de)
423 F2FS_I(dir)->task = current;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800424 return de;
425}
426
427/*
428 * Find an entry in the specified directory with the wanted name.
429 * It returns the page where the entry was found (as a parameter - res_page),
430 * and the entry itself. Page is returned mapped and unlocked.
431 * Entry is guaranteed to be valid.
432 */
433struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
434 const struct qstr *child, struct page **res_page)
435{
436 struct f2fs_dir_entry *de = NULL;
Eric Biggers43c780b2020-05-07 00:59:04 -0700437 struct f2fs_filename fname;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800438 int err;
439
Eric Biggers43c780b2020-05-07 00:59:04 -0700440 err = f2fs_setup_filename(dir, child, 1, &fname);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800441 if (err) {
Eric Biggers54475f52016-12-05 11:12:44 -0800442 if (err == -ENOENT)
443 *res_page = NULL;
444 else
445 *res_page = ERR_PTR(err);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800446 return NULL;
447 }
448
449 de = __f2fs_find_entry(dir, &fname, res_page);
450
Eric Biggers43c780b2020-05-07 00:59:04 -0700451 f2fs_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900452 return de;
453}
454
455struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
456{
Al Viro80e5d1ff52021-04-15 19:46:50 -0400457 return f2fs_find_entry(dir, &dotdot_name, p);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900458}
459
Linus Torvalds835c92d2016-08-06 09:49:02 -0400460ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
Chao Yu91246c22016-07-19 08:27:47 +0800461 struct page **page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900462{
463 ino_t res = 0;
464 struct f2fs_dir_entry *de;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900465
Chao Yu91246c22016-07-19 08:27:47 +0800466 de = f2fs_find_entry(dir, qstr, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900467 if (de) {
468 res = le32_to_cpu(de->ino);
Chao Yu91246c22016-07-19 08:27:47 +0800469 f2fs_put_page(*page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900470 }
471
472 return res;
473}
474
475void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
476 struct page *page, struct inode *inode)
477{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700478 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Yi Zhuang5f029c02021-04-06 09:47:35 +0800479
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900480 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800481 f2fs_wait_on_page_writeback(page, type, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900482 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700483 set_de_type(de, inode->i_mode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900484 set_page_dirty(page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900485
Deepa Dinamani078cd822016-09-14 07:48:04 -0700486 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700487 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900488 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900489}
490
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000491static void init_dent_inode(struct inode *dir, struct inode *inode,
492 const struct f2fs_filename *fname,
Eric Biggers43c780b2020-05-07 00:59:04 -0700493 struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900494{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900495 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900496
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000497 if (!fname) /* tmpfile case? */
498 return;
499
Chao Yubae0ee72018-12-25 17:43:42 +0800500 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900501
Al Viro53dc9a62013-01-25 16:01:21 -0500502 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900503 ri = F2FS_INODE(ipage);
Eric Biggers43c780b2020-05-07 00:59:04 -0700504 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
505 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000506 if (IS_ENCRYPTED(dir)) {
507 file_set_enc_name(inode);
508 /*
509 * Roll-forward recovery doesn't have encryption keys available,
510 * so it can't compute the dirhash for encrypted+casefolded
511 * filenames. Append it to i_name if possible. Else, disable
512 * roll-forward recovery of the dentry (i.e., make fsync'ing the
513 * file force a checkpoint) by setting LOST_PINO.
514 */
515 if (IS_CASEFOLDED(dir)) {
516 if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
517 F2FS_NAME_LEN)
518 put_unaligned(fname->hash, (f2fs_hash_t *)
519 &ri->i_name[fname->disk_name.len]);
520 else
521 file_lost_pino(inode);
522 }
523 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900524 set_page_dirty(ipage);
525}
526
Chao Yu4d57b862018-05-30 00:20:41 +0800527void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700528 struct f2fs_dentry_ptr *d)
529{
Eric Biggers43c780b2020-05-07 00:59:04 -0700530 struct fscrypt_str dot = FSTR_INIT(".", 1);
531 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700532
Chao Yu291bf802016-03-09 22:07:28 +0800533 /* update dirent of "." */
534 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700535
Chao Yu291bf802016-03-09 22:07:28 +0800536 /* update dirent of ".." */
537 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700538}
539
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900540static int make_empty_dir(struct inode *inode,
541 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900542{
543 struct page *dentry_page;
544 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700545 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900546
Chao Yu622f28a2014-09-24 18:19:10 +0800547 if (f2fs_has_inline_dentry(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800548 return f2fs_make_empty_inline_dir(inode, parent, page);
Chao Yu622f28a2014-09-24 18:19:10 +0800549
Chao Yu4d57b862018-05-30 00:20:41 +0800550 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900551 if (IS_ERR(dentry_page))
552 return PTR_ERR(dentry_page);
553
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800554 dentry_blk = page_address(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900555
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300556 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +0800557 f2fs_do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900558
Jaegeuk Kim39936832012-11-22 16:21:29 +0900559 set_page_dirty(dentry_page);
560 f2fs_put_page(dentry_page, 1);
561 return 0;
562}
563
Chao Yu4d57b862018-05-30 00:20:41 +0800564struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700565 const struct f2fs_filename *fname, struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900566{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900567 struct page *page;
568 int err;
569
Jaegeuk Kim91942322016-05-20 10:13:22 -0700570 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800571 page = f2fs_new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900572 if (IS_ERR(page))
573 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900574
575 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700576 /* in order to handle error case */
577 get_page(page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900578 err = make_empty_dir(inode, dir, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700579 if (err) {
580 lock_page(page);
581 goto put_error;
582 }
583 put_page(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900584 }
585
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700586 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900587 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900588 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900589
Eric Biggers43c780b2020-05-07 00:59:04 -0700590 err = f2fs_init_security(inode, dir,
591 fname ? fname->usr_fname : NULL, page);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900592 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900593 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700594
Chao Yu8c7d4b52020-03-21 20:19:33 +0800595 if (IS_ENCRYPTED(inode)) {
Eric Biggerse075b692020-09-16 21:11:27 -0700596 err = fscrypt_set_context(inode, page);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700597 if (err)
598 goto put_error;
599 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900600 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800601 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900602 if (IS_ERR(page))
603 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900604 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900605
Daniel Rosenberg7ad08a52020-11-19 06:09:04 +0000606 init_dent_inode(dir, inode, fname, page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900607
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900608 /*
609 * This file should be checkpointed during fsync.
610 * We lost i_pino from now on.
611 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700612 if (is_inode_flag_set(inode, FI_INC_LINK)) {
Sheng Yongd58dfb72017-06-26 10:41:35 +0800613 if (!S_ISDIR(inode->i_mode))
614 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800615 /*
616 * If link the tmpfile to alias through linkat path,
617 * we should remove this inode from orphan list.
618 */
619 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800620 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700621 f2fs_i_links_write(inode, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900622 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900623 return page;
624
Jaegeuk Kima8865372013-12-27 17:04:17 +0900625put_error:
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700626 clear_nlink(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800627 f2fs_update_inode(inode, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700628 f2fs_put_page(page, 1);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900629 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900630}
631
Chao Yu4d57b862018-05-30 00:20:41 +0800632void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900633 unsigned int current_depth)
634{
Jaegeuk Kim91942322016-05-20 10:13:22 -0700635 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700636 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700637 f2fs_i_links_write(dir, true);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700638 clear_inode_flag(inode, FI_NEW_INODE);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900639 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700640 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700641 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900642
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700643 if (F2FS_I(dir)->i_current_depth != current_depth)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700644 f2fs_i_depth_write(dir, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900645
Jaegeuk Kim91942322016-05-20 10:13:22 -0700646 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
647 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900648}
649
Chao Yu4d57b862018-05-30 00:20:41 +0800650int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900651{
652 int bit_start = 0;
653 int zero_start, zero_end;
654next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700655 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
656 if (zero_start >= max_slots)
657 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900658
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700659 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900660 if (zero_end - zero_start >= slots)
661 return zero_start;
662
663 bit_start = zero_end + 1;
664
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700665 if (zero_end + 1 >= max_slots)
666 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900667 goto next;
668}
669
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800670bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
Eric Biggers43c780b2020-05-07 00:59:04 -0700671 const struct f2fs_filename *fname)
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800672{
673 struct f2fs_dentry_ptr d;
674 unsigned int bit_pos;
Eric Biggers43c780b2020-05-07 00:59:04 -0700675 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800676
677 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
678
679 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
680
681 return bit_pos < d.max;
682}
683
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700684void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Eric Biggers43c780b2020-05-07 00:59:04 -0700685 const struct fscrypt_str *name, f2fs_hash_t name_hash,
686 unsigned int bit_pos)
Chao Yu3b4d7322015-02-16 16:17:20 +0800687{
688 struct f2fs_dir_entry *de;
689 int slots = GET_DENTRY_SLOTS(name->len);
690 int i;
691
692 de = &d->dentry[bit_pos];
693 de->hash_code = name_hash;
694 de->name_len = cpu_to_le16(name->len);
695 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700696 de->ino = cpu_to_le32(ino);
697 set_de_type(de, mode);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800698 for (i = 0; i < slots; i++) {
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700699 __set_bit_le(bit_pos + i, (void *)d->bitmap);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800700 /* avoid wrong garbage data for readdir */
701 if (i)
702 (de + i)->name_len = 0;
703 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800704}
705
Eric Biggers43c780b2020-05-07 00:59:04 -0700706int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
707 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900708{
709 unsigned int bit_pos;
710 unsigned int level;
711 unsigned int current_depth;
712 unsigned long bidx, block;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900713 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900714 struct page *dentry_page = NULL;
715 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800716 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700717 struct page *page = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800718 int slots, err = 0;
Chao Yu622f28a2014-09-24 18:19:10 +0800719
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900720 level = 0;
Eric Biggers43c780b2020-05-07 00:59:04 -0700721 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700722
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900723 current_depth = F2FS_I(dir)->i_current_depth;
Eric Biggers43c780b2020-05-07 00:59:04 -0700724 if (F2FS_I(dir)->chash == fname->hash) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900725 level = F2FS_I(dir)->clevel;
726 F2FS_I(dir)->chash = 0;
727 }
728
729start:
Chao Yu55523512017-02-25 11:08:28 +0800730 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800731 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700732 return -ENOSPC;
Chao Yu55523512017-02-25 11:08:28 +0800733 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200734
Chao Yu675f10b2016-02-22 18:29:18 +0800735 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
736 return -ENOSPC;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900737
738 /* Increase the depth, if required */
739 if (level == current_depth)
740 ++current_depth;
741
Jaegeuk Kim38431542014-02-27 18:20:00 +0900742 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900743 nblock = bucket_blocks(level);
744
Jaegeuk Kim38431542014-02-27 18:20:00 +0900745 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700746 (le32_to_cpu(fname->hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900747
748 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800749 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
Chao Yu675f10b2016-02-22 18:29:18 +0800750 if (IS_ERR(dentry_page))
751 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900752
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800753 dentry_blk = page_address(dentry_page);
Chao Yu4d57b862018-05-30 00:20:41 +0800754 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700755 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900756 if (bit_pos < NR_DENTRY_IN_BLOCK)
757 goto add_dentry;
758
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900759 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900760 }
761
762 /* Move to next level to find the empty slot for new dentry */
763 ++level;
764 goto start;
765add_dentry:
Chao Yubae0ee72018-12-25 17:43:42 +0800766 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900767
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700768 if (inode) {
769 down_write(&F2FS_I(inode)->i_sem);
Eric Biggers43c780b2020-05-07 00:59:04 -0700770 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700771 if (IS_ERR(page)) {
772 err = PTR_ERR(page);
773 goto fail;
774 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900775 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800776
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300777 make_dentry_ptr_block(NULL, &d, dentry_blk);
Eric Biggers43c780b2020-05-07 00:59:04 -0700778 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
779 bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800780
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900781 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900782
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700783 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700784 f2fs_i_pino_write(inode, dir->i_ino);
Chao Yu98194032019-09-10 09:14:16 +0800785
786 /* synchronize inode page's data from inode cache */
787 if (is_inode_flag_set(inode, FI_NEW_INODE))
788 f2fs_update_inode(inode, page);
789
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700790 f2fs_put_page(page, 1);
791 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900792
Chao Yu4d57b862018-05-30 00:20:41 +0800793 f2fs_update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900794fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700795 if (inode)
796 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900797
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900798 f2fs_put_page(dentry_page, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800799
800 return err;
801}
802
Eric Biggers43c780b2020-05-07 00:59:04 -0700803int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
804 struct inode *inode, nid_t ino, umode_t mode)
Shuoran Liue7ba1082016-08-29 11:27:56 +0800805{
Shuoran Liue7ba1082016-08-29 11:27:56 +0800806 int err = -EAGAIN;
807
Shuoran Liue7ba1082016-08-29 11:27:56 +0800808 if (f2fs_has_inline_dentry(dir))
Eric Biggers43c780b2020-05-07 00:59:04 -0700809 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800810 if (err == -EAGAIN)
Eric Biggers43c780b2020-05-07 00:59:04 -0700811 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800812
813 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
814 return err;
815}
816
Chao Yu675f10b2016-02-22 18:29:18 +0800817/*
818 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
819 * f2fs_unlock_op().
820 */
Chao Yu4d57b862018-05-30 00:20:41 +0800821int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Chao Yu675f10b2016-02-22 18:29:18 +0800822 struct inode *inode, nid_t ino, umode_t mode)
823{
Eric Biggers43c780b2020-05-07 00:59:04 -0700824 struct f2fs_filename fname;
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800825 struct page *page = NULL;
826 struct f2fs_dir_entry *de = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800827 int err;
828
Eric Biggers43c780b2020-05-07 00:59:04 -0700829 err = f2fs_setup_filename(dir, name, 0, &fname);
Chao Yu675f10b2016-02-22 18:29:18 +0800830 if (err)
831 return err;
832
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800833 /*
Liu Songe5cc2c52020-06-25 20:40:11 +0800834 * An immature stackable filesystem shows a race condition between lookup
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800835 * and create. If we have same task when doing lookup and create, it's
836 * definitely fine as expected by VFS normally. Otherwise, let's just
837 * verify on-disk dentry one more time, which guarantees filesystem
838 * consistency more.
839 */
840 if (current != F2FS_I(dir)->task) {
841 de = __f2fs_find_entry(dir, &fname, &page);
842 F2FS_I(dir)->task = NULL;
843 }
844 if (de) {
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800845 f2fs_put_page(page, 0);
846 err = -EEXIST;
847 } else if (IS_ERR(page)) {
848 err = PTR_ERR(page);
849 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800850 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800851 }
Eric Biggers43c780b2020-05-07 00:59:04 -0700852 f2fs_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900853 return err;
854}
855
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700856int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
857{
858 struct page *page;
859 int err = 0;
860
861 down_write(&F2FS_I(inode)->i_sem);
Eric Biggers43c780b2020-05-07 00:59:04 -0700862 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700863 if (IS_ERR(page)) {
864 err = PTR_ERR(page);
865 goto fail;
866 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700867 f2fs_put_page(page, 1);
868
Jaegeuk Kim91942322016-05-20 10:13:22 -0700869 clear_inode_flag(inode, FI_NEW_INODE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530870 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700871fail:
872 up_write(&F2FS_I(inode)->i_sem);
873 return err;
874}
875
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700876void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
Chao Yudbeacf02014-09-24 18:17:04 +0800877{
878 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
879
880 down_write(&F2FS_I(inode)->i_sem);
881
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700882 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700883 f2fs_i_links_write(dir, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700884 inode->i_ctime = current_time(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800885
Jaegeuk Kima1961242016-05-20 09:43:20 -0700886 f2fs_i_links_write(inode, false);
Chao Yudbeacf02014-09-24 18:17:04 +0800887 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700888 f2fs_i_links_write(inode, false);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700889 f2fs_i_size_write(inode, 0);
Chao Yudbeacf02014-09-24 18:17:04 +0800890 }
891 up_write(&F2FS_I(inode)->i_sem);
Chao Yudbeacf02014-09-24 18:17:04 +0800892
893 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800894 f2fs_add_orphan_inode(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800895 else
Chao Yu4d57b862018-05-30 00:20:41 +0800896 f2fs_release_orphan_inode(sbi);
Chao Yudbeacf02014-09-24 18:17:04 +0800897}
898
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900899/*
arter97e1c42042014-08-06 23:22:50 +0900900 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900901 * entry in name page does not need to be touched during deletion.
902 */
903void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800904 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900905{
906 struct f2fs_dentry_block *dentry_blk;
907 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900908 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900909 int i;
910
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800911 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
912
Chao Yu63189b72018-03-08 14:22:56 +0800913 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
Chao Yu4d57b862018-05-30 00:20:41 +0800914 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800915
Chao Yu622f28a2014-09-24 18:19:10 +0800916 if (f2fs_has_inline_dentry(dir))
917 return f2fs_delete_inline_entry(dentry, page, dir, inode);
918
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900919 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800920 f2fs_wait_on_page_writeback(page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900921
Gu Zheng1c3bb972014-06-27 17:57:04 +0800922 dentry_blk = page_address(page);
923 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900924 for (i = 0; i < slots; i++)
Jaegeuk Kim23380b82017-03-07 14:11:06 -0800925 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900926
927 /* Let's check and deallocate this dentry page */
928 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
929 NR_DENTRY_IN_BLOCK,
930 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900931 set_page_dirty(page);
932
Chao Yu206e61b2015-08-12 17:48:21 +0800933 if (bit_pos == NR_DENTRY_IN_BLOCK &&
Chao Yu4d57b862018-05-30 00:20:41 +0800934 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500935 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900936 clear_page_dirty_for_io(page);
937 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +0800938
939 clear_page_private_gcing(page);
940
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700941 inode_dec_dirty_pages(dir);
Chao Yu4d57b862018-05-30 00:20:41 +0800942 f2fs_remove_dirty_inode(dir);
Chao Yub763f3b2021-04-28 17:20:31 +0800943
944 detach_page_private(page);
945 set_page_private(page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900946 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900947 f2fs_put_page(page, 1);
Chao Yu9995e402020-03-20 18:17:54 +0800948
949 dir->i_ctime = dir->i_mtime = current_time(dir);
950 f2fs_mark_inode_dirty_sync(dir, false);
951
952 if (inode)
953 f2fs_drop_nlink(dir, inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900954}
955
956bool f2fs_empty_dir(struct inode *dir)
957{
958 unsigned long bidx;
959 struct page *dentry_page;
960 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800961 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900962 unsigned long nblock = dir_blocks(dir);
963
Chao Yu622f28a2014-09-24 18:19:10 +0800964 if (f2fs_has_inline_dentry(dir))
965 return f2fs_empty_inline_dir(dir);
966
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900967 for (bidx = 0; bidx < nblock; bidx++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800968 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900969 if (IS_ERR(dentry_page)) {
970 if (PTR_ERR(dentry_page) == -ENOENT)
971 continue;
972 else
973 return false;
974 }
975
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800976 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900977 if (bidx == 0)
978 bit_pos = 2;
979 else
980 bit_pos = 0;
981 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
982 NR_DENTRY_IN_BLOCK,
983 bit_pos);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900984
985 f2fs_put_page(dentry_page, 1);
986
987 if (bit_pos < NR_DENTRY_IN_BLOCK)
988 return false;
989 }
990 return true;
991}
992
Chao Yued6bd4b2016-10-29 18:46:34 +0800993int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700994 unsigned int start_pos, struct fscrypt_str *fstr)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700995{
996 unsigned char d_type = DT_UNKNOWN;
997 unsigned int bit_pos;
998 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700999 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
Sheng Yongf6df8f22017-11-22 18:23:38 +08001000 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
Chao Yue1293bd2018-09-07 19:49:07 +08001001 struct blk_plug plug;
1002 bool readdir_ra = sbi->readdir_ra == 1;
Yangtao Lid4bf15a2021-08-04 11:29:46 +08001003 bool found_valid_dirent = false;
Chao Yue1293bd2018-09-07 19:49:07 +08001004 int err = 0;
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001005
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001006 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001007
Chao Yue1293bd2018-09-07 19:49:07 +08001008 if (readdir_ra)
1009 blk_start_plug(&plug);
1010
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001011 while (bit_pos < d->max) {
1012 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
1013 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001014 break;
1015
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001016 de = &d->dentry[bit_pos];
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -08001017 if (de->name_len == 0) {
Yangtao Lid4bf15a2021-08-04 11:29:46 +08001018 if (found_valid_dirent || !bit_pos) {
1019 printk_ratelimited(
1020 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
1021 KERN_WARNING, sbi->sb->s_id,
1022 le32_to_cpu(de->ino));
1023 set_sbi_flag(sbi, SBI_NEED_FSCK);
1024 }
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -08001025 bit_pos++;
1026 ctx->pos = start_pos + bit_pos;
1027 continue;
1028 }
1029
Chao Yu4d57b862018-05-30 00:20:41 +08001030 d_type = f2fs_get_de_type(de);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001031
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001032 de_name.name = d->filename[bit_pos];
1033 de_name.len = le16_to_cpu(de->name_len);
1034
Jaegeuk Kim4e240d12018-11-14 12:40:30 -08001035 /* check memory boundary before moving forward */
1036 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Sheng Yong720db062019-01-07 15:02:34 +08001037 if (unlikely(bit_pos > d->max ||
1038 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001039 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1040 __func__, le16_to_cpu(de->name_len));
Jaegeuk Kim4e240d12018-11-14 12:40:30 -08001041 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu10f966b2019-06-20 11:36:14 +08001042 err = -EFSCORRUPTED;
Jaegeuk Kim4e240d12018-11-14 12:40:30 -08001043 goto out;
1044 }
1045
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301046 if (IS_ENCRYPTED(d->inode)) {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001047 int save_len = fstr->len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001048
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001049 err = fscrypt_fname_disk_to_usr(d->inode,
Chao Yu36af5f42019-05-28 17:23:33 +08001050 (u32)le32_to_cpu(de->hash_code),
1051 0, &de_name, fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001052 if (err)
Chao Yue1293bd2018-09-07 19:49:07 +08001053 goto out;
Jaegeuk Kim569cf182015-09-03 13:38:23 -07001054
1055 de_name = *fstr;
1056 fstr->len = save_len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001057 }
1058
1059 if (!dir_emit(ctx, de_name.name, de_name.len,
Chao Yue1293bd2018-09-07 19:49:07 +08001060 le32_to_cpu(de->ino), d_type)) {
1061 err = 1;
1062 goto out;
1063 }
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001064
Chao Yue1293bd2018-09-07 19:49:07 +08001065 if (readdir_ra)
Chao Yu4d57b862018-05-30 00:20:41 +08001066 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
Sheng Yongf6df8f22017-11-22 18:23:38 +08001067
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001068 ctx->pos = start_pos + bit_pos;
Yangtao Lid4bf15a2021-08-04 11:29:46 +08001069 found_valid_dirent = true;
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001070 }
Chao Yue1293bd2018-09-07 19:49:07 +08001071out:
1072 if (readdir_ra)
1073 blk_finish_plug(&plug);
1074 return err;
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001075}
1076
Al Viro6f7f2312013-05-17 18:02:17 -04001077static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001078{
Al Viro496ad9a2013-01-23 17:07:38 -05001079 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001080 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001081 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001082 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +08001083 struct file_ra_state *ra = &file->f_ra;
Chao Yue97a3c42017-10-13 18:01:33 +08001084 loff_t start_pos = ctx->pos;
Al Viro6f7f2312013-05-17 18:02:17 -04001085 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001086 struct f2fs_dentry_ptr d;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001087 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001088 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001089
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301090 if (IS_ENCRYPTED(inode)) {
Eric Biggersec0caa972020-12-02 18:20:37 -08001091 err = fscrypt_prepare_readdir(inode);
Eric Biggers3b1ada552019-12-09 13:23:48 -08001092 if (err)
Chao Yue97a3c42017-10-13 18:01:33 +08001093 goto out;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -07001094
Jeff Layton8b10fe62020-08-10 10:21:39 -04001095 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001096 if (err < 0)
Chao Yue97a3c42017-10-13 18:01:33 +08001097 goto out;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001098 }
1099
1100 if (f2fs_has_inline_dentry(inode)) {
1101 err = f2fs_read_inline_dir(file, ctx, &fstr);
Chao Yue97a3c42017-10-13 18:01:33 +08001102 goto out_free;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001103 }
Chao Yu622f28a2014-09-24 18:19:10 +08001104
Chao Yuab383be2017-10-13 18:01:36 +08001105 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
Chao Yu4414dea2017-10-13 18:01:34 +08001106
1107 /* allow readdir() to be interrupted */
1108 if (fatal_signal_pending(current)) {
1109 err = -ERESTARTSYS;
1110 goto out_free;
1111 }
1112 cond_resched();
1113
Chao Yucb7a8442017-10-13 18:01:35 +08001114 /* readahead for multi pages of dir */
1115 if (npages - n > 1 && !ra_has_index(ra, n))
1116 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1117 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1118
Gao Xiang613f3dc2019-02-21 12:57:35 +08001119 dentry_page = f2fs_find_data_page(inode, n);
Chao Yu57b62d22015-11-19 16:09:07 +08001120 if (IS_ERR(dentry_page)) {
1121 err = PTR_ERR(dentry_page);
Chao Yued6bd4b2016-10-29 18:46:34 +08001122 if (err == -ENOENT) {
1123 err = 0;
Chao Yu57b62d22015-11-19 16:09:07 +08001124 continue;
Chao Yued6bd4b2016-10-29 18:46:34 +08001125 } else {
Chao Yue97a3c42017-10-13 18:01:33 +08001126 goto out_free;
Chao Yued6bd4b2016-10-29 18:46:34 +08001127 }
Chao Yu57b62d22015-11-19 16:09:07 +08001128 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001129
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001130 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001131
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +03001132 make_dentry_ptr_block(inode, &d, dentry_blk);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001133
Chao Yued6bd4b2016-10-29 18:46:34 +08001134 err = f2fs_fill_dentries(ctx, &d,
1135 n * NR_DENTRY_IN_BLOCK, &fstr);
1136 if (err) {
Gao Xiang613f3dc2019-02-21 12:57:35 +08001137 f2fs_put_page(dentry_page, 0);
Chao Yue9837bc2015-12-01 11:41:50 +08001138 break;
1139 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +09001140
Gao Xiang613f3dc2019-02-21 12:57:35 +08001141 f2fs_put_page(dentry_page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001142 }
Chao Yue97a3c42017-10-13 18:01:33 +08001143out_free:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001144 fscrypt_fname_free_buffer(&fstr);
Chao Yue97a3c42017-10-13 18:01:33 +08001145out:
1146 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
Chao Yued6bd4b2016-10-29 18:46:34 +08001147 return err < 0 ? err : 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001148}
1149
1150const struct file_operations f2fs_dir_operations = {
1151 .llseek = generic_file_llseek,
1152 .read = generic_read_dir,
Al Viroe77d0c62016-05-10 16:41:13 -04001153 .iterate_shared = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001154 .fsync = f2fs_sync_file,
1155 .unlocked_ioctl = f2fs_ioctl,
hujianyang08b95122015-05-12 16:05:57 +08001156#ifdef CONFIG_COMPAT
1157 .compat_ioctl = f2fs_compat_ioctl,
1158#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001159};