Skip to content

Commit f0712c2

Browse files
committed
Merge tag 'exfat-for-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat
Pull exfat updates from Namjae Jeon: - Add support for FS_IOC_{GET,SET}FSLABEL ioctl - Two small clean-up patches - Optimizes allocation bitmap loading time on large partitions with small cluster sizes - Allow changes for discard, zero_size_dir, and errors options via remount - Validate that the clusters used for the allocation bitmap are correctly marked as in-use during mount, preventing potential data corruption from reallocating the bitmap's own space - Uses ratelimit to avoid too many error prints on I/O error path * tag 'exfat-for-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat: exfat: Add support for FS_IOC_{GET,SET}FSLABEL exfat: combine iocharset and utf8 option setup exfat: support modifying mount options via remount exfat: optimize allocation bitmap loading time exfat: Remove unnecessary parentheses exfat: drop redundant conversion to bool exfat: validate cluster allocation bits of the allocation bitmap exfat: limit log print for IO error
2 parents f2327dc + d01579d commit f0712c2

File tree

10 files changed

+360
-35
lines changed

10 files changed

+360
-35
lines changed

fs/exfat/balloc.c

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/slab.h>
88
#include <linux/bitmap.h>
99
#include <linux/buffer_head.h>
10+
#include <linux/backing-dev.h>
1011

