Commit f0712c20 authored by Linus Torvalds's avatar Linus Torvalds
Browse files
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
parents f2327dc8 d01579d5
Loading
Loading
Loading
Loading
+73 −12
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include <linux/slab.h>
#include <linux/bitmap.h>
#include <linux/buffer_head.h>
#include <linux/backing-dev.h>

#include "exfat_raw.h"
#include "exfat_fs.h"
@@ -26,13 +27,58 @@
/*
 *  Allocation Bitmap Management Functions
 */
static bool exfat_test_bitmap_range(struct super_block *sb, unsigned int clu,
		unsigned int count)
{
	struct exfat_sb_info *sbi = EXFAT_SB(sb);
	unsigned int start = clu;
	unsigned int end = clu + count;
	unsigned int ent_idx, i, b;
	unsigned int bit_offset, bits_to_check;
	__le_long *bitmap_le;
	unsigned long mask, word;

	if (!is_valid_cluster(sbi, start) || !is_valid_cluster(sbi, end - 1))
		return false;

	while (start < end) {
		ent_idx = CLUSTER_TO_BITMAP_ENT(start);
		i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
		b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);

		bitmap_le = (__le_long *)sbi->vol_amap[i]->b_data;

		/* Calculate how many bits we can check in the current word */
		bit_offset = b % BITS_PER_LONG;
		bits_to_check = min(end - start,
				    (unsigned int)(BITS_PER_LONG - bit_offset));

		/* Create a bitmask for the range of bits to check */
		if (bits_to_check >= BITS_PER_LONG)
			mask = ~0UL;
		else
			mask = ((1UL << bits_to_check) - 1) << bit_offset;
		word = lel_to_cpu(bitmap_le[b / BITS_PER_LONG]);

		/* Check if all bits in the mask are set */
		if ((word & mask) != mask)
			return false;

		start += bits_to_check;
	}

	return true;
}

static int exfat_allocate_bitmap(struct super_block *sb,
		struct exfat_dentry *ep)
{
	struct exfat_sb_info *sbi = EXFAT_SB(sb);
	struct blk_plug plug;
	long long map_size;
	unsigned int i, need_map_size;
	unsigned int i, j, need_map_size;
	sector_t sector;
	unsigned int max_ra_count;

	sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
	map_size = le64_to_cpu(ep->dentry.bitmap.size);
@@ -56,12 +102,31 @@ static int exfat_allocate_bitmap(struct super_block *sb,
		return -ENOMEM;

	sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
	max_ra_count = min(sb->s_bdi->ra_pages, sb->s_bdi->io_pages) <<
		(PAGE_SHIFT - sb->s_blocksize_bits);
	for (i = 0; i < sbi->map_sectors; i++) {
		/* Trigger the next readahead in advance. */
		if (0 == (i % max_ra_count)) {
			blk_start_plug(&plug);
			for (j = i; j < min(max_ra_count, sbi->map_sectors - i) + i; j++)
				sb_breadahead(sb, sector + j);
			blk_finish_plug(&plug);
		}

		sbi->vol_amap[i] = sb_bread(sb, sector + i);
		if (!sbi->vol_amap[i]) {
			/* release all buffers and free vol_amap */
			int j = 0;
		if (!sbi->vol_amap[i])
			goto err_out;
	}

	if (exfat_test_bitmap_range(sb, sbi->map_clu,
		EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) == false)
		goto err_out;

	return 0;

err_out:
	j = 0;
	/* release all buffers and free vol_amap */
	while (j < i)
		brelse(sbi->vol_amap[j++]);

@@ -69,10 +134,6 @@ static int exfat_allocate_bitmap(struct super_block *sb,
	sbi->vol_amap = NULL;
	return -EIO;
}
	}

	return 0;
}

int exfat_load_bitmap(struct super_block *sb)
{
+160 −0
Original line number Diff line number Diff line
@@ -1244,3 +1244,163 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)

	return count;
}

static int exfat_get_volume_label_dentry(struct super_block *sb,
		struct exfat_entry_set_cache *es)
{
	int i;
	int dentry = 0;
	unsigned int type;
	struct exfat_sb_info *sbi = EXFAT_SB(sb);
	struct exfat_hint_femp hint_femp;
	struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
	struct exfat_chain clu;
	struct exfat_dentry *ep;
	struct buffer_head *bh;

	hint_femp.eidx = EXFAT_HINT_NONE;
	exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);

	while (clu.dir != EXFAT_EOF_CLUSTER) {
		for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
			ep = exfat_get_dentry(sb, &clu, i, &bh);
			if (!ep)
				return -EIO;

			type = exfat_get_entry_type(ep);
			if (hint_femp.eidx == EXFAT_HINT_NONE) {
				if (type == TYPE_DELETED || type == TYPE_UNUSED) {
					hint_femp.cur = clu;
					hint_femp.eidx = dentry;
					hint_femp.count = 1;
				}
			}

			if (type == TYPE_UNUSED) {
				brelse(bh);
				goto not_found;
			}

			if (type != TYPE_VOLUME) {
				brelse(bh);
				continue;
			}

			memset(es, 0, sizeof(*es));
			es->sb = sb;
			es->bh = es->__bh;
			es->bh[0] = bh;
			es->num_bh = 1;
			es->start_off = EXFAT_DEN_TO_B(i) % sb->s_blocksize;

			return 0;
		}

		if (exfat_get_next_cluster(sb, &(clu.dir)))
			return -EIO;
	}

