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