]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
erofs: add 'fsoffset' mount option to specify filesystem offset
authorSheng Yong <shengyong1@xiaomi.com>
Sat, 17 May 2025 09:05:43 +0000 (17:05 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 22 May 2025 03:57:57 +0000 (11:57 +0800)
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
or a dm device is still needed to attach the image file at an
appropriate offset first. Similarly, if an EROFS image within a
block device does not start at offset 0, it cannot be mounted
directly either.

To address this issue, this patch adds a new mount option `fsoffset=x'
to accept a start offset for the primary device. The offset should be
aligned to the block size. EROFS will add this offset before performing
read requests.

Signed-off-by: Sheng Yong <shengyong1@xiaomi.com>
Signed-off-by: Wang Shuai <wangshuai12@xiaomi.com>
Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20250517090544.2687651-1-shengyong1@xiaomi.com
[ Gao Xiang: minor update on documentation and the error message. ]
Reviewed-by: Hongbo Li <lihongbo22@huawei.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Documentation/filesystems/erofs.rst
fs/erofs/data.c
fs/erofs/fileio.c
fs/erofs/internal.h
fs/erofs/super.c
fs/erofs/zdata.c

index c293f8e37468cddacafb5d3edc615134408d6b3b..7ddb235aee9d4e2678f3062e2094d43f2e5bbb2f 100644 (file)
@@ -128,6 +128,7 @@ device=%s              Specify a path to an extra device to be used together.
 fsid=%s                Specify a filesystem image ID for Fscache back-end.
 domain_id=%s           Specify a domain ID in fscache mode so that different images
                        with the same blobs under a given domain ID can share storage.
+fsoffset=%llu          Specify block-aligned filesystem offset for the primary device.
 ===================    =========================================================
 
 Sysfs Entries
index 2409d2ab0c280e874569d65e7a55bd6bcefa9898..6a329c329f434046c80d9718f60e71a11ddd4f37 100644 (file)
@@ -27,7 +27,7 @@ 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 = (buf->off + offset) >> PAGE_SHIFT;
        struct folio *folio = NULL;
 
        if (buf->page) {
@@ -54,6 +54,7 @@ void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb)
        struct erofs_sb_info *sbi = EROFS_SB(sb);
 
        buf->file = NULL;
+       buf->off = sbi->dif0.fsoff;
        if (erofs_is_fileio_mode(sbi)) {
                buf->file = sbi->dif0.file;     /* some fs like FUSE needs it */
                buf->mapping = buf->file->f_mapping;
@@ -299,7 +300,7 @@ static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
                iomap->private = buf.base;
        } else {
                iomap->type = IOMAP_MAPPED;
-               iomap->addr = mdev.m_pa;
+               iomap->addr = mdev.m_dif->fsoff + mdev.m_pa;
                if (flags & IOMAP_DAX)
                        iomap->addr += mdev.m_dif->dax_part_off;
        }
index 94fff404db815b285c645f597cc0cc9f60e1a3bd..7d81f504bff08f3d5c5d44d131460df5c3e7847d 100644 (file)
@@ -147,7 +147,8 @@ io_retry:
                                if (err)
                                        break;
                                io->rq = erofs_fileio_rq_alloc(&io->dev);
-                               io->rq->bio.bi_iter.bi_sector = io->dev.m_pa >> 9;
+                               io->rq->bio.bi_iter.bi_sector =
+                                       (io->dev.m_dif->fsoff + io->dev.m_pa) >> 9;
                                attached = 0;
                        }
                        if (!bio_add_folio(&io->rq->bio, folio, len, cur))
index 4ac188d5d8944fbb3086b627b070c5c572343644..a32c03a80c7001c73833ff6cd33c297320ffa61d 100644 (file)
@@ -44,7 +44,7 @@ struct erofs_device_info {
        struct erofs_fscache *fscache;
        struct file *file;
        struct dax_device *dax_dev;
-       u64 dax_part_off;
+       u64 fsoff, dax_part_off;
 
        erofs_blk_t blocks;
        erofs_blk_t uniaddr;
@@ -199,6 +199,7 @@ enum {
 struct erofs_buf {
        struct address_space *mapping;
        struct file *file;
+       u64 off;
        struct page *page;
        void *base;
 };
index 6e57b9cc6ed2e09a1886d3803b64871c6619513f..e1e9f06e8342f31584b1e9eb8fbd9702ead15ea9 100644 (file)
@@ -359,7 +359,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_fsoffset,
 };
 
 static const struct constant_table erofs_param_cache_strategy[] = {
@@ -386,6 +386,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_u64("fsoffset",         Opt_fsoffset),
        {}
 };
 
@@ -509,6 +510,9 @@ 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_fsoffset:
+               sbi->dif0.fsoff = result.uint_64;
+               break;
        }
        return 0;
 }
@@ -649,6 +653,14 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
                }
        }
 
+       if (sbi->dif0.fsoff) {
+               if (sbi->dif0.fsoff & (sb->s_blocksize - 1))
+                       return invalfc(fc, "fsoffset %llu is not aligned to block size %lu",
+                                      sbi->dif0.fsoff, sb->s_blocksize);
+               if (erofs_is_fscache_mode(sb))
+                       return invalfc(fc, "cannot use fsoffset in fscache mode");
+       }
+
        if (test_opt(&sbi->opt, DAX_ALWAYS)) {
                if (!sbi->dif0.dax_dev) {
                        errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
@@ -978,6 +990,8 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)
        if (sbi->domain_id)
                seq_printf(seq, ",domain_id=%s", sbi->domain_id);
 #endif
+       if (sbi->dif0.fsoff)
+               seq_printf(seq, ",fsoffset=%llu", sbi->dif0.fsoff);
        return 0;
 }
 
index b0f93b9be8ccf9bc5cde542f849087853f10ce84..ab61c84d47cd03109f981d323de531ec0e383f3f 100644 (file)
@@ -1737,7 +1737,8 @@ drain_io:
                                        bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
                                                        REQ_OP_READ, GFP_NOIO);
                                bio->bi_end_io = z_erofs_endio;
-                               bio->bi_iter.bi_sector = cur >> 9;
+                               bio->bi_iter.bi_sector =
+                                               (mdev.m_dif->fsoff + cur) >> 9;
                                bio->bi_private = q[JQ_SUBMIT];
                                if (readahead)
                                        bio->bi_opf |= REQ_RAHEAD;