Skip to content

Commit 0e85284

Browse files
authored
Merge pull request TheScarastic#20 from namjaejeon/exfat-next
Exfat next
2 parents d400592 + a52427b commit 0e85284

11 files changed

Lines changed: 352 additions & 53 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ script:
148148
- sudo ./check generic/248
149149
- sudo ./check generic/249
150150
- sudo ./check generic/257
151+
- sudo ./check generic/260
151152
- sudo ./check generic/263
152153
- sudo ./check generic/285
153154
- sudo ./check generic/286
155+
- sudo ./check generic/288
154156
- sudo ./check generic/308
155157
- sudo ./check generic/309
156158
- sudo ./check generic/310

balloc.c

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
44
*/
55

6+
#include <linux/version.h>
67
#include <linux/blkdev.h>
78
#include <linux/slab.h>
89
#include <linux/buffer_head.h>
10+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
11+
#include <linux/sched/signal.h>
12+
#endif
913

1014
#include "exfat_raw.h"
1115
#include "exfat_fs.h"
@@ -141,11 +145,7 @@ void exfat_free_bitmap(struct exfat_sb_info *sbi)
141145
kfree(sbi->vol_amap);
142146
}
143147

144-
/*
145-
* If the value of "clu" is 0, it means cluster 2 which is the first cluster of
146-
* the cluster heap.
147-
*/
148-
int exfat_set_bitmap(struct inode *inode, unsigned int clu)
148+
int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
149149
{
150150
int i, b;
151151
unsigned int ent_idx;
@@ -158,15 +158,11 @@ int exfat_set_bitmap(struct inode *inode, unsigned int clu)
158158
b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
159159

160160
set_bit_le(b, sbi->vol_amap[i]->b_data);
161-
exfat_update_bh(sbi->vol_amap[i], IS_DIRSYNC(inode));
161+
exfat_update_bh(sbi->vol_amap[i], sync);
162162
return 0;
163163
}
164164

165-
/*
166-
* If the value of "clu" is 0, it means cluster 2 which is the first cluster of
167-
* the cluster heap.
168-
*/
169-
void exfat_clear_bitmap(struct inode *inode, unsigned int clu)
165+
void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
170166
{
171167
int i, b;
172168
unsigned int ent_idx;
@@ -180,14 +176,13 @@ void exfat_clear_bitmap(struct inode *inode, unsigned int clu)
180176
b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
181177

182178
clear_bit_le(b, sbi->vol_amap[i]->b_data);
183-
exfat_update_bh(sbi->vol_amap[i], IS_DIRSYNC(inode));
179+
exfat_update_bh(sbi->vol_amap[i], sync);
184180

185181
if (opts->discard) {
186182
int ret_discard;
187183

188184
ret_discard = sb_issue_discard(sb,
189-
exfat_cluster_to_sector(sbi, clu +
190-
EXFAT_RESERVED_CLUSTERS),
185+
exfat_cluster_to_sector(sbi, clu),
191186
(1 << sbi->sect_per_clus_bits), GFP_NOFS, 0);
192187

193188
if (ret_discard == -EOPNOTSUPP) {
@@ -273,3 +268,83 @@ int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
273268
*ret_count = count;
274269
return 0;
275270
}
271+
272+
int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
273+
{
274+
unsigned int trim_begin, trim_end, count, next_free_clu;
275+
u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
276+
struct super_block *sb = inode->i_sb;
277+
struct exfat_sb_info *sbi = EXFAT_SB(sb);
278+
int err = 0;
279+
280+
clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
281+
EXFAT_FIRST_CLUSTER);
282+
clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
283+
trim_minlen = range->minlen >> sbi->cluster_size_bits;
284+
285+
if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
286+
return -EINVAL;
287+
288+
if (clu_end >= sbi->num_clusters)
289+
clu_end = sbi->num_clusters - 1;
290+
291+
mutex_lock(&sbi->bitmap_lock);
292+
293+
trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
294+
if (trim_begin == EXFAT_EOF_CLUSTER)
295+
goto unlock;
296+
297+
next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
298+
if (next_free_clu == EXFAT_EOF_CLUSTER)
299+
goto unlock;
300+
301+
do {
302+
if (next_free_clu == trim_end + 1) {
303+
/* extend trim range for continuous free cluster */
304+
trim_end++;
305+
} else {
306+
/* trim current range if it's larger than trim_minlen */
307+
count = trim_end - trim_begin + 1;
308+
if (count >= trim_minlen) {
309+
err = sb_issue_discard(sb,
310+
exfat_cluster_to_sector(sbi, trim_begin),
311+
count * sbi->sect_per_clus, GFP_NOFS, 0);
312+
if (err)
313+
goto unlock;
314+
315+
trimmed_total += count;
316+
}
317+
318+
/* set next start point of the free hole */
319+
trim_begin = trim_end = next_free_clu;
320+
}
321+
322+
if (next_free_clu >= clu_end)
323+
break;
324+
325+
if (fatal_signal_pending(current)) {
326+
err = -ERESTARTSYS;
327+
goto unlock;
328+
}
329+
330+
next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
331+
} while (next_free_clu != EXFAT_EOF_CLUSTER &&
332+
next_free_clu > trim_end);
333+
334+
/* try to trim remainder */
335+
count = trim_end - trim_begin + 1;
336+
if (count >= trim_minlen) {
337+
err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
338+
count * sbi->sect_per_clus, GFP_NOFS, 0);
339+
if (err)
340+
goto unlock;
341+
342+
trimmed_total += count;
343+
}
344+
345+
unlock:
346+
mutex_unlock(&sbi->bitmap_lock);
347+
range->len = trimmed_total << sbi->cluster_size_bits;
348+
349+
return err;
350+
}

