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
exfat_extract_uni_name(struct exfat_dentry * ep,unsigned short * uniname)13 static 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
exfat_get_uniname_from_ext_entry(struct super_block * sb,struct exfat_chain * p_dir,int entry,unsigned short * uniname)31 static 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_entry_set_cache *es;
36 unsigned int uni_len = 0, len;
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 len = exfat_extract_uni_name(ep, uniname);
56 uni_len += len;
57 if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
58 break;
59 uniname += EXFAT_FILE_NAME_LEN;
60 }
61
62 exfat_free_dentry_set(es, false);
63 }
64
65 /* read a directory entry from the opened directory */
exfat_readdir(struct inode * inode,loff_t * cpos,struct exfat_dir_entry * dir_entry)66 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
67 {
68 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
69 unsigned int type, clu_offset, max_dentries;
70 sector_t sector;
71 struct exfat_chain dir, clu;
72 struct exfat_uni_name uni_name;
73 struct exfat_dentry *ep;
74 struct super_block *sb = inode->i_sb;
75 struct exfat_sb_info *sbi = EXFAT_SB(sb);
76 struct exfat_inode_info *ei = EXFAT_I(inode);
77 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
78 struct buffer_head *bh;
79
80 /* check if the given file ID is opened */
81 if (ei->type != TYPE_DIR)
82 return -EPERM;
83
84 if (ei->entry == -1)
85 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
86 else
87 exfat_chain_set(&dir, ei->start_clu,
88 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
89
90 dentries_per_clu = sbi->dentries_per_clu;
91 dentries_per_clu_bits = ilog2(dentries_per_clu);
92 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
93 (u64)sbi->num_clusters << dentries_per_clu_bits);
94
95 clu_offset = dentry >> dentries_per_clu_bits;
96 exfat_chain_dup(&clu, &dir);
97
98 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
99 clu.dir += clu_offset;
100 clu.size -= clu_offset;
101 } else {
102 /* hint_information */
103 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
104 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
105 clu_offset -= ei->hint_bmap.off;
106 clu.dir = ei->hint_bmap.clu;
107 }
108
109 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
110 if (exfat_get_next_cluster(sb, &(clu.dir)))
111 return -EIO;
112
113 clu_offset--;
114 }
115 }
116
117 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
118 i = dentry & (dentries_per_clu - 1);
119
120 for ( ; i < dentries_per_clu; i++, dentry++) {
121 ep = exfat_get_dentry(sb, &clu, i, &bh, §or);
122 if (!ep)
123 return -EIO;
124
125 type = exfat_get_entry_type(ep);
126 if (type == TYPE_UNUSED) {
127 brelse(bh);
128 goto out;
129 }
130
131 if (type != TYPE_FILE && type != TYPE_DIR) {
132 brelse(bh);
133 continue;
134 }
135
136 num_ext = ep->dentry.file.num_ext;
137 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
138 exfat_get_entry_time(sbi, &dir_entry->crtime,
139 ep->dentry.file.create_tz,
140 ep->dentry.file.create_time,
141 ep->dentry.file.create_date,
142 ep->dentry.file.create_time_cs);
143 exfat_get_entry_time(sbi, &dir_entry->mtime,
144 ep->dentry.file.modify_tz,
145 ep->dentry.file.modify_time,
146 ep->dentry.file.modify_date,
147 ep->dentry.file.modify_time_cs);
148 exfat_get_entry_time(sbi, &dir_entry->atime,
149 ep->dentry.file.access_tz,
150 ep->dentry.file.access_time,
151 ep->dentry.file.access_date,
152 0);
153
154 *uni_name.name = 0x0;
155 exfat_get_uniname_from_ext_entry(sb, &clu, i,
156 uni_name.name);
157 exfat_utf16_to_nls(sb, &uni_name,
158 dir_entry->namebuf.lfn,
159 dir_entry->namebuf.lfnbuf_len);
160 brelse(bh);
161
162 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL);
163 if (!ep)
164 return -EIO;
165 dir_entry->size =
166 le64_to_cpu(ep->dentry.stream.valid_size);
167 dir_entry->entry = dentry;
168 brelse(bh);
169
170 ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
171 ei->hint_bmap.clu = clu.dir;
172
173 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
174 return 0;
175 }
176
177 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
178 if (--clu.size > 0)
179 clu.dir++;
180 else
181 clu.dir = EXFAT_EOF_CLUSTER;
182 } else {
183 if (exfat_get_next_cluster(sb, &(clu.dir)))
184 return -EIO;
185 }
186 }
187
188 out:
189 dir_entry->namebuf.lfn[0] = '\0';
190 *cpos = EXFAT_DEN_TO_B(dentry);
191 return 0;
192 }
193
exfat_init_namebuf(struct exfat_dentry_namebuf * nb)194 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
195 {
196 nb->lfn = NULL;
197 nb->lfnbuf_len = 0;
198 }
199
exfat_alloc_namebuf(struct exfat_dentry_namebuf * nb)200 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
201 {
202 nb->lfn = __getname();
203 if (!nb->lfn)
204 return -ENOMEM;
205 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
206 return 0;
207 }
208
exfat_free_namebuf(struct exfat_dentry_namebuf * nb)209 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
210 {
211 if (!nb->lfn)
212 return;
213
214 __putname(nb->lfn);
215 exfat_init_namebuf(nb);
216 }
217
218 /*
219 * Before calling dir_emit*(), sbi->s_lock should be released
220 * because page fault can occur in dir_emit*().
221 */
222 #define ITER_POS_FILLED_DOTS (2)
exfat_iterate(struct file * filp,struct dir_context * ctx)223 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
224 {
225 struct inode *inode = filp->f_path.dentry->d_inode;
226 struct super_block *sb = inode->i_sb;
227 struct inode *tmp;
228 struct exfat_dir_entry de;
229 struct exfat_dentry_namebuf *nb = &(de.namebuf);
230 struct exfat_inode_info *ei = EXFAT_I(inode);
231 unsigned long inum;
232 loff_t cpos, i_pos;
233 int err = 0, fake_offset = 0;
234
235 exfat_init_namebuf(nb);
236
237 cpos = ctx->pos;
238 if (!dir_emit_dots(filp, ctx))
239 goto out;
240
241 if (ctx->pos == ITER_POS_FILLED_DOTS) {
242 cpos = 0;
243 fake_offset = 1;
244 }
245
246 cpos = round_up(cpos, DENTRY_SIZE);
247
248 /* name buffer should be allocated before use */
249 err = exfat_alloc_namebuf(nb);
250 if (err)
251 goto out;
252 get_new:
253 mutex_lock(&EXFAT_SB(sb)->s_lock);
254
255 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
256 goto end_of_dir;
257
258 err = exfat_readdir(inode, &cpos, &de);
259 if (err) {
260 /*
261 * At least we tried to read a sector.
262 * Move cpos to next sector position (should be aligned).
263 */
264 if (err == -EIO) {
265 cpos += 1 << (sb->s_blocksize_bits);
266 cpos &= ~(sb->s_blocksize - 1);
267 }
268
269 err = -EIO;
270 goto end_of_dir;
271 }
272
273 if (!nb->lfn[0])
274 goto end_of_dir;
275
276 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 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 mutex_unlock(&EXFAT_SB(sb)->s_lock);
286 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
287 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
288 goto out;
289 ctx->pos = cpos;
290 goto get_new;
291
292 end_of_dir:
293 if (!cpos && fake_offset)
294 cpos = ITER_POS_FILLED_DOTS;
295 ctx->pos = cpos;
296 mutex_unlock(&EXFAT_SB(sb)->s_lock);
297 out:
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
306 const struct file_operations exfat_dir_operations = {
307 .llseek = generic_file_llseek,
308 .read = generic_read_dir,
309 .iterate = exfat_iterate,
310 .fsync = exfat_file_fsync,
311 };
312
exfat_alloc_new_dir(struct inode * inode,struct exfat_chain * clu)313 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
314 {
315 int ret;
316
317 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
318
319 ret = exfat_alloc_cluster(inode, 1, clu);
320 if (ret)
321 return ret;
322
323 return exfat_zeroed_cluster(inode, clu->dir);
324 }
325
exfat_calc_num_entries(struct exfat_uni_name * p_uniname)326 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
327 {
328 int len;
329
330 len = p_uniname->name_len;
331 if (len == 0)
332 return -EINVAL;
333
334 /* 1 file entry + 1 stream entry + name entries */
335 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
336 }
337
exfat_get_entry_type(struct exfat_dentry * ep)338 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
339 {
340 if (ep->type == EXFAT_UNUSED)
341 return TYPE_UNUSED;
342 if (IS_EXFAT_DELETED(ep->type))
343 return TYPE_DELETED;
344 if (ep->type == EXFAT_INVAL)
345 return TYPE_INVALID;
346 if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
347 if (ep->type == EXFAT_BITMAP)
348 return TYPE_BITMAP;
349 if (ep->type == EXFAT_UPCASE)
350 return TYPE_UPCASE;
351 if (ep->type == EXFAT_VOLUME)
352 return TYPE_VOLUME;
353 if (ep->type == EXFAT_FILE) {
354 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
355 return TYPE_DIR;
356 return TYPE_FILE;
357 }
358 return TYPE_CRITICAL_PRI;
359 }
360 if (IS_EXFAT_BENIGN_PRI(ep->type)) {
361 if (ep->type == EXFAT_GUID)
362 return TYPE_GUID;
363 if (ep->type == EXFAT_PADDING)
364 return TYPE_PADDING;
365 if (ep->type == EXFAT_ACLTAB)
366 return TYPE_ACLTAB;
367 return TYPE_BENIGN_PRI;
368 }
369 if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
370 if (ep->type == EXFAT_STREAM)
371 return TYPE_STREAM;
372 if (ep->type == EXFAT_NAME)
373 return TYPE_EXTEND;
374 if (ep->type == EXFAT_ACL)
375 return TYPE_ACL;
376 return TYPE_CRITICAL_SEC;
377 }
378 return TYPE_BENIGN_SEC;
379 }
380
exfat_set_entry_type(struct exfat_dentry * ep,unsigned int type)381 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
382 {
383 if (type == TYPE_UNUSED) {
384 ep->type = EXFAT_UNUSED;
385 } else if (type == TYPE_DELETED) {
386 ep->type &= EXFAT_DELETE;
387 } else if (type == TYPE_STREAM) {
388 ep->type = EXFAT_STREAM;
389 } else if (type == TYPE_EXTEND) {
390 ep->type = EXFAT_NAME;
391 } else if (type == TYPE_BITMAP) {
392 ep->type = EXFAT_BITMAP;
393 } else if (type == TYPE_UPCASE) {
394 ep->type = EXFAT_UPCASE;
395 } else if (type == TYPE_VOLUME) {
396 ep->type = EXFAT_VOLUME;
397 } else if (type == TYPE_DIR) {
398 ep->type = EXFAT_FILE;
399 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
400 } else if (type == TYPE_FILE) {
401 ep->type = EXFAT_FILE;
402 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
403 }
404 }
405
exfat_init_stream_entry(struct exfat_dentry * ep,unsigned char flags,unsigned int start_clu,unsigned long long size)406 static void exfat_init_stream_entry(struct exfat_dentry *ep,
407 unsigned char flags, unsigned int start_clu,
408 unsigned long long size)
409 {
410 exfat_set_entry_type(ep, TYPE_STREAM);
411 ep->dentry.stream.flags = flags;
412 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
413 ep->dentry.stream.valid_size = cpu_to_le64(size);
414 ep->dentry.stream.size = cpu_to_le64(size);
415 }
416
exfat_init_name_entry(struct exfat_dentry * ep,unsigned short * uniname)417 static void exfat_init_name_entry(struct exfat_dentry *ep,
418 unsigned short *uniname)
419 {
420 int i;
421
422 exfat_set_entry_type(ep, TYPE_EXTEND);
423 ep->dentry.name.flags = 0x0;
424
425 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
426 if (*uniname != 0x0) {
427 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
428 uniname++;
429 } else {
430 ep->dentry.name.unicode_0_14[i] = 0x0;
431 }
432 }
433 }
434
exfat_init_dir_entry(struct inode * inode,struct exfat_chain * p_dir,int entry,unsigned int type,unsigned int start_clu,unsigned long long size)435 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
436 int entry, unsigned int type, unsigned int start_clu,
437 unsigned long long size)
438 {
439 struct super_block *sb = inode->i_sb;
440 struct exfat_sb_info *sbi = EXFAT_SB(sb);
441 struct timespec64 ts = current_time(inode);
442 sector_t sector;
443 struct exfat_dentry *ep;
444 struct buffer_head *bh;
445
446 /*
447 * We cannot use exfat_get_dentry_set here because file ep is not
448 * initialized yet.
449 */
450 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
451 if (!ep)
452 return -EIO;
453
454 exfat_set_entry_type(ep, type);
455 exfat_set_entry_time(sbi, &ts,
456 &ep->dentry.file.create_tz,
457 &ep->dentry.file.create_time,
458 &ep->dentry.file.create_date,
459 &ep->dentry.file.create_time_cs);
460 exfat_set_entry_time(sbi, &ts,
461 &ep->dentry.file.modify_tz,
462 &ep->dentry.file.modify_time,
463 &ep->dentry.file.modify_date,
464 &ep->dentry.file.modify_time_cs);
465 exfat_set_entry_time(sbi, &ts,
466 &ep->dentry.file.access_tz,
467 &ep->dentry.file.access_time,
468 &ep->dentry.file.access_date,
469 NULL);
470
471 exfat_update_bh(bh, IS_DIRSYNC(inode));
472 brelse(bh);
473
474 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
475 if (!ep)
476 return -EIO;
477
478 exfat_init_stream_entry(ep,
479 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
480 start_clu, size);
481 exfat_update_bh(bh, IS_DIRSYNC(inode));
482 brelse(bh);
483
484 return 0;
485 }
486
exfat_update_dir_chksum(struct inode * inode,struct exfat_chain * p_dir,int entry)487 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
488 int entry)
489 {
490 struct super_block *sb = inode->i_sb;
491 int ret = 0;
492 int i, num_entries;
493 sector_t sector;
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, §or);
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, NULL);
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
exfat_init_ext_entry(struct inode * inode,struct exfat_chain * p_dir,int entry,int num_entries,struct exfat_uni_name * p_uniname)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 sector_t sector;
529 unsigned short *uniname = p_uniname->name;
530 struct exfat_dentry *ep;
531 struct buffer_head *bh;
532 int sync = IS_DIRSYNC(inode);
533
534 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or);
535 if (!ep)
536 return -EIO;
537
538 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
539 exfat_update_bh(bh, sync);
540 brelse(bh);
541
542 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or);
543 if (!ep)
544 return -EIO;
545
546 ep->dentry.stream.name_len = p_uniname->name_len;
547 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
548 exfat_update_bh(bh, sync);
549 brelse(bh);
550
551 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
552 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
553 if (!ep)
554 return -EIO;
555
556 exfat_init_name_entry(ep, uniname);
557 exfat_update_bh(bh, sync);
558 brelse(bh);
559 uniname += EXFAT_FILE_NAME_LEN;
560 }
561
562 exfat_update_dir_chksum(inode, p_dir, entry);
563 return 0;
564 }
565
exfat_remove_entries(struct inode * inode,struct exfat_chain * p_dir,int entry,int order,int num_entries)566 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
567 int entry, int order, int num_entries)
568 {
569 struct super_block *sb = inode->i_sb;
570 int i;
571 sector_t sector;
572 struct exfat_dentry *ep;
573 struct buffer_head *bh;
574
575 for (i = order; i < num_entries; i++) {
576 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or);
577 if (!ep)
578 return -EIO;
579
580 exfat_set_entry_type(ep, TYPE_DELETED);
581 exfat_update_bh(bh, IS_DIRSYNC(inode));
582 brelse(bh);
583 }
584
585 return 0;
586 }
587
exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache * es)588 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
589 {
590 int chksum_type = CS_DIR_ENTRY, i;
591 unsigned short chksum = 0;
592 struct exfat_dentry *ep;
593
594 for (i = 0; i < es->num_entries; i++) {
595 ep = exfat_get_dentry_cached(es, i);
596 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
597 chksum_type);
598 chksum_type = CS_DEFAULT;
599 }
600 ep = exfat_get_dentry_cached(es, 0);
601 ep->dentry.file.checksum = cpu_to_le16(chksum);
602 es->modified = true;
603 }
604
exfat_free_dentry_set(struct exfat_entry_set_cache * es,int sync)605 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
606 {
607 int i, err = 0;
608
609 if (es->modified)
610 err = exfat_update_bhs(es->bh, es->num_bh, sync);
611
612 for (i = 0; i < es->num_bh; i++)
613 if (err)
614 bforget(es->bh[i]);
615 else
616 brelse(es->bh[i]);
617
618 if (IS_DYNAMIC_ES(es))
619 kfree(es->bh);
620
621 kfree(es);
622 return err;
623 }
624
exfat_walk_fat_chain(struct super_block * sb,struct exfat_chain * p_dir,unsigned int byte_offset,unsigned int * clu)625 static 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
exfat_find_location(struct super_block * sb,struct exfat_chain * p_dir,int entry,sector_t * sector,int * offset)657 int 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)
exfat_dir_readahead(struct super_block * sb,sector_t sec)683 static 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) {
697 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
698 (unsigned long long)sec, sbi->data_start_sector);
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
exfat_get_dentry(struct super_block * sb,struct exfat_chain * p_dir,int entry,struct buffer_head ** bh,sector_t * sector)717 struct 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) {
726 exfat_err(sb, "abnormal access to deleted dentry");
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
746 enum 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
exfat_validate_entry(unsigned int type,enum exfat_validate_dentry_mode * mode)754 static 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
exfat_get_dentry_cached(struct exfat_entry_set_cache * es,int num)797 struct 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
807 /*
808 * Returns a set of dentries for a file or dir.
809 *
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.
813 *
814 * in:
815 * sb+p_dir+entry: indicates a file/dir
816 * type: specifies how many dentries should be included.
817 * return:
818 * pointer of entry set on success,
819 * NULL on failure.
820 */
exfat_get_dentry_set(struct super_block * sb,struct exfat_chain * p_dir,int entry,unsigned int type)821 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
822 struct exfat_chain *p_dir, int entry, unsigned int type)
823 {
824 int ret, i, num_bh;
825 unsigned int off, byte_offset, clu = 0;
826 sector_t sec;
827 struct exfat_sb_info *sbi = EXFAT_SB(sb);
828 struct exfat_entry_set_cache *es;
829 struct exfat_dentry *ep;
830 int num_entries;
831 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
832 struct buffer_head *bh;
833
834 if (p_dir->dir == DIR_DELETED) {
835 exfat_err(sb, "access to deleted dentry");
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
844 es = kzalloc(sizeof(*es), GFP_KERNEL);
845 if (!es)
846 return NULL;
847 es->sb = sb;
848 es->modified = false;
849
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);
855 es->start_off = off;
856 es->bh = es->__bh;
857
858 /* sector offset in cluster */
859 sec = EXFAT_B_TO_BLK(byte_offset, sb);
860 sec += exfat_cluster_to_sector(sbi, clu);
861
862 bh = sb_bread(sb, sec);
863 if (!bh)
864 goto free_es;
865 es->bh[es->num_bh++] = bh;
866
867 ep = exfat_get_dentry_cached(es, 0);
868 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
869 goto free_es;
870
871 num_entries = type == ES_ALL_ENTRIES ?
872 ep->dentry.file.num_ext + 1 : type;
873 es->num_entries = num_entries;
874
875 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
876 if (num_bh > ARRAY_SIZE(es->__bh)) {
877 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_NOFS);
878 if (!es->bh) {
879 brelse(bh);
880 kfree(es);
881 return NULL;
882 }
883 es->bh[0] = bh;
884 }
885
886 for (i = 1; i < num_bh; i++) {
887 /* get the next sector */
888 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
889 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
890 clu++;
891 else if (exfat_get_next_cluster(sb, &clu))
892 goto free_es;
893 sec = exfat_cluster_to_sector(sbi, clu);
894 } else {
895 sec++;
896 }
897
898 bh = sb_bread(sb, sec);
899 if (!bh)
900 goto free_es;
901 es->bh[es->num_bh++] = bh;
902 }
903
904 /* validiate cached dentries */
905 for (i = 1; i < num_entries; i++) {
906 ep = exfat_get_dentry_cached(es, i);
907 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
908 goto free_es;
909 }
910 return es;
911
912 free_es:
913 exfat_free_dentry_set(es, false);
914 return NULL;
915 }
916
917 enum {
918 DIRENT_STEP_FILE,
919 DIRENT_STEP_STRM,
920 DIRENT_STEP_NAME,
921 DIRENT_STEP_SECD,
922 };
923
924 /*
925 * @ei: inode info of parent directory
926 * @p_dir: directory structure of parent directory
927 * @num_entries:entry size of p_uniname
928 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
929 * for traversing cluster chain.
930 * @return:
931 * >= 0: file directory entry position where the name exists
932 * -ENOENT: entry with the name does not exist
933 * -EIO: I/O error
934 */
exfat_find_dir_entry(struct super_block * sb,struct exfat_inode_info * ei,struct exfat_chain * p_dir,struct exfat_uni_name * p_uniname,int num_entries,unsigned int type,struct exfat_hint * hint_opt)935 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
936 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
937 int num_entries, unsigned int type, struct exfat_hint *hint_opt)
938 {
939 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
940 int order, step, name_len = 0;
941 int dentries_per_clu, num_empty = 0;
942 unsigned int entry_type;
943 unsigned short *uniname = NULL;
944 struct exfat_chain clu;
945 struct exfat_hint *hint_stat = &ei->hint_stat;
946 struct exfat_hint_femp candi_empty;
947 struct exfat_sb_info *sbi = EXFAT_SB(sb);
948
949 dentries_per_clu = sbi->dentries_per_clu;
950
951 exfat_chain_dup(&clu, p_dir);
952
953 if (hint_stat->eidx) {
954 clu.dir = hint_stat->clu;
955 dentry = hint_stat->eidx;
956 end_eidx = dentry;
957 }
958
959 candi_empty.eidx = EXFAT_HINT_NONE;
960 rewind:
961 order = 0;
962 step = DIRENT_STEP_FILE;
963 while (clu.dir != EXFAT_EOF_CLUSTER) {
964 i = dentry & (dentries_per_clu - 1);
965 for (; i < dentries_per_clu; i++, dentry++) {
966 struct exfat_dentry *ep;
967 struct buffer_head *bh;
968
969 if (rewind && dentry == end_eidx)
970 goto not_found;
971
972 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
973 if (!ep)
974 return -EIO;
975
976 entry_type = exfat_get_entry_type(ep);
977
978 if (entry_type == TYPE_UNUSED ||
979 entry_type == TYPE_DELETED) {
980 step = DIRENT_STEP_FILE;
981
982 num_empty++;
983 if (candi_empty.eidx == EXFAT_HINT_NONE &&
984 num_empty == 1) {
985 exfat_chain_set(&candi_empty.cur,
986 clu.dir, clu.size, clu.flags);
987 }
988
989 if (candi_empty.eidx == EXFAT_HINT_NONE &&
990 num_empty >= num_entries) {
991 candi_empty.eidx =
992 dentry - (num_empty - 1);
993 WARN_ON(candi_empty.eidx < 0);
994 candi_empty.count = num_empty;
995
996 if (ei->hint_femp.eidx ==
997 EXFAT_HINT_NONE ||
998 candi_empty.eidx <=
999 ei->hint_femp.eidx)
1000 ei->hint_femp = candi_empty;
1001 }
1002
1003 brelse(bh);
1004 if (entry_type == TYPE_UNUSED)
1005 goto not_found;
1006 continue;
1007 }
1008
1009 num_empty = 0;
1010 candi_empty.eidx = EXFAT_HINT_NONE;
1011
1012 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1013 step = DIRENT_STEP_FILE;
1014 hint_opt->clu = clu.dir;
1015 hint_opt->eidx = i;
1016 if (type == TYPE_ALL || type == entry_type) {
1017 num_ext = ep->dentry.file.num_ext;
1018 step = DIRENT_STEP_STRM;
1019 }
1020 brelse(bh);
1021 continue;
1022 }
1023
1024 if (entry_type == TYPE_STREAM) {
1025 u16 name_hash;
1026
1027 if (step != DIRENT_STEP_STRM) {
1028 step = DIRENT_STEP_FILE;
1029 brelse(bh);
1030 continue;
1031 }
1032 step = DIRENT_STEP_FILE;
1033 name_hash = le16_to_cpu(
1034 ep->dentry.stream.name_hash);
1035 if (p_uniname->name_hash == name_hash &&
1036 p_uniname->name_len ==
1037 ep->dentry.stream.name_len) {
1038 step = DIRENT_STEP_NAME;
1039 order = 1;
1040 name_len = 0;
1041 }
1042 brelse(bh);
1043 continue;
1044 }
1045
1046 brelse(bh);
1047 if (entry_type == TYPE_EXTEND) {
1048 unsigned short entry_uniname[16], unichar;
1049
1050 if (step != DIRENT_STEP_NAME ||
1051 name_len >= MAX_NAME_LENGTH) {
1052 step = DIRENT_STEP_FILE;
1053 continue;
1054 }
1055
1056 if (++order == 2)
1057 uniname = p_uniname->name;
1058 else
1059 uniname += EXFAT_FILE_NAME_LEN;
1060
1061 len = exfat_extract_uni_name(ep, entry_uniname);
1062 name_len += len;
1063
1064 unichar = *(uniname+len);
1065 *(uniname+len) = 0x0;
1066
1067 if (exfat_uniname_ncmp(sb, uniname,
1068 entry_uniname, len)) {
1069 step = DIRENT_STEP_FILE;
1070 } else if (p_uniname->name_len == name_len) {
1071 if (order == num_ext)
1072 goto found;
1073 step = DIRENT_STEP_SECD;
1074 }
1075
1076 *(uniname+len) = unichar;
1077 continue;
1078 }
1079
1080 if (entry_type &
1081 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1082 if (step == DIRENT_STEP_SECD) {
1083 if (++order == num_ext)
1084 goto found;
1085 continue;
1086 }
1087 }
1088 step = DIRENT_STEP_FILE;
1089 }
1090
1091 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1092 if (--clu.size > 0)
1093 clu.dir++;
1094 else
1095 clu.dir = EXFAT_EOF_CLUSTER;
1096 } else {
1097 if (exfat_get_next_cluster(sb, &clu.dir))
1098 return -EIO;
1099 }
1100 }
1101
1102 not_found:
1103 /*
1104 * We started at not 0 index,so we should try to find target
1105 * from 0 index to the index we started at.
1106 */
1107 if (!rewind && end_eidx) {
1108 rewind = 1;
1109 dentry = 0;
1110 clu.dir = p_dir->dir;
1111 /* reset empty hint */
1112 num_empty = 0;
1113 candi_empty.eidx = EXFAT_HINT_NONE;
1114 goto rewind;
1115 }
1116
1117 /* initialized hint_stat */
1118 hint_stat->clu = p_dir->dir;
1119 hint_stat->eidx = 0;
1120 return -ENOENT;
1121
1122 found:
1123 /* next dentry we'll find is out of this cluster */
1124 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1125 int ret = 0;
1126
1127 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1128 if (--clu.size > 0)
1129 clu.dir++;
1130 else
1131 clu.dir = EXFAT_EOF_CLUSTER;
1132 } else {
1133 ret = exfat_get_next_cluster(sb, &clu.dir);
1134 }
1135
1136 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1137 /* just initialized hint_stat */
1138 hint_stat->clu = p_dir->dir;
1139 hint_stat->eidx = 0;
1140 return (dentry - num_ext);
1141 }
1142 }
1143
1144 hint_stat->clu = clu.dir;
1145 hint_stat->eidx = dentry + 1;
1146 return dentry - num_ext;
1147 }
1148
exfat_count_ext_entries(struct super_block * sb,struct exfat_chain * p_dir,int entry,struct exfat_dentry * ep)1149 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1150 int entry, struct exfat_dentry *ep)
1151 {
1152 int i, count = 0;
1153 unsigned int type;
1154 struct exfat_dentry *ext_ep;
1155 struct buffer_head *bh;
1156
1157 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1158 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1159 if (!ext_ep)
1160 return -EIO;
1161
1162 type = exfat_get_entry_type(ext_ep);
1163 brelse(bh);
1164 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1165 count++;
1166 else
1167 break;
1168 }
1169 return count;
1170 }
1171
exfat_count_dir_entries(struct super_block * sb,struct exfat_chain * p_dir)1172 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1173 {
1174 int i, count = 0;
1175 int dentries_per_clu;
1176 unsigned int entry_type;
1177 struct exfat_chain clu;
1178 struct exfat_dentry *ep;
1179 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1180 struct buffer_head *bh;
1181
1182 dentries_per_clu = sbi->dentries_per_clu;
1183
1184 exfat_chain_dup(&clu, p_dir);
1185
1186 while (clu.dir != EXFAT_EOF_CLUSTER) {
1187 for (i = 0; i < dentries_per_clu; i++) {
1188 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1189 if (!ep)
1190 return -EIO;
1191 entry_type = exfat_get_entry_type(ep);
1192 brelse(bh);
1193
1194 if (entry_type == TYPE_UNUSED)
1195 return count;
1196 if (entry_type != TYPE_DIR)
1197 continue;
1198 count++;
1199 }
1200
1201 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1202 if (--clu.size > 0)
1203 clu.dir++;
1204 else
1205 clu.dir = EXFAT_EOF_CLUSTER;
1206 } else {
1207 if (exfat_get_next_cluster(sb, &(clu.dir)))
1208 return -EIO;
1209 }
1210 }
1211
1212 return count;
1213 }
1214