not_found:
	if (hint_femp.eidx == EXFAT_HINT_NONE) {
		hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
		hint_femp.eidx = dentry;
		hint_femp.count = 0;
	}

	ei->hint_femp = hint_femp;

	return -ENOENT;
}

int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
{
	int ret, i;
	struct exfat_sb_info *sbi = EXFAT_SB(sb);
	struct exfat_entry_set_cache es;
	struct exfat_dentry *ep;

	mutex_lock(&sbi->s_lock);

	memset(label_out, 0, sizeof(*label_out));
	ret = exfat_get_volume_label_dentry(sb, &es);
	if (ret < 0) {
		/*
		 * ENOENT signifies that a volume label dentry doesn't exist
		 * We will treat this as an empty volume label and not fail.
		 */
		if (ret == -ENOENT)
			ret = 0;

		goto unlock;
	}

	ep = exfat_get_dentry_cached(&es, 0);
	label_out->name_len = ep->dentry.volume_label.char_count;
	if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
		ret = -EIO;
		exfat_put_dentry_set(&es, false);
		goto unlock;
	}

	for (i = 0; i < label_out->name_len; i++)
		label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);

	exfat_put_dentry_set(&es, false);
unlock:
	mutex_unlock(&sbi->s_lock);
	return ret;
}

int exfat_write_volume_label(struct super_block *sb,
			     struct exfat_uni_name *label)
{
	int ret, i;
	struct exfat_sb_info *sbi = EXFAT_SB(sb);
	struct inode *root_inode = sb->s_root->d_inode;
	struct exfat_entry_set_cache es;
	struct exfat_chain clu;
	struct exfat_dentry *ep;

	if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
		return -EINVAL;

	mutex_lock(&sbi->s_lock);

	ret = exfat_get_volume_label_dentry(sb, &es);
	if (ret == -ENOENT) {
		if (label->name_len == 0) {
			/* No volume label dentry, no need to clear */
			ret = 0;
			goto unlock;
		}

		ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
	}

	if (ret < 0)
		goto unlock;

	ep = exfat_get_dentry_cached(&es, 0);

	if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
		/* volume label had been cleared */
		exfat_put_dentry_set(&es, 0);
		goto unlock;
	}

	memset(ep, 0, sizeof(*ep));
	ep->type = EXFAT_VOLUME;

	for (i = 0; i < label->name_len; i++)
		ep->dentry.volume_label.volume_label[i] =
			cpu_to_le16(label->name[i]);

	ep->dentry.volume_label.char_count = label->name_len;
	es.modified = true;

	ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));

unlock:
	mutex_unlock(&sbi->s_lock);
	return ret;
}
+7 −0
Original line number Diff line number Diff line
@@ -477,6 +477,9 @@ int exfat_force_shutdown(struct super_block *sb, u32 flags);
/* namei.c */
extern const struct dentry_operations exfat_dentry_ops;
extern const struct dentry_operations exfat_utf8_dentry_ops;
int exfat_find_empty_entry(struct inode *inode,
		struct exfat_chain *p_dir, int num_entries,
			   struct exfat_entry_set_cache *es);

/* cache.c */
int exfat_cache_init(void);
@@ -517,6 +520,10 @@ int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
		unsigned int num_entries);
int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync);
int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir);
int exfat_read_volume_label(struct super_block *sb,
			    struct exfat_uni_name *label_out);
int exfat_write_volume_label(struct super_block *sb,
			     struct exfat_uni_name *label);

/* inode.c */
extern const struct inode_operations exfat_file_inode_operations;
+6 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@
#define BOOTSEC_OLDBPB_LEN		53

#define EXFAT_FILE_NAME_LEN		15
#define EXFAT_VOLUME_LABEL_LEN		11

#define EXFAT_MIN_SECT_SIZE_BITS		9
#define EXFAT_MAX_SECT_SIZE_BITS		12
@@ -159,6 +160,11 @@ struct exfat_dentry {
			__le32 start_clu;
			__le64 size;
		} __packed upcase; /* up-case table directory entry */
		struct {
			__u8 char_count;
			__le16 volume_label[EXFAT_VOLUME_LABEL_LEN];
			__u8 reserved[8];
		} __packed volume_label; /* volume label directory entry */
		struct {
			__u8 flags;
			__u8 vendor_guid[16];
+6 −5
Original line number Diff line number Diff line
@@ -89,35 +89,36 @@ int exfat_ent_get(struct super_block *sb, unsigned int loc,
	int err;

	if (!is_valid_cluster(sbi, loc)) {
		exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
		exfat_fs_error_ratelimit(sb,
			"invalid access to FAT (entry 0x%08x)",
			loc);
		return -EIO;
	}

	err = __exfat_ent_get(sb, loc, content);
	if (err) {
		exfat_fs_error(sb,
		exfat_fs_error_ratelimit(sb,
			"failed to access to FAT (entry 0x%08x, err:%d)",
			loc, err);
		return err;
	}

	if (*content == EXFAT_FREE_CLUSTER) {
		exfat_fs_error(sb,
		exfat_fs_error_ratelimit(sb,
			"invalid access to FAT free cluster (entry 0x%08x)",
			loc);
		return -EIO;
	}

	if (*content == EXFAT_BAD_CLUSTER) {
		exfat_fs_error(sb,
		exfat_fs_error_ratelimit(sb,
			"invalid access to FAT bad cluster (entry 0x%08x)",
			loc);
		return -EIO;
	}

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