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