dir.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <linux/version.h>
77
#include <linux/slab.h>
8+
#include <linux/compat.h>
89
#include <linux/bio.h>
910
#include <linux/buffer_head.h>
1011

@@ -147,7 +148,7 @@ static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_ent
147148
0);
148149

149150
*uni_name.name = 0x0;
150-
exfat_get_uniname_from_ext_entry(sb, &dir, dentry,
151+
exfat_get_uniname_from_ext_entry(sb, &clu, i,
151152
uni_name.name);
152153
exfat_utf16_to_nls(sb, &uni_name,
153154
dir_entry->namebuf.lfn,
@@ -307,6 +308,10 @@ const struct file_operations exfat_dir_operations = {
307308
.llseek = generic_file_llseek,
308309
.read = generic_read_dir,
309310
.iterate = exfat_iterate,
311+
.unlocked_ioctl = exfat_ioctl,
312+
#ifdef CONFIG_COMPAT
313+
.compat_ioctl = exfat_compat_ioctl,
314+
#endif
310315
.fsync = exfat_file_fsync,
311316
};
312317

@@ -316,7 +321,7 @@ int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
316321

317322
exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
318323

319-
ret = exfat_alloc_cluster(inode, 1, clu);
324+
ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
320325
if (ret)
321326
return ret;
322327

@@ -917,14 +922,19 @@ enum {
917922
};
918923

919924
/*
920-
* return values:
921-
* >= 0 : return dir entiry position with the name in dir
922-
* -ENOENT : entry with the name does not exist
923-
* -EIO : I/O error
925+
* @ei: inode info of parent directory
926+
* @p_dir: directory structure of parent directory
927+
* @num_entries:entry size of p_uniname
928+
* @hint_opt: If p_uniname is found, filled with optimized dir/entry
929+
* for traversing cluster chain.
930+
* @return:
931+
* >= 0: file directory entry position where the name exists
932+
* -ENOENT: entry with the name does not exist
933+
* -EIO: I/O error
924934
*/
925935
int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
926936
struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
927-
int num_entries, unsigned int type)
937+
int num_entries, unsigned int type, struct exfat_hint *hint_opt)
928938
{
929939
int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
930940
int order, step, name_len = 0;
@@ -1001,6 +1011,8 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
10011011

10021012
if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
10031013
step = DIRENT_STEP_FILE;
1014+
hint_opt->clu = clu.dir;
1015+
hint_opt->eidx = i;
10041016
if (type == TYPE_ALL || type == entry_type) {
10051017
num_ext = ep->dentry.file.num_ext;
10061018
step = DIRENT_STEP_STRM;

exfat_fs.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <linux/ratelimit.h>
1212
#include <linux/nls.h>
1313

14-
#define EXFAT_VERSION "5.11.1"
14+
#define EXFAT_VERSION "5.12.3"
1515

1616
#define EXFAT_SUPER_MAGIC 0x2011BAB0UL
1717
#define EXFAT_ROOT_INO 1
@@ -247,6 +247,7 @@ struct exfat_sb_info {
247247
unsigned int used_clusters; /* number of used clusters */
248248

249249
struct mutex s_lock; /* superblock lock */
250+
struct mutex bitmap_lock; /* bitmap lock */
250251
struct exfat_mount_options options;
251252
struct nls_table *nls_io; /* Charset used for input and display */
252253
struct ratelimit_state ratelimit;
@@ -401,7 +402,7 @@ int exfat_clear_volume_dirty(struct super_block *sb);
401402
#define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu)
402403

403404
int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
404-
struct exfat_chain *p_chain);
405+
struct exfat_chain *p_chain, bool sync_bmap);
405406
int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
406407
int exfat_ent_get(struct super_block *sb, unsigned int loc,
407408
unsigned int *content);
@@ -420,15 +421,24 @@ int exfat_count_num_clusters(struct super_block *sb,
420421
/* balloc.c */
421422
int exfat_load_bitmap(struct super_block *sb);
422423
void exfat_free_bitmap(struct exfat_sb_info *sbi);
423-
int exfat_set_bitmap(struct inode *inode, unsigned int clu);
424-
void exfat_clear_bitmap(struct inode *inode, unsigned int clu);
424+
int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync);
425+
void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync);
425426
unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu);
426427
int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count);
428+
int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);
427429

