All of lore.kernel.org
 help / color / mirror / Atom feed
From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 08/17] fs: fat: refactor write interface for a file offset
Date: Fri, 20 Jul 2018 11:57:14 +0900	[thread overview]
Message-ID: <20180720025723.6736-9-takahiro.akashi@linaro.org> (raw)
In-Reply-To: <20180720025723.6736-1-takahiro.akashi@linaro.org>

The current write implementation is quite simple: remove existing clusters
and then allocating new ones and filling them with data. This, inevitably,
enforces always writing from the beginning of a file.

As the first step to lift this restriction, fat_file_write() and
set_contents() are modified to accept an additional parameter, file offset
and further re-factored so that, in the next patch, all the necessary code
will be put into set_contents().

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 fs/fat/fat_write.c | 178 +++++++++++++++++----------------------------
 1 file changed, 65 insertions(+), 113 deletions(-)

diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
index 96edb6674c..3a9c53e253 100644
--- a/fs/fat/fat_write.c
+++ b/fs/fat/fat_write.c
@@ -528,6 +528,43 @@ static int clear_fatent(fsdata *mydata, __u32 entry)
 	return 0;
 }
 
+/*
+ * Set start cluster in directory entry
+ */
+static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
+				__u32 start_cluster)
+{
+	if (mydata->fatsize == 32)
+		dentptr->starthi =
+			cpu_to_le16((start_cluster & 0xffff0000) >> 16);
+	dentptr->start = cpu_to_le16(start_cluster & 0xffff);
+}
+
+/*
+ * Check whether adding a file makes the file system to
+ * exceed the size of the block device
+ * Return -1 when overflow occurs, otherwise return 0
+ */
+static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
+{
+	__u32 startsect, sect_num, offset;
+
+	if (clustnum > 0) {
+		startsect = clust_to_sect(mydata, clustnum);
+	} else {
+		startsect = mydata->rootdir_sect;
+	}
+
+	sect_num = div_u64_rem(size, mydata->sect_size, &offset);
+
+	if (offset != 0)
+		sect_num++;
+
+	if (startsect + sect_num > total_sector)
+		return -1;
+	return 0;
+}
+
 /*
  * Write at most 'maxsize' bytes from 'buffer' into
  * the file associated with 'dentptr'
@@ -535,29 +572,36 @@ static int clear_fatent(fsdata *mydata, __u32 entry)
  * or return -1 on fatal errors.
  */
 static int
-set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
+set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
 	      loff_t maxsize, loff_t *gotsize)
 {
-	loff_t filesize = FAT2CPU32(dentptr->size);
+	loff_t filesize;
 	unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
 	__u32 curclust = START(dentptr);
 	__u32 endclust = 0, newclust = 0;
 	loff_t actsize;
 
 	*gotsize = 0;
-	debug("Filesize: %llu bytes\n", filesize);
-
-	if (maxsize > 0 && filesize > maxsize)
-		filesize = maxsize;
+	filesize = maxsize;
 
 	debug("%llu bytes\n", filesize);
 
-	if (!curclust) {
-		if (filesize) {
-			debug("error: nonempty clusterless file!\n");
+	if (curclust) {
+		/*
+		 * release already-allocated clusters anyway
+		 */
+		if (clear_fatent(mydata, curclust)) {
+			printf("Error: clearing FAT entries\n");
 			return -1;
 		}
-		return 0;
+	}
+
+	curclust = find_empty_cluster(mydata);
+	set_start_cluster(mydata, dentptr, curclust);
+
+	if (check_overflow(mydata, curclust, filesize)) {
+		printf("Error: no space left: %llu\n", filesize);
+		return -1;
 	}
 
 	actsize = bytesperclust;
@@ -568,6 +612,7 @@ set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
 			newclust = determine_fatent(mydata, endclust);
 
 			if ((newclust - 1) != endclust)
+				/* write to <curclust..endclust> */
 				goto getit;
 
 			if (CHECK_CLUST(newclust, mydata->fatsize)) {
@@ -614,18 +659,8 @@ getit:
 		actsize = bytesperclust;
 		curclust = endclust = newclust;
 	} while (1);
-}
 
-/*
- * Set start cluster in directory entry
- */
-static void set_start_cluster(const fsdata *mydata, dir_entry *dentptr,
-				__u32 start_cluster)
-{
-	if (mydata->fatsize == 32)
-		dentptr->starthi =
-			cpu_to_le16((start_cluster & 0xffff0000) >> 16);
-	dentptr->start = cpu_to_le16(start_cluster & 0xffff);
+	return 0;
 }
 
 /*
@@ -642,31 +677,6 @@ static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
 	set_name(dentptr, filename);
 }
 
-/*
- * Check whether adding a file makes the file system to
- * exceed the size of the block device
- * Return -1 when overflow occurs, otherwise return 0
- */
-static int check_overflow(fsdata *mydata, __u32 clustnum, loff_t size)
-{
-	__u32 startsect, sect_num, offset;
-
-	if (clustnum > 0) {
-		startsect = clust_to_sect(mydata, clustnum);
-	} else {
-		startsect = mydata->rootdir_sect;
-	}
-
-	sect_num = div_u64_rem(size, mydata->sect_size, &offset);
-
-	if (offset != 0)
-		sect_num++;
-
-	if (startsect + sect_num > total_sector)
-		return -1;
-	return 0;
-}
-
 /*
  * Find a directory entry based on filename or start cluster number
  * If the directory entry is not found,
@@ -784,11 +794,10 @@ static int normalize_longname(char *l_filename, const char *filename)
 	return 0;
 }
 
-static int do_fat_write(const char *filename, void *buffer, loff_t size,
-			loff_t *actwrite)
+int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
+		      loff_t size, loff_t *actwrite)
 {
 	dir_entry *retdent;
-	__u32 start_cluster;
 	fsdata datablock = { .fatbuf = NULL, };
 	fsdata *mydata = &datablock;
 	fat_itr *itr = NULL;
@@ -796,6 +805,8 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size,
 	char *filename_copy, *parent, *basename;
 	char l_filename[VFAT_MAXLEN_BYTES];
 
+	debug("writing %s\n", filename);
+
 	filename_copy = strdup(filename);
 	if (!filename_copy)
 		return -ENOMEM;
@@ -841,47 +852,8 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size,
 			goto exit;
 		}
 
-		/* Update file size and start_cluster in a directory entry */
-		retdent->size = cpu_to_le32(size);
-		start_cluster = START(retdent);
-
-		if (start_cluster) {
-			if (size) {
-				ret = check_overflow(mydata, start_cluster,
-							size);
-				if (ret) {
-					printf("Error: %llu overflow\n", size);
-					ret = -ENOSPC;
-					goto exit;
-				}
-			}
-
-			ret = clear_fatent(mydata, start_cluster);
-			if (ret) {
-				printf("Error: clearing FAT entries\n");
-				ret = -EIO;
-				goto exit;
-			}
-
-			if (!size)
-				set_start_cluster(mydata, retdent, 0);
-		} else if (size) {
-			ret = start_cluster = find_empty_cluster(mydata);
-			if (ret < 0) {
-				printf("Error: finding empty cluster\n");
-				ret = -ENOSPC;
-				goto exit;
-			}
-
-			ret = check_overflow(mydata, start_cluster, size);
-			if (ret) {
-				printf("Error: %llu overflow\n", size);
-				ret = -ENOSPC;
-				goto exit;
-			}
-
-			set_start_cluster(mydata, retdent, start_cluster);
-		}
+		/* Update file size in a directory entry */
+		retdent->size = cpu_to_le32(pos + size);
 	} else {
 		/* Create a new file */
 
@@ -909,32 +881,13 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size,
 			goto exit;
 		}
 
-		if (size) {
-			ret = start_cluster = find_empty_cluster(mydata);
-			if (ret < 0) {
-				printf("Error: finding empty cluster\n");
-				ret = -ENOSPC;
-				goto exit;
-			}
-
-			ret = check_overflow(mydata, start_cluster, size);
-			if (ret) {
-				printf("Error: %llu overflow\n", size);
-				ret = -ENOSPC;
-				goto exit;
-			}
-		} else {
-			start_cluster = 0;
-		}
-
 		/* Set attribute as archieve for regular file */
-		fill_dentry(itr->fsdata, itr->dent, filename,
-					start_cluster, size, 0x20);
+		fill_dentry(itr->fsdata, itr->dent, filename, 0, size, 0x20);
 
 		retdent = itr->dent;
 	}
 
-	ret = set_contents(mydata, retdent, buffer, size, actwrite);
+	ret = set_contents(mydata, retdent, pos, buffer, size, actwrite);
 	if (ret < 0) {
 		printf("Error: writing contents\n");
 		ret = -EIO;
@@ -973,6 +926,5 @@ int file_fat_write(const char *filename, void *buffer, loff_t offset,
 		return -EINVAL;
 	}
 
-	printf("writing %s\n", filename);
-	return do_fat_write(filename, buffer, maxsize, actwrite);
+	return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
 }
-- 
2.17.0

  parent reply	other threads:[~2018-07-20  2:57 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-20  2:57 [U-Boot] [PATCH 00/17] fs: fat: extend FAT write operations AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 01/17] fs: fat: extend get_fs_info() for write use AKASHI Takahiro
2018-07-20 18:06   ` Heinrich Schuchardt
2018-07-22  7:43     ` Heinrich Schuchardt
2018-07-20  2:57 ` [U-Boot] [PATCH 02/17] fs: fat: handle "." and ".." of root dir correctly with fat_itr_resolve() AKASHI Takahiro
2018-07-20 18:09   ` Heinrich Schuchardt
2018-07-23  7:55     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 03/17] fs: fat: make directory iterator global for write use AKASHI Takahiro
2018-07-20 18:02   ` Heinrich Schuchardt
2018-07-23  8:06     ` AKASHI Takahiro
2018-07-23  8:18       ` AKASHI Takahiro
2018-08-11 13:34   ` Heinrich Schuchardt
2018-08-20  4:45     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 04/17] fs: fat: assure iterator's ->dent belongs to ->clust AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 05/17] fs: fat: check and normailze file name AKASHI Takahiro
2018-07-31  4:31   ` Heinrich Schuchardt
2018-07-20  2:57 ` [U-Boot] [PATCH 06/17] fs: fat: write returns error code instead of -1 AKASHI Takahiro
2018-07-20 17:55   ` Heinrich Schuchardt
2018-07-23  8:32     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 07/17] fs: fat: support write with sub-directory path AKASHI Takahiro
2018-07-20  2:57 ` AKASHI Takahiro [this message]
2018-07-20  2:57 ` [U-Boot] [PATCH 09/17] fs: fat: support write with non-zero offset AKASHI Takahiro
2018-07-20 17:46   ` Heinrich Schuchardt
2018-07-23  8:41     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 10/17] cmd: fat: add offset parameter to fatwrite AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 11/17] fs: add mkdir interface AKASHI Takahiro
2018-07-20 17:35   ` Heinrich Schuchardt
2018-07-23 23:48     ` Simon Glass
2018-07-23 23:56       ` Tom Rini
2018-07-24  1:45         ` AKASHI Takahiro
2018-07-25  2:45           ` Simon Glass
2018-07-24  0:56     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 12/17] fs: fat: remember the starting cluster number of directory AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 13/17] fs: fat: support mkdir AKASHI Takahiro
2018-07-20 17:14   ` Heinrich Schuchardt
2018-07-24  2:01     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 14/17] cmd: fat: add fatmkdir command AKASHI Takahiro
2018-07-20 17:09   ` Heinrich Schuchardt
2018-07-24  2:07     ` AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 15/17] efi_loader: file: support creating a directory AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 16/17] efi_loader: implement a pseudo "file delete" AKASHI Takahiro
2018-07-20  2:57 ` [U-Boot] [PATCH 17/17] fs-test: fix false positive error at Test Case 12 AKASHI Takahiro
2018-07-29  7:02   ` Heinrich Schuchardt
2018-07-31  7:55     ` AKASHI Takahiro
2018-07-20  9:48 ` [U-Boot] [PATCH 00/17] fs: fat: extend FAT write operations Heinrich Schuchardt
2018-07-22  6:44   ` Heinrich Schuchardt
2018-07-23  7:35     ` AKASHI Takahiro
2018-07-23 14:46     ` Tom Rini
2018-08-06 22:34       ` Heinrich Schuchardt
2018-08-07  5:38         ` AKASHI Takahiro
2018-08-07  5:52           ` AKASHI Takahiro
2018-08-07  5:53           ` Heinrich Schuchardt

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=20180720025723.6736-9-takahiro.akashi@linaro.org \
    --to=takahiro.akashi@linaro.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.