blob: 84572e11cc05f34ba3bfdf0190654c5d7a8961e0 [file] [log] [blame]
Namjae Jeonca061972020-03-02 15:21:35 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6#include <linux/slab.h>
Hyeongseok Kim654762d2021-03-04 09:20:35 +09007#include <linux/compat.h>
Namjae Jeonca061972020-03-02 15:21:35 +09008#include <linux/bio.h>
9#include <linux/buffer_head.h>
10
11#include "exfat_raw.h"
12#include "exfat_fs.h"
13
14static int exfat_extract_uni_name(struct exfat_dentry *ep,
15 unsigned short *uniname)
16{
17 int i, len = 0;
18
19 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
20 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
21 if (*uniname == 0x0)
22 return len;
23 uniname++;
24 len++;
25 }
26
27 *uniname = 0x0;
28 return len;
29
30}
31
Namjae Jeon8258ef22023-01-14 13:09:48 +090032static int exfat_get_uniname_from_ext_entry(struct super_block *sb,
Namjae Jeonca061972020-03-02 15:21:35 +090033 struct exfat_chain *p_dir, int entry, unsigned short *uniname)
34{
Namjae Jeon8258ef22023-01-14 13:09:48 +090035 int i, err;
Yuezhang Mo20914ff2022-11-17 11:37:13 +080036 struct exfat_entry_set_cache es;
Namjae Jeond4233452023-07-13 21:59:37 +090037 unsigned int uni_len = 0, len;
Namjae Jeonca061972020-03-02 15:21:35 +090038
Namjae Jeon8258ef22023-01-14 13:09:48 +090039 err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES);
40 if (err)
41 return err;
Namjae Jeonca061972020-03-02 15:21:35 +090042
Namjae Jeonca061972020-03-02 15:21:35 +090043 /*
44 * First entry : file entry
45 * Second entry : stream-extension entry
46 * Third entry : first file-name entry
47 * So, the index of first file-name dentry should start from 2.
48 */
Yuezhang Mof3fe3952022-03-17 19:39:20 +080049 for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
Yuezhang Mo20914ff2022-11-17 11:37:13 +080050 struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +090051
Namjae Jeonca061972020-03-02 15:21:35 +090052 /* end of name entry */
53 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +090054 break;
Namjae Jeonca061972020-03-02 15:21:35 +090055
Namjae Jeond4233452023-07-13 21:59:37 +090056 len = exfat_extract_uni_name(ep, uniname);
57 uni_len += len;
58 if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
59 break;
Namjae Jeonca061972020-03-02 15:21:35 +090060 uniname += EXFAT_FILE_NAME_LEN;
61 }
62
Yuezhang Mo3b9681a2022-03-17 18:12:40 +080063 exfat_put_dentry_set(&es, false);
Namjae Jeon8258ef22023-01-14 13:09:48 +090064 return 0;
Namjae Jeonca061972020-03-02 15:21:35 +090065}
66
67/* read a directory entry from the opened directory */
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +090068static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
Namjae Jeonca061972020-03-02 15:21:35 +090069{
Namjae Jeon8258ef22023-01-14 13:09:48 +090070 int i, dentries_per_clu, num_ext, err;
Namjae Jeon1e5654d2021-06-11 09:40:24 +090071 unsigned int type, clu_offset, max_dentries;
Namjae Jeonca061972020-03-02 15:21:35 +090072 struct exfat_chain dir, clu;
73 struct exfat_uni_name uni_name;
74 struct exfat_dentry *ep;
75 struct super_block *sb = inode->i_sb;
76 struct exfat_sb_info *sbi = EXFAT_SB(sb);
77 struct exfat_inode_info *ei = EXFAT_I(inode);
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +090078 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
Namjae Jeonca061972020-03-02 15:21:35 +090079 struct buffer_head *bh;
80
81 /* check if the given file ID is opened */
82 if (ei->type != TYPE_DIR)
83 return -EPERM;
84
85 if (ei->entry == -1)
86 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
87 else
88 exfat_chain_set(&dir, ei->start_clu,
89 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
90
91 dentries_per_clu = sbi->dentries_per_clu;
Namjae Jeon1e5654d2021-06-11 09:40:24 +090092 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
Yuezhang Mo088f1342022-08-16 16:55:06 +080093 (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
Namjae Jeonca061972020-03-02 15:21:35 +090094
Yuezhang Mo088f1342022-08-16 16:55:06 +080095 clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
Namjae Jeonca061972020-03-02 15:21:35 +090096 exfat_chain_dup(&clu, &dir);
97
98 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
99 clu.dir += clu_offset;
100 clu.size -= clu_offset;
101 } else {
102 /* hint_information */
103 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
104 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
105 clu_offset -= ei->hint_bmap.off;
106 clu.dir = ei->hint_bmap.clu;
107 }
108
Yuezhang Mo706fdca2022-10-20 14:27:37 +0800109 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
Namjae Jeonca061972020-03-02 15:21:35 +0900110 if (exfat_get_next_cluster(sb, &(clu.dir)))
111 return -EIO;
112
113 clu_offset--;
114 }
115 }
116
Namjae Jeon1e5654d2021-06-11 09:40:24 +0900117 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
Namjae Jeonca061972020-03-02 15:21:35 +0900118 i = dentry & (dentries_per_clu - 1);
119
120 for ( ; i < dentries_per_clu; i++, dentry++) {
Yuezhang.Moc71510b2021-12-16 21:58:37 +0900121 ep = exfat_get_dentry(sb, &clu, i, &bh);
Namjae Jeonca061972020-03-02 15:21:35 +0900122 if (!ep)
123 return -EIO;
124
125 type = exfat_get_entry_type(ep);
126 if (type == TYPE_UNUSED) {
127 brelse(bh);
128 break;
129 }
130
131 if (type != TYPE_FILE && type != TYPE_DIR) {
132 brelse(bh);
133 continue;
134 }
135
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900136 num_ext = ep->dentry.file.num_ext;
Namjae Jeonca061972020-03-02 15:21:35 +0900137 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
138 exfat_get_entry_time(sbi, &dir_entry->crtime,
139 ep->dentry.file.create_tz,
140 ep->dentry.file.create_time,
141 ep->dentry.file.create_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900142 ep->dentry.file.create_time_cs);
Namjae Jeonca061972020-03-02 15:21:35 +0900143 exfat_get_entry_time(sbi, &dir_entry->mtime,
144 ep->dentry.file.modify_tz,
145 ep->dentry.file.modify_time,
146 ep->dentry.file.modify_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900147 ep->dentry.file.modify_time_cs);
Namjae Jeonca061972020-03-02 15:21:35 +0900148 exfat_get_entry_time(sbi, &dir_entry->atime,
149 ep->dentry.file.access_tz,
150 ep->dentry.file.access_time,
151 ep->dentry.file.access_date,
152 0);
153
154 *uni_name.name = 0x0;
Namjae Jeon8258ef22023-01-14 13:09:48 +0900155 err = exfat_get_uniname_from_ext_entry(sb, &clu, i,
Namjae Jeonca061972020-03-02 15:21:35 +0900156 uni_name.name);
Namjae Jeon8258ef22023-01-14 13:09:48 +0900157 if (err) {
158 brelse(bh);
159 continue;
160 }
Namjae Jeonca061972020-03-02 15:21:35 +0900161 exfat_utf16_to_nls(sb, &uni_name,
162 dir_entry->namebuf.lfn,
163 dir_entry->namebuf.lfnbuf_len);
164 brelse(bh);
165
Yuezhang.Moc71510b2021-12-16 21:58:37 +0900166 ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
Namjae Jeonca061972020-03-02 15:21:35 +0900167 if (!ep)
168 return -EIO;
169 dir_entry->size =
170 le64_to_cpu(ep->dentry.stream.valid_size);
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900171 dir_entry->entry = dentry;
Namjae Jeonca061972020-03-02 15:21:35 +0900172 brelse(bh);
173
Yuezhang Mo088f1342022-08-16 16:55:06 +0800174 ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
Namjae Jeonca061972020-03-02 15:21:35 +0900175 ei->hint_bmap.clu = clu.dir;
176
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900177 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
Namjae Jeonca061972020-03-02 15:21:35 +0900178 return 0;
179 }
180
181 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
182 if (--clu.size > 0)
183 clu.dir++;
184 else
185 clu.dir = EXFAT_EOF_CLUSTER;
186 } else {
187 if (exfat_get_next_cluster(sb, &(clu.dir)))
188 return -EIO;
189 }
190 }
191
192 dir_entry->namebuf.lfn[0] = '\0';
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900193 *cpos = EXFAT_DEN_TO_B(dentry);
Namjae Jeonca061972020-03-02 15:21:35 +0900194 return 0;
195}
196
197static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
198{
199 nb->lfn = NULL;
200 nb->lfnbuf_len = 0;
201}
202
203static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
204{
205 nb->lfn = __getname();
206 if (!nb->lfn)
207 return -ENOMEM;
208 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
209 return 0;
210}
211
212static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
213{
214 if (!nb->lfn)
215 return;
216
217 __putname(nb->lfn);
218 exfat_init_namebuf(nb);
219}
220
Sungjong Seoff847722023-07-14 17:43:54 +0900221/*
222 * Before calling dir_emit*(), sbi->s_lock should be released
223 * because page fault can occur in dir_emit*().
224 */
Namjae Jeonca061972020-03-02 15:21:35 +0900225#define ITER_POS_FILLED_DOTS (2)
Al Viro703e3e92022-08-03 21:27:11 -0400226static int exfat_iterate(struct file *file, struct dir_context *ctx)
Namjae Jeonca061972020-03-02 15:21:35 +0900227{
Al Viro703e3e92022-08-03 21:27:11 -0400228 struct inode *inode = file_inode(file);
Namjae Jeonca061972020-03-02 15:21:35 +0900229 struct super_block *sb = inode->i_sb;
230 struct inode *tmp;
231 struct exfat_dir_entry de;
232 struct exfat_dentry_namebuf *nb = &(de.namebuf);
233 struct exfat_inode_info *ei = EXFAT_I(inode);
234 unsigned long inum;
235 loff_t cpos, i_pos;
236 int err = 0, fake_offset = 0;
237
238 exfat_init_namebuf(nb);
Namjae Jeonca061972020-03-02 15:21:35 +0900239
240 cpos = ctx->pos;
Al Viro703e3e92022-08-03 21:27:11 -0400241 if (!dir_emit_dots(file, ctx))
Sungjong Seoff847722023-07-14 17:43:54 +0900242 goto out;
Namjae Jeonca061972020-03-02 15:21:35 +0900243
244 if (ctx->pos == ITER_POS_FILLED_DOTS) {
245 cpos = 0;
246 fake_offset = 1;
247 }
248
Yuezhang Mo6cb5d1a2022-09-22 14:43:47 +0800249 cpos = round_up(cpos, DENTRY_SIZE);
Namjae Jeonca061972020-03-02 15:21:35 +0900250
251 /* name buffer should be allocated before use */
252 err = exfat_alloc_namebuf(nb);
253 if (err)
Sungjong Seoff847722023-07-14 17:43:54 +0900254 goto out;
Namjae Jeonca061972020-03-02 15:21:35 +0900255get_new:
Sungjong Seoff847722023-07-14 17:43:54 +0900256 mutex_lock(&EXFAT_SB(sb)->s_lock);
257
Namjae Jeon1e5654d2021-06-11 09:40:24 +0900258 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
Namjae Jeonca061972020-03-02 15:21:35 +0900259 goto end_of_dir;
260
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900261 err = exfat_readdir(inode, &cpos, &de);
Namjae Jeonca061972020-03-02 15:21:35 +0900262 if (err) {
263 /*
Sungjong Seoff847722023-07-14 17:43:54 +0900264 * At least we tried to read a sector.
265 * Move cpos to next sector position (should be aligned).
Namjae Jeonca061972020-03-02 15:21:35 +0900266 */
267 if (err == -EIO) {
268 cpos += 1 << (sb->s_blocksize_bits);
269 cpos &= ~(sb->s_blocksize - 1);
270 }
271
272 err = -EIO;
273 goto end_of_dir;
274 }
275
Namjae Jeonca061972020-03-02 15:21:35 +0900276 if (!nb->lfn[0])
277 goto end_of_dir;
278
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900279 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
Namjae Jeonca061972020-03-02 15:21:35 +0900280 tmp = exfat_iget(sb, i_pos);
281 if (tmp) {
282 inum = tmp->i_ino;
283 iput(tmp);
284 } else {
285 inum = iunique(sb, EXFAT_ROOT_INO);
286 }
287
Namjae Jeonca061972020-03-02 15:21:35 +0900288 mutex_unlock(&EXFAT_SB(sb)->s_lock);
289 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
Jan Cincera0ab8ba72023-10-30 20:53:18 +0900290 (de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG))
Sungjong Seoff847722023-07-14 17:43:54 +0900291 goto out;
Namjae Jeonca061972020-03-02 15:21:35 +0900292 ctx->pos = cpos;
293 goto get_new;
294
295end_of_dir:
296 if (!cpos && fake_offset)
297 cpos = ITER_POS_FILLED_DOTS;
298 ctx->pos = cpos;
Namjae Jeonca061972020-03-02 15:21:35 +0900299 mutex_unlock(&EXFAT_SB(sb)->s_lock);
Sungjong Seoff847722023-07-14 17:43:54 +0900300out:
Namjae Jeonca061972020-03-02 15:21:35 +0900301 /*
302 * To improve performance, free namebuf after unlock sb_lock.
303 * If namebuf is not allocated, this function do nothing
304 */
305 exfat_free_namebuf(nb);
306 return err;
307}
308
Linus Torvalds3e327152023-08-05 12:25:01 -0700309WRAP_DIR_ITER(exfat_iterate) // FIXME!
Namjae Jeonca061972020-03-02 15:21:35 +0900310const struct file_operations exfat_dir_operations = {
311 .llseek = generic_file_llseek,
312 .read = generic_read_dir,
Linus Torvalds3e327152023-08-05 12:25:01 -0700313 .iterate_shared = shared_exfat_iterate,
Hyeongseok Kim654762d2021-03-04 09:20:35 +0900314 .unlocked_ioctl = exfat_ioctl,
315#ifdef CONFIG_COMPAT
316 .compat_ioctl = exfat_compat_ioctl,
317#endif
Sungjong Seo52674562020-06-18 20:43:26 +0900318 .fsync = exfat_file_fsync,
Namjae Jeonca061972020-03-02 15:21:35 +0900319};
320
321int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
322{
323 int ret;
324
325 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
326
Hyeongseok Kim23befe42021-03-15 13:12:55 +0900327 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
Namjae Jeonca061972020-03-02 15:21:35 +0900328 if (ret)
329 return ret;
330
331 return exfat_zeroed_cluster(inode, clu->dir);
332}
333
334int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
335{
336 int len;
337
338 len = p_uniname->name_len;
339 if (len == 0)
340 return -EINVAL;
341
342 /* 1 file entry + 1 stream entry + name entries */
Yuezhang Mof3fe3952022-03-17 19:39:20 +0800343 return ES_ENTRY_NUM(len);
Namjae Jeonca061972020-03-02 15:21:35 +0900344}
345
346unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
347{
348 if (ep->type == EXFAT_UNUSED)
349 return TYPE_UNUSED;
350 if (IS_EXFAT_DELETED(ep->type))
351 return TYPE_DELETED;
352 if (ep->type == EXFAT_INVAL)
353 return TYPE_INVALID;
354 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
355 if (ep->type == EXFAT_BITMAP)
356 return TYPE_BITMAP;
357 if (ep->type == EXFAT_UPCASE)
358 return TYPE_UPCASE;
359 if (ep->type == EXFAT_VOLUME)
360 return TYPE_VOLUME;
361 if (ep->type == EXFAT_FILE) {
Jan Cincera0ab8ba72023-10-30 20:53:18 +0900362 if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR)
Namjae Jeonca061972020-03-02 15:21:35 +0900363 return TYPE_DIR;
364 return TYPE_FILE;
365 }
366 return TYPE_CRITICAL_PRI;
367 }
368 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
369 if (ep->type == EXFAT_GUID)
370 return TYPE_GUID;
371 if (ep->type == EXFAT_PADDING)
372 return TYPE_PADDING;
373 if (ep->type == EXFAT_ACLTAB)
374 return TYPE_ACLTAB;
375 return TYPE_BENIGN_PRI;
376 }
377 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
378 if (ep->type == EXFAT_STREAM)
379 return TYPE_STREAM;
380 if (ep->type == EXFAT_NAME)
381 return TYPE_EXTEND;
382 if (ep->type == EXFAT_ACL)
383 return TYPE_ACL;
384 return TYPE_CRITICAL_SEC;
385 }
Namjae Jeon8258ef22023-01-14 13:09:48 +0900386
387 if (ep->type == EXFAT_VENDOR_EXT)
388 return TYPE_VENDOR_EXT;
389 if (ep->type == EXFAT_VENDOR_ALLOC)
390 return TYPE_VENDOR_ALLOC;
391
Namjae Jeonca061972020-03-02 15:21:35 +0900392 return TYPE_BENIGN_SEC;
393}
394
395static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
396{
397 if (type == TYPE_UNUSED) {
398 ep->type = EXFAT_UNUSED;
399 } else if (type == TYPE_DELETED) {
400 ep->type &= EXFAT_DELETE;
401 } else if (type == TYPE_STREAM) {
402 ep->type = EXFAT_STREAM;
403 } else if (type == TYPE_EXTEND) {
404 ep->type = EXFAT_NAME;
405 } else if (type == TYPE_BITMAP) {
406 ep->type = EXFAT_BITMAP;
407 } else if (type == TYPE_UPCASE) {
408 ep->type = EXFAT_UPCASE;
409 } else if (type == TYPE_VOLUME) {
410 ep->type = EXFAT_VOLUME;
411 } else if (type == TYPE_DIR) {
412 ep->type = EXFAT_FILE;
Jan Cincera0ab8ba72023-10-30 20:53:18 +0900413 ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR);
Namjae Jeonca061972020-03-02 15:21:35 +0900414 } else if (type == TYPE_FILE) {
415 ep->type = EXFAT_FILE;
Jan Cincera0ab8ba72023-10-30 20:53:18 +0900416 ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE);
Namjae Jeonca061972020-03-02 15:21:35 +0900417 }
418}
419
420static void exfat_init_stream_entry(struct exfat_dentry *ep,
Yuezhang Moee785c12023-07-20 14:40:08 +0800421 unsigned int start_clu, unsigned long long size)
Namjae Jeonca061972020-03-02 15:21:35 +0900422{
Yuezhang Mof1925792024-01-12 14:48:46 +0800423 memset(ep, 0, sizeof(*ep));
Namjae Jeonca061972020-03-02 15:21:35 +0900424 exfat_set_entry_type(ep, TYPE_STREAM);
Yuezhang Moee785c12023-07-20 14:40:08 +0800425 if (size == 0)
426 ep->dentry.stream.flags = ALLOC_FAT_CHAIN;
427 else
428 ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN;
Namjae Jeonca061972020-03-02 15:21:35 +0900429 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
430 ep->dentry.stream.valid_size = cpu_to_le64(size);
431 ep->dentry.stream.size = cpu_to_le64(size);
432}
433
434static void exfat_init_name_entry(struct exfat_dentry *ep,
435 unsigned short *uniname)
436{
437 int i;
438
439 exfat_set_entry_type(ep, TYPE_EXTEND);
440 ep->dentry.name.flags = 0x0;
441
442 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
Hyeongseok.Kim4ba6ccd2020-06-09 14:30:44 +0900443 if (*uniname != 0x0) {
444 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
445 uniname++;
446 } else {
447 ep->dentry.name.unicode_0_14[i] = 0x0;
448 }
Namjae Jeonca061972020-03-02 15:21:35 +0900449 }
450}
451
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800452void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
453 unsigned int type, unsigned int start_clu,
454 unsigned long long size, struct timespec64 *ts)
Namjae Jeonca061972020-03-02 15:21:35 +0900455{
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800456 struct super_block *sb = es->sb;
Namjae Jeonca061972020-03-02 15:21:35 +0900457 struct exfat_sb_info *sbi = EXFAT_SB(sb);
Namjae Jeonca061972020-03-02 15:21:35 +0900458 struct exfat_dentry *ep;
Namjae Jeonca061972020-03-02 15:21:35 +0900459
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800460 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
Yuezhang Mof1925792024-01-12 14:48:46 +0800461 memset(ep, 0, sizeof(*ep));
Namjae Jeonca061972020-03-02 15:21:35 +0900462 exfat_set_entry_type(ep, type);
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800463 exfat_set_entry_time(sbi, ts,
Namjae Jeonca061972020-03-02 15:21:35 +0900464 &ep->dentry.file.create_tz,
465 &ep->dentry.file.create_time,
466 &ep->dentry.file.create_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900467 &ep->dentry.file.create_time_cs);
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800468 exfat_set_entry_time(sbi, ts,
Namjae Jeonca061972020-03-02 15:21:35 +0900469 &ep->dentry.file.modify_tz,
470 &ep->dentry.file.modify_time,
471 &ep->dentry.file.modify_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900472 &ep->dentry.file.modify_time_cs);
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800473 exfat_set_entry_time(sbi, ts,
Namjae Jeonca061972020-03-02 15:21:35 +0900474 &ep->dentry.file.access_tz,
475 &ep->dentry.file.access_time,
476 &ep->dentry.file.access_date,
477 NULL);
478
Yuezhang Mocf8663f2022-08-04 16:01:50 +0800479 ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
Yuezhang Moee785c12023-07-20 14:40:08 +0800480 exfat_init_stream_entry(ep, start_clu, size);
Namjae Jeonca061972020-03-02 15:21:35 +0900481}
482
Namjae Jeon8258ef22023-01-14 13:09:48 +0900483static void exfat_free_benign_secondary_clusters(struct inode *inode,
484 struct exfat_dentry *ep)
485{
486 struct super_block *sb = inode->i_sb;
487 struct exfat_chain dir;
488 unsigned int start_clu =
489 le32_to_cpu(ep->dentry.generic_secondary.start_clu);
490 u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
491 unsigned char flags = ep->dentry.generic_secondary.flags;
492
493 if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
494 return;
495
496 exfat_chain_set(&dir, start_clu,
497 EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
498 flags);
499 exfat_free_cluster(inode, &dir);
500}
501
Yuezhang Mod97e0602022-08-05 16:42:02 +0800502void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
503 struct exfat_uni_name *p_uniname)
Namjae Jeonca061972020-03-02 15:21:35 +0900504{
Namjae Jeonca061972020-03-02 15:21:35 +0900505 int i;
Namjae Jeonca061972020-03-02 15:21:35 +0900506 unsigned short *uniname = p_uniname->name;
507 struct exfat_dentry *ep;
Namjae Jeonca061972020-03-02 15:21:35 +0900508
Yuezhang Mod97e0602022-08-05 16:42:02 +0800509 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
Namjae Jeonca061972020-03-02 15:21:35 +0900510 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
Namjae Jeonca061972020-03-02 15:21:35 +0900511
Yuezhang Mod97e0602022-08-05 16:42:02 +0800512 ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
Namjae Jeonca061972020-03-02 15:21:35 +0900513 ep->dentry.stream.name_len = p_uniname->name_len;
514 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
Namjae Jeonca061972020-03-02 15:21:35 +0900515
Yuezhang Mod97e0602022-08-05 16:42:02 +0800516 for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
517 ep = exfat_get_dentry_cached(es, i);
Namjae Jeonca061972020-03-02 15:21:35 +0900518 exfat_init_name_entry(ep, uniname);
Namjae Jeonca061972020-03-02 15:21:35 +0900519 uniname += EXFAT_FILE_NAME_LEN;
520 }
521
Yuezhang Mo4d714552022-08-05 16:57:04 +0800522 exfat_update_dir_chksum(es);
Namjae Jeonca061972020-03-02 15:21:35 +0900523}
524
Yuezhang Moff4343d2022-08-05 15:55:58 +0800525void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
526 int order)
Namjae Jeonca061972020-03-02 15:21:35 +0900527{
Namjae Jeonca061972020-03-02 15:21:35 +0900528 int i;
Namjae Jeonca061972020-03-02 15:21:35 +0900529 struct exfat_dentry *ep;
Namjae Jeonca061972020-03-02 15:21:35 +0900530
Yuezhang Moff4343d2022-08-05 15:55:58 +0800531 for (i = order; i < es->num_entries; i++) {
532 ep = exfat_get_dentry_cached(es, i);
Namjae Jeonca061972020-03-02 15:21:35 +0900533
Namjae Jeon8258ef22023-01-14 13:09:48 +0900534 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
535 exfat_free_benign_secondary_clusters(inode, ep);
536
Namjae Jeonca061972020-03-02 15:21:35 +0900537 exfat_set_entry_type(ep, TYPE_DELETED);
Namjae Jeonca061972020-03-02 15:21:35 +0900538 }
539
Yuezhang Moff4343d2022-08-05 15:55:58 +0800540 if (order < es->num_entries)
541 es->modified = true;
Namjae Jeonca061972020-03-02 15:21:35 +0900542}
543
Yuezhang Mo4d714552022-08-05 16:57:04 +0800544void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
Namjae Jeonca061972020-03-02 15:21:35 +0900545{
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900546 int chksum_type = CS_DIR_ENTRY, i;
Namjae Jeonca061972020-03-02 15:21:35 +0900547 unsigned short chksum = 0;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900548 struct exfat_dentry *ep;
Namjae Jeonca061972020-03-02 15:21:35 +0900549
Yuezhang Mof3fe3952022-03-17 19:39:20 +0800550 for (i = ES_IDX_FILE; i < es->num_entries; i++) {
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900551 ep = exfat_get_dentry_cached(es, i);
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +0900552 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
553 chksum_type);
Namjae Jeonca061972020-03-02 15:21:35 +0900554 chksum_type = CS_DEFAULT;
555 }
Yuezhang Mof3fe3952022-03-17 19:39:20 +0800556 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900557 ep->dentry.file.checksum = cpu_to_le16(chksum);
558 es->modified = true;
559}
Namjae Jeonca061972020-03-02 15:21:35 +0900560
Yuezhang Mo3b9681a2022-03-17 18:12:40 +0800561int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900562{
Tetsuhiro Kohada3db3c3f2020-06-23 15:22:19 +0900563 int i, err = 0;
Namjae Jeonca061972020-03-02 15:21:35 +0900564
Tetsuhiro Kohada3db3c3f2020-06-23 15:22:19 +0900565 if (es->modified)
566 err = exfat_update_bhs(es->bh, es->num_bh, sync);
567
568 for (i = 0; i < es->num_bh; i++)
569 if (err)
570 bforget(es->bh[i]);
571 else
572 brelse(es->bh[i]);
Yuezhang Moa3ff29a2022-11-09 13:50:22 +0800573
574 if (IS_DYNAMIC_ES(es))
575 kfree(es->bh);
576
Tetsuhiro Kohada8b0c4712020-06-24 09:54:54 +0900577 return err;
Namjae Jeonca061972020-03-02 15:21:35 +0900578}
579
580static int exfat_walk_fat_chain(struct super_block *sb,
581 struct exfat_chain *p_dir, unsigned int byte_offset,
582 unsigned int *clu)
583{
584 struct exfat_sb_info *sbi = EXFAT_SB(sb);
585 unsigned int clu_offset;
586 unsigned int cur_clu;
587
588 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
589 cur_clu = p_dir->dir;
590
591 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
592 cur_clu += clu_offset;
593 } else {
594 while (clu_offset > 0) {
595 if (exfat_get_next_cluster(sb, &cur_clu))
596 return -EIO;
597 if (cur_clu == EXFAT_EOF_CLUSTER) {
598 exfat_fs_error(sb,
599 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
600 p_dir->dir,
601 EXFAT_B_TO_DEN(byte_offset));
602 return -EIO;
603 }
604 clu_offset--;
605 }
606 }
607
608 *clu = cur_clu;
609 return 0;
610}
611
Christophe Vu-Brugier8cf05882021-11-02 22:23:57 +0100612static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
613 int entry, sector_t *sector, int *offset)
Namjae Jeonca061972020-03-02 15:21:35 +0900614{
615 int ret;
616 unsigned int off, clu = 0;
617 struct exfat_sb_info *sbi = EXFAT_SB(sb);
618
619 off = EXFAT_DEN_TO_B(entry);
620
621 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
622 if (ret)
623 return ret;
624
625 /* byte offset in cluster */
626 off = EXFAT_CLU_OFFSET(off, sbi);
627
628 /* byte offset in sector */
629 *offset = EXFAT_BLK_OFFSET(off, sb);
630
631 /* sector offset in cluster */
632 *sector = EXFAT_B_TO_BLK(off, sb);
633 *sector += exfat_cluster_to_sector(sbi, clu);
634 return 0;
635}
636
637#define EXFAT_MAX_RA_SIZE (128*1024)
638static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
639{
640 struct exfat_sb_info *sbi = EXFAT_SB(sb);
641 struct buffer_head *bh;
642 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
643 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
644 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
645 unsigned int ra_count = min(adj_ra_count, max_ra_count);
646
647 /* Read-ahead is not required */
648 if (sbi->sect_per_clus == 1)
649 return 0;
650
651 if (sec < sbi->data_start_sector) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900652 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
653 (unsigned long long)sec, sbi->data_start_sector);
Namjae Jeonca061972020-03-02 15:21:35 +0900654 return -EIO;
655 }
656
657 /* Not sector aligned with ra_count, resize ra_count to page size */
658 if ((sec - sbi->data_start_sector) & (ra_count - 1))
659 ra_count = page_ra_count;
660
661 bh = sb_find_get_block(sb, sec);
662 if (!bh || !buffer_uptodate(bh)) {
663 unsigned int i;
664
665 for (i = 0; i < ra_count; i++)
666 sb_breadahead(sb, (sector_t)(sec + i));
667 }
668 brelse(bh);
669 return 0;
670}
671
672struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
Yuezhang.Moc71510b2021-12-16 21:58:37 +0900673 struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
Namjae Jeonca061972020-03-02 15:21:35 +0900674{
675 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
676 int off;
677 sector_t sec;
678
679 if (p_dir->dir == DIR_DELETED) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900680 exfat_err(sb, "abnormal access to deleted dentry");
Namjae Jeonca061972020-03-02 15:21:35 +0900681 return NULL;
682 }
683
684 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
685 return NULL;
686
687 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
688 !(entry & (dentries_per_page - 1)))
689 exfat_dir_readahead(sb, sec);
690
691 *bh = sb_bread(sb, sec);
692 if (!*bh)
693 return NULL;
694
Namjae Jeonca061972020-03-02 15:21:35 +0900695 return (struct exfat_dentry *)((*bh)->b_data + off);
696}
697
698enum exfat_validate_dentry_mode {
Namjae Jeonca061972020-03-02 15:21:35 +0900699 ES_MODE_GET_FILE_ENTRY,
700 ES_MODE_GET_STRM_ENTRY,
701 ES_MODE_GET_NAME_ENTRY,
702 ES_MODE_GET_CRITICAL_SEC_ENTRY,
Namjae Jeon8258ef22023-01-14 13:09:48 +0900703 ES_MODE_GET_BENIGN_SEC_ENTRY,
Namjae Jeonca061972020-03-02 15:21:35 +0900704};
705
706static bool exfat_validate_entry(unsigned int type,
707 enum exfat_validate_dentry_mode *mode)
708{
709 if (type == TYPE_UNUSED || type == TYPE_DELETED)
710 return false;
711
712 switch (*mode) {
Namjae Jeonca061972020-03-02 15:21:35 +0900713 case ES_MODE_GET_FILE_ENTRY:
714 if (type != TYPE_STREAM)
715 return false;
716 *mode = ES_MODE_GET_STRM_ENTRY;
Namjae Jeon8258ef22023-01-14 13:09:48 +0900717 break;
Namjae Jeonca061972020-03-02 15:21:35 +0900718 case ES_MODE_GET_STRM_ENTRY:
719 if (type != TYPE_EXTEND)
720 return false;
721 *mode = ES_MODE_GET_NAME_ENTRY;
Namjae Jeon8258ef22023-01-14 13:09:48 +0900722 break;
Namjae Jeonca061972020-03-02 15:21:35 +0900723 case ES_MODE_GET_NAME_ENTRY:
Namjae Jeon8258ef22023-01-14 13:09:48 +0900724 if (type & TYPE_BENIGN_SEC)
725 *mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
726 else if (type != TYPE_EXTEND)
Namjae Jeonca061972020-03-02 15:21:35 +0900727 return false;
Namjae Jeon8258ef22023-01-14 13:09:48 +0900728 break;
729 case ES_MODE_GET_BENIGN_SEC_ENTRY:
730 /* Assume unreconized benign secondary entry */
731 if (!(type & TYPE_BENIGN_SEC))
Namjae Jeonca061972020-03-02 15:21:35 +0900732 return false;
Namjae Jeon8258ef22023-01-14 13:09:48 +0900733 break;
Namjae Jeonca061972020-03-02 15:21:35 +0900734 default:
Namjae Jeonca061972020-03-02 15:21:35 +0900735 return false;
736 }
Namjae Jeon8258ef22023-01-14 13:09:48 +0900737
738 return true;
Namjae Jeonca061972020-03-02 15:21:35 +0900739}
740
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900741struct exfat_dentry *exfat_get_dentry_cached(
742 struct exfat_entry_set_cache *es, int num)
743{
744 int off = es->start_off + num * DENTRY_SIZE;
745 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
746 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
747
748 return (struct exfat_dentry *)p;
749}
750
Namjae Jeonca061972020-03-02 15:21:35 +0900751/*
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800752 * Returns a set of dentries.
Namjae Jeonca061972020-03-02 15:21:35 +0900753 *
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900754 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
755 * User should call exfat_get_dentry_set() after setting 'modified' to apply
756 * changes made in this entry set to the real device.
Namjae Jeonca061972020-03-02 15:21:35 +0900757 *
758 * in:
759 * sb+p_dir+entry: indicates a file/dir
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800760 * num_entries: specifies how many dentries should be included.
761 * It will be set to es->num_entries if it is not 0.
762 * If num_entries is 0, es->num_entries will be obtained
763 * from the first dentry.
764 * out:
765 * es: pointer of entry set on success.
Namjae Jeonca061972020-03-02 15:21:35 +0900766 * return:
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800767 * 0 on success
768 * -error code on failure
Namjae Jeonca061972020-03-02 15:21:35 +0900769 */
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800770static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800771 struct super_block *sb, struct exfat_chain *p_dir, int entry,
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800772 unsigned int num_entries)
Namjae Jeonca061972020-03-02 15:21:35 +0900773{
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900774 int ret, i, num_bh;
Yuezhang Mo36955d32022-11-17 11:31:30 +0800775 unsigned int off;
Namjae Jeonca061972020-03-02 15:21:35 +0900776 sector_t sec;
777 struct exfat_sb_info *sbi = EXFAT_SB(sb);
Namjae Jeonca061972020-03-02 15:21:35 +0900778 struct buffer_head *bh;
779
780 if (p_dir->dir == DIR_DELETED) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900781 exfat_err(sb, "access to deleted dentry");
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800782 return -EIO;
Namjae Jeonca061972020-03-02 15:21:35 +0900783 }
784
Yuezhang Mo36955d32022-11-17 11:31:30 +0800785 ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
Namjae Jeonca061972020-03-02 15:21:35 +0900786 if (ret)
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800787 return ret;
Namjae Jeonca061972020-03-02 15:21:35 +0900788
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800789 memset(es, 0, sizeof(*es));
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900790 es->sb = sb;
791 es->modified = false;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900792 es->start_off = off;
Yuezhang Moa3ff29a2022-11-09 13:50:22 +0800793 es->bh = es->__bh;
Namjae Jeonca061972020-03-02 15:21:35 +0900794
Namjae Jeonca061972020-03-02 15:21:35 +0900795 bh = sb_bread(sb, sec);
796 if (!bh)
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800797 return -EIO;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900798 es->bh[es->num_bh++] = bh;
Namjae Jeonca061972020-03-02 15:21:35 +0900799
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800800 if (num_entries == ES_ALL_ENTRIES) {
801 struct exfat_dentry *ep;
Namjae Jeonca061972020-03-02 15:21:35 +0900802
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800803 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
804 if (ep->type != EXFAT_FILE) {
805 brelse(bh);
806 return -EIO;
807 }
808
809 num_entries = ep->dentry.file.num_ext + 1;
810 }
811
Namjae Jeonca061972020-03-02 15:21:35 +0900812 es->num_entries = num_entries;
Namjae Jeonca061972020-03-02 15:21:35 +0900813
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900814 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
Yuezhang Moa3ff29a2022-11-09 13:50:22 +0800815 if (num_bh > ARRAY_SIZE(es->__bh)) {
816 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL);
817 if (!es->bh) {
818 brelse(bh);
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800819 return -ENOMEM;
Yuezhang Moa3ff29a2022-11-09 13:50:22 +0800820 }
821 es->bh[0] = bh;
822 }
823
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900824 for (i = 1; i < num_bh; i++) {
825 /* get the next sector */
826 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
Yuezhang Mo36955d32022-11-17 11:31:30 +0800827 unsigned int clu = exfat_sector_to_cluster(sbi, sec);
828
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900829 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
830 clu++;
831 else if (exfat_get_next_cluster(sb, &clu))
Yuezhang Mo3b9681a2022-03-17 18:12:40 +0800832 goto put_es;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900833 sec = exfat_cluster_to_sector(sbi, clu);
Namjae Jeonca061972020-03-02 15:21:35 +0900834 } else {
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900835 sec++;
Namjae Jeonca061972020-03-02 15:21:35 +0900836 }
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900837
838 bh = sb_bread(sb, sec);
839 if (!bh)
Yuezhang Mo3b9681a2022-03-17 18:12:40 +0800840 goto put_es;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900841 es->bh[es->num_bh++] = bh;
Namjae Jeonca061972020-03-02 15:21:35 +0900842 }
843
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800844 return 0;
845
846put_es:
847 exfat_put_dentry_set(es, false);
848 return -EIO;
849}
850
851int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
852 struct super_block *sb, struct exfat_chain *p_dir,
853 int entry, unsigned int num_entries)
854{
855 int ret, i;
856 struct exfat_dentry *ep;
857 enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
858
859 ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
860 if (ret < 0)
861 return ret;
862
Christophe Vu-Brugier6fa96cd2021-11-02 22:23:56 +0100863 /* validate cached dentries */
Yuezhang Mo7b6bab22023-12-08 19:17:02 +0800864 for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900865 ep = exfat_get_dentry_cached(es, i);
866 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
Yuezhang Mo3b9681a2022-03-17 18:12:40 +0800867 goto put_es;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900868 }
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800869 return 0;
Namjae Jeonca061972020-03-02 15:21:35 +0900870
Yuezhang Mo3b9681a2022-03-17 18:12:40 +0800871put_es:
872 exfat_put_dentry_set(es, false);
Yuezhang Mo20914ff2022-11-17 11:37:13 +0800873 return -EIO;
Namjae Jeonca061972020-03-02 15:21:35 +0900874}
875
Yuezhang Mo01da3a52023-10-30 17:25:31 +0800876static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
877{
878 struct exfat_dentry *ep;
879 struct buffer_head *bh;
880 int i, off;
881 bool unused_hit = false;
882
883 /*
884 * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
885 * Although it violates the specification for a deleted entry to
886 * follow an unused entry, some exFAT implementations could work
887 * like this. Therefore, to improve compatibility, let's allow it.
888 */
889 for (i = 0; i < es->num_entries; i++) {
890 ep = exfat_get_dentry_cached(es, i);
891 if (ep->type == EXFAT_UNUSED) {
892 unused_hit = true;
893 } else if (!IS_EXFAT_DELETED(ep->type)) {
894 if (unused_hit)
895 goto err_used_follow_unused;
896 i++;
897 goto count_skip_entries;
898 }
899 }
900
901 return 0;
902
903err_used_follow_unused:
904 off = es->start_off + (i << DENTRY_SIZE_BITS);
905 bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
906
907 exfat_fs_error(es->sb,
908 "in sector %lld, dentry %d should be unused, but 0x%x",
909 bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
910
911 return -EIO;
912
913count_skip_entries:
914 es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off);
915 for (; i < es->num_entries; i++) {
916 ep = exfat_get_dentry_cached(es, i);
917 if (IS_EXFAT_DELETED(ep->type))
918 break;
919 }
920
921 return i;
922}
923
924/*
925 * Get an empty dentry set.
926 *
927 * in:
928 * sb+p_dir+entry: indicates the empty dentry location
929 * num_entries: specifies how many empty dentries should be included.
930 * out:
931 * es: pointer of empty dentry set on success.
932 * return:
933 * 0 : on success
934 * >0 : the dentries are not empty, the return value is the number of
935 * dentries to be skipped for the next lookup.
936 * <0 : on failure
937 */
938int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
939 struct super_block *sb, struct exfat_chain *p_dir,
940 int entry, unsigned int num_entries)
941{
942 int ret;
943
944 ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
945 if (ret < 0)
946 return ret;
947
948 ret = exfat_validate_empty_dentry_set(es);
949 if (ret)
950 exfat_put_dentry_set(es, false);
951
952 return ret;
953}
954
Yuezhang Moff398992022-07-21 09:59:32 +0800955static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
956{
957 hint_femp->eidx = EXFAT_HINT_NONE;
958 hint_femp->count = 0;
959}
960
961static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
962 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
Yuezhang Moe298c8a2022-11-07 17:22:13 +0900963 int dentry, int num_entries, int entry_type)
Yuezhang Moff398992022-07-21 09:59:32 +0800964{
965 if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
966 ei->hint_femp.eidx > dentry) {
Yuezhang Moe298c8a2022-11-07 17:22:13 +0900967 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
968
Yuezhang Moff398992022-07-21 09:59:32 +0800969 if (candi_empty->count == 0) {
970 candi_empty->cur = *clu;
971 candi_empty->eidx = dentry;
972 }
973
Yuezhang Moe298c8a2022-11-07 17:22:13 +0900974 if (entry_type == TYPE_UNUSED)
975 candi_empty->count += total_entries - dentry;
976 else
977 candi_empty->count++;
978
979 if (candi_empty->count == num_entries ||
980 candi_empty->count + candi_empty->eidx == total_entries)
Yuezhang Moff398992022-07-21 09:59:32 +0800981 ei->hint_femp = *candi_empty;
982 }
983}
984
Namjae Jeonca061972020-03-02 15:21:35 +0900985enum {
986 DIRENT_STEP_FILE,
987 DIRENT_STEP_STRM,
988 DIRENT_STEP_NAME,
989 DIRENT_STEP_SECD,
990};
991
992/*
Hyeongseok Kimc6e2f522021-03-22 12:53:36 +0900993 * @ei: inode info of parent directory
994 * @p_dir: directory structure of parent directory
995 * @num_entries:entry size of p_uniname
996 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
997 * for traversing cluster chain.
998 * @return:
999 * >= 0: file directory entry position where the name exists
1000 * -ENOENT: entry with the name does not exist
1001 * -EIO: I/O error
Namjae Jeonca061972020-03-02 15:21:35 +09001002 */
1003int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
1004 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
Yuezhang Mo72880cb2022-04-10 16:12:14 +08001005 struct exfat_hint *hint_opt)
Namjae Jeonca061972020-03-02 15:21:35 +09001006{
1007 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
1008 int order, step, name_len = 0;
Yuezhang Moff398992022-07-21 09:59:32 +08001009 int dentries_per_clu;
Namjae Jeonca061972020-03-02 15:21:35 +09001010 unsigned int entry_type;
1011 unsigned short *uniname = NULL;
1012 struct exfat_chain clu;
1013 struct exfat_hint *hint_stat = &ei->hint_stat;
1014 struct exfat_hint_femp candi_empty;
1015 struct exfat_sb_info *sbi = EXFAT_SB(sb);
Yuezhang Mo72880cb2022-04-10 16:12:14 +08001016 int num_entries = exfat_calc_num_entries(p_uniname);
1017
1018 if (num_entries < 0)
1019 return num_entries;
Namjae Jeonca061972020-03-02 15:21:35 +09001020
1021 dentries_per_clu = sbi->dentries_per_clu;
1022
1023 exfat_chain_dup(&clu, p_dir);
1024
1025 if (hint_stat->eidx) {
1026 clu.dir = hint_stat->clu;
1027 dentry = hint_stat->eidx;
1028 end_eidx = dentry;
1029 }
1030
Yuezhang Moff398992022-07-21 09:59:32 +08001031 exfat_reset_empty_hint(&ei->hint_femp);
1032
Namjae Jeonca061972020-03-02 15:21:35 +09001033rewind:
1034 order = 0;
1035 step = DIRENT_STEP_FILE;
Yuezhang Moff398992022-07-21 09:59:32 +08001036 exfat_reset_empty_hint(&candi_empty);
1037
Namjae Jeonca061972020-03-02 15:21:35 +09001038 while (clu.dir != EXFAT_EOF_CLUSTER) {
1039 i = dentry & (dentries_per_clu - 1);
1040 for (; i < dentries_per_clu; i++, dentry++) {
1041 struct exfat_dentry *ep;
1042 struct buffer_head *bh;
1043
1044 if (rewind && dentry == end_eidx)
1045 goto not_found;
1046
Yuezhang.Moc71510b2021-12-16 21:58:37 +09001047 ep = exfat_get_dentry(sb, &clu, i, &bh);
Namjae Jeonca061972020-03-02 15:21:35 +09001048 if (!ep)
1049 return -EIO;
1050
1051 entry_type = exfat_get_entry_type(ep);
1052
1053 if (entry_type == TYPE_UNUSED ||
1054 entry_type == TYPE_DELETED) {
1055 step = DIRENT_STEP_FILE;
1056
Yuezhang Moff398992022-07-21 09:59:32 +08001057 exfat_set_empty_hint(ei, &candi_empty, &clu,
Yuezhang Moe298c8a2022-11-07 17:22:13 +09001058 dentry, num_entries,
1059 entry_type);
Namjae Jeonca061972020-03-02 15:21:35 +09001060
1061 brelse(bh);
1062 if (entry_type == TYPE_UNUSED)
1063 goto not_found;
1064 continue;
1065 }
1066
Yuezhang Moff398992022-07-21 09:59:32 +08001067 exfat_reset_empty_hint(&candi_empty);
Namjae Jeonca061972020-03-02 15:21:35 +09001068
1069 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1070 step = DIRENT_STEP_FILE;
Hyeongseok Kimc6e2f522021-03-22 12:53:36 +09001071 hint_opt->clu = clu.dir;
1072 hint_opt->eidx = i;
Yuezhang Mo72880cb2022-04-10 16:12:14 +08001073 num_ext = ep->dentry.file.num_ext;
1074 step = DIRENT_STEP_STRM;
Namjae Jeonca061972020-03-02 15:21:35 +09001075 brelse(bh);
1076 continue;
1077 }
1078
1079 if (entry_type == TYPE_STREAM) {
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +09001080 u16 name_hash;
Namjae Jeonca061972020-03-02 15:21:35 +09001081
1082 if (step != DIRENT_STEP_STRM) {
1083 step = DIRENT_STEP_FILE;
1084 brelse(bh);
1085 continue;
1086 }
1087 step = DIRENT_STEP_FILE;
1088 name_hash = le16_to_cpu(
1089 ep->dentry.stream.name_hash);
1090 if (p_uniname->name_hash == name_hash &&
1091 p_uniname->name_len ==
1092 ep->dentry.stream.name_len) {
1093 step = DIRENT_STEP_NAME;
1094 order = 1;
1095 name_len = 0;
1096 }
1097 brelse(bh);
1098 continue;
1099 }
1100
1101 brelse(bh);
1102 if (entry_type == TYPE_EXTEND) {
1103 unsigned short entry_uniname[16], unichar;
1104
Namjae Jeond4233452023-07-13 21:59:37 +09001105 if (step != DIRENT_STEP_NAME ||
1106 name_len >= MAX_NAME_LENGTH) {
Namjae Jeonca061972020-03-02 15:21:35 +09001107 step = DIRENT_STEP_FILE;
1108 continue;
1109 }
1110
1111 if (++order == 2)
1112 uniname = p_uniname->name;
1113 else
1114 uniname += EXFAT_FILE_NAME_LEN;
1115
1116 len = exfat_extract_uni_name(ep, entry_uniname);
1117 name_len += len;
1118
1119 unichar = *(uniname+len);
1120 *(uniname+len) = 0x0;
1121
1122 if (exfat_uniname_ncmp(sb, uniname,
1123 entry_uniname, len)) {
1124 step = DIRENT_STEP_FILE;
1125 } else if (p_uniname->name_len == name_len) {
1126 if (order == num_ext)
1127 goto found;
1128 step = DIRENT_STEP_SECD;
1129 }
1130
1131 *(uniname+len) = unichar;
1132 continue;
1133 }
1134
1135 if (entry_type &
1136 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1137 if (step == DIRENT_STEP_SECD) {
1138 if (++order == num_ext)
1139 goto found;
1140 continue;
1141 }
1142 }
1143 step = DIRENT_STEP_FILE;
1144 }
1145
1146 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1147 if (--clu.size > 0)
1148 clu.dir++;
1149 else
1150 clu.dir = EXFAT_EOF_CLUSTER;
1151 } else {
1152 if (exfat_get_next_cluster(sb, &clu.dir))
1153 return -EIO;
1154 }
1155 }
1156
1157not_found:
1158 /*
1159 * We started at not 0 index,so we should try to find target
1160 * from 0 index to the index we started at.
1161 */
1162 if (!rewind && end_eidx) {
1163 rewind = 1;
1164 dentry = 0;
1165 clu.dir = p_dir->dir;
Namjae Jeonca061972020-03-02 15:21:35 +09001166 goto rewind;
1167 }
1168
Yuezhang Moe298c8a2022-11-07 17:22:13 +09001169 /*
1170 * set the EXFAT_EOF_CLUSTER flag to avoid search
1171 * from the beginning again when allocated a new cluster
1172 */
1173 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1174 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1175 ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1176 ei->hint_femp.count = 0;
1177 }
1178
Namjae Jeonca061972020-03-02 15:21:35 +09001179 /* initialized hint_stat */
1180 hint_stat->clu = p_dir->dir;
1181 hint_stat->eidx = 0;
1182 return -ENOENT;
1183
1184found:
1185 /* next dentry we'll find is out of this cluster */
1186 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1187 int ret = 0;
1188
1189 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1190 if (--clu.size > 0)
1191 clu.dir++;
1192 else
1193 clu.dir = EXFAT_EOF_CLUSTER;
1194 } else {
1195 ret = exfat_get_next_cluster(sb, &clu.dir);
1196 }
1197
Namjae Jeond2fa0c32020-07-03 11:19:46 +09001198 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
Namjae Jeonca061972020-03-02 15:21:35 +09001199 /* just initialized hint_stat */
1200 hint_stat->clu = p_dir->dir;
1201 hint_stat->eidx = 0;
1202 return (dentry - num_ext);
1203 }
1204 }
1205
1206 hint_stat->clu = clu.dir;
1207 hint_stat->eidx = dentry + 1;
1208 return dentry - num_ext;
1209}
1210
Namjae Jeonca061972020-03-02 15:21:35 +09001211int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1212{
1213 int i, count = 0;
1214 int dentries_per_clu;
1215 unsigned int entry_type;
1216 struct exfat_chain clu;
1217 struct exfat_dentry *ep;
1218 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1219 struct buffer_head *bh;
1220
1221 dentries_per_clu = sbi->dentries_per_clu;
1222
1223 exfat_chain_dup(&clu, p_dir);
1224
1225 while (clu.dir != EXFAT_EOF_CLUSTER) {
1226 for (i = 0; i < dentries_per_clu; i++) {
Yuezhang.Moc71510b2021-12-16 21:58:37 +09001227 ep = exfat_get_dentry(sb, &clu, i, &bh);
Namjae Jeonca061972020-03-02 15:21:35 +09001228 if (!ep)
1229 return -EIO;
1230 entry_type = exfat_get_entry_type(ep);
1231 brelse(bh);
1232
1233 if (entry_type == TYPE_UNUSED)
1234 return count;
1235 if (entry_type != TYPE_DIR)
1236 continue;
1237 count++;
1238 }
1239
1240 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1241 if (--clu.size > 0)
1242 clu.dir++;
1243 else
1244 clu.dir = EXFAT_EOF_CLUSTER;
1245 } else {
1246 if (exfat_get_next_cluster(sb, &(clu.dir)))
1247 return -EIO;
1248 }
1249 }
1250
1251 return count;
1252}