blob: dedbc55cd48f55f1e895dea0dd56ea590bd17ae3 [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>
7#include <linux/bio.h>
8#include <linux/buffer_head.h>
9
10#include "exfat_raw.h"
11#include "exfat_fs.h"
12
13static int exfat_extract_uni_name(struct exfat_dentry *ep,
14 unsigned short *uniname)
15{
16 int i, len = 0;
17
18 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
19 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
20 if (*uniname == 0x0)
21 return len;
22 uniname++;
23 len++;
24 }
25
26 *uniname = 0x0;
27 return len;
28
29}
30
31static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
32 struct exfat_chain *p_dir, int entry, unsigned short *uniname)
33{
34 int i;
Namjae Jeonca061972020-03-02 15:21:35 +090035 struct exfat_entry_set_cache *es;
36
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +090037 es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES);
Namjae Jeonca061972020-03-02 15:21:35 +090038 if (!es)
39 return;
40
Namjae Jeonca061972020-03-02 15:21:35 +090041 /*
42 * First entry : file entry
43 * Second entry : stream-extension entry
44 * Third entry : first file-name entry
45 * So, the index of first file-name dentry should start from 2.
46 */
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +090047 for (i = 2; i < es->num_entries; i++) {
48 struct exfat_dentry *ep = exfat_get_dentry_cached(es, i);
49
Namjae Jeonca061972020-03-02 15:21:35 +090050 /* end of name entry */
51 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +090052 break;
Namjae Jeonca061972020-03-02 15:21:35 +090053
54 exfat_extract_uni_name(ep, uniname);
55 uniname += EXFAT_FILE_NAME_LEN;
56 }
57
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +090058 exfat_free_dentry_set(es, false);
Namjae Jeonca061972020-03-02 15:21:35 +090059}
60
61/* read a directory entry from the opened directory */
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +090062static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
Namjae Jeonca061972020-03-02 15:21:35 +090063{
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +090064 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
Namjae Jeon5c6956e2021-06-11 09:40:24 +090065 unsigned int type, clu_offset, max_dentries;
Namjae Jeonca061972020-03-02 15:21:35 +090066 sector_t sector;
67 struct exfat_chain dir, clu;
68 struct exfat_uni_name uni_name;
69 struct exfat_dentry *ep;
70 struct super_block *sb = inode->i_sb;
71 struct exfat_sb_info *sbi = EXFAT_SB(sb);
72 struct exfat_inode_info *ei = EXFAT_I(inode);
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +090073 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
Namjae Jeonca061972020-03-02 15:21:35 +090074 struct buffer_head *bh;
75
76 /* check if the given file ID is opened */
77 if (ei->type != TYPE_DIR)
78 return -EPERM;
79
80 if (ei->entry == -1)
81 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
82 else
83 exfat_chain_set(&dir, ei->start_clu,
84 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
85
86 dentries_per_clu = sbi->dentries_per_clu;
87 dentries_per_clu_bits = ilog2(dentries_per_clu);
Namjae Jeon5c6956e2021-06-11 09:40:24 +090088 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
89 (u64)sbi->num_clusters << dentries_per_clu_bits);
Namjae Jeonca061972020-03-02 15:21:35 +090090
91 clu_offset = dentry >> dentries_per_clu_bits;
92 exfat_chain_dup(&clu, &dir);
93
94 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
95 clu.dir += clu_offset;
96 clu.size -= clu_offset;
97 } else {
98 /* hint_information */
99 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
100 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
101 clu_offset -= ei->hint_bmap.off;
102 clu.dir = ei->hint_bmap.clu;
103 }
104
105 while (clu_offset > 0) {
106 if (exfat_get_next_cluster(sb, &(clu.dir)))
107 return -EIO;
108
109 clu_offset--;
110 }
111 }
112
Namjae Jeon5c6956e2021-06-11 09:40:24 +0900113 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
Namjae Jeonca061972020-03-02 15:21:35 +0900114 i = dentry & (dentries_per_clu - 1);
115
116 for ( ; i < dentries_per_clu; i++, dentry++) {
117 ep = exfat_get_dentry(sb, &clu, i, &bh, &sector);
118 if (!ep)
119 return -EIO;
120
121 type = exfat_get_entry_type(ep);
122 if (type == TYPE_UNUSED) {
123 brelse(bh);
124 break;
125 }
126
127 if (type != TYPE_FILE && type != TYPE_DIR) {
128 brelse(bh);
129 continue;
130 }
131
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900132 num_ext = ep->dentry.file.num_ext;
Namjae Jeonca061972020-03-02 15:21:35 +0900133 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
134 exfat_get_entry_time(sbi, &dir_entry->crtime,
135 ep->dentry.file.create_tz,
136 ep->dentry.file.create_time,
137 ep->dentry.file.create_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900138 ep->dentry.file.create_time_cs);
Namjae Jeonca061972020-03-02 15:21:35 +0900139 exfat_get_entry_time(sbi, &dir_entry->mtime,
140 ep->dentry.file.modify_tz,
141 ep->dentry.file.modify_time,
142 ep->dentry.file.modify_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900143 ep->dentry.file.modify_time_cs);
Namjae Jeonca061972020-03-02 15:21:35 +0900144 exfat_get_entry_time(sbi, &dir_entry->atime,
145 ep->dentry.file.access_tz,
146 ep->dentry.file.access_time,
147 ep->dentry.file.access_date,
148 0);
149
150 *uni_name.name = 0x0;
151 exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
152 uni_name.name);
153 exfat_utf16_to_nls(sb, &uni_name,
154 dir_entry->namebuf.lfn,
155 dir_entry->namebuf.lfnbuf_len);
156 brelse(bh);
157
158 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
159 if (!ep)
160 return -EIO;
161 dir_entry->size =
162 le64_to_cpu(ep->dentry.stream.valid_size);
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900163 dir_entry->entry = dentry;
Namjae Jeonca061972020-03-02 15:21:35 +0900164 brelse(bh);
165
166 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
167 ei->hint_bmap.clu = clu.dir;
168
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900169 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
Namjae Jeonca061972020-03-02 15:21:35 +0900170 return 0;
171 }
172
173 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
174 if (--clu.size > 0)
175 clu.dir++;
176 else
177 clu.dir = EXFAT_EOF_CLUSTER;
178 } else {
179 if (exfat_get_next_cluster(sb, &(clu.dir)))
180 return -EIO;
181 }
182 }
183
184 dir_entry->namebuf.lfn[0] = '\0';
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900185 *cpos = EXFAT_DEN_TO_B(dentry);
Namjae Jeonca061972020-03-02 15:21:35 +0900186 return 0;
187}
188
189static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
190{
191 nb->lfn = NULL;
192 nb->lfnbuf_len = 0;
193}
194
195static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
196{
197 nb->lfn = __getname();
198 if (!nb->lfn)
199 return -ENOMEM;
200 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
201 return 0;
202}
203
204static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
205{
206 if (!nb->lfn)
207 return;
208
209 __putname(nb->lfn);
210 exfat_init_namebuf(nb);
211}
212
213/* skip iterating emit_dots when dir is empty */
214#define ITER_POS_FILLED_DOTS (2)
215static int exfat_iterate(struct file *filp, struct dir_context *ctx)
216{
217 struct inode *inode = filp->f_path.dentry->d_inode;
218 struct super_block *sb = inode->i_sb;
219 struct inode *tmp;
220 struct exfat_dir_entry de;
221 struct exfat_dentry_namebuf *nb = &(de.namebuf);
222 struct exfat_inode_info *ei = EXFAT_I(inode);
223 unsigned long inum;
224 loff_t cpos, i_pos;
225 int err = 0, fake_offset = 0;
226
227 exfat_init_namebuf(nb);
228 mutex_lock(&EXFAT_SB(sb)->s_lock);
229
230 cpos = ctx->pos;
231 if (!dir_emit_dots(filp, ctx))
232 goto unlock;
233
234 if (ctx->pos == ITER_POS_FILLED_DOTS) {
235 cpos = 0;
236 fake_offset = 1;
237 }
238
239 if (cpos & (DENTRY_SIZE - 1)) {
240 err = -ENOENT;
241 goto unlock;
242 }
243
244 /* name buffer should be allocated before use */
245 err = exfat_alloc_namebuf(nb);
246 if (err)
247 goto unlock;
248get_new:
Namjae Jeon5c6956e2021-06-11 09:40:24 +0900249 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
Namjae Jeonca061972020-03-02 15:21:35 +0900250 goto end_of_dir;
251
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900252 err = exfat_readdir(inode, &cpos, &de);
Namjae Jeonca061972020-03-02 15:21:35 +0900253 if (err) {
254 /*
255 * At least we tried to read a sector. Move cpos to next sector
256 * position (should be aligned).
257 */
258 if (err == -EIO) {
259 cpos += 1 << (sb->s_blocksize_bits);
260 cpos &= ~(sb->s_blocksize - 1);
261 }
262
263 err = -EIO;
264 goto end_of_dir;
265 }
266
Namjae Jeonca061972020-03-02 15:21:35 +0900267 if (!nb->lfn[0])
268 goto end_of_dir;
269
Tetsuhiro Kohada04cee522020-09-17 10:39:16 +0900270 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
Namjae Jeonca061972020-03-02 15:21:35 +0900271 tmp = exfat_iget(sb, i_pos);
272 if (tmp) {
273 inum = tmp->i_ino;
274 iput(tmp);
275 } else {
276 inum = iunique(sb, EXFAT_ROOT_INO);
277 }
278
279 /*
280 * Before calling dir_emit(), sb_lock should be released.
281 * Because page fault can occur in dir_emit() when the size
282 * of buffer given from user is larger than one page size.
283 */
284 mutex_unlock(&EXFAT_SB(sb)->s_lock);
285 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
286 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
287 goto out_unlocked;
288 mutex_lock(&EXFAT_SB(sb)->s_lock);
289 ctx->pos = cpos;
290 goto get_new;
291
292end_of_dir:
293 if (!cpos && fake_offset)
294 cpos = ITER_POS_FILLED_DOTS;
295 ctx->pos = cpos;
296unlock:
297 mutex_unlock(&EXFAT_SB(sb)->s_lock);
298out_unlocked:
299 /*
300 * To improve performance, free namebuf after unlock sb_lock.
301 * If namebuf is not allocated, this function do nothing
302 */
303 exfat_free_namebuf(nb);
304 return err;
305}
306
307const struct file_operations exfat_dir_operations = {
308 .llseek = generic_file_llseek,
309 .read = generic_read_dir,
310 .iterate = exfat_iterate,
Sungjong Seo52674562020-06-18 20:43:26 +0900311 .fsync = exfat_file_fsync,
Namjae Jeonca061972020-03-02 15:21:35 +0900312};
313
314int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
315{
316 int ret;
317
318 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
319
320 ret = exfat_alloc_cluster(inode, 1, clu);
321 if (ret)
322 return ret;
323
324 return exfat_zeroed_cluster(inode, clu->dir);
325}
326
327int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
328{
329 int len;
330
331 len = p_uniname->name_len;
332 if (len == 0)
333 return -EINVAL;
334
335 /* 1 file entry + 1 stream entry + name entries */
336 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
337}
338
339unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
340{
341 if (ep->type == EXFAT_UNUSED)
342 return TYPE_UNUSED;
343 if (IS_EXFAT_DELETED(ep->type))
344 return TYPE_DELETED;
345 if (ep->type == EXFAT_INVAL)
346 return TYPE_INVALID;
347 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
348 if (ep->type == EXFAT_BITMAP)
349 return TYPE_BITMAP;
350 if (ep->type == EXFAT_UPCASE)
351 return TYPE_UPCASE;
352 if (ep->type == EXFAT_VOLUME)
353 return TYPE_VOLUME;
354 if (ep->type == EXFAT_FILE) {
355 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
356 return TYPE_DIR;
357 return TYPE_FILE;
358 }
359 return TYPE_CRITICAL_PRI;
360 }
361 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
362 if (ep->type == EXFAT_GUID)
363 return TYPE_GUID;
364 if (ep->type == EXFAT_PADDING)
365 return TYPE_PADDING;
366 if (ep->type == EXFAT_ACLTAB)
367 return TYPE_ACLTAB;
368 return TYPE_BENIGN_PRI;
369 }
370 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
371 if (ep->type == EXFAT_STREAM)
372 return TYPE_STREAM;
373 if (ep->type == EXFAT_NAME)
374 return TYPE_EXTEND;
375 if (ep->type == EXFAT_ACL)
376 return TYPE_ACL;
377 return TYPE_CRITICAL_SEC;
378 }
379 return TYPE_BENIGN_SEC;
380}
381
382static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
383{
384 if (type == TYPE_UNUSED) {
385 ep->type = EXFAT_UNUSED;
386 } else if (type == TYPE_DELETED) {
387 ep->type &= EXFAT_DELETE;
388 } else if (type == TYPE_STREAM) {
389 ep->type = EXFAT_STREAM;
390 } else if (type == TYPE_EXTEND) {
391 ep->type = EXFAT_NAME;
392 } else if (type == TYPE_BITMAP) {
393 ep->type = EXFAT_BITMAP;
394 } else if (type == TYPE_UPCASE) {
395 ep->type = EXFAT_UPCASE;
396 } else if (type == TYPE_VOLUME) {
397 ep->type = EXFAT_VOLUME;
398 } else if (type == TYPE_DIR) {
399 ep->type = EXFAT_FILE;
400 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
401 } else if (type == TYPE_FILE) {
402 ep->type = EXFAT_FILE;
403 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
404 }
405}
406
407static void exfat_init_stream_entry(struct exfat_dentry *ep,
408 unsigned char flags, unsigned int start_clu,
409 unsigned long long size)
410{
411 exfat_set_entry_type(ep, TYPE_STREAM);
412 ep->dentry.stream.flags = flags;
413 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
414 ep->dentry.stream.valid_size = cpu_to_le64(size);
415 ep->dentry.stream.size = cpu_to_le64(size);
416}
417
418static void exfat_init_name_entry(struct exfat_dentry *ep,
419 unsigned short *uniname)
420{
421 int i;
422
423 exfat_set_entry_type(ep, TYPE_EXTEND);
424 ep->dentry.name.flags = 0x0;
425
426 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
Hyeongseok.Kim4ba6ccd2020-06-09 14:30:44 +0900427 if (*uniname != 0x0) {
428 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
429 uniname++;
430 } else {
431 ep->dentry.name.unicode_0_14[i] = 0x0;
432 }
Namjae Jeonca061972020-03-02 15:21:35 +0900433 }
434}
435
436int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
437 int entry, unsigned int type, unsigned int start_clu,
438 unsigned long long size)
439{
440 struct super_block *sb = inode->i_sb;
441 struct exfat_sb_info *sbi = EXFAT_SB(sb);
442 struct timespec64 ts = current_time(inode);
443 sector_t sector;
444 struct exfat_dentry *ep;
445 struct buffer_head *bh;
446
447 /*
448 * We cannot use exfat_get_dentry_set here because file ep is not
449 * initialized yet.
450 */
451 ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector);
452 if (!ep)
453 return -EIO;
454
455 exfat_set_entry_type(ep, type);
456 exfat_set_entry_time(sbi, &ts,
457 &ep->dentry.file.create_tz,
458 &ep->dentry.file.create_time,
459 &ep->dentry.file.create_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900460 &ep->dentry.file.create_time_cs);
Namjae Jeonca061972020-03-02 15:21:35 +0900461 exfat_set_entry_time(sbi, &ts,
462 &ep->dentry.file.modify_tz,
463 &ep->dentry.file.modify_time,
464 &ep->dentry.file.modify_date,
Tetsuhiro Kohadaed0f84d2020-04-22 08:30:56 +0900465 &ep->dentry.file.modify_time_cs);
Namjae Jeonca061972020-03-02 15:21:35 +0900466 exfat_set_entry_time(sbi, &ts,
467 &ep->dentry.file.access_tz,
468 &ep->dentry.file.access_time,
469 &ep->dentry.file.access_date,
470 NULL);
471
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900472 exfat_update_bh(bh, IS_DIRSYNC(inode));
Namjae Jeonca061972020-03-02 15:21:35 +0900473 brelse(bh);
474
475 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector);
476 if (!ep)
477 return -EIO;
478
479 exfat_init_stream_entry(ep,
480 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
481 start_clu, size);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900482 exfat_update_bh(bh, IS_DIRSYNC(inode));
Namjae Jeonca061972020-03-02 15:21:35 +0900483 brelse(bh);
484
485 return 0;
486}
487
488int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
489 int entry)
490{
491 struct super_block *sb = inode->i_sb;
492 int ret = 0;
493 int i, num_entries;
494 sector_t sector;
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +0900495 u16 chksum;
Namjae Jeonca061972020-03-02 15:21:35 +0900496 struct exfat_dentry *ep, *fep;
497 struct buffer_head *fbh, *bh;
498
499 fep = exfat_get_dentry(sb, p_dir, entry, &fbh, &sector);
500 if (!fep)
501 return -EIO;
502
503 num_entries = fep->dentry.file.num_ext + 1;
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +0900504 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
Namjae Jeonca061972020-03-02 15:21:35 +0900505
506 for (i = 1; i < num_entries; i++) {
507 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
508 if (!ep) {
509 ret = -EIO;
510 goto release_fbh;
511 }
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +0900512 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
Namjae Jeonca061972020-03-02 15:21:35 +0900513 CS_DEFAULT);
514 brelse(bh);
515 }
516
517 fep->dentry.file.checksum = cpu_to_le16(chksum);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900518 exfat_update_bh(fbh, IS_DIRSYNC(inode));
Namjae Jeonca061972020-03-02 15:21:35 +0900519release_fbh:
520 brelse(fbh);
521 return ret;
522}
523
524int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
525 int entry, int num_entries, struct exfat_uni_name *p_uniname)
526{
527 struct super_block *sb = inode->i_sb;
528 int i;
529 sector_t sector;
530 unsigned short *uniname = p_uniname->name;
531 struct exfat_dentry *ep;
532 struct buffer_head *bh;
533 int sync = IS_DIRSYNC(inode);
534
535 ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector);
536 if (!ep)
537 return -EIO;
538
539 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900540 exfat_update_bh(bh, sync);
Namjae Jeonca061972020-03-02 15:21:35 +0900541 brelse(bh);
542
543 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector);
544 if (!ep)
545 return -EIO;
546
547 ep->dentry.stream.name_len = p_uniname->name_len;
548 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900549 exfat_update_bh(bh, sync);
Namjae Jeonca061972020-03-02 15:21:35 +0900550 brelse(bh);
551
552 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
553 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector);
554 if (!ep)
555 return -EIO;
556
557 exfat_init_name_entry(ep, uniname);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900558 exfat_update_bh(bh, sync);
Namjae Jeonca061972020-03-02 15:21:35 +0900559 brelse(bh);
560 uniname += EXFAT_FILE_NAME_LEN;
561 }
562
563 exfat_update_dir_chksum(inode, p_dir, entry);
564 return 0;
565}
566
567int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
568 int entry, int order, int num_entries)
569{
570 struct super_block *sb = inode->i_sb;
571 int i;
572 sector_t sector;
573 struct exfat_dentry *ep;
574 struct buffer_head *bh;
575
576 for (i = order; i < num_entries; i++) {
577 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector);
578 if (!ep)
579 return -EIO;
580
581 exfat_set_entry_type(ep, TYPE_DELETED);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +0900582 exfat_update_bh(bh, IS_DIRSYNC(inode));
Namjae Jeonca061972020-03-02 15:21:35 +0900583 brelse(bh);
584 }
585
586 return 0;
587}
588
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900589void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
Namjae Jeonca061972020-03-02 15:21:35 +0900590{
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900591 int chksum_type = CS_DIR_ENTRY, i;
Namjae Jeonca061972020-03-02 15:21:35 +0900592 unsigned short chksum = 0;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900593 struct exfat_dentry *ep;
Namjae Jeonca061972020-03-02 15:21:35 +0900594
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900595 for (i = 0; i < es->num_entries; i++) {
596 ep = exfat_get_dentry_cached(es, i);
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +0900597 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
598 chksum_type);
Namjae Jeonca061972020-03-02 15:21:35 +0900599 chksum_type = CS_DEFAULT;
600 }
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900601 ep = exfat_get_dentry_cached(es, 0);
602 ep->dentry.file.checksum = cpu_to_le16(chksum);
603 es->modified = true;
604}
Namjae Jeonca061972020-03-02 15:21:35 +0900605
Tetsuhiro Kohada8b0c4712020-06-24 09:54:54 +0900606int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900607{
Tetsuhiro Kohada3db3c3f2020-06-23 15:22:19 +0900608 int i, err = 0;
Namjae Jeonca061972020-03-02 15:21:35 +0900609
Tetsuhiro Kohada3db3c3f2020-06-23 15:22:19 +0900610 if (es->modified)
611 err = exfat_update_bhs(es->bh, es->num_bh, sync);
612
613 for (i = 0; i < es->num_bh; i++)
614 if (err)
615 bforget(es->bh[i]);
616 else
617 brelse(es->bh[i]);
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900618 kfree(es);
Tetsuhiro Kohada8b0c4712020-06-24 09:54:54 +0900619 return err;
Namjae Jeonca061972020-03-02 15:21:35 +0900620}
621
622static int exfat_walk_fat_chain(struct super_block *sb,
623 struct exfat_chain *p_dir, unsigned int byte_offset,
624 unsigned int *clu)
625{
626 struct exfat_sb_info *sbi = EXFAT_SB(sb);
627 unsigned int clu_offset;
628 unsigned int cur_clu;
629
630 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
631 cur_clu = p_dir->dir;
632
633 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
634 cur_clu += clu_offset;
635 } else {
636 while (clu_offset > 0) {
637 if (exfat_get_next_cluster(sb, &cur_clu))
638 return -EIO;
639 if (cur_clu == EXFAT_EOF_CLUSTER) {
640 exfat_fs_error(sb,
641 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
642 p_dir->dir,
643 EXFAT_B_TO_DEN(byte_offset));
644 return -EIO;
645 }
646 clu_offset--;
647 }
648 }
649
650 *clu = cur_clu;
651 return 0;
652}
653
654int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
655 int entry, sector_t *sector, int *offset)
656{
657 int ret;
658 unsigned int off, clu = 0;
659 struct exfat_sb_info *sbi = EXFAT_SB(sb);
660
661 off = EXFAT_DEN_TO_B(entry);
662
663 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
664 if (ret)
665 return ret;
666
667 /* byte offset in cluster */
668 off = EXFAT_CLU_OFFSET(off, sbi);
669
670 /* byte offset in sector */
671 *offset = EXFAT_BLK_OFFSET(off, sb);
672
673 /* sector offset in cluster */
674 *sector = EXFAT_B_TO_BLK(off, sb);
675 *sector += exfat_cluster_to_sector(sbi, clu);
676 return 0;
677}
678
679#define EXFAT_MAX_RA_SIZE (128*1024)
680static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
681{
682 struct exfat_sb_info *sbi = EXFAT_SB(sb);
683 struct buffer_head *bh;
684 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
685 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
686 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
687 unsigned int ra_count = min(adj_ra_count, max_ra_count);
688
689 /* Read-ahead is not required */
690 if (sbi->sect_per_clus == 1)
691 return 0;
692
693 if (sec < sbi->data_start_sector) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900694 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
695 (unsigned long long)sec, sbi->data_start_sector);
Namjae Jeonca061972020-03-02 15:21:35 +0900696 return -EIO;
697 }
698
699 /* Not sector aligned with ra_count, resize ra_count to page size */
700 if ((sec - sbi->data_start_sector) & (ra_count - 1))
701 ra_count = page_ra_count;
702
703 bh = sb_find_get_block(sb, sec);
704 if (!bh || !buffer_uptodate(bh)) {
705 unsigned int i;
706
707 for (i = 0; i < ra_count; i++)
708 sb_breadahead(sb, (sector_t)(sec + i));
709 }
710 brelse(bh);
711 return 0;
712}
713
714struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
715 struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
716 sector_t *sector)
717{
718 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
719 int off;
720 sector_t sec;
721
722 if (p_dir->dir == DIR_DELETED) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900723 exfat_err(sb, "abnormal access to deleted dentry");
Namjae Jeonca061972020-03-02 15:21:35 +0900724 return NULL;
725 }
726
727 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
728 return NULL;
729
730 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
731 !(entry & (dentries_per_page - 1)))
732 exfat_dir_readahead(sb, sec);
733
734 *bh = sb_bread(sb, sec);
735 if (!*bh)
736 return NULL;
737
738 if (sector)
739 *sector = sec;
740 return (struct exfat_dentry *)((*bh)->b_data + off);
741}
742
743enum exfat_validate_dentry_mode {
744 ES_MODE_STARTED,
745 ES_MODE_GET_FILE_ENTRY,
746 ES_MODE_GET_STRM_ENTRY,
747 ES_MODE_GET_NAME_ENTRY,
748 ES_MODE_GET_CRITICAL_SEC_ENTRY,
749};
750
751static bool exfat_validate_entry(unsigned int type,
752 enum exfat_validate_dentry_mode *mode)
753{
754 if (type == TYPE_UNUSED || type == TYPE_DELETED)
755 return false;
756
757 switch (*mode) {
758 case ES_MODE_STARTED:
759 if (type != TYPE_FILE && type != TYPE_DIR)
760 return false;
761 *mode = ES_MODE_GET_FILE_ENTRY;
762 return true;
763 case ES_MODE_GET_FILE_ENTRY:
764 if (type != TYPE_STREAM)
765 return false;
766 *mode = ES_MODE_GET_STRM_ENTRY;
767 return true;
768 case ES_MODE_GET_STRM_ENTRY:
769 if (type != TYPE_EXTEND)
770 return false;
771 *mode = ES_MODE_GET_NAME_ENTRY;
772 return true;
773 case ES_MODE_GET_NAME_ENTRY:
774 if (type == TYPE_STREAM)
775 return false;
776 if (type != TYPE_EXTEND) {
777 if (!(type & TYPE_CRITICAL_SEC))
778 return false;
779 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
780 }
781 return true;
782 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
783 if (type == TYPE_EXTEND || type == TYPE_STREAM)
784 return false;
785 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
786 return false;
787 return true;
788 default:
789 WARN_ON_ONCE(1);
790 return false;
791 }
792}
793
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900794struct exfat_dentry *exfat_get_dentry_cached(
795 struct exfat_entry_set_cache *es, int num)
796{
797 int off = es->start_off + num * DENTRY_SIZE;
798 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
799 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
800
801 return (struct exfat_dentry *)p;
802}
803
Namjae Jeonca061972020-03-02 15:21:35 +0900804/*
805 * Returns a set of dentries for a file or dir.
806 *
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900807 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
808 * User should call exfat_get_dentry_set() after setting 'modified' to apply
809 * changes made in this entry set to the real device.
Namjae Jeonca061972020-03-02 15:21:35 +0900810 *
811 * in:
812 * sb+p_dir+entry: indicates a file/dir
813 * type: specifies how many dentries should be included.
Namjae Jeonca061972020-03-02 15:21:35 +0900814 * return:
815 * pointer of entry set on success,
816 * NULL on failure.
817 */
818struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900819 struct exfat_chain *p_dir, int entry, unsigned int type)
Namjae Jeonca061972020-03-02 15:21:35 +0900820{
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900821 int ret, i, num_bh;
Namjae Jeonca061972020-03-02 15:21:35 +0900822 unsigned int off, byte_offset, clu = 0;
Namjae Jeonca061972020-03-02 15:21:35 +0900823 sector_t sec;
824 struct exfat_sb_info *sbi = EXFAT_SB(sb);
825 struct exfat_entry_set_cache *es;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900826 struct exfat_dentry *ep;
827 int num_entries;
Namjae Jeonca061972020-03-02 15:21:35 +0900828 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
829 struct buffer_head *bh;
830
831 if (p_dir->dir == DIR_DELETED) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900832 exfat_err(sb, "access to deleted dentry");
Namjae Jeonca061972020-03-02 15:21:35 +0900833 return NULL;
834 }
835
836 byte_offset = EXFAT_DEN_TO_B(entry);
837 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
838 if (ret)
839 return NULL;
840
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900841 es = kzalloc(sizeof(*es), GFP_KERNEL);
842 if (!es)
843 return NULL;
844 es->sb = sb;
845 es->modified = false;
846
Namjae Jeonca061972020-03-02 15:21:35 +0900847 /* byte offset in cluster */
848 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
849
850 /* byte offset in sector */
851 off = EXFAT_BLK_OFFSET(byte_offset, sb);
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900852 es->start_off = off;
Namjae Jeonca061972020-03-02 15:21:35 +0900853
854 /* sector offset in cluster */
855 sec = EXFAT_B_TO_BLK(byte_offset, sb);
856 sec += exfat_cluster_to_sector(sbi, clu);
857
858 bh = sb_bread(sb, sec);
859 if (!bh)
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900860 goto free_es;
861 es->bh[es->num_bh++] = bh;
Namjae Jeonca061972020-03-02 15:21:35 +0900862
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900863 ep = exfat_get_dentry_cached(es, 0);
864 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
865 goto free_es;
Namjae Jeonca061972020-03-02 15:21:35 +0900866
867 num_entries = type == ES_ALL_ENTRIES ?
868 ep->dentry.file.num_ext + 1 : type;
Namjae Jeonca061972020-03-02 15:21:35 +0900869 es->num_entries = num_entries;
Namjae Jeonca061972020-03-02 15:21:35 +0900870
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900871 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
872 for (i = 1; i < num_bh; i++) {
873 /* get the next sector */
874 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
875 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
876 clu++;
877 else if (exfat_get_next_cluster(sb, &clu))
Namjae Jeonca061972020-03-02 15:21:35 +0900878 goto free_es;
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900879 sec = exfat_cluster_to_sector(sbi, clu);
Namjae Jeonca061972020-03-02 15:21:35 +0900880 } else {
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900881 sec++;
Namjae Jeonca061972020-03-02 15:21:35 +0900882 }
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900883
884 bh = sb_bread(sb, sec);
885 if (!bh)
886 goto free_es;
887 es->bh[es->num_bh++] = bh;
Namjae Jeonca061972020-03-02 15:21:35 +0900888 }
889
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900890 /* validiate cached dentries */
891 for (i = 1; i < num_entries; i++) {
892 ep = exfat_get_dentry_cached(es, i);
893 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
894 goto free_es;
895 }
Namjae Jeonca061972020-03-02 15:21:35 +0900896 return es;
897
898free_es:
Tetsuhiro Kohada943af1f2020-05-20 16:56:41 +0900899 exfat_free_dentry_set(es, false);
Namjae Jeonca061972020-03-02 15:21:35 +0900900 return NULL;
901}
902
903enum {
904 DIRENT_STEP_FILE,
905 DIRENT_STEP_STRM,
906 DIRENT_STEP_NAME,
907 DIRENT_STEP_SECD,
908};
909
910/*
911 * return values:
912 * >= 0 : return dir entiry position with the name in dir
Namjae Jeonca061972020-03-02 15:21:35 +0900913 * -ENOENT : entry with the name does not exist
914 * -EIO : I/O error
915 */
916int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
917 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
918 int num_entries, unsigned int type)
919{
920 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
921 int order, step, name_len = 0;
922 int dentries_per_clu, num_empty = 0;
923 unsigned int entry_type;
924 unsigned short *uniname = NULL;
925 struct exfat_chain clu;
926 struct exfat_hint *hint_stat = &ei->hint_stat;
927 struct exfat_hint_femp candi_empty;
928 struct exfat_sb_info *sbi = EXFAT_SB(sb);
929
930 dentries_per_clu = sbi->dentries_per_clu;
931
932 exfat_chain_dup(&clu, p_dir);
933
934 if (hint_stat->eidx) {
935 clu.dir = hint_stat->clu;
936 dentry = hint_stat->eidx;
937 end_eidx = dentry;
938 }
939
940 candi_empty.eidx = EXFAT_HINT_NONE;
941rewind:
942 order = 0;
943 step = DIRENT_STEP_FILE;
944 while (clu.dir != EXFAT_EOF_CLUSTER) {
945 i = dentry & (dentries_per_clu - 1);
946 for (; i < dentries_per_clu; i++, dentry++) {
947 struct exfat_dentry *ep;
948 struct buffer_head *bh;
949
950 if (rewind && dentry == end_eidx)
951 goto not_found;
952
953 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
954 if (!ep)
955 return -EIO;
956
957 entry_type = exfat_get_entry_type(ep);
958
959 if (entry_type == TYPE_UNUSED ||
960 entry_type == TYPE_DELETED) {
961 step = DIRENT_STEP_FILE;
962
963 num_empty++;
964 if (candi_empty.eidx == EXFAT_HINT_NONE &&
965 num_empty == 1) {
966 exfat_chain_set(&candi_empty.cur,
967 clu.dir, clu.size, clu.flags);
968 }
969
970 if (candi_empty.eidx == EXFAT_HINT_NONE &&
971 num_empty >= num_entries) {
972 candi_empty.eidx =
973 dentry - (num_empty - 1);
974 WARN_ON(candi_empty.eidx < 0);
975 candi_empty.count = num_empty;
976
977 if (ei->hint_femp.eidx ==
978 EXFAT_HINT_NONE ||
979 candi_empty.eidx <=
Tetsuhiro Kohadaa7a24162020-09-11 13:45:19 +0900980 ei->hint_femp.eidx)
981 ei->hint_femp = candi_empty;
Namjae Jeonca061972020-03-02 15:21:35 +0900982 }
983
984 brelse(bh);
985 if (entry_type == TYPE_UNUSED)
986 goto not_found;
987 continue;
988 }
989
990 num_empty = 0;
991 candi_empty.eidx = EXFAT_HINT_NONE;
992
993 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
994 step = DIRENT_STEP_FILE;
995 if (type == TYPE_ALL || type == entry_type) {
996 num_ext = ep->dentry.file.num_ext;
997 step = DIRENT_STEP_STRM;
998 }
999 brelse(bh);
1000 continue;
1001 }
1002
1003 if (entry_type == TYPE_STREAM) {
Tetsuhiro Kohada5875bf22020-05-29 19:14:59 +09001004 u16 name_hash;
Namjae Jeonca061972020-03-02 15:21:35 +09001005
1006 if (step != DIRENT_STEP_STRM) {
1007 step = DIRENT_STEP_FILE;
1008 brelse(bh);
1009 continue;
1010 }
1011 step = DIRENT_STEP_FILE;
1012 name_hash = le16_to_cpu(
1013 ep->dentry.stream.name_hash);
1014 if (p_uniname->name_hash == name_hash &&
1015 p_uniname->name_len ==
1016 ep->dentry.stream.name_len) {
1017 step = DIRENT_STEP_NAME;
1018 order = 1;
1019 name_len = 0;
1020 }
1021 brelse(bh);
1022 continue;
1023 }
1024
1025 brelse(bh);
1026 if (entry_type == TYPE_EXTEND) {
1027 unsigned short entry_uniname[16], unichar;
1028
1029 if (step != DIRENT_STEP_NAME) {
1030 step = DIRENT_STEP_FILE;
1031 continue;
1032 }
1033
1034 if (++order == 2)
1035 uniname = p_uniname->name;
1036 else
1037 uniname += EXFAT_FILE_NAME_LEN;
1038
1039 len = exfat_extract_uni_name(ep, entry_uniname);
1040 name_len += len;
1041
1042 unichar = *(uniname+len);
1043 *(uniname+len) = 0x0;
1044
1045 if (exfat_uniname_ncmp(sb, uniname,
1046 entry_uniname, len)) {
1047 step = DIRENT_STEP_FILE;
1048 } else if (p_uniname->name_len == name_len) {
1049 if (order == num_ext)
1050 goto found;
1051 step = DIRENT_STEP_SECD;
1052 }
1053
1054 *(uniname+len) = unichar;
1055 continue;
1056 }
1057
1058 if (entry_type &
1059 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1060 if (step == DIRENT_STEP_SECD) {
1061 if (++order == num_ext)
1062 goto found;
1063 continue;
1064 }
1065 }
1066 step = DIRENT_STEP_FILE;
1067 }
1068
1069 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1070 if (--clu.size > 0)
1071 clu.dir++;
1072 else
1073 clu.dir = EXFAT_EOF_CLUSTER;
1074 } else {
1075 if (exfat_get_next_cluster(sb, &clu.dir))
1076 return -EIO;
1077 }
1078 }
1079
1080not_found:
1081 /*
1082 * We started at not 0 index,so we should try to find target
1083 * from 0 index to the index we started at.
1084 */
1085 if (!rewind && end_eidx) {
1086 rewind = 1;
1087 dentry = 0;
1088 clu.dir = p_dir->dir;
1089 /* reset empty hint */
1090 num_empty = 0;
1091 candi_empty.eidx = EXFAT_HINT_NONE;
1092 goto rewind;
1093 }
1094
1095 /* initialized hint_stat */
1096 hint_stat->clu = p_dir->dir;
1097 hint_stat->eidx = 0;
1098 return -ENOENT;
1099
1100found:
1101 /* next dentry we'll find is out of this cluster */
1102 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1103 int ret = 0;
1104
1105 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1106 if (--clu.size > 0)
1107 clu.dir++;
1108 else
1109 clu.dir = EXFAT_EOF_CLUSTER;
1110 } else {
1111 ret = exfat_get_next_cluster(sb, &clu.dir);
1112 }
1113
Namjae Jeond2fa0c32020-07-03 11:19:46 +09001114 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
Namjae Jeonca061972020-03-02 15:21:35 +09001115 /* just initialized hint_stat */
1116 hint_stat->clu = p_dir->dir;
1117 hint_stat->eidx = 0;
1118 return (dentry - num_ext);
1119 }
1120 }
1121
1122 hint_stat->clu = clu.dir;
1123 hint_stat->eidx = dentry + 1;
1124 return dentry - num_ext;
1125}
1126
1127int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1128 int entry, struct exfat_dentry *ep)
1129{
1130 int i, count = 0;
1131 unsigned int type;
1132 struct exfat_dentry *ext_ep;
1133 struct buffer_head *bh;
1134
1135 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1136 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1137 if (!ext_ep)
1138 return -EIO;
1139
1140 type = exfat_get_entry_type(ext_ep);
1141 brelse(bh);
1142 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1143 count++;
1144 else
1145 break;
1146 }
1147 return count;
1148}
1149
1150int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1151{
1152 int i, count = 0;
1153 int dentries_per_clu;
1154 unsigned int entry_type;
1155 struct exfat_chain clu;
1156 struct exfat_dentry *ep;
1157 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1158 struct buffer_head *bh;
1159
1160 dentries_per_clu = sbi->dentries_per_clu;
1161
1162 exfat_chain_dup(&clu, p_dir);
1163
1164 while (clu.dir != EXFAT_EOF_CLUSTER) {
1165 for (i = 0; i < dentries_per_clu; i++) {
1166 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1167 if (!ep)
1168 return -EIO;
1169 entry_type = exfat_get_entry_type(ep);
1170 brelse(bh);
1171
1172 if (entry_type == TYPE_UNUSED)
1173 return count;
1174 if (entry_type != TYPE_DIR)
1175 continue;
1176 count++;
1177 }
1178
1179 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1180 if (--clu.size > 0)
1181 clu.dir++;
1182 else
1183 clu.dir = EXFAT_EOF_CLUSTER;
1184 } else {
1185 if (exfat_get_next_cluster(sb, &(clu.dir)))
1186 return -EIO;
1187 }
1188 }
1189
1190 return count;
1191}