lseek related fixes
Subsystems (6)
cover lseek related fixes
Everyone:
As a follow up to lseek() related fixes that went in recently, I went throught all of the calls to it in the codebase I could find and fixed a number of type problems as well as some other minor issue. This series is the result. Hopefuly all of the patches are self-explanatory.
Feedback is welcome!
Thanks,
Andrey Smirnov
Andrey Smirnov (17):
libfile: Make failure path of open_and_lseek() consistent
common: Always return enum filetype in file_name_detect_type_offset()
MIPS: ath79: Use errno to get error code from open_and_lseek()
ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update()
ARM: i.MX: bbu: Fix lseek error check in
imx_bbu_external_nand_update()
ARM: i.MX: bbu: Fix lseek error check in
imx_bbu_internal_v2_write_nand_dbbt()
ARM: i.MX: bbu: Fix variable type in
imx_bbu_internal_v2_write_nand_dbbt()
bpkfs: Fix lseek error check in bpkfs_probe()
uimage: Fix lseek error check in uimage_verify()
uimage: Fix lseek error check in uimage_load()
uimage: Fix lseek error check in uimage_load_to_buf()
state: Fix lseek error check in state_backend_bucket_direct_read()
state: Fix lseek error check in state_backend_bucket_direct_write()
state: Fix lseek error check in state_mtd_peb_read()
state: Fix lseek error check in state_mtd_peb_write()
commands: loadxy: Make use of open_and_lseek()
commands: loadb: Make use of open_and_lseek()
arch/arm/mach-imx/imx-bbu-external-nand.c | 10 ++++--- arch/arm/mach-imx/imx-bbu-internal.c | 10 ++++--- arch/mips/mach-ath79/art.c | 4 +-- commands/loadb.c | 12 +-------- commands/loadxy.c | 13 ++------- common/filetype.c | 4 +-- common/state/backend_bucket_circular.c | 14 +++++----- common/state/backend_bucket_direct.c | 23 ++++++++-------- common/uimage.c | 32 +++++++++++------------ fs/bpkfs.c | 4 +-- lib/libfile.c | 26 +++++++++--------- 11 files changed, 65 insertions(+), 87 deletions(-)
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 1/17 libfile: Make failure path of open_and_lseek() consistent
Change the code of open_and_lseek() to make sure that opened file is closed in every failure path as well. While at it make sure we don't mix returning function return code and returning errno by opting to always return the former.
--- lib/libfile.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
lib/libfile.c +12 -14
@@ -522,12 +522,12 @@ err_out1:* @mode: The file open mode * @pos: The position to lseek to * -* Return: If successful this function returns a positive filedescriptor -* number, otherwise a negative error code is returned +* Return: If successful this function returns a positive +* filedescriptor number, otherwise -1 is returned */ int open_and_lseek (const char *filename,int mode,loff_t pos) { -int fd, ret; +int fd; fd =open (filename, mode);if (fd <0 ) {
@@ -541,28 +541,26 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)if (mode & (O_WRONLY |O_RDWR )) {struct stat s; - ret =fstat (fd, &s); -if (ret) { +if (fstat (fd, &s)) {perror ("fstat" ); -return ret; + goto out; } -if (s.st_size < pos) { - ret =ftruncate (fd, pos); -if (ret) { -perror ("ftruncate" ); -return ret; - } +if (s.st_size < pos &&ftruncate (fd, pos)) { +perror ("ftruncate" ); + goto out; } }if (lseek (fd, pos,SEEK_SET ) != pos) {perror ("lseek" ); -close (fd); -return -errno; + goto out; }return fd; +out: +close (fd); +return -1 ; }/**
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 2/17 common: Always return enum filetype in file_name_detect_type_offset()
In reply to Andrey SmirnovNone of the callers of file_name_detect_type_offset() are prepared to deal with negative error code. Change the code to return filetype_unknown if open_and_lseek() fails.
--- common/filetype.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
common/filetype.c +2 -2
@@ -383,7 +383,7 @@ enum filetype file_name_detect_type_offset(const char *filename, loff_t pos) fd =open_and_lseek (filename,O_RDONLY , pos);if (fd <0 ) -return fd; + goto out; buf =xzalloc (FILE_TYPE_SAFE_BUFSIZE );
@@ -396,7 +396,7 @@ enum filetype file_name_detect_type_offset(const char *filename, loff_t pos) err_out:close (fd);free (buf); - +out:return type; }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 3/17 MIPS: ath79: Use errno to get error code from open_and_lseek()
In reply to Andrey SmirnovOpen_and_lseek() return actual error code via errno, so change the code to use it instead of return value.
--- arch/mips/mach-ath79/art.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/mach-ath79/art.c +2 -2
@@ -44,8 +44,8 @@ static int art_read_mac(struct device_d *dev, const char *file) fd =open_and_lseek (file,O_RDONLY ,AR93000_EPPROM_OFFSET );if (fd <0 ) {dev_err (dev,"Failed to open eeprom path %s %d\n" , - file, fd); -return fd; + file, -errno); +return -errno; } rbytes =read_full (fd, &eeprom,sizeof (eeprom));
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 4/17 ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update()
In reply to Andrey SmirnovMEMGETBADBLOCK returns loff_t, so that's the type we should use to store its result.
--- arch/arm/mach-imx/imx-bbu-external-nand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-imx/imx-bbu-external-nand.c +1 -1
@@ -40,7 +40,7 @@ static int imx_bbu_external_nand_update(struct bbu_handler *handler, struct bbu_int size_available, size_need;int ret;uint32_t num_bb =0 , bbt =0 ; -uint64_t offset =0 ; +loff_t offset =0 ;int block =0 , len, now, blocksize;void *image = data->image;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 5/17 ARM: i.MX: bbu: Fix lseek error check in imx_bbu_external_nand_update()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- arch/arm/mach-imx/imx-bbu-external-nand.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm/mach-imx/imx-bbu-external-nand.c +5 -3
@@ -147,10 +147,12 @@ static int imx_bbu_external_nand_update(struct bbu_handler *handler, struct bbu_ goto out;if (ret) { - ret =lseek (fd, offset + blocksize,SEEK_SET ); -if (ret <0 ) - goto out; offset += blocksize; +if (lseek (fd, offset,SEEK_SET ) != offset) { + ret = -errno; + goto out; + } +continue ; }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 6/17 ARM: i.MX: bbu: Fix lseek error check in imx_bbu_internal_v2_write_nand_dbbt()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- arch/arm/mach-imx/imx-bbu-internal.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm/mach-imx/imx-bbu-internal.c +5 -3
@@ -330,10 +330,12 @@ static int imx_bbu_internal_v2_write_nand_dbbt(struct imx_internal_bbu_handler * goto out;if (ret) { - ret =lseek (fd, offset + blocksize,SEEK_SET ); -if (ret <0 ) - goto out; offset += blocksize; +if (lseek (fd, offset,SEEK_SET ) != offset) { + ret = -errno; + goto out; + } +continue ; }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 7/17 ARM: i.MX: bbu: Fix variable type in imx_bbu_internal_v2_write_nand_dbbt()
In reply to Andrey SmirnovMEMGETBADBLOCK returns loff_t, so that's the type we should use to store its result.
--- arch/arm/mach-imx/imx-bbu-internal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-imx/imx-bbu-internal.c +1 -1
@@ -207,7 +207,7 @@ static int imx_bbu_internal_v2_write_nand_dbbt(struct imx_internal_bbu_handler *int size_available, size_need;int ret;uint32_t *ptr, *num_bb, *bb; -uint64_t offset; +loff_t offset;int block =0 , len, now, blocksize;int dbbt_start_page =4 ;int firmware_start_page =12 ;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 8/17 bpkfs: Fix lseek error check in bpkfs_probe()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- fs/bpkfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bpkfs.c +2 -2
@@ -455,9 +455,9 @@ static int bpkfs_probe(struct device_d *dev)list_add_tail (&d->list, &h->list_data); priv->nb_data_entries++; - ret =lseek (fd, d->size,SEEK_CUR ); -if (ret <0 ) { +if (lseek (fd, d->size,SEEK_CUR ) != d->size) {dev_err (dev,"could not seek: %s\n" ,errno_str ()); + ret = -errno; goto err; }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 9/17 uimage: Fix lseek error check in uimage_verify()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- common/uimage.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
common/uimage.c +4 -3
@@ -265,11 +265,12 @@ int uimage_verify(struct uimage_handle *handle) {u32 crc =0 ;int len, ret; +loff_t off;void *buf; - ret =lseek (handle->fd,sizeof (struct image_header ),SEEK_SET ); -if (ret <0 ) -return ret; + off =sizeof (struct image_header ); +if (lseek (handle->fd, off,SEEK_SET ) != off) +return -errno; buf =xmalloc (PAGE_SIZE );
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 10/17 uimage: Fix lseek error check in uimage_load()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- common/uimage.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
common/uimage.c +4 -4
@@ -308,6 +308,7 @@ int uimage_load(struct uimage_handle *handle, unsigned int image_no,image_header_t *hdr = &handle->header;struct uimage_handle_data *iha;int ret; +loff_t off; int (*uncompress_fn)(unsigned char *inbuf, int len, int(*fill)(void*, unsigned int), int(*flush)(void*, unsigned int),
@@ -320,10 +321,9 @@ int uimage_load(struct uimage_handle *handle, unsigned int image_no, iha = &handle->ihd[image_no]; - ret =lseek (handle->fd, iha->offset + handle->data_offset, -SEEK_SET ); -if (ret <0 ) -return ret; + off = iha->offset + handle->data_offset; +if (lseek (handle->fd, off,SEEK_SET ) != off) +return -errno;/* if ramdisk U-Boot expect to ignore the compression type */ if (hdr->ih_comp ==IH_COMP_NONE || hdr->ih_type ==IH_TYPE_RAMDISK )
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 11/17 uimage: Fix lseek error check in uimage_load_to_buf()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets.
--- common/uimage.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
common/uimage.c +7 -10
@@ -457,6 +457,7 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, {u32 size;int ret; +loff_t off;struct uimage_handle_data *ihd;char ftbuf[128 ];enum filetype ft;
@@ -467,9 +468,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, ihd = &handle->ihd[image_no]; - ret =lseek (handle->fd, ihd->offset + handle->data_offset, -SEEK_SET ); -if (ret <0 ) + off = ihd->offset + handle->data_offset; +if (lseek (handle->fd, off,SEEK_SET ) != off)return NULL ;if (handle->header.ih_comp ==IH_COMP_NONE ) {
@@ -497,10 +497,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,if (ft != filetype_gzip)return NULL ; - ret =lseek (handle->fd, ihd->offset + handle->data_offset + - ihd->len -4 , -SEEK_SET ); -if (ret <0 ) + off = ihd->offset + handle->data_offset + ihd->len -4 ; +if (lseek (handle->fd, off,SEEK_SET ) != off)return NULL ; ret =read (handle->fd, &size,4 );
@@ -509,9 +507,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, size =le32_to_cpu (size); - ret =lseek (handle->fd, ihd->offset + handle->data_offset, -SEEK_SET ); -if (ret <0 ) + off = ihd->offset + handle->data_offset; +if (lseek (handle->fd, off,SEEK_SET ) != off)return NULL ; buf =malloc (size);
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 12/17 state: Fix lseek error check in state_backend_bucket_direct_read()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- common/state/backend_bucket_direct.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
common/state/backend_bucket_direct.c +8 -8
@@ -56,10 +56,9 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucketvoid *buf;int ret; - ret =lseek (direct->fd, direct->offset,SEEK_SET ); -if (ret <0 ) { -dev_err (direct->dev,"Failed to seek file, %d\n" , ret); -return ret; +if (lseek (direct->fd, direct->offset,SEEK_SET ) != direct->offset) { +dev_err (direct->dev,"Failed to seek file, %d\n" , -errno); +return -errno; } ret =read_full (direct->fd, &meta,sizeof (meta));if (ret <0 ) {
@@ -77,10 +76,11 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucketreturn -EINVAL ; } read_len = direct->max_size; - ret =lseek (direct->fd, direct->offset,SEEK_SET ); -if (ret <0 ) { -dev_err (direct->dev,"Failed to seek file, %d\n" , ret); -return ret; +if (lseek (direct->fd, direct->offset,SEEK_SET ) != + direct->offset) { +dev_err (direct->dev,"Failed to seek file, %d\n" , + -errno); +return -errno; } }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 13/17 state: Fix lseek error check in state_backend_bucket_direct_write()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- common/state/backend_bucket_direct.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
common/state/backend_bucket_direct.c +3 -4
@@ -113,10 +113,9 @@ static int state_backend_bucket_direct_write(struct state_backend_storage_bucketif (len > direct->max_size -sizeof (meta))return -E2BIG ; - ret =lseek (direct->fd, direct->offset,SEEK_SET ); -if (ret <0 ) { -dev_err (direct->dev,"Failed to seek file, %d\n" , ret); -return ret; +if (lseek (direct->fd, direct->offset,SEEK_SET ) != direct->offset) { +dev_err (direct->dev,"Failed to seek file, %d\n" , -errno); +return -errno; }/* write the meta data only if there is head room */
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 14/17 state: Fix lseek error check in state_mtd_peb_read()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- common/state/backend_bucket_circular.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
common/state/backend_bucket_circular.c +3 -4
@@ -162,11 +162,10 @@ static int state_mtd_peb_read(struct state_backend_storage_bucket_circular *circ offset += (off_t )circ->eraseblock * circ->mtd->erasesize; - ret =lseek (circ->fd, offset,SEEK_SET ); -if (ret <0 ) { +if (lseek (circ->fd, offset,SEEK_SET ) != offset) {dev_err (circ->dev,"Failed to set circular read position to %lld, %d\n" , - (long long ) offset, ret); -return ret; + (long long ) offset, -errno); +return -errno; } dev_dbg(circ->dev, "Read state from %lld length %d\n", (long long) offset,
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 15/17 state: Fix lseek error check in state_mtd_peb_write()
In reply to Andrey SmirnovDon't use 'int' to store lseek()'s return value to avoid problems with large seek offsets. While at it, make sure to populate return error code from 'errno'.
--- common/state/backend_bucket_circular.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
common/state/backend_bucket_circular.c +3 -4
@@ -190,11 +190,10 @@ static int state_mtd_peb_write(struct state_backend_storage_bucket_circular *cir offset += circ->eraseblock * circ->mtd->erasesize; - ret =lseek (circ->fd, offset,SEEK_SET ); -if (ret <0 ) { +if (lseek (circ->fd, offset,SEEK_SET ) != offset) {dev_err (circ->dev,"Failed to set position for circular write %lld, %d\n" , - (long long ) offset, ret); -return ret; + (long long ) offset, -errno); +return -errno; } ret =write_full (circ->fd, buf, len);
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 16/17 commands: loadxy: Make use of open_and_lseek()
In reply to Andrey SmirnovSave a bit of extra code by replacing explict calls to open() and lseek() with a single call to open_and_lseek().
--- commands/loadxy.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
commands/loadxy.c +2 -11
@@ -37,6 +37,7 @@#include <fcntl.h> #include <fs.h> #include <malloc.h> +#include <libfile.h> #define DEF_FILE "image.bin"
@@ -176,21 +177,11 @@ static int do_loadx(int argc, char *argv[]) output_file =DEF_FILE ;/* File should exist */ - ofd =open (output_file,O_WRONLY |O_CREAT ); + ofd =open_and_lseek (output_file,O_WRONLY |O_CREAT , offset);if (ofd <0 ) {perror (argv[0 ]);return 3 ; } -/* Seek to the right offset */ -if (offset) { -int seek =lseek (ofd, offset,SEEK_SET ); -if (seek != offset) { -close (ofd); - ofd =0 ; -perror (argv[0 ]); -return 4 ; - } - } current_baudrate =console_get_baudrate (cdev);
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 17/17 commands: loadb: Make use of open_and_lseek()
In reply to Andrey SmirnovSave a bit of extra code by replacing explict calls to open() and lseek() with a single call to open_and_lseek().
--- commands/loadb.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
commands/loadb.c +1 -11
@@ -661,21 +661,11 @@ static int do_load_serial_bin(int argc, char *argv[]) output_file =DEF_FILE ;/* File should exist */ - ofd =open (output_file,O_WRONLY |O_CREAT ); + ofd =open_and_lseek (output_file,O_WRONLY |O_CREAT , offset);if (ofd <0 ) {perror (argv[0 ]);return 3 ; } -/* Seek to the right offset */ -if (offset) { -int seek =lseek (ofd, offset,SEEK_SET ); -if (seek != offset) { -close (ofd); - ofd =0 ; -perror (argv[0 ]); -return 4 ; - } - } printf("## Ready for binary (kermit) download " "to 0x%08lX offset on %s device at %d bps...\n", offset,
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Quoted 9 lines · Andrey Smirnov
Everyone:
As a follow up to
lseek()related fixes that went in recently, I went throught all of the calls to it in the codebase I could find and fixed a number of type problems as well as some other minor issue. This series is the result. Hopefuly all of the patches are self-explanatory.Feedback is welcome!
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox