blob: dac07d17cdbd0178ce8c8c720cd64ca0bbe18711 [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 Yu4d57b862018-05-30 00:20:41 +0800139struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700140 f2fs_hash_t namehash, int *max_slots,
141 struct f2fs_dentry_ptr *d)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900142{
143 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900144 unsigned long bit_pos = 0;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900145 int max_len = 0;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700146#ifdef CONFIG_UNICODE
147 struct qstr entry;
148#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900149
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700150 if (max_slots)
151 *max_slots = 0;
152 while (bit_pos < d->max) {
153 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900154 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800155 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900156 continue;
157 }
Chao Yubda19072015-03-09 17:33:16 +0800158
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700159 de = &d->dentry[bit_pos];
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700160#ifdef CONFIG_UNICODE
161 entry.name = d->filename[bit_pos];
162 entry.len = de->name_len;
163#endif
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700164
Chao Yua4a13f52016-04-27 22:22:20 +0800165 if (unlikely(!de->name_len)) {
166 bit_pos++;
167 continue;
168 }
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700169 if (de->hash_code == namehash) {
170#ifdef CONFIG_UNICODE
171 if (F2FS_SB(d->inode->i_sb)->s_encoding &&
172 IS_CASEFOLDED(d->inode) &&
173 !f2fs_ci_compare(d->inode,
174 fname->usr_fname, &entry))
175 goto found;
Chao Yua4a13f52016-04-27 22:22:20 +0800176
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700177#endif
178 if (fscrypt_match_name(fname, d->filename[bit_pos],
179 le16_to_cpu(de->name_len)))
180 goto found;
181 }
Eric Biggers1f73d492017-04-24 10:00:12 -0700182
Chao Yubda19072015-03-09 17:33:16 +0800183 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900184 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800185 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800186
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900187 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900188 }
189
190 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900191found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700192 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900193 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900194 return de;
195}
196
197static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700198 unsigned int level,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700199 struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700200 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900201{
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700202 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
203 int s = GET_DENTRY_SLOTS(name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900204 unsigned int nbucket, nblock;
205 unsigned int bidx, end_block;
206 struct page *dentry_page;
207 struct f2fs_dir_entry *de = NULL;
208 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700209 int max_slots;
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700210 f2fs_hash_t namehash = f2fs_dentry_hash(dir, &name, fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900211
Jaegeuk Kim38431542014-02-27 18:20:00 +0900212 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900213 nblock = bucket_blocks(level);
214
Jaegeuk Kim38431542014-02-27 18:20:00 +0900215 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
216 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900217 end_block = bidx + nblock;
218
219 for (; bidx < end_block; bidx++) {
220 /* no need to allocate new dentry pages to all the indices */
Chao Yu4d57b862018-05-30 00:20:41 +0800221 dentry_page = f2fs_find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900222 if (IS_ERR(dentry_page)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700223 if (PTR_ERR(dentry_page) == -ENOENT) {
224 room = true;
225 continue;
226 } else {
227 *res_page = dentry_page;
228 break;
229 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900230 }
231
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700232 de = find_in_block(dir, dentry_page, fname, namehash,
233 &max_slots, res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900234 if (de)
235 break;
236
237 if (max_slots >= s)
238 room = true;
239 f2fs_put_page(dentry_page, 0);
240 }
241
Sheng Yongd3bb9102017-04-22 10:39:20 +0800242 if (!de && room && F2FS_I(dir)->chash != namehash) {
243 F2FS_I(dir)->chash = namehash;
244 F2FS_I(dir)->clevel = level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900245 }
246
247 return de;
248}
249
Shuoran Liue7ba1082016-08-29 11:27:56 +0800250struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
251 struct fscrypt_name *fname, struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900252{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900253 unsigned long npages = dir_blocks(dir);
254 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900255 unsigned int max_depth;
256 unsigned int level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900257
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700258 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700259 *res_page = NULL;
Chao Yu4d57b862018-05-30 00:20:41 +0800260 de = f2fs_find_in_inline_dir(dir, fname, res_page);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700261 goto out;
262 }
263
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700264 if (npages == 0) {
265 *res_page = NULL;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700266 goto out;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700267 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700268
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900269 max_depth = F2FS_I(dir)->i_current_depth;
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800270 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800271 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
272 dir->i_ino, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800273 max_depth = MAX_DIR_HASH_DEPTH;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700274 f2fs_i_depth_write(dir, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800275 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900276
277 for (level = 0; level < max_depth; level++) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700278 *res_page = NULL;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800279 de = find_in_level(dir, level, fname, res_page);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700280 if (de || IS_ERR(*res_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900281 break;
282 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700283out:
Sheng Yongd3bb9102017-04-22 10:39:20 +0800284 /* This is to increase the speed of f2fs_create */
285 if (!de)
286 F2FS_I(dir)->task = current;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800287 return de;
288}
289
290/*
291 * Find an entry in the specified directory with the wanted name.
292 * It returns the page where the entry was found (as a parameter - res_page),
293 * and the entry itself. Page is returned mapped and unlocked.
294 * Entry is guaranteed to be valid.
295 */
296struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
297 const struct qstr *child, struct page **res_page)
298{
299 struct f2fs_dir_entry *de = NULL;
300 struct fscrypt_name fname;
301 int err;
302
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700303#ifdef CONFIG_UNICODE
304 if (f2fs_has_strict_mode(F2FS_I_SB(dir)) && IS_CASEFOLDED(dir) &&
305 utf8_validate(F2FS_I_SB(dir)->s_encoding, child)) {
306 *res_page = ERR_PTR(-EINVAL);
307 return NULL;
308 }
309#endif
310
Shuoran Liue7ba1082016-08-29 11:27:56 +0800311 err = fscrypt_setup_filename(dir, child, 1, &fname);
312 if (err) {
Eric Biggers54475f52016-12-05 11:12:44 -0800313 if (err == -ENOENT)
314 *res_page = NULL;
315 else
316 *res_page = ERR_PTR(err);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800317 return NULL;
318 }
319
320 de = __f2fs_find_entry(dir, &fname, res_page);
321
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700322 fscrypt_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900323 return de;
324}
325
326struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
327{
Sheng Yong8be0fea2016-06-04 22:01:28 +0800328 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900329
Sheng Yong8be0fea2016-06-04 22:01:28 +0800330 return f2fs_find_entry(dir, &dotdot, p);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900331}
332
Linus Torvalds835c92d2016-08-06 09:49:02 -0400333ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
Chao Yu91246c22016-07-19 08:27:47 +0800334 struct page **page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900335{
336 ino_t res = 0;
337 struct f2fs_dir_entry *de;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900338
Chao Yu91246c22016-07-19 08:27:47 +0800339 de = f2fs_find_entry(dir, qstr, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900340 if (de) {
341 res = le32_to_cpu(de->ino);
Chao Yu91246c22016-07-19 08:27:47 +0800342 f2fs_put_page(*page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900343 }
344
345 return res;
346}
347
348void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
349 struct page *page, struct inode *inode)
350{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700351 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900352 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800353 f2fs_wait_on_page_writeback(page, type, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900354 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700355 set_de_type(de, inode->i_mode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900356 set_page_dirty(page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900357
Deepa Dinamani078cd822016-09-14 07:48:04 -0700358 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700359 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900360 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900361}
362
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900363static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900364{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900365 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900366
Chao Yubae0ee72018-12-25 17:43:42 +0800367 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900368
Al Viro53dc9a62013-01-25 16:01:21 -0500369 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900370 ri = F2FS_INODE(ipage);
371 ri->i_namelen = cpu_to_le32(name->len);
372 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900373 set_page_dirty(ipage);
374}
375
Chao Yu4d57b862018-05-30 00:20:41 +0800376void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700377 struct f2fs_dentry_ptr *d)
378{
Chao Yu291bf802016-03-09 22:07:28 +0800379 struct qstr dot = QSTR_INIT(".", 1);
380 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700381
Chao Yu291bf802016-03-09 22:07:28 +0800382 /* update dirent of "." */
383 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700384
Chao Yu291bf802016-03-09 22:07:28 +0800385 /* update dirent of ".." */
386 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700387}
388
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900389static int make_empty_dir(struct inode *inode,
390 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900391{
392 struct page *dentry_page;
393 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700394 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900395
Chao Yu622f28a2014-09-24 18:19:10 +0800396 if (f2fs_has_inline_dentry(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800397 return f2fs_make_empty_inline_dir(inode, parent, page);
Chao Yu622f28a2014-09-24 18:19:10 +0800398
Chao Yu4d57b862018-05-30 00:20:41 +0800399 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900400 if (IS_ERR(dentry_page))
401 return PTR_ERR(dentry_page);
402
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800403 dentry_blk = page_address(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900404
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300405 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +0800406 f2fs_do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900407
Jaegeuk Kim39936832012-11-22 16:21:29 +0900408 set_page_dirty(dentry_page);
409 f2fs_put_page(dentry_page, 1);
410 return 0;
411}
412
Chao Yu4d57b862018-05-30 00:20:41 +0800413struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Chao Yu9421d572016-08-28 18:57:55 +0800414 const struct qstr *new_name, const struct qstr *orig_name,
415 struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900416{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900417 struct page *page;
Sheng Yongff62af22018-03-15 18:51:42 +0800418 int dummy_encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900419 int err;
420
Jaegeuk Kim91942322016-05-20 10:13:22 -0700421 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800422 page = f2fs_new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900423 if (IS_ERR(page))
424 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900425
426 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700427 /* in order to handle error case */
428 get_page(page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900429 err = make_empty_dir(inode, dir, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700430 if (err) {
431 lock_page(page);
432 goto put_error;
433 }
434 put_page(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900435 }
436
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700437 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900438 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900439 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900440
Chao Yu9421d572016-08-28 18:57:55 +0800441 err = f2fs_init_security(inode, dir, orig_name, page);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900442 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900443 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700444
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530445 if ((IS_ENCRYPTED(dir) || dummy_encrypt) &&
Sheng Yongff62af22018-03-15 18:51:42 +0800446 f2fs_may_encrypt(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700447 err = fscrypt_inherit_context(dir, inode, page, false);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700448 if (err)
449 goto put_error;
450 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900451 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800452 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900453 if (IS_ERR(page))
454 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900455 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900456
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800457 if (new_name) {
Chao Yu9421d572016-08-28 18:57:55 +0800458 init_dent_inode(new_name, page);
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530459 if (IS_ENCRYPTED(dir))
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800460 file_set_enc_name(inode);
461 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900462
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900463 /*
464 * This file should be checkpointed during fsync.
465 * We lost i_pino from now on.
466 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700467 if (is_inode_flag_set(inode, FI_INC_LINK)) {
Sheng Yongd58dfb72017-06-26 10:41:35 +0800468 if (!S_ISDIR(inode->i_mode))
469 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800470 /*
471 * If link the tmpfile to alias through linkat path,
472 * we should remove this inode from orphan list.
473 */
474 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800475 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700476 f2fs_i_links_write(inode, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900477 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900478 return page;
479
Jaegeuk Kima8865372013-12-27 17:04:17 +0900480put_error:
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700481 clear_nlink(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800482 f2fs_update_inode(inode, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700483 f2fs_put_page(page, 1);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900484 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900485}
486
Chao Yu4d57b862018-05-30 00:20:41 +0800487void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900488 unsigned int current_depth)
489{
Jaegeuk Kim91942322016-05-20 10:13:22 -0700490 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700491 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700492 f2fs_i_links_write(dir, true);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700493 clear_inode_flag(inode, FI_NEW_INODE);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900494 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700495 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700496 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900497
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700498 if (F2FS_I(dir)->i_current_depth != current_depth)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700499 f2fs_i_depth_write(dir, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900500
Jaegeuk Kim91942322016-05-20 10:13:22 -0700501 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
502 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900503}
504
Chao Yu4d57b862018-05-30 00:20:41 +0800505int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900506{
507 int bit_start = 0;
508 int zero_start, zero_end;
509next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700510 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
511 if (zero_start >= max_slots)
512 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900513
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700514 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900515 if (zero_end - zero_start >= slots)
516 return zero_start;
517
518 bit_start = zero_end + 1;
519
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700520 if (zero_end + 1 >= max_slots)
521 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900522 goto next;
523}
524
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700525void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Chao Yu3b4d7322015-02-16 16:17:20 +0800526 const struct qstr *name, f2fs_hash_t name_hash,
527 unsigned int bit_pos)
528{
529 struct f2fs_dir_entry *de;
530 int slots = GET_DENTRY_SLOTS(name->len);
531 int i;
532
533 de = &d->dentry[bit_pos];
534 de->hash_code = name_hash;
535 de->name_len = cpu_to_le16(name->len);
536 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700537 de->ino = cpu_to_le32(ino);
538 set_de_type(de, mode);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800539 for (i = 0; i < slots; i++) {
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700540 __set_bit_le(bit_pos + i, (void *)d->bitmap);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800541 /* avoid wrong garbage data for readdir */
542 if (i)
543 (de + i)->name_len = 0;
544 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800545}
546
Chao Yu675f10b2016-02-22 18:29:18 +0800547int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800548 const struct qstr *orig_name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700549 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900550{
551 unsigned int bit_pos;
552 unsigned int level;
553 unsigned int current_depth;
554 unsigned long bidx, block;
555 f2fs_hash_t dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900556 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900557 struct page *dentry_page = NULL;
558 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800559 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700560 struct page *page = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800561 int slots, err = 0;
Chao Yu622f28a2014-09-24 18:19:10 +0800562
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900563 level = 0;
Chao Yu675f10b2016-02-22 18:29:18 +0800564 slots = GET_DENTRY_SLOTS(new_name->len);
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -0700565 dentry_hash = f2fs_dentry_hash(dir, new_name, NULL);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700566
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900567 current_depth = F2FS_I(dir)->i_current_depth;
568 if (F2FS_I(dir)->chash == dentry_hash) {
569 level = F2FS_I(dir)->clevel;
570 F2FS_I(dir)->chash = 0;
571 }
572
573start:
Chao Yu55523512017-02-25 11:08:28 +0800574 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
575 f2fs_show_injection_info(FAULT_DIR_DEPTH);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700576 return -ENOSPC;
Chao Yu55523512017-02-25 11:08:28 +0800577 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200578
Chao Yu675f10b2016-02-22 18:29:18 +0800579 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
580 return -ENOSPC;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900581
582 /* Increase the depth, if required */
583 if (level == current_depth)
584 ++current_depth;
585
Jaegeuk Kim38431542014-02-27 18:20:00 +0900586 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900587 nblock = bucket_blocks(level);
588
Jaegeuk Kim38431542014-02-27 18:20:00 +0900589 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
590 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900591
592 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800593 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
Chao Yu675f10b2016-02-22 18:29:18 +0800594 if (IS_ERR(dentry_page))
595 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900596
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800597 dentry_blk = page_address(dentry_page);
Chao Yu4d57b862018-05-30 00:20:41 +0800598 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700599 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900600 if (bit_pos < NR_DENTRY_IN_BLOCK)
601 goto add_dentry;
602
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900603 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900604 }
605
606 /* Move to next level to find the empty slot for new dentry */
607 ++level;
608 goto start;
609add_dentry:
Chao Yubae0ee72018-12-25 17:43:42 +0800610 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900611
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700612 if (inode) {
613 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800614 page = f2fs_init_inode_metadata(inode, dir, new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800615 orig_name, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700616 if (IS_ERR(page)) {
617 err = PTR_ERR(page);
618 goto fail;
619 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900620 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800621
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300622 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu675f10b2016-02-22 18:29:18 +0800623 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800624
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900625 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900626
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700627 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700628 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700629 f2fs_put_page(page, 1);
630 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900631
Chao Yu4d57b862018-05-30 00:20:41 +0800632 f2fs_update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900633fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700634 if (inode)
635 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900636
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900637 f2fs_put_page(dentry_page, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800638
639 return err;
640}
641
Chao Yu4d57b862018-05-30 00:20:41 +0800642int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
Shuoran Liue7ba1082016-08-29 11:27:56 +0800643 struct inode *inode, nid_t ino, umode_t mode)
644{
645 struct qstr new_name;
646 int err = -EAGAIN;
647
648 new_name.name = fname_name(fname);
649 new_name.len = fname_len(fname);
650
651 if (f2fs_has_inline_dentry(dir))
652 err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
653 inode, ino, mode);
654 if (err == -EAGAIN)
655 err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
656 inode, ino, mode);
657
658 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
659 return err;
660}
661
Chao Yu675f10b2016-02-22 18:29:18 +0800662/*
663 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
664 * f2fs_unlock_op().
665 */
Chao Yu4d57b862018-05-30 00:20:41 +0800666int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Chao Yu675f10b2016-02-22 18:29:18 +0800667 struct inode *inode, nid_t ino, umode_t mode)
668{
669 struct fscrypt_name fname;
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800670 struct page *page = NULL;
671 struct f2fs_dir_entry *de = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800672 int err;
673
674 err = fscrypt_setup_filename(dir, name, 0, &fname);
675 if (err)
676 return err;
677
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800678 /*
679 * An immature stakable filesystem shows a race condition between lookup
680 * and create. If we have same task when doing lookup and create, it's
681 * definitely fine as expected by VFS normally. Otherwise, let's just
682 * verify on-disk dentry one more time, which guarantees filesystem
683 * consistency more.
684 */
685 if (current != F2FS_I(dir)->task) {
686 de = __f2fs_find_entry(dir, &fname, &page);
687 F2FS_I(dir)->task = NULL;
688 }
689 if (de) {
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800690 f2fs_put_page(page, 0);
691 err = -EEXIST;
692 } else if (IS_ERR(page)) {
693 err = PTR_ERR(page);
694 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800695 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800696 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700697 fscrypt_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900698 return err;
699}
700
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700701int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
702{
703 struct page *page;
704 int err = 0;
705
706 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800707 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700708 if (IS_ERR(page)) {
709 err = PTR_ERR(page);
710 goto fail;
711 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700712 f2fs_put_page(page, 1);
713
Jaegeuk Kim91942322016-05-20 10:13:22 -0700714 clear_inode_flag(inode, FI_NEW_INODE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530715 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700716fail:
717 up_write(&F2FS_I(inode)->i_sem);
718 return err;
719}
720
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700721void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
Chao Yudbeacf02014-09-24 18:17:04 +0800722{
723 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
724
725 down_write(&F2FS_I(inode)->i_sem);
726
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700727 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700728 f2fs_i_links_write(dir, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700729 inode->i_ctime = current_time(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800730
Jaegeuk Kima1961242016-05-20 09:43:20 -0700731 f2fs_i_links_write(inode, false);
Chao Yudbeacf02014-09-24 18:17:04 +0800732 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700733 f2fs_i_links_write(inode, false);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700734 f2fs_i_size_write(inode, 0);
Chao Yudbeacf02014-09-24 18:17:04 +0800735 }
736 up_write(&F2FS_I(inode)->i_sem);
Chao Yudbeacf02014-09-24 18:17:04 +0800737
738 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800739 f2fs_add_orphan_inode(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800740 else
Chao Yu4d57b862018-05-30 00:20:41 +0800741 f2fs_release_orphan_inode(sbi);
Chao Yudbeacf02014-09-24 18:17:04 +0800742}
743
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900744/*
arter97e1c42042014-08-06 23:22:50 +0900745 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900746 * entry in name page does not need to be touched during deletion.
747 */
748void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800749 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900750{
751 struct f2fs_dentry_block *dentry_blk;
752 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900753 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900754 int i;
755
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800756 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
757
Chao Yu63189b72018-03-08 14:22:56 +0800758 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
Chao Yu4d57b862018-05-30 00:20:41 +0800759 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800760
Chao Yu622f28a2014-09-24 18:19:10 +0800761 if (f2fs_has_inline_dentry(dir))
762 return f2fs_delete_inline_entry(dentry, page, dir, inode);
763
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900764 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800765 f2fs_wait_on_page_writeback(page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900766
Gu Zheng1c3bb972014-06-27 17:57:04 +0800767 dentry_blk = page_address(page);
768 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900769 for (i = 0; i < slots; i++)
Jaegeuk Kim23380b82017-03-07 14:11:06 -0800770 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900771
772 /* Let's check and deallocate this dentry page */
773 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
774 NR_DENTRY_IN_BLOCK,
775 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900776 set_page_dirty(page);
777
Deepa Dinamani078cd822016-09-14 07:48:04 -0700778 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700779 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900780
Chao Yudbeacf02014-09-24 18:17:04 +0800781 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700782 f2fs_drop_nlink(dir, inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900783
Chao Yu206e61b2015-08-12 17:48:21 +0800784 if (bit_pos == NR_DENTRY_IN_BLOCK &&
Chao Yu4d57b862018-05-30 00:20:41 +0800785 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500786 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900787 clear_page_dirty_for_io(page);
Chao Yu240a5912019-03-06 17:30:59 +0800788 f2fs_clear_page_private(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900789 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800790 clear_cold_data(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700791 inode_dec_dirty_pages(dir);
Chao Yu4d57b862018-05-30 00:20:41 +0800792 f2fs_remove_dirty_inode(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900793 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900794 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900795}
796
797bool f2fs_empty_dir(struct inode *dir)
798{
799 unsigned long bidx;
800 struct page *dentry_page;
801 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800802 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900803 unsigned long nblock = dir_blocks(dir);
804
Chao Yu622f28a2014-09-24 18:19:10 +0800805 if (f2fs_has_inline_dentry(dir))
806 return f2fs_empty_inline_dir(dir);
807
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900808 for (bidx = 0; bidx < nblock; bidx++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800809 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900810 if (IS_ERR(dentry_page)) {
811 if (PTR_ERR(dentry_page) == -ENOENT)
812 continue;
813 else
814 return false;
815 }
816
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800817 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900818 if (bidx == 0)
819 bit_pos = 2;
820 else
821 bit_pos = 0;
822 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
823 NR_DENTRY_IN_BLOCK,
824 bit_pos);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900825
826 f2fs_put_page(dentry_page, 1);
827
828 if (bit_pos < NR_DENTRY_IN_BLOCK)
829 return false;
830 }
831 return true;
832}
833
Chao Yued6bd4b2016-10-29 18:46:34 +0800834int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700835 unsigned int start_pos, struct fscrypt_str *fstr)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700836{
837 unsigned char d_type = DT_UNKNOWN;
838 unsigned int bit_pos;
839 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700840 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
Sheng Yongf6df8f22017-11-22 18:23:38 +0800841 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
Chao Yue1293bd2018-09-07 19:49:07 +0800842 struct blk_plug plug;
843 bool readdir_ra = sbi->readdir_ra == 1;
844 int err = 0;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700845
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700846 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700847
Chao Yue1293bd2018-09-07 19:49:07 +0800848 if (readdir_ra)
849 blk_start_plug(&plug);
850
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700851 while (bit_pos < d->max) {
852 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
853 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700854 break;
855
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700856 de = &d->dentry[bit_pos];
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800857 if (de->name_len == 0) {
858 bit_pos++;
859 ctx->pos = start_pos + bit_pos;
Chao Yuddf06b72019-01-08 10:21:24 +0800860 printk_ratelimited(
861 "%s, invalid namelen(0), ino:%u, run fsck to fix.",
862 KERN_WARNING, le32_to_cpu(de->ino));
863 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800864 continue;
865 }
866
Chao Yu4d57b862018-05-30 00:20:41 +0800867 d_type = f2fs_get_de_type(de);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700868
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700869 de_name.name = d->filename[bit_pos];
870 de_name.len = le16_to_cpu(de->name_len);
871
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800872 /* check memory boundary before moving forward */
873 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Sheng Yong720db062019-01-07 15:02:34 +0800874 if (unlikely(bit_pos > d->max ||
875 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800876 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
877 __func__, le16_to_cpu(de->name_len));
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800878 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu10f966b2019-06-20 11:36:14 +0800879 err = -EFSCORRUPTED;
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800880 goto out;
881 }
882
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530883 if (IS_ENCRYPTED(d->inode)) {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700884 int save_len = fstr->len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700885
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400886 err = fscrypt_fname_disk_to_usr(d->inode,
Chao Yu36af5f42019-05-28 17:23:33 +0800887 (u32)le32_to_cpu(de->hash_code),
888 0, &de_name, fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400889 if (err)
Chao Yue1293bd2018-09-07 19:49:07 +0800890 goto out;
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700891
892 de_name = *fstr;
893 fstr->len = save_len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700894 }
895
896 if (!dir_emit(ctx, de_name.name, de_name.len,
Chao Yue1293bd2018-09-07 19:49:07 +0800897 le32_to_cpu(de->ino), d_type)) {
898 err = 1;
899 goto out;
900 }
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700901
Chao Yue1293bd2018-09-07 19:49:07 +0800902 if (readdir_ra)
Chao Yu4d57b862018-05-30 00:20:41 +0800903 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
Sheng Yongf6df8f22017-11-22 18:23:38 +0800904
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700905 ctx->pos = start_pos + bit_pos;
906 }
Chao Yue1293bd2018-09-07 19:49:07 +0800907out:
908 if (readdir_ra)
909 blk_finish_plug(&plug);
910 return err;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700911}
912
Al Viro6f7f2312013-05-17 18:02:17 -0400913static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900914{
Al Viro496ad9a2013-01-23 17:07:38 -0500915 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900916 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900917 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900918 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800919 struct file_ra_state *ra = &file->f_ra;
Chao Yue97a3c42017-10-13 18:01:33 +0800920 loff_t start_pos = ctx->pos;
Al Viro6f7f2312013-05-17 18:02:17 -0400921 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700922 struct f2fs_dentry_ptr d;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700923 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700924 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900925
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530926 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700927 err = fscrypt_get_encryption_info(inode);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800928 if (err && err != -ENOKEY)
Chao Yue97a3c42017-10-13 18:01:33 +0800929 goto out;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700930
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700931 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700932 if (err < 0)
Chao Yue97a3c42017-10-13 18:01:33 +0800933 goto out;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700934 }
935
936 if (f2fs_has_inline_dentry(inode)) {
937 err = f2fs_read_inline_dir(file, ctx, &fstr);
Chao Yue97a3c42017-10-13 18:01:33 +0800938 goto out_free;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700939 }
Chao Yu622f28a2014-09-24 18:19:10 +0800940
Chao Yuab383be2017-10-13 18:01:36 +0800941 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
Chao Yu4414dea2017-10-13 18:01:34 +0800942
943 /* allow readdir() to be interrupted */
944 if (fatal_signal_pending(current)) {
945 err = -ERESTARTSYS;
946 goto out_free;
947 }
948 cond_resched();
949
Chao Yucb7a8442017-10-13 18:01:35 +0800950 /* readahead for multi pages of dir */
951 if (npages - n > 1 && !ra_has_index(ra, n))
952 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
953 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
954
Gao Xiang613f3dc2019-02-21 12:57:35 +0800955 dentry_page = f2fs_find_data_page(inode, n);
Chao Yu57b62d22015-11-19 16:09:07 +0800956 if (IS_ERR(dentry_page)) {
957 err = PTR_ERR(dentry_page);
Chao Yued6bd4b2016-10-29 18:46:34 +0800958 if (err == -ENOENT) {
959 err = 0;
Chao Yu57b62d22015-11-19 16:09:07 +0800960 continue;
Chao Yued6bd4b2016-10-29 18:46:34 +0800961 } else {
Chao Yue97a3c42017-10-13 18:01:33 +0800962 goto out_free;
Chao Yued6bd4b2016-10-29 18:46:34 +0800963 }
Chao Yu57b62d22015-11-19 16:09:07 +0800964 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900965
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800966 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900967
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300968 make_dentry_ptr_block(inode, &d, dentry_blk);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700969
Chao Yued6bd4b2016-10-29 18:46:34 +0800970 err = f2fs_fill_dentries(ctx, &d,
971 n * NR_DENTRY_IN_BLOCK, &fstr);
972 if (err) {
Gao Xiang613f3dc2019-02-21 12:57:35 +0800973 f2fs_put_page(dentry_page, 0);
Chao Yue9837bc2015-12-01 11:41:50 +0800974 break;
975 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900976
Gao Xiang613f3dc2019-02-21 12:57:35 +0800977 f2fs_put_page(dentry_page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900978 }
Chao Yue97a3c42017-10-13 18:01:33 +0800979out_free:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700980 fscrypt_fname_free_buffer(&fstr);
Chao Yue97a3c42017-10-13 18:01:33 +0800981out:
982 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
Chao Yued6bd4b2016-10-29 18:46:34 +0800983 return err < 0 ? err : 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900984}
985
Chao Yued3360a2016-02-14 18:56:55 +0800986static int f2fs_dir_open(struct inode *inode, struct file *filp)
987{
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530988 if (IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700989 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
Chao Yued3360a2016-02-14 18:56:55 +0800990 return 0;
991}
992
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900993const struct file_operations f2fs_dir_operations = {
994 .llseek = generic_file_llseek,
995 .read = generic_read_dir,
Al Viroe77d0c62016-05-10 16:41:13 -0400996 .iterate_shared = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900997 .fsync = f2fs_sync_file,
Chao Yued3360a2016-02-14 18:56:55 +0800998 .open = f2fs_dir_open,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900999 .unlocked_ioctl = f2fs_ioctl,
hujianyang08b95122015-05-12 16:05:57 +08001000#ifdef CONFIG_COMPAT
1001 .compat_ioctl = f2fs_compat_ioctl,
1002#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +09001003};
Daniel Rosenberg2c2eb7a2019-07-23 16:05:29 -07001004
1005#ifdef CONFIG_UNICODE
1006static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
1007 const char *str, const struct qstr *name)
1008{
1009 struct qstr qstr = {.name = str, .len = len };
1010
1011 if (!IS_CASEFOLDED(dentry->d_parent->d_inode)) {
1012 if (len != name->len)
1013 return -1;
1014 return memcmp(str, name, len);
1015 }
1016
1017 return f2fs_ci_compare(dentry->d_parent->d_inode, name, &qstr);
1018}
1019
1020static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
1021{
1022 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1023 const struct unicode_map *um = sbi->s_encoding;
1024 unsigned char *norm;
1025 int len, ret = 0;
1026
1027 if (!IS_CASEFOLDED(dentry->d_inode))
1028 return 0;
1029
1030 norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
1031 if (!norm)
1032 return -ENOMEM;
1033
1034 len = utf8_casefold(um, str, norm, PATH_MAX);
1035 if (len < 0) {
1036 if (f2fs_has_strict_mode(sbi))
1037 ret = -EINVAL;
1038 goto out;
1039 }
1040 str->hash = full_name_hash(dentry, norm, len);
1041out:
1042 kvfree(norm);
1043 return ret;
1044}
1045
1046const struct dentry_operations f2fs_dentry_ops = {
1047 .d_hash = f2fs_d_hash,
1048 .d_compare = f2fs_d_compare,
1049};
1050#endif