2022-09-14 10:55:33

by Jia Zhu

[permalink] [raw]
Subject: [PATCH V3 4/6] erofs: introduce fscache-based domain

A new fscache-based shared domain mode is going to be introduced for
erofs. In which case, same data blobs in same domain will be shared
and reused to reduce on-disk space usage.

As the first step, we use pseudo mnt to manage and maintain domain's
lifecycle.

The implementation of sharing blobs will be introduced in subsequent
patches.

Signed-off-by: Jia Zhu <[email protected]>
---
fs/erofs/fscache.c | 134 ++++++++++++++++++++++++++++++++++++++------
fs/erofs/internal.h | 9 +++
2 files changed, 127 insertions(+), 16 deletions(-)

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index 4159cf781924..b2100dc67cde 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -1,10 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022, Alibaba Cloud
+ * Copyright (C) 2022, Bytedance Inc. All rights reserved.
*/
#include <linux/fscache.h>
#include "internal.h"

+static DEFINE_MUTEX(erofs_domain_list_lock);
+static LIST_HEAD(erofs_domain_list);
+
static struct netfs_io_request *erofs_fscache_alloc_request(struct address_space *mapping,
loff_t start, size_t len)
{
@@ -417,6 +421,106 @@ const struct address_space_operations erofs_fscache_access_aops = {
.readahead = erofs_fscache_readahead,
};

+static
+struct erofs_domain *erofs_fscache_domain_get(struct erofs_domain *domain)
+{
+ refcount_inc(&domain->ref);
+ return domain;
+}
+
+static void erofs_fscache_domain_put(struct erofs_domain *domain)
+{
+ if (!domain)
+ return;
+ if (refcount_dec_and_test(&domain->ref)) {
+ fscache_relinquish_volume(domain->volume, NULL, false);
+ mutex_lock(&erofs_domain_list_lock);
+ list_del(&domain->list);
+ mutex_unlock(&erofs_domain_list_lock);
+ kfree(domain->domain_id);
+ kfree(domain);
+ }
+}
+
+static int erofs_fscache_register_volume(struct super_block *sb)
+{
+ struct erofs_sb_info *sbi = EROFS_SB(sb);
+ char *domain_id = sbi->opt.domain_id;
+ struct fscache_volume *volume;
+ char *name;
+ int ret = 0;
+
+ if (domain_id)
+ name = kasprintf(GFP_KERNEL, "erofs,%s", domain_id);
+ else
+ name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
+ if (!name)
+ return -ENOMEM;
+
+ volume = fscache_acquire_volume(name, NULL, NULL, 0);
+ if (IS_ERR_OR_NULL(volume)) {
+ erofs_err(sb, "failed to register volume for %s", name);
+ ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
+ volume = NULL;
+ }
+
+ sbi->volume = volume;
+ kfree(name);
+ return ret;
+}
+
+static int erofs_fscache_init_domain(struct super_block *sb)
+{
+ int err;
+ struct erofs_domain *domain;
+ struct erofs_sb_info *sbi = EROFS_SB(sb);
+
+ domain = kzalloc(sizeof(struct erofs_domain), GFP_KERNEL);
+ if (!domain)
+ return -ENOMEM;
+
+ domain->domain_id = kstrdup(sbi->opt.domain_id, GFP_KERNEL);
+ if (!domain->domain_id) {
+ kfree(domain);
+ return -ENOMEM;
+ }
+ sbi->domain = domain;
+ err = erofs_fscache_register_volume(sb);
+ if (err)
+ goto out;
+
+ domain->volume = sbi->volume;
+ refcount_set(&domain->ref, 1);
+ mutex_init(&domain->mutex);
+ list_add(&domain->list, &erofs_domain_list);
+ return 0;
+out:
+ kfree(domain->domain_id);
+ kfree(domain);
+ sbi->domain = NULL;
+ return err;
+}
+
+static int erofs_fscache_register_domain(struct super_block *sb)
+{
+ int err;
+ struct erofs_domain *domain;
+ struct erofs_sb_info *sbi = EROFS_SB(sb);
+
+ mutex_lock(&erofs_domain_list_lock);
+ list_for_each_entry(domain, &erofs_domain_list, list) {
+ if (!strcmp(domain->domain_id, sbi->opt.domain_id)) {
+ sbi->domain = erofs_fscache_domain_get(domain);
+ sbi->volume = domain->volume;
+ mutex_unlock(&erofs_domain_list_lock);
+ return 0;
+ }
+ }
+ err = erofs_fscache_init_domain(sb);
+ mutex_unlock(&erofs_domain_list_lock);
+ return err;
+}
+
struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
char *name, bool need_inode)
{
@@ -486,24 +590,16 @@ void erofs_fscache_unregister_cookie(struct erofs_fscache *ctx)
int erofs_fscache_register_fs(struct super_block *sb)
{
struct erofs_sb_info *sbi = EROFS_SB(sb);
- struct fscache_volume *volume;
struct erofs_fscache *fscache;
- char *name;
- int ret = 0;
+ int ret;

- name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
- if (!name)
- return -ENOMEM;
+ if (sbi->opt.domain_id)
+ ret = erofs_fscache_register_domain(sb);
+ else
+ ret = erofs_fscache_register_volume(sb);

- volume = fscache_acquire_volume(name, NULL, NULL, 0);
- if (IS_ERR_OR_NULL(volume)) {
- erofs_err(sb, "failed to register volume for %s", name);
- ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
- volume = NULL;
- }
-
- sbi->volume = volume;
- kfree(name);
+ if (ret)
+ return ret;

fscache = erofs_fscache_register_cookie(sb, sbi->opt.fsid, true);
if (IS_ERR(fscache))
@@ -518,7 +614,13 @@ void erofs_fscache_unregister_fs(struct super_block *sb)
struct erofs_sb_info *sbi = EROFS_SB(sb);

erofs_fscache_unregister_cookie(sbi->s_fscache);
- fscache_relinquish_volume(sbi->volume, NULL, false);
sbi->s_fscache = NULL;
+
+ if (sbi->domain)
+ erofs_fscache_domain_put(sbi->domain);
+ else
+ fscache_relinquish_volume(sbi->volume, NULL, false);
+
sbi->volume = NULL;
+ sbi->domain = NULL;
}
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 2d129c6b3027..5ce6889d6f1d 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -99,6 +99,14 @@ struct erofs_sb_lz4_info {
u16 max_pclusterblks;
};

+struct erofs_domain {
+ refcount_t ref;
+ struct mutex mutex;
+ struct list_head list;
+ struct fscache_volume *volume;
+ char *domain_id;
+};
+
struct erofs_fscache {
struct fscache_cookie *cookie;
struct inode *inode;
@@ -158,6 +166,7 @@ struct erofs_sb_info {
/* fscache support */
struct fscache_volume *volume;
struct erofs_fscache *s_fscache;
+ struct erofs_domain *domain;
};

#define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
--
2.20.1


2022-09-14 14:23:13

by Gao Xiang

[permalink] [raw]
Subject: Re: [PATCH V3 4/6] erofs: introduce fscache-based domain

On Wed, Sep 14, 2022 at 06:50:39PM +0800, Jia Zhu wrote:
> A new fscache-based shared domain mode is going to be introduced for
> erofs. In which case, same data blobs in same domain will be shared
> and reused to reduce on-disk space usage.
>
> As the first step, we use pseudo mnt to manage and maintain domain's
> lifecycle.
>
> The implementation of sharing blobs will be introduced in subsequent
> patches.
>
> Signed-off-by: Jia Zhu <[email protected]>
> ---
> fs/erofs/fscache.c | 134 ++++++++++++++++++++++++++++++++++++++------
> fs/erofs/internal.h | 9 +++
> 2 files changed, 127 insertions(+), 16 deletions(-)
>
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index 4159cf781924..b2100dc67cde 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -1,10 +1,14 @@
> // SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (C) 2022, Alibaba Cloud
> + * Copyright (C) 2022, Bytedance Inc. All rights reserved.
> */
> #include <linux/fscache.h>
> #include "internal.h"
>
> +static DEFINE_MUTEX(erofs_domain_list_lock);
> +static LIST_HEAD(erofs_domain_list);
> +
> static struct netfs_io_request *erofs_fscache_alloc_request(struct address_space *mapping,
> loff_t start, size_t len)
> {
> @@ -417,6 +421,106 @@ const struct address_space_operations erofs_fscache_access_aops = {
> .readahead = erofs_fscache_readahead,
> };
>
> +static
> +struct erofs_domain *erofs_fscache_domain_get(struct erofs_domain *domain)
> +{
> + refcount_inc(&domain->ref);
> + return domain;
> +}

We can just open-code that.

> +
> +static void erofs_fscache_domain_put(struct erofs_domain *domain)
> +{
> + if (!domain)
> + return;
> + if (refcount_dec_and_test(&domain->ref)) {
> + fscache_relinquish_volume(domain->volume, NULL, false);
> + mutex_lock(&erofs_domain_list_lock);
> + list_del(&domain->list);
> + mutex_unlock(&erofs_domain_list_lock);
> + kfree(domain->domain_id);
> + kfree(domain);
> + }
> +}
> +
> +static int erofs_fscache_register_volume(struct super_block *sb)
> +{
> + struct erofs_sb_info *sbi = EROFS_SB(sb);
> + char *domain_id = sbi->opt.domain_id;
> + struct fscache_volume *volume;
> + char *name;
> + int ret = 0;
> +
> + if (domain_id)
> + name = kasprintf(GFP_KERNEL, "erofs,%s", domain_id);
> + else
> + name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);

I think we could just use

name = kasprintf(GFP_KERNEL, "erofs,%s",
domain_id ? domain_id : sbi->opt.fsid);

here.

> + if (!name)
> + return -ENOMEM;
> +
> + volume = fscache_acquire_volume(name, NULL, NULL, 0);
> + if (IS_ERR_OR_NULL(volume)) {
> + erofs_err(sb, "failed to register volume for %s", name);
> + ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
> + volume = NULL;
> + }
> +
> + sbi->volume = volume;
> + kfree(name);
> + return ret;
> +}
> +
> +static int erofs_fscache_init_domain(struct super_block *sb)
> +{
> + int err;
> + struct erofs_domain *domain;
> + struct erofs_sb_info *sbi = EROFS_SB(sb);
> +
> + domain = kzalloc(sizeof(struct erofs_domain), GFP_KERNEL);
> + if (!domain)
> + return -ENOMEM;
> +
> + domain->domain_id = kstrdup(sbi->opt.domain_id, GFP_KERNEL);
> + if (!domain->domain_id) {
> + kfree(domain);
> + return -ENOMEM;
> + }
> + sbi->domain = domain;
> + err = erofs_fscache_register_volume(sb);
> + if (err)
> + goto out;
> +
> + domain->volume = sbi->volume;
> + refcount_set(&domain->ref, 1);
> + mutex_init(&domain->mutex);
> + list_add(&domain->list, &erofs_domain_list);
> + return 0;
> +out:
> + kfree(domain->domain_id);
> + kfree(domain);
> + sbi->domain = NULL;
> + return err;
> +}
> +
> +static int erofs_fscache_register_domain(struct super_block *sb)
> +{
> + int err;
> + struct erofs_domain *domain;
> + struct erofs_sb_info *sbi = EROFS_SB(sb);
> +
> + mutex_lock(&erofs_domain_list_lock);
> + list_for_each_entry(domain, &erofs_domain_list, list) {
> + if (!strcmp(domain->domain_id, sbi->opt.domain_id)) {
> + sbi->domain = erofs_fscache_domain_get(domain);
> + sbi->volume = domain->volume;
> + mutex_unlock(&erofs_domain_list_lock);
> + return 0;
> + }
> + }
> + err = erofs_fscache_init_domain(sb);

why introduce erofs_fscache_init_domain?

> + mutex_unlock(&erofs_domain_list_lock);
> + return err;
> +}
> +
> struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
> char *name, bool need_inode)
> {
> @@ -486,24 +590,16 @@ void erofs_fscache_unregister_cookie(struct erofs_fscache *ctx)
> int erofs_fscache_register_fs(struct super_block *sb)
> {
> struct erofs_sb_info *sbi = EROFS_SB(sb);
> - struct fscache_volume *volume;
> struct erofs_fscache *fscache;
> - char *name;
> - int ret = 0;
> + int ret;
>
> - name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
> - if (!name)
> - return -ENOMEM;
> + if (sbi->opt.domain_id)
> + ret = erofs_fscache_register_domain(sb);
> + else
> + ret = erofs_fscache_register_volume(sb);
>
> - volume = fscache_acquire_volume(name, NULL, NULL, 0);
> - if (IS_ERR_OR_NULL(volume)) {
> - erofs_err(sb, "failed to register volume for %s", name);
> - ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
> - volume = NULL;
> - }
> -
> - sbi->volume = volume;
> - kfree(name);
> + if (ret)
> + return ret;
>
> fscache = erofs_fscache_register_cookie(sb, sbi->opt.fsid, true);
> if (IS_ERR(fscache))
> @@ -518,7 +614,13 @@ void erofs_fscache_unregister_fs(struct super_block *sb)
> struct erofs_sb_info *sbi = EROFS_SB(sb);
>
> erofs_fscache_unregister_cookie(sbi->s_fscache);
> - fscache_relinquish_volume(sbi->volume, NULL, false);
> sbi->s_fscache = NULL;
> +
> + if (sbi->domain)
> + erofs_fscache_domain_put(sbi->domain);
> + else
> + fscache_relinquish_volume(sbi->volume, NULL, false);
> +

How about using one helper and pass in sb directly instead?

Thanks,
Gao Xiang

2022-09-15 03:38:04

by Jingbo Xu

[permalink] [raw]
Subject: Re: [PATCH V3 4/6] erofs: introduce fscache-based domain



On 9/14/22 6:50 PM, Jia Zhu wrote:
> A new fscache-based shared domain mode is going to be introduced for
> erofs. In which case, same data blobs in same domain will be shared
> and reused to reduce on-disk space usage.
>
> As the first step, we use pseudo mnt to manage and maintain domain's
> lifecycle.

The commit message needs to be updated, since the pseudo mnt is not
introduced yet in this patch.

>
> The implementation of sharing blobs will be introduced in subsequent
> patches.
>
> Signed-off-by: Jia Zhu <[email protected]>
> ---
> fs/erofs/fscache.c | 134 ++++++++++++++++++++++++++++++++++++++------
> fs/erofs/internal.h | 9 +++
> 2 files changed, 127 insertions(+), 16 deletions(-)
>
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index 4159cf781924..b2100dc67cde 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -1,10 +1,14 @@
> // SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (C) 2022, Alibaba Cloud
> + * Copyright (C) 2022, Bytedance Inc. All rights reserved.
> */
> #include <linux/fscache.h>
> #include "internal.h"
>
> +static DEFINE_MUTEX(erofs_domain_list_lock);
> +static LIST_HEAD(erofs_domain_list);
> +
> static struct netfs_io_request *erofs_fscache_alloc_request(struct address_space *mapping,
> loff_t start, size_t len)
> {
> @@ -417,6 +421,106 @@ const struct address_space_operations erofs_fscache_access_aops = {
> .readahead = erofs_fscache_readahead,
> };
>
> +static
> +struct erofs_domain *erofs_fscache_domain_get(struct erofs_domain *domain)
> +{
> + refcount_inc(&domain->ref);

refcount_inc_not_zero() is prefered here.

Considering the following time sequence:

CPU1 CPU2
------ ------
erofs_fscache_domain_put
refcount decreased to 0
erofs_fscache_register_domain
mutex_lock
erofs_fscache_domain_get
inc refcount to 1
mutex_unlock
mutex_lock
remove the domain from list
mutex_unlock



> + return domain;
> +}
> +
> +static void erofs_fscache_domain_put(struct erofs_domain *domain)
> +{
> + if (!domain)
> + return;
> + if (refcount_dec_and_test(&domain->ref)) {
> + fscache_relinquish_volume(domain->volume, NULL, false);
> + mutex_lock(&erofs_domain_list_lock);
> + list_del(&domain->list);
> + mutex_unlock(&erofs_domain_list_lock);

We need to delete the domain from the list first, and then calling
fscache_relinquish_volume(), so that others won't race with this.


> + kfree(domain->domain_id);
> + kfree(domain);
> + }
> +}
> +
> +static int erofs_fscache_register_volume(struct super_block *sb)
> +{
> + struct erofs_sb_info *sbi = EROFS_SB(sb);
> + char *domain_id = sbi->opt.domain_id;
> + struct fscache_volume *volume;
> + char *name;
> + int ret = 0;
> +
> + if (domain_id)
> + name = kasprintf(GFP_KERNEL, "erofs,%s", domain_id);
> + else
> + name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
> + if (!name)
> + return -ENOMEM;
> +
> + volume = fscache_acquire_volume(name, NULL, NULL, 0);
> + if (IS_ERR_OR_NULL(volume)) {
> + erofs_err(sb, "failed to register volume for %s", name);
> + ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
> + volume = NULL;
> + }
> +
> + sbi->volume = volume;
> + kfree(name);
> + return ret;
> +}
> +
> +static int erofs_fscache_init_domain(struct super_block *sb)
> +{
> + int err;
> + struct erofs_domain *domain;
> + struct erofs_sb_info *sbi = EROFS_SB(sb);
> +
> + domain = kzalloc(sizeof(struct erofs_domain), GFP_KERNEL);
> + if (!domain)
> + return -ENOMEM;
> +
> + domain->domain_id = kstrdup(sbi->opt.domain_id, GFP_KERNEL);
> + if (!domain->domain_id) {
> + kfree(domain);
> + return -ENOMEM;
> + }
> + sbi->domain = domain;

Why bothering setting sbi->domain here? Can't we set sbi->domain finnaly
when the domain has been fully initialized?


> + err = erofs_fscache_register_volume(sb);
> + if (err)
> + goto out;
> +
> + domain->volume = sbi->volume;
> + refcount_set(&domain->ref, 1);
> + mutex_init(&domain->mutex);
> + list_add(&domain->list, &erofs_domain_list);
> + return 0;
> +out:
> + kfree(domain->domain_id);
> + kfree(domain);
> + sbi->domain = NULL;
> + return err;
> +}
> +
> +static int erofs_fscache_register_domain(struct super_block *sb)
> +{
> + int err;
> + struct erofs_domain *domain;
> + struct erofs_sb_info *sbi = EROFS_SB(sb);
> +
> + mutex_lock(&erofs_domain_list_lock);
> + list_for_each_entry(domain, &erofs_domain_list, list) {
> + if (!strcmp(domain->domain_id, sbi->opt.domain_id)) {
> + sbi->domain = erofs_fscache_domain_get(domain);
> + sbi->volume = domain->volume;
> + mutex_unlock(&erofs_domain_list_lock);
> + return 0;
> + }
> + }
> + err = erofs_fscache_init_domain(sb);
> + mutex_unlock(&erofs_domain_list_lock);
> + return err;
> +}
> +
> struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
> char *name, bool need_inode)
> {
> @@ -486,24 +590,16 @@ void erofs_fscache_unregister_cookie(struct erofs_fscache *ctx)
> int erofs_fscache_register_fs(struct super_block *sb)
> {
> struct erofs_sb_info *sbi = EROFS_SB(sb);
> - struct fscache_volume *volume;
> struct erofs_fscache *fscache;
> - char *name;
> - int ret = 0;
> + int ret;
>
> - name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
> - if (!name)
> - return -ENOMEM;
> + if (sbi->opt.domain_id)
> + ret = erofs_fscache_register_domain(sb);
> + else
> + ret = erofs_fscache_register_volume(sb);
>
> - volume = fscache_acquire_volume(name, NULL, NULL, 0);
> - if (IS_ERR_OR_NULL(volume)) {
> - erofs_err(sb, "failed to register volume for %s", name);
> - ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
> - volume = NULL;
> - }
> -
> - sbi->volume = volume;
> - kfree(name);
> + if (ret)
> + return ret;
>
> fscache = erofs_fscache_register_cookie(sb, sbi->opt.fsid, true);
> if (IS_ERR(fscache))
> @@ -518,7 +614,13 @@ void erofs_fscache_unregister_fs(struct super_block *sb)
> struct erofs_sb_info *sbi = EROFS_SB(sb);
>
> erofs_fscache_unregister_cookie(sbi->s_fscache);
> - fscache_relinquish_volume(sbi->volume, NULL, false);
> sbi->s_fscache = NULL;
> +
> + if (sbi->domain)
> + erofs_fscache_domain_put(sbi->domain);
> + else
> + fscache_relinquish_volume(sbi->volume, NULL, false);
> +
> sbi->volume = NULL;
> + sbi->domain = NULL;
> }
> diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
> index 2d129c6b3027..5ce6889d6f1d 100644
> --- a/fs/erofs/internal.h
> +++ b/fs/erofs/internal.h
> @@ -99,6 +99,14 @@ struct erofs_sb_lz4_info {
> u16 max_pclusterblks;
> };
>
> +struct erofs_domain {
> + refcount_t ref;
> + struct mutex mutex;
> + struct list_head list;
> + struct fscache_volume *volume;
> + char *domain_id;
> +};
> +
> struct erofs_fscache {
> struct fscache_cookie *cookie;
> struct inode *inode;
> @@ -158,6 +166,7 @@ struct erofs_sb_info {
> /* fscache support */
> struct fscache_volume *volume;
> struct erofs_fscache *s_fscache;
> + struct erofs_domain *domain;
> };
>
> #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)

--
Thanks,
Jingbo

2022-09-15 03:38:09

by Jingbo Xu

[permalink] [raw]
Subject: Re: [PATCH V3 4/6] erofs: introduce fscache-based domain



On 9/14/22 9:21 PM, Gao Xiang wrote:
>> @@ -518,7 +614,13 @@ void erofs_fscache_unregister_fs(struct super_block *sb)
>> struct erofs_sb_info *sbi = EROFS_SB(sb);
>>
>> erofs_fscache_unregister_cookie(sbi->s_fscache);
>> - fscache_relinquish_volume(sbi->volume, NULL, false);
>> sbi->s_fscache = NULL;
>> +
>> + if (sbi->domain)
>> + erofs_fscache_domain_put(sbi->domain);
>> + else
>> + fscache_relinquish_volume(sbi->volume, NULL, false);
>> +
>
> How about using one helper and pass in sb directly instead?
>

Then this helper has only one caller...


--
Thanks,
Jingbo

2022-09-15 08:38:48

by Jia Zhu

[permalink] [raw]
Subject: Re: [External] Re: [PATCH V3 4/6] erofs: introduce fscache-based domain



在 2022/9/14 21:21, Gao Xiang 写道:
> On Wed, Sep 14, 2022 at 06:50:39PM +0800, Jia Zhu wrote:
>> A new fscache-based shared domain mode is going to be introduced for
>> erofs. In which case, same data blobs in same domain will be shared
>> and reused to reduce on-disk space usage.
>>
>> As the first step, we use pseudo mnt to manage and maintain domain's
>> lifecycle.
>>
>> The implementation of sharing blobs will be introduced in subsequent
>> patches.
>>
>> Signed-off-by: Jia Zhu <[email protected]>
>> ---
>> fs/erofs/fscache.c | 134 ++++++++++++++++++++++++++++++++++++++------
>> fs/erofs/internal.h | 9 +++
>> 2 files changed, 127 insertions(+), 16 deletions(-)
>>
>> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
>> index 4159cf781924..b2100dc67cde 100644
>> --- a/fs/erofs/fscache.c
>> +++ b/fs/erofs/fscache.c
>> @@ -1,10 +1,14 @@
>> // SPDX-License-Identifier: GPL-2.0-or-later
>> /*
>> * Copyright (C) 2022, Alibaba Cloud
>> + * Copyright (C) 2022, Bytedance Inc. All rights reserved.
>> */
>> #include <linux/fscache.h>
>> #include "internal.h"
>>
>> +static DEFINE_MUTEX(erofs_domain_list_lock);
>> +static LIST_HEAD(erofs_domain_list);
>> +
>> static struct netfs_io_request *erofs_fscache_alloc_request(struct address_space *mapping,
>> loff_t start, size_t len)
>> {
>> @@ -417,6 +421,106 @@ const struct address_space_operations erofs_fscache_access_aops = {
>> .readahead = erofs_fscache_readahead,
>> };
>>
>> +static
>> +struct erofs_domain *erofs_fscache_domain_get(struct erofs_domain *domain)
>> +{
>> + refcount_inc(&domain->ref);
>> + return domain;
>> +}
>
> We can just open-code that.
Thanks, I'll revise it.
>
>> +
>> +static void erofs_fscache_domain_put(struct erofs_domain *domain)
>> +{
>> + if (!domain)
>> + return;
>> + if (refcount_dec_and_test(&domain->ref)) {
>> + fscache_relinquish_volume(domain->volume, NULL, false);
>> + mutex_lock(&erofs_domain_list_lock);
>> + list_del(&domain->list);
>> + mutex_unlock(&erofs_domain_list_lock);
>> + kfree(domain->domain_id);
>> + kfree(domain);
>> + }
>> +}
>> +
>> +static int erofs_fscache_register_volume(struct super_block *sb)
>> +{
>> + struct erofs_sb_info *sbi = EROFS_SB(sb);
>> + char *domain_id = sbi->opt.domain_id;
>> + struct fscache_volume *volume;
>> + char *name;
>> + int ret = 0;
>> +
>> + if (domain_id)
>> + name = kasprintf(GFP_KERNEL, "erofs,%s", domain_id);
>> + else
>> + name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
>
> I think we could just use
>
> name = kasprintf(GFP_KERNEL, "erofs,%s",
> domain_id ? domain_id : sbi->opt.fsid);
>
> here.
Thanks.
>
>> + if (!name)
>> + return -ENOMEM;
>> +
>> + volume = fscache_acquire_volume(name, NULL, NULL, 0);
>> + if (IS_ERR_OR_NULL(volume)) {
>> + erofs_err(sb, "failed to register volume for %s", name);
>> + ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
>> + volume = NULL;
>> + }
>> +
>> + sbi->volume = volume;
>> + kfree(name);
>> + return ret;
>> +}
>> +
>> +static int erofs_fscache_init_domain(struct super_block *sb)
>> +{
>> + int err;
>> + struct erofs_domain *domain;
>> + struct erofs_sb_info *sbi = EROFS_SB(sb);
>> +
>> + domain = kzalloc(sizeof(struct erofs_domain), GFP_KERNEL);
>> + if (!domain)
>> + return -ENOMEM;
>> +
>> + domain->domain_id = kstrdup(sbi->opt.domain_id, GFP_KERNEL);
>> + if (!domain->domain_id) {
>> + kfree(domain);
>> + return -ENOMEM;
>> + }
>> + sbi->domain = domain;
>> + err = erofs_fscache_register_volume(sb);
>> + if (err)
>> + goto out;
>> +
>> + domain->volume = sbi->volume;
>> + refcount_set(&domain->ref, 1);
>> + mutex_init(&domain->mutex);
>> + list_add(&domain->list, &erofs_domain_list);
>> + return 0;
>> +out:
>> + kfree(domain->domain_id);
>> + kfree(domain);
>> + sbi->domain = NULL;
>> + return err;
>> +}
>> +
>> +static int erofs_fscache_register_domain(struct super_block *sb)
>> +{
>> + int err;
>> + struct erofs_domain *domain;
>> + struct erofs_sb_info *sbi = EROFS_SB(sb);
>> +
>> + mutex_lock(&erofs_domain_list_lock);
>> + list_for_each_entry(domain, &erofs_domain_list, list) {
>> + if (!strcmp(domain->domain_id, sbi->opt.domain_id)) {
>> + sbi->domain = erofs_fscache_domain_get(domain);
>> + sbi->volume = domain->volume;
>> + mutex_unlock(&erofs_domain_list_lock);
>> + return 0;
>> + }
>> + }
>> + err = erofs_fscache_init_domain(sb);
>
> why introduce erofs_fscache_init_domain?
>
To make error handling path simpler, avoid lots of 'goto' and 'unlock'
>> + mutex_unlock(&erofs_domain_list_lock);
>> + return err;
>> +}
>> +
>> struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
>> char *name, bool need_inode)
>> {
>> @@ -486,24 +590,16 @@ void erofs_fscache_unregister_cookie(struct erofs_fscache *ctx)
>> int erofs_fscache_register_fs(struct super_block *sb)
>> {
>> struct erofs_sb_info *sbi = EROFS_SB(sb);
>> - struct fscache_volume *volume;
>> struct erofs_fscache *fscache;
>> - char *name;
>> - int ret = 0;
>> + int ret;
>>
>> - name = kasprintf(GFP_KERNEL, "erofs,%s", sbi->opt.fsid);
>> - if (!name)
>> - return -ENOMEM;
>> + if (sbi->opt.domain_id)
>> + ret = erofs_fscache_register_domain(sb);
>> + else
>> + ret = erofs_fscache_register_volume(sb);
>>
>> - volume = fscache_acquire_volume(name, NULL, NULL, 0);
>> - if (IS_ERR_OR_NULL(volume)) {
>> - erofs_err(sb, "failed to register volume for %s", name);
>> - ret = volume ? PTR_ERR(volume) : -EOPNOTSUPP;
>> - volume = NULL;
>> - }
>> -
>> - sbi->volume = volume;
>> - kfree(name);
>> + if (ret)
>> + return ret;
>>
>> fscache = erofs_fscache_register_cookie(sb, sbi->opt.fsid, true);
>> if (IS_ERR(fscache))
>> @@ -518,7 +614,13 @@ void erofs_fscache_unregister_fs(struct super_block *sb)
>> struct erofs_sb_info *sbi = EROFS_SB(sb);
>>
>> erofs_fscache_unregister_cookie(sbi->s_fscache);
>> - fscache_relinquish_volume(sbi->volume, NULL, false);
>> sbi->s_fscache = NULL;
>> +
>> + if (sbi->domain)
>> + erofs_fscache_domain_put(sbi->domain);
>> + else
>> + fscache_relinquish_volume(sbi->volume, NULL, false);
>> +
>
> How about using one helper and pass in sb directly instead?
Thanks for your suggestion, I'll revise it in next version.
>
> Thanks,
> Gao Xiang