exfat: fix inode->i_blocks for non-512 byte sector size device
[linux-block.git] / fs / exfat / dir.c
CommitLineData
ca061973
NJ
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6#include <linux/slab.h>
654762df 7#include <linux/compat.h>
ca061973
NJ
8#include <linux/bio.h>
9#include <linux/buffer_head.h>
10
11#include "exfat_raw.h"
12#include "exfat_fs.h"
13
14static int exfat_extract_uni_name(struct exfat_dentry *ep,
15 unsigned short *uniname)
16{
17 int i, len = 0;
18
19 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
20 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
21 if (*uniname == 0x0)
22 return len;
23 uniname++;
24 len++;
25 }
26
27 *uniname = 0x0;
28 return len;
29
30}
31
32static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
33 struct exfat_chain *p_dir, int entry, unsigned short *uniname)
34{
35 int i;
20914ff6 36 struct exfat_entry_set_cache es;
ca061973 37
20914ff6 38 if (exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES))
ca061973
NJ
39 return;
40
ca061973
NJ
41 /*
42 * First entry : file entry
43 * Second entry : stream-extension entry
44 * Third entry : first file-name entry
45 * So, the index of first file-name dentry should start from 2.
46 */
f3fe3954 47 for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
20914ff6 48 struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
943af1fd 49
ca061973
NJ
50 /* end of name entry */
51 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
943af1fd 52 break;
ca061973
NJ
53
54 exfat_extract_uni_name(ep, uniname);
55 uniname += EXFAT_FILE_NAME_LEN;
56 }
57
3b9681ac 58 exfat_put_dentry_set(&es, false);
ca061973
NJ
59}
60
61/* read a directory entry from the opened directory */
04cee52f 62static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
ca061973 63{
088f1343 64 int i, dentries_per_clu, num_ext;
1e5654de 65 unsigned int type, clu_offset, max_dentries;
ca061973
NJ
66 struct exfat_chain dir, clu;
67 struct exfat_uni_name uni_name;
68 struct exfat_dentry *ep;
69 struct super_block *sb = inode->i_sb;
70 struct exfat_sb_info *sbi = EXFAT_SB(sb);
71 struct exfat_inode_info *ei = EXFAT_I(inode);
04cee52f 72 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
ca061973
NJ
73 struct buffer_head *bh;
74
75 /* check if the given file ID is opened */
76 if (ei->type != TYPE_DIR)
77 return -EPERM;
78
79 if (ei->entry == -1)
80 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
81 else
82 exfat_chain_set(&dir, ei->start_clu,
83 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
84
85 dentries_per_clu = sbi->dentries_per_clu;
1e5654de 86 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
088f1343 87 (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
ca061973 88
088f1343 89 clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
ca061973
NJ
90 exfat_chain_dup(&clu, &dir);
91
92 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
93 clu.dir += clu_offset;
94 clu.size -= clu_offset;
95 } else {
96 /* hint_information */
97 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
98 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
99 clu_offset -= ei->hint_bmap.off;
100 clu.dir = ei->hint_bmap.clu;
101 }
102
706fdcac 103 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
ca061973
NJ
104 if (exfat_get_next_cluster(sb, &(clu.dir)))
105 return -EIO;
106
107 clu_offset--;
108 }
109 }
110
1e5654de 111 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
ca061973
NJ
112 i = dentry & (dentries_per_clu - 1);
113
114 for ( ; i < dentries_per_clu; i++, dentry++) {
c71510b3 115 ep = exfat_get_dentry(sb, &clu, i, &bh);
ca061973
NJ
116 if (!ep)
117 return -EIO;
118
119 type = exfat_get_entry_type(ep);
120 if (type == TYPE_UNUSED) {
121 brelse(bh);
122 break;
123 }
124
125 if (type != TYPE_FILE && type != TYPE_DIR) {
126 brelse(bh);
127 continue;
128 }
129
04cee52f 130 num_ext = ep->dentry.file.num_ext;
ca061973
NJ
131 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
132 exfat_get_entry_time(sbi, &dir_entry->crtime,
133 ep->dentry.file.create_tz,
134 ep->dentry.file.create_time,
135 ep->dentry.file.create_date,
ed0f84d3 136 ep->dentry.file.create_time_cs);
ca061973
NJ
137 exfat_get_entry_time(sbi, &dir_entry->mtime,
138 ep->dentry.file.modify_tz,
139 ep->dentry.file.modify_time,
140 ep->dentry.file.modify_date,
ed0f84d3 141 ep->dentry.file.modify_time_cs);
ca061973
NJ
142 exfat_get_entry_time(sbi, &dir_entry->atime,
143 ep->dentry.file.access_tz,
144 ep->dentry.file.access_time,
145 ep->dentry.file.access_date,
146 0);
147
148 *uni_name.name = 0x0;
c6e2f52e 149 exfat_get_uniname_from_ext_entry(sb, &clu, i,
ca061973
NJ
150 uni_name.name);
151 exfat_utf16_to_nls(sb, &uni_name,
152 dir_entry->namebuf.lfn,
153 dir_entry->namebuf.lfnbuf_len);
154 brelse(bh);
155
c71510b3 156 ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
ca061973
NJ
157 if (!ep)
158 return -EIO;
159 dir_entry->size =
160 le64_to_cpu(ep->dentry.stream.valid_size);
04cee52f 161 dir_entry->entry = dentry;
ca061973
NJ
162 brelse(bh);
163
088f1343 164 ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
ca061973
NJ
165 ei->hint_bmap.clu = clu.dir;
166
04cee52f 167 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
ca061973
NJ
168 return 0;
169 }
170
171 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
172 if (--clu.size > 0)
173 clu.dir++;
174 else
175 clu.dir = EXFAT_EOF_CLUSTER;
176 } else {
177 if (exfat_get_next_cluster(sb, &(clu.dir)))
178 return -EIO;
179 }
180 }
181
182 dir_entry->namebuf.lfn[0] = '\0';
04cee52f 183 *cpos = EXFAT_DEN_TO_B(dentry);
ca061973
NJ
184 return 0;
185}
186
187static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
188{
189 nb->lfn = NULL;
190 nb->lfnbuf_len = 0;
191}
192
193static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
194{
195 nb->lfn = __getname();
196 if (!nb->lfn)
197 return -ENOMEM;
198 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
199 return 0;
200}
201
202static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
203{
204 if (!nb->lfn)
205 return;
206
207 __putname(nb->lfn);
208 exfat_init_namebuf(nb);
209}
210
211/* skip iterating emit_dots when dir is empty */
212#define ITER_POS_FILLED_DOTS (2)
703e3e9a 213static int exfat_iterate(struct file *file, struct dir_context *ctx)
ca061973 214{
703e3e9a 215 struct inode *inode = file_inode(file);
ca061973
NJ
216 struct super_block *sb = inode->i_sb;
217 struct inode *tmp;
218 struct exfat_dir_entry de;
219 struct exfat_dentry_namebuf *nb = &(de.namebuf);
220 struct exfat_inode_info *ei = EXFAT_I(inode);
221 unsigned long inum;
222 loff_t cpos, i_pos;
223 int err = 0, fake_offset = 0;
224
225 exfat_init_namebuf(nb);
226 mutex_lock(&EXFAT_SB(sb)->s_lock);
227
228 cpos = ctx->pos;
703e3e9a 229 if (!dir_emit_dots(file, ctx))
ca061973
NJ
230 goto unlock;
231
232 if (ctx->pos == ITER_POS_FILLED_DOTS) {
233 cpos = 0;
234 fake_offset = 1;
235 }
236
6cb5d1a1 237 cpos = round_up(cpos, DENTRY_SIZE);
ca061973
NJ
238
239 /* name buffer should be allocated before use */
240 err = exfat_alloc_namebuf(nb);
241 if (err)
242 goto unlock;
243get_new:
1e5654de 244 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
ca061973
NJ
245 goto end_of_dir;
246
04cee52f 247 err = exfat_readdir(inode, &cpos, &de);
ca061973
NJ
248 if (err) {
249 /*
250 * At least we tried to read a sector. Move cpos to next sector
251 * position (should be aligned).
252 */
253 if (err == -EIO) {
254 cpos += 1 << (sb->s_blocksize_bits);
255 cpos &= ~(sb->s_blocksize - 1);
256 }
257
258 err = -EIO;
259 goto end_of_dir;
260 }
261
ca061973
NJ
262 if (!nb->lfn[0])
263 goto end_of_dir;
264
04cee52f 265 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
ca061973
NJ
266 tmp = exfat_iget(sb, i_pos);
267 if (tmp) {
268 inum = tmp->i_ino;
269 iput(tmp);
270 } else {
271 inum = iunique(sb, EXFAT_ROOT_INO);
272 }
273
274 /*
275 * Before calling dir_emit(), sb_lock should be released.
276 * Because page fault can occur in dir_emit() when the size
277 * of buffer given from user is larger than one page size.
278 */
279 mutex_unlock(&EXFAT_SB(sb)->s_lock);
280 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
281 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
282 goto out_unlocked;
283 mutex_lock(&EXFAT_SB(sb)->s_lock);
284 ctx->pos = cpos;
285 goto get_new;
286
287end_of_dir:
288 if (!cpos && fake_offset)
289 cpos = ITER_POS_FILLED_DOTS;
290 ctx->pos = cpos;
291unlock:
292 mutex_unlock(&EXFAT_SB(sb)->s_lock);
293out_unlocked:
294 /*
295 * To improve performance, free namebuf after unlock sb_lock.
296 * If namebuf is not allocated, this function do nothing
297 */
298 exfat_free_namebuf(nb);
299 return err;
300}
301
302const struct file_operations exfat_dir_operations = {
303 .llseek = generic_file_llseek,
304 .read = generic_read_dir,
305 .iterate = exfat_iterate,
654762df
HK
306 .unlocked_ioctl = exfat_ioctl,
307#ifdef CONFIG_COMPAT
308 .compat_ioctl = exfat_compat_ioctl,
309#endif
5267456e 310 .fsync = exfat_file_fsync,
ca061973
NJ
311};
312
313int 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
23befe49 319 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
ca061973
NJ
320 if (ret)
321 return ret;
322
323 return exfat_zeroed_cluster(inode, clu->dir);
324}
325
326int 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 */
f3fe3954 335 return ES_ENTRY_NUM(len);
ca061973
NJ
336}
337
338unsigned 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
381static 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
406static 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
417static 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++) {
4ba6ccd6
HK
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 }
ca061973
NJ
432 }
433}
434
435int 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);
ca061973
NJ
442 struct exfat_dentry *ep;
443 struct buffer_head *bh;
444
445 /*
446 * We cannot use exfat_get_dentry_set here because file ep is not
447 * initialized yet.
448 */
c71510b3 449 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
ca061973
NJ
450 if (!ep)
451 return -EIO;
452
453 exfat_set_entry_type(ep, type);
454 exfat_set_entry_time(sbi, &ts,
455 &ep->dentry.file.create_tz,
456 &ep->dentry.file.create_time,
457 &ep->dentry.file.create_date,
ed0f84d3 458 &ep->dentry.file.create_time_cs);
ca061973
NJ
459 exfat_set_entry_time(sbi, &ts,
460 &ep->dentry.file.modify_tz,
461 &ep->dentry.file.modify_time,
462 &ep->dentry.file.modify_date,
ed0f84d3 463 &ep->dentry.file.modify_time_cs);
ca061973
NJ
464 exfat_set_entry_time(sbi, &ts,
465 &ep->dentry.file.access_tz,
466 &ep->dentry.file.access_time,
467 &ep->dentry.file.access_date,
468 NULL);
469
2c7f8937 470 exfat_update_bh(bh, IS_DIRSYNC(inode));
ca061973
NJ
471 brelse(bh);
472
c71510b3 473 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
ca061973
NJ
474 if (!ep)
475 return -EIO;
476
477 exfat_init_stream_entry(ep,
478 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
479 start_clu, size);
2c7f8937 480 exfat_update_bh(bh, IS_DIRSYNC(inode));
ca061973
NJ
481 brelse(bh);
482
483 return 0;
484}
485
486int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
487 int entry)
488{
489 struct super_block *sb = inode->i_sb;
490 int ret = 0;
491 int i, num_entries;
5875bf28 492 u16 chksum;
ca061973
NJ
493 struct exfat_dentry *ep, *fep;
494 struct buffer_head *fbh, *bh;
495
c71510b3 496 fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
ca061973
NJ
497 if (!fep)
498 return -EIO;
499
500 num_entries = fep->dentry.file.num_ext + 1;
5875bf28 501 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
ca061973
NJ
502
503 for (i = 1; i < num_entries; i++) {
c71510b3 504 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
ca061973
NJ
505 if (!ep) {
506 ret = -EIO;
507 goto release_fbh;
508 }
5875bf28 509 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
ca061973
NJ
510 CS_DEFAULT);
511 brelse(bh);
512 }
513
514 fep->dentry.file.checksum = cpu_to_le16(chksum);
2c7f8937 515 exfat_update_bh(fbh, IS_DIRSYNC(inode));
ca061973
NJ
516release_fbh:
517 brelse(fbh);
518 return ret;
519}
520
521int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
522 int entry, int num_entries, struct exfat_uni_name *p_uniname)
523{
524 struct super_block *sb = inode->i_sb;
525 int i;
ca061973
NJ
526 unsigned short *uniname = p_uniname->name;
527 struct exfat_dentry *ep;
528 struct buffer_head *bh;
529 int sync = IS_DIRSYNC(inode);
530
c71510b3 531 ep = exfat_get_dentry(sb, p_dir, entry, &bh);
ca061973
NJ
532 if (!ep)
533 return -EIO;
534
535 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
2c7f8937 536 exfat_update_bh(bh, sync);
ca061973
NJ
537 brelse(bh);
538
c71510b3 539 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
ca061973
NJ
540 if (!ep)
541 return -EIO;
542
543 ep->dentry.stream.name_len = p_uniname->name_len;
544 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
2c7f8937 545 exfat_update_bh(bh, sync);
ca061973
NJ
546 brelse(bh);
547
548 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
c71510b3 549 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
ca061973
NJ
550 if (!ep)
551 return -EIO;
552
553 exfat_init_name_entry(ep, uniname);
2c7f8937 554 exfat_update_bh(bh, sync);
ca061973
NJ
555 brelse(bh);
556 uniname += EXFAT_FILE_NAME_LEN;
557 }
558
559 exfat_update_dir_chksum(inode, p_dir, entry);
560 return 0;
561}
562
563int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
564 int entry, int order, int num_entries)
565{
566 struct super_block *sb = inode->i_sb;
567 int i;
ca061973
NJ
568 struct exfat_dentry *ep;
569 struct buffer_head *bh;
570
571 for (i = order; i < num_entries; i++) {
c71510b3 572 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
ca061973
NJ
573 if (!ep)
574 return -EIO;
575
576 exfat_set_entry_type(ep, TYPE_DELETED);
2c7f8937 577 exfat_update_bh(bh, IS_DIRSYNC(inode));
ca061973
NJ
578 brelse(bh);
579 }
580
581 return 0;
582}
583
943af1fd 584void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
ca061973 585{
943af1fd 586 int chksum_type = CS_DIR_ENTRY, i;
ca061973 587 unsigned short chksum = 0;
943af1fd 588 struct exfat_dentry *ep;
ca061973 589
f3fe3954 590 for (i = ES_IDX_FILE; i < es->num_entries; i++) {
943af1fd 591 ep = exfat_get_dentry_cached(es, i);
5875bf28
TK
592 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
593 chksum_type);
ca061973
NJ
594 chksum_type = CS_DEFAULT;
595 }
f3fe3954 596 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
943af1fd
TK
597 ep->dentry.file.checksum = cpu_to_le16(chksum);
598 es->modified = true;
599}
ca061973 600
3b9681ac 601int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
943af1fd 602{
3db3c3fb 603 int i, err = 0;
ca061973 604
3db3c3fb
TK
605 if (es->modified)
606 err = exfat_update_bhs(es->bh, es->num_bh, sync);
607
608 for (i = 0; i < es->num_bh; i++)
609 if (err)
610 bforget(es->bh[i]);
611 else
612 brelse(es->bh[i]);
a3ff29a9
YM
613
614 if (IS_DYNAMIC_ES(es))
615 kfree(es->bh);
616
8b0c4717 617 return err;
ca061973
NJ
618}
619
620static 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
8cf05883
CVB
652static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
653 int entry, sector_t *sector, int *offset)
ca061973
NJ
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)
678static 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) {
d1727d55
JP
692 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
693 (unsigned long long)sec, sbi->data_start_sector);
ca061973
NJ
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
712struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
c71510b3 713 struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
ca061973
NJ
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) {
d1727d55 720 exfat_err(sb, "abnormal access to deleted dentry");
ca061973
NJ
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
ca061973
NJ
735 return (struct exfat_dentry *)((*bh)->b_data + off);
736}
737
738enum 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
746static 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
943af1fd
TK
789struct 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
ca061973
NJ
799/*
800 * Returns a set of dentries for a file or dir.
801 *
943af1fd
TK
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.
ca061973
NJ
805 *
806 * in:
807 * sb+p_dir+entry: indicates a file/dir
808 * type: specifies how many dentries should be included.
ca061973
NJ
809 * return:
810 * pointer of entry set on success,
811 * NULL on failure.
812 */
20914ff6
YM
813int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
814 struct super_block *sb, struct exfat_chain *p_dir, int entry,
815 unsigned int type)
ca061973 816{
943af1fd 817 int ret, i, num_bh;
36955d36 818 unsigned int off;
ca061973
NJ
819 sector_t sec;
820 struct exfat_sb_info *sbi = EXFAT_SB(sb);
943af1fd
TK
821 struct exfat_dentry *ep;
822 int num_entries;
ca061973
NJ
823 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
824 struct buffer_head *bh;
825
826 if (p_dir->dir == DIR_DELETED) {
d1727d55 827 exfat_err(sb, "access to deleted dentry");
20914ff6 828 return -EIO;
ca061973
NJ
829 }
830
36955d36 831 ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
ca061973 832 if (ret)
20914ff6 833 return ret;
ca061973 834
20914ff6 835 memset(es, 0, sizeof(*es));
943af1fd
TK
836 es->sb = sb;
837 es->modified = false;
943af1fd 838 es->start_off = off;
a3ff29a9 839 es->bh = es->__bh;
ca061973 840
ca061973
NJ
841 bh = sb_bread(sb, sec);
842 if (!bh)
20914ff6 843 return -EIO;
943af1fd 844 es->bh[es->num_bh++] = bh;
ca061973 845
f3fe3954 846 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
943af1fd 847 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
3b9681ac 848 goto put_es;
ca061973
NJ
849
850 num_entries = type == ES_ALL_ENTRIES ?
851 ep->dentry.file.num_ext + 1 : type;
ca061973 852 es->num_entries = num_entries;
ca061973 853
943af1fd 854 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
a3ff29a9
YM
855 if (num_bh > ARRAY_SIZE(es->__bh)) {
856 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL);
857 if (!es->bh) {
858 brelse(bh);
20914ff6 859 return -ENOMEM;
a3ff29a9
YM
860 }
861 es->bh[0] = bh;
862 }
863
943af1fd
TK
864 for (i = 1; i < num_bh; i++) {
865 /* get the next sector */
866 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
36955d36
YM
867 unsigned int clu = exfat_sector_to_cluster(sbi, sec);
868
943af1fd
TK
869 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
870 clu++;
871 else if (exfat_get_next_cluster(sb, &clu))
3b9681ac 872 goto put_es;
943af1fd 873 sec = exfat_cluster_to_sector(sbi, clu);
ca061973 874 } else {
943af1fd 875 sec++;
ca061973 876 }
943af1fd
TK
877
878 bh = sb_bread(sb, sec);
879 if (!bh)
3b9681ac 880 goto put_es;
943af1fd 881 es->bh[es->num_bh++] = bh;
ca061973
NJ
882 }
883
6fa96cd5 884 /* validate cached dentries */
f3fe3954 885 for (i = ES_IDX_STREAM; i < num_entries; i++) {
943af1fd
TK
886 ep = exfat_get_dentry_cached(es, i);
887 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
3b9681ac 888 goto put_es;
943af1fd 889 }
20914ff6 890 return 0;
ca061973 891
3b9681ac
YM
892put_es:
893 exfat_put_dentry_set(es, false);
20914ff6 894 return -EIO;
ca061973
NJ
895}
896
ff39899b
YM
897static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
898{
899 hint_femp->eidx = EXFAT_HINT_NONE;
900 hint_femp->count = 0;
901}
902
903static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
904 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
e298c8a8 905 int dentry, int num_entries, int entry_type)
ff39899b
YM
906{
907 if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
908 ei->hint_femp.eidx > dentry) {
e298c8a8
YM
909 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
910
ff39899b
YM
911 if (candi_empty->count == 0) {
912 candi_empty->cur = *clu;
913 candi_empty->eidx = dentry;
914 }
915
e298c8a8
YM
916 if (entry_type == TYPE_UNUSED)
917 candi_empty->count += total_entries - dentry;
918 else
919 candi_empty->count++;
920
921 if (candi_empty->count == num_entries ||
922 candi_empty->count + candi_empty->eidx == total_entries)
ff39899b
YM
923 ei->hint_femp = *candi_empty;
924 }
925}
926
ca061973
NJ
927enum {
928 DIRENT_STEP_FILE,
929 DIRENT_STEP_STRM,
930 DIRENT_STEP_NAME,
931 DIRENT_STEP_SECD,
932};
933
934/*
c6e2f52e
HK
935 * @ei: inode info of parent directory
936 * @p_dir: directory structure of parent directory
937 * @num_entries:entry size of p_uniname
938 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
939 * for traversing cluster chain.
940 * @return:
941 * >= 0: file directory entry position where the name exists
942 * -ENOENT: entry with the name does not exist
943 * -EIO: I/O error
ca061973
NJ
944 */
945int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
946 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
72880cb5 947 struct exfat_hint *hint_opt)
ca061973
NJ
948{
949 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
950 int order, step, name_len = 0;
ff39899b 951 int dentries_per_clu;
ca061973
NJ
952 unsigned int entry_type;
953 unsigned short *uniname = NULL;
954 struct exfat_chain clu;
955 struct exfat_hint *hint_stat = &ei->hint_stat;
956 struct exfat_hint_femp candi_empty;
957 struct exfat_sb_info *sbi = EXFAT_SB(sb);
72880cb5
YM
958 int num_entries = exfat_calc_num_entries(p_uniname);
959
960 if (num_entries < 0)
961 return num_entries;
ca061973
NJ
962
963 dentries_per_clu = sbi->dentries_per_clu;
964
965 exfat_chain_dup(&clu, p_dir);
966
967 if (hint_stat->eidx) {
968 clu.dir = hint_stat->clu;
969 dentry = hint_stat->eidx;
970 end_eidx = dentry;
971 }
972
ff39899b
YM
973 exfat_reset_empty_hint(&ei->hint_femp);
974
ca061973
NJ
975rewind:
976 order = 0;
977 step = DIRENT_STEP_FILE;
ff39899b
YM
978 exfat_reset_empty_hint(&candi_empty);
979
ca061973
NJ
980 while (clu.dir != EXFAT_EOF_CLUSTER) {
981 i = dentry & (dentries_per_clu - 1);
982 for (; i < dentries_per_clu; i++, dentry++) {
983 struct exfat_dentry *ep;
984 struct buffer_head *bh;
985
986 if (rewind && dentry == end_eidx)
987 goto not_found;
988
c71510b3 989 ep = exfat_get_dentry(sb, &clu, i, &bh);
ca061973
NJ
990 if (!ep)
991 return -EIO;
992
993 entry_type = exfat_get_entry_type(ep);
994
995 if (entry_type == TYPE_UNUSED ||
996 entry_type == TYPE_DELETED) {
997 step = DIRENT_STEP_FILE;
998
ff39899b 999 exfat_set_empty_hint(ei, &candi_empty, &clu,
e298c8a8
YM
1000 dentry, num_entries,
1001 entry_type);
ca061973
NJ
1002
1003 brelse(bh);
1004 if (entry_type == TYPE_UNUSED)
1005 goto not_found;
1006 continue;
1007 }
1008
ff39899b 1009 exfat_reset_empty_hint(&candi_empty);
ca061973
NJ
1010
1011 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1012 step = DIRENT_STEP_FILE;
c6e2f52e
HK
1013 hint_opt->clu = clu.dir;
1014 hint_opt->eidx = i;
72880cb5
YM
1015 num_ext = ep->dentry.file.num_ext;
1016 step = DIRENT_STEP_STRM;
ca061973
NJ
1017 brelse(bh);
1018 continue;
1019 }
1020
1021 if (entry_type == TYPE_STREAM) {
5875bf28 1022 u16 name_hash;
ca061973
NJ
1023
1024 if (step != DIRENT_STEP_STRM) {
1025 step = DIRENT_STEP_FILE;
1026 brelse(bh);
1027 continue;
1028 }
1029 step = DIRENT_STEP_FILE;
1030 name_hash = le16_to_cpu(
1031 ep->dentry.stream.name_hash);
1032 if (p_uniname->name_hash == name_hash &&
1033 p_uniname->name_len ==
1034 ep->dentry.stream.name_len) {
1035 step = DIRENT_STEP_NAME;
1036 order = 1;
1037 name_len = 0;
1038 }
1039 brelse(bh);
1040 continue;
1041 }
1042
1043 brelse(bh);
1044 if (entry_type == TYPE_EXTEND) {
1045 unsigned short entry_uniname[16], unichar;
1046
1047 if (step != DIRENT_STEP_NAME) {
1048 step = DIRENT_STEP_FILE;
1049 continue;
1050 }
1051
1052 if (++order == 2)
1053 uniname = p_uniname->name;
1054 else
1055 uniname += EXFAT_FILE_NAME_LEN;
1056
1057 len = exfat_extract_uni_name(ep, entry_uniname);
1058 name_len += len;
1059
1060 unichar = *(uniname+len);
1061 *(uniname+len) = 0x0;
1062
1063 if (exfat_uniname_ncmp(sb, uniname,
1064 entry_uniname, len)) {
1065 step = DIRENT_STEP_FILE;
1066 } else if (p_uniname->name_len == name_len) {
1067 if (order == num_ext)
1068 goto found;
1069 step = DIRENT_STEP_SECD;
1070 }
1071
1072 *(uniname+len) = unichar;
1073 continue;
1074 }
1075
1076 if (entry_type &
1077 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1078 if (step == DIRENT_STEP_SECD) {
1079 if (++order == num_ext)
1080 goto found;
1081 continue;
1082 }
1083 }
1084 step = DIRENT_STEP_FILE;
1085 }
1086
1087 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1088 if (--clu.size > 0)
1089 clu.dir++;
1090 else
1091 clu.dir = EXFAT_EOF_CLUSTER;
1092 } else {
1093 if (exfat_get_next_cluster(sb, &clu.dir))
1094 return -EIO;
1095 }
1096 }
1097
1098not_found:
1099 /*
1100 * We started at not 0 index,so we should try to find target
1101 * from 0 index to the index we started at.
1102 */
1103 if (!rewind && end_eidx) {
1104 rewind = 1;
1105 dentry = 0;
1106 clu.dir = p_dir->dir;
ca061973
NJ
1107 goto rewind;
1108 }
1109
e298c8a8
YM
1110 /*
1111 * set the EXFAT_EOF_CLUSTER flag to avoid search
1112 * from the beginning again when allocated a new cluster
1113 */
1114 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1115 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1116 ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1117 ei->hint_femp.count = 0;
1118 }
1119
ca061973
NJ
1120 /* initialized hint_stat */
1121 hint_stat->clu = p_dir->dir;
1122 hint_stat->eidx = 0;
1123 return -ENOENT;
1124
1125found:
1126 /* next dentry we'll find is out of this cluster */
1127 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1128 int ret = 0;
1129
1130 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1131 if (--clu.size > 0)
1132 clu.dir++;
1133 else
1134 clu.dir = EXFAT_EOF_CLUSTER;
1135 } else {
1136 ret = exfat_get_next_cluster(sb, &clu.dir);
1137 }
1138
d2fa0c33 1139 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
ca061973
NJ
1140 /* just initialized hint_stat */
1141 hint_stat->clu = p_dir->dir;
1142 hint_stat->eidx = 0;
1143 return (dentry - num_ext);
1144 }
1145 }
1146
1147 hint_stat->clu = clu.dir;
1148 hint_stat->eidx = dentry + 1;
1149 return dentry - num_ext;
1150}
1151
1152int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1153 int entry, struct exfat_dentry *ep)
1154{
1155 int i, count = 0;
1156 unsigned int type;
1157 struct exfat_dentry *ext_ep;
1158 struct buffer_head *bh;
1159
1160 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
c71510b3 1161 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
ca061973
NJ
1162 if (!ext_ep)
1163 return -EIO;
1164
1165 type = exfat_get_entry_type(ext_ep);
1166 brelse(bh);
1167 if (type == TYPE_EXTEND || type == TYPE_STREAM)
1168 count++;
1169 else
1170 break;
1171 }
1172 return count;
1173}
1174
1175int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1176{
1177 int i, count = 0;
1178 int dentries_per_clu;
1179 unsigned int entry_type;
1180 struct exfat_chain clu;
1181 struct exfat_dentry *ep;
1182 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1183 struct buffer_head *bh;
1184
1185 dentries_per_clu = sbi->dentries_per_clu;
1186
1187 exfat_chain_dup(&clu, p_dir);
1188
1189 while (clu.dir != EXFAT_EOF_CLUSTER) {
1190 for (i = 0; i < dentries_per_clu; i++) {
c71510b3 1191 ep = exfat_get_dentry(sb, &clu, i, &bh);
ca061973
NJ
1192 if (!ep)
1193 return -EIO;
1194 entry_type = exfat_get_entry_type(ep);
1195 brelse(bh);
1196
1197 if (entry_type == TYPE_UNUSED)
1198 return count;
1199 if (entry_type != TYPE_DIR)
1200 continue;
1201 count++;
1202 }
1203
1204 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1205 if (--clu.size > 0)
1206 clu.dir++;
1207 else
1208 clu.dir = EXFAT_EOF_CLUSTER;
1209 } else {
1210 if (exfat_get_next_cluster(sb, &(clu.dir)))
1211 return -EIO;
1212 }
1213 }
1214
1215 return count;
1216}