mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 01/10] fs: give erase() a new erase_type parameter
Date: Tue, 30 Jul 2024 10:31:59 +0200	[thread overview]
Message-ID: <Zqik_wu9O7wYMFzh@pengutronix.de> (raw)
In-Reply-To: <20240730071929.2016537-2-a.fatoum@pengutronix.de>

On Tue, Jul 30, 2024 at 09:19:20AM +0200, Ahmad Fatoum wrote:
> Many managed flashes provide an erase operation, which need not be used
> for regular writes, but is provided nonetheless for efficient clearing
> of large regions of storage to a fixed bit pattern. The erase type allows
> users to specify _why_ the erase operation is done, so the driver or
> the core code can choose the most optimal operation.
> 
> Existing code is annotated wither either ERASE_TO_WRITE for code that is

s/wither/with/

> immediately followed by a write and ERASE_TO_CLEAR for the standalone
> erase operations of the shell and fastboot erase command.
> 
> Use of an enum allows the future addition of more types, e.g.:
> 
>   ERASE_TO_ALL_ZERO: to efficiently implement android sparse all-ones
>                      (like util-linux blkdiscard --zeroout)
>   ERASE_TO_ALL_ONE: to efficiently implement android sparse all-zeroes

I suggest using ERASE_TO_ALL_ONE to implement sparse all-ones ;)

Will fix both of these while applying in case there's nothing else in
this series.

Sascha

>   ERASE_TO_DISCARD: as alternative to ERASE_TO_CLEAR, when unwritten
>                     regions are indeed allowed to have arbitrary content
>                      (like util-linux blkdiscard without argument)
>   ERASE_TO_SECURE: erase data in a manner appropriate for wiping secrets
>                      (like util-linux blkdiscard --secure)
> 
> We don't introduce any functional change here yet, as no device yet sets
> DEVFS_WRITE_AUTOERASE. This will follow in a separate commit.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  arch/arm/mach-imx/imx-bbu-external-nand.c |  2 +-
>  arch/arm/mach-imx/imx-bbu-internal.c      |  4 ++--
>  arch/arm/mach-omap/am33xx_bbu_spi_mlo.c   |  2 +-
>  commands/flash.c                          |  2 +-
>  commands/nandtest.c                       |  2 +-
>  common/bbu.c                              |  2 +-
>  common/environment.c                      |  7 ++++--
>  common/fastboot.c                         |  2 +-
>  common/state/backend_bucket_circular.c    |  2 +-
>  drivers/misc/storage-by-uuid.c            |  3 +++
>  drivers/usb/gadget/function/dfu.c         |  4 ++--
>  fs/devfs-core.c                           |  7 +++++-
>  fs/devfs.c                                |  5 ++++-
>  fs/fs.c                                   |  4 ++--
>  include/driver.h                          | 13 ++++++++++++
>  include/fs.h                              | 26 +++++++++++++++++++++--
>  lib/libfile.c                             |  2 +-
>  17 files changed, 69 insertions(+), 20 deletions(-)
> 
> diff --git a/arch/arm/mach-imx/imx-bbu-external-nand.c b/arch/arm/mach-imx/imx-bbu-external-nand.c
> index 7523008cdb5a..72c2271ff37a 100644
> --- a/arch/arm/mach-imx/imx-bbu-external-nand.c
> +++ b/arch/arm/mach-imx/imx-bbu-external-nand.c
> @@ -153,7 +153,7 @@ static int imx_bbu_external_nand_update(struct bbu_handler *handler, struct bbu_
>  
>  		debug("writing %d bytes at 0x%08llx\n", now, nand_offset);
>  
> -		ret = erase(fd, blocksize, nand_offset);
> +		ret = erase(fd, blocksize, nand_offset, ERASE_TO_WRITE);
>  		if (ret)
>  			goto out;
>  
> diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
> index 8cdaab5c1676..4190d60d18dd 100644
> --- a/arch/arm/mach-imx/imx-bbu-internal.c
> +++ b/arch/arm/mach-imx/imx-bbu-internal.c
> @@ -108,7 +108,7 @@ static int imx_bbu_write_device(struct imx_internal_bbu_handler *imx_handler,
>  	if (imx_bbu_erase_required(imx_handler)) {
>  		pr_debug("%s: erasing %s from 0x%08x to 0x%08x\n", __func__,
>  				devicefile, offset, image_len);
> -		ret = erase(fd, image_len, offset);
> +		ret = erase(fd, image_len, offset, ERASE_TO_WRITE);
>  		if (ret) {
>  			pr_err("erasing %s failed with %s\n", devicefile,
>  					strerror(-ret));
> @@ -340,7 +340,7 @@ static int imx_bbu_internal_v2_write_nand_dbbt(struct imx_internal_bbu_handler *
>  
>  		pr_debug("writing %d bytes at 0x%08llx\n", now, offset);
>  
> -		ret = erase(fd, blocksize, offset);
> +		ret = erase(fd, blocksize, offset, ERASE_TO_WRITE);
>  		if (ret)
>  			goto out;
>  
> diff --git a/arch/arm/mach-omap/am33xx_bbu_spi_mlo.c b/arch/arm/mach-omap/am33xx_bbu_spi_mlo.c
> index 2c58c9ae690e..951c3f8ce099 100644
> --- a/arch/arm/mach-omap/am33xx_bbu_spi_mlo.c
> +++ b/arch/arm/mach-omap/am33xx_bbu_spi_mlo.c
> @@ -71,7 +71,7 @@ static int spi_nor_mlo_handler(struct bbu_handler *handler,
>  		goto out;
>  	}
>  
> -	ret = erase(dstfd, ERASE_SIZE_ALL, 0);
> +	ret = erase(dstfd, ERASE_SIZE_ALL, 0, ERASE_TO_WRITE);
>  	if (ret < 0) {
>  		printf("could not erase %s: %m", data->devicefile);
>  		goto out1;
> diff --git a/commands/flash.c b/commands/flash.c
> index 5b7060aeadfb..de3bf65e5059 100644
> --- a/commands/flash.c
> +++ b/commands/flash.c
> @@ -43,7 +43,7 @@ static int do_flerase(int argc, char *argv[])
>  		goto out;
>  	}
>  
> -	if (erase(fd, size, start)) {
> +	if (erase(fd, size, start, ERASE_TO_CLEAR)) {
>  		perror("erase");
>  		ret = 1;
>  	}
> diff --git a/commands/nandtest.c b/commands/nandtest.c
> index ba7c87a32eea..bc138646a460 100644
> --- a/commands/nandtest.c
> +++ b/commands/nandtest.c
> @@ -160,7 +160,7 @@ static int erase_and_write(loff_t ofs, unsigned char *data,
>  	er.start = ofs;
>  	er.length = meminfo.erasesize;
>  
> -	ret = erase(fd, er.length, er.start);
> +	ret = erase(fd, er.length, er.start, ERASE_TO_WRITE);
>  	if (ret < 0) {
>  		perror("\nerase");
>  		printf("Could't not erase flash at 0x%08llx length 0x%08llx.\n",
> diff --git a/common/bbu.c b/common/bbu.c
> index f71747aa2f6d..9ea8a1a3f247 100644
> --- a/common/bbu.c
> +++ b/common/bbu.c
> @@ -453,7 +453,7 @@ int bbu_std_file_handler(struct bbu_handler *handler,
>  		goto err_close;
>  	}
>  
> -	ret = erase(fd, data->len, 0);
> +	ret = erase(fd, data->len, 0, ERASE_TO_WRITE);
>  	if (ret && ret != -ENOSYS) {
>  		printf("erasing %s failed with %s\n", data->devicefile,
>  				strerror(-ret));
> diff --git a/common/environment.c b/common/environment.c
> index 39cad0c16a77..dbf4c2f52365 100644
> --- a/common/environment.c
> +++ b/common/environment.c
> @@ -152,7 +152,10 @@ static inline int protect(int fd, size_t count, unsigned long offset, int prot)
>  	return 0;
>  }
>  
> -static inline int erase(int fd, size_t count, unsigned long offset)
> +enum erase_type {
> +	ERASE_TO_WRITE
> +};
> +static inline int erase(int fd, size_t count, unsigned long offset, enum erase_type type)
>  {
>  	return 0;
>  }
> @@ -380,7 +383,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags)
>  		goto out;
>  	}
>  
> -	ret = erase(envfd, ERASE_SIZE_ALL, 0);
> +	ret = erase(envfd, ERASE_SIZE_ALL, 0, ERASE_TO_WRITE);
>  
>  	/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
>  	if (ret && errno != ENOSYS && errno != EOPNOTSUPP) {
> diff --git a/common/fastboot.c b/common/fastboot.c
> index f8a01dea7a65..d283fc8fc098 100644
> --- a/common/fastboot.c
> +++ b/common/fastboot.c
> @@ -789,7 +789,7 @@ static void cb_erase(struct fastboot *fb, const char *cmd)
>  	if (fd < 0)
>  		fastboot_tx_print(fb, FASTBOOT_MSG_FAIL, strerror(-fd));
>  
> -	ret = erase(fd, ERASE_SIZE_ALL, 0);
> +	ret = erase(fd, ERASE_SIZE_ALL, 0, ERASE_TO_CLEAR);
>  
>  	close(fd);
>  
> diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c
> index 2ac5bf6a8218..6b5873aa9af1 100644
> --- a/common/state/backend_bucket_circular.c
> +++ b/common/state/backend_bucket_circular.c
> @@ -220,7 +220,7 @@ static int state_mtd_peb_write(struct state_backend_storage_bucket_circular *cir
>  static int state_mtd_peb_erase(struct state_backend_storage_bucket_circular *circ)
>  {
>  	return erase(circ->fd, circ->mtd->erasesize,
> -		     (off_t)circ->eraseblock * circ->mtd->erasesize);
> +		     (off_t)circ->eraseblock * circ->mtd->erasesize, ERASE_TO_WRITE);
>  }
>  #endif
>  
> diff --git a/drivers/misc/storage-by-uuid.c b/drivers/misc/storage-by-uuid.c
> index db15b64d7dcf..8b8fd901685e 100644
> --- a/drivers/misc/storage-by-uuid.c
> +++ b/drivers/misc/storage-by-uuid.c
> @@ -126,6 +126,9 @@ static void storage_by_uuid_add_partitions(struct sbu *sbu, struct cdev *rcdev)
>  	sbu->cdev.dev = sbu->dev;
>  	sbu->cdev.priv = sbu;
>  
> +	if (rcdev->flags & DEVFS_WRITE_AUTOERASE)
> +		sbu->cdev.flags |= DEVFS_WRITE_AUTOERASE;
> +
>  	ret = devfs_create(&sbu->cdev);
>  	if (ret) {
>  		dev_err(sbu->dev, "Failed to create cdev: %s\n", strerror(-ret));
> diff --git a/drivers/usb/gadget/function/dfu.c b/drivers/usb/gadget/function/dfu.c
> index a41ff22dcebc..208e0cd23c29 100644
> --- a/drivers/usb/gadget/function/dfu.c
> +++ b/drivers/usb/gadget/function/dfu.c
> @@ -208,7 +208,7 @@ static void dfu_do_write(struct dfu_work *dw)
>  
>  	if (prog_erase && (dfu_written + wlen) > dfu_erased) {
>  		size = roundup(wlen, dfu_mtdinfo.erasesize);
> -		ret = erase(dfufd, size, dfu_erased);
> +		ret = erase(dfufd, size, dfu_erased, ERASE_TO_WRITE);
>  		dfu_erased += size;
>  		if (ret && ret != -ENOSYS) {
>  			perror("erase");
> @@ -333,7 +333,7 @@ static void dfu_do_copy(struct dfu_work *dw)
>  		return;
>  	}
>  
> -	ret = erase(fd, ERASE_SIZE_ALL, 0);
> +	ret = erase_flash(fd, ERASE_SIZE_ALL, 0);
>  	close(fd);
>  	if (ret && ret != -ENOSYS) {
>  		perror("erase");
> diff --git a/fs/devfs-core.c b/fs/devfs-core.c
> index ed445fbd4712..2715193c6956 100644
> --- a/fs/devfs-core.c
> +++ b/fs/devfs-core.c
> @@ -509,6 +509,7 @@ static struct cdev *__devfs_add_partition(struct cdev *cdev,
>  	loff_t _end = end ? *end : 0;
>  	static struct cdev *new;
>  	struct cdev *overlap;
> +	unsigned inherited_flags = 0;
>  
>  	if (cdev_by_name(partinfo->name))
>  		return ERR_PTR(-EEXIST);
> @@ -551,11 +552,14 @@ static struct cdev *__devfs_add_partition(struct cdev *cdev,
>  		return overlap;
>  	}
>  
> +	/* Filter flags that we want to pass along to children */
> +	inherited_flags |= cdev->flags & DEVFS_WRITE_AUTOERASE;
> +
>  	if (IS_ENABLED(CONFIG_MTD) && cdev->mtd) {
>  		struct mtd_info *mtd;
>  
>  		mtd = mtd_add_partition(cdev->mtd, offset, size,
> -				partinfo->flags, partinfo->name);
> +				partinfo->flags | inherited_flags, partinfo->name);
>  		if (IS_ERR(mtd))
>  			return (void *)mtd;
>  
> @@ -571,6 +575,7 @@ static struct cdev *__devfs_add_partition(struct cdev *cdev,
>  	new->priv = cdev->priv;
>  	new->size = size;
>  	new->offset = cdev->offset + offset;
> +	new->flags = inherited_flags;
>  
>  	new->dev = cdev->dev;
>  	new->master = cdev;
> diff --git a/fs/devfs.c b/fs/devfs.c
> index 9dbfa91b1d9f..5feb76cfef86 100644
> --- a/fs/devfs.c
> +++ b/fs/devfs.c
> @@ -61,13 +61,16 @@ static int devfs_lseek(struct device *_dev, FILE *f, loff_t pos)
>  }
>  
>  static int devfs_erase(struct device *_dev, FILE *f, loff_t count,
> -		       loff_t offset)
> +		       loff_t offset, enum erase_type type)
>  {
>  	struct cdev *cdev = f->priv;
>  
>  	if (cdev->flags & DEVFS_PARTITION_READONLY)
>  		return -EPERM;
>  
> +	if ((type == ERASE_TO_WRITE) && (cdev->flags & DEVFS_WRITE_AUTOERASE))
> +		return 0;
> +
>  	if (count + offset > cdev->size)
>  		count = cdev->size - offset;
>  
> diff --git a/fs/fs.c b/fs/fs.c
> index 656434f67ffa..5e7e05f4990a 100644
> --- a/fs/fs.c
> +++ b/fs/fs.c
> @@ -600,7 +600,7 @@ loff_t lseek(int fd, loff_t offset, int whence)
>  }
>  EXPORT_SYMBOL(lseek);
>  
> -int erase(int fd, loff_t count, loff_t offset)
> +int erase(int fd, loff_t count, loff_t offset, enum erase_type type)
>  {
>  	struct fs_driver *fsdrv;
>  	FILE *f = fd_to_file(fd, false);
> @@ -621,7 +621,7 @@ int erase(int fd, loff_t count, loff_t offset)
>  		assert_command_context();
>  
>  	if (fsdrv->erase)
> -		ret = fsdrv->erase(&f->fsdev->dev, f, count, offset);
> +		ret = fsdrv->erase(&f->fsdev->dev, f, count, offset, type);
>  	else
>  		ret = -ENOSYS;
>  
> diff --git a/include/driver.h b/include/driver.h
> index 2b4db2221a6c..c4067d531cc7 100644
> --- a/include/driver.h
> +++ b/include/driver.h
> @@ -562,6 +562,19 @@ extern struct list_head cdev_list;
>  #define DEVFS_PARTITION_BOOTABLE_LEGACY	(1U << 11)
>  #define DEVFS_PARTITION_BOOTABLE_ESP	(1U << 12)
>  #define DEVFS_PARTITION_FOR_FIXUP	(1U << 13)
> +#define DEVFS_WRITE_AUTOERASE		(1U << 14)
> +
> +/**
> + * cdev_write_requires_erase - Check whether writes must be done to erased blocks
> + * @cdev: pointer to cdev structure
> + *
> + * This function checks if the erase operation is required, so a subsequent
> + * write operation can write all bits.
> + */
> +static inline bool cdev_write_requires_erase(const struct cdev *cdev)
> +{
> +	return !(cdev->flags & DEVFS_WRITE_AUTOERASE);
> +}
>  
>  static inline bool cdev_is_mbr_partitioned(const struct cdev *master)
>  {
> diff --git a/include/fs.h b/include/fs.h
> index f87c4c40273f..f5950a052923 100644
> --- a/include/fs.h
> +++ b/include/fs.h
> @@ -10,6 +10,7 @@
>  #include <sys/stat.h>
>  #include <driver.h>
>  #include <filetype.h>
> +#include <linux/bits.h>
>  #include <linux/fs.h>
>  #include <linux/string.h>
>  #include <linux/limits.h>
> @@ -38,6 +39,27 @@ typedef struct filep {
>  
>  #define FS_DRIVER_NO_DEV	1
>  
> +/**
> + * enum erase_type - Type of erase operation
> + * @ERASE_TO_WRITE: Conduct low-level erase operation to prepare for a write
> + *                  to all or part of the erased regions. This is required
> + *                  for raw flashes, but can be omitted by flashes behind
> + *                  a FTL that autoerases on write (e.g. eMMCs)
> + * @ERASE_TO_CLEAR: Force an erase of the region. When read afterwards,
> + *		    A fixed pattern should result, usually either all-zeroes
> + *		    or all-ones depending on storage technology.
> + *
> + * Many managed flashes provide an erase operation, which need not be used
> + * for regular writes, but is provided nonetheless for efficient clearing
> + * of large regions of storage to a fixed bit pattern. The erase type allows
> + * users to specify _why_ the erase operation is done, so the driver or
> + * the core code can choose the most optimal operation.
> + */
> +enum erase_type {
> +	ERASE_TO_WRITE,
> +	ERASE_TO_CLEAR
> +};
> +
>  struct fs_driver {
>  	int (*probe) (struct device *dev);
>  
> @@ -58,7 +80,7 @@ struct fs_driver {
>  
>  	int (*ioctl)(struct device *dev, FILE *f, unsigned int request, void *buf);
>  	int (*erase)(struct device *dev, FILE *f, loff_t count,
> -			loff_t offset);
> +			loff_t offset, enum erase_type type);
>  	int (*protect)(struct device *dev, FILE *f, size_t count,
>  			loff_t offset, int prot);
>  	int (*discard_range)(struct device *dev, FILE *f, loff_t count,
> @@ -127,7 +149,7 @@ int umount_by_cdev(struct cdev *cdev);
>  
>  /* not-so-standard functions */
>  #define ERASE_SIZE_ALL	((loff_t) - 1)
> -int erase(int fd, loff_t count, loff_t offset);
> +int erase(int fd, loff_t count, loff_t offset, enum erase_type type);
>  int protect(int fd, size_t count, loff_t offset, int prot);
>  int discard_range(int fd, loff_t count, loff_t offset);
>  int protect_file(const char *file, int prot);
> diff --git a/lib/libfile.c b/lib/libfile.c
> index a34e011f4f7c..80d4591dd6e5 100644
> --- a/lib/libfile.c
> +++ b/lib/libfile.c
> @@ -412,7 +412,7 @@ int write_file_flash(const char *filename, const void *buf, size_t size)
>  	if (fd < 0)
>  		return fd;
>  
> -	ret = erase(fd, size, 0);
> +	ret = erase(fd, size, 0, ERASE_TO_WRITE);
>  	if (ret < 0)
>  		goto out_close;
>  
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



  reply	other threads:[~2024-07-30  8:36 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-30  7:19 [PATCH 00/10] mmc: add SD/eMMC erase support Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 01/10] fs: give erase() a new erase_type parameter Ahmad Fatoum
2024-07-30  8:31   ` Sascha Hauer [this message]
2024-07-30  8:32     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 02/10] block: factor out chunk_flush helper Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 03/10] block: allow block devices to implement the cdev erase operation Ahmad Fatoum
2024-07-30  8:55   ` Sascha Hauer
2024-07-30 11:10     ` Ahmad Fatoum
2024-07-30 11:21       ` Sascha Hauer
2024-07-30  7:19 ` [PATCH 04/10] mci: turn bool members into bitfield in struct mci Ahmad Fatoum
2024-07-30  9:06   ` Sascha Hauer
2024-07-30 11:10     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 05/10] mci: describe more command structure in mci.h Ahmad Fatoum
2024-07-30  9:25   ` Yann Sionneau
2024-07-30 11:07     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 06/10] mci: core: use CONFIG_MCI_WRITE, not CONFIG_BLOCK_WRITE Ahmad Fatoum
2024-07-30  9:18   ` Sascha Hauer
2024-07-30 11:08     ` Ahmad Fatoum
2024-07-31  7:19       ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 07/10] mci: add support for discarding write blocks Ahmad Fatoum
2024-07-30  9:23   ` Yann Sionneau
2024-07-30 11:14     ` Ahmad Fatoum
2024-07-30 10:05   ` Sascha Hauer
2024-07-30 11:17     ` Ahmad Fatoum
2024-07-30  7:19 ` [PATCH 08/10] commands: sync: add new command to flush cached writes Ahmad Fatoum
2024-07-30 10:08   ` Sascha Hauer
2024-07-30  7:19 ` [PATCH 09/10] mci: core: remove reference to SD Card from common mci_card_probe Ahmad Fatoum
2024-07-30 10:09   ` Sascha Hauer
2024-07-30  7:19 ` [PATCH 10/10] commands: blkstats: add command to print block device statistics Ahmad Fatoum

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=Zqik_wu9O7wYMFzh@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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