From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Cc: Tom Rini <trini@konsulko.com>, u-boot@lists.denx.de
Subject: Re: [PATCH v2 1/5] fs: fat: finding an empty FAT cluster
Date: Mon, 1 Aug 2022 10:02:44 +0900 [thread overview]
Message-ID: <20220801010244.GA37247@laputa> (raw)
In-Reply-To: <20220731115837.77646-2-heinrich.schuchardt@canonical.com>
On Sun, Jul 31, 2022 at 01:58:33PM +0200, Heinrich Schuchardt wrote:
> Currently we have two functions with redundant coding to find an empty
> cluster:
>
> * find_empty_cluster() seeks from the beginning of the FAT table
> * determine_fatent() seeks after a given entry
>
> Both do not detect the end of the FAT table correctly and return an invalid
> cluster number if no empty entry if found.
>
> find_empty_cluster() is replaced by an invocation of determine_fatent().
>
> determine_fatent() is changed to seek in a second round from the beginning
> of the FAT table and to return an error code if no free entry is found.
> With this patch we will always find an empty cluster if it exists.
>
> Further patches are needed to handle the disk full error gracefully.
>
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> ---
> v2:
> no change
I made this comment before:
https://lists.denx.de/pipermail/u-boot/2022-July/488827.html
-Takahiro Akashi
> ---
> fs/fat/fat_write.c | 56 ++++++++++++++++++++++++----------------------
> 1 file changed, 29 insertions(+), 27 deletions(-)
>
> diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
> index 8ff2f6def0..a137e14f41 100644
> --- a/fs/fat/fat_write.c
> +++ b/fs/fat/fat_write.c
> @@ -536,22 +536,41 @@ static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
> return 0;
> }
>
> -/*
> - * Determine the next free cluster after 'entry' in a FAT (12/16/32) table
> - * and link it to 'entry'. EOC marker is not set on returned entry.
> +/**
> + * determine_fatent() - get next free FAT cluster
> + *
> + * The parameter @entry indicates the current cluster. To reduce fragementation
> + * the function first searches for a free cluster after the current cluster.
> + * If none is found, the search is repeated from the beginning of the FAT table.
> + *
> + * If @entry is set, the new FAT entry is appended to the given one.
> + * If @entry is zero, only the number of the first free cluster is returned.
> + *
> + * @entry: current entry
> + * Return: next free cluster or negative error
> */
> -static __u32 determine_fatent(fsdata *mydata, __u32 entry)
> +static int determine_fatent(fsdata *mydata, __u32 entry)
> {
> - __u32 next_fat, next_entry = entry + 1;
> + __u32 next_fat, next_entry = entry;
> + int second_round = 0;
>
> while (1) {
> + ++next_entry;
> + if (CHECK_CLUST(next_entry, mydata->fatsize)) {
> + if (!second_round) {
> + second_round = 1;
> + next_entry = 3;
> + } else {
> + return -ENOSPC;
> + }
> + }
> next_fat = get_fatent(mydata, next_entry);
> - if (next_fat == 0) {
> + if (!next_fat) {
> /* found free entry, link to entry */
> - set_fatent_value(mydata, entry, next_entry);
> + if (entry)
> + set_fatent_value(mydata, entry, next_entry);
> break;
> }
> - next_entry++;
> }
> debug("FAT%d: entry: %08x, entry_value: %04x\n",
> mydata->fatsize, entry, next_entry);
> @@ -794,23 +813,6 @@ get_set_cluster(fsdata *mydata, __u32 clustnum, loff_t pos, __u8 *buffer,
> return 0;
> }
>
> -/*
> - * Find the first empty cluster
> - */
> -static int find_empty_cluster(fsdata *mydata)
> -{
> - __u32 fat_val, entry = 3;
> -
> - while (1) {
> - fat_val = get_fatent(mydata, entry);
> - if (fat_val == 0)
> - break;
> - entry++;
> - }
> -
> - return entry;
> -}
> -
> /**
> * new_dir_table() - allocate a cluster for additional directory entries
> *
> @@ -824,7 +826,7 @@ static int new_dir_table(fat_itr *itr)
> int dir_oldclust = itr->clust;
> unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
>
> - dir_newclust = find_empty_cluster(mydata);
> + dir_newclust = determine_fatent(mydata, 0);
>
> /*
> * Flush before updating FAT to ensure valid directory structure
> @@ -1066,7 +1068,7 @@ set_clusters:
>
> /* Assure that curclust is valid */
> if (!curclust) {
> - curclust = find_empty_cluster(mydata);
> + curclust = determine_fatent(mydata, 0);
> set_start_cluster(mydata, dentptr, curclust);
> } else {
> newclust = get_fatent(mydata, curclust);
> --
> 2.36.1
>
next prev parent reply other threads:[~2022-08-01 1:02 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-31 11:58 [PATCH v2 0/5] fs/fat: fix handling of full disk Heinrich Schuchardt
2022-07-31 11:58 ` [PATCH v2 1/5] fs: fat: finding an empty FAT cluster Heinrich Schuchardt
2022-08-01 1:02 ` AKASHI Takahiro [this message]
2022-08-01 8:21 ` Heinrich Schuchardt
2022-08-02 0:02 ` AKASHI Takahiro
2022-07-31 11:58 ` [PATCH v2 2/5] fs: fat: determine_fatent() error handling Heinrich Schuchardt
2022-07-31 11:58 ` [PATCH v2 3/5] fs: fat: carve out fat_create_dir_entry() Heinrich Schuchardt
2022-07-31 11:58 ` [PATCH v2 4/5] test: let fs_obj_mkdir() provide full file system type Heinrich Schuchardt
2022-07-31 11:58 ` [PATCH v2 5/5] test: add test for full FAT16 directory Heinrich Schuchardt
2022-08-01 1:50 ` AKASHI Takahiro
2022-08-01 6:14 ` Heinrich Schuchardt
2022-08-02 0:19 ` AKASHI Takahiro
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=20220801010244.GA37247@laputa \
--to=takahiro.akashi@linaro.org \
--cc=heinrich.schuchardt@canonical.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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