Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/exfat/dir.c
49933 views
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
#include <linux/filelock.h>
11
12
#include "exfat_raw.h"
13
#include "exfat_fs.h"
14
15
static int exfat_extract_uni_name(struct exfat_dentry *ep,
16
unsigned short *uniname)
17
{
18
int i, len = 0;
19
20
for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
21
*uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
22
if (*uniname == 0x0)
23
return len;
24
uniname++;
25
len++;
26
}
27
28
*uniname = 0x0;
29
return len;
30
31
}
32
33
static int exfat_get_uniname_from_ext_entry(struct super_block *sb,
34
struct exfat_chain *p_dir, int entry, unsigned short *uniname)
35
{
36
int i, err;
37
struct exfat_entry_set_cache es;
38
unsigned int uni_len = 0, len;
39
40
err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES);
41
if (err)
42
return err;
43
44
/*
45
* First entry : file entry
46
* Second entry : stream-extension entry
47
* Third entry : first file-name entry
48
* So, the index of first file-name dentry should start from 2.
49
*/
50
for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
51
struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
52
53
/* end of name entry */
54
if (exfat_get_entry_type(ep) != TYPE_EXTEND)
55
break;
56
57
len = exfat_extract_uni_name(ep, uniname);
58
uni_len += len;
59
if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
60
break;
61
uniname += EXFAT_FILE_NAME_LEN;
62
}
63
64
exfat_put_dentry_set(&es, false);
65
return 0;
66
}
67
68
/* read a directory entry from the opened directory */
69
static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
70
{
71
int i, dentries_per_clu, num_ext, err;
72
unsigned int type, clu_offset, max_dentries;
73
struct exfat_chain dir, clu;
74
struct exfat_uni_name uni_name;
75
struct exfat_dentry *ep;
76
struct super_block *sb = inode->i_sb;
77
struct exfat_sb_info *sbi = EXFAT_SB(sb);
78
struct exfat_inode_info *ei = EXFAT_I(inode);
79
unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
80
struct buffer_head *bh;
81
82
/* check if the given file ID is opened */
83
if (ei->type != TYPE_DIR)
84
return -EPERM;
85
86
exfat_chain_set(&dir, ei->start_clu,
87
EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
88
89
dentries_per_clu = sbi->dentries_per_clu;
90
max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
91
(u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
92
93
clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
94
exfat_chain_dup(&clu, &dir);
95
96
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
97
clu.dir += clu_offset;
98
clu.size -= clu_offset;
99
} else {
100
/* hint_information */
101
if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
102
ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
103
clu_offset -= ei->hint_bmap.off;
104
clu.dir = ei->hint_bmap.clu;
105
}
106
107
while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
108
if (exfat_get_next_cluster(sb, &(clu.dir)))
109
return -EIO;
110
111
clu_offset--;
112
}
113
}
114
115
while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
116
i = dentry & (dentries_per_clu - 1);
117
118
for ( ; i < dentries_per_clu; i++, dentry++) {
119
ep = exfat_get_dentry(sb, &clu, i, &bh);
120
if (!ep)
121
return -EIO;
122
123
type = exfat_get_entry_type(ep);
124
if (type == TYPE_UNUSED) {
125
brelse(bh);
126
goto out;
127
}
128
129
if (type != TYPE_FILE && type != TYPE_DIR) {
130
brelse(bh);
131
continue;
132
}
133
134
num_ext = ep->dentry.file.num_ext;
135
dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
136
137
*uni_name.name = 0x0;
138
err = exfat_get_uniname_from_ext_entry(sb, &clu, i,
139
uni_name.name);
140
if (err) {
141
brelse(bh);
142
continue;
143
}
144
exfat_utf16_to_nls(sb, &uni_name,
145
dir_entry->namebuf.lfn,
146
dir_entry->namebuf.lfnbuf_len);
147
brelse(bh);
148
149
ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
150
if (!ep)
151
return -EIO;
152
dir_entry->entry = i;
153
dir_entry->dir = clu;
154
brelse(bh);
155
156
ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
157
ei->hint_bmap.clu = clu.dir;
158
159
*cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
160
return 0;
161
}
162
163
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
164
if (--clu.size > 0)
165
clu.dir++;
166
else
167
clu.dir = EXFAT_EOF_CLUSTER;
168
} else {
169
if (exfat_get_next_cluster(sb, &(clu.dir)))
170
return -EIO;
171
}
172
}
173
174
out:
175
dir_entry->namebuf.lfn[0] = '\0';
176
*cpos = EXFAT_DEN_TO_B(dentry);
177
return 0;
178
}
179
180
static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
181
{
182
nb->lfn = NULL;
183
nb->lfnbuf_len = 0;
184
}
185
186
static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
187
{
188
nb->lfn = __getname();
189
if (!nb->lfn)
190
return -ENOMEM;
191
nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
192
return 0;
193
}
194
195
static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
196
{
197
if (!nb->lfn)
198
return;
199
200
__putname(nb->lfn);
201
exfat_init_namebuf(nb);
202
}
203
204
/*
205
* Before calling dir_emit*(), sbi->s_lock should be released
206
* because page fault can occur in dir_emit*().
207
*/
208
#define ITER_POS_FILLED_DOTS (2)
209
static int exfat_iterate(struct file *file, struct dir_context *ctx)
210
{
211
struct inode *inode = file_inode(file);
212
struct super_block *sb = inode->i_sb;
213
struct inode *tmp;
214
struct exfat_dir_entry de;
215
struct exfat_dentry_namebuf *nb = &(de.namebuf);
216
struct exfat_inode_info *ei = EXFAT_I(inode);
217
unsigned long inum;
218
loff_t cpos, i_pos;
219
int err = 0, fake_offset = 0;
220
221
exfat_init_namebuf(nb);
222
223
cpos = ctx->pos;
224
if (!dir_emit_dots(file, ctx))
225
goto out;
226
227
if (ctx->pos == ITER_POS_FILLED_DOTS) {
228
cpos = 0;
229
fake_offset = 1;
230
}
231
232
cpos = round_up(cpos, DENTRY_SIZE);
233
234
/* name buffer should be allocated before use */
235
err = exfat_alloc_namebuf(nb);
236
if (err)
237
goto out;
238
get_new:
239
mutex_lock(&EXFAT_SB(sb)->s_lock);
240
241
if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
242
goto end_of_dir;
243
244
err = exfat_readdir(inode, &cpos, &de);
245
if (err) {
246
/*
247
* At least we tried to read a sector.
248
* Move cpos to next sector position (should be aligned).
249
*/
250
if (err == -EIO) {
251
cpos += 1 << (sb->s_blocksize_bits);
252
cpos &= ~(sb->s_blocksize - 1);
253
}
254
255
err = -EIO;
256
goto end_of_dir;
257
}
258
259
if (!nb->lfn[0])
260
goto end_of_dir;
261
262
i_pos = ((loff_t)de.dir.dir << 32) | (de.entry & 0xffffffff);
263
tmp = exfat_iget(sb, i_pos);
264
if (tmp) {
265
inum = tmp->i_ino;
266
iput(tmp);
267
} else {
268
inum = iunique(sb, EXFAT_ROOT_INO);
269
}
270
271
mutex_unlock(&EXFAT_SB(sb)->s_lock);
272
if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
273
(de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG))
274
goto out;
275
ctx->pos = cpos;
276
goto get_new;
277
278
end_of_dir:
279
if (!cpos && fake_offset)
280
cpos = ITER_POS_FILLED_DOTS;
281
ctx->pos = cpos;
282
mutex_unlock(&EXFAT_SB(sb)->s_lock);
283
out:
284
/*
285
* To improve performance, free namebuf after unlock sb_lock.
286
* If namebuf is not allocated, this function do nothing
287
*/
288
exfat_free_namebuf(nb);
289
return err;
290
}
291
292
WRAP_DIR_ITER(exfat_iterate) // FIXME!
293
const struct file_operations exfat_dir_operations = {
294
.llseek = generic_file_llseek,
295
.read = generic_read_dir,
296
.iterate_shared = shared_exfat_iterate,
297
.unlocked_ioctl = exfat_ioctl,
298
#ifdef CONFIG_COMPAT
299
.compat_ioctl = exfat_compat_ioctl,
300
#endif
301
.fsync = exfat_file_fsync,
302
.setlease = generic_setlease,
303
};
304
305
int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
306
{
307
int ret;
308
309
exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
310
311
ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
312
if (ret)
313
return ret;
314
315
return exfat_zeroed_cluster(inode, clu->dir);
316
}
317
318
int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
319
{
320
int len;
321
322
len = p_uniname->name_len;
323
if (len == 0)
324
return -EINVAL;
325
326
/* 1 file entry + 1 stream entry + name entries */
327
return ES_ENTRY_NUM(len);
328
}
329
330
unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
331
{
332
if (ep->type == EXFAT_UNUSED)
333
return TYPE_UNUSED;
334
if (IS_EXFAT_DELETED(ep->type))
335
return TYPE_DELETED;
336
if (ep->type == EXFAT_INVAL)
337
return TYPE_INVALID;
338
if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
339
if (ep->type == EXFAT_BITMAP)
340
return TYPE_BITMAP;
341
if (ep->type == EXFAT_UPCASE)
342
return TYPE_UPCASE;
343
if (ep->type == EXFAT_VOLUME)
344
return TYPE_VOLUME;
345
if (ep->type == EXFAT_FILE) {
346
if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR)
347
return TYPE_DIR;
348
return TYPE_FILE;
349
}
350
return TYPE_CRITICAL_PRI;
351
}
352
if (IS_EXFAT_BENIGN_PRI(ep->type)) {
353
if (ep->type == EXFAT_GUID)
354
return TYPE_GUID;
355
if (ep->type == EXFAT_PADDING)
356
return TYPE_PADDING;
357
if (ep->type == EXFAT_ACLTAB)
358
return TYPE_ACLTAB;
359
return TYPE_BENIGN_PRI;
360
}
361
if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
362
if (ep->type == EXFAT_STREAM)
363
return TYPE_STREAM;
364
if (ep->type == EXFAT_NAME)
365
return TYPE_EXTEND;
366
if (ep->type == EXFAT_ACL)
367
return TYPE_ACL;
368
return TYPE_CRITICAL_SEC;
369
}
370
371
if (ep->type == EXFAT_VENDOR_EXT)
372
return TYPE_VENDOR_EXT;
373
if (ep->type == EXFAT_VENDOR_ALLOC)
374
return TYPE_VENDOR_ALLOC;
375
376
return TYPE_BENIGN_SEC;
377
}
378
379
static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
380
{
381
if (type == TYPE_UNUSED) {
382
ep->type = EXFAT_UNUSED;
383
} else if (type == TYPE_DELETED) {
384
ep->type &= EXFAT_DELETE;
385
} else if (type == TYPE_STREAM) {
386
ep->type = EXFAT_STREAM;
387
} else if (type == TYPE_EXTEND) {
388
ep->type = EXFAT_NAME;
389
} else if (type == TYPE_BITMAP) {
390
ep->type = EXFAT_BITMAP;
391
} else if (type == TYPE_UPCASE) {
392
ep->type = EXFAT_UPCASE;
393
} else if (type == TYPE_VOLUME) {
394
ep->type = EXFAT_VOLUME;
395
} else if (type == TYPE_DIR) {
396
ep->type = EXFAT_FILE;
397
ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR);
398
} else if (type == TYPE_FILE) {
399
ep->type = EXFAT_FILE;
400
ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE);
401
}
402
}
403
404
static void exfat_init_stream_entry(struct exfat_dentry *ep,
405
unsigned int start_clu, unsigned long long size)
406
{
407
memset(ep, 0, sizeof(*ep));
408
exfat_set_entry_type(ep, TYPE_STREAM);
409
if (size == 0)
410
ep->dentry.stream.flags = ALLOC_FAT_CHAIN;
411
else
412
ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN;
413
ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
414
ep->dentry.stream.valid_size = cpu_to_le64(size);
415
ep->dentry.stream.size = cpu_to_le64(size);
416
}
417
418
static void exfat_init_name_entry(struct exfat_dentry *ep,
419
unsigned short *uniname)
420
{
421
int i;
422
423
exfat_set_entry_type(ep, TYPE_EXTEND);
424
ep->dentry.name.flags = 0x0;
425
426
for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
427
if (*uniname != 0x0) {
428
ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
429
uniname++;
430
} else {
431
ep->dentry.name.unicode_0_14[i] = 0x0;
432
}
433
}
434
}
435
436
void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
437
unsigned int type, unsigned int start_clu,
438
unsigned long long size, struct timespec64 *ts)
439
{
440
struct super_block *sb = es->sb;
441
struct exfat_sb_info *sbi = EXFAT_SB(sb);
442
struct exfat_dentry *ep;
443
444
ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
445
memset(ep, 0, sizeof(*ep));
446
exfat_set_entry_type(ep, type);
447
exfat_set_entry_time(sbi, ts,
448
&ep->dentry.file.create_tz,
449
&ep->dentry.file.create_time,
450
&ep->dentry.file.create_date,
451
&ep->dentry.file.create_time_cs);
452
exfat_set_entry_time(sbi, ts,
453
&ep->dentry.file.modify_tz,
454
&ep->dentry.file.modify_time,
455
&ep->dentry.file.modify_date,
456
&ep->dentry.file.modify_time_cs);
457
exfat_set_entry_time(sbi, ts,
458
&ep->dentry.file.access_tz,
459
&ep->dentry.file.access_time,
460
&ep->dentry.file.access_date,
461
NULL);
462
463
ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
464
exfat_init_stream_entry(ep, start_clu, size);
465
}
466
467
static void exfat_free_benign_secondary_clusters(struct inode *inode,
468
struct exfat_dentry *ep)
469
{
470
struct super_block *sb = inode->i_sb;
471
struct exfat_chain dir;
472
unsigned int start_clu =
473
le32_to_cpu(ep->dentry.generic_secondary.start_clu);
474
u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
475
unsigned char flags = ep->dentry.generic_secondary.flags;
476
477
if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
478
return;
479
480
exfat_chain_set(&dir, start_clu,
481
EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
482
flags);
483
exfat_free_cluster(inode, &dir);
484
}
485
486
void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
487
struct exfat_uni_name *p_uniname)
488
{
489
int i;
490
unsigned short *uniname = p_uniname->name;
491
struct exfat_dentry *ep;
492
493
ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
494
ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
495
496
ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
497
ep->dentry.stream.name_len = p_uniname->name_len;
498
ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
499
500
for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
501
ep = exfat_get_dentry_cached(es, i);
502
exfat_init_name_entry(ep, uniname);
503
uniname += EXFAT_FILE_NAME_LEN;
504
}
505
506
exfat_update_dir_chksum(es);
507
}
508
509
void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
510
int order)
511
{
512
int i;
513
struct exfat_dentry *ep;
514
515
for (i = order; i < es->num_entries; i++) {
516
ep = exfat_get_dentry_cached(es, i);
517
518
if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
519
exfat_free_benign_secondary_clusters(inode, ep);
520
521
exfat_set_entry_type(ep, TYPE_DELETED);
522
}
523
524
if (order < es->num_entries)
525
es->modified = true;
526
}
527
528
void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
529
{
530
int chksum_type = CS_DIR_ENTRY, i;
531
unsigned short chksum = 0;
532
struct exfat_dentry *ep;
533
534
for (i = ES_IDX_FILE; i < es->num_entries; i++) {
535
ep = exfat_get_dentry_cached(es, i);
536
chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
537
chksum_type);
538
chksum_type = CS_DEFAULT;
539
}
540
ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
541
ep->dentry.file.checksum = cpu_to_le16(chksum);
542
es->modified = true;
543
}
544
545
int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
546
{
547
int i, err = 0;
548
549
if (es->modified)
550
err = exfat_update_bhs(es->bh, es->num_bh, sync);
551
552
for (i = 0; i < es->num_bh; i++)
553
if (err)
554
bforget(es->bh[i]);
555
else
556
brelse(es->bh[i]);
557
558
if (IS_DYNAMIC_ES(es))
559
kfree(es->bh);
560
561
return err;
562
}
563
564
static int exfat_walk_fat_chain(struct super_block *sb,
565
struct exfat_chain *p_dir, unsigned int byte_offset,
566
unsigned int *clu)
567
{
568
struct exfat_sb_info *sbi = EXFAT_SB(sb);
569
unsigned int clu_offset;
570
unsigned int cur_clu;
571
572
clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
573
cur_clu = p_dir->dir;
574
575
if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
576
cur_clu += clu_offset;
577
} else {
578
while (clu_offset > 0) {
579
if (exfat_get_next_cluster(sb, &cur_clu))
580
return -EIO;
581
if (cur_clu == EXFAT_EOF_CLUSTER) {
582
exfat_fs_error(sb,
583
"invalid dentry access beyond EOF (clu : %u, eidx : %d)",
584
p_dir->dir,
585
EXFAT_B_TO_DEN(byte_offset));
586
return -EIO;
587
}
588
clu_offset--;
589
}
590
}
591
592
*clu = cur_clu;
593
return 0;
594
}
595
596
static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
597
int entry, sector_t *sector, int *offset)
598
{
599
int ret;
600
unsigned int off, clu = 0;
601
struct exfat_sb_info *sbi = EXFAT_SB(sb);
602
603
off = EXFAT_DEN_TO_B(entry);
604
605
ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
606
if (ret)
607
return ret;
608
609
if (!exfat_test_bitmap(sb, clu)) {
610
exfat_err(sb, "failed to test cluster bit(%u)", clu);
611
return -EIO;
612
}
613
614
/* byte offset in cluster */
615
off = EXFAT_CLU_OFFSET(off, sbi);
616
617
/* byte offset in sector */
618
*offset = EXFAT_BLK_OFFSET(off, sb);
619
620
/* sector offset in cluster */
621
*sector = EXFAT_B_TO_BLK(off, sb);
622
*sector += exfat_cluster_to_sector(sbi, clu);
623
return 0;
624
}
625
626
#define EXFAT_MAX_RA_SIZE (128*1024)
627
static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
628
{
629
struct exfat_sb_info *sbi = EXFAT_SB(sb);
630
struct buffer_head *bh;
631
unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
632
unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
633
unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
634
unsigned int ra_count = min(adj_ra_count, max_ra_count);
635
636
/* Read-ahead is not required */
637
if (sbi->sect_per_clus == 1)
638
return 0;
639
640
if (sec < sbi->data_start_sector) {
641
exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
642
(unsigned long long)sec, sbi->data_start_sector);
643
return -EIO;
644
}
645
646
/* Not sector aligned with ra_count, resize ra_count to page size */
647
if ((sec - sbi->data_start_sector) & (ra_count - 1))
648
ra_count = page_ra_count;
649
650
bh = sb_find_get_block(sb, sec);
651
if (!bh || !buffer_uptodate(bh)) {
652
unsigned int i;
653
654
for (i = 0; i < ra_count; i++)
655
sb_breadahead(sb, (sector_t)(sec + i));
656
}
657
brelse(bh);
658
return 0;
659
}
660
661
struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
662
struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
663
{
664
unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
665
int off;
666
sector_t sec;
667
668
if (p_dir->dir == DIR_DELETED) {
669
exfat_err(sb, "abnormal access to deleted dentry");
670
return NULL;
671
}
672
673
if (exfat_find_location(sb, p_dir, entry, &sec, &off))
674
return NULL;
675
676
if (p_dir->dir != EXFAT_FREE_CLUSTER &&
677
!(entry & (dentries_per_page - 1)))
678
exfat_dir_readahead(sb, sec);
679
680
*bh = sb_bread(sb, sec);
681
if (!*bh)
682
return NULL;
683
684
return (struct exfat_dentry *)((*bh)->b_data + off);
685
}
686
687
enum exfat_validate_dentry_mode {
688
ES_MODE_GET_FILE_ENTRY,
689
ES_MODE_GET_STRM_ENTRY,
690
ES_MODE_GET_NAME_ENTRY,
691
ES_MODE_GET_CRITICAL_SEC_ENTRY,
692
ES_MODE_GET_BENIGN_SEC_ENTRY,
693
};
694
695
static bool exfat_validate_entry(unsigned int type,
696
enum exfat_validate_dentry_mode *mode)
697
{
698
if (type == TYPE_UNUSED || type == TYPE_DELETED)
699
return false;
700
701
switch (*mode) {
702
case ES_MODE_GET_FILE_ENTRY:
703
if (type != TYPE_STREAM)
704
return false;
705
*mode = ES_MODE_GET_STRM_ENTRY;
706
break;
707
case ES_MODE_GET_STRM_ENTRY:
708
if (type != TYPE_EXTEND)
709
return false;
710
*mode = ES_MODE_GET_NAME_ENTRY;
711
break;
712
case ES_MODE_GET_NAME_ENTRY:
713
if (type & TYPE_BENIGN_SEC)
714
*mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
715
else if (type != TYPE_EXTEND)
716
return false;
717
break;
718
case ES_MODE_GET_BENIGN_SEC_ENTRY:
719
/* Assume unreconized benign secondary entry */
720
if (!(type & TYPE_BENIGN_SEC))
721
return false;
722
break;
723
default:
724
return false;
725
}
726
727
return true;
728
}
729
730
struct exfat_dentry *exfat_get_dentry_cached(
731
struct exfat_entry_set_cache *es, int num)
732
{
733
int off = es->start_off + num * DENTRY_SIZE;
734
struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
735
char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
736
737
return (struct exfat_dentry *)p;
738
}
739
740
/*
741
* Returns a set of dentries.
742
*
743
* Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
744
* User should call exfat_get_dentry_set() after setting 'modified' to apply
745
* changes made in this entry set to the real device.
746
*
747
* in:
748
* sb+p_dir+entry: indicates a file/dir
749
* num_entries: specifies how many dentries should be included.
750
* It will be set to es->num_entries if it is not 0.
751
* If num_entries is 0, es->num_entries will be obtained
752
* from the first dentry.
753
* out:
754
* es: pointer of entry set on success.
755
* return:
756
* 0 on success
757
* -error code on failure
758
*/
759
static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
760
struct super_block *sb, struct exfat_chain *p_dir, int entry,
761
unsigned int num_entries)
762
{
763
int ret, i, num_bh;
764
unsigned int off;
765
sector_t sec;
766
struct exfat_sb_info *sbi = EXFAT_SB(sb);
767
struct buffer_head *bh;
768
769
if (p_dir->dir == DIR_DELETED) {
770
exfat_err(sb, "access to deleted dentry");
771
return -EIO;
772
}
773
774
ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
775
if (ret)
776
return ret;
777
778
memset(es, 0, sizeof(*es));
779
es->sb = sb;
780
es->modified = false;
781
es->start_off = off;
782
es->bh = es->__bh;
783
784
bh = sb_bread(sb, sec);
785
if (!bh)
786
return -EIO;
787
es->bh[es->num_bh++] = bh;
788
789
if (num_entries == ES_ALL_ENTRIES) {
790
struct exfat_dentry *ep;
791
792
ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
793
if (ep->type != EXFAT_FILE) {
794
brelse(bh);
795
return -EIO;
796
}
797
798
num_entries = ep->dentry.file.num_ext + 1;
799
}
800
801
es->num_entries = num_entries;
802
803
num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
804
if (num_bh > ARRAY_SIZE(es->__bh)) {
805
es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_NOFS);
806
if (!es->bh) {
807
brelse(bh);
808
return -ENOMEM;
809
}
810
es->bh[0] = bh;
811
}
812
813
for (i = 1; i < num_bh; i++) {
814
/* get the next sector */
815
if (exfat_is_last_sector_in_cluster(sbi, sec)) {
816
unsigned int clu = exfat_sector_to_cluster(sbi, sec);
817
818
if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
819
clu++;
820
else if (exfat_get_next_cluster(sb, &clu))
821
goto put_es;
822
sec = exfat_cluster_to_sector(sbi, clu);
823
} else {
824
sec++;
825
}
826
827
bh = sb_bread(sb, sec);
828
if (!bh)
829
goto put_es;
830
es->bh[es->num_bh++] = bh;
831
}
832
833
return 0;
834
835
put_es:
836
exfat_put_dentry_set(es, false);
837
return -EIO;
838
}
839
840
int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
841
struct super_block *sb, struct exfat_chain *p_dir,
842
int entry, unsigned int num_entries)
843
{
844
int ret, i;
845
struct exfat_dentry *ep;
846
enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
847
848
ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
849
if (ret < 0)
850
return ret;
851
852
/* validate cached dentries */
853
for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
854
ep = exfat_get_dentry_cached(es, i);
855
if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
856
goto put_es;
857
}
858
return 0;
859
860
put_es:
861
exfat_put_dentry_set(es, false);
862
return -EIO;
863
}
864
865
static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
866
{
867
struct exfat_dentry *ep;
868
struct buffer_head *bh;
869
int i, off;
870
bool unused_hit = false;
871
872
/*
873
* ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
874
* Although it violates the specification for a deleted entry to
875
* follow an unused entry, some exFAT implementations could work
876
* like this. Therefore, to improve compatibility, let's allow it.
877
*/
878
for (i = 0; i < es->num_entries; i++) {
879
ep = exfat_get_dentry_cached(es, i);
880
if (ep->type == EXFAT_UNUSED) {
881
unused_hit = true;
882
} else if (!IS_EXFAT_DELETED(ep->type)) {
883
if (unused_hit)
884
goto err_used_follow_unused;
885
i++;
886
goto count_skip_entries;
887
}
888
}
889
890
return 0;
891
892
err_used_follow_unused:
893
off = es->start_off + (i << DENTRY_SIZE_BITS);
894
bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
895
896
exfat_fs_error(es->sb,
897
"in sector %lld, dentry %d should be unused, but 0x%x",
898
bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
899
900
return -EIO;
901
902
count_skip_entries:
903
es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off);
904
for (; i < es->num_entries; i++) {
905
ep = exfat_get_dentry_cached(es, i);
906
if (IS_EXFAT_DELETED(ep->type))
907
break;
908
}
909
910
return i;
911
}
912
913
/*
914
* Get an empty dentry set.
915
*
916
* in:
917
* sb+p_dir+entry: indicates the empty dentry location
918
* num_entries: specifies how many empty dentries should be included.
919
* out:
920
* es: pointer of empty dentry set on success.
921
* return:
922
* 0 : on success
923
* >0 : the dentries are not empty, the return value is the number of
924
* dentries to be skipped for the next lookup.
925
* <0 : on failure
926
*/
927
int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
928
struct super_block *sb, struct exfat_chain *p_dir,
929
int entry, unsigned int num_entries)
930
{
931
int ret;
932
933
ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
934
if (ret < 0)
935
return ret;
936
937
ret = exfat_validate_empty_dentry_set(es);
938
if (ret)
939
exfat_put_dentry_set(es, false);
940
941
return ret;
942
}
943
944
static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
945
{
946
hint_femp->eidx = EXFAT_HINT_NONE;
947
hint_femp->count = 0;
948
}
949
950
static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
951
struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
952
int dentry, int num_entries, int entry_type)
953
{
954
if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
955
ei->hint_femp.eidx > dentry) {
956
int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
957
958
if (candi_empty->count == 0) {
959
candi_empty->cur = *clu;
960
candi_empty->eidx = dentry;
961
}
962
963
if (entry_type == TYPE_UNUSED)
964
candi_empty->count += total_entries - dentry;
965
else
966
candi_empty->count++;
967
968
if (candi_empty->count == num_entries ||
969
candi_empty->count + candi_empty->eidx == total_entries)
970
ei->hint_femp = *candi_empty;
971
}
972
}
973
974
enum {
975
DIRENT_STEP_FILE,
976
DIRENT_STEP_STRM,
977
DIRENT_STEP_NAME,
978
DIRENT_STEP_SECD,
979
};
980
981
/*
982
* @ei: inode info of parent directory
983
* @p_dir: directory structure of parent directory
984
* @num_entries:entry size of p_uniname
985
* @hint_opt: If p_uniname is found, filled with optimized dir/entry
986
* for traversing cluster chain.
987
* @return:
988
* >= 0: file directory entry position where the name exists
989
* -ENOENT: entry with the name does not exist
990
* -EIO: I/O error
991
*/
992
int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
993
struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
994
struct exfat_hint *hint_opt)
995
{
996
int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
997
int order, step, name_len = 0;
998
int dentries_per_clu;
999
unsigned int entry_type;
1000
unsigned short *uniname = NULL;
1001
struct exfat_chain clu;
1002
struct exfat_hint *hint_stat = &ei->hint_stat;
1003
struct exfat_hint_femp candi_empty;
1004
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1005
int num_entries = exfat_calc_num_entries(p_uniname);
1006
unsigned int clu_count = 0;
1007
1008
if (num_entries < 0)
1009
return num_entries;
1010
1011
dentries_per_clu = sbi->dentries_per_clu;
1012
1013
exfat_chain_dup(&clu, p_dir);
1014
1015
if (hint_stat->eidx) {
1016
clu.dir = hint_stat->clu;
1017
dentry = hint_stat->eidx;
1018
end_eidx = dentry;
1019
}
1020
1021
exfat_reset_empty_hint(&ei->hint_femp);
1022
1023
rewind:
1024
order = 0;
1025
step = DIRENT_STEP_FILE;
1026
exfat_reset_empty_hint(&candi_empty);
1027
1028
while (clu.dir != EXFAT_EOF_CLUSTER) {
1029
i = dentry & (dentries_per_clu - 1);
1030
for (; i < dentries_per_clu; i++, dentry++) {
1031
struct exfat_dentry *ep;
1032
struct buffer_head *bh;
1033
1034
if (rewind && dentry == end_eidx)
1035
goto not_found;
1036
1037
ep = exfat_get_dentry(sb, &clu, i, &bh);
1038
if (!ep)
1039
return -EIO;
1040
1041
entry_type = exfat_get_entry_type(ep);
1042
1043
if (entry_type == TYPE_UNUSED ||
1044
entry_type == TYPE_DELETED) {
1045
step = DIRENT_STEP_FILE;
1046
1047
exfat_set_empty_hint(ei, &candi_empty, &clu,
1048
dentry, num_entries,
1049
entry_type);
1050
1051
brelse(bh);
1052
if (entry_type == TYPE_UNUSED)
1053
goto not_found;
1054
continue;
1055
}
1056
1057
exfat_reset_empty_hint(&candi_empty);
1058
1059
if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1060
step = DIRENT_STEP_FILE;
1061
hint_opt->clu = clu.dir;
1062
hint_opt->eidx = i;
1063
num_ext = ep->dentry.file.num_ext;
1064
step = DIRENT_STEP_STRM;
1065
brelse(bh);
1066
continue;
1067
}
1068
1069
if (entry_type == TYPE_STREAM) {
1070
u16 name_hash;
1071
1072
if (step != DIRENT_STEP_STRM) {
1073
step = DIRENT_STEP_FILE;
1074
brelse(bh);
1075
continue;
1076
}
1077
step = DIRENT_STEP_FILE;
1078
name_hash = le16_to_cpu(
1079
ep->dentry.stream.name_hash);
1080
if (p_uniname->name_hash == name_hash &&
1081
p_uniname->name_len ==
1082
ep->dentry.stream.name_len) {
1083
step = DIRENT_STEP_NAME;
1084
order = 1;
1085
name_len = 0;
1086
}
1087
brelse(bh);
1088
continue;
1089
}
1090
1091
brelse(bh);
1092
if (entry_type == TYPE_EXTEND) {
1093
unsigned short entry_uniname[16], unichar;
1094
1095
if (step != DIRENT_STEP_NAME ||
1096
name_len >= MAX_NAME_LENGTH) {
1097
step = DIRENT_STEP_FILE;
1098
continue;
1099
}
1100
1101
if (++order == 2)
1102
uniname = p_uniname->name;
1103
else
1104
uniname += EXFAT_FILE_NAME_LEN;
1105
1106
len = exfat_extract_uni_name(ep, entry_uniname);
1107
name_len += len;
1108
1109
unichar = *(uniname+len);
1110
*(uniname+len) = 0x0;
1111
1112
if (exfat_uniname_ncmp(sb, uniname,
1113
entry_uniname, len)) {
1114
step = DIRENT_STEP_FILE;
1115
} else if (p_uniname->name_len == name_len) {
1116
if (order == num_ext)
1117
goto found;
1118
step = DIRENT_STEP_SECD;
1119
}
1120
1121
*(uniname+len) = unichar;
1122
continue;
1123
}
1124
1125
if (entry_type &
1126
(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1127
if (step == DIRENT_STEP_SECD) {
1128
if (++order == num_ext)
1129
goto found;
1130
continue;
1131
}
1132
}
1133
step = DIRENT_STEP_FILE;
1134
}
1135
1136
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1137
if (--clu.size > 0)
1138
clu.dir++;
1139
else
1140
clu.dir = EXFAT_EOF_CLUSTER;
1141
} else {
1142
if (exfat_get_next_cluster(sb, &clu.dir))
1143
return -EIO;
1144
1145
/* break if the cluster chain includes a loop */
1146
if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
1147
goto not_found;
1148
}
1149
}
1150
1151
not_found:
1152
/*
1153
* We started at not 0 index,so we should try to find target
1154
* from 0 index to the index we started at.
1155
*/
1156
if (!rewind && end_eidx) {
1157
rewind = 1;
1158
dentry = 0;
1159
clu.dir = p_dir->dir;
1160
goto rewind;
1161
}
1162
1163
/*
1164
* set the EXFAT_EOF_CLUSTER flag to avoid search
1165
* from the beginning again when allocated a new cluster
1166
*/
1167
if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1168
ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1169
ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1170
ei->hint_femp.count = 0;
1171
}
1172
1173
/* initialized hint_stat */
1174
hint_stat->clu = p_dir->dir;
1175
hint_stat->eidx = 0;
1176
return -ENOENT;
1177
1178
found:
1179
/* next dentry we'll find is out of this cluster */
1180
if (!((dentry + 1) & (dentries_per_clu - 1))) {
1181
int ret = 0;
1182
1183
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1184
if (--clu.size > 0)
1185
clu.dir++;
1186
else
1187
clu.dir = EXFAT_EOF_CLUSTER;
1188
} else {
1189
ret = exfat_get_next_cluster(sb, &clu.dir);
1190
}
1191
1192
if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1193
/* just initialized hint_stat */
1194
hint_stat->clu = p_dir->dir;
1195
hint_stat->eidx = 0;
1196
return (dentry - num_ext);
1197
}
1198
}
1199
1200
hint_stat->clu = clu.dir;
1201
hint_stat->eidx = dentry + 1;
1202
return dentry - num_ext;
1203
}
1204
1205
int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1206
{
1207
int i, count = 0;
1208
int dentries_per_clu;
1209
unsigned int entry_type;
1210
unsigned int clu_count = 0;
1211
struct exfat_chain clu;
1212
struct exfat_dentry *ep;
1213
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1214
struct buffer_head *bh;
1215
1216
dentries_per_clu = sbi->dentries_per_clu;
1217
1218
exfat_chain_dup(&clu, p_dir);
1219
1220
while (clu.dir != EXFAT_EOF_CLUSTER) {
1221
for (i = 0; i < dentries_per_clu; i++) {
1222
ep = exfat_get_dentry(sb, &clu, i, &bh);
1223
if (!ep)
1224
return -EIO;
1225
entry_type = exfat_get_entry_type(ep);
1226
brelse(bh);
1227
1228
if (entry_type == TYPE_UNUSED)
1229
return count;
1230
if (entry_type != TYPE_DIR)
1231
continue;
1232
count++;
1233
}
1234
1235
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1236
if (--clu.size > 0)
1237
clu.dir++;
1238
else
1239
clu.dir = EXFAT_EOF_CLUSTER;
1240
} else {
1241
if (exfat_get_next_cluster(sb, &(clu.dir)))
1242
return -EIO;
1243
1244
if (unlikely(++clu_count > sbi->used_clusters)) {
1245
exfat_fs_error(sb, "FAT or bitmap is corrupted");
1246
return -EIO;
1247
}
1248
1249
}
1250
}
1251
1252
return count;
1253
}
1254
1255
static int exfat_get_volume_label_dentry(struct super_block *sb,
1256
struct exfat_entry_set_cache *es)
1257
{
1258
int i;
1259
int dentry = 0;
1260
unsigned int type;
1261
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1262
struct exfat_hint_femp hint_femp;
1263
struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
1264
struct exfat_chain clu;
1265
struct exfat_dentry *ep;
1266
struct buffer_head *bh;
1267
1268
hint_femp.eidx = EXFAT_HINT_NONE;
1269
exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
1270
1271
while (clu.dir != EXFAT_EOF_CLUSTER) {
1272
for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
1273
ep = exfat_get_dentry(sb, &clu, i, &bh);
1274
if (!ep)
1275
return -EIO;
1276
1277
type = exfat_get_entry_type(ep);
1278
if (hint_femp.eidx == EXFAT_HINT_NONE) {
1279
if (type == TYPE_DELETED || type == TYPE_UNUSED) {
1280
hint_femp.cur = clu;
1281
hint_femp.eidx = dentry;
1282
hint_femp.count = 1;
1283
}
1284
}
1285
1286
if (type == TYPE_UNUSED) {
1287
brelse(bh);
1288
goto not_found;
1289
}
1290
1291
if (type != TYPE_VOLUME) {
1292
brelse(bh);
1293
continue;
1294
}
1295
1296
memset(es, 0, sizeof(*es));
1297
es->sb = sb;
1298
es->bh = es->__bh;
1299
es->bh[0] = bh;
1300
es->num_bh = 1;
1301
es->start_off = EXFAT_DEN_TO_B(i) % sb->s_blocksize;
1302
1303
return 0;
1304
}
1305
1306
if (exfat_get_next_cluster(sb, &(clu.dir)))
1307
return -EIO;
1308
}
1309
1310
not_found:
1311
if (hint_femp.eidx == EXFAT_HINT_NONE) {
1312
hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1313
hint_femp.eidx = dentry;
1314
hint_femp.count = 0;
1315
}
1316
1317
ei->hint_femp = hint_femp;
1318
1319
return -ENOENT;
1320
}
1321
1322
int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
1323
{
1324
int ret, i;
1325
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1326
struct exfat_entry_set_cache es;
1327
struct exfat_dentry *ep;
1328
1329
mutex_lock(&sbi->s_lock);
1330
1331
memset(label_out, 0, sizeof(*label_out));
1332
ret = exfat_get_volume_label_dentry(sb, &es);
1333
if (ret < 0) {
1334
/*
1335
* ENOENT signifies that a volume label dentry doesn't exist
1336
* We will treat this as an empty volume label and not fail.
1337
*/
1338
if (ret == -ENOENT)
1339
ret = 0;
1340
1341
goto unlock;
1342
}
1343
1344
ep = exfat_get_dentry_cached(&es, 0);
1345
label_out->name_len = ep->dentry.volume_label.char_count;
1346
if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
1347
ret = -EIO;
1348
exfat_put_dentry_set(&es, false);
1349
goto unlock;
1350
}
1351
1352
for (i = 0; i < label_out->name_len; i++)
1353
label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);
1354
1355
exfat_put_dentry_set(&es, false);
1356
unlock:
1357
mutex_unlock(&sbi->s_lock);
1358
return ret;
1359
}
1360
1361
int exfat_write_volume_label(struct super_block *sb,
1362
struct exfat_uni_name *label)
1363
{
1364
int ret, i;
1365
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1366
struct inode *root_inode = sb->s_root->d_inode;
1367
struct exfat_entry_set_cache es;
1368
struct exfat_chain clu;
1369
struct exfat_dentry *ep;
1370
1371
if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
1372
return -EINVAL;
1373
1374
mutex_lock(&sbi->s_lock);
1375
1376
ret = exfat_get_volume_label_dentry(sb, &es);
1377
if (ret == -ENOENT) {
1378
if (label->name_len == 0) {
1379
/* No volume label dentry, no need to clear */
1380
ret = 0;
1381
goto unlock;
1382
}
1383
1384
ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
1385
}
1386
1387
if (ret < 0)
1388
goto unlock;
1389
1390
ep = exfat_get_dentry_cached(&es, 0);
1391
1392
if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
1393
/* volume label had been cleared */
1394
exfat_put_dentry_set(&es, 0);
1395
goto unlock;
1396
}
1397
1398
memset(ep, 0, sizeof(*ep));
1399
ep->type = EXFAT_VOLUME;
1400
1401
for (i = 0; i < label->name_len; i++)
1402
ep->dentry.volume_label.volume_label[i] =
1403
cpu_to_le16(label->name[i]);
1404
1405
ep->dentry.volume_label.char_count = label->name_len;
1406
es.modified = true;
1407
1408
ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));
1409
1410
unlock:
1411
mutex_unlock(&sbi->s_lock);
1412
return ret;
1413
}
1414
1415