From: Hongzhen Luo Date: Thu, 6 Jun 2024 11:18:33 +0000 (+0800) Subject: erofs-utils: lib: support virtual files X-Git-Tag: v1.8~58 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=db5998d22bb0be6cc88310eb81227a455c7ee893;p=platform%2Fupstream%2Ferofs-utils.git erofs-utils: lib: support virtual files The current erofs-utils I/O implementation is through file descriptors. The new `erofs_vfile` provides a more flexible way to perform I/Os. Signed-off-by: Hongzhen Luo Link: https://lore.kernel.org/r/20240606111833.2389455-1-hongzhen@linux.alibaba.com [ Gao Xiang: minor styling fixes. ] Signed-off-by: Gao Xiang --- diff --git a/dump/main.c b/dump/main.c index 50f4662..7c77c92 100644 --- a/dump/main.c +++ b/dump/main.c @@ -12,7 +12,6 @@ #include #include "erofs/print.h" #include "erofs/inode.h" -#include "erofs/io.h" #include "erofs/dir.h" #include "erofs/compress.h" #include "erofs/fragments.h" @@ -167,7 +166,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv) usage(argc, argv); exit(0); case 3: - err = blob_open_ro(&sbi, optarg); + err = erofs_blob_open_ro(&sbi, optarg); if (err) return err; ++sbi.extra_devices; @@ -181,7 +180,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv) dumpcfg.show_subdirectories = true; break; case 6: - sbi.diskoffset = strtoull(optarg, &endptr, 0); + sbi.bdev.offset = strtoull(optarg, &endptr, 0); if (*endptr != '\0') { erofs_err("invalid disk offset %s", optarg); return -EINVAL; @@ -685,7 +684,7 @@ int main(int argc, char **argv) goto exit; } - err = dev_open_ro(&sbi, cfg.c_img_path); + err = erofs_dev_open(&sbi, cfg.c_img_path, O_RDONLY | O_TRUNC); if (err) { erofs_err("failed to open image file"); goto exit; @@ -718,9 +717,9 @@ int main(int argc, char **argv) exit_put_super: erofs_put_super(&sbi); exit_dev_close: - dev_close(&sbi); + erofs_dev_close(&sbi); exit: - blob_closeall(&sbi); + erofs_blob_closeall(&sbi); erofs_exit_configure(); return err; } diff --git a/fsck/main.c b/fsck/main.c index 4dcb49d..a505e99 100644 --- a/fsck/main.c +++ b/fsck/main.c @@ -10,7 +10,6 @@ #include #include #include "erofs/print.h" -#include "erofs/io.h" #include "erofs/compress.h" #include "erofs/decompress.h" #include "erofs/dir.h" @@ -184,7 +183,7 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv) } break; case 3: - ret = blob_open_ro(&sbi, optarg); + ret = erofs_blob_open_ro(&sbi, optarg); if (ret) return ret; ++sbi.extra_devices; @@ -220,7 +219,7 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv) has_opt_preserve = true; break; case 12: - sbi.diskoffset = strtoull(optarg, &endptr, 0); + sbi.bdev.offset = strtoull(optarg, &endptr, 0); if (*endptr != '\0') { erofs_err("invalid disk offset %s", optarg); return -EINVAL; @@ -312,7 +311,7 @@ static int erofs_check_sb_chksum(void) struct erofs_super_block *sb; int ret; - ret = blk_read(&sbi, 0, buf, 0, 1); + ret = erofs_blk_read(&sbi, 0, buf, 0, 1); if (ret) { erofs_err("failed to read superblock to check checksum: %d", ret); @@ -360,7 +359,7 @@ static int erofs_verify_xattr(struct erofs_inode *inode) } addr = erofs_iloc(inode) + inode->inode_isize; - ret = dev_read(sbi, 0, buf, addr, xattr_hdr_size); + ret = erofs_dev_read(sbi, 0, buf, addr, xattr_hdr_size); if (ret < 0) { erofs_err("failed to read xattr header @ nid %llu: %d", inode->nid | 0ULL, ret); @@ -390,7 +389,7 @@ static int erofs_verify_xattr(struct erofs_inode *inode) while (remaining > 0) { unsigned int entry_sz; - ret = dev_read(sbi, 0, buf, addr, xattr_entry_size); + ret = erofs_dev_read(sbi, 0, buf, addr, xattr_entry_size); if (ret) { erofs_err("failed to read xattr entry @ nid %llu: %d", inode->nid | 0ULL, ret); @@ -966,7 +965,7 @@ int main(int argc, char *argv[]) cfg.c_dbg_lvl = -1; #endif - err = dev_open_ro(&sbi, cfg.c_img_path); + err = erofs_dev_open(&sbi, cfg.c_img_path, O_RDONLY); if (err) { erofs_err("failed to open image file"); goto exit; @@ -1022,9 +1021,9 @@ exit_hardlink: exit_put_super: erofs_put_super(&sbi); exit_dev_close: - dev_close(&sbi); + erofs_dev_close(&sbi); exit: - blob_closeall(&sbi); + erofs_blob_closeall(&sbi); erofs_exit_configure(); return err ? 1 : 0; } diff --git a/fuse/main.c b/fuse/main.c index 32f59a3..70e3218 100644 --- a/fuse/main.c +++ b/fuse/main.c @@ -10,7 +10,6 @@ #include "macosx.h" #include "erofs/config.h" #include "erofs/print.h" -#include "erofs/io.h" #include "erofs/dir.h" #include "erofs/inode.h" @@ -574,7 +573,7 @@ static int optional_opt_func(void *data, const char *arg, int key, switch (key) { case 1: - ret = blob_open_ro(&sbi, arg + sizeof("--device=") - 1); + ret = erofs_blob_open_ro(&sbi, arg + sizeof("--device=") - 1); if (ret) return -1; ++sbi.extra_devices; @@ -676,8 +675,8 @@ int main(int argc, char *argv[]) if (fusecfg.odebug && cfg.c_dbg_lvl < EROFS_DBG) cfg.c_dbg_lvl = EROFS_DBG; - sbi.diskoffset = fusecfg.offset; - ret = dev_open_ro(&sbi, fusecfg.disk); + sbi.bdev.offset = fusecfg.offset; + ret = erofs_dev_open(&sbi, fusecfg.disk, O_RDONLY); if (ret) { fprintf(stderr, "failed to open: %s\n", fusecfg.disk); goto err_fuse_free_args; @@ -747,8 +746,8 @@ int main(int argc, char *argv[]) err_super_put: erofs_put_super(&sbi); err_dev_close: - blob_closeall(&sbi); - dev_close(&sbi); + erofs_blob_closeall(&sbi); + erofs_dev_close(&sbi); err_fuse_free_args: free(opts.mountpoint); fuse_opt_free_args(&args); diff --git a/include/erofs/internal.h b/include/erofs/internal.h index 2067cb9..d93bfab 100644 --- a/include/erofs/internal.h +++ b/include/erofs/internal.h @@ -26,6 +26,7 @@ typedef unsigned short umode_t; #include #endif #include "atomic.h" +#include "io.h" #ifndef PATH_MAX #define PATH_MAX 4096 /* # chars in a path name including nul */ @@ -115,9 +116,8 @@ struct erofs_sb_info { u8 xattr_prefix_count; struct erofs_xattr_prefix_item *xattr_prefixes; - int devfd, devblksz; - /* offset when reading multi-part images */ - u64 diskoffset; + struct erofs_vfile bdev; + int devblksz; u64 devsz; dev_t dev; unsigned int nblobs; @@ -443,6 +443,47 @@ int z_erofs_fill_inode(struct erofs_inode *vi); int z_erofs_map_blocks_iter(struct erofs_inode *vi, struct erofs_map_blocks *map, int flags); +/* io.c */ +int erofs_dev_open(struct erofs_sb_info *sbi, const char *dev, int flags); +void erofs_dev_close(struct erofs_sb_info *sbi); +void erofs_blob_closeall(struct erofs_sb_info *sbi); +int erofs_blob_open_ro(struct erofs_sb_info *sbi, const char *dev); + +int erofs_dev_read(struct erofs_sb_info *sbi, int device_id, + void *buf, u64 offset, size_t len); + +static inline int erofs_dev_write(struct erofs_sb_info *sbi, const void *buf, + u64 offset, size_t len) +{ + return erofs_io_pwrite(&sbi->bdev, buf, offset, len); +} + +static inline int erofs_dev_fillzero(struct erofs_sb_info *sbi, u64 offset, + size_t len, bool pad) +{ + return erofs_io_fallocate(&sbi->bdev, offset, len, pad); +} + +static inline int erofs_dev_resize(struct erofs_sb_info *sbi, + erofs_blk_t blocks) +{ + return erofs_io_ftruncate(&sbi->bdev, (u64)blocks * erofs_blksiz(sbi)); +} + +static inline int erofs_blk_write(struct erofs_sb_info *sbi, const void *buf, + erofs_blk_t blkaddr, u32 nblocks) +{ + return erofs_dev_write(sbi, buf, erofs_pos(sbi, blkaddr), + erofs_pos(sbi, nblocks)); +} + +static inline int erofs_blk_read(struct erofs_sb_info *sbi, int device_id, + void *buf, erofs_blk_t start, u32 nblocks) +{ + return erofs_dev_read(sbi, device_id, buf, erofs_pos(sbi, start), + erofs_pos(sbi, nblocks)); +} + #ifdef EUCLEAN #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */ #else diff --git a/include/erofs/io.h b/include/erofs/io.h index 4db5716..c82dfdf 100644 --- a/include/erofs/io.h +++ b/include/erofs/io.h @@ -16,44 +16,37 @@ extern "C" #define _GNU_SOURCE #endif #include -#include "internal.h" +#include "defs.h" #ifndef O_BINARY #define O_BINARY 0 #endif -void blob_closeall(struct erofs_sb_info *sbi); -int blob_open_ro(struct erofs_sb_info *sbi, const char *dev); -int dev_open(struct erofs_sb_info *sbi, const char *devname); -int dev_open_ro(struct erofs_sb_info *sbi, const char *dev); -void dev_close(struct erofs_sb_info *sbi); -int dev_write(struct erofs_sb_info *sbi, const void *buf, - u64 offset, size_t len); -int dev_read(struct erofs_sb_info *sbi, int device_id, - void *buf, u64 offset, size_t len); -int dev_fillzero(struct erofs_sb_info *sbi, u64 offset, - size_t len, bool padding); -int dev_fsync(struct erofs_sb_info *sbi); -int dev_resize(struct erofs_sb_info *sbi, erofs_blk_t nblocks); - -ssize_t erofs_copy_file_range(int fd_in, erofs_off_t *off_in, - int fd_out, erofs_off_t *off_out, +struct erofs_vfile; + +struct erofs_vfops { + int (*pread)(struct erofs_vfile *vf, void *buf, u64 offset, size_t len); + int (*pwrite)(struct erofs_vfile *vf, const void *buf, u64 offset, size_t len); + int (*fsync)(struct erofs_vfile *vf); + int (*fallocate)(struct erofs_vfile *vf, u64 offset, size_t len, bool pad); + int (*ftruncate)(struct erofs_vfile *vf, u64 length); +}; + +struct erofs_vfile { + struct erofs_vfops *ops; + u64 offset; + int fd; +}; + +int erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, u64 pos, size_t len); +int erofs_io_fsync(struct erofs_vfile *vf); +int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, size_t len, bool pad); +int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length); +int erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 offset, size_t len); + +ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out, size_t length); -static inline int blk_write(struct erofs_sb_info *sbi, const void *buf, - erofs_blk_t blkaddr, u32 nblocks) -{ - return dev_write(sbi, buf, erofs_pos(sbi, blkaddr), - erofs_pos(sbi, nblocks)); -} - -static inline int blk_read(struct erofs_sb_info *sbi, int device_id, void *buf, - erofs_blk_t start, u32 nblocks) -{ - return dev_read(sbi, device_id, buf, erofs_pos(sbi, start), - erofs_pos(sbi, nblocks)); -} - #ifdef __cplusplus } #endif diff --git a/lib/blobchunk.c b/lib/blobchunk.c index 8082aa4..dc1bf3c 100644 --- a/lib/blobchunk.c +++ b/lib/blobchunk.c @@ -9,7 +9,6 @@ #include "erofs/blobchunk.h" #include "erofs/block_list.h" #include "erofs/cache.h" -#include "erofs/io.h" #include "sha256.h" #include @@ -195,7 +194,8 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode, 0 : extent_end - extent_start, first_extent, true); - return dev_write(inode->sbi, inode->chunkindexes, off, inode->extent_isize); + return erofs_dev_write(inode->sbi, inode->chunkindexes, off, + inode->extent_isize); } int erofs_blob_mergechunks(struct erofs_inode *inode, unsigned int chunkbits, @@ -477,7 +477,7 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi) { struct erofs_buffer_head *bh; ssize_t length; - erofs_off_t pos_in, pos_out; + u64 pos_in, pos_out; ssize_t ret; if (blobfile) { @@ -506,7 +506,7 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi) }; memcpy(dis.tag, sbi->devs[i].tag, sizeof(dis.tag)); - ret = dev_write(sbi, &dis, pos_out, sizeof(dis)); + ret = erofs_dev_write(sbi, &dis, pos_out, sizeof(dis)); if (ret) return ret; pos_out += sizeof(dis); @@ -525,11 +525,11 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi) pos_out = erofs_btell(bh, false); remapped_base = erofs_blknr(sbi, pos_out); - pos_out += sbi->diskoffset; + pos_out += sbi->bdev.offset; if (blobfile) { pos_in = 0; ret = erofs_copy_file_range(fileno(blobfile), &pos_in, - sbi->devfd, &pos_out, datablob_size); + sbi->bdev.fd, &pos_out, datablob_size); ret = ret < datablob_size ? -EIO : 0; } else { ret = 0; diff --git a/lib/cache.c b/lib/cache.c index 0a88061..664e598 100644 --- a/lib/cache.c +++ b/lib/cache.c @@ -7,7 +7,6 @@ */ #include #include -#include "erofs/io.h" #include "erofs/print.h" static struct erofs_buffer_block blkh = { @@ -400,8 +399,8 @@ int erofs_bflush(struct erofs_buffer_block *bb) padding = blksiz - (p->buffers.off & (blksiz - 1)); if (padding != blksiz) - dev_fillzero(&sbi, erofs_pos(&sbi, blkaddr) - padding, - padding, true); + erofs_dev_fillzero(&sbi, erofs_pos(&sbi, blkaddr) - padding, + padding, true); if (p->type != DATA) erofs_metablkcnt += BLK_ROUND_UP(&sbi, p->buffers.off); diff --git a/lib/compress.c b/lib/compress.c index f783236..c9fd5b6 100644 --- a/lib/compress.c +++ b/lib/compress.c @@ -12,7 +12,6 @@ #include #include #include "erofs/print.h" -#include "erofs/io.h" #include "erofs/cache.h" #include "erofs/compress.h" #include "erofs/dedupe.h" @@ -407,7 +406,7 @@ static int write_uncompressed_extent(struct z_erofs_compress_sctx *ctx, } else { erofs_dbg("Writing %u uncompressed data to block %u", count, ctx->blkaddr); - ret = blk_write(sbi, dst, ctx->blkaddr, 1); + ret = erofs_blk_write(sbi, dst, ctx->blkaddr, 1); if (ret) return ret; } @@ -659,8 +658,8 @@ frag_packing: erofs_dbg("Writing %u compressed data to %u of %u blocks", e->length, ctx->blkaddr, e->compressedblks); - ret = blk_write(sbi, dst - padding, ctx->blkaddr, - e->compressedblks); + ret = erofs_blk_write(sbi, dst - padding, ctx->blkaddr, + e->compressedblks); if (ret) return ret; } @@ -1288,8 +1287,8 @@ int z_erofs_merge_segment(struct z_erofs_compress_ictx *ictx, /* skip write data but leave blkaddr for inline fallback */ if (ei->e.inlined || !ei->e.compressedblks) continue; - ret2 = blk_write(sbi, sctx->membuf + blkoff * erofs_blksiz(sbi), - ei->e.blkaddr, ei->e.compressedblks); + ret2 = erofs_blk_write(sbi, sctx->membuf + blkoff * erofs_blksiz(sbi), + ei->e.blkaddr, ei->e.compressedblks); blkoff += ei->e.compressedblks; if (ret2) { ret = ret2; @@ -1603,8 +1602,8 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi, return PTR_ERR(bh); } erofs_mapbh(bh->block); - ret = dev_write(sbi, &lz4alg, erofs_btell(bh, false), - sizeof(lz4alg)); + ret = erofs_dev_write(sbi, &lz4alg, erofs_btell(bh, false), + sizeof(lz4alg)); bh->op = &erofs_drop_directly_bhops; } #ifdef HAVE_LIBLZMA @@ -1627,8 +1626,8 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi, return PTR_ERR(bh); } erofs_mapbh(bh->block); - ret = dev_write(sbi, &lzmaalg, erofs_btell(bh, false), - sizeof(lzmaalg)); + ret = erofs_dev_write(sbi, &lzmaalg, erofs_btell(bh, false), + sizeof(lzmaalg)); bh->op = &erofs_drop_directly_bhops; } #endif @@ -1651,8 +1650,8 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi, return PTR_ERR(bh); } erofs_mapbh(bh->block); - ret = dev_write(sbi, &zalg, erofs_btell(bh, false), - sizeof(zalg)); + ret = erofs_dev_write(sbi, &zalg, erofs_btell(bh, false), + sizeof(zalg)); bh->op = &erofs_drop_directly_bhops; } #ifdef HAVE_LIBZSTD @@ -1674,8 +1673,8 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi, return PTR_ERR(bh); } erofs_mapbh(bh->block); - ret = dev_write(sbi, &zalg, erofs_btell(bh, false), - sizeof(zalg)); + ret = erofs_dev_write(sbi, &zalg, erofs_btell(bh, false), + sizeof(zalg)); bh->op = &erofs_drop_directly_bhops; } #endif diff --git a/lib/data.c b/lib/data.c index c139e0c..a8402ed 100644 --- a/lib/data.c +++ b/lib/data.c @@ -6,7 +6,6 @@ #include #include "erofs/print.h" #include "erofs/internal.h" -#include "erofs/io.h" #include "erofs/trace.h" #include "erofs/decompress.h" @@ -95,7 +94,7 @@ int erofs_map_blocks(struct erofs_inode *inode, pos = roundup(erofs_iloc(vi) + vi->inode_isize + vi->xattr_isize, unit) + unit * chunknr; - err = blk_read(sbi, 0, buf, erofs_blknr(sbi, pos), 1); + err = erofs_blk_read(sbi, 0, buf, erofs_blknr(sbi, pos), 1); if (err < 0) return -EIO; @@ -176,7 +175,7 @@ int erofs_read_one_data(struct erofs_inode *inode, struct erofs_map_blocks *map, if (ret) return ret; - ret = dev_read(sbi, mdev.m_deviceid, buffer, mdev.m_pa + offset, len); + ret = erofs_dev_read(sbi, mdev.m_deviceid, buffer, mdev.m_pa + offset, len); if (ret < 0) return -EIO; return 0; @@ -266,7 +265,7 @@ int z_erofs_read_one_data(struct erofs_inode *inode, return ret; } - ret = dev_read(sbi, mdev.m_deviceid, raw, mdev.m_pa, map->m_plen); + ret = erofs_dev_read(sbi, mdev.m_deviceid, raw, mdev.m_pa, map->m_plen); if (ret < 0) return ret; @@ -417,7 +416,7 @@ static void *erofs_read_metadata_bdi(struct erofs_sb_info *sbi, u8 data[EROFS_MAX_BLOCK_SIZE]; *offset = round_up(*offset, 4); - ret = blk_read(sbi, 0, data, erofs_blknr(sbi, *offset), 1); + ret = erofs_blk_read(sbi, 0, data, erofs_blknr(sbi, *offset), 1); if (ret) return ERR_PTR(ret); len = le16_to_cpu(*(__le16 *)(data + erofs_blkoff(sbi, *offset))); @@ -433,7 +432,7 @@ static void *erofs_read_metadata_bdi(struct erofs_sb_info *sbi, for (i = 0; i < len; i += cnt) { cnt = min_t(int, erofs_blksiz(sbi) - erofs_blkoff(sbi, *offset), len - i); - ret = blk_read(sbi, 0, data, erofs_blknr(sbi, *offset), 1); + ret = erofs_blk_read(sbi, 0, data, erofs_blknr(sbi, *offset), 1); if (ret) { free(buffer); return ERR_PTR(ret); diff --git a/lib/diskbuf.c b/lib/diskbuf.c index e5889df..e5f1f0c 100644 --- a/lib/diskbuf.c +++ b/lib/diskbuf.c @@ -104,10 +104,10 @@ int erofs_diskbuf_init(unsigned int nstrms) struct stat st; /* try to use the devfd for regfiles on stream 0 */ - if (strm == dbufstrm && sbi.devsz == INT64_MAX) { + if (strm == dbufstrm && !sbi.bdev.ops) { strm->devpos = 1ULL << 40; - if (!ftruncate(sbi.devfd, strm->devpos << 1)) { - strm->fd = dup(sbi.devfd); + if (!ftruncate(sbi.bdev.fd, strm->devpos << 1)) { + strm->fd = dup(sbi.bdev.fd); if (lseek(strm->fd, strm->devpos, SEEK_SET) != strm->devpos) return -EIO; diff --git a/lib/inode.c b/lib/inode.c index cd48e55..9d7d518 100644 --- a/lib/inode.c +++ b/lib/inode.c @@ -19,7 +19,6 @@ #include "erofs/diskbuf.h" #include "erofs/inode.h" #include "erofs/cache.h" -#include "erofs/io.h" #include "erofs/compress.h" #include "erofs/xattr.h" #include "erofs/exclude.h" @@ -315,7 +314,7 @@ static int write_dirblock(struct erofs_sb_info *sbi, char buf[EROFS_MAX_BLOCK_SIZE]; fill_dirblock(buf, erofs_blksiz(sbi), q, head, end); - return blk_write(sbi, buf, blkaddr, 1); + return erofs_blk_write(sbi, buf, blkaddr, 1); } erofs_nid_t erofs_lookupnid(struct erofs_inode *inode) @@ -413,7 +412,7 @@ int erofs_write_file_from_buffer(struct erofs_inode *inode, char *buf) return ret; if (nblocks) - blk_write(sbi, buf, inode->u.i_blkaddr, nblocks); + erofs_blk_write(sbi, buf, inode->u.i_blkaddr, nblocks); inode->idata_size = inode->i_size % erofs_blksiz(sbi); if (inode->idata_size) { inode->idata = malloc(inode->idata_size); @@ -456,7 +455,7 @@ static int write_uncompressed_file_from_fd(struct erofs_inode *inode, int fd) return -EAGAIN; } - ret = blk_write(sbi, buf, inode->u.i_blkaddr + i, 1); + ret = erofs_blk_write(sbi, buf, inode->u.i_blkaddr + i, 1); if (ret) return ret; } @@ -584,7 +583,7 @@ static int erofs_bh_flush_write_inode(struct erofs_buffer_head *bh) BUG_ON(1); } - ret = dev_write(sbi, &u, off, inode->inode_isize); + ret = erofs_dev_write(sbi, &u, off, inode->inode_isize); if (ret) return ret; off += inode->inode_isize; @@ -595,7 +594,7 @@ static int erofs_bh_flush_write_inode(struct erofs_buffer_head *bh) if (IS_ERR(xattrs)) return PTR_ERR(xattrs); - ret = dev_write(sbi, xattrs, off, inode->xattr_isize); + ret = erofs_dev_write(sbi, xattrs, off, inode->xattr_isize); free(xattrs); if (ret) return ret; @@ -611,8 +610,8 @@ static int erofs_bh_flush_write_inode(struct erofs_buffer_head *bh) } else { /* write compression metadata */ off = roundup(off, 8); - ret = dev_write(sbi, inode->compressmeta, off, - inode->extent_isize); + ret = erofs_dev_write(sbi, inode->compressmeta, off, + inode->extent_isize); if (ret) return ret; } @@ -733,7 +732,7 @@ static int erofs_bh_flush_write_inline(struct erofs_buffer_head *bh) const erofs_off_t off = erofs_btell(bh, false); int ret; - ret = dev_write(inode->sbi, inode->idata, off, inode->idata_size); + ret = erofs_dev_write(inode->sbi, inode->idata, off, inode->idata_size); if (ret) return ret; @@ -804,13 +803,13 @@ static int erofs_write_tail_end(struct erofs_inode *inode) /* pad 0'ed data for the other cases */ zero_pos = pos + inode->idata_size; } - ret = dev_write(sbi, inode->idata, pos, inode->idata_size); + ret = erofs_dev_write(sbi, inode->idata, pos, inode->idata_size); if (ret) return ret; DBG_BUGON(inode->idata_size > erofs_blksiz(sbi)); if (inode->idata_size < erofs_blksiz(sbi)) { - ret = dev_fillzero(sbi, zero_pos, + ret = erofs_dev_fillzero(sbi, zero_pos, erofs_blksiz(sbi) - inode->idata_size, false); if (ret) diff --git a/lib/io.c b/lib/io.c index bfae73a..2db384c 100644 --- a/lib/io.c +++ b/lib/io.c @@ -13,7 +13,7 @@ #include #include #include -#include "erofs/io.h" +#include "erofs/internal.h" #ifdef HAVE_LINUX_FS_H #include #endif @@ -26,7 +26,140 @@ #define EROFS_MODNAME "erofs_io" #include "erofs/print.h" -static int dev_get_blkdev_size(int fd, u64 *bytes) +int erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, + u64 pos, size_t len) +{ + ssize_t ret; + + if (unlikely(cfg.c_dry_run)) + return 0; + + if (vf->ops) + return vf->ops->pwrite(vf, buf, pos, len); + + pos += vf->offset; + do { +#ifdef HAVE_PWRITE64 + ret = pwrite64(vf->fd, buf, len, (off64_t)pos); +#else + ret = pwrite(vf->fd, buf, len, (off_t)pos); +#endif + if (ret <= 0) { + erofs_err("failed to write: %s", strerror(errno)); + return -errno; + } + len -= ret; + buf += ret; + pos += ret; + } while (len); + + return 0; +} + +int erofs_io_fsync(struct erofs_vfile *vf) +{ + int ret; + + if (unlikely(cfg.c_dry_run)) + return 0; + + if (vf->ops) + return vf->ops->fsync(vf); + + ret = fsync(vf->fd); + if (ret) { + erofs_err("failed to fsync(!): %s", strerror(errno)); + return -errno; + } + return 0; +} + +int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, + size_t len, bool zeroout) +{ + static const char zero[EROFS_MAX_BLOCK_SIZE] = {0}; + int ret; + + if (unlikely(cfg.c_dry_run)) + return 0; + + if (vf->ops) + return vf->ops->fallocate(vf, offset, len, zeroout); + +#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) + if (!zeroout && fallocate(vf->fd, FALLOC_FL_PUNCH_HOLE | + FALLOC_FL_KEEP_SIZE, offset + vf->offset, len) >= 0) + return 0; +#endif + while (len > EROFS_MAX_BLOCK_SIZE) { + ret = erofs_io_pwrite(vf, zero, offset, EROFS_MAX_BLOCK_SIZE); + if (ret) + return ret; + len -= EROFS_MAX_BLOCK_SIZE; + offset += EROFS_MAX_BLOCK_SIZE; + } + return erofs_io_pwrite(vf, zero, offset, len); +} + +int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length) +{ + int ret; + struct stat st; + + if (unlikely(cfg.c_dry_run)) + return 0; + + if (vf->ops) + return vf->ops->ftruncate(vf, length); + + ret = fstat(vf->fd, &st); + if (ret) { + erofs_err("failed to fstat: %s", strerror(errno)); + return -errno; + } + length += vf->offset; + if (S_ISBLK(st.st_mode) || st.st_size == length) + return 0; + return ftruncate(vf->fd, length); +} + +int erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len) +{ + ssize_t ret; + + if (unlikely(cfg.c_dry_run)) + return 0; + + if (vf->ops) + return vf->ops->pread(vf, buf, pos, len); + + pos += vf->offset; + do { +#ifdef HAVE_PREAD64 + ret = pread64(vf->fd, buf, len, (off64_t)pos); +#else + ret = pread(vf->fd, buf, len, (off_t)pos); +#endif + if (ret <= 0) { + if (!ret) { + erofs_info("reach EOF of device"); + memset(buf, 0, len); + return 0; + } + if (errno != EINTR) { + erofs_err("failed to read: %s", strerror(errno)); + return -errno; + } + ret = 0; + } + pos += ret; + len -= ret; + buf += ret; + } while (len); + return 0; +} + +static int erofs_get_bdev_size(int fd, u64 *bytes) { errno = ENOTSUP; #ifdef BLKGETSIZE64 @@ -46,17 +179,25 @@ static int dev_get_blkdev_size(int fd, u64 *bytes) return -errno; } -void dev_close(struct erofs_sb_info *sbi) +#if defined(__linux__) && !defined(BLKDISCARD) +#define BLKDISCARD _IO(0x12, 119) +#endif + +static int erofs_bdev_discard(int fd, u64 block, u64 count) { - close(sbi->devfd); - free(sbi->devname); - sbi->devname = NULL; - sbi->devfd = -1; - sbi->devsz = 0; +#ifdef BLKDISCARD + u64 range[2] = { block, count }; + + return ioctl(fd, BLKDISCARD, &range); +#else + return -EOPNOTSUPP; +#endif } -int dev_open(struct erofs_sb_info *sbi, const char *dev) +int erofs_dev_open(struct erofs_sb_info *sbi, const char *dev, int flags) { + bool ro = (flags & O_ACCMODE) == O_RDONLY; + bool truncate = flags & O_TRUNC; struct stat st; int fd, ret; @@ -65,28 +206,36 @@ int dev_open(struct erofs_sb_info *sbi, const char *dev) repeat: #endif - fd = open(dev, O_RDWR | O_CREAT | O_BINARY, 0644); + fd = open(dev, (ro ? O_RDONLY : O_RDWR | O_CREAT) | O_BINARY, 0644); if (fd < 0) { - erofs_err("failed to open(%s).", dev); + erofs_err("failed to open %s: %s", dev, strerror(errno)); return -errno; } + if (ro || !truncate) + goto out; + ret = fstat(fd, &st); if (ret) { - erofs_err("failed to fstat(%s).", dev); + erofs_err("failed to fstat(%s): %s", dev, strerror(errno)); close(fd); return -errno; } switch (st.st_mode & S_IFMT) { case S_IFBLK: - ret = dev_get_blkdev_size(fd, &sbi->devsz); + ret = erofs_get_bdev_size(fd, &sbi->devsz); if (ret) { - erofs_err("failed to get block device size(%s).", dev); + erofs_err("failed to get block device size(%s): %s", + dev, strerror(errno)); close(fd); return ret; } sbi->devsz = round_down(sbi->devsz, erofs_blksiz(sbi)); + ret = erofs_bdev_discard(fd, 0, sbi->devsz); + if (ret) + erofs_err("failed to erase block device(%s): %s", + dev, erofs_strerror(ret)); break; case S_IFREG: if (st.st_size) { @@ -117,8 +266,6 @@ repeat: return -errno; } } - /* INT64_MAX is the limit of kernel vfs */ - sbi->devsz = INT64_MAX; sbi->devblksz = st.st_blksize; break; default: @@ -127,18 +274,26 @@ repeat: return -EINVAL; } +out: sbi->devname = strdup(dev); if (!sbi->devname) { close(fd); return -ENOMEM; } - sbi->devfd = fd; - + sbi->bdev.fd = fd; erofs_info("successfully to open %s", dev); return 0; } -void blob_closeall(struct erofs_sb_info *sbi) +void erofs_dev_close(struct erofs_sb_info *sbi) +{ + close(sbi->bdev.fd); + free(sbi->devname); + sbi->devname = NULL; + sbi->bdev.fd = -1; +} + +void erofs_blob_closeall(struct erofs_sb_info *sbi) { unsigned int i; @@ -147,7 +302,7 @@ void blob_closeall(struct erofs_sb_info *sbi) sbi->nblobs = 0; } -int blob_open_ro(struct erofs_sb_info *sbi, const char *dev) +int erofs_blob_open_ro(struct erofs_sb_info *sbi, const char *dev) { int fd = open(dev, O_RDONLY | O_BINARY); @@ -162,182 +317,18 @@ int blob_open_ro(struct erofs_sb_info *sbi, const char *dev) return 0; } -/* XXX: temporary soluation. Disk I/O implementation needs to be refactored. */ -int dev_open_ro(struct erofs_sb_info *sbi, const char *dev) +int erofs_dev_read(struct erofs_sb_info *sbi, int device_id, + void *buf, u64 offset, size_t len) { - int fd = open(dev, O_RDONLY | O_BINARY); - - if (fd < 0) { - erofs_err("failed to open(%s).", dev); - return -errno; - } - - sbi->devname = strdup(dev); - if (!sbi->devname) { - close(fd); - return -ENOMEM; - } - sbi->devfd = fd; - sbi->devsz = INT64_MAX; - return 0; -} - -int dev_write(struct erofs_sb_info *sbi, const void *buf, u64 offset, size_t len) -{ - int ret; - - if (cfg.c_dry_run) - return 0; - - if (!buf) { - erofs_err("buf is NULL"); - return -EINVAL; - } - - offset += sbi->diskoffset; - if (offset >= sbi->devsz || len > sbi->devsz || - offset > sbi->devsz - len) { - erofs_err("Write posion[%" PRIu64 ", %zd] is too large beyond the end of device(%" PRIu64 ").", - offset, len, sbi->devsz); - return -EINVAL; - } - -#ifdef HAVE_PWRITE64 - ret = pwrite64(sbi->devfd, buf, len, (off64_t)offset); -#else - ret = pwrite(sbi->devfd, buf, len, (off_t)offset); -#endif - if (ret != (int)len) { - if (ret < 0) { - erofs_err("Failed to write data into device - %s:[%" PRIu64 ", %zd].", - sbi->devname, offset, len); - return -errno; - } - - erofs_err("Writing data into device - %s:[%" PRIu64 ", %zd] - was truncated.", - sbi->devname, offset, len); - return -ERANGE; - } - return 0; -} - -int dev_fillzero(struct erofs_sb_info *sbi, u64 offset, size_t len, bool padding) -{ - static const char zero[EROFS_MAX_BLOCK_SIZE] = {0}; - int ret; - - if (cfg.c_dry_run) - return 0; - -#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) - if (!padding && fallocate(sbi->devfd, FALLOC_FL_PUNCH_HOLE | - FALLOC_FL_KEEP_SIZE, offset + sbi->diskoffset, len) >= 0) - return 0; -#endif - while (len > erofs_blksiz(sbi)) { - ret = dev_write(sbi, zero, offset, erofs_blksiz(sbi)); - if (ret) - return ret; - len -= erofs_blksiz(sbi); - offset += erofs_blksiz(sbi); - } - return dev_write(sbi, zero, offset, len); -} - -int dev_fsync(struct erofs_sb_info *sbi) -{ - int ret; - - ret = fsync(sbi->devfd); - if (ret) { - erofs_err("Could not fsync device!!!"); - return -EIO; - } - return 0; -} - -int dev_resize(struct erofs_sb_info *sbi, erofs_blk_t blocks) -{ - int ret; - struct stat st; - u64 length; - - if (cfg.c_dry_run || sbi->devsz != INT64_MAX) - return 0; - - ret = fstat(sbi->devfd, &st); - if (ret) { - erofs_err("failed to fstat."); - return -errno; - } - - length = (u64)blocks * erofs_blksiz(sbi); - length += sbi->diskoffset; - if (st.st_size == length) - return 0; - if (st.st_size > length) - return ftruncate(sbi->devfd, length); - - length = length - st.st_size; -#if defined(HAVE_FALLOCATE) - if (fallocate(sbi->devfd, 0, st.st_size, length) >= 0) - return 0; -#endif - return dev_fillzero(sbi, st.st_size - sbi->diskoffset, - length, true); -} - -int dev_read(struct erofs_sb_info *sbi, int device_id, - void *buf, u64 offset, size_t len) -{ - int read_count, fd; - - if (cfg.c_dry_run) - return 0; - - if (!buf) { - erofs_err("buf is NULL"); - return -EINVAL; - } - - if (!device_id) { - fd = sbi->devfd; - offset += sbi->diskoffset; - } else { - if (device_id > sbi->nblobs) { - erofs_err("invalid device id %d", device_id); - return -ENODEV; - } - fd = sbi->blobfd[device_id - 1]; - } - - while (len > 0) { -#ifdef HAVE_PREAD64 - read_count = pread64(fd, buf, len, (off64_t)offset); -#else - read_count = pread(fd, buf, len, (off_t)offset); -#endif - if (read_count < 1) { - if (!read_count) { - erofs_info("Reach EOF of device - %s:[%" PRIu64 ", %zd].", - sbi->devname, offset, len); - memset(buf, 0, len); - return 0; - } else if (errno != EINTR) { - erofs_err("Failed to read data from device - %s:[%" PRIu64 ", %zd].", - sbi->devname, offset, len); - return -errno; - } - } - offset += read_count; - len -= read_count; - buf += read_count; - } - return 0; + if (device_id) + return erofs_io_pread(&((struct erofs_vfile) { + .fd = sbi->blobfd[device_id - 1], + }), buf, offset, len); + return erofs_io_pread(&sbi->bdev, buf, offset, len); } -static ssize_t __erofs_copy_file_range(int fd_in, erofs_off_t *off_in, - int fd_out, erofs_off_t *off_out, +static ssize_t __erofs_copy_file_range(int fd_in, u64 *off_in, + int fd_out, u64 *off_out, size_t length) { size_t copied = 0; @@ -408,8 +399,7 @@ static ssize_t __erofs_copy_file_range(int fd_in, erofs_off_t *off_in, return copied; } -ssize_t erofs_copy_file_range(int fd_in, erofs_off_t *off_in, - int fd_out, erofs_off_t *off_out, +ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out, size_t length) { #ifdef HAVE_COPY_FILE_RANGE diff --git a/lib/namei.c b/lib/namei.c index 294d7a3..6f35ee6 100644 --- a/lib/namei.c +++ b/lib/namei.c @@ -12,7 +12,7 @@ #include #endif #include "erofs/print.h" -#include "erofs/io.h" +#include "erofs/internal.h" static dev_t erofs_new_decode_dev(u32 dev) { @@ -34,7 +34,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi) DBG_BUGON(!sbi); inode_loc = erofs_iloc(vi); - ret = dev_read(sbi, 0, buf, inode_loc, sizeof(*dic)); + ret = erofs_dev_read(sbi, 0, buf, inode_loc, sizeof(*dic)); if (ret < 0) return -EIO; @@ -51,7 +51,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi) case EROFS_INODE_LAYOUT_EXTENDED: vi->inode_isize = sizeof(struct erofs_inode_extended); - ret = dev_read(sbi, 0, buf + sizeof(*dic), + ret = erofs_dev_read(sbi, 0, buf + sizeof(*dic), inode_loc + sizeof(*dic), sizeof(*die) - sizeof(*dic)); if (ret < 0) diff --git a/lib/rebuild.c b/lib/rebuild.c index 5993730..618067a 100644 --- a/lib/rebuild.c +++ b/lib/rebuild.c @@ -11,7 +11,6 @@ #include "erofs/print.h" #include "erofs/inode.h" #include "erofs/rebuild.h" -#include "erofs/io.h" #include "erofs/dir.h" #include "erofs/xattr.h" #include "erofs/blobchunk.h" diff --git a/lib/super.c b/lib/super.c index 61a1618..6de5e49 100644 --- a/lib/super.c +++ b/lib/super.c @@ -4,7 +4,6 @@ */ #include #include -#include "erofs/io.h" #include "erofs/print.h" #include "erofs/xattr.h" @@ -56,7 +55,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi, struct erofs_deviceslot dis; int ret; - ret = dev_read(sbi, 0, &dis, pos, sizeof(dis)); + ret = erofs_dev_read(sbi, 0, &dis, pos, sizeof(dis)); if (ret < 0) { free(sbi->devs); sbi->devs = NULL; @@ -79,7 +78,7 @@ int erofs_read_superblock(struct erofs_sb_info *sbi) int ret; sbi->blkszbits = ilog2(EROFS_MAX_BLOCK_SIZE); - ret = blk_read(sbi, 0, data, 0, erofs_blknr(sbi, sizeof(data))); + ret = erofs_blk_read(sbi, 0, data, 0, erofs_blknr(sbi, sizeof(data))); if (ret < 0) { erofs_err("cannot read erofs superblock: %d", ret); return -EIO; diff --git a/lib/tar.c b/lib/tar.c index 8d606f9..3514381 100644 --- a/lib/tar.c +++ b/lib/tar.c @@ -12,7 +12,6 @@ #include "erofs/inode.h" #include "erofs/list.h" #include "erofs/tar.h" -#include "erofs/io.h" #include "erofs/xattr.h" #include "erofs/blobchunk.h" #include "erofs/rebuild.h" diff --git a/lib/xattr.c b/lib/xattr.c index 0f6fbe2..7b263cd 100644 --- a/lib/xattr.c +++ b/lib/xattr.c @@ -16,7 +16,6 @@ #include "erofs/hashtable.h" #include "erofs/xattr.h" #include "erofs/cache.h" -#include "erofs/io.h" #include "erofs/fragments.h" #include "xxhash.h" #include "liberofs_private.h" @@ -925,7 +924,7 @@ int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *p shared_xattrs_list = sorted_n[0]; free(sorted_n); bh->op = &erofs_drop_directly_bhops; - ret = dev_write(sbi, buf, erofs_btell(bh, false), shared_xattrs_size); + ret = erofs_dev_write(sbi, buf, erofs_btell(bh, false), shared_xattrs_size); free(buf); erofs_bdrop(bh, false); out: @@ -1048,7 +1047,7 @@ static int init_inode_xattrs(struct erofs_inode *vi) it.blkaddr = erofs_blknr(sbi, erofs_iloc(vi) + vi->inode_isize); it.ofs = erofs_blkoff(sbi, erofs_iloc(vi) + vi->inode_isize); - ret = blk_read(sbi, 0, it.page, it.blkaddr, 1); + ret = erofs_blk_read(sbi, 0, it.page, it.blkaddr, 1); if (ret < 0) return -EIO; @@ -1068,7 +1067,7 @@ static int init_inode_xattrs(struct erofs_inode *vi) /* cannot be unaligned */ DBG_BUGON(it.ofs != erofs_blksiz(sbi)); - ret = blk_read(sbi, 0, it.page, ++it.blkaddr, 1); + ret = erofs_blk_read(sbi, 0, it.page, ++it.blkaddr, 1); if (ret < 0) { free(vi->xattr_shared_xattrs); vi->xattr_shared_xattrs = NULL; @@ -1114,7 +1113,7 @@ static inline int xattr_iter_fixup(struct xattr_iter *it) it->blkaddr += erofs_blknr(sbi, it->ofs); - ret = blk_read(sbi, 0, it->page, it->blkaddr, 1); + ret = erofs_blk_read(sbi, 0, it->page, it->blkaddr, 1); if (ret < 0) return -EIO; @@ -1141,7 +1140,7 @@ static int inline_xattr_iter_pre(struct xattr_iter *it, it->blkaddr = erofs_blknr(sbi, erofs_iloc(vi) + inline_xattr_ofs); it->ofs = erofs_blkoff(sbi, erofs_iloc(vi) + inline_xattr_ofs); - ret = blk_read(sbi, 0, it->page, it->blkaddr, 1); + ret = erofs_blk_read(sbi, 0, it->page, it->blkaddr, 1); if (ret < 0) return -EIO; @@ -1368,7 +1367,7 @@ static int shared_getxattr(struct erofs_inode *vi, struct getxattr_iter *it) it->it.ofs = xattrblock_offset(vi, vi->xattr_shared_xattrs[i]); if (!i || blkaddr != it->it.blkaddr) { - ret = blk_read(vi->sbi, 0, it->it.page, blkaddr, 1); + ret = erofs_blk_read(vi->sbi, 0, it->it.page, blkaddr, 1); if (ret < 0) return -EIO; @@ -1524,7 +1523,7 @@ static int shared_listxattr(struct erofs_inode *vi, struct listxattr_iter *it) it->it.ofs = xattrblock_offset(vi, vi->xattr_shared_xattrs[i]); if (!i || blkaddr != it->it.blkaddr) { - ret = blk_read(vi->sbi, 0, it->it.page, blkaddr, 1); + ret = erofs_blk_read(vi->sbi, 0, it->it.page, blkaddr, 1); if (ret < 0) return -EIO; diff --git a/lib/zmap.c b/lib/zmap.c index 2ec8505..a5c5b00 100644 --- a/lib/zmap.c +++ b/lib/zmap.c @@ -7,7 +7,7 @@ * Created by Gao Xiang * Modified by Huang Jianan */ -#include "erofs/io.h" +#include "erofs/internal.h" #include "erofs/print.h" static int z_erofs_do_map_blocks(struct erofs_inode *vi, @@ -43,7 +43,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi) return 0; pos = round_up(erofs_iloc(vi) + vi->inode_isize + vi->xattr_isize, 8); - ret = dev_read(sbi, 0, buf, pos, sizeof(buf)); + ret = erofs_dev_read(sbi, 0, buf, pos, sizeof(buf)); if (ret < 0) return -EIO; @@ -133,7 +133,7 @@ static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m, if (map->index == eblk) return 0; - ret = blk_read(m->inode->sbi, 0, mpage, eblk, 1); + ret = erofs_blk_read(m->inode->sbi, 0, mpage, eblk, 1); if (ret < 0) return -EIO; diff --git a/mkfs/main.c b/mkfs/main.c index 347efe6..007bea8 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -19,7 +19,6 @@ #include "erofs/diskbuf.h" #include "erofs/inode.h" #include "erofs/tar.h" -#include "erofs/io.h" #include "erofs/compress.h" #include "erofs/dedupe.h" #include "erofs/xattr.h" @@ -501,7 +500,7 @@ static void erofs_rebuild_cleanup(void) list_for_each_entry_safe(src, n, &rebuild_src_list, list) { list_del(&src->list); erofs_put_super(src); - dev_close(src); + erofs_dev_close(src); free(src); } rebuild_src_count = 0; @@ -739,7 +738,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) cfg.c_ovlfs_strip = false; break; case 517: - sbi.diskoffset = strtoull(optarg, &endptr, 0); + sbi.bdev.offset = strtoull(optarg, &endptr, 0); if (*endptr != '\0') { erofs_err("invalid disk offset %s", optarg); return -EINVAL; @@ -882,7 +881,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) return -ENOMEM; } - err = dev_open_ro(src, srcpath); + err = erofs_dev_open(src, srcpath, O_RDONLY); if (err) { free(src); erofs_rebuild_cleanup(); @@ -982,7 +981,7 @@ int erofs_mkfs_update_super_block(struct erofs_buffer_head *bh, } memcpy(buf + EROFS_SUPER_OFFSET, &sb, sizeof(sb)); - ret = dev_write(&sbi, buf, erofs_btell(bh, false), EROFS_SUPER_END); + ret = erofs_dev_write(&sbi, buf, erofs_btell(bh, false), EROFS_SUPER_END); free(buf); erofs_bdrop(bh, false); return ret; @@ -996,7 +995,7 @@ static int erofs_mkfs_superblock_csum_set(void) unsigned int len; struct erofs_super_block *sb; - ret = blk_read(&sbi, 0, buf, 0, erofs_blknr(&sbi, EROFS_SUPER_END) + 1); + ret = erofs_blk_read(&sbi, 0, buf, 0, erofs_blknr(&sbi, EROFS_SUPER_END) + 1); if (ret) { erofs_err("failed to read superblock to set checksum: %s", erofs_strerror(ret)); @@ -1026,7 +1025,7 @@ static int erofs_mkfs_superblock_csum_set(void) /* set up checksum field to erofs_super_block */ sb->checksum = cpu_to_le32(crc); - ret = blk_write(&sbi, buf, 0, 1); + ret = erofs_blk_write(&sbi, buf, 0, 1); if (ret) { erofs_err("failed to write checksummed superblock: %s", erofs_strerror(ret)); @@ -1221,7 +1220,7 @@ int main(int argc, char **argv) sbi.build_time_nsec = t.tv_usec; } - err = dev_open(&sbi, cfg.c_img_path); + err = erofs_dev_open(&sbi, cfg.c_img_path, O_RDWR | O_TRUNC); if (err) { fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]); return 1; @@ -1452,7 +1451,7 @@ int main(int argc, char **argv) if (err) goto exit; - err = dev_resize(&sbi, nblocks); + err = erofs_dev_resize(&sbi, nblocks); if (!err && erofs_sb_has_sb_chksum(&sbi)) err = erofs_mkfs_superblock_csum_set(); @@ -1460,7 +1459,7 @@ exit: z_erofs_compress_exit(); z_erofs_dedupe_exit(); erofs_blocklist_close(); - dev_close(&sbi); + erofs_dev_close(&sbi); erofs_cleanup_compress_hints(); erofs_cleanup_exclude_rules(); if (cfg.c_chunkbits)