2023-02-09 05:18:59

by Jingbo Xu

[permalink] [raw]
Subject: [PATCH v2 0/4] erofs: cleanup for fscache share domain mode

v2:
- patch 4: add EROFS_REG_COOKIE_SHARE flag to indicate that the blob is
shareable among several erofs fs instances (Zhu Jia)
- patch 4: rearrange the prototype of related functions to make it more
compact (including rename erofs_fscache_domain_init_cookie() to
erofs_domain_init_cookie())

Jingbo Xu (4):
erofs: remove unused device mapping in meta routine
erofs: maintain cookies of share domain in self-contained list
erofs: relinquish volume with mutex held
erofs: unify anonymous inodes for blob

fs/erofs/fscache.c | 148 ++++++++++++++++++--------------------------
fs/erofs/internal.h | 11 ++--
fs/erofs/super.c | 2 +
3 files changed, 68 insertions(+), 93 deletions(-)

--
2.19.1.6.gb485710b



2023-02-09 05:19:03

by Jingbo Xu

[permalink] [raw]
Subject: [PATCH v2 4/4] erofs: unify anonymous inodes for blob

Currently there're two anonymous inodes (inode and anon_inode in struct
erofs_fscache) for each blob. The former was introduced as the
address_space of page cache for bootstrap.

The latter was initially introduced as both the address_space of page
cache and also a sentinel in the shared domain. Since now the management
of cookies in share domain has been decoupled with the anonymous inode,
there's no need to maintain an extra anonymous inode. Let's unify these
two anonymous inodes.

Besides, in non-share-domain mode only bootstrap will allocate anonymous
inode. To simplify the implementation, always allocate anonymous inode
for both bootstrap and data blobs. Similarly release anonymous inodes
for data blobs when .put_super() is called, or we'll get "VFS: Busy
inodes after unmount." warning.

Also remove the redundant set_nlink() when initializing the anonymous
inode, since i_nlink has already been initialized to 1 when the inode
gets allocated.

Signed-off-by: Jingbo Xu <[email protected]>
---
fs/erofs/fscache.c | 85 ++++++++++++++++++---------------------------
fs/erofs/internal.h | 7 ++--
fs/erofs/super.c | 2 ++
3 files changed, 38 insertions(+), 56 deletions(-)

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index a6f030966147..a9498239163a 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -422,14 +422,14 @@ static int erofs_fscache_register_domain(struct super_block *sb)
return err;
}

-static
-struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
- char *name,
- unsigned int flags)
+static struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
+ char *name, unsigned int flags)
{
struct fscache_volume *volume = EROFS_SB(sb)->volume;
struct erofs_fscache *ctx;
struct fscache_cookie *cookie;
+ struct super_block *isb;
+ struct inode *inode;
int ret;

ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
@@ -445,33 +445,32 @@ struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
ret = -EINVAL;
goto err;
}
-
fscache_use_cookie(cookie, false);
- ctx->cookie = cookie;
-
- if (flags & EROFS_REG_COOKIE_NEED_INODE) {
- struct inode *const inode = new_inode(sb);
-
- if (!inode) {
- erofs_err(sb, "failed to get anon inode for %s", name);
- ret = -ENOMEM;
- goto err_cookie;
- }
-
- set_nlink(inode, 1);
- inode->i_size = OFFSET_MAX;
- inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
- mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
- inode->i_private = ctx;

- ctx->inode = inode;
+ /*
+ * Allocate anonymous inode in global pseudo mount for shareable blobs,
+ * so that they are accessible among erofs fs instances.
+ */
+ isb = flags & EROFS_REG_COOKIE_SHARE ? erofs_pseudo_mnt->mnt_sb : sb;
+ inode = new_inode(isb);
+ if (!inode) {
+ erofs_err(sb, "failed to get anon inode for %s", name);
+ ret = -ENOMEM;
+ goto err_cookie;
}

+ inode->i_size = OFFSET_MAX;
+ inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
+ mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
+ inode->i_private = ctx;
+
+ ctx->cookie = cookie;
+ ctx->inode = inode;
return ctx;

err_cookie:
- fscache_unuse_cookie(ctx->cookie, NULL, NULL);
- fscache_relinquish_cookie(ctx->cookie, false);
+ fscache_unuse_cookie(cookie, NULL, NULL);
+ fscache_relinquish_cookie(cookie, false);
err:
kfree(ctx);
return ERR_PTR(ret);
@@ -482,18 +481,13 @@ static void erofs_fscache_relinquish_cookie(struct erofs_fscache *ctx)
fscache_unuse_cookie(ctx->cookie, NULL, NULL);
fscache_relinquish_cookie(ctx->cookie, false);
iput(ctx->inode);
- iput(ctx->anon_inode);
kfree(ctx->name);
kfree(ctx);
}

