blob: 0e78edbb8e16ca629a85da1b7039e7d16ffbad01 [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>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090011#include "f2fs.h"
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +090012#include "node.h"
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090013#include "acl.h"
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090014#include "xattr.h"
Chao Yue97a3c42017-10-13 18:01:33 +080015#include <trace/events/f2fs.h>
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090016
17static unsigned long dir_blocks(struct inode *inode)
18{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030019 return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
20 >> PAGE_SHIFT;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090021}
22
Jaegeuk Kim38431542014-02-27 18:20:00 +090023static unsigned int dir_buckets(unsigned int level, int dir_level)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090024{
Chao Yubfec07d2014-05-28 08:56:09 +080025 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
Jaegeuk Kim38431542014-02-27 18:20:00 +090026 return 1 << (level + dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090027 else
Chao Yubfec07d2014-05-28 08:56:09 +080028 return MAX_DIR_BUCKETS;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090029}
30
31static unsigned int bucket_blocks(unsigned int level)
32{
33 if (level < MAX_DIR_HASH_DEPTH / 2)
34 return 2;
35 else
36 return 4;
37}
38
Chao Yuebfa7322016-09-18 23:30:03 +080039static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090040 [F2FS_FT_UNKNOWN] = DT_UNKNOWN,
41 [F2FS_FT_REG_FILE] = DT_REG,
42 [F2FS_FT_DIR] = DT_DIR,
43 [F2FS_FT_CHRDEV] = DT_CHR,
44 [F2FS_FT_BLKDEV] = DT_BLK,
45 [F2FS_FT_FIFO] = DT_FIFO,
46 [F2FS_FT_SOCK] = DT_SOCK,
47 [F2FS_FT_SYMLINK] = DT_LNK,
48};
49
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090050static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
51 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
52 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
53 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
54 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
55 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
56 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
57 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
58};
59
Chao Yu2e79d952018-05-30 00:20:40 +080060static void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090061{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090062 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
63}
64
Chao Yu4d57b862018-05-30 00:20:41 +080065unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
Chao Yu675f10b2016-02-22 18:29:18 +080066{
67 if (de->file_type < F2FS_FT_MAX)
68 return f2fs_filetype_table[de->file_type];
69 return DT_UNKNOWN;
70}
71
Jaegeuk Kim38431542014-02-27 18:20:00 +090072static unsigned long dir_block_index(unsigned int level,
73 int dir_level, unsigned int idx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090074{
75 unsigned long i;
76 unsigned long bidx = 0;
77
78 for (i = 0; i < level; i++)
Jaegeuk Kim38431542014-02-27 18:20:00 +090079 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090080 bidx += idx * bucket_blocks(level);
81 return bidx;
82}
83
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +090084static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070085 struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -070086 f2fs_hash_t namehash,
87 int *max_slots,
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070088 struct page **res_page)
89{
90 struct f2fs_dentry_block *dentry_blk;
91 struct f2fs_dir_entry *de;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -070092 struct f2fs_dentry_ptr d;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070093
Yunlong Songbdbc90f2018-02-28 20:31:52 +080094 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -070095
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +030096 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +080097 de = f2fs_find_target_dentry(fname, namehash, max_slots, &d);
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -070098 if (de)
99 *res_page = dentry_page;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700100
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700101 return de;
102}
103
Chao Yu4d57b862018-05-30 00:20:41 +0800104struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700105 f2fs_hash_t namehash, int *max_slots,
106 struct f2fs_dentry_ptr *d)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900107{
108 struct f2fs_dir_entry *de;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900109 unsigned long bit_pos = 0;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900110 int max_len = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900111
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700112 if (max_slots)
113 *max_slots = 0;
114 while (bit_pos < d->max) {
115 if (!test_bit_le(bit_pos, d->bitmap)) {
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900116 bit_pos++;
Chao Yubda19072015-03-09 17:33:16 +0800117 max_len++;
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900118 continue;
119 }
Chao Yubda19072015-03-09 17:33:16 +0800120
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700121 de = &d->dentry[bit_pos];
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700122
Chao Yua4a13f52016-04-27 22:22:20 +0800123 if (unlikely(!de->name_len)) {
124 bit_pos++;
125 continue;
126 }
127
Eric Biggers1f73d492017-04-24 10:00:12 -0700128 if (de->hash_code == namehash &&
129 fscrypt_match_name(fname, d->filename[bit_pos],
130 le16_to_cpu(de->name_len)))
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700131 goto found;
Eric Biggers1f73d492017-04-24 10:00:12 -0700132
Chao Yubda19072015-03-09 17:33:16 +0800133 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900134 *max_slots = max_len;
Chao Yubda19072015-03-09 17:33:16 +0800135 max_len = 0;
Chao Yu81e366f2014-07-10 12:37:46 +0800136
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900137 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900138 }
139
140 de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900141found:
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700142 if (max_slots && max_len > *max_slots)
Jaegeuk Kim5d0c66712014-02-27 13:57:53 +0900143 *max_slots = max_len;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900144 return de;
145}
146
147static struct f2fs_dir_entry *find_in_level(struct inode *dir,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700148 unsigned int level,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700149 struct fscrypt_name *fname,
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700150 struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900151{
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700152 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
153 int s = GET_DENTRY_SLOTS(name.len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900154 unsigned int nbucket, nblock;
155 unsigned int bidx, end_block;
156 struct page *dentry_page;
157 struct f2fs_dir_entry *de = NULL;
158 bool room = false;
Jaegeuk Kim4e6ebf62014-10-13 17:26:14 -0700159 int max_slots;
Jaegeuk Kim6332cd32017-04-24 10:00:08 -0700160 f2fs_hash_t namehash = f2fs_dentry_hash(&name, fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900161
Jaegeuk Kim38431542014-02-27 18:20:00 +0900162 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900163 nblock = bucket_blocks(level);
164
Jaegeuk Kim38431542014-02-27 18:20:00 +0900165 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
166 le32_to_cpu(namehash) % nbucket);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900167 end_block = bidx + nblock;
168
169 for (; bidx < end_block; bidx++) {
170 /* no need to allocate new dentry pages to all the indices */
Chao Yu4d57b862018-05-30 00:20:41 +0800171 dentry_page = f2fs_find_data_page(dir, bidx);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900172 if (IS_ERR(dentry_page)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700173 if (PTR_ERR(dentry_page) == -ENOENT) {
174 room = true;
175 continue;
176 } else {
177 *res_page = dentry_page;
178 break;
179 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900180 }
181
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700182 de = find_in_block(dentry_page, fname, namehash, &max_slots,
183 res_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900184 if (de)
185 break;
186
187 if (max_slots >= s)
188 room = true;
189 f2fs_put_page(dentry_page, 0);
190 }
191
Sheng Yongd3bb9102017-04-22 10:39:20 +0800192 if (!de && room && F2FS_I(dir)->chash != namehash) {
193 F2FS_I(dir)->chash = namehash;
194 F2FS_I(dir)->clevel = level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900195 }
196
197 return de;
198}
199
Shuoran Liue7ba1082016-08-29 11:27:56 +0800200struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
201 struct fscrypt_name *fname, struct page **res_page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900202{
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900203 unsigned long npages = dir_blocks(dir);
204 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900205 unsigned int max_depth;
206 unsigned int level;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900207
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700208 if (f2fs_has_inline_dentry(dir)) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700209 *res_page = NULL;
Chao Yu4d57b862018-05-30 00:20:41 +0800210 de = f2fs_find_in_inline_dir(dir, fname, res_page);
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700211 goto out;
212 }
213
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700214 if (npages == 0) {
215 *res_page = NULL;
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700216 goto out;
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700217 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700218
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900219 max_depth = F2FS_I(dir)->i_current_depth;
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800220 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800221 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
222 dir->i_ino, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800223 max_depth = MAX_DIR_HASH_DEPTH;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700224 f2fs_i_depth_write(dir, max_depth);
Jaegeuk Kim1f6fa262015-12-31 10:28:52 -0800225 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900226
227 for (level = 0; level < max_depth; level++) {
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700228 *res_page = NULL;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800229 de = find_in_level(dir, level, fname, res_page);
Jaegeuk Kim42d96402016-05-25 14:29:11 -0700230 if (de || IS_ERR(*res_page))
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900231 break;
232 }
Jaegeuk Kim6e22c692015-04-27 17:12:39 -0700233out:
Sheng Yongd3bb9102017-04-22 10:39:20 +0800234 /* This is to increase the speed of f2fs_create */
235 if (!de)
236 F2FS_I(dir)->task = current;
Shuoran Liue7ba1082016-08-29 11:27:56 +0800237 return de;
238}
239
240/*
241 * Find an entry in the specified directory with the wanted name.
242 * It returns the page where the entry was found (as a parameter - res_page),
243 * and the entry itself. Page is returned mapped and unlocked.
244 * Entry is guaranteed to be valid.
245 */
246struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
247 const struct qstr *child, struct page **res_page)
248{
249 struct f2fs_dir_entry *de = NULL;
250 struct fscrypt_name fname;
251 int err;
252
253 err = fscrypt_setup_filename(dir, child, 1, &fname);
254 if (err) {
Eric Biggers54475f52016-12-05 11:12:44 -0800255 if (err == -ENOENT)
256 *res_page = NULL;
257 else
258 *res_page = ERR_PTR(err);
Shuoran Liue7ba1082016-08-29 11:27:56 +0800259 return NULL;
260 }
261
262 de = __f2fs_find_entry(dir, &fname, res_page);
263
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700264 fscrypt_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900265 return de;
266}
267
268struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
269{
Sheng Yong8be0fea2016-06-04 22:01:28 +0800270 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900271
Sheng Yong8be0fea2016-06-04 22:01:28 +0800272 return f2fs_find_entry(dir, &dotdot, p);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900273}
274
Linus Torvalds835c92d2016-08-06 09:49:02 -0400275ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
Chao Yu91246c22016-07-19 08:27:47 +0800276 struct page **page)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900277{
278 ino_t res = 0;
279 struct f2fs_dir_entry *de;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900280
Chao Yu91246c22016-07-19 08:27:47 +0800281 de = f2fs_find_entry(dir, qstr, page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900282 if (de) {
283 res = le32_to_cpu(de->ino);
Chao Yu91246c22016-07-19 08:27:47 +0800284 f2fs_put_page(*page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900285 }
286
287 return res;
288}
289
290void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
291 struct page *page, struct inode *inode)
292{
Jaegeuk Kim59a06152014-10-13 19:34:26 -0700293 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900294 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800295 f2fs_wait_on_page_writeback(page, type, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900296 de->ino = cpu_to_le32(inode->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700297 set_de_type(de, inode->i_mode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900298 set_page_dirty(page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900299
Deepa Dinamani078cd822016-09-14 07:48:04 -0700300 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700301 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900302 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900303}
304
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900305static void init_dent_inode(const struct qstr *name, struct page *ipage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900306{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900307 struct f2fs_inode *ri;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900308
Chao Yubae0ee72018-12-25 17:43:42 +0800309 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900310
Al Viro53dc9a62013-01-25 16:01:21 -0500311 /* copy name info. to this inode page */
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900312 ri = F2FS_INODE(ipage);
313 ri->i_namelen = cpu_to_le32(name->len);
314 memcpy(ri->i_name, name->name, name->len);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900315 set_page_dirty(ipage);
316}
317
Chao Yu4d57b862018-05-30 00:20:41 +0800318void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700319 struct f2fs_dentry_ptr *d)
320{
Chao Yu291bf802016-03-09 22:07:28 +0800321 struct qstr dot = QSTR_INIT(".", 1);
322 struct qstr dotdot = QSTR_INIT("..", 2);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700323
Chao Yu291bf802016-03-09 22:07:28 +0800324 /* update dirent of "." */
325 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700326
Chao Yu291bf802016-03-09 22:07:28 +0800327 /* update dirent of ".." */
328 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700329}
330
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900331static int make_empty_dir(struct inode *inode,
332 struct inode *parent, struct page *page)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900333{
334 struct page *dentry_page;
335 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim062a3e72014-10-18 23:06:41 -0700336 struct f2fs_dentry_ptr d;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900337
Chao Yu622f28a2014-09-24 18:19:10 +0800338 if (f2fs_has_inline_dentry(inode))
Chao Yu4d57b862018-05-30 00:20:41 +0800339 return f2fs_make_empty_inline_dir(inode, parent, page);
Chao Yu622f28a2014-09-24 18:19:10 +0800340
Chao Yu4d57b862018-05-30 00:20:41 +0800341 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900342 if (IS_ERR(dentry_page))
343 return PTR_ERR(dentry_page);
344
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800345 dentry_blk = page_address(dentry_page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900346
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300347 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu4d57b862018-05-30 00:20:41 +0800348 f2fs_do_make_empty_dir(inode, parent, &d);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900349
Jaegeuk Kim39936832012-11-22 16:21:29 +0900350 set_page_dirty(dentry_page);
351 f2fs_put_page(dentry_page, 1);
352 return 0;
353}
354
Chao Yu4d57b862018-05-30 00:20:41 +0800355struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Chao Yu9421d572016-08-28 18:57:55 +0800356 const struct qstr *new_name, const struct qstr *orig_name,
357 struct page *dpage)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900358{
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900359 struct page *page;
Sheng Yongff62af22018-03-15 18:51:42 +0800360 int dummy_encrypt = DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir));
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900361 int err;
362
Jaegeuk Kim91942322016-05-20 10:13:22 -0700363 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800364 page = f2fs_new_inode_page(inode);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900365 if (IS_ERR(page))
366 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900367
368 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700369 /* in order to handle error case */
370 get_page(page);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900371 err = make_empty_dir(inode, dir, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700372 if (err) {
373 lock_page(page);
374 goto put_error;
375 }
376 put_page(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900377 }
378
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700379 err = f2fs_init_acl(inode, dir, page, dpage);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900380 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900381 goto put_error;
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900382
Chao Yu9421d572016-08-28 18:57:55 +0800383 err = f2fs_init_security(inode, dir, orig_name, page);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900384 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900385 goto put_error;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700386
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530387 if ((IS_ENCRYPTED(dir) || dummy_encrypt) &&
Sheng Yongff62af22018-03-15 18:51:42 +0800388 f2fs_may_encrypt(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700389 err = fscrypt_inherit_context(dir, inode, page, false);
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -0700390 if (err)
391 goto put_error;
392 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900393 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800394 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900395 if (IS_ERR(page))
396 return page;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900397 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900398
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800399 if (new_name) {
Chao Yu9421d572016-08-28 18:57:55 +0800400 init_dent_inode(new_name, page);
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530401 if (IS_ENCRYPTED(dir))
Kinglong Meed03ba4c2017-03-10 16:28:46 +0800402 file_set_enc_name(inode);
403 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900404
Jaegeuk Kim83d5d6f2013-05-28 12:25:47 +0900405 /*
406 * This file should be checkpointed during fsync.
407 * We lost i_pino from now on.
408 */
Jaegeuk Kim91942322016-05-20 10:13:22 -0700409 if (is_inode_flag_set(inode, FI_INC_LINK)) {
Sheng Yongd58dfb72017-06-26 10:41:35 +0800410 if (!S_ISDIR(inode->i_mode))
411 file_lost_pino(inode);
Chao Yu50732df2014-06-19 16:23:19 +0800412 /*
413 * If link the tmpfile to alias through linkat path,
414 * we should remove this inode from orphan list.
415 */
416 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800417 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
Jaegeuk Kima1961242016-05-20 09:43:20 -0700418 f2fs_i_links_write(inode, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900419 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900420 return page;
421
Jaegeuk Kima8865372013-12-27 17:04:17 +0900422put_error:
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700423 clear_nlink(inode);
Chao Yu4d57b862018-05-30 00:20:41 +0800424 f2fs_update_inode(inode, page);
Jaegeuk Kim221149c2016-05-02 12:34:48 -0700425 f2fs_put_page(page, 1);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900426 return ERR_PTR(err);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900427}
428
Chao Yu4d57b862018-05-30 00:20:41 +0800429void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900430 unsigned int current_depth)
431{
Jaegeuk Kim91942322016-05-20 10:13:22 -0700432 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700433 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700434 f2fs_i_links_write(dir, true);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700435 clear_inode_flag(inode, FI_NEW_INODE);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900436 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700437 dir->i_mtime = dir->i_ctime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700438 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900439
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700440 if (F2FS_I(dir)->i_current_depth != current_depth)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700441 f2fs_i_depth_write(dir, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900442
Jaegeuk Kim91942322016-05-20 10:13:22 -0700443 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
444 clear_inode_flag(inode, FI_INC_LINK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900445}
446
Chao Yu4d57b862018-05-30 00:20:41 +0800447int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900448{
449 int bit_start = 0;
450 int zero_start, zero_end;
451next:
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700452 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
453 if (zero_start >= max_slots)
454 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900455
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700456 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900457 if (zero_end - zero_start >= slots)
458 return zero_start;
459
460 bit_start = zero_end + 1;
461
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700462 if (zero_end + 1 >= max_slots)
463 return max_slots;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900464 goto next;
465}
466
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700467void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
Chao Yu3b4d7322015-02-16 16:17:20 +0800468 const struct qstr *name, f2fs_hash_t name_hash,
469 unsigned int bit_pos)
470{
471 struct f2fs_dir_entry *de;
472 int slots = GET_DENTRY_SLOTS(name->len);
473 int i;
474
475 de = &d->dentry[bit_pos];
476 de->hash_code = name_hash;
477 de->name_len = cpu_to_le16(name->len);
478 memcpy(d->filename[bit_pos], name->name, name->len);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700479 de->ino = cpu_to_le32(ino);
480 set_de_type(de, mode);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800481 for (i = 0; i < slots; i++) {
Jaegeuk Kim6bf6b262016-08-31 16:20:37 -0700482 __set_bit_le(bit_pos + i, (void *)d->bitmap);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800483 /* avoid wrong garbage data for readdir */
484 if (i)
485 (de + i)->name_len = 0;
486 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800487}
488
Chao Yu675f10b2016-02-22 18:29:18 +0800489int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800490 const struct qstr *orig_name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700491 struct inode *inode, nid_t ino, umode_t mode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900492{
493 unsigned int bit_pos;
494 unsigned int level;
495 unsigned int current_depth;
496 unsigned long bidx, block;
497 f2fs_hash_t dentry_hash;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900498 unsigned int nbucket, nblock;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900499 struct page *dentry_page = NULL;
500 struct f2fs_dentry_block *dentry_blk = NULL;
Chao Yu3b4d7322015-02-16 16:17:20 +0800501 struct f2fs_dentry_ptr d;
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700502 struct page *page = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800503 int slots, err = 0;
Chao Yu622f28a2014-09-24 18:19:10 +0800504
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900505 level = 0;
Chao Yu675f10b2016-02-22 18:29:18 +0800506 slots = GET_DENTRY_SLOTS(new_name->len);
Jaegeuk Kim6332cd32017-04-24 10:00:08 -0700507 dentry_hash = f2fs_dentry_hash(new_name, NULL);
Jaegeuk Kim9ea97162015-04-27 14:51:02 -0700508
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900509 current_depth = F2FS_I(dir)->i_current_depth;
510 if (F2FS_I(dir)->chash == dentry_hash) {
511 level = F2FS_I(dir)->clevel;
512 F2FS_I(dir)->chash = 0;
513 }
514
515start:
Chao Yu55523512017-02-25 11:08:28 +0800516 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
517 f2fs_show_injection_info(FAULT_DIR_DEPTH);
Jaegeuk Kimcb789422016-04-29 16:29:22 -0700518 return -ENOSPC;
Chao Yu55523512017-02-25 11:08:28 +0800519 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +0200520
Chao Yu675f10b2016-02-22 18:29:18 +0800521 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
522 return -ENOSPC;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900523
524 /* Increase the depth, if required */
525 if (level == current_depth)
526 ++current_depth;
527
Jaegeuk Kim38431542014-02-27 18:20:00 +0900528 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900529 nblock = bucket_blocks(level);
530
Jaegeuk Kim38431542014-02-27 18:20:00 +0900531 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
532 (le32_to_cpu(dentry_hash) % nbucket));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900533
534 for (block = bidx; block <= (bidx + nblock - 1); block++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800535 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
Chao Yu675f10b2016-02-22 18:29:18 +0800536 if (IS_ERR(dentry_page))
537 return PTR_ERR(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900538
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800539 dentry_blk = page_address(dentry_page);
Chao Yu4d57b862018-05-30 00:20:41 +0800540 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
Jaegeuk Kima82afa22014-10-13 16:28:13 -0700541 slots, NR_DENTRY_IN_BLOCK);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900542 if (bit_pos < NR_DENTRY_IN_BLOCK)
543 goto add_dentry;
544
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900545 f2fs_put_page(dentry_page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900546 }
547
548 /* Move to next level to find the empty slot for new dentry */
549 ++level;
550 goto start;
551add_dentry:
Chao Yubae0ee72018-12-25 17:43:42 +0800552 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900553
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700554 if (inode) {
555 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800556 page = f2fs_init_inode_metadata(inode, dir, new_name,
Chao Yu9421d572016-08-28 18:57:55 +0800557 orig_name, NULL);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700558 if (IS_ERR(page)) {
559 err = PTR_ERR(page);
560 goto fail;
561 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900562 }
Chao Yu3b4d7322015-02-16 16:17:20 +0800563
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300564 make_dentry_ptr_block(NULL, &d, dentry_blk);
Chao Yu675f10b2016-02-22 18:29:18 +0800565 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
Chao Yu3b4d7322015-02-16 16:17:20 +0800566
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900567 set_page_dirty(dentry_page);
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900568
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700569 if (inode) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700570 f2fs_i_pino_write(inode, dir->i_ino);
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700571 f2fs_put_page(page, 1);
572 }
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900573
Chao Yu4d57b862018-05-30 00:20:41 +0800574 f2fs_update_parent_metadata(dir, inode, current_depth);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900575fail:
Jaegeuk Kim510022a2015-03-30 15:07:16 -0700576 if (inode)
577 up_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900578
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900579 f2fs_put_page(dentry_page, 1);
Chao Yu675f10b2016-02-22 18:29:18 +0800580
581 return err;
582}
583
Chao Yu4d57b862018-05-30 00:20:41 +0800584int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
Shuoran Liue7ba1082016-08-29 11:27:56 +0800585 struct inode *inode, nid_t ino, umode_t mode)
586{
587 struct qstr new_name;
588 int err = -EAGAIN;
589
590 new_name.name = fname_name(fname);
591 new_name.len = fname_len(fname);
592
593 if (f2fs_has_inline_dentry(dir))
594 err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
595 inode, ino, mode);
596 if (err == -EAGAIN)
597 err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
598 inode, ino, mode);
599
600 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
601 return err;
602}
603
Chao Yu675f10b2016-02-22 18:29:18 +0800604/*
605 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
606 * f2fs_unlock_op().
607 */
Chao Yu4d57b862018-05-30 00:20:41 +0800608int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Chao Yu675f10b2016-02-22 18:29:18 +0800609 struct inode *inode, nid_t ino, umode_t mode)
610{
611 struct fscrypt_name fname;
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800612 struct page *page = NULL;
613 struct f2fs_dir_entry *de = NULL;
Chao Yu675f10b2016-02-22 18:29:18 +0800614 int err;
615
616 err = fscrypt_setup_filename(dir, name, 0, &fname);
617 if (err)
618 return err;
619
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800620 /*
621 * An immature stakable filesystem shows a race condition between lookup
622 * and create. If we have same task when doing lookup and create, it's
623 * definitely fine as expected by VFS normally. Otherwise, let's just
624 * verify on-disk dentry one more time, which guarantees filesystem
625 * consistency more.
626 */
627 if (current != F2FS_I(dir)->task) {
628 de = __f2fs_find_entry(dir, &fname, &page);
629 F2FS_I(dir)->task = NULL;
630 }
631 if (de) {
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800632 f2fs_put_page(page, 0);
633 err = -EEXIST;
634 } else if (IS_ERR(page)) {
635 err = PTR_ERR(page);
636 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800637 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
Jaegeuk Kim88c5c132017-02-14 09:54:37 -0800638 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700639 fscrypt_free_filename(&fname);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900640 return err;
641}
642
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700643int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
644{
645 struct page *page;
646 int err = 0;
647
648 down_write(&F2FS_I(inode)->i_sem);
Chao Yu4d57b862018-05-30 00:20:41 +0800649 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700650 if (IS_ERR(page)) {
651 err = PTR_ERR(page);
652 goto fail;
653 }
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700654 f2fs_put_page(page, 1);
655
Jaegeuk Kim91942322016-05-20 10:13:22 -0700656 clear_inode_flag(inode, FI_NEW_INODE);
Sahitya Tummalac75f2fe2018-10-05 10:47:39 +0530657 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kimb97a9b52014-06-20 21:37:02 -0700658fail:
659 up_write(&F2FS_I(inode)->i_sem);
660 return err;
661}
662
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700663void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
Chao Yudbeacf02014-09-24 18:17:04 +0800664{
665 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
666
667 down_write(&F2FS_I(inode)->i_sem);
668
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700669 if (S_ISDIR(inode->i_mode))
Jaegeuk Kima1961242016-05-20 09:43:20 -0700670 f2fs_i_links_write(dir, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700671 inode->i_ctime = current_time(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800672
Jaegeuk Kima1961242016-05-20 09:43:20 -0700673 f2fs_i_links_write(inode, false);
Chao Yudbeacf02014-09-24 18:17:04 +0800674 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kima1961242016-05-20 09:43:20 -0700675 f2fs_i_links_write(inode, false);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700676 f2fs_i_size_write(inode, 0);
Chao Yudbeacf02014-09-24 18:17:04 +0800677 }
678 up_write(&F2FS_I(inode)->i_sem);
Chao Yudbeacf02014-09-24 18:17:04 +0800679
680 if (inode->i_nlink == 0)
Chao Yu4d57b862018-05-30 00:20:41 +0800681 f2fs_add_orphan_inode(inode);
Chao Yudbeacf02014-09-24 18:17:04 +0800682 else
Chao Yu4d57b862018-05-30 00:20:41 +0800683 f2fs_release_orphan_inode(sbi);
Chao Yudbeacf02014-09-24 18:17:04 +0800684}
685
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900686/*
arter97e1c42042014-08-06 23:22:50 +0900687 * It only removes the dentry from the dentry page, corresponding name
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900688 * entry in name page does not need to be touched during deletion.
689 */
690void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
Chao Yudbeacf02014-09-24 18:17:04 +0800691 struct inode *dir, struct inode *inode)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900692{
693 struct f2fs_dentry_block *dentry_blk;
694 unsigned int bit_pos;
Namjae Jeon457d08e2012-12-08 14:54:50 +0900695 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900696 int i;
697
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800698 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
699
Chao Yu63189b72018-03-08 14:22:56 +0800700 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
Chao Yu4d57b862018-05-30 00:20:41 +0800701 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
Jaegeuk Kim0a007b972017-12-28 08:09:44 -0800702
Chao Yu622f28a2014-09-24 18:19:10 +0800703 if (f2fs_has_inline_dentry(dir))
704 return f2fs_delete_inline_entry(dentry, page, dir, inode);
705
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900706 lock_page(page);
Chao Yubae0ee72018-12-25 17:43:42 +0800707 f2fs_wait_on_page_writeback(page, DATA, true, true);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900708
Gu Zheng1c3bb972014-06-27 17:57:04 +0800709 dentry_blk = page_address(page);
710 bit_pos = dentry - dentry_blk->dentry;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900711 for (i = 0; i < slots; i++)
Jaegeuk Kim23380b82017-03-07 14:11:06 -0800712 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900713
714 /* Let's check and deallocate this dentry page */
715 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
716 NR_DENTRY_IN_BLOCK,
717 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900718 set_page_dirty(page);
719
Deepa Dinamani078cd822016-09-14 07:48:04 -0700720 dir->i_ctime = dir->i_mtime = current_time(dir);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700721 f2fs_mark_inode_dirty_sync(dir, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900722
Chao Yudbeacf02014-09-24 18:17:04 +0800723 if (inode)
Jaegeuk Kim9f7c45c2016-06-01 21:18:25 -0700724 f2fs_drop_nlink(dir, inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900725
Chao Yu206e61b2015-08-12 17:48:21 +0800726 if (bit_pos == NR_DENTRY_IN_BLOCK &&
Chao Yu4d57b862018-05-30 00:20:41 +0800727 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500728 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900729 clear_page_dirty_for_io(page);
Chao Yu240a5912019-03-06 17:30:59 +0800730 f2fs_clear_page_private(page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900731 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +0800732 clear_cold_data(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -0700733 inode_dec_dirty_pages(dir);
Chao Yu4d57b862018-05-30 00:20:41 +0800734 f2fs_remove_dirty_inode(dir);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900735 }
Namjae Jeon508198b2012-12-08 14:54:35 +0900736 f2fs_put_page(page, 1);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900737}
738
739bool f2fs_empty_dir(struct inode *dir)
740{
741 unsigned long bidx;
742 struct page *dentry_page;
743 unsigned int bit_pos;
Chao Yudbeacf02014-09-24 18:17:04 +0800744 struct f2fs_dentry_block *dentry_blk;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900745 unsigned long nblock = dir_blocks(dir);
746
Chao Yu622f28a2014-09-24 18:19:10 +0800747 if (f2fs_has_inline_dentry(dir))
748 return f2fs_empty_inline_dir(dir);
749
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900750 for (bidx = 0; bidx < nblock; bidx++) {
Chao Yu4d57b862018-05-30 00:20:41 +0800751 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900752 if (IS_ERR(dentry_page)) {
753 if (PTR_ERR(dentry_page) == -ENOENT)
754 continue;
755 else
756 return false;
757 }
758
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800759 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900760 if (bidx == 0)
761 bit_pos = 2;
762 else
763 bit_pos = 0;
764 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
765 NR_DENTRY_IN_BLOCK,
766 bit_pos);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900767
768 f2fs_put_page(dentry_page, 1);
769
770 if (bit_pos < NR_DENTRY_IN_BLOCK)
771 return false;
772 }
773 return true;
774}
775
Chao Yued6bd4b2016-10-29 18:46:34 +0800776int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700777 unsigned int start_pos, struct fscrypt_str *fstr)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700778{
779 unsigned char d_type = DT_UNKNOWN;
780 unsigned int bit_pos;
781 struct f2fs_dir_entry *de = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700782 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
Sheng Yongf6df8f22017-11-22 18:23:38 +0800783 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
Chao Yue1293bd2018-09-07 19:49:07 +0800784 struct blk_plug plug;
785 bool readdir_ra = sbi->readdir_ra == 1;
786 int err = 0;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700787
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700788 bit_pos = ((unsigned long)ctx->pos % d->max);
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700789
Chao Yue1293bd2018-09-07 19:49:07 +0800790 if (readdir_ra)
791 blk_start_plug(&plug);
792
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700793 while (bit_pos < d->max) {
794 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
795 if (bit_pos >= d->max)
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700796 break;
797
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700798 de = &d->dentry[bit_pos];
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800799 if (de->name_len == 0) {
800 bit_pos++;
801 ctx->pos = start_pos + bit_pos;
Chao Yuddf06b72019-01-08 10:21:24 +0800802 printk_ratelimited(
803 "%s, invalid namelen(0), ino:%u, run fsck to fix.",
804 KERN_WARNING, le32_to_cpu(de->ino));
805 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim7d9dfa12016-02-12 14:29:28 -0800806 continue;
807 }
808
Chao Yu4d57b862018-05-30 00:20:41 +0800809 d_type = f2fs_get_de_type(de);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700810
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700811 de_name.name = d->filename[bit_pos];
812 de_name.len = le16_to_cpu(de->name_len);
813
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800814 /* check memory boundary before moving forward */
815 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
Sheng Yong720db062019-01-07 15:02:34 +0800816 if (unlikely(bit_pos > d->max ||
817 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800818 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
819 __func__, le16_to_cpu(de->name_len));
Jaegeuk Kim4e240d12018-11-14 12:40:30 -0800820 set_sbi_flag(sbi, SBI_NEED_FSCK);
821 err = -EINVAL;
822 goto out;
823 }
824
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530825 if (IS_ENCRYPTED(d->inode)) {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700826 int save_len = fstr->len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700827
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400828 err = fscrypt_fname_disk_to_usr(d->inode,
Chao Yu36af5f42019-05-28 17:23:33 +0800829 (u32)le32_to_cpu(de->hash_code),
830 0, &de_name, fstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400831 if (err)
Chao Yue1293bd2018-09-07 19:49:07 +0800832 goto out;
Jaegeuk Kim569cf182015-09-03 13:38:23 -0700833
834 de_name = *fstr;
835 fstr->len = save_len;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700836 }
837
838 if (!dir_emit(ctx, de_name.name, de_name.len,
Chao Yue1293bd2018-09-07 19:49:07 +0800839 le32_to_cpu(de->ino), d_type)) {
840 err = 1;
841 goto out;
842 }
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700843
Chao Yue1293bd2018-09-07 19:49:07 +0800844 if (readdir_ra)
Chao Yu4d57b862018-05-30 00:20:41 +0800845 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
Sheng Yongf6df8f22017-11-22 18:23:38 +0800846
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700847 ctx->pos = start_pos + bit_pos;
848 }
Chao Yue1293bd2018-09-07 19:49:07 +0800849out:
850 if (readdir_ra)
851 blk_finish_plug(&plug);
852 return err;
Jaegeuk Kim38594de2014-10-15 21:29:51 -0700853}
854
Al Viro6f7f2312013-05-17 18:02:17 -0400855static int f2fs_readdir(struct file *file, struct dir_context *ctx)
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900856{
Al Viro496ad9a2013-01-23 17:07:38 -0500857 struct inode *inode = file_inode(file);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900858 unsigned long npages = dir_blocks(inode);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900859 struct f2fs_dentry_block *dentry_blk = NULL;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900860 struct page *dentry_page = NULL;
Chao Yu817202d92014-04-28 17:59:43 +0800861 struct file_ra_state *ra = &file->f_ra;
Chao Yue97a3c42017-10-13 18:01:33 +0800862 loff_t start_pos = ctx->pos;
Al Viro6f7f2312013-05-17 18:02:17 -0400863 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700864 struct f2fs_dentry_ptr d;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700865 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700866 int err = 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900867
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530868 if (IS_ENCRYPTED(inode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700869 err = fscrypt_get_encryption_info(inode);
Jaegeuk Kim8074bb52016-02-23 09:21:37 -0800870 if (err && err != -ENOKEY)
Chao Yue97a3c42017-10-13 18:01:33 +0800871 goto out;
Jaegeuk Kim26bf3dc2015-05-19 22:26:54 -0700872
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700873 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700874 if (err < 0)
Chao Yue97a3c42017-10-13 18:01:33 +0800875 goto out;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700876 }
877
878 if (f2fs_has_inline_dentry(inode)) {
879 err = f2fs_read_inline_dir(file, ctx, &fstr);
Chao Yue97a3c42017-10-13 18:01:33 +0800880 goto out_free;
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700881 }
Chao Yu622f28a2014-09-24 18:19:10 +0800882
Chao Yuab383be2017-10-13 18:01:36 +0800883 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
Chao Yu4414dea2017-10-13 18:01:34 +0800884
885 /* allow readdir() to be interrupted */
886 if (fatal_signal_pending(current)) {
887 err = -ERESTARTSYS;
888 goto out_free;
889 }
890 cond_resched();
891
Chao Yucb7a8442017-10-13 18:01:35 +0800892 /* readahead for multi pages of dir */
893 if (npages - n > 1 && !ra_has_index(ra, n))
894 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
895 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
896
Gao Xiang613f3dc2019-02-21 12:57:35 +0800897 dentry_page = f2fs_find_data_page(inode, n);
Chao Yu57b62d22015-11-19 16:09:07 +0800898 if (IS_ERR(dentry_page)) {
899 err = PTR_ERR(dentry_page);
Chao Yued6bd4b2016-10-29 18:46:34 +0800900 if (err == -ENOENT) {
901 err = 0;
Chao Yu57b62d22015-11-19 16:09:07 +0800902 continue;
Chao Yued6bd4b2016-10-29 18:46:34 +0800903 } else {
Chao Yue97a3c42017-10-13 18:01:33 +0800904 goto out_free;
Chao Yued6bd4b2016-10-29 18:46:34 +0800905 }
Chao Yu57b62d22015-11-19 16:09:07 +0800906 }
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900907
Yunlong Songbdbc90f2018-02-28 20:31:52 +0800908 dentry_blk = page_address(dentry_page);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900909
Tomohiro Kusumi64c24ec2017-04-04 13:01:22 +0300910 make_dentry_ptr_block(inode, &d, dentry_blk);
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700911
Chao Yued6bd4b2016-10-29 18:46:34 +0800912 err = f2fs_fill_dentries(ctx, &d,
913 n * NR_DENTRY_IN_BLOCK, &fstr);
914 if (err) {
Gao Xiang613f3dc2019-02-21 12:57:35 +0800915 f2fs_put_page(dentry_page, 0);
Chao Yue9837bc2015-12-01 11:41:50 +0800916 break;
917 }
Jaegeuk Kim99b072bb2013-07-05 17:28:12 +0900918
Gao Xiang613f3dc2019-02-21 12:57:35 +0800919 f2fs_put_page(dentry_page, 0);
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900920 }
Chao Yue97a3c42017-10-13 18:01:33 +0800921out_free:
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700922 fscrypt_fname_free_buffer(&fstr);
Chao Yue97a3c42017-10-13 18:01:33 +0800923out:
924 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
Chao Yued6bd4b2016-10-29 18:46:34 +0800925 return err < 0 ? err : 0;
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900926}
927
Chao Yued3360a2016-02-14 18:56:55 +0800928static int f2fs_dir_open(struct inode *inode, struct file *filp)
929{
Chandan Rajendra62230e0d2018-12-12 15:20:11 +0530930 if (IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700931 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
Chao Yued3360a2016-02-14 18:56:55 +0800932 return 0;
933}
934
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900935const struct file_operations f2fs_dir_operations = {
936 .llseek = generic_file_llseek,
937 .read = generic_read_dir,
Al Viroe77d0c62016-05-10 16:41:13 -0400938 .iterate_shared = f2fs_readdir,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900939 .fsync = f2fs_sync_file,
Chao Yued3360a2016-02-14 18:56:55 +0800940 .open = f2fs_dir_open,
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900941 .unlocked_ioctl = f2fs_ioctl,
hujianyang08b95122015-05-12 16:05:57 +0800942#ifdef CONFIG_COMPAT
943 .compat_ioctl = f2fs_compat_ioctl,
944#endif
Jaegeuk Kim6b4ea012012-11-14 16:59:04 +0900945};