blob: 501999af581d5093e4fd7da5e5cb4999ccccf13f [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
Jaegeuk Kim38431542014-02-27 18:20:00 +090073static unsigned long dir_block_index(unsigned int level,
74 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090075{
76 unsigned long i;
77 unsigned long bidx = 0;
78
79 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +090080 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090081 bidx += idx * bucket_blocks(level);
82 return bidx;
83}
84
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -070085static struct f2fs_dir_entry *find_in_block(struct inode *dir,
86 struct page *dentry_page,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070087 struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -070088 f2fs_hash_t namehash,
89 int *max_slots,
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070090 struct page **res_page)
91{
92 struct f2fs_dentry_block *dentry_blk;
93 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -070094 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070095
Yunlong Songbdbc90f2018-02-28 20:31:52 +080096 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -070097
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -070098 make_dentry_ptr_block(dir, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +080099 de = f2fs_find_target_dentry(fname, namehash, max_slots, &d);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700100 if (de)
101 *res_page = dentry_page;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700102
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700103 return de;
104}
105
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700106#ifdef CONFIG_UNICODE
107/*
108 * Test whether a case-insensitive directory entry matches the filename
109 * being searched for.
110 *
111 * Returns: 0 if the directory entry matches, more than 0 if it
112 * doesn't match or less than zero on error.
113 */
114int f2fs_ci_compare(const struct inode *parent, const struct qstr *name,
115 const struct qstr *entry)
116{
117 const struct f2fs_sb_info *sbi = F2FS_SB(parent->i_sb);
118 const struct unicode_map *um = sbi->s_encoding;
119 int ret;
120
121 ret = utf8_strncasecmp(um, name, entry);
122 if (ret < 0) {
123 /* Handle invalid character sequence as either an error
124 * or as an opaque byte sequence.
125 */
126 if (f2fs_has_strict_mode(sbi))
127 return -EINVAL;
128
129 if (name->len != entry->len)
130 return 1;
131
132 return !!memcmp(name->name, entry->name, name->len);
133 }
134
135 return ret;
136}
137#endif
138
Chao Yufe76a162019-08-21 23:13:34 +0800139static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d,
140 struct f2fs_dir_entry *de,
141 struct fscrypt_name *fname,
142 unsigned long bit_pos,
143 f2fs_hash_t namehash)
144{
145#ifdef CONFIG_UNICODE
146 struct inode *parent = d->inode;
147 struct f2fs_sb_info *sbi = F2FS_I_SB(parent);
148 struct qstr entry;
149#endif
150
151 if (de->hash_code != namehash)
152 return false;
153
154#ifdef CONFIG_UNICODE
155 entry.name = d->filename[bit_pos];
156 entry.len = de->name_len;
157
158 if (sbi->s_encoding && IS_CASEFOLDED(parent))
159 return !f2fs_ci_compare(parent, fname->usr_fname, &entry);
160#endif
161 if (fscrypt_match_name(fname, d->filename[bit_pos],
162 le16_to_cpu(de->name_len)))
163 return true;
164 return false;
165}
166
Chao Yu4d57b862018-05-30 00:20:41 +0800167struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700168 f2fs_hash_t namehash, int *max_slots,
169 struct f2fs_dentry_ptr *d)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900170{
171 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900172 unsigned long bit_pos = 0;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900173 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900174
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700175 if (max_slots)
176 *max_slots = 0;
177 while (bit_pos < d->max) {
178 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900179 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800180 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900181 continue;
182 }
Chao Yubda19072015-03-09 17:33:16 +0800183
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700184 de = &d->dentry[bit_pos];
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700185
Chao Yua4a13f52016-04-27 22:22:20 +0800186 if (unlikely(!de->name_len)) {
187 bit_pos++;
188 continue;
189 }
190
Chao Yufe76a162019-08-21 23:13:34 +0800191 if (f2fs_match_name(d, de, fname, bit_pos, namehash))
192 goto found;
Eric Biggers1f73d492017-04-24 10:00:12 -0700193
Chao Yubda19072015-03-09 17:33:16 +0800194 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900195 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800196 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800197
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900198 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900199 }
200
201 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900202found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700203 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900204 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900205 return de;
206}
207
208static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700209 unsigned int level,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700210 struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700211 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900212{
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700213 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
214 int s = GET_DENTRY_SLOTS(name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900215 unsigned int nbucket, nblock;
216 unsigned int bidx, end_block;
217 struct page *dentry_page;
218 struct f2fs_dir_entry *de = NULL;
219 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700220 int max_slots;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700221 f2fs_hash_t namehash = f2fs_dentry_hash(dir, &name, fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900222
Jaegeuk Kim38431542014-02-27 18:20:00 +0900223 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900224 nblock = bucket_blocks(level);
225
Jaegeuk Kim38431542014-02-27 18:20:00 +0900226 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
227 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900228 end_block = bidx + nblock;
229
230 for (; bidx < end_block; bidx++) {
231 /* no need to allocate new dentry pages to all the indices */
Chao Yu4d57b862018-05-30 00:20:41 +0800232 dentry_page = f2fs_find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900233 if (IS_ERR(dentry_page)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700234 if (PTR_ERR(dentry_page) == -ENOENT) {
235 room = true;
236 continue;
237 } else {
238 *res_page = dentry_page;
239 break;
240 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900241 }
242
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700243 de = find_in_block(dir, dentry_page, fname, namehash,
244 &max_slots, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900245 if (de)
246 break;
247
248 if (max_slots >= s)
249 room = true;
250 f2fs_put_page(dentry_page, 0);
251 }
252
Sheng Yongd3bb9102017-04-22 10:39:20 +0800253 if (!de && room && F2FS_I(dir)->chash != namehash) {
254 F2FS_I(dir)->chash = namehash;
255 F2FS_I(dir)->clevel = level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900256 }
257
258 return de;
259}
260
Shuoran Liue7ba1082016-08-29 11:27:56 +0800261struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
262 struct fscrypt_name *fname, struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900263{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900264 unsigned long npages = dir_blocks(dir);
265 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900266 unsigned int max_depth;
267 unsigned int level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900268
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700269 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700270 *res_page = NULL;
Chao Yu4d57b862018-05-30 00:20:41 +0800271 de = f2fs_find_in_inline_dir(dir, fname, res_page);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700272 goto out;
273 }
274
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700275 if (npages == 0) {
276 *res_page = NULL;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700277 goto out;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700278 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700279
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900280 max_depth = F2FS_I(dir)->i_current_depth;
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800281 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800282 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
283 dir->i_ino, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800284 max_depth = MAX_DIR_HASH_DEPTH;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700285 f2fs_i_depth_write(dir, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800286 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900287
288 for (level = 0; level < max_depth; level++) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700289 *res_page = NULL;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800290 de = find_in_level(dir, level, fname, res_page);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700291 if (de || IS_ERR(*res_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900292 break;
293 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700294out:
Sheng Yongd3bb9102017-04-22 10:39:20 +0800295 /* This is to increase the speed of f2fs_create */
296 if (!de)
297 F2FS_I(dir)->task = current;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800298 return de;
299}
300
301/*
302 * Find an entry in the specified directory with the wanted name.
303 * It returns the page where the entry was found (as a parameter - res_page),
304 * and the entry itself. Page is returned mapped and unlocked.
305 * Entry is guaranteed to be valid.
306 */
307struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
308 const struct qstr *child, struct page **res_page)
309{
310 struct f2fs_dir_entry *de = NULL;
311 struct fscrypt_name fname;
312 int err;
313
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700314#ifdef CONFIG_UNICODE
315 if (f2fs_has_strict_mode(F2FS_I_SB(dir)) && IS_CASEFOLDED(dir) &&
316 utf8_validate(F2FS_I_SB(dir)->s_encoding, child)) {
317 *res_page = ERR_PTR(-EINVAL);
318 return NULL;
319 }
320#endif
321
Shuoran Liue7ba1082016-08-29 11:27:56 +0800322 err = fscrypt_setup_filename(dir, child, 1, &fname);
323 if (err) {
Eric Biggers54475f52016-12-05 11:12:44 -0800324 if (err == -ENOENT)
325 *res_page = NULL;
326 else
327 *res_page = ERR_PTR(err);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800328 return NULL;
329 }
330
331 de = __f2fs_find_entry(dir, &fname, res_page);
332
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700333 fscrypt_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900334 return de;
335}
336
337struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
338{
Sheng Yong8be0fea2016-06-04 22:01:28 +0800339 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900340
Sheng Yong8be0fea2016-06-04 22:01:28 +0800341 return f2fs_find_entry(dir, &dotdot, p);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900342}
343
Linus Torvalds835c92d2016-08-06 09:49:02 -0400344ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
Chao Yu91246c22016-07-19 08:27:47 +0800345 struct page **page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900346{
347 ino_t res = 0;
348 struct f2fs_dir_entry *de;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900349
Chao Yu91246c22016-07-19 08:27:47 +0800350 de = f2fs_find_entry(dir, qstr, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900351 if (de) {
352 res = le32_to_cpu(de->ino);
Chao Yu91246c22016-07-19 08:27:47 +0800353 f2fs_put_page(*page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900354 }
355
356 return res;
357}
358
359void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
360 struct page *page, struct inode *inode)
361{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700362 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900363 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800364 f2fs_wait_on_page_writeback(page, type, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900365 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700366 set_de_type(de, inode->i_mode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900367 set_page_dirty(page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900368
Deepa Dinamani078cd822016-09-14 07:48:04 -0700369 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700370 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900371 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900372}
373
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900374static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900375{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900376 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900377
Chao Yubae0ee72018-12-25 17:43:42 +0800378 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900379
Al Viro53dc9a62013-01-25 16:01:21 -0500380 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900381 ri = F2FS_INODE(ipage);
382 ri->i_namelen = cpu_to_le32(name->len);
383 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900384 set_page_dirty(ipage);
385}
386
Chao Yu4d57b862018-05-30 00:20:41 +0800387void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700388 struct f2fs_dentry_ptr *d)
389{
Chao Yu291bf802016-03-09 22:07:28 +0800390 struct qstr dot = QSTR_INIT(".", 1);
391 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700392
Chao Yu291bf802016-03-09 22:07:28 +0800393 /* update dirent of "." */
394 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700395
Chao Yu291bf802016-03-09 22:07:28 +0800396 /* update dirent of ".." */
397 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700398}
399
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900400static int make_empty_dir(struct inode *inode,
401 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900402{
403 struct page *dentry_page;
404 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700405 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900406
Chao Yu622f28a2014-09-24 18:19:10 +0800407 if (f2fs_has_inline_dentry(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800408 return f2fs_make_empty_inline_dir(inode, parent, page);
Chao Yu622f28a2014-09-24 18:19:10 +0800409
Chao Yu4d57b862018-05-30 00:20:41 +0800410 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900411 if (IS_ERR(dentry_page))
412 return PTR_ERR(dentry_page);
413
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800414 dentry_blk = page_address(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900415
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300416 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +0800417 f2fs_do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900418
Jaegeuk Kim39936832012-11-22 16:21:29 +0900419 set_page_dirty(dentry_page);
420 f2fs_put_page(dentry_page, 1);
421 return 0;
422}
423
Chao Yu4d57b862018-05-30 00:20:41 +0800424struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Chao Yu9421d572016-08-28 18:57:55 +0800425 const struct qstr *new_name, const struct qstr *orig_name,
426 struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900427{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900428 struct page *page;
Sheng Yongff62af22018-03-15 18:51:42 +0800429 int dummy_encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900430 int err;
431
Jaegeuk Kim91942322016-05-20 10:13:22 -0700432 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800433 page = f2fs_new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900434 if (IS_ERR(page))
435 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900436
437 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700438 /* in order to handle error case */
439 get_page(page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900440 err = make_empty_dir(inode, dir, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700441 if (err) {
442 lock_page(page);
443 goto put_error;
444 }
445 put_page(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900446 }
447
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700448 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900449 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900450 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900451
Chao Yu9421d572016-08-28 18:57:55 +0800452 err = f2fs_init_security(inode, dir, orig_name, page);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900453 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900454 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700455
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530456 if ((IS_ENCRYPTED(dir) || dummy_encrypt) &&
Sheng Yongff62af22018-03-15 18:51:42 +0800457 f2fs_may_encrypt(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700458 err = fscrypt_inherit_context(dir, inode, page, false);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700459 if (err)
460 goto put_error;
461 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900462 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800463 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900464 if (IS_ERR(page))
465 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900466 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900467
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800468 if (new_name) {
Chao Yu9421d572016-08-28 18:57:55 +0800469 init_dent_inode(new_name, page);
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530470 if (IS_ENCRYPTED(dir))
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800471 file_set_enc_name(inode);
472 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900473
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900474 /*
475 * This file should be checkpointed during fsync.
476 * We lost i_pino from now on.
477 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700478 if (is_inode_flag_set(inode, FI_INC_LINK)) {
Sheng Yongd58dfb72017-06-26 10:41:35 +0800479 if (!S_ISDIR(inode->i_mode))
480 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800481 /*
482 * If link the tmpfile to alias through linkat path,
483 * we should remove this inode from orphan list.
484 */
485 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800486 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700487 f2fs_i_links_write(inode, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900488 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900489 return page;
490
Jaegeuk Kima8865372013-12-27 17:04:17 +0900491put_error:
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700492 clear_nlink(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800493 f2fs_update_inode(inode, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700494 f2fs_put_page(page, 1);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900495 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900496}
497
Chao Yu4d57b862018-05-30 00:20:41 +0800498void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900499 unsigned int current_depth)
500{
Jaegeuk Kim91942322016-05-20 10:13:22 -0700501 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700502 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700503 f2fs_i_links_write(dir, true);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700504 clear_inode_flag(inode, FI_NEW_INODE);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900505 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700506 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700507 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900508
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700509 if (F2FS_I(dir)->i_current_depth != current_depth)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700510 f2fs_i_depth_write(dir, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900511
Jaegeuk Kim91942322016-05-20 10:13:22 -0700512 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
513 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900514}
515
Chao Yu4d57b862018-05-30 00:20:41 +0800516int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900517{
518 int bit_start = 0;
519 int zero_start, zero_end;
520next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700521 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
522 if (zero_start >= max_slots)
523 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900524
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700525 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900526 if (zero_end - zero_start >= slots)
527 return zero_start;
528
529 bit_start = zero_end + 1;
530
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700531 if (zero_end + 1 >= max_slots)
532 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900533 goto next;
534}
535
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700536void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Chao Yu3b4d7322015-02-16 16:17:20 +0800537 const struct qstr *name, f2fs_hash_t name_hash,
538 unsigned int bit_pos)
539{
540 struct f2fs_dir_entry *de;
541 int slots = GET_DENTRY_SLOTS(name->len);
542 int i;
543
544 de = &d->dentry[bit_pos];
545 de->hash_code = name_hash;
546 de->name_len = cpu_to_le16(name->len);
547 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700548 de->ino = cpu_to_le32(ino);
549 set_de_type(de, mode);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800550 for (i = 0; i < slots; i++) {
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700551 __set_bit_le(bit_pos + i, (void *)d->bitmap);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800552 /* avoid wrong garbage data for readdir */
553 if (i)
554 (de + i)->name_len = 0;
555 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800556}
557
Chao Yu675f10b2016-02-22 18:29:18 +0800558int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800559 const struct qstr *orig_name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700560 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900561{
562 unsigned int bit_pos;
563 unsigned int level;
564 unsigned int current_depth;
565 unsigned long bidx, block;
566 f2fs_hash_t dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900567 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900568 struct page *dentry_page = NULL;
569 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800570 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700571 struct page *page = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800572 int slots, err = 0;
Chao Yu622f28a2014-09-24 18:19:10 +0800573
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900574 level = 0;
Chao Yu675f10b2016-02-22 18:29:18 +0800575 slots = GET_DENTRY_SLOTS(new_name->len);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700576 dentry_hash = f2fs_dentry_hash(dir, new_name, NULL);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700577
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900578 current_depth = F2FS_I(dir)->i_current_depth;
579 if (F2FS_I(dir)->chash == dentry_hash) {
580 level = F2FS_I(dir)->clevel;
581 F2FS_I(dir)->chash = 0;
582 }
583
584start:
Chao Yu55523512017-02-25 11:08:28 +0800585 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
586 f2fs_show_injection_info(FAULT_DIR_DEPTH);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700587 return -ENOSPC;
Chao Yu55523512017-02-25 11:08:28 +0800588 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200589
Chao Yu675f10b2016-02-22 18:29:18 +0800590 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
591 return -ENOSPC;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900592
593 /* Increase the depth, if required */
594 if (level == current_depth)
595 ++current_depth;
596
Jaegeuk Kim38431542014-02-27 18:20:00 +0900597 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900598 nblock = bucket_blocks(level);
599
Jaegeuk Kim38431542014-02-27 18:20:00 +0900600 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
601 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900602
603 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800604 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
Chao Yu675f10b2016-02-22 18:29:18 +0800605 if (IS_ERR(dentry_page))
606 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900607
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800608 dentry_blk = page_address(dentry_page);
Chao Yu4d57b862018-05-30 00:20:41 +0800609 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700610 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900611 if (bit_pos < NR_DENTRY_IN_BLOCK)
612 goto add_dentry;
613
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900614 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900615 }
616
617 /* Move to next level to find the empty slot for new dentry */
618 ++level;
619 goto start;
620add_dentry:
Chao Yubae0ee72018-12-25 17:43:42 +0800621 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900622
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700623 if (inode) {
624 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800625 page = f2fs_init_inode_metadata(inode, dir, new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800626 orig_name, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700627 if (IS_ERR(page)) {
628 err = PTR_ERR(page);
629 goto fail;
630 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900631 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800632
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300633 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu675f10b2016-02-22 18:29:18 +0800634 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800635
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900636 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900637
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700638 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700639 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700640 f2fs_put_page(page, 1);
641 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900642
Chao Yu4d57b862018-05-30 00:20:41 +0800643 f2fs_update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900644fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700645 if (inode)
646 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900647
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900648 f2fs_put_page(dentry_page, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800649
650 return err;
651}
652
Chao Yu4d57b862018-05-30 00:20:41 +0800653int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
Shuoran Liue7ba1082016-08-29 11:27:56 +0800654 struct inode *inode, nid_t ino, umode_t mode)
655{
656 struct qstr new_name;
657 int err = -EAGAIN;
658
659 new_name.name = fname_name(fname);
660 new_name.len = fname_len(fname);
661
662 if (f2fs_has_inline_dentry(dir))
663 err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
664 inode, ino, mode);
665 if (err == -EAGAIN)
666 err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
667 inode, ino, mode);
668
669 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
670 return err;
671}
672
Chao Yu675f10b2016-02-22 18:29:18 +0800673/*
674 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
675 * f2fs_unlock_op().
676 */
Chao Yu4d57b862018-05-30 00:20:41 +0800677int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Chao Yu675f10b2016-02-22 18:29:18 +0800678 struct inode *inode, nid_t ino, umode_t mode)
679{
680 struct fscrypt_name fname;
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800681 struct page *page = NULL;
682 struct f2fs_dir_entry *de = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800683 int err;
684
685 err = fscrypt_setup_filename(dir, name, 0, &fname);
686 if (err)
687 return err;
688
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800689 /*
690 * An immature stakable filesystem shows a race condition between lookup
691 * and create. If we have same task when doing lookup and create, it's
692 * definitely fine as expected by VFS normally. Otherwise, let's just
693 * verify on-disk dentry one more time, which guarantees filesystem
694 * consistency more.
695 */
696 if (current != F2FS_I(dir)->task) {
697 de = __f2fs_find_entry(dir, &fname, &page);
698 F2FS_I(dir)->task = NULL;
699 }
700 if (de) {
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800701 f2fs_put_page(page, 0);
702 err = -EEXIST;
703 } else if (IS_ERR(page)) {
704 err = PTR_ERR(page);
705 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800706 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800707 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700708 fscrypt_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900709 return err;
710}
711
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700712int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
713{
714 struct page *page;
715 int err = 0;
716
717 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800718 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700719 if (IS_ERR(page)) {
720 err = PTR_ERR(page);
721 goto fail;
722 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700723 f2fs_put_page(page, 1);
724
Jaegeuk Kim91942322016-05-20 10:13:22 -0700725 clear_inode_flag(inode, FI_NEW_INODE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530726 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700727fail:
728 up_write(&F2FS_I(inode)->i_sem);
729 return err;
730}
731
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700732void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
Chao Yudbeacf02014-09-24 18:17:04 +0800733{
734 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
735
736 down_write(&F2FS_I(inode)->i_sem);
737
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700738 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700739 f2fs_i_links_write(dir, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700740 inode->i_ctime = current_time(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800741
Jaegeuk Kima1961242016-05-20 09:43:20 -0700742 f2fs_i_links_write(inode, false);
Chao Yudbeacf02014-09-24 18:17:04 +0800743 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700744 f2fs_i_links_write(inode, false);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700745 f2fs_i_size_write(inode, 0);
Chao Yudbeacf02014-09-24 18:17:04 +0800746 }
747 up_write(&F2FS_I(inode)->i_sem);
Chao Yudbeacf02014-09-24 18:17:04 +0800748
749 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800750 f2fs_add_orphan_inode(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800751 else
Chao Yu4d57b862018-05-30 00:20:41 +0800752 f2fs_release_orphan_inode(sbi);
Chao Yudbeacf02014-09-24 18:17:04 +0800753}
754
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900755/*
arter97e1c42042014-08-06 23:22:50 +0900756 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900757 * entry in name page does not need to be touched during deletion.
758 */
759void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800760 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900761{
762 struct f2fs_dentry_block *dentry_blk;
763 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900764 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900765 int i;
766
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800767 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
768
Chao Yu63189b72018-03-08 14:22:56 +0800769 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
Chao Yu4d57b862018-05-30 00:20:41 +0800770 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800771
Chao Yu622f28a2014-09-24 18:19:10 +0800772 if (f2fs_has_inline_dentry(dir))
773 return f2fs_delete_inline_entry(dentry, page, dir, inode);
774
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900775 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800776 f2fs_wait_on_page_writeback(page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900777
Gu Zheng1c3bb972014-06-27 17:57:04 +0800778 dentry_blk = page_address(page);
779 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900780 for (i = 0; i < slots; i++)
Jaegeuk Kim23380b82017-03-07 14:11:06 -0800781 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900782
783 /* Let's check and deallocate this dentry page */
784 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
785 NR_DENTRY_IN_BLOCK,
786 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900787 set_page_dirty(page);
788
Deepa Dinamani078cd822016-09-14 07:48:04 -0700789 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700790 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900791
Chao Yudbeacf02014-09-24 18:17:04 +0800792 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700793 f2fs_drop_nlink(dir, inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900794
Chao Yu206e61b2015-08-12 17:48:21 +0800795 if (bit_pos == NR_DENTRY_IN_BLOCK &&
Chao Yu4d57b862018-05-30 00:20:41 +0800796 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500797 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900798 clear_page_dirty_for_io(page);
Chao Yu240a5912019-03-06 17:30:59 +0800799 f2fs_clear_page_private(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900800 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800801 clear_cold_data(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700802 inode_dec_dirty_pages(dir);
Chao Yu4d57b862018-05-30 00:20:41 +0800803 f2fs_remove_dirty_inode(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900804 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900805 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900806}
807
808bool f2fs_empty_dir(struct inode *dir)
809{
810 unsigned long bidx;
811 struct page *dentry_page;
812 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800813 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900814 unsigned long nblock = dir_blocks(dir);
815
Chao Yu622f28a2014-09-24 18:19:10 +0800816 if (f2fs_has_inline_dentry(dir))
817 return f2fs_empty_inline_dir(dir);
818
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900819 for (bidx = 0; bidx < nblock; bidx++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800820 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900821 if (IS_ERR(dentry_page)) {
822 if (PTR_ERR(dentry_page) == -ENOENT)
823 continue;
824 else
825 return false;
826 }
827
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800828 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900829 if (bidx == 0)
830 bit_pos = 2;
831 else
832 bit_pos = 0;
833 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
834 NR_DENTRY_IN_BLOCK,
835 bit_pos);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900836
837 f2fs_put_page(dentry_page, 1);
838
839 if (bit_pos < NR_DENTRY_IN_BLOCK)
840 return false;
841 }
842 return true;
843}
844
Chao Yued6bd4b2016-10-29 18:46:34 +0800845int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700846 unsigned int start_pos, struct fscrypt_str *fstr)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700847{
848 unsigned char d_type = DT_UNKNOWN;
849 unsigned int bit_pos;
850 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700851 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
Sheng Yongf6df8f22017-11-22 18:23:38 +0800852 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
Chao Yue1293bd2018-09-07 19:49:07 +0800853 struct blk_plug plug;
854 bool readdir_ra = sbi->readdir_ra == 1;
855 int err = 0;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700856
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700857 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700858
Chao Yue1293bd2018-09-07 19:49:07 +0800859 if (readdir_ra)
860 blk_start_plug(&plug);
861
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700862 while (bit_pos < d->max) {
863 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
864 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700865 break;
866
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700867 de = &d->dentry[bit_pos];
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800868 if (de->name_len == 0) {
869 bit_pos++;
870 ctx->pos = start_pos + bit_pos;
Chao Yuddf06b72019-01-08 10:21:24 +0800871 printk_ratelimited(
872 "%s, invalid namelen(0), ino:%u, run fsck to fix.",
873 KERN_WARNING, le32_to_cpu(de->ino));
874 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800875 continue;
876 }
877
Chao Yu4d57b862018-05-30 00:20:41 +0800878 d_type = f2fs_get_de_type(de);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700879
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700880 de_name.name = d->filename[bit_pos];
881 de_name.len = le16_to_cpu(de->name_len);
882
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800883 /* check memory boundary before moving forward */
884 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Sheng Yong720db062019-01-07 15:02:34 +0800885 if (unlikely(bit_pos > d->max ||
886 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800887 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
888 __func__, le16_to_cpu(de->name_len));
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800889 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu10f966b2019-06-20 11:36:14 +0800890 err = -EFSCORRUPTED;
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800891 goto out;
892 }
893
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530894 if (IS_ENCRYPTED(d->inode)) {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700895 int save_len = fstr->len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700896
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400897 err = fscrypt_fname_disk_to_usr(d->inode,
Chao Yu36af5f42019-05-28 17:23:33 +0800898 (u32)le32_to_cpu(de->hash_code),
899 0, &de_name, fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400900 if (err)
Chao Yue1293bd2018-09-07 19:49:07 +0800901 goto out;
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700902
903 de_name = *fstr;
904 fstr->len = save_len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700905 }
906
907 if (!dir_emit(ctx, de_name.name, de_name.len,
Chao Yue1293bd2018-09-07 19:49:07 +0800908 le32_to_cpu(de->ino), d_type)) {
909 err = 1;
910 goto out;
911 }
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700912
Chao Yue1293bd2018-09-07 19:49:07 +0800913 if (readdir_ra)
Chao Yu4d57b862018-05-30 00:20:41 +0800914 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
Sheng Yongf6df8f22017-11-22 18:23:38 +0800915
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700916 ctx->pos = start_pos + bit_pos;
917 }
Chao Yue1293bd2018-09-07 19:49:07 +0800918out:
919 if (readdir_ra)
920 blk_finish_plug(&plug);
921 return err;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700922}
923
Al Viro6f7f2312013-05-17 18:02:17 -0400924static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900925{
Al Viro496ad9a2013-01-23 17:07:38 -0500926 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900927 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900928 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900929 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800930 struct file_ra_state *ra = &file->f_ra;
Chao Yue97a3c42017-10-13 18:01:33 +0800931 loff_t start_pos = ctx->pos;
Al Viro6f7f2312013-05-17 18:02:17 -0400932 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700933 struct f2fs_dentry_ptr d;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700934 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700935 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900936
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530937 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700938 err = fscrypt_get_encryption_info(inode);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800939 if (err && err != -ENOKEY)
Chao Yue97a3c42017-10-13 18:01:33 +0800940 goto out;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700941
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700942 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700943 if (err < 0)
Chao Yue97a3c42017-10-13 18:01:33 +0800944 goto out;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700945 }
946
947 if (f2fs_has_inline_dentry(inode)) {
948 err = f2fs_read_inline_dir(file, ctx, &fstr);
Chao Yue97a3c42017-10-13 18:01:33 +0800949 goto out_free;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700950 }
Chao Yu622f28a2014-09-24 18:19:10 +0800951
Chao Yuab383be2017-10-13 18:01:36 +0800952 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
Chao Yu4414dea2017-10-13 18:01:34 +0800953
954 /* allow readdir() to be interrupted */
955 if (fatal_signal_pending(current)) {
956 err = -ERESTARTSYS;
957 goto out_free;
958 }
959 cond_resched();
960
Chao Yucb7a8442017-10-13 18:01:35 +0800961 /* readahead for multi pages of dir */
962 if (npages - n > 1 && !ra_has_index(ra, n))
963 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
964 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
965
Gao Xiang613f3dc2019-02-21 12:57:35 +0800966 dentry_page = f2fs_find_data_page(inode, n);
Chao Yu57b62d22015-11-19 16:09:07 +0800967 if (IS_ERR(dentry_page)) {
968 err = PTR_ERR(dentry_page);
Chao Yued6bd4b2016-10-29 18:46:34 +0800969 if (err == -ENOENT) {
970 err = 0;
Chao Yu57b62d22015-11-19 16:09:07 +0800971 continue;
Chao Yued6bd4b2016-10-29 18:46:34 +0800972 } else {
Chao Yue97a3c42017-10-13 18:01:33 +0800973 goto out_free;
Chao Yued6bd4b2016-10-29 18:46:34 +0800974 }
Chao Yu57b62d22015-11-19 16:09:07 +0800975 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900976
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800977 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900978
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300979 make_dentry_ptr_block(inode, &d, dentry_blk);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700980
Chao Yued6bd4b2016-10-29 18:46:34 +0800981 err = f2fs_fill_dentries(ctx, &d,
982 n * NR_DENTRY_IN_BLOCK, &fstr);
983 if (err) {
Gao Xiang613f3dc2019-02-21 12:57:35 +0800984 f2fs_put_page(dentry_page, 0);
Chao Yue9837bc2015-12-01 11:41:50 +0800985 break;
986 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900987
Gao Xiang613f3dc2019-02-21 12:57:35 +0800988 f2fs_put_page(dentry_page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900989 }
Chao Yue97a3c42017-10-13 18:01:33 +0800990out_free:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700991 fscrypt_fname_free_buffer(&fstr);
Chao Yue97a3c42017-10-13 18:01:33 +0800992out:
993 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
Chao Yued6bd4b2016-10-29 18:46:34 +0800994 return err < 0 ? err : 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900995}
996
Chao Yued3360a2016-02-14 18:56:55 +0800997static int f2fs_dir_open(struct inode *inode, struct file *filp)
998{
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530999 if (IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001000 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
Chao Yued3360a2016-02-14 18:56:55 +08001001 return 0;
1002}
1003
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001004const struct file_operations f2fs_dir_operations = {
1005 .llseek = generic_file_llseek,
1006 .read = generic_read_dir,
Al Viroe77d0c62016-05-10 16:41:13 -04001007 .iterate_shared = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001008 .fsync = f2fs_sync_file,
Chao Yued3360a2016-02-14 18:56:55 +08001009 .open = f2fs_dir_open,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001010 .unlocked_ioctl = f2fs_ioctl,
hujianyang08b95122015-05-12 16:05:57 +08001011#ifdef CONFIG_COMPAT
1012 .compat_ioctl = f2fs_compat_ioctl,
1013#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001014};
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001015
1016#ifdef CONFIG_UNICODE
1017static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
1018 const char *str, const struct qstr *name)
1019{
1020 struct qstr qstr = {.name = str, .len = len };
1021
1022 if (!IS_CASEFOLDED(dentry->d_parent->d_inode)) {
1023 if (len != name->len)
1024 return -1;
1025 return memcmp(str, name, len);
1026 }
1027
1028 return f2fs_ci_compare(dentry->d_parent->d_inode, name, &qstr);
1029}
1030
1031static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
1032{
1033 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1034 const struct unicode_map *um = sbi->s_encoding;
1035 unsigned char *norm;
1036 int len, ret = 0;
1037
1038 if (!IS_CASEFOLDED(dentry->d_inode))
1039 return 0;
1040
1041 norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
1042 if (!norm)
1043 return -ENOMEM;
1044
1045 len = utf8_casefold(um, str, norm, PATH_MAX);
1046 if (len < 0) {
1047 if (f2fs_has_strict_mode(sbi))
1048 ret = -EINVAL;
1049 goto out;
1050 }
1051 str->hash = full_name_hash(dentry, norm, len);
1052out:
1053 kvfree(norm);
1054 return ret;
1055}
1056
1057const struct dentry_operations f2fs_dentry_ops = {
1058 .d_hash = f2fs_d_hash,
1059 .d_compare = f2fs_d_compare,
1060};
1061#endif