blob: 069f498af1e38f642c944e13af59e8792c1955eb [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
78 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
79
80 if (IS_CASEFOLDED(dir)) {
81 fname->cf_name.name = f2fs_kmalloc(sbi, F2FS_NAME_LEN,
82 GFP_NOFS);
83 if (!fname->cf_name.name)
84 return -ENOMEM;
85 fname->cf_name.len = utf8_casefold(sbi->s_encoding,
86 fname->usr_fname,
87 fname->cf_name.name,
88 F2FS_NAME_LEN);
89 if ((int)fname->cf_name.len <= 0) {
90 kfree(fname->cf_name.name);
91 fname->cf_name.name = NULL;
92 if (f2fs_has_strict_mode(sbi))
93 return -EINVAL;
94 /* fall back to treating name as opaque byte sequence */
95 }
96 }
97#endif
98 return 0;
99}
100
101static int __f2fs_setup_filename(const struct inode *dir,
102 const struct fscrypt_name *crypt_name,
103 struct f2fs_filename *fname)
104{
105 int err;
106
107 memset(fname, 0, sizeof(*fname));
108
109 fname->usr_fname = crypt_name->usr_fname;
110 fname->disk_name = crypt_name->disk_name;
111#ifdef CONFIG_FS_ENCRYPTION
112 fname->crypto_buf = crypt_name->crypto_buf;
113#endif
114 if (crypt_name->is_ciphertext_name) {
115 /* hash was decoded from the no-key name */
116 fname->hash = cpu_to_le32(crypt_name->hash);
117 } else {
118 err = f2fs_init_casefolded_name(dir, fname);
119 if (err) {
120 f2fs_free_filename(fname);
121 return err;
122 }
123 f2fs_hash_filename(dir, fname);
124 }
125 return 0;
126}
127
128/*
129 * Prepare to search for @iname in @dir. This is similar to
130 * fscrypt_setup_filename(), but this also handles computing the casefolded name
131 * and the f2fs dirhash if needed, then packing all the information about this
132 * filename up into a 'struct f2fs_filename'.
133 */
134int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
135 int lookup, struct f2fs_filename *fname)
136{
137 struct fscrypt_name crypt_name;
138 int err;
139
140 err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
141 if (err)
142 return err;
143
144 return __f2fs_setup_filename(dir, &crypt_name, fname);
145}
146
147/*
148 * Prepare to look up @dentry in @dir. This is similar to
149 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
150 * and the f2fs dirhash if needed, then packing all the information about this
151 * filename up into a 'struct f2fs_filename'.
152 */
153int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
154 struct f2fs_filename *fname)
155{
156 struct fscrypt_name crypt_name;
157 int err;
158
159 err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
160 if (err)
161 return err;
162
163 return __f2fs_setup_filename(dir, &crypt_name, fname);
164}
165
166void f2fs_free_filename(struct f2fs_filename *fname)
167{
168#ifdef CONFIG_FS_ENCRYPTION
169 kfree(fname->crypto_buf.name);
170 fname->crypto_buf.name = NULL;
171#endif
172#ifdef CONFIG_UNICODE
173 kfree(fname->cf_name.name);
174 fname->cf_name.name = NULL;
175#endif
176}
177
Jaegeuk Kim38431542014-02-27 18:20:00 +0900178static unsigned long dir_block_index(unsigned int level,
179 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900180{
181 unsigned long i;
182 unsigned long bidx = 0;
183
184 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +0900185 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900186 bidx += idx * bucket_blocks(level);
187 return bidx;
188}
189
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700190static struct f2fs_dir_entry *find_in_block(struct inode *dir,
191 struct page *dentry_page,
Eric Biggers43c780b2020-05-07 00:59:04 -0700192 const struct f2fs_filename *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700193 int *max_slots,
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700194 struct page **res_page)
195{
196 struct f2fs_dentry_block *dentry_blk;
197 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700198 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700199
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800200 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700201
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700202 make_dentry_ptr_block(dir, &d, dentry_blk);
Eric Biggers43c780b2020-05-07 00:59:04 -0700203 de = f2fs_find_target_dentry(&d, fname, max_slots);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700204 if (de)
205 *res_page = dentry_page;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700206
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700207 return de;
208}
209
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700210#ifdef CONFIG_UNICODE
211/*
212 * Test whether a case-insensitive directory entry matches the filename
213 * being searched for.
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700214 */
Eric Biggersf874fa12020-05-07 00:59:03 -0700215static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
Eric Biggers43c780b2020-05-07 00:59:04 -0700216 const u8 *de_name, u32 de_name_len)
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700217{
Eric Biggersf874fa12020-05-07 00:59:03 -0700218 const struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700219 const struct unicode_map *um = sbi->s_encoding;
Eric Biggers43c780b2020-05-07 00:59:04 -0700220 struct qstr entry = QSTR_INIT(de_name, de_name_len);
Eric Biggersf874fa12020-05-07 00:59:03 -0700221 int res;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700222
Eric Biggers43c780b2020-05-07 00:59:04 -0700223 res = utf8_strncasecmp_folded(um, name, &entry);
Eric Biggersf874fa12020-05-07 00:59:03 -0700224 if (res < 0) {
225 /*
226 * In strict mode, ignore invalid names. In non-strict mode,
227 * fall back to treating them as opaque byte sequences.
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700228 */
Eric Biggers43c780b2020-05-07 00:59:04 -0700229 if (f2fs_has_strict_mode(sbi) || name->len != entry.len)
Eric Biggersf874fa12020-05-07 00:59:03 -0700230 return false;
Eric Biggers43c780b2020-05-07 00:59:04 -0700231 return !memcmp(name->name, entry.name, name->len);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700232 }
Eric Biggersf874fa12020-05-07 00:59:03 -0700233 return res == 0;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700234}
Eric Biggers43c780b2020-05-07 00:59:04 -0700235#endif /* CONFIG_UNICODE */
Chao Yu950d47f2019-08-21 23:13:35 +0800236
Eric Biggers43c780b2020-05-07 00:59:04 -0700237static inline bool f2fs_match_name(const struct inode *dir,
238 const struct f2fs_filename *fname,
239 const u8 *de_name, u32 de_name_len)
Chao Yu950d47f2019-08-21 23:13:35 +0800240{
Eric Biggers43c780b2020-05-07 00:59:04 -0700241 struct fscrypt_name f;
Chao Yufe76a162019-08-21 23:13:34 +0800242
243#ifdef CONFIG_UNICODE
Eric Biggers43c780b2020-05-07 00:59:04 -0700244 if (fname->cf_name.name) {
245 struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
Chao Yufe76a162019-08-21 23:13:34 +0800246
Eric Biggers43c780b2020-05-07 00:59:04 -0700247 return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
Chao Yu950d47f2019-08-21 23:13:35 +0800248 }
Chao Yufe76a162019-08-21 23:13:34 +0800249#endif
Eric Biggers43c780b2020-05-07 00:59:04 -0700250 f.usr_fname = fname->usr_fname;
251 f.disk_name = fname->disk_name;
252#ifdef CONFIG_FS_ENCRYPTION
253 f.crypto_buf = fname->crypto_buf;
254#endif
255 return fscrypt_match_name(&f, de_name, de_name_len);
Chao Yufe76a162019-08-21 23:13:34 +0800256}
257
Eric Biggers43c780b2020-05-07 00:59:04 -0700258struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
259 const struct f2fs_filename *fname, int *max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900260{
261 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900262 unsigned long bit_pos = 0;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900263 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900264
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700265 if (max_slots)
266 *max_slots = 0;
267 while (bit_pos < d->max) {
268 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900269 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800270 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900271 continue;
272 }
Chao Yubda19072015-03-09 17:33:16 +0800273
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700274 de = &d->dentry[bit_pos];
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700275
Chao Yua4a13f52016-04-27 22:22:20 +0800276 if (unlikely(!de->name_len)) {
277 bit_pos++;
278 continue;
279 }
280
Eric Biggers43c780b2020-05-07 00:59:04 -0700281 if (de->hash_code == fname->hash &&
282 f2fs_match_name(d->inode, fname, d->filename[bit_pos],
283 le16_to_cpu(de->name_len)))
Chao Yufe76a162019-08-21 23:13:34 +0800284 goto found;
Eric Biggers1f73d492017-04-24 10:00:12 -0700285
Chao Yubda19072015-03-09 17:33:16 +0800286 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900287 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800288 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800289
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900290 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900291 }
292
293 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900294found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700295 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900296 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900297 return de;
298}
299
300static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700301 unsigned int level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700302 const struct f2fs_filename *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700303 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900304{
Eric Biggers43c780b2020-05-07 00:59:04 -0700305 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900306 unsigned int nbucket, nblock;
307 unsigned int bidx, end_block;
308 struct page *dentry_page;
309 struct f2fs_dir_entry *de = NULL;
310 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700311 int max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900312
Jaegeuk Kim38431542014-02-27 18:20:00 +0900313 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900314 nblock = bucket_blocks(level);
315
Jaegeuk Kim38431542014-02-27 18:20:00 +0900316 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700317 le32_to_cpu(fname->hash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900318 end_block = bidx + nblock;
319
320 for (; bidx < end_block; bidx++) {
321 /* no need to allocate new dentry pages to all the indices */
Chao Yu4d57b862018-05-30 00:20:41 +0800322 dentry_page = f2fs_find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900323 if (IS_ERR(dentry_page)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700324 if (PTR_ERR(dentry_page) == -ENOENT) {
325 room = true;
326 continue;
327 } else {
328 *res_page = dentry_page;
329 break;
330 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900331 }
332
Eric Biggers43c780b2020-05-07 00:59:04 -0700333 de = find_in_block(dir, dentry_page, fname, &max_slots,
334 res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900335 if (de)
336 break;
337
338 if (max_slots >= s)
339 room = true;
340 f2fs_put_page(dentry_page, 0);
341 }
342
Eric Biggers43c780b2020-05-07 00:59:04 -0700343 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
344 F2FS_I(dir)->chash = fname->hash;
Sheng Yongd3bb9102017-04-22 10:39:20 +0800345 F2FS_I(dir)->clevel = level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900346 }
347
348 return de;
349}
350
Shuoran Liue7ba1082016-08-29 11:27:56 +0800351struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700352 const struct f2fs_filename *fname,
353 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900354{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900355 unsigned long npages = dir_blocks(dir);
356 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900357 unsigned int max_depth;
358 unsigned int level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900359
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700360 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700361 *res_page = NULL;
Chao Yu4d57b862018-05-30 00:20:41 +0800362 de = f2fs_find_in_inline_dir(dir, fname, res_page);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700363 goto out;
364 }
365
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700366 if (npages == 0) {
367 *res_page = NULL;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700368 goto out;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700369 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700370
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900371 max_depth = F2FS_I(dir)->i_current_depth;
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800372 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800373 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
374 dir->i_ino, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800375 max_depth = MAX_DIR_HASH_DEPTH;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700376 f2fs_i_depth_write(dir, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800377 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900378
379 for (level = 0; level < max_depth; level++) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700380 *res_page = NULL;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800381 de = find_in_level(dir, level, fname, res_page);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700382 if (de || IS_ERR(*res_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900383 break;
384 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700385out:
Sheng Yongd3bb9102017-04-22 10:39:20 +0800386 /* This is to increase the speed of f2fs_create */
387 if (!de)
388 F2FS_I(dir)->task = current;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800389 return de;
390}
391
392/*
393 * Find an entry in the specified directory with the wanted name.
394 * It returns the page where the entry was found (as a parameter - res_page),
395 * and the entry itself. Page is returned mapped and unlocked.
396 * Entry is guaranteed to be valid.
397 */
398struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
399 const struct qstr *child, struct page **res_page)
400{
401 struct f2fs_dir_entry *de = NULL;
Eric Biggers43c780b2020-05-07 00:59:04 -0700402 struct f2fs_filename fname;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800403 int err;
404
Eric Biggers43c780b2020-05-07 00:59:04 -0700405 err = f2fs_setup_filename(dir, child, 1, &fname);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800406 if (err) {
Eric Biggers54475f52016-12-05 11:12:44 -0800407 if (err == -ENOENT)
408 *res_page = NULL;
409 else
410 *res_page = ERR_PTR(err);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800411 return NULL;
412 }
413
414 de = __f2fs_find_entry(dir, &fname, res_page);
415
Eric Biggers43c780b2020-05-07 00:59:04 -0700416 f2fs_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900417 return de;
418}
419
420struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
421{
Sheng Yong8be0fea2016-06-04 22:01:28 +0800422 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900423
Sheng Yong8be0fea2016-06-04 22:01:28 +0800424 return f2fs_find_entry(dir, &dotdot, p);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900425}
426
Linus Torvalds835c92d2016-08-06 09:49:02 -0400427ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
Chao Yu91246c22016-07-19 08:27:47 +0800428 struct page **page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900429{
430 ino_t res = 0;
431 struct f2fs_dir_entry *de;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900432
Chao Yu91246c22016-07-19 08:27:47 +0800433 de = f2fs_find_entry(dir, qstr, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900434 if (de) {
435 res = le32_to_cpu(de->ino);
Chao Yu91246c22016-07-19 08:27:47 +0800436 f2fs_put_page(*page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900437 }
438
439 return res;
440}
441
442void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
443 struct page *page, struct inode *inode)
444{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700445 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900446 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800447 f2fs_wait_on_page_writeback(page, type, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900448 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700449 set_de_type(de, inode->i_mode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900450 set_page_dirty(page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900451
Deepa Dinamani078cd822016-09-14 07:48:04 -0700452 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700453 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900454 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900455}
456
Eric Biggers43c780b2020-05-07 00:59:04 -0700457static void init_dent_inode(const struct f2fs_filename *fname,
458 struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900459{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900460 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900461
Chao Yubae0ee72018-12-25 17:43:42 +0800462 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900463
Al Viro53dc9a62013-01-25 16:01:21 -0500464 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900465 ri = F2FS_INODE(ipage);
Eric Biggers43c780b2020-05-07 00:59:04 -0700466 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
467 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900468 set_page_dirty(ipage);
469}
470
Chao Yu4d57b862018-05-30 00:20:41 +0800471void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700472 struct f2fs_dentry_ptr *d)
473{
Eric Biggers43c780b2020-05-07 00:59:04 -0700474 struct fscrypt_str dot = FSTR_INIT(".", 1);
475 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700476
Chao Yu291bf802016-03-09 22:07:28 +0800477 /* update dirent of "." */
478 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700479
Chao Yu291bf802016-03-09 22:07:28 +0800480 /* update dirent of ".." */
481 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700482}
483
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900484static int make_empty_dir(struct inode *inode,
485 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900486{
487 struct page *dentry_page;
488 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700489 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900490
Chao Yu622f28a2014-09-24 18:19:10 +0800491 if (f2fs_has_inline_dentry(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800492 return f2fs_make_empty_inline_dir(inode, parent, page);
Chao Yu622f28a2014-09-24 18:19:10 +0800493
Chao Yu4d57b862018-05-30 00:20:41 +0800494 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900495 if (IS_ERR(dentry_page))
496 return PTR_ERR(dentry_page);
497
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800498 dentry_blk = page_address(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900499
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300500 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +0800501 f2fs_do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900502
Jaegeuk Kim39936832012-11-22 16:21:29 +0900503 set_page_dirty(dentry_page);
504 f2fs_put_page(dentry_page, 1);
505 return 0;
506}
507
Chao Yu4d57b862018-05-30 00:20:41 +0800508struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Eric Biggers43c780b2020-05-07 00:59:04 -0700509 const struct f2fs_filename *fname, struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900510{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900511 struct page *page;
512 int err;
513
Jaegeuk Kim91942322016-05-20 10:13:22 -0700514 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800515 page = f2fs_new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900516 if (IS_ERR(page))
517 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900518
519 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700520 /* in order to handle error case */
521 get_page(page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900522 err = make_empty_dir(inode, dir, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700523 if (err) {
524 lock_page(page);
525 goto put_error;
526 }
527 put_page(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900528 }
529
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700530 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900531 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900532 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900533
Eric Biggers43c780b2020-05-07 00:59:04 -0700534 err = f2fs_init_security(inode, dir,
535 fname ? fname->usr_fname : NULL, page);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900536 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900537 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700538
Chao Yu8c7d4b52020-03-21 20:19:33 +0800539 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700540 err = fscrypt_inherit_context(dir, inode, page, false);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700541 if (err)
542 goto put_error;
543 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900544 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800545 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900546 if (IS_ERR(page))
547 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900548 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900549
Eric Biggers43c780b2020-05-07 00:59:04 -0700550 if (fname) {
551 init_dent_inode(fname, page);
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530552 if (IS_ENCRYPTED(dir))
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800553 file_set_enc_name(inode);
554 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900555
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900556 /*
557 * This file should be checkpointed during fsync.
558 * We lost i_pino from now on.
559 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700560 if (is_inode_flag_set(inode, FI_INC_LINK)) {
Sheng Yongd58dfb72017-06-26 10:41:35 +0800561 if (!S_ISDIR(inode->i_mode))
562 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800563 /*
564 * If link the tmpfile to alias through linkat path,
565 * we should remove this inode from orphan list.
566 */
567 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800568 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700569 f2fs_i_links_write(inode, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900570 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900571 return page;
572
Jaegeuk Kima8865372013-12-27 17:04:17 +0900573put_error:
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700574 clear_nlink(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800575 f2fs_update_inode(inode, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700576 f2fs_put_page(page, 1);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900577 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900578}
579
Chao Yu4d57b862018-05-30 00:20:41 +0800580void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581 unsigned int current_depth)
582{
Jaegeuk Kim91942322016-05-20 10:13:22 -0700583 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700584 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700585 f2fs_i_links_write(dir, true);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700586 clear_inode_flag(inode, FI_NEW_INODE);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900587 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700588 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700589 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900590
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700591 if (F2FS_I(dir)->i_current_depth != current_depth)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700592 f2fs_i_depth_write(dir, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900593
Jaegeuk Kim91942322016-05-20 10:13:22 -0700594 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
595 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900596}
597
Chao Yu4d57b862018-05-30 00:20:41 +0800598int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900599{
600 int bit_start = 0;
601 int zero_start, zero_end;
602next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700603 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
604 if (zero_start >= max_slots)
605 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900606
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700607 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900608 if (zero_end - zero_start >= slots)
609 return zero_start;
610
611 bit_start = zero_end + 1;
612
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700613 if (zero_end + 1 >= max_slots)
614 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900615 goto next;
616}
617
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800618bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
Eric Biggers43c780b2020-05-07 00:59:04 -0700619 const struct f2fs_filename *fname)
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800620{
621 struct f2fs_dentry_ptr d;
622 unsigned int bit_pos;
Eric Biggers43c780b2020-05-07 00:59:04 -0700623 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kimb06af2a2019-12-09 19:03:05 -0800624
625 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
626
627 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
628
629 return bit_pos < d.max;
630}
631
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700632void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Eric Biggers43c780b2020-05-07 00:59:04 -0700633 const struct fscrypt_str *name, f2fs_hash_t name_hash,
634 unsigned int bit_pos)
Chao Yu3b4d7322015-02-16 16:17:20 +0800635{
636 struct f2fs_dir_entry *de;
637 int slots = GET_DENTRY_SLOTS(name->len);
638 int i;
639
640 de = &d->dentry[bit_pos];
641 de->hash_code = name_hash;
642 de->name_len = cpu_to_le16(name->len);
643 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700644 de->ino = cpu_to_le32(ino);
645 set_de_type(de, mode);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800646 for (i = 0; i < slots; i++) {
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700647 __set_bit_le(bit_pos + i, (void *)d->bitmap);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800648 /* avoid wrong garbage data for readdir */
649 if (i)
650 (de + i)->name_len = 0;
651 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800652}
653
Eric Biggers43c780b2020-05-07 00:59:04 -0700654int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
655 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900656{
657 unsigned int bit_pos;
658 unsigned int level;
659 unsigned int current_depth;
660 unsigned long bidx, block;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900661 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900662 struct page *dentry_page = NULL;
663 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800664 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700665 struct page *page = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800666 int slots, err = 0;
Chao Yu622f28a2014-09-24 18:19:10 +0800667
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900668 level = 0;
Eric Biggers43c780b2020-05-07 00:59:04 -0700669 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700670
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900671 current_depth = F2FS_I(dir)->i_current_depth;
Eric Biggers43c780b2020-05-07 00:59:04 -0700672 if (F2FS_I(dir)->chash == fname->hash) {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900673 level = F2FS_I(dir)->clevel;
674 F2FS_I(dir)->chash = 0;
675 }
676
677start:
Chao Yu55523512017-02-25 11:08:28 +0800678 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800679 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700680 return -ENOSPC;
Chao Yu55523512017-02-25 11:08:28 +0800681 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200682
Chao Yu675f10b2016-02-22 18:29:18 +0800683 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
684 return -ENOSPC;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900685
686 /* Increase the depth, if required */
687 if (level == current_depth)
688 ++current_depth;
689
Jaegeuk Kim38431542014-02-27 18:20:00 +0900690 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900691 nblock = bucket_blocks(level);
692
Jaegeuk Kim38431542014-02-27 18:20:00 +0900693 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
Eric Biggers43c780b2020-05-07 00:59:04 -0700694 (le32_to_cpu(fname->hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900695
696 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800697 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
Chao Yu675f10b2016-02-22 18:29:18 +0800698 if (IS_ERR(dentry_page))
699 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900700
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800701 dentry_blk = page_address(dentry_page);
Chao Yu4d57b862018-05-30 00:20:41 +0800702 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700703 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900704 if (bit_pos < NR_DENTRY_IN_BLOCK)
705 goto add_dentry;
706
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900707 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900708 }
709
710 /* Move to next level to find the empty slot for new dentry */
711 ++level;
712 goto start;
713add_dentry:
Chao Yubae0ee72018-12-25 17:43:42 +0800714 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900715
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700716 if (inode) {
717 down_write(&F2FS_I(inode)->i_sem);
Eric Biggers43c780b2020-05-07 00:59:04 -0700718 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700719 if (IS_ERR(page)) {
720 err = PTR_ERR(page);
721 goto fail;
722 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900723 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800724
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300725 make_dentry_ptr_block(NULL, &d, dentry_blk);
Eric Biggers43c780b2020-05-07 00:59:04 -0700726 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
727 bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800728
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900729 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900730
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700731 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700732 f2fs_i_pino_write(inode, dir->i_ino);
Chao Yu98194032019-09-10 09:14:16 +0800733
734 /* synchronize inode page's data from inode cache */
735 if (is_inode_flag_set(inode, FI_NEW_INODE))
736 f2fs_update_inode(inode, page);
737
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700738 f2fs_put_page(page, 1);
739 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900740
Chao Yu4d57b862018-05-30 00:20:41 +0800741 f2fs_update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900742fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700743 if (inode)
744 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900745
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900746 f2fs_put_page(dentry_page, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800747
748 return err;
749}
750
Eric Biggers43c780b2020-05-07 00:59:04 -0700751int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
752 struct inode *inode, nid_t ino, umode_t mode)
Shuoran Liue7ba1082016-08-29 11:27:56 +0800753{
Shuoran Liue7ba1082016-08-29 11:27:56 +0800754 int err = -EAGAIN;
755
Shuoran Liue7ba1082016-08-29 11:27:56 +0800756 if (f2fs_has_inline_dentry(dir))
Eric Biggers43c780b2020-05-07 00:59:04 -0700757 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800758 if (err == -EAGAIN)
Eric Biggers43c780b2020-05-07 00:59:04 -0700759 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800760
761 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
762 return err;
763}
764
Chao Yu675f10b2016-02-22 18:29:18 +0800765/*
766 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
767 * f2fs_unlock_op().
768 */
Chao Yu4d57b862018-05-30 00:20:41 +0800769int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Chao Yu675f10b2016-02-22 18:29:18 +0800770 struct inode *inode, nid_t ino, umode_t mode)
771{
Eric Biggers43c780b2020-05-07 00:59:04 -0700772 struct f2fs_filename fname;
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800773 struct page *page = NULL;
774 struct f2fs_dir_entry *de = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800775 int err;
776
Eric Biggers43c780b2020-05-07 00:59:04 -0700777 err = f2fs_setup_filename(dir, name, 0, &fname);
Chao Yu675f10b2016-02-22 18:29:18 +0800778 if (err)
779 return err;
780
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800781 /*
Liu Songe5cc2c52020-06-25 20:40:11 +0800782 * An immature stackable filesystem shows a race condition between lookup
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800783 * and create. If we have same task when doing lookup and create, it's
784 * definitely fine as expected by VFS normally. Otherwise, let's just
785 * verify on-disk dentry one more time, which guarantees filesystem
786 * consistency more.
787 */
788 if (current != F2FS_I(dir)->task) {
789 de = __f2fs_find_entry(dir, &fname, &page);
790 F2FS_I(dir)->task = NULL;
791 }
792 if (de) {
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800793 f2fs_put_page(page, 0);
794 err = -EEXIST;
795 } else if (IS_ERR(page)) {
796 err = PTR_ERR(page);
797 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800798 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800799 }
Eric Biggers43c780b2020-05-07 00:59:04 -0700800 f2fs_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900801 return err;
802}
803
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700804int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
805{
806 struct page *page;
807 int err = 0;
808
809 down_write(&F2FS_I(inode)->i_sem);
Eric Biggers43c780b2020-05-07 00:59:04 -0700810 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700811 if (IS_ERR(page)) {
812 err = PTR_ERR(page);
813 goto fail;
814 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700815 f2fs_put_page(page, 1);
816
Jaegeuk Kim91942322016-05-20 10:13:22 -0700817 clear_inode_flag(inode, FI_NEW_INODE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530818 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700819fail:
820 up_write(&F2FS_I(inode)->i_sem);
821 return err;
822}
823
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700824void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
Chao Yudbeacf02014-09-24 18:17:04 +0800825{
826 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
827
828 down_write(&F2FS_I(inode)->i_sem);
829
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700830 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700831 f2fs_i_links_write(dir, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700832 inode->i_ctime = current_time(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800833
Jaegeuk Kima1961242016-05-20 09:43:20 -0700834 f2fs_i_links_write(inode, false);
Chao Yudbeacf02014-09-24 18:17:04 +0800835 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700836 f2fs_i_links_write(inode, false);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700837 f2fs_i_size_write(inode, 0);
Chao Yudbeacf02014-09-24 18:17:04 +0800838 }
839 up_write(&F2FS_I(inode)->i_sem);
Chao Yudbeacf02014-09-24 18:17:04 +0800840
841 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800842 f2fs_add_orphan_inode(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800843 else
Chao Yu4d57b862018-05-30 00:20:41 +0800844 f2fs_release_orphan_inode(sbi);
Chao Yudbeacf02014-09-24 18:17:04 +0800845}
846
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900847/*
arter97e1c42042014-08-06 23:22:50 +0900848 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900849 * entry in name page does not need to be touched during deletion.
850 */
851void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800852 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900853{
854 struct f2fs_dentry_block *dentry_blk;
855 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900856 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900857 int i;
858
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800859 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
860
Chao Yu63189b72018-03-08 14:22:56 +0800861 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
Chao Yu4d57b862018-05-30 00:20:41 +0800862 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800863
Chao Yu622f28a2014-09-24 18:19:10 +0800864 if (f2fs_has_inline_dentry(dir))
865 return f2fs_delete_inline_entry(dentry, page, dir, inode);
866
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900867 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800868 f2fs_wait_on_page_writeback(page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900869
Gu Zheng1c3bb972014-06-27 17:57:04 +0800870 dentry_blk = page_address(page);
871 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900872 for (i = 0; i < slots; i++)
Jaegeuk Kim23380b82017-03-07 14:11:06 -0800873 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900874
875 /* Let's check and deallocate this dentry page */
876 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
877 NR_DENTRY_IN_BLOCK,
878 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900879 set_page_dirty(page);
880
Chao Yu206e61b2015-08-12 17:48:21 +0800881 if (bit_pos == NR_DENTRY_IN_BLOCK &&
Chao Yu4d57b862018-05-30 00:20:41 +0800882 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500883 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900884 clear_page_dirty_for_io(page);
Chao Yu240a5912019-03-06 17:30:59 +0800885 f2fs_clear_page_private(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900886 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800887 clear_cold_data(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700888 inode_dec_dirty_pages(dir);
Chao Yu4d57b862018-05-30 00:20:41 +0800889 f2fs_remove_dirty_inode(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900890 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900891 f2fs_put_page(page, 1);
Chao Yu9995e402020-03-20 18:17:54 +0800892
893 dir->i_ctime = dir->i_mtime = current_time(dir);
894 f2fs_mark_inode_dirty_sync(dir, false);
895
896 if (inode)
897 f2fs_drop_nlink(dir, inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900898}
899
900bool f2fs_empty_dir(struct inode *dir)
901{
902 unsigned long bidx;
903 struct page *dentry_page;
904 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800905 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900906 unsigned long nblock = dir_blocks(dir);
907
Chao Yu622f28a2014-09-24 18:19:10 +0800908 if (f2fs_has_inline_dentry(dir))
909 return f2fs_empty_inline_dir(dir);
910
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900911 for (bidx = 0; bidx < nblock; bidx++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800912 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900913 if (IS_ERR(dentry_page)) {
914 if (PTR_ERR(dentry_page) == -ENOENT)
915 continue;
916 else
917 return false;
918 }
919
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800920 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900921 if (bidx == 0)
922 bit_pos = 2;
923 else
924 bit_pos = 0;
925 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
926 NR_DENTRY_IN_BLOCK,
927 bit_pos);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900928
929 f2fs_put_page(dentry_page, 1);
930
931 if (bit_pos < NR_DENTRY_IN_BLOCK)
932 return false;
933 }
934 return true;
935}
936
Chao Yued6bd4b2016-10-29 18:46:34 +0800937int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700938 unsigned int start_pos, struct fscrypt_str *fstr)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700939{
940 unsigned char d_type = DT_UNKNOWN;
941 unsigned int bit_pos;
942 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700943 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
Sheng Yongf6df8f22017-11-22 18:23:38 +0800944 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
Chao Yue1293bd2018-09-07 19:49:07 +0800945 struct blk_plug plug;
946 bool readdir_ra = sbi->readdir_ra == 1;
947 int err = 0;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700948
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700949 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700950
Chao Yue1293bd2018-09-07 19:49:07 +0800951 if (readdir_ra)
952 blk_start_plug(&plug);
953
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700954 while (bit_pos < d->max) {
955 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
956 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700957 break;
958
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700959 de = &d->dentry[bit_pos];
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800960 if (de->name_len == 0) {
961 bit_pos++;
962 ctx->pos = start_pos + bit_pos;
Chao Yuddf06b72019-01-08 10:21:24 +0800963 printk_ratelimited(
Chao Yuc45d6002019-11-01 17:53:23 +0800964 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
965 KERN_WARNING, sbi->sb->s_id,
966 le32_to_cpu(de->ino));
Chao Yuddf06b72019-01-08 10:21:24 +0800967 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800968 continue;
969 }
970
Chao Yu4d57b862018-05-30 00:20:41 +0800971 d_type = f2fs_get_de_type(de);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700972
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700973 de_name.name = d->filename[bit_pos];
974 de_name.len = le16_to_cpu(de->name_len);
975
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800976 /* check memory boundary before moving forward */
977 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Sheng Yong720db062019-01-07 15:02:34 +0800978 if (unlikely(bit_pos > d->max ||
979 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800980 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
981 __func__, le16_to_cpu(de->name_len));
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800982 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu10f966b2019-06-20 11:36:14 +0800983 err = -EFSCORRUPTED;
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800984 goto out;
985 }
986
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530987 if (IS_ENCRYPTED(d->inode)) {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700988 int save_len = fstr->len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700989
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400990 err = fscrypt_fname_disk_to_usr(d->inode,
Chao Yu36af5f42019-05-28 17:23:33 +0800991 (u32)le32_to_cpu(de->hash_code),
992 0, &de_name, fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400993 if (err)
Chao Yue1293bd2018-09-07 19:49:07 +0800994 goto out;
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700995
996 de_name = *fstr;
997 fstr->len = save_len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700998 }
999
1000 if (!dir_emit(ctx, de_name.name, de_name.len,
Chao Yue1293bd2018-09-07 19:49:07 +08001001 le32_to_cpu(de->ino), d_type)) {
1002 err = 1;
1003 goto out;
1004 }
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001005
Chao Yue1293bd2018-09-07 19:49:07 +08001006 if (readdir_ra)
Chao Yu4d57b862018-05-30 00:20:41 +08001007 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
Sheng Yongf6df8f22017-11-22 18:23:38 +08001008
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001009 ctx->pos = start_pos + bit_pos;
1010 }
Chao Yue1293bd2018-09-07 19:49:07 +08001011out:
1012 if (readdir_ra)
1013 blk_finish_plug(&plug);
1014 return err;
Jaegeuk Kim38594de2014-10-15 21:29:51 -07001015}
1016
Al Viro6f7f2312013-05-17 18:02:17 -04001017static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001018{
Al Viro496ad9a2013-01-23 17:07:38 -05001019 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001020 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001021 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001022 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +08001023 struct file_ra_state *ra = &file->f_ra;
Chao Yue97a3c42017-10-13 18:01:33 +08001024 loff_t start_pos = ctx->pos;
Al Viro6f7f2312013-05-17 18:02:17 -04001025 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001026 struct f2fs_dentry_ptr d;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001027 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001028 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001029
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301030 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001031 err = fscrypt_get_encryption_info(inode);
Eric Biggers3b1ada552019-12-09 13:23:48 -08001032 if (err)
Chao Yue97a3c42017-10-13 18:01:33 +08001033 goto out;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -07001034
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001035 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001036 if (err < 0)
Chao Yue97a3c42017-10-13 18:01:33 +08001037 goto out;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001038 }
1039
1040 if (f2fs_has_inline_dentry(inode)) {
1041 err = f2fs_read_inline_dir(file, ctx, &fstr);
Chao Yue97a3c42017-10-13 18:01:33 +08001042 goto out_free;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -07001043 }
Chao Yu622f28a2014-09-24 18:19:10 +08001044
Chao Yuab383be2017-10-13 18:01:36 +08001045 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
Chao Yu4414dea2017-10-13 18:01:34 +08001046
1047 /* allow readdir() to be interrupted */
1048 if (fatal_signal_pending(current)) {
1049 err = -ERESTARTSYS;
1050 goto out_free;
1051 }
1052 cond_resched();
1053
Chao Yucb7a8442017-10-13 18:01:35 +08001054 /* readahead for multi pages of dir */
1055 if (npages - n > 1 && !ra_has_index(ra, n))
1056 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1057 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1058
Gao Xiang613f3dc2019-02-21 12:57:35 +08001059 dentry_page = f2fs_find_data_page(inode, n);
Chao Yu57b62d22015-11-19 16:09:07 +08001060 if (IS_ERR(dentry_page)) {
1061 err = PTR_ERR(dentry_page);
Chao Yued6bd4b2016-10-29 18:46:34 +08001062 if (err == -ENOENT) {
1063 err = 0;
Chao Yu57b62d22015-11-19 16:09:07 +08001064 continue;
Chao Yued6bd4b2016-10-29 18:46:34 +08001065 } else {
Chao Yue97a3c42017-10-13 18:01:33 +08001066 goto out_free;
Chao Yued6bd4b2016-10-29 18:46:34 +08001067 }
Chao Yu57b62d22015-11-19 16:09:07 +08001068 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001069
Yunlong Songbdbc90f2018-02-28 20:31:52 +08001070 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001071
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +03001072 make_dentry_ptr_block(inode, &d, dentry_blk);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -07001073
Chao Yued6bd4b2016-10-29 18:46:34 +08001074 err = f2fs_fill_dentries(ctx, &d,
1075 n * NR_DENTRY_IN_BLOCK, &fstr);
1076 if (err) {
Gao Xiang613f3dc2019-02-21 12:57:35 +08001077 f2fs_put_page(dentry_page, 0);
Chao Yue9837bc2015-12-01 11:41:50 +08001078 break;
1079 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +09001080
Gao Xiang613f3dc2019-02-21 12:57:35 +08001081 f2fs_put_page(dentry_page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001082 }
Chao Yue97a3c42017-10-13 18:01:33 +08001083out_free:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001084 fscrypt_fname_free_buffer(&fstr);
Chao Yue97a3c42017-10-13 18:01:33 +08001085out:
1086 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
Chao Yued6bd4b2016-10-29 18:46:34 +08001087 return err < 0 ? err : 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001088}
1089
Chao Yued3360a2016-02-14 18:56:55 +08001090static int f2fs_dir_open(struct inode *inode, struct file *filp)
1091{
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301092 if (IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001093 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
Chao Yued3360a2016-02-14 18:56:55 +08001094 return 0;
1095}
1096
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001097const struct file_operations f2fs_dir_operations = {
1098 .llseek = generic_file_llseek,
1099 .read = generic_read_dir,
Al Viroe77d0c62016-05-10 16:41:13 -04001100 .iterate_shared = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001101 .fsync = f2fs_sync_file,
Chao Yued3360a2016-02-14 18:56:55 +08001102 .open = f2fs_dir_open,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001103 .unlocked_ioctl = f2fs_ioctl,
hujianyang08b95122015-05-12 16:05:57 +08001104#ifdef CONFIG_COMPAT
1105 .compat_ioctl = f2fs_compat_ioctl,
1106#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001107};
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001108
1109#ifdef CONFIG_UNICODE
1110static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
1111 const char *str, const struct qstr *name)
1112{
Eric Biggers80f23882020-01-23 20:15:49 -08001113 const struct dentry *parent = READ_ONCE(dentry->d_parent);
Eric Biggersf874fa12020-05-07 00:59:03 -07001114 const struct inode *dir = READ_ONCE(parent->d_inode);
1115 const struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1116 struct qstr entry = QSTR_INIT(str, len);
Eric Biggersfc3bb092020-06-01 13:08:05 -07001117 char strbuf[DNAME_INLINE_LEN];
Eric Biggersf874fa12020-05-07 00:59:03 -07001118 int res;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001119
Eric Biggersf874fa12020-05-07 00:59:03 -07001120 if (!dir || !IS_CASEFOLDED(dir))
1121 goto fallback;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001122
Eric Biggersfc3bb092020-06-01 13:08:05 -07001123 /*
1124 * If the dentry name is stored in-line, then it may be concurrently
1125 * modified by a rename. If this happens, the VFS will eventually retry
1126 * the lookup, so it doesn't matter what ->d_compare() returns.
1127 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1128 * string. Therefore, we have to copy the name into a temporary buffer.
1129 */
1130 if (len <= DNAME_INLINE_LEN - 1) {
1131 memcpy(strbuf, str, len);
1132 strbuf[len] = 0;
1133 entry.name = strbuf;
1134 /* prevent compiler from optimizing out the temporary buffer */
1135 barrier();
1136 }
1137
Eric Biggersf874fa12020-05-07 00:59:03 -07001138 res = utf8_strncasecmp(sbi->s_encoding, name, &entry);
1139 if (res >= 0)
1140 return res;
1141
1142 if (f2fs_has_strict_mode(sbi))
1143 return -EINVAL;
1144fallback:
1145 if (len != name->len)
1146 return 1;
1147 return !!memcmp(str, name->name, len);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001148}
1149
1150static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
1151{
1152 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1153 const struct unicode_map *um = sbi->s_encoding;
Eric Biggers80f23882020-01-23 20:15:49 -08001154 const struct inode *inode = READ_ONCE(dentry->d_inode);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001155 unsigned char *norm;
1156 int len, ret = 0;
1157
Eric Biggers80f23882020-01-23 20:15:49 -08001158 if (!inode || !IS_CASEFOLDED(inode))
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001159 return 0;
1160
1161 norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
1162 if (!norm)
1163 return -ENOMEM;
1164
1165 len = utf8_casefold(um, str, norm, PATH_MAX);
1166 if (len < 0) {
1167 if (f2fs_has_strict_mode(sbi))
1168 ret = -EINVAL;
1169 goto out;
1170 }
1171 str->hash = full_name_hash(dentry, norm, len);
1172out:
1173 kvfree(norm);
1174 return ret;
1175}
1176
1177const struct dentry_operations f2fs_dentry_ops = {
1178 .d_hash = f2fs_d_hash,
1179 .d_compare = f2fs_d_compare,
1180};
1181#endif