1112
#include "exfat_raw.h"
1213
#include "exfat_fs.h"
@@ -26,13 +27,58 @@
2627
/*
2728
* Allocation Bitmap Management Functions
2829
*/
30+
static bool exfat_test_bitmap_range(struct super_block *sb, unsigned int clu,
31+
unsigned int count)
32+
{
33+
struct exfat_sb_info *sbi = EXFAT_SB(sb);
34+
unsigned int start = clu;
35+
unsigned int end = clu + count;
36+
unsigned int ent_idx, i, b;
37+
unsigned int bit_offset, bits_to_check;
38+
__le_long *bitmap_le;
39+
unsigned long mask, word;
40+
41+
if (!is_valid_cluster(sbi, start) || !is_valid_cluster(sbi, end - 1))
42+
return false;
43+
44+
while (start < end) {
45+
ent_idx = CLUSTER_TO_BITMAP_ENT(start);
46+
i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
47+
b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
48+
49+
bitmap_le = (__le_long *)sbi->vol_amap[i]->b_data;
50+
51+
/* Calculate how many bits we can check in the current word */
52+
bit_offset = b % BITS_PER_LONG;
53+
bits_to_check = min(end - start,
54+
(unsigned int)(BITS_PER_LONG - bit_offset));
55+
56+
/* Create a bitmask for the range of bits to check */
57+
if (bits_to_check >= BITS_PER_LONG)
58+
mask = ~0UL;
59+
else
60+
mask = ((1UL << bits_to_check) - 1) << bit_offset;
61+
word = lel_to_cpu(bitmap_le[b / BITS_PER_LONG]);
62+
63+
/* Check if all bits in the mask are set */
64+
if ((word & mask) != mask)
65+
return false;
66+
67+
start += bits_to_check;
68+
}
69+
70+
return true;
71+
}
72+
2973
static int exfat_allocate_bitmap(struct super_block *sb,
3074
struct exfat_dentry *ep)
3175
{
3276
struct exfat_sb_info *sbi = EXFAT_SB(sb);
77+
struct blk_plug plug;
3378
long long map_size;
34-
unsigned int i, need_map_size;
79+
unsigned int i, j, need_map_size;
3580
sector_t sector;
81+
unsigned int max_ra_count;
3682

3783
sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
3884
map_size = le64_to_cpu(ep->dentry.bitmap.size);
@@ -56,22 +102,37 @@ static int exfat_allocate_bitmap(struct super_block *sb,
56102
return -ENOMEM;
57103

58104
sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
105+
max_ra_count = min(sb->s_bdi->ra_pages, sb->s_bdi->io_pages) <<
106+
(PAGE_SHIFT - sb->s_blocksize_bits);
59107
for (i = 0; i < sbi->map_sectors; i++) {
60-
sbi->vol_amap[i] = sb_bread(sb, sector + i);
61-
if (!sbi->vol_amap[i]) {
62-
/* release all buffers and free vol_amap */
63-
int j = 0;
64-
65-
while (j < i)
66-
brelse(sbi->vol_amap[j++]);
67-
68-
kvfree(sbi->vol_amap);
69-
sbi->vol_amap = NULL;
70-
return -EIO;
108+
/* Trigger the next readahead in advance. */
109+
if (0 == (i % max_ra_count)) {
110+
blk_start_plug(&plug);
111+
for (j = i; j < min(max_ra_count, sbi->map_sectors - i) + i; j++)
112+
sb_breadahead(sb, sector + j);
113+
blk_finish_plug(&plug);
71114
}
115+
116+
sbi->vol_amap[i] = sb_bread(sb, sector + i);
117+
if (!sbi->vol_amap[i])
118+
goto err_out;
72119
}
73120

121+
if (exfat_test_bitmap_range(sb, sbi->map_clu,
122+
EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) == false)
123+
goto err_out;
124+
74125
return 0;
126+
127+
err_out:
128+
j = 0;
129+
/* release all buffers and free vol_amap */
130+
while (j < i)
131+
brelse(sbi->vol_amap[j++]);
132+
133+
kvfree(sbi->vol_amap);
134+
sbi->vol_amap = NULL;
135+
return -EIO;
75136
}
76137

77138
int exfat_load_bitmap(struct super_block *sb)

fs/exfat/dir.c

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,3 +1244,163 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
12441244

12451245
return count;
12461246
}
1247+
1248+
static int exfat_get_volume_label_dentry(struct super_block *sb,
1249+
struct exfat_entry_set_cache *es)
1250+
{
1251+
int i;
1252+
int dentry = 0;
1253+
unsigned int type;
1254+
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1255+
struct exfat_hint_femp hint_femp;
1256+
struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
1257+
struct exfat_chain clu;
1258+
struct exfat_dentry *ep;
1259+
struct buffer_head *bh;
1260+
1261+
hint_femp.eidx = EXFAT_HINT_NONE;
1262+
exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
1263+
1264+
while (clu.dir != EXFAT_EOF_CLUSTER) {
1265+
for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
1266+
ep = exfat_get_dentry(sb, &clu, i, &bh);
1267+
if (!ep)
1268+
return -EIO;
1269+
1270+
type = exfat_get_entry_type(ep);
1271+
if (hint_femp.eidx == EXFAT_HINT_NONE) {
1272+
if (type == TYPE_DELETED || type == TYPE_UNUSED) {
1273+
hint_femp.cur = clu;
1274+
hint_femp.eidx = dentry;
1275+
hint_femp.count = 1;
1276+
}
1277+
}
1278+
1279+
if (type == TYPE_UNUSED) {
1280+
brelse(bh);
1281+
goto not_found;
1282+
}
1283+
1284+
if (type != TYPE_VOLUME) {
1285+
brelse(bh);
1286+
continue;
1287+
}
1288+
1289+
memset(es, 0, sizeof(*es));
1290+
es->sb = sb;
1291+
es->bh = es->__bh;
1292+
es->bh[0] = bh;
1293+
es->num_bh = 1;
1294+
es->start_off = EXFAT_DEN_TO_B(i) % sb->s_blocksize;
1295+
1296+
return 0;
1297+
}
1298+
1299+
if (exfat_get_next_cluster(sb, &(clu.dir)))
1300+
return -EIO;
1301+
}
1302+
1303+
not_found:
1304+
if (hint_femp.eidx == EXFAT_HINT_NONE) {
1305+
hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1306+
hint_femp.eidx = dentry;
1307+
hint_femp.count = 0;
1308+
}
1309+
1310+
ei->hint_femp = hint_femp;
1311+
1312+
return -ENOENT;
1313+
}
1314+
1315+
int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
1316+
{
1317+
int ret, i;
1318+
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1319+
struct exfat_entry_set_cache es;
1320+
struct exfat_dentry *ep;
1321+
1322+
mutex_lock(&sbi->s_lock);
1323+
1324+
memset(label_out, 0, sizeof(*label_out));
1325+
ret = exfat_get_volume_label_dentry(sb, &es);
1326+
if (ret < 0) {
1327+
/*
1328+
* ENOENT signifies that a volume label dentry doesn't exist
1329+
* We will treat this as an empty volume label and not fail.
1330+
*/
1331+
if (ret == -ENOENT)
1332+
ret = 0;
1333+
1334+
goto unlock;
1335+
}
1336+
1337+
ep = exfat_get_dentry_cached(&es, 0);
1338+
label_out->name_len = ep->dentry.volume_label.char_count;
1339+
if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
1340+
ret = -EIO;
1341+
exfat_put_dentry_set(&es, false);
1342+
goto unlock;
1343+
}
1344+
1345+
for (i = 0; i < label_out->name_len; i++)
1346+
label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);
1347+
1348+
exfat_put_dentry_set(&es, false);
1349+
unlock:
1350+
mutex_unlock(&sbi->s_lock);
1351+
return ret;
1352+
}
1353+
1354+
int exfat_write_volume_label(struct super_block *sb,
1355+
struct exfat_uni_name *label)
1356+
{
1357+
int ret, i;
1358+
struct exfat_sb_info *sbi = EXFAT_SB(sb);
1359+
struct inode *root_inode = sb->s_root->d_inode;
1360+
struct exfat_entry_set_cache es;
1361+
struct exfat_chain clu;
1362+
struct exfat_dentry *ep;
1363+
1364+
if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
1365+
return -EINVAL;
1366+
1367+
mutex_lock(&sbi->s_lock);
1368+
1369+
ret = exfat_get_volume_label_dentry(sb, &es);
1370+
if (ret == -ENOENT) {
1371+
if (label->name_len == 0) {
1372+
/* No volume label dentry, no need to clear */
1373+
ret = 0;
1374+
goto unlock;
1375+
}
1376+
1377+
ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
1378+
}
1379+
1380+
if (ret < 0)
1381+
goto unlock;
1382+
1383+
ep = exfat_get_dentry_cached(&es, 0);
1384+
1385+
if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
1386+
/* volume label had been cleared */
1387+
exfat_put_dentry_set(&es, 0);
1388+
goto unlock;
1389+
}
1390+
1391+
memset(ep, 0, sizeof(*ep));
1392+
ep->type = EXFAT_VOLUME;
1393+
1394+
for (i = 0; i < label->name_len; i++)
1395+
ep->dentry.volume_label.volume_label[i] =
1396+
cpu_to_le16(label->name[i]);
1397+
1398+
ep->dentry.volume_label.char_count = label->name_len;
1399+
es.modified = true;
1400+
1401+
ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));
1402+
1403+
unlock:
1404+
mutex_unlock(&sbi->s_lock);
1405+
return ret;
1406+
}

