From: Sheng Yong <shengyo...@xiaomi.com>

When attempting to use an archive file, such as APEX on android,
as a file-backed mount source, it fails because EROFS image within
the archive file does not start at offset 0. As a result, a loop
device is still needed to attach the image file at an appropriate
offset first.

To address this issue, this patch parses the `source' parameter in
EROFS to accept a start offset for the file-backed mount. The format
is `/path/to/archive_file:offs', where `offs' represents the start
offset. EROFS will add this offset before performing read requests.

Signed-off-by: Sheng Yong <shengyo...@xiaomi.com>
Signed-off-by: Wang Shuai <wangshua...@xiaomi.com>
---
 fs/erofs/data.c     |  7 ++++++-
 fs/erofs/fileio.c   |  8 ++++++--
 fs/erofs/internal.h |  2 ++
 fs/erofs/super.c    | 37 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/fs/erofs/data.c b/fs/erofs/data.c
index 2409d2ab0c28..957743201ef5 100644
--- a/fs/erofs/data.c
+++ b/fs/erofs/data.c
@@ -27,9 +27,12 @@ void erofs_put_metabuf(struct erofs_buf *buf)
 
 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 {
-       pgoff_t index = offset >> PAGE_SHIFT;
+       pgoff_t index;
        struct folio *folio = NULL;
 
+       offset += buf->fofs;
+       index = offset >> PAGE_SHIFT;
+
        if (buf->page) {
                folio = page_folio(buf->page);
                if (folio_file_page(folio, index) != buf->page)
@@ -54,9 +57,11 @@ void erofs_init_metabuf(struct erofs_buf *buf, struct 
super_block *sb)
        struct erofs_sb_info *sbi = EROFS_SB(sb);
 
        buf->file = NULL;
+       buf->fofs = 0;
        if (erofs_is_fileio_mode(sbi)) {
                buf->file = sbi->dif0.file;     /* some fs like FUSE needs it */
                buf->mapping = buf->file->f_mapping;
+               buf->fofs = sbi->dif0.fofs;
        } else if (erofs_is_fscache_mode(sb))
                buf->mapping = sbi->dif0.fscache->inode->i_mapping;
        else
diff --git a/fs/erofs/fileio.c b/fs/erofs/fileio.c
index bec4b56b3826..62288ef19920 100644
--- a/fs/erofs/fileio.c
+++ b/fs/erofs/fileio.c
@@ -45,15 +45,19 @@ static void erofs_fileio_ki_complete(struct kiocb *iocb, 
long ret)
 
 static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq)
 {
+       struct erofs_sb_info *sbi;
        struct iov_iter iter;
        int ret;
 
        if (!rq)
                return;
-       rq->iocb.ki_pos = rq->bio.bi_iter.bi_sector << SECTOR_SHIFT;
+
+       sbi = EROFS_SB(rq->sb);
+       rq->iocb.ki_pos = sbi->dif0.fofs +
+                               (rq->bio.bi_iter.bi_sector << SECTOR_SHIFT);
        rq->iocb.ki_ioprio = get_current_ioprio();
        rq->iocb.ki_complete = erofs_fileio_ki_complete;
-       if (test_opt(&EROFS_SB(rq->sb)->opt, DIRECT_IO) &&
+       if (test_opt(&sbi->opt, DIRECT_IO) &&
            rq->iocb.ki_filp->f_mode & FMODE_CAN_ODIRECT)
                rq->iocb.ki_flags = IOCB_DIRECT;
        iov_iter_bvec(&iter, ITER_DEST, rq->bvecs, rq->bio.bi_vcnt,
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 4ac188d5d894..b0a752b955f6 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -43,6 +43,7 @@ struct erofs_device_info {
        char *path;
        struct erofs_fscache *fscache;
        struct file *file;
+       loff_t fofs;
        struct dax_device *dax_dev;
        u64 dax_part_off;
 
@@ -199,6 +200,7 @@ enum {
 struct erofs_buf {
        struct address_space *mapping;
        struct file *file;
+       loff_t fofs;
        struct page *page;
        void *base;
 };
diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index cadec6b1b554..87ddeb327a8a 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -356,7 +356,7 @@ static void erofs_default_options(struct erofs_sb_info *sbi)
 
 enum {
        Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum,
-       Opt_device, Opt_fsid, Opt_domain_id, Opt_directio,
+       Opt_device, Opt_fsid, Opt_domain_id, Opt_directio, Opt_source,
        Opt_err
 };
 
@@ -384,6 +384,7 @@ static const struct fs_parameter_spec erofs_fs_parameters[] 
= {
        fsparam_string("fsid",          Opt_fsid),
        fsparam_string("domain_id",     Opt_domain_id),
        fsparam_flag_no("directio",     Opt_directio),
+       fsparam_string("source",        Opt_source),
        {}
 };
 
@@ -411,6 +412,31 @@ static bool erofs_fc_set_dax_mode(struct fs_context *fc, 
unsigned int mode)
 #endif
 }
 
+static loff_t erofs_fc_parse_source(struct fs_context *fc,
+                                   struct fs_parameter *param)
+{
+       const char *devname = param->string;
+       const char *fofs_start __maybe_unused;
+       loff_t fofs = 0;
+
+       if (!devname || !*devname)
+               return invalfc(fc, "Empty source");
+
+#ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
+       fofs_start = strchr(devname, ':');
+       if (!fofs_start)
+               goto out;
+       if (kstrtoll(fofs_start + 1, 0, &fofs) < 0)
+               return invalfc(fc, "Invalid filebacked offset %s", fofs_start);
+       fc->source = kstrndup(devname, fofs_start - devname, GFP_KERNEL);
+       return fofs;
+out:
+#endif
+       fc->source = devname;
+       param->string = NULL;
+       return fofs;
+}
+
 static int erofs_fc_parse_param(struct fs_context *fc,
                                struct fs_parameter *param)
 {
@@ -507,6 +533,11 @@ static int erofs_fc_parse_param(struct fs_context *fc,
                errorfc(fc, "%s option not supported", 
erofs_fs_parameters[opt].name);
 #endif
                break;
+       case Opt_source:
+               sbi->dif0.fofs = erofs_fc_parse_source(fc, param);
+               if (sbi->dif0.fofs < 0)
+                       return -EINVAL;
+               break;
        }
        return 0;
 }
@@ -697,6 +728,10 @@ static int erofs_fc_get_tree(struct fs_context *fc)
                file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0);
                if (IS_ERR(file))
                        return PTR_ERR(file);
+               if (sbi->dif0.fofs + PAGE_SIZE >= 
i_size_read(file_inode(file))) {
+                       fput(file);
+                       return invalf(fc, "Start offset too large");
+               }
                sbi->dif0.file = file;
 
                if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) &&
-- 
2.43.0


Reply via email to