mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vyacheslav Dubeyko <slava@dubeyko.com>
To: Namjae Jeon <namjae.jeon@samsung.com>,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Cc: gregkh@linuxfoundation.org, valdis.kletnieks@vt.edu, hch@lst.de,
	sj1557.seo@samsung.com
Subject: Re: [PATCH v6 07/13] exfat: add bitmap operations
Date: Mon, 09 Dec 2019 15:07:29 +0300	[thread overview]
Message-ID: <5d673a46538fe38243e8b934a7a4bb84440686e0.camel@dubeyko.com> (raw)
In-Reply-To: <20191209065149.2230-8-namjae.jeon@samsung.com>

On Mon, 2019-12-09 at 01:51 -0500, Namjae Jeon wrote:
> This adds the implementation of bitmap operations for exfat.
> 
> Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
> Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com>
> ---
>  fs/exfat/balloc.c | 272
> ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 272 insertions(+)
>  create mode 100644 fs/exfat/balloc.c
> 
> diff --git a/fs/exfat/balloc.c b/fs/exfat/balloc.c
> new file mode 100644
> index 000000000000..d7b70620e349
> --- /dev/null
> +++ b/fs/exfat/balloc.c
> @@ -0,0 +1,272 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + *  Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
> + */
> +
> +#include <linux/blkdev.h>
> +#include <linux/slab.h>
> +#include <linux/buffer_head.h>
> +
> +#include "exfat_raw.h"
> +#include "exfat_fs.h"
> +
> +static const unsigned char free_bit[] = {
> +	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0,
> 2,/*  0 ~  19*/
> +	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/*
> 20 ~  39*/
> +	0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1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~  59*/
> +	4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/*
> 60 ~  79*/
> +	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/*
> 80 ~  99*/
> +	3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
> 6,/*100 ~ 119*/
> +	4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3,
> 4,/*120 ~ 139*/
> +	3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
> 6,/*140 ~ 159*/
> +	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4,
> 5,/*160 ~ 179*/
> +	4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4,
> 5,/*180 ~ 199*/
> +	3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5,
> 6,/*200 ~ 219*/
> +	5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6,
> 7,/*220 ~ 239*/
> +	4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7,
> 8             /*240 ~ 255*/
> +};
> +
> +/*
> + *  Allocation Bitmap Management Functions
> + */
> +static int exfat_allocate_bitmap(struct super_block *sb,
> +		struct exfat_dentry *ep)
> +{
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +	long long map_size;
> +	unsigned int i, need_map_size;
> +	sector_t sector;
> +
> +	sbi->map_clu = le32_to_cpu(ep->bitmap_start_clu);
> +	map_size = le64_to_cpu(ep->bitmap_size);
> +	need_map_size = (((sbi->num_clusters - BASE_CLUSTER) - 1) >> 3)
> + 1;

This code is slightly complicated. Why 3 was used here? Maybe it makes
sense to refactor this code and to make it more clear?

> +	if (need_map_size != map_size) {
> +		exfat_msg(sb, KERN_ERR,
> +				"bogus allocation bitmap size(need :
> %u, cur : %lld)",
> +				need_map_size, map_size);
> +		/*
> +		 * Only allowed when bogus allocation
> +		 * bitmap size is large
> +		 */
> +		if (need_map_size > map_size)
> +			return -EIO;
> +	}
> +	sbi->map_sectors = ((need_map_size - 1) >>
> +			(sb->s_blocksize_bits)) + 1;
> +	sbi->vol_amap = kmalloc_array(sbi->map_sectors,
> +				sizeof(struct buffer_head *),
> GFP_KERNEL);
> +	if (!sbi->vol_amap)
> +		return -ENOMEM;
> +
> +	sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
> +	for (i = 0; i < sbi->map_sectors; i++) {
> +		sbi->vol_amap[i] = sb_bread(sb, sector + i);
> +		if (!sbi->vol_amap[i]) {
> +			/* release all buffers and free vol_amap */
> +			int j = 0;
> +
> +			while (j < i)
> +				brelse(sbi->vol_amap[j++]);
> +
> +			kfree(sbi->vol_amap);
> +			sbi->vol_amap = NULL;
> +			return -EIO;
> +		}
> +	}
> +
> +	sbi->pbr_bh = NULL;
> +	return 0;
> +}
> +
> +int exfat_load_bitmap(struct super_block *sb)
> +{
> +	unsigned int i, type;
> +	struct exfat_chain clu;
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +
> +	exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
> +
> +	while (clu.dir != EOF_CLUSTER) {
> +		for (i = 0; i < sbi->dentries_per_clu; i++) {
> +			struct exfat_dentry *ep;
> +			struct buffer_head *bh;
> +
> +			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
> +			if (!ep)
> +				return -EIO;
> +
> +			type = exfat_get_entry_type(ep);
> +			if (type == TYPE_UNUSED)
> +				break;
> +			if (type != TYPE_BITMAP)
> +				continue;
> +			if (ep->bitmap_flags == 0x0) {
> +				int err;
> +
> +				err = exfat_allocate_bitmap(sb, ep);
> +				brelse(bh);
> +				return err;
> +			}
> +			brelse(bh);
> +		}
> +
> +		if (exfat_get_next_cluster(sb, &clu.dir))
> +			return -EIO;
> +	}
> +
> +	return -EINVAL;
> +}
> +
> +void exfat_free_bitmap(struct super_block *sb)
> +{
> +	int i;
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +
> +	brelse(sbi->pbr_bh);
> +
> +	for (i = 0; i < sbi->map_sectors; i++)
> +		__brelse(sbi->vol_amap[i]);
> +
> +	kfree(sbi->vol_amap);
> +	sbi->vol_amap = NULL;
> +}
> +
> +/*
> + * If the value of "clu" is 0, it means cluster 2 which is the first
> cluster of
> + * the cluster heap.
> + */
> +int exfat_set_bitmap(struct inode *inode, unsigned int clu)
> +{
> +	int i, b;
> +	struct super_block *sb = inode->i_sb;
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +
> +	i = clu >> (sb->s_blocksize_bits + 3);
> +	b = clu & ((sb->s_blocksize << 3) - 1);

This code looks like the dark magic. I believe it makes sense to
introduce some constant for 3 here.

> +
> +	set_bit_le(b, sbi->vol_amap[i]->b_data);
> +	exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode));
> +	return 0;
> +}
> +
> +/*
> + * If the value of "clu" is 0, it means cluster 2 which is the first
> cluster of
> + * the cluster heap.
> + */
> +void exfat_clear_bitmap(struct inode *inode, unsigned int clu)
> +{
> +	int i, b;
> +	struct super_block *sb = inode->i_sb;
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +	struct exfat_mount_options *opts = &sbi->options;
> +
> +	i = clu >> (sb->s_blocksize_bits + 3);
> +	b = clu & ((sb->s_blocksize << 3) - 1);
> +

Ditto.

> +	clear_bit_le(b, sbi->vol_amap[i]->b_data);
> +	exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode));
> +
> +	if (opts->discard) {
> +		int ret_discard;
> +
> +		ret_discard = sb_issue_discard(sb,
> +				exfat_cluster_to_sector(sbi, clu + 2),
> +				(1 << sbi->sect_per_clus_bits),
> GFP_NOFS, 0);

Why clu + 2? It's unclear, frankly speaking.

> +
> +		if (ret_discard == -EOPNOTSUPP) {
> +			exfat_msg(sb, KERN_ERR,
> +				"discard not supported by device,
> disabling");
> +			opts->discard = 0;
> +		}
> +	}
> +}
> +
> +/*
> + * If the value of "clu" is 0, it means cluster 2 which is the first
> cluster of
> + * the cluster heap.
> + */
> +unsigned int exfat_test_bitmap(struct super_block *sb, unsigned int
> clu)
> +{
> +	unsigned int i, map_i, map_b;
> +	unsigned int clu_base, clu_free;
> +	unsigned char k, clu_mask;
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +
> +	clu_base = (clu & ~(0x7)) + 2;
> +	clu_mask = (1 << (clu - clu_base + 2)) - 1;
> +

What does 0x7 mean here? Why clu - clu_base + 2? Maybe it need to
introduce some constants and to add some comments?

> +	map_i = clu >> (sb->s_blocksize_bits + 3);
> +	map_b = (clu >> 3) & (unsigned int)(sb->s_blocksize - 1);
> +

Ditto.

> +	for (i = 2; i < sbi->num_clusters; i += 8) {


Why we start from 2 and add 8? Maybe introduce some constants?

> +		k = *(sbi->vol_amap[map_i]->b_data + map_b);
> +		if (clu_mask > 0) {
> +			k |= clu_mask;
> +			clu_mask = 0;
> +		}
> +		if (k < 0xFF) {
> +			clu_free = clu_base + free_bit[k];
> +			if (clu_free < sbi->num_clusters)
> +				return clu_free;
> +		}
> +		clu_base += 8;

Ditto.

> +
> +		if (++map_b >= sb->s_blocksize ||
> +		    clu_base >= sbi->num_clusters) {
> +			if (++map_i >= sbi->map_sectors) {
> +				clu_base = 2;
> +				map_i = 0;
> +			}
> +			map_b = 0;
> +		}
> +	}
> +
> +	return EOF_CLUSTER;
> +}
> +
> +int exfat_count_used_clusters(struct super_block *sb, unsigned int
> *ret_count)
> +{
> +	struct exfat_sb_info *sbi = EXFAT_SB(sb);
> +	unsigned int count = 0;
> +	unsigned int i, map_i = 0, map_b = 0;
> +	unsigned int total_clus = sbi->num_clusters - 2;
> +	unsigned int last_mask = total_clus & 7;

Ditto.

> +	unsigned char clu_bits;
> +	const unsigned char last_bit_mask[] = {0, 0b00000001,
> 0b00000011,
> +		0b00000111, 0b00001111, 0b00011111, 0b00111111,
> 0b01111111};
> +
> +	total_clus &= ~last_mask;
> +	for (i = 0; i < total_clus; i += 8) {

Ditto.

Thanks,
Viacheslav Dubeyko.

> +		clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
> +		count += used_bit[clu_bits];
> +		if (++map_b >= (unsigned int)sb->s_blocksize) {
> +			map_i++;
> +			map_b = 0;
> +		}
> +	}
> +
> +	if (last_mask) {
> +		clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
> +		clu_bits &= last_bit_mask[last_mask];
> +		count += used_bit[clu_bits];
> +	}
> +
> +	*ret_count = count;
> +	return 0;
> +}


  reply	other threads:[~2019-12-09 12:07 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20191209065457epcas1p3130d80fd65c9a1c1af5f67dadc49e913@epcas1p3.samsung.com>
2019-12-09  6:51 ` [PATCH v6 00/13] add the latest exfat driver Namjae Jeon
     [not found]   ` <CGME20191209065457epcas1p196e28d337922e1c1c52c00fb2a384a2a@epcas1p1.samsung.com>
2019-12-09  6:51     ` [PATCH v6 01/13] exfat: add in-memory and on-disk structures and headers Namjae Jeon
     [not found]   ` <CGME20191209065458epcas1p1104c7fa3f7a34164a0b51d902b78af0e@epcas1p1.samsung.com>
2019-12-09  6:51     ` [PATCH v6 02/13] exfat: add super block operations Namjae Jeon
2019-12-09  9:24       ` Vyacheslav Dubeyko
2019-12-11  5:18         ` Namjae Jeon
     [not found]   ` <CGME20191209065459epcas1p3349caea59da1b9b458a73923d724ca35@epcas1p3.samsung.com>
2019-12-09  6:51     ` [PATCH v6 03/13] exfat: add inode operations Namjae Jeon
2019-12-09  9:54       ` Vyacheslav Dubeyko
2019-12-11  5:22         ` Namjae Jeon
     [not found]   ` <CGME20191209065459epcas1p299d251e17cd37880103cb920ab02330c@epcas1p2.samsung.com>
2019-12-09  6:51     ` [PATCH v6 04/13] exfat: add directory operations Namjae Jeon
2019-12-09 11:05       ` Vyacheslav Dubeyko
     [not found]   ` <CGME20191209065500epcas1p3da26ef7963bbba978ed614bc19b2ea07@epcas1p3.samsung.com>
2019-12-09  6:51     ` [PATCH v6 05/13] exfat: add file operations Namjae Jeon
2019-12-09 11:32       ` Vyacheslav Dubeyko
2019-12-11  1:00         ` Namjae Jeon
     [not found]   ` <CGME20191209065500epcas1p2f236643696f32677b809c6bb12ce0bef@epcas1p2.samsung.com>
2019-12-09  6:51     ` [PATCH v6 06/13] exfat: add exfat entry operations Namjae Jeon
2019-12-09 11:41       ` Vyacheslav Dubeyko
     [not found]   ` <CGME20191209065501epcas1p267d7a2c08e4893ba86694f39c63405f9@epcas1p2.samsung.com>
2019-12-09  6:51     ` [PATCH v6 07/13] exfat: add bitmap operations Namjae Jeon
2019-12-09 12:07       ` Vyacheslav Dubeyko [this message]
     [not found]   ` <CGME20191209065501epcas1p44767c16feecd953fd5def1aaecb09313@epcas1p4.samsung.com>
2019-12-09  6:51     ` [PATCH v6 08/13] exfat: add exfat cache Namjae Jeon
     [not found]   ` <CGME20191209065502epcas1p4337a0f7518e55dbef1573d53c99f88ef@epcas1p4.samsung.com>
2019-12-09  6:51     ` [PATCH v6 09/13] exfat: add misc operations Namjae Jeon
     [not found]   ` <CGME20191209065502epcas1p1bb1c240f10ccde7c0c0cd2d5396db39d@epcas1p1.samsung.com>
2019-12-09  6:51     ` [PATCH v6 10/13] exfat: add nls operations Namjae Jeon
     [not found]   ` <CGME20191209065503epcas1p12d8b47ee6211884a217959e6f54995fb@epcas1p1.samsung.com>
2019-12-09  6:51     ` [PATCH v6 11/13] exfat: add Kconfig and Makefile Namjae Jeon
     [not found]   ` <CGME20191209065503epcas1p3e9efaa9e22ebe270d8827ce34935a58b@epcas1p3.samsung.com>
2019-12-09  6:51     ` [PATCH v6 12/13] exfat: add exfat in fs/Kconfig and fs/Makefile Namjae Jeon
     [not found]   ` <CGME20191209065504epcas1p261e9cd0660deddb18d76684038c9b8fa@epcas1p2.samsung.com>
2019-12-09  6:51     ` [PATCH v6 13/13] MAINTAINERS: add exfat filesystem Namjae Jeon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5d673a46538fe38243e8b934a7a4bb84440686e0.camel@dubeyko.com \
    --to=slava@dubeyko.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namjae.jeon@samsung.com \
    --cc=sj1557.seo@samsung.com \
    --cc=valdis.kletnieks@vt.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®