blob: 2646964954408c7406b2d6451b453e2eccf39afb [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 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
Chao Yu4414dea2017-10-13 18:01:34 +080010#include <linux/sched/signal.h>
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -070011#include <linux/unicode.h>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090012#include "f2fs.h"
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +090013#include "node.h"
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090014#include "acl.h"
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090015#include "xattr.h"
Chao Yue97a3c42017-10-13 18:01:33 +080016#include <trace/events/f2fs.h>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090017
18static unsigned long dir_blocks(struct inode *inode)
19{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030020 return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
21 >> PAGE_SHIFT;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090022}
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 Yuebfa7322016-09-18 23:30:03 +080040static unsigned 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
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090051static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
52 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
53 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
54 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
55 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
56 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
57 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
58 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
59};
60
Chao Yu2e79d952018-05-30 00:20:40 +080061static void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090062{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090063 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
64}
65
Chao Yu4d57b862018-05-30 00:20:41 +080066unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
Chao Yu675f10b2016-02-22 18:29:18 +080067{
68 if (de->file_type < F2FS_FT_MAX)
69 return f2fs_filetype_table[de->file_type];
70 return DT_UNKNOWN;
71}
72
Eric Biggers43c780b2020-05-07 00:59:04 -070073/* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
74int f2fs_init_casefolded_name(const struct inode *dir,
75 struct f2fs_filename *fname)
76{
77#ifdef CONFIG_UNICODE
Daniel Rosenbergeca48732020-07-08 02:12:36 -070078 struct super_block *sb = dir->i_sb;
79 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Eric Biggers43c780b2020-05-07 00:59:04 -070080
81 if (IS_CASEFOLDED(dir)) {
82 fname->cf_name.name = f2fs_kmalloc(sbi, F2FS_NAME_LEN,
83 GFP_NOFS);
84 if (!fname->cf_name.name)
85 return -ENOMEM;
Daniel Rosenbergeca48732020-07-08 02:12:36 -070086 fname->cf_name.len = utf8_casefold(sb->s_encoding,
Eric Biggers43c780b2020-05-07 00:59:04 -070087 fname->usr_fname,
88 fname->cf_name.name,
89 F2FS_NAME_LEN);
90 if ((int)fname->cf_name.len <= 0) {
91 kfree(fname->cf_name.name);
92 fname->cf_name.name = NULL;
Daniel Rosenbergeca48732020-07-08 02:12:36 -070093 if (sb_has_strict_encoding(sb))
Eric Biggers43c780b2020-05-07 00:59:04 -070094 return -EINVAL;
95 /* fall back to treating name as opaque byte sequence */
96 }
97 }
98#endif
99 return 0;
100}
101
102static int __f2fs_setup_filename(const struct inode *dir,
103 const struct fscrypt_name *crypt_name,
104 struct f2fs_filename *fname)
105{
106 int err;
107
108 memset(fname, 0, sizeof(*fname));
109
110 fname->usr_fname = crypt_name->usr_fname;
111 fname->disk_name = crypt_name->disk_name;
112#ifdef CONFIG_FS_ENCRYPTION
113 fname->crypto_buf = crypt_name->crypto_buf;
114#endif
115 if (crypt_name->is_ciphertext_name) {
116 /* hash was decoded from the no-key name */
117 fname->hash = cpu_to_le32(crypt_name->hash);
118 } else {
119 err = f2fs_init_casefolded_name(dir, fname);
120 if (err) {
121 f2fs_free_filename(fname);
122 return err;
123 }
124 f2fs_hash_filename(dir, fname);
125 }
126 return 0;
127}
128
129/*
130 * Prepare to search for @iname in @dir. This is similar to
131 * fscrypt_setup_filename(), but this also handles computing the casefolded name
132 * and the f2fs dirhash if needed, then packing all the information about this
133 * filename up into a 'struct f2fs_filename'.
134 */
135int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
136 int lookup, struct f2fs_filename *fname)
137{
138 struct fscrypt_name crypt_name;
139 int err;
140
141 err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
142 if (err)
143 return err;
144
145 return __f2fs_setup_filename(dir, &crypt_name, fname);
146}
147
148/*
149 * Prepare to look up @dentry in @dir. This is similar to
150 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
151 * and the f2fs dirhash if needed, then packing all the information about this
152 * filename up into a 'struct f2fs_filename'.
153 */
154int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
155 struct f2fs_filename *fname)
156{
157 struct fscrypt_name crypt_name;
158 int err;
159
160 err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
161 if (err)
162 return err;
163
164 return __f2fs_setup_filename(dir, &crypt_name, fname);
165}
166
167void f2fs_free_filename(struct f2fs_filename *fname)
168{
169#ifdef CONFIG_FS_ENCRYPTION
170 kfree(fname->crypto_buf.name);
171 fname->crypto_buf.name = NULL;
172#endif
173#ifdef CONFIG_UNICODE
174 kfree(fname->cf_name.name);
175 fname->cf_name.name = NULL;
176#endif
177}
178
Jaegeuk Kim38431542014-02-27 18:20:00 +0900179static unsigned long dir_block_index(unsigned int level,
180 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900181{
182 unsigned long i;
183 unsigned long bidx = 0;
184
185 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +0900186 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900187 bidx += idx * bucket_blocks(level);
188 return bidx;
189}
190
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700191static struct f2fs_dir_entry *find_in_block(struct inode *dir,
192 struct page *dentry_page,
Eric Biggers43c780b2020-05-07 00:59:04 -0700193 const struct f2fs_filename *fname,
Chao Yu17f930e2020-09-26 08:20:46 +0800194 int *max_slots)
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700195{
196 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700197 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700198
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800199 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700200
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700201 make_dentry_ptr_block(dir, &d, dentry_blk);
Chao Yu17f930e2020-09-26 08:20:46 +0800202 return f2fs_find_target_dentry(&d, fname, max_slots);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700203}
204
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700205#ifdef CONFIG_UNICODE
206/*
207 * Test whether a case-insensitive directory entry matches the filename
208 * being searched for.
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700209 */
Eric Biggersf874fa12020-05-07 00:59:03 -0700210static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
Eric Biggers43c780b2020-05-07 00:59:04 -0700211 const u8 *de_name, u32 de_name_len)
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700212{
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700213 const struct super_block *sb = dir->i_sb;
214 const struct unicode_map *um = sb->s_encoding;
Eric Biggers43c780b2020-05-07 00:59:04 -0700215 struct qstr entry = QSTR_INIT(de_name, de_name_len);
Eric Biggersf874fa12020-05-07 00:59:03 -0700216 int res;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700217
Eric Biggers43c780b2020-05-07 00:59:04 -0700218 res = utf8_strncasecmp_folded(um, name, &entry);
Eric Biggersf874fa12020-05-07 00:59:03 -0700219 if (res < 0) {
220 /*
221 * In strict mode, ignore invalid names. In non-strict mode,
222 * fall back to treating them as opaque byte sequences.
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700223 */
Daniel Rosenbergeca48732020-07-08 02:12:36 -0700224 if (sb_has_strict_encoding(sb) || name->len != entry.len)
Eric Biggersf874fa12020-05-07 00:59:03 -0700225 return false;
Eric Biggers43c780b2020-05-07 00:59:04 -0700226 return !memcmp(name->name, entry.name, name->len);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700227 }
Eric Biggersf874fa12020-05-07 00:59:03 -0700228 return res == 0;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700229}
Eric Biggers43c780b2020-05-07 00:59:04 -0700230#endif /* CONFIG_UNICODE */
Chao Yu950d47f2019-08-21 23:13:35 +0800231
Eric Biggers43c780b2020-05-07 00:59:04 -0700232static inline bool f2fs_match_name(const struct inode *dir,
233 const struct f2fs_filename *fname,
234 const u8 *de_name, u32 de_name_len)
Chao Yu950d47f2019-08-21 23:13:35 +0800235{
Eric Biggers43c780b2020-05-07 00:59:04 -0700236 struct fscrypt_name f;
Chao Yufe76a162019-08-21 23:13:34 +0800237
238#ifdef CONFIG_UNICODE
Eric Biggers43c780b2020-05-07 00:59:04 -0700239 if (fname->cf_name.name) {
240 struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
Chao Yufe76a162019-08-21 23:13:34 +0800241
Eric Biggers43c780b2020-05-07 00:59:04 -0700242 return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
Chao Yu950d47f2019-08-21 23:13:35 +0800243 }
Chao Yufe76a162019-08-21 23:13:34 +0800244#endif
Eric Biggers43c780b2020-05-07 00:59:04 -0700245 f.usr_fname = fname->usr_fname;
246 f.disk_name = fname->disk_name;
247#ifdef CONFIG_FS_ENCRYPTION
248 f.crypto_buf = fname->crypto_buf;
249#endif
250 return fscrypt_match_name(&f, de_name, de_name_len);
Chao Yufe76a162019-08-21 23:13:34 +0800251}
252
Eric Biggers43c780b2020-05-07 00:59:04 -0700253struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
254 const struct f2fs_filename *fname, int *max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900255{
256 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900257 unsigned long bit_pos = 0;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900258 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900259
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700260 if (max_slots)
261 *max_slots = 0;
262 while (bit_pos < d->max) {
263 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900264 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800265 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900266 continue;
267 }
Chao Yubda19072015-03-09 17:33:16 +0800268
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700269 de = &d->dentry[bit_pos];
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700270
Chao Yua4a13f52016-04-27 22:22:20 +0800271 if (unlikely(!de->name_len)) {
272 bit_pos++;
273 continue;
274 }
275
Eric Biggers43c780b2020-05-07 00:59:04 -0700276 if (de->hash_code == fname->hash &&
277 f2fs_match_name(d->inode, fname, d->filename[bit_pos],
278 le16_to_cpu(de->name_len)))
Chao Yufe76a162019-08-21 23:13:34 +0800279 goto found;
Eric Biggers1f73d492017-04-24 10:00:12 -0700280
Chao Yubda19072015-03-09 17:33:16 +0800281 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900282 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800283 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800284
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900285 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900286 }
287
288 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900289found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700290 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900291 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900292 return de;
293}
294
295static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700296 unsigned int level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700297 const struct f2fs_filename *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700298 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900299{
Eric Biggers43c780b2020-05-07 00:59:04 -0700300 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900301 unsigned int nbucket, nblock;
302 unsigned int bidx, end_block;
303 struct page *dentry_page;
304 struct f2fs_dir_entry *de = NULL;
305 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700306 int max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900307
Jaegeuk Kim38431542014-02-27 18:20:00 +0900308 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900309 nblock = bucket_blocks(level);
310
Jaegeuk Kim38431542014-02-27 18:20:00 +0900311 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700312 le32_to_cpu(fname->hash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900313 end_block = bidx + nblock;
314
315 for (; bidx < end_block; bidx++) {
316 /* no need to allocate new dentry pages to all the indices */
Chao Yu4d57b862018-05-30 00:20:41 +0800317 dentry_page = f2fs_find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900318 if (IS_ERR(dentry_page)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700319 if (PTR_ERR(dentry_page) == -ENOENT) {
320 room = true;
321 continue;
322 } else {
323 *res_page = dentry_page;
324 break;
325 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900326 }
327
Chao Yu17f930e2020-09-26 08:20:46 +0800328 de = find_in_block(dir, dentry_page, fname, &max_slots);
329 if (de) {
330 *res_page = dentry_page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900331 break;
Chao Yu17f930e2020-09-26 08:20:46 +0800332 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900333
334 if (max_slots >= s)
335 room = true;
336 f2fs_put_page(dentry_page, 0);
337 }
338
Eric Biggers43c780b2020-05-07 00:59:04 -0700339 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
340 F2FS_I(dir)->chash = fname->hash;
Sheng Yongd3bb9102017-04-22 10:39:20 +0800341 F2FS_I(dir)->clevel = level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900342 }
343
344 return de;
345}
346
Shuoran Liue7ba1082016-08-29 11:27:56 +0800347struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700348 const struct f2fs_filename *fname,
349 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900350{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900351 unsigned long npages = dir_blocks(dir);
352 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900353 unsigned int max_depth;
354 unsigned int level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900355
Chao Yu6d7ab882020-09-29 09:22:50 +0800356 *res_page = NULL;
357
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700358 if (f2fs_has_inline_dentry(dir)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800359 de = f2fs_find_in_inline_dir(dir, fname, res_page);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700360 goto out;
361 }
362
Chao Yu6d7ab882020-09-29 09:22:50 +0800363 if (npages == 0)
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700364 goto out;
365
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900366 max_depth = F2FS_I(dir)->i_current_depth;
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800367 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800368 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
369 dir->i_ino, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800370 max_depth = MAX_DIR_HASH_DEPTH;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700371 f2fs_i_depth_write(dir, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800372 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900373
374 for (level = 0; level < max_depth; level++) {
Shuoran Liue7ba1082016-08-29 11:27:56 +0800375 de = find_in_level(dir, level, fname, res_page);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700376 if (de || IS_ERR(*res_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900377 break;
378 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700379out:
Sheng Yongd3bb9102017-04-22 10:39:20 +0800380 /* This is to increase the speed of f2fs_create */
381 if (!de)
382 F2FS_I(dir)->task = current;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800383 return de;
384}
385
386/*
387 * Find an entry in the specified directory with the wanted name.
388 * It returns the page where the entry was found (as a parameter - res_page),
389 * and the entry itself. Page is returned mapped and unlocked.
390 * Entry is guaranteed to be valid.
391 */
392struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
393 const struct qstr *child, struct page **res_page)
394{
395 struct f2fs_dir_entry *de = NULL;
Eric Biggers43c780b2020-05-07 00:59:04 -0700396 struct f2fs_filename fname;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800397 int err;
398
Eric Biggers43c780b2020-05-07 00:59:04 -0700399 err = f2fs_setup_filename(dir, child, 1, &fname);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800400 if (err) {
Eric Biggers54475f52016-12-05 11:12:44 -0800401 if (err == -ENOENT)
402 *res_page = NULL;
403 else
404 *res_page = ERR_PTR(err);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800405 return NULL;
406 }
407
408 de = __f2fs_find_entry(dir, &fname, res_page);
409
Eric Biggers43c780b2020-05-07 00:59:04 -0700410 f2fs_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900411 return de;
412}
413
414struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
415{
Sheng Yong8be0fea2016-06-04 22:01:28 +0800416 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900417
Sheng Yong8be0fea2016-06-04 22:01:28 +0800418 return f2fs_find_entry(dir, &dotdot, p);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900419}
420
Linus Torvalds835c92d2016-08-06 09:49:02 -0400421ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
Chao Yu91246c22016-07-19 08:27:47 +0800422 struct page **page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900423{
424 ino_t res = 0;
425 struct f2fs_dir_entry *de;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900426
Chao Yu91246c22016-07-19 08:27:47 +0800427 de = f2fs_find_entry(dir, qstr, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900428 if (de) {
429 res = le32_to_cpu(de->ino);
Chao Yu91246c22016-07-19 08:27:47 +0800430 f2fs_put_page(*page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900431 }
432
433 return res;
434}
435
436void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
437 struct page *page, struct inode *inode)
438{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700439 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900440 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800441 f2fs_wait_on_page_writeback(page, type, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900442 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700443 set_de_type(de, inode->i_mode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900444 set_page_dirty(page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900445
Deepa Dinamani078cd822016-09-14 07:48:04 -0700446 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700447 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900448 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900449}
450
Eric Biggers43c780b2020-05-07 00:59:04 -0700451static void init_dent_inode(const struct f2fs_filename *fname,
452 struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900453{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900454 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900455
Chao Yubae0ee72018-12-25 17:43:42 +0800456 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900457
Al Viro53dc9a62013-01-25 16:01:21 -0500458 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900459 ri = F2FS_INODE(ipage);
Eric Biggers43c780b2020-05-07 00:59:04 -0700460 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
461 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900462 set_page_dirty(ipage);
463}
464
Chao Yu4d57b862018-05-30 00:20:41 +0800465void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700466 struct f2fs_dentry_ptr *d)
467{
Eric Biggers43c780b2020-05-07 00:59:04 -0700468 struct fscrypt_str dot = FSTR_INIT(".", 1);
469 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700470
Chao Yu291bf802016-03-09 22:07:28 +0800471 /* update dirent of "." */
472 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700473
Chao Yu291bf802016-03-09 22:07:28 +0800474 /* update dirent of ".." */
475 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700476}
477
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900478static int make_empty_dir(struct inode *inode,
479 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900480{
481 struct page *dentry_page;
482 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700483 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900484
Chao Yu622f28a2014-09-24 18:19:10 +0800485 if (f2fs_has_inline_dentry(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800486 return f2fs_make_empty_inline_dir(inode, parent, page);
Chao Yu622f28a2014-09-24 18:19:10 +0800487
Chao Yu4d57b862018-05-30 00:20:41 +0800488 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900489 if (IS_ERR(dentry_page))
490 return PTR_ERR(dentry_page);
491
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800492 dentry_blk = page_address(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900493
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300494 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +0800495 f2fs_do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900496
Jaegeuk Kim39936832012-11-22 16:21:29 +0900497 set_page_dirty(dentry_page);
498 f2fs_put_page(dentry_page, 1);
499 return 0;
500}
501
Chao Yu4d57b862018-05-30 00:20:41 +0800502struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700503 const struct f2fs_filename *fname, struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900504{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900505 struct page *page;
506 int err;
507
Jaegeuk Kim91942322016-05-20 10:13:22 -0700508 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800509 page = f2fs_new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900510 if (IS_ERR(page))
511 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900512
513 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700514 /* in order to handle error case */
515 get_page(page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900516 err = make_empty_dir(inode, dir, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700517 if (err) {
518 lock_page(page);
519 goto put_error;
520 }
521 put_page(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900522 }
523
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700524 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900525 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900526 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900527
Eric Biggers43c780b2020-05-07 00:59:04 -0700528 err = f2fs_init_security(inode, dir,
529 fname ? fname->usr_fname : NULL, page);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900530 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900531 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700532
Chao Yu8c7d4b52020-03-21 20:19:33 +0800533 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700534 err = fscrypt_inherit_context(dir, inode, page, false);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700535 if (err)
536 goto put_error;
537 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900538 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800539 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900540 if (IS_ERR(page))
541 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900542 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900543
Eric Biggers43c780b2020-05-07 00:59:04 -0700544 if (fname) {
545 init_dent_inode(fname, page);
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530546 if (IS_ENCRYPTED(dir))
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800547 file_set_enc_name(inode);
548 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900549
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900550 /*
551 * This file should be checkpointed during fsync.
552 * We lost i_pino from now on.
553 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700554 if (is_inode_flag_set(inode, FI_INC_LINK)) {
Sheng Yongd58dfb72017-06-26 10:41:35 +0800555 if (!S_ISDIR(inode->i_mode))
556 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800557 /*
558 * If link the tmpfile to alias through linkat path,
559 * we should remove this inode from orphan list.
560 */
561 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800562 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700563 f2fs_i_links_write(inode, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900564 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900565 return page;
566
Jaegeuk Kima8865372013-12-27 17:04:17 +0900567put_error:
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700568 clear_nlink(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800569 f2fs_update_inode(inode, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700570 f2fs_put_page(page, 1);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900571 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900572}
573
Chao Yu4d57b862018-05-30 00:20:41 +0800574void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900575 unsigned int current_depth)
576{
Jaegeuk Kim91942322016-05-20 10:13:22 -0700577 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700578 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700579 f2fs_i_links_write(dir, true);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700580 clear_inode_flag(inode, FI_NEW_INODE);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700582 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700583 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900584
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700585 if (F2FS_I(dir)->i_current_depth != current_depth)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700586 f2fs_i_depth_write(dir, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900587
Jaegeuk Kim91942322016-05-20 10:13:22 -0700588 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
589 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900590}
591
Chao Yu4d57b862018-05-30 00:20:41 +0800592int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900593{
594 int bit_start = 0;
595 int zero_start, zero_end;
596next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700597 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
598 if (zero_start >= max_slots)
599 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900600
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700601 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900602 if (zero_end - zero_start >= slots)
603 return zero_start;
604
605 bit_start = zero_end + 1;
606
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700607 if (zero_end + 1 >= max_slots)
608 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900609 goto next;
610}
611
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800612bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
Eric Biggers43c780b2020-05-07 00:59:04 -0700613 const struct f2fs_filename *fname)
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800614{
615 struct f2fs_dentry_ptr d;
616 unsigned int bit_pos;
Eric Biggers43c780b2020-05-07 00:59:04 -0700617 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800618
619 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
620
621 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
622
623 return bit_pos < d.max;
624}
625
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700626void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Eric Biggers43c780b2020-05-07 00:59:04 -0700627 const struct fscrypt_str *name, f2fs_hash_t name_hash,
628 unsigned int bit_pos)
Chao Yu3b4d7322015-02-16 16:17:20 +0800629{
630 struct f2fs_dir_entry *de;
631 int slots = GET_DENTRY_SLOTS(name->len);
632 int i;
633
634 de = &d->dentry[bit_pos];
635 de->hash_code = name_hash;
636 de->name_len = cpu_to_le16(name->len);
637 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700638 de->ino = cpu_to_le32(ino);
639 set_de_type(de, mode);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800640 for (i = 0; i < slots; i++) {
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700641 __set_bit_le(bit_pos + i, (void *)d->bitmap);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800642 /* avoid wrong garbage data for readdir */
643 if (i)
644 (de + i)->name_len = 0;
645 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800646}
647
Eric Biggers43c780b2020-05-07 00:59:04 -0700648int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
649 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900650{
651 unsigned int bit_pos;
652 unsigned int level;
653 unsigned int current_depth;
654 unsigned long bidx, block;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900655 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900656 struct page *dentry_page = NULL;
657 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800658 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700659 struct page *page = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800660 int slots, err = 0;
Chao Yu622f28a2014-09-24 18:19:10 +0800661
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900662 level = 0;
Eric Biggers43c780b2020-05-07 00:59:04 -0700663 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700664
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900665 current_depth = F2FS_I(dir)->i_current_depth;
Eric Biggers43c780b2020-05-07 00:59:04 -0700666 if (F2FS_I(dir)->chash == fname->hash) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900667 level = F2FS_I(dir)->clevel;
668 F2FS_I(dir)->chash = 0;
669 }
670
671start:
Chao Yu55523512017-02-25 11:08:28 +0800672 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800673 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700674 return -ENOSPC;
Chao Yu55523512017-02-25 11:08:28 +0800675 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200676
Chao Yu675f10b2016-02-22 18:29:18 +0800677 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
678 return -ENOSPC;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900679
680 /* Increase the depth, if required */
681 if (level == current_depth)
682 ++current_depth;
683
Jaegeuk Kim38431542014-02-27 18:20:00 +0900684 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900685 nblock = bucket_blocks(level);
686
Jaegeuk Kim38431542014-02-27 18:20:00 +0900687 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700688 (le32_to_cpu(fname->hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900689
690 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800691 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
Chao Yu675f10b2016-02-22 18:29:18 +0800692 if (IS_ERR(dentry_page))
693 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900694
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800695 dentry_blk = page_address(dentry_page);
Chao Yu4d57b862018-05-30 00:20:41 +0800696 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700697 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900698 if (bit_pos < NR_DENTRY_IN_BLOCK)
699 goto add_dentry;
700
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900701 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900702 }
703
704 /* Move to next level to find the empty slot for new dentry */
705 ++level;
706 goto start;
707add_dentry:
Chao Yubae0ee72018-12-25 17:43:42 +0800708 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900709
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700710 if (inode) {
711 down_write(&F2FS_I(inode)->i_sem);
Eric Biggers43c780b2020-05-07 00:59:04 -0700712 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700713 if (IS_ERR(page)) {
714 err = PTR_ERR(page);
715 goto fail;
716 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900717 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800718
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300719 make_dentry_ptr_block(NULL, &d, dentry_blk);
Eric Biggers43c780b2020-05-07 00:59:04 -0700720 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
721 bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800722
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900723 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900724
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700725 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700726 f2fs_i_pino_write(inode, dir->i_ino);
Chao Yu98194032019-09-10 09:14:16 +0800727
728 /* synchronize inode page's data from inode cache */
729 if (is_inode_flag_set(inode, FI_NEW_INODE))
730 f2fs_update_inode(inode, page);
731
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700732 f2fs_put_page(page, 1);
733 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900734
Chao Yu4d57b862018-05-30 00:20:41 +0800735 f2fs_update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900736fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700737 if (inode)
738 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900739
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900740 f2fs_put_page(dentry_page, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800741
742 return err;
743}
744
Eric Biggers43c780b2020-05-07 00:59:04 -0700745int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
746 struct inode *inode, nid_t ino, umode_t mode)
Shuoran Liue7ba1082016-08-29 11:27:56 +0800747{
Shuoran Liue7ba1082016-08-29 11:27:56 +0800748 int err = -EAGAIN;
749
Shuoran Liue7ba1082016-08-29 11:27:56 +0800750 if (f2fs_has_inline_dentry(dir))
Eric Biggers43c780b2020-05-07 00:59:04 -0700751 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800752 if (err == -EAGAIN)
Eric Biggers43c780b2020-05-07 00:59:04 -0700753 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800754
755 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
756 return err;
757}
758
Chao Yu675f10b2016-02-22 18:29:18 +0800759/*
760 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
761 * f2fs_unlock_op().
762 */
Chao Yu4d57b862018-05-30 00:20:41 +0800763int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Chao Yu675f10b2016-02-22 18:29:18 +0800764 struct inode *inode, nid_t ino, umode_t mode)
765{
Eric Biggers43c780b2020-05-07 00:59:04 -0700766 struct f2fs_filename fname;
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800767 struct page *page = NULL;
768 struct f2fs_dir_entry *de = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800769 int err;
770
Eric Biggers43c780b2020-05-07 00:59:04 -0700771 err = f2fs_setup_filename(dir, name, 0, &fname);
Chao Yu675f10b2016-02-22 18:29:18 +0800772 if (err)
773 return err;
774
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800775 /*
Liu Songe5cc2c52020-06-25 20:40:11 +0800776 * An immature stackable filesystem shows a race condition between lookup
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800777 * and create. If we have same task when doing lookup and create, it's
778 * definitely fine as expected by VFS normally. Otherwise, let's just
779 * verify on-disk dentry one more time, which guarantees filesystem
780 * consistency more.
781 */
782 if (current != F2FS_I(dir)->task) {
783 de = __f2fs_find_entry(dir, &fname, &page);
784 F2FS_I(dir)->task = NULL;
785 }
786 if (de) {
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800787 f2fs_put_page(page, 0);
788 err = -EEXIST;
789 } else if (IS_ERR(page)) {
790 err = PTR_ERR(page);
791 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800792 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800793 }
Eric Biggers43c780b2020-05-07 00:59:04 -0700794 f2fs_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900795 return err;
796}
797
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700798int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
799{
800 struct page *page;
801 int err = 0;
802
803 down_write(&F2FS_I(inode)->i_sem);
Eric Biggers43c780b2020-05-07 00:59:04 -0700804 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700805 if (IS_ERR(page)) {
806 err = PTR_ERR(page);
807 goto fail;
808 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700809 f2fs_put_page(page, 1);
810
Jaegeuk Kim91942322016-05-20 10:13:22 -0700811 clear_inode_flag(inode, FI_NEW_INODE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530812 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700813fail:
814 up_write(&F2FS_I(inode)->i_sem);
815 return err;
816}
817
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700818void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
Chao Yudbeacf02014-09-24 18:17:04 +0800819{
820 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
821
822 down_write(&F2FS_I(inode)->i_sem);
823
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700824 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700825 f2fs_i_links_write(dir, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700826 inode->i_ctime = current_time(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800827
Jaegeuk Kima1961242016-05-20 09:43:20 -0700828 f2fs_i_links_write(inode, false);
Chao Yudbeacf02014-09-24 18:17:04 +0800829 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700830 f2fs_i_links_write(inode, false);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700831 f2fs_i_size_write(inode, 0);
Chao Yudbeacf02014-09-24 18:17:04 +0800832 }
833 up_write(&F2FS_I(inode)->i_sem);
Chao Yudbeacf02014-09-24 18:17:04 +0800834
835 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800836 f2fs_add_orphan_inode(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800837 else
Chao Yu4d57b862018-05-30 00:20:41 +0800838 f2fs_release_orphan_inode(sbi);
Chao Yudbeacf02014-09-24 18:17:04 +0800839}
840
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900841/*
arter97e1c42042014-08-06 23:22:50 +0900842 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900843 * entry in name page does not need to be touched during deletion.
844 */
845void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800846 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900847{
848 struct f2fs_dentry_block *dentry_blk;
849 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900850 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900851 int i;
852
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800853 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
854
Chao Yu63189b72018-03-08 14:22:56 +0800855 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
Chao Yu4d57b862018-05-30 00:20:41 +0800856 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800857
Chao Yu622f28a2014-09-24 18:19:10 +0800858 if (f2fs_has_inline_dentry(dir))
859 return f2fs_delete_inline_entry(dentry, page, dir, inode);
860
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900861 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800862 f2fs_wait_on_page_writeback(page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900863
Gu Zheng1c3bb972014-06-27 17:57:04 +0800864 dentry_blk = page_address(page);
865 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900866 for (i = 0; i < slots; i++)
Jaegeuk Kim23380b82017-03-07 14:11:06 -0800867 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900868
869 /* Let's check and deallocate this dentry page */
870 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
871 NR_DENTRY_IN_BLOCK,
872 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900873 set_page_dirty(page);
874
Chao Yu206e61b2015-08-12 17:48:21 +0800875 if (bit_pos == NR_DENTRY_IN_BLOCK &&
Chao Yu4d57b862018-05-30 00:20:41 +0800876 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500877 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900878 clear_page_dirty_for_io(page);
Chao Yu240a5912019-03-06 17:30:59 +0800879 f2fs_clear_page_private(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900880 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800881 clear_cold_data(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700882 inode_dec_dirty_pages(dir);
Chao Yu4d57b862018-05-30 00:20:41 +0800883 f2fs_remove_dirty_inode(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900884 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900885 f2fs_put_page(page, 1);
Chao Yu9995e402020-03-20 18:17:54 +0800886
887 dir->i_ctime = dir->i_mtime = current_time(dir);
888 f2fs_mark_inode_dirty_sync(dir, false);
889
890 if (inode)
891 f2fs_drop_nlink(dir, inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900892}
893
894bool f2fs_empty_dir(struct inode *dir)
895{
896 unsigned long bidx;
897 struct page *dentry_page;
898 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800899 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900900 unsigned long nblock = dir_blocks(dir);
901
Chao Yu622f28a2014-09-24 18:19:10 +0800902 if (f2fs_has_inline_dentry(dir))
903 return f2fs_empty_inline_dir(dir);
904
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900905 for (bidx = 0; bidx < nblock; bidx++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800906 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900907 if (IS_ERR(dentry_page)) {
908 if (PTR_ERR(dentry_page) == -ENOENT)
909 continue;
910 else
911 return false;
912 }
913
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800914 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900915 if (bidx == 0)
916 bit_pos = 2;
917 else
918 bit_pos = 0;
919 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
920 NR_DENTRY_IN_BLOCK,
921 bit_pos);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900922
923 f2fs_put_page(dentry_page, 1);
924
925 if (bit_pos < NR_DENTRY_IN_BLOCK)
926 return false;
927 }
928 return true;
929}
930
Chao Yued6bd4b2016-10-29 18:46:34 +0800931int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700932 unsigned int start_pos, struct fscrypt_str *fstr)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700933{
934 unsigned char d_type = DT_UNKNOWN;
935 unsigned int bit_pos;
936 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700937 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
Sheng Yongf6df8f22017-11-22 18:23:38 +0800938 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
Chao Yue1293bd2018-09-07 19:49:07 +0800939 struct blk_plug plug;
940 bool readdir_ra = sbi->readdir_ra == 1;
941 int err = 0;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700942
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700943 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700944
Chao Yue1293bd2018-09-07 19:49:07 +0800945 if (readdir_ra)
946 blk_start_plug(&plug);
947
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700948 while (bit_pos < d->max) {
949 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
950 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700951 break;
952
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700953 de = &d->dentry[bit_pos];
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800954 if (de->name_len == 0) {
955 bit_pos++;
956 ctx->pos = start_pos + bit_pos;
Chao Yuddf06b72019-01-08 10:21:24 +0800957 printk_ratelimited(
Chao Yuc45d6002019-11-01 17:53:23 +0800958 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
959 KERN_WARNING, sbi->sb->s_id,
960 le32_to_cpu(de->ino));
Chao Yuddf06b72019-01-08 10:21:24 +0800961 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800962 continue;
963 }
964
Chao Yu4d57b862018-05-30 00:20:41 +0800965 d_type = f2fs_get_de_type(de);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700966
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700967 de_name.name = d->filename[bit_pos];
968 de_name.len = le16_to_cpu(de->name_len);
969
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800970 /* check memory boundary before moving forward */
971 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Sheng Yong720db062019-01-07 15:02:34 +0800972 if (unlikely(bit_pos > d->max ||
973 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800974 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
975 __func__, le16_to_cpu(de->name_len));
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800976 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu10f966b2019-06-20 11:36:14 +0800977 err = -EFSCORRUPTED;
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800978 goto out;
979 }
980
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530981 if (IS_ENCRYPTED(d->inode)) {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700982 int save_len = fstr->len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700983
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400984 err = fscrypt_fname_disk_to_usr(d->inode,
Chao Yu36af5f42019-05-28 17:23:33 +0800985 (u32)le32_to_cpu(de->hash_code),
986 0, &de_name, fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400987 if (err)
Chao Yue1293bd2018-09-07 19:49:07 +0800988 goto out;
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700989
990 de_name = *fstr;
991 fstr->len = save_len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700992 }
993
994 if (!dir_emit(ctx, de_name.name, de_name.len,
Chao Yue1293bd2018-09-07 19:49:07 +0800995 le32_to_cpu(de->ino), d_type)) {
996 err = 1;
997 goto out;
998 }
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700999
Chao Yue1293bd2018-09-07 19:49:07 +08001000 if (readdir_ra)
Chao Yu4d57b862018-05-30 00:20:41 +08001001 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
Sheng Yongf6df8f22017-11-22 18:23:38 +08001002
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001003 ctx->pos = start_pos + bit_pos;
1004 }
Chao Yue1293bd2018-09-07 19:49:07 +08001005out:
1006 if (readdir_ra)
1007 blk_finish_plug(&plug);
1008 return err;
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001009}
1010
Al Viro6f7f2312013-05-17 18:02:17 -04001011static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001012{
Al Viro496ad9a2013-01-23 17:07:38 -05001013 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001014 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001015 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001016 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +08001017 struct file_ra_state *ra = &file->f_ra;
Chao Yue97a3c42017-10-13 18:01:33 +08001018 loff_t start_pos = ctx->pos;
Al Viro6f7f2312013-05-17 18:02:17 -04001019 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001020 struct f2fs_dentry_ptr d;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001021 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001022 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001023
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301024 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001025 err = fscrypt_get_encryption_info(inode);
Eric Biggers3b1ada552019-12-09 13:23:48 -08001026 if (err)
Chao Yue97a3c42017-10-13 18:01:33 +08001027 goto out;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -07001028
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001029 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001030 if (err < 0)
Chao Yue97a3c42017-10-13 18:01:33 +08001031 goto out;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001032 }
1033
1034 if (f2fs_has_inline_dentry(inode)) {
1035 err = f2fs_read_inline_dir(file, ctx, &fstr);
Chao Yue97a3c42017-10-13 18:01:33 +08001036 goto out_free;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001037 }
Chao Yu622f28a2014-09-24 18:19:10 +08001038
Chao Yuab383be2017-10-13 18:01:36 +08001039 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
Chao Yu4414dea2017-10-13 18:01:34 +08001040
1041 /* allow readdir() to be interrupted */
1042 if (fatal_signal_pending(current)) {
1043 err = -ERESTARTSYS;
1044 goto out_free;
1045 }
1046 cond_resched();
1047
Chao Yucb7a8442017-10-13 18:01:35 +08001048 /* readahead for multi pages of dir */
1049 if (npages - n > 1 && !ra_has_index(ra, n))
1050 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1051 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1052
Gao Xiang613f3dc2019-02-21 12:57:35 +08001053 dentry_page = f2fs_find_data_page(inode, n);
Chao Yu57b62d22015-11-19 16:09:07 +08001054 if (IS_ERR(dentry_page)) {
1055 err = PTR_ERR(dentry_page);
Chao Yued6bd4b2016-10-29 18:46:34 +08001056 if (err == -ENOENT) {
1057 err = 0;
Chao Yu57b62d22015-11-19 16:09:07 +08001058 continue;
Chao Yued6bd4b2016-10-29 18:46:34 +08001059 } else {
Chao Yue97a3c42017-10-13 18:01:33 +08001060 goto out_free;
Chao Yued6bd4b2016-10-29 18:46:34 +08001061 }
Chao Yu57b62d22015-11-19 16:09:07 +08001062 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001063
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001064 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001065
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +03001066 make_dentry_ptr_block(inode, &d, dentry_blk);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001067
Chao Yued6bd4b2016-10-29 18:46:34 +08001068 err = f2fs_fill_dentries(ctx, &d,
1069 n * NR_DENTRY_IN_BLOCK, &fstr);
1070 if (err) {
Gao Xiang613f3dc2019-02-21 12:57:35 +08001071 f2fs_put_page(dentry_page, 0);
Chao Yue9837bc2015-12-01 11:41:50 +08001072 break;
1073 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +09001074
Gao Xiang613f3dc2019-02-21 12:57:35 +08001075 f2fs_put_page(dentry_page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001076 }
Chao Yue97a3c42017-10-13 18:01:33 +08001077out_free:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001078 fscrypt_fname_free_buffer(&fstr);
Chao Yue97a3c42017-10-13 18:01:33 +08001079out:
1080 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
Chao Yued6bd4b2016-10-29 18:46:34 +08001081 return err < 0 ? err : 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001082}
1083
Chao Yued3360a2016-02-14 18:56:55 +08001084static int f2fs_dir_open(struct inode *inode, struct file *filp)
1085{
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301086 if (IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001087 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
Chao Yued3360a2016-02-14 18:56:55 +08001088 return 0;
1089}
1090
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001091const struct file_operations f2fs_dir_operations = {
1092 .llseek = generic_file_llseek,
1093 .read = generic_read_dir,
Al Viroe77d0c62016-05-10 16:41:13 -04001094 .iterate_shared = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001095 .fsync = f2fs_sync_file,
Chao Yued3360a2016-02-14 18:56:55 +08001096 .open = f2fs_dir_open,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001097 .unlocked_ioctl = f2fs_ioctl,
hujianyang08b95122015-05-12 16:05:57 +08001098#ifdef CONFIG_COMPAT
1099 .compat_ioctl = f2fs_compat_ioctl,
1100#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001101};
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001102
1103#ifdef CONFIG_UNICODE
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001104const struct dentry_operations f2fs_dentry_ops = {
Daniel Rosenbergeca48732020-07-08 02:12:36 -07001105 .d_hash = generic_ci_d_hash,
1106 .d_compare = generic_ci_d_compare,
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001107};
1108#endif