-static
-struct erofs_fscache *erofs_fscache_domain_init_cookie(struct super_block *sb,
- char *name,
- unsigned int flags)
+static struct erofs_fscache *erofs_domain_init_cookie(struct super_block *sb,
+ char *name, unsigned int flags)
{
- int err;
- struct inode *inode;
struct erofs_fscache *ctx;
struct erofs_domain *domain = EROFS_SB(sb)->domain;

@@ -503,35 +497,23 @@ struct erofs_fscache *erofs_fscache_domain_init_cookie(struct super_block *sb,

ctx->name = kstrdup(name, GFP_KERNEL);
if (!ctx->name) {
- err = -ENOMEM;
- goto out;
- }
-
- inode = new_inode(erofs_pseudo_mnt->mnt_sb);
- if (!inode) {
- err = -ENOMEM;
- goto out;
+ erofs_fscache_relinquish_cookie(ctx);
+ return ERR_PTR(-ENOMEM);
}

+ refcount_inc(&domain->ref);
ctx->domain = domain;
- ctx->anon_inode = inode;
list_add(&ctx->node, &erofs_domain_cookies_list);
- inode->i_private = ctx;
- refcount_inc(&domain->ref);
return ctx;
-out:
- erofs_fscache_relinquish_cookie(ctx);
- return ERR_PTR(err);
}

-static
-struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
- char *name,
- unsigned int flags)
+static struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
+ char *name, unsigned int flags)
{
struct erofs_fscache *ctx;
struct erofs_domain *domain = EROFS_SB(sb)->domain;

+ flags |= EROFS_REG_COOKIE_SHARE;
mutex_lock(&erofs_domain_cookies_lock);
list_for_each_entry(ctx, &erofs_domain_cookies_list, node) {
if (ctx->domain != domain || strcmp(ctx->name, name))
@@ -546,7 +528,7 @@ struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
mutex_unlock(&erofs_domain_cookies_lock);
return ctx;
}
- ctx = erofs_fscache_domain_init_cookie(sb, name, flags);
+ ctx = erofs_domain_init_cookie(sb, name, flags);
mutex_unlock(&erofs_domain_cookies_lock);
return ctx;
}
@@ -585,7 +567,7 @@ int erofs_fscache_register_fs(struct super_block *sb)
int ret;
struct erofs_sb_info *sbi = EROFS_SB(sb);
struct erofs_fscache *fscache;
- unsigned int flags;
+ unsigned int flags = 0;

