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