fs/exfat/exfat_fs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ int exfat_force_shutdown(struct super_block *sb, u32 flags);
477477
/* namei.c */
478478
extern const struct dentry_operations exfat_dentry_ops;
479479
extern const struct dentry_operations exfat_utf8_dentry_ops;
480+
int exfat_find_empty_entry(struct inode *inode,
481+
struct exfat_chain *p_dir, int num_entries,
482+
struct exfat_entry_set_cache *es);
480483

481484
/* cache.c */
482485
int exfat_cache_init(void);
@@ -517,6 +520,10 @@ int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
517520
unsigned int num_entries);
518521
int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync);
519522
int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
523+
int exfat_read_volume_label(struct super_block *sb,
524+
struct exfat_uni_name *label_out);
525+
int exfat_write_volume_label(struct super_block *sb,
526+
struct exfat_uni_name *label);
520527

521528
/* inode.c */
522529
extern const struct inode_operations exfat_file_inode_operations;

fs/exfat/exfat_raw.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#define BOOTSEC_OLDBPB_LEN 53
8181

8282
#define EXFAT_FILE_NAME_LEN 15
83+
#define EXFAT_VOLUME_LABEL_LEN 11
8384

8485
#define EXFAT_MIN_SECT_SIZE_BITS 9
8586
#define EXFAT_MAX_SECT_SIZE_BITS 12
@@ -159,6 +160,11 @@ struct exfat_dentry {
159160
__le32 start_clu;
160161
__le64 size;
161162
} __packed upcase; /* up-case table directory entry */
163+
struct {
164+
__u8 char_count;
165+
__le16 volume_label[EXFAT_VOLUME_LABEL_LEN];
166+
__u8 reserved[8];
167+
} __packed volume_label; /* volume label directory entry */
162168
struct {
163169
__u8 flags;
164170
__u8 vendor_guid[16];

fs/exfat/fatent.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,36 @@ int exfat_ent_get(struct super_block *sb, unsigned int loc,
8989
int err;
9090

9191
if (!is_valid_cluster(sbi, loc)) {
92-
exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
92+
exfat_fs_error_ratelimit(sb,
93+
"invalid access to FAT (entry 0x%08x)",
9394
loc);
9495
return -EIO;
9596
}
9697

9798
err = __exfat_ent_get(sb, loc, content);
9899
if (err) {
99-
exfat_fs_error(sb,
100+
exfat_fs_error_ratelimit(sb,
100101
"failed to access to FAT (entry 0x%08x, err:%d)",
101102
loc, err);
102103
return err;
103104
}
104105

105106
if (*content == EXFAT_FREE_CLUSTER) {
106-
exfat_fs_error(sb,
107+
exfat_fs_error_ratelimit(sb,
107108
"invalid access to FAT free cluster (entry 0x%08x)",
108109
loc);
109110
return -EIO;
110111
}
111112

112113
if (*content == EXFAT_BAD_CLUSTER) {
113-
exfat_fs_error(sb,
114+
exfat_fs_error_ratelimit(sb,
114115
"invalid access to FAT bad cluster (entry 0x%08x)",
115116
loc);
116117
return -EIO;
117118
}
118119

119120
if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
120-
exfat_fs_error(sb,
121+
exfat_fs_error_ratelimit(sb,
121122
"invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
122123
loc, *content);
123124
return -EIO;

0 commit comments

Comments
 (0)