if (sbi->domain_id)
ret = erofs_fscache_register_domain(sb);
@@ -604,7 +586,6 @@ int erofs_fscache_register_fs(struct super_block *sb)
*
* Acquired domain/volume will be relinquished in kill_sb() on error.
*/
- flags = EROFS_REG_COOKIE_NEED_INODE;
if (sbi->domain_id)
flags |= EROFS_REG_COOKIE_NEED_NOEXIST;
fscache = erofs_fscache_register_cookie(sb, sbi->fsid, flags);
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 8358cf5f731e..50cd257d04e3 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -107,8 +107,7 @@ struct erofs_domain {

struct erofs_fscache {
struct fscache_cookie *cookie;
- struct inode *inode;
- struct inode *anon_inode;
+ struct inode *inode; /* anonymous indoe for the blob */

/* used for share domain mode */
struct erofs_domain *domain;
@@ -450,8 +449,8 @@ extern const struct file_operations erofs_dir_fops;
extern const struct iomap_ops z_erofs_iomap_report_ops;

/* flags for erofs_fscache_register_cookie() */
-#define EROFS_REG_COOKIE_NEED_INODE 1
-#define EROFS_REG_COOKIE_NEED_NOEXIST 2
+#define EROFS_REG_COOKIE_SHARE 0x0001
+#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002

void erofs_unmap_metabuf(struct erofs_buf *buf);
void erofs_put_metabuf(struct erofs_buf *buf);
diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 715efa94eed4..19b1ae79cec4 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -968,6 +968,8 @@ static void erofs_put_super(struct super_block *sb)
iput(sbi->packed_inode);
sbi->packed_inode = NULL;
#endif
+ erofs_free_dev_context(sbi->devs);
+ sbi->devs = NULL;
erofs_fscache_unregister_fs(sb);
}

--
2.19.1.6.gb485710b


2023-02-09 05:19:06

by Jingbo Xu

[permalink] [raw]
Subject: [PATCH v2 3/4] erofs: relinquish volume with mutex held

Relinquish fscache volume with mutex held. Otherwise if a new domain is
registered when the old domain with the same name gets removed from the
list but not relinquished yet, fscache may complain the collision.

Signed-off-by: Jingbo Xu <[email protected]>
Reviewed-by: Jia Zhu <[email protected]>
---
fs/erofs/fscache.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index 2f5930e177cc..a6f030966147 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -328,8 +328,8 @@ static void erofs_fscache_domain_put(struct erofs_domain *domain)
kern_unmount(erofs_pseudo_mnt);
erofs_pseudo_mnt = NULL;
}
- mutex_unlock(&erofs_domain_list_lock);
fscache_relinquish_volume(domain->volume, NULL, false);
+ mutex_unlock(&erofs_domain_list_lock);
kfree(domain->domain_id);
kfree(domain);
return;
--
2.19.1.6.gb485710b


2023-02-09 06:02:59

by Jia Zhu

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] erofs: unify anonymous inodes for blob



在 2023/2/9 13:18, Jingbo Xu 写道:
> Currently there're two anonymous inodes (inode and anon_inode in struct
> erofs_fscache) for each blob. The former was introduced as the
> address_space of page cache for bootstrap.
>
> The latter was initially introduced as both the address_space of page
> cache and also a sentinel in the shared domain. Since now the management
> of cookies in share domain has been decoupled with the anonymous inode,
> there's no need to maintain an extra anonymous inode. Let's unify these
> two anonymous inodes.
>
> Besides, in non-share-domain mode only bootstrap will allocate anonymous
> inode. To simplify the implementation, always allocate anonymous inode
> for both bootstrap and data blobs. Similarly release anonymous inodes
> for data blobs when .put_super() is called, or we'll get "VFS: Busy
> inodes after unmount." warning.
>
> Also remove the redundant set_nlink() when initializing the anonymous
> inode, since i_nlink has already been initialized to 1 when the inode
> gets allocated.
>
> Signed-off-by: Jingbo Xu <[email protected]>

Reviewed-by: Jia Zhu <[email protected]>