428430
/* file.c */
429431
extern const struct file_operations exfat_file_operations;
430432
int __exfat_truncate(struct inode *inode, loff_t new_size);
431433
void exfat_truncate(struct inode *inode, loff_t size);
434+
435+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
436+
int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
437+
struct iattr *attr);
438+
int exfat_getattr(struct user_namespace *mnt_userns, const struct path *path,
439+
struct kstat *stat, unsigned int request_mask,
440+
unsigned int query_flags);
441+
#else
432442
int exfat_setattr(struct dentry *dentry, struct iattr *attr);
433443
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
434444
int exfat_getattr(const struct path *path, struct kstat *stat,
@@ -437,7 +447,11 @@ int exfat_getattr(const struct path *path, struct kstat *stat,
437447
int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry,
438448
struct kstat *stat);
439449
#endif
450+
#endif
440451
int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
452+
long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
453+
long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
454+
unsigned long arg);
441455

442456

443457
/* namei.c */
@@ -469,7 +483,7 @@ void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es);
469483
int exfat_calc_num_entries(struct exfat_uni_name *p_uniname);
470484
int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
471485
struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
472-
int num_entries, unsigned int type);
486+
int num_entries, unsigned int type, struct exfat_hint *hint_opt);
473487
int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu);
474488
int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
475489
int entry, sector_t *sector, int *offset);

exfat_raw.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777

7878
#define EXFAT_FILE_NAME_LEN 15
7979

80+
#define EXFAT_MIN_SECT_SIZE_BITS 9
81+
#define EXFAT_MAX_SECT_SIZE_BITS 12
82+
#define EXFAT_MAX_SECT_PER_CLUS_BITS(x) (25 - (x)->sect_size_bits)
83+
8084
/* EXFAT: Main and Backup Boot Sector (512 bytes) */
8185
struct boot_sector {
8286
__u8 jmp_boot[BOOTSEC_JUMP_BOOT_LEN];

0 commit comments

Comments
 (0)