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