> ---
> fs/erofs/fscache.c | 85 ++++++++++++++++++---------------------------
> fs/erofs/internal.h | 7 ++--
> fs/erofs/super.c | 2 ++
> 3 files changed, 38 insertions(+), 56 deletions(-)
>
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index a6f030966147..a9498239163a 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -422,14 +422,14 @@ static int erofs_fscache_register_domain(struct super_block *sb)
> return err;
> }
>
> -static
> -struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
> - char *name,
> - unsigned int flags)
> +static struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
> + char *name, unsigned int flags)
> {
> struct fscache_volume *volume = EROFS_SB(sb)->volume;
> struct erofs_fscache *ctx;
> struct fscache_cookie *cookie;
> + struct super_block *isb;
> + struct inode *inode;
> int ret;
>
> ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
> @@ -445,33 +445,32 @@ struct erofs_fscache *erofs_fscache_acquire_cookie(struct super_block *sb,
> ret = -EINVAL;
> goto err;
> }
> -
> fscache_use_cookie(cookie, false);
> - ctx->cookie = cookie;
> -
> - if (flags & EROFS_REG_COOKIE_NEED_INODE) {
> - struct inode *const inode = new_inode(sb);
> -
> - if (!inode) {
> - erofs_err(sb, "failed to get anon inode for %s", name);
> - ret = -ENOMEM;
> - goto err_cookie;
> - }
> -
> - set_nlink(inode, 1);
> - inode->i_size = OFFSET_MAX;
> - inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
> - mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
> - inode->i_private = ctx;
>
> - ctx->inode = inode;
> + /*
> + * Allocate anonymous inode in global pseudo mount for shareable blobs,
> + * so that they are accessible among erofs fs instances.
> + */
> + isb = flags & EROFS_REG_COOKIE_SHARE ? erofs_pseudo_mnt->mnt_sb : sb;
> + inode = new_inode(isb);
> + if (!inode) {
> + erofs_err(sb, "failed to get anon inode for %s", name);
> + ret = -ENOMEM;
> + goto err_cookie;
> }
>
> + inode->i_size = OFFSET_MAX;
> + inode->i_mapping->a_ops = &erofs_fscache_meta_aops;
> + mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
> + inode->i_private = ctx;
> +
> + ctx->cookie = cookie;
> + ctx->inode = inode;
> return ctx;
>
> err_cookie:
> - fscache_unuse_cookie(ctx->cookie, NULL, NULL);
> - fscache_relinquish_cookie(ctx->cookie, false);
> + fscache_unuse_cookie(cookie, NULL, NULL);
> + fscache_relinquish_cookie(cookie, false);
> err:
> kfree(ctx);
> return ERR_PTR(ret);
> @@ -482,18 +481,13 @@ static void erofs_fscache_relinquish_cookie(struct erofs_fscache *ctx)
> fscache_unuse_cookie(ctx->cookie, NULL, NULL);
> fscache_relinquish_cookie(ctx->cookie, false);
> iput(ctx->inode);
> - iput(ctx->anon_inode);
> kfree(ctx->name);
> kfree(ctx);
> }
>
> -static
> -struct erofs_fscache *erofs_fscache_domain_init_cookie(struct super_block *sb,
> - char *name,
> - unsigned int flags)
> +static struct erofs_fscache *erofs_domain_init_cookie(struct super_block *sb,
> + char *name, unsigned int flags)
> {
> - int err;
> - struct inode *inode;
> struct erofs_fscache *ctx;
> struct erofs_domain *domain = EROFS_SB(sb)->domain;
>
> @@ -503,35 +497,23 @@ struct erofs_fscache *erofs_fscache_domain_init_cookie(struct super_block *sb,
>
> ctx->name = kstrdup(name, GFP_KERNEL);
> if (!ctx->name) {
> - err = -ENOMEM;
> - goto out;
> - }
> -
> - inode = new_inode(erofs_pseudo_mnt->mnt_sb);
> - if (!inode) {
> - err = -ENOMEM;
> - goto out;
> + erofs_fscache_relinquish_cookie(ctx);
> + return ERR_PTR(-ENOMEM);
> }
>
> + refcount_inc(&domain->ref);
> ctx->domain = domain;
> - ctx->anon_inode = inode;
> list_add(&ctx->node, &erofs_domain_cookies_list);
> - inode->i_private = ctx;
> - refcount_inc(&domain->ref);
> return ctx;
> -out:
> - erofs_fscache_relinquish_cookie(ctx);
> - return ERR_PTR(err);
> }
>
> -static
> -struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
> - char *name,
> - unsigned int flags)
> +static struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
> + char *name, unsigned int flags)
> {
> struct erofs_fscache *ctx;
> struct erofs_domain *domain = EROFS_SB(sb)->domain;
>
> + flags |= EROFS_REG_COOKIE_SHARE;
> mutex_lock(&erofs_domain_cookies_lock);
> list_for_each_entry(ctx, &erofs_domain_cookies_list, node) {
> if (ctx->domain != domain || strcmp(ctx->name, name))
> @@ -546,7 +528,7 @@ struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
> mutex_unlock(&erofs_domain_cookies_lock);
> return ctx;
> }
> - ctx = erofs_fscache_domain_init_cookie(sb, name, flags);
> + ctx = erofs_domain_init_cookie(sb, name, flags);
> mutex_unlock(&erofs_domain_cookies_lock);
> return ctx;
> }
> @@ -585,7 +567,7 @@ int erofs_fscache_register_fs(struct super_block *sb)
> int ret;
> struct erofs_sb_info *sbi = EROFS_SB(sb);
> struct erofs_fscache *fscache;
> - unsigned int flags;
> + unsigned int flags = 0;
>
> if (sbi->domain_id)
> ret = erofs_fscache_register_domain(sb);
> @@ -604,7 +586,6 @@ int erofs_fscache_register_fs(struct super_block *sb)
> *
> * Acquired domain/volume will be relinquished in kill_sb() on error.
> */
> - flags = EROFS_REG_COOKIE_NEED_INODE;
> if (sbi->domain_id)
> flags |= EROFS_REG_COOKIE_NEED_NOEXIST;
> fscache = erofs_fscache_register_cookie(sb, sbi->fsid, flags);
> diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
> index 8358cf5f731e..50cd257d04e3 100644
> --- a/fs/erofs/internal.h
> +++ b/fs/erofs/internal.h
> @@ -107,8 +107,7 @@ struct erofs_domain {
>
> struct erofs_fscache {
> struct fscache_cookie *cookie;
> - struct inode *inode;
> - struct inode *anon_inode;
> + struct inode *inode; /* anonymous indoe for the blob */
>
> /* used for share domain mode */
> struct erofs_domain *domain;
> @@ -450,8 +449,8 @@ extern const struct file_operations erofs_dir_fops;
> extern const struct iomap_ops z_erofs_iomap_report_ops;
>
> /* flags for erofs_fscache_register_cookie() */
> -#define EROFS_REG_COOKIE_NEED_INODE 1
> -#define EROFS_REG_COOKIE_NEED_NOEXIST 2
> +#define EROFS_REG_COOKIE_SHARE 0x0001
> +#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
>
> void erofs_unmap_metabuf(struct erofs_buf *buf);
> void erofs_put_metabuf(struct erofs_buf *buf);
> diff --git a/fs/erofs/super.c b/fs/erofs/super.c
> index 715efa94eed4..19b1ae79cec4 100644
> --- a/fs/erofs/super.c
> +++ b/fs/erofs/super.c
> @@ -968,6 +968,8 @@ static void erofs_put_super(struct super_block *sb)
> iput(sbi->packed_inode);
> sbi->packed_inode = NULL;
> #endif
> + erofs_free_dev_context(sbi->devs);
> + sbi->devs = NULL;
> erofs_fscache_unregister_fs(sb);
> }
>

2023-02-09 06:06:14

by Gao Xiang

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] erofs: relinquish volume with mutex held



