diff mbox series

[1/4] ceph: use type refcount_t for refcount of snap realm

Message ID 20180731150747.27745-1-cgxu519@gmx.com (mailing list archive)
State New, archived
Headers show
Series [1/4] ceph: use type refcount_t for refcount of snap realm | expand

Commit Message

Chengguang Xu July 31, 2018, 3:07 p.m. UTC
Use refcount_t to replace atomic_t when operating refcount.
Meanwhile, introduce a variable to avoid calling refcount_read
twice in debug message.

Signed-off-by: Chengguang Xu <cgxu519@gmx.com>
---
 fs/ceph/snap.c  | 18 ++++++++++--------
 fs/ceph/super.h |  2 +-
 2 files changed, 11 insertions(+), 9 deletions(-)

Comments

Yan, Zheng Aug. 1, 2018, 3:43 a.m. UTC | #1
On Tue, Jul 31, 2018 at 11:10 PM Chengguang Xu <cgxu519@gmx.com> wrote:
>
> Use refcount_t to replace atomic_t when operating refcount.
> Meanwhile, introduce a variable to avoid calling refcount_read
> twice in debug message.
>
> Signed-off-by: Chengguang Xu <cgxu519@gmx.com>
> ---
>  fs/ceph/snap.c  | 18 ++++++++++--------
>  fs/ceph/super.h |  2 +-
>  2 files changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c
> index 01b0144c0b73..b5cff23d9ea2 100644
> --- a/fs/ceph/snap.c
> +++ b/fs/ceph/snap.c
> @@ -63,15 +63,15 @@
>  void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
>                          struct ceph_snap_realm *realm)
>  {
> -       dout("get_realm %p %d -> %d\n", realm,
> -            atomic_read(&realm->nref), atomic_read(&realm->nref)+1);
>         /*
>          * since we _only_ increment realm refs or empty the empty
>          * list with snap_rwsem held, adjusting the empty list here is
>          * safe.  we do need to protect against concurrent empty list
>          * additions, however.
>          */
> -       if (atomic_inc_return(&realm->nref) == 1) {
> +       if (refcount_inc_not_zero(&realm->nref)) {

refcount_inc_not_zero and atomic_inc_return are completely different


> +               unsigned int refcnt = refcount_read(&realm->nref);
> +               dout("get_realm %p %d -> %d\n", realm, refcnt - 1, refcnt);
>                 spin_lock(&mdsc->snap_empty_lock);
>                 list_del_init(&realm->empty_item);
>                 spin_unlock(&mdsc->snap_empty_lock);
> @@ -115,7 +115,7 @@ static struct ceph_snap_realm *ceph_create_snap_realm(
>         if (!realm)
>                 return ERR_PTR(-ENOMEM);
>
> -       atomic_set(&realm->nref, 1);    /* for caller */
> +       refcount_set(&realm->nref, 1);    /* for caller */
>         realm->ino = ino;
>         INIT_LIST_HEAD(&realm->children);
>         INIT_LIST_HEAD(&realm->child_item);
> @@ -193,9 +193,10 @@ static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
>  static void __put_snap_realm(struct ceph_mds_client *mdsc,
>                              struct ceph_snap_realm *realm)
>  {
> +       unsigned int refcnt = refcount_read(&realm->nref);
>         dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
> -            atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
> -       if (atomic_dec_and_test(&realm->nref))
> +               refcnt, refcnt - 1);
> +       if (refcount_dec_and_test(&realm->nref))
>                 __destroy_snap_realm(mdsc, realm);
>  }
>
> @@ -205,9 +206,10 @@ static void __put_snap_realm(struct ceph_mds_client *mdsc,
>  void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
>                          struct ceph_snap_realm *realm)
>  {
> +       unsigned int refcnt = refcount_read(&realm->nref);
>         dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
> -            atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
> -       if (!atomic_dec_and_test(&realm->nref))
> +               refcnt, refcnt - 1);
> +       if (!refcount_dec_and_test(&realm->nref))
>                 return;
>
>         if (down_write_trylock(&mdsc->snap_rwsem)) {
> diff --git a/fs/ceph/super.h b/fs/ceph/super.h
> index fe11d6903b29..d4b0ca5d7a15 100644
> --- a/fs/ceph/super.h
> +++ b/fs/ceph/super.h
> @@ -761,7 +761,7 @@ struct ceph_readdir_cache_control {
>  struct ceph_snap_realm {
>         u64 ino;
>         struct inode *inode;
> -       atomic_t nref;
> +       refcount_t nref;
>         struct rb_node node;
>
>         u64 created, seq;
> --
> 2.17.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox series

Patch

diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c
index 01b0144c0b73..b5cff23d9ea2 100644
--- a/fs/ceph/snap.c
+++ b/fs/ceph/snap.c
@@ -63,15 +63,15 @@ 
 void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
 			 struct ceph_snap_realm *realm)
 {
-	dout("get_realm %p %d -> %d\n", realm,
-	     atomic_read(&realm->nref), atomic_read(&realm->nref)+1);
 	/*
 	 * since we _only_ increment realm refs or empty the empty
 	 * list with snap_rwsem held, adjusting the empty list here is
 	 * safe.  we do need to protect against concurrent empty list
 	 * additions, however.
 	 */
-	if (atomic_inc_return(&realm->nref) == 1) {
+	if (refcount_inc_not_zero(&realm->nref)) {
+		unsigned int refcnt = refcount_read(&realm->nref);
+		dout("get_realm %p %d -> %d\n", realm, refcnt - 1, refcnt);
 		spin_lock(&mdsc->snap_empty_lock);
 		list_del_init(&realm->empty_item);
 		spin_unlock(&mdsc->snap_empty_lock);
@@ -115,7 +115,7 @@  static struct ceph_snap_realm *ceph_create_snap_realm(
 	if (!realm)
 		return ERR_PTR(-ENOMEM);
 
-	atomic_set(&realm->nref, 1);    /* for caller */
+	refcount_set(&realm->nref, 1);    /* for caller */
 	realm->ino = ino;
 	INIT_LIST_HEAD(&realm->children);
 	INIT_LIST_HEAD(&realm->child_item);
@@ -193,9 +193,10 @@  static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
 static void __put_snap_realm(struct ceph_mds_client *mdsc,
 			     struct ceph_snap_realm *realm)
 {
+	unsigned int refcnt = refcount_read(&realm->nref);
 	dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
-	     atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
-	if (atomic_dec_and_test(&realm->nref))
+		refcnt, refcnt - 1);
+	if (refcount_dec_and_test(&realm->nref))
 		__destroy_snap_realm(mdsc, realm);
 }
 
@@ -205,9 +206,10 @@  static void __put_snap_realm(struct ceph_mds_client *mdsc,
 void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
 			 struct ceph_snap_realm *realm)
 {
+	unsigned int refcnt = refcount_read(&realm->nref);
 	dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm,
-	     atomic_read(&realm->nref), atomic_read(&realm->nref)-1);
-	if (!atomic_dec_and_test(&realm->nref))
+		refcnt, refcnt - 1);
+	if (!refcount_dec_and_test(&realm->nref))
 		return;
 
 	if (down_write_trylock(&mdsc->snap_rwsem)) {
diff --git a/fs/ceph/super.h b/fs/ceph/super.h
index fe11d6903b29..d4b0ca5d7a15 100644
--- a/fs/ceph/super.h
+++ b/fs/ceph/super.h
@@ -761,7 +761,7 @@  struct ceph_readdir_cache_control {
 struct ceph_snap_realm {
 	u64 ino;
 	struct inode *inode;
-	atomic_t nref;
+	refcount_t nref;
 	struct rb_node node;
 
 	u64 created, seq;