On 2023/2/9 13:18, Jingbo Xu wrote:
> Relinquish fscache volume with mutex held. Otherwise if a new domain is
> registered when the old domain with the same name gets removed from the
> list but not relinquished yet, fscache may complain the collision.
>
> Signed-off-by: Jingbo Xu <[email protected]>
> Reviewed-by: Jia Zhu <[email protected]>

Do we need to backport this to old kernels?
IOWs, whether "Fixes:" tag is needed?

Thanks,
Gao Xiang

> ---
> fs/erofs/fscache.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index 2f5930e177cc..a6f030966147 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -328,8 +328,8 @@ static void erofs_fscache_domain_put(struct erofs_domain *domain)
> kern_unmount(erofs_pseudo_mnt);
> erofs_pseudo_mnt = NULL;
> }
> - mutex_unlock(&erofs_domain_list_lock);
> fscache_relinquish_volume(domain->volume, NULL, false);
> + mutex_unlock(&erofs_domain_list_lock);
> kfree(domain->domain_id);
> kfree(domain);
> return;

2023-02-09 06:14:39

by Jingbo Xu

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] erofs: relinquish volume with mutex held



On 2/9/23 2:06 PM, Gao Xiang wrote:
>
>
> On 2023/2/9 13:18, Jingbo Xu wrote:
>> Relinquish fscache volume with mutex held.  Otherwise if a new domain is
>> registered when the old domain with the same name gets removed from the
>> list but not relinquished yet, fscache may complain the collision.
>>
>> Signed-off-by: Jingbo Xu <[email protected]>
>> Reviewed-by: Jia Zhu <[email protected]>
>
> Do we need to backport this to old kernels?
> IOWs, whether "Fixes:" tag is needed?

Yeah the small fix is appropriate for being backported.

Fixes: 8b7adf1dff3d ("erofs: introduce fscache-based domain")

I will add the "Fixes" tag in the next version.


--
Thanks,
Jingbo