linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] Enumerated bits in enum
@ 2022-10-11 12:58 David Sterba
  2022-10-11 12:58 ` [PATCH 1/6] btrfs: add helper for bit enumeration David Sterba
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

With some macro magic it's possible to let enum autoincrement define all
bits without manually specifying the value. How it's done is described
in the first patch, the rest are example conversions.

David Sterba (6):
  btrfs: add helper for bit enumeration
  btrfs: convert BTRFS_ILOCK-* defines to enum bit
  btrfs: convert extent_io page op defines to enum bits
  btrfs: convert EXTENT_* bits to enums
  btrfs: convert QGROUP_* defines to enum bits
  btrfs: convert __TRANS_* defines to enum bits

 fs/btrfs/ctree.h          |  9 +++--
 fs/btrfs/extent-io-tree.h | 71 +++++++++++++++++++++------------------
 fs/btrfs/extent_io.h      | 17 ++++++----
 fs/btrfs/misc.h           |  8 +++++
 fs/btrfs/qgroup.h         |  9 +++--
 fs/btrfs/transaction.h    | 18 +++++-----
 6 files changed, 78 insertions(+), 54 deletions(-)

-- 
2.37.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/6] btrfs: add helper for bit enumeration
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
@ 2022-10-11 12:58 ` David Sterba
  2022-10-11 12:58 ` [PATCH 2/6] btrfs: convert BTRFS_ILOCK-* defines to enum bit David Sterba
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Define helper macro that can be used in enum {} to utilize the automatic
increment to define all bits without directly defining the values or
using additional linear bits.

1. capture the sequence value, N
2. use the value to define the given enum with N-th bit set
3. reset the sequence back to N

Use for enums that do not require fixed values for symbolic names (like
for on-disk structures):

enum {
	ENUM_BIT(FIRST),
	ENUM_BIT(SECOND),
	ENUM_BIT(THIRD)
};

Where the values would be 0x1, 0x2 and 0x4.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/misc.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/btrfs/misc.h b/fs/btrfs/misc.h
index f9850edfd726..69826ccd6644 100644
--- a/fs/btrfs/misc.h
+++ b/fs/btrfs/misc.h
@@ -10,6 +10,14 @@
 
 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
 
+/*
+ * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
+ */
+#define ENUM_BIT(name)                                  \
+	__ ## name ## _BIT,                             \
+	name = (1U << __ ## name ## _BIT),              \
+	__ ## name ## _SEQ = __ ## name ## _BIT
+
 static inline void cond_wake_up(struct wait_queue_head *wq)
 {
 	/*
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/6] btrfs: convert BTRFS_ILOCK-* defines to enum bit
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
  2022-10-11 12:58 ` [PATCH 1/6] btrfs: add helper for bit enumeration David Sterba
@ 2022-10-11 12:58 ` David Sterba
  2022-10-11 12:58 ` [PATCH 3/6] btrfs: convert extent_io page op defines to enum bits David Sterba
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/ctree.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index a8b629a166be..ec924cb7033f 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -34,6 +34,7 @@
 #include "async-thread.h"
 #include "block-rsv.h"
 #include "locking.h"
+#include "misc.h"
 
 struct btrfs_trans_handle;
 struct btrfs_transaction;
@@ -3128,9 +3129,11 @@ ssize_t btrfs_dio_rw(struct kiocb *iocb, struct iov_iter *iter, size_t done_befo
 extern const struct dentry_operations btrfs_dentry_operations;
 
 /* Inode locking type flags, by default the exclusive lock is taken */
-#define BTRFS_ILOCK_SHARED	(1U << 0)
-#define BTRFS_ILOCK_TRY 	(1U << 1)
-#define BTRFS_ILOCK_MMAP	(1U << 2)
+enum btrfs_ilock_type {
+	ENUM_BIT(BTRFS_ILOCK_SHARED),
+	ENUM_BIT(BTRFS_ILOCK_TRY),
+	ENUM_BIT(BTRFS_ILOCK_MMAP),
+};
 
 int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags);
 void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags);
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/6] btrfs: convert extent_io page op defines to enum bits
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
  2022-10-11 12:58 ` [PATCH 1/6] btrfs: add helper for bit enumeration David Sterba
  2022-10-11 12:58 ` [PATCH 2/6] btrfs: convert BTRFS_ILOCK-* defines to enum bit David Sterba
@ 2022-10-11 12:58 ` David Sterba
  2022-10-11 12:58 ` [PATCH 4/6] btrfs: convert EXTENT_* bits to enums David Sterba
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/extent_io.h | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index 7929f054dda3..a5ec1475988f 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -9,6 +9,7 @@
 #include <linux/btrfs_tree.h>
 #include "compression.h"
 #include "ulist.h"
+#include "misc.h"
 
 enum {
 	EXTENT_BUFFER_UPTODATE,
@@ -29,13 +30,15 @@ enum {
 };
 
 /* these are flags for __process_pages_contig */
-#define PAGE_UNLOCK		(1 << 0)
-/* Page starts writeback, clear dirty bit and set writeback bit */
-#define PAGE_START_WRITEBACK	(1 << 1)
-#define PAGE_END_WRITEBACK	(1 << 2)
-#define PAGE_SET_ORDERED	(1 << 3)
-#define PAGE_SET_ERROR		(1 << 4)
-#define PAGE_LOCK		(1 << 5)
+enum {
+	ENUM_BIT(PAGE_UNLOCK),
+	/* Page starts writeback, clear dirty bit and set writeback bit */
+	ENUM_BIT(PAGE_START_WRITEBACK),
+	ENUM_BIT(PAGE_END_WRITEBACK),
+	ENUM_BIT(PAGE_SET_ORDERED),
+	ENUM_BIT(PAGE_SET_ERROR),
+	ENUM_BIT(PAGE_LOCK),
+};
 
 /*
  * page->private values.  Every page that is controlled by the extent
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/6] btrfs: convert EXTENT_* bits to enums
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
                   ` (2 preceding siblings ...)
  2022-10-11 12:58 ` [PATCH 3/6] btrfs: convert extent_io page op defines to enum bits David Sterba
@ 2022-10-11 12:58 ` David Sterba
  2022-10-11 12:58 ` [PATCH 5/6] btrfs: convert QGROUP_* defines to enum bits David Sterba
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/extent-io-tree.h | 71 +++++++++++++++++++++------------------
 1 file changed, 38 insertions(+), 33 deletions(-)

diff --git a/fs/btrfs/extent-io-tree.h b/fs/btrfs/extent-io-tree.h
index c71aa29f719d..673637ed2fa9 100644
--- a/fs/btrfs/extent-io-tree.h
+++ b/fs/btrfs/extent-io-tree.h
@@ -3,43 +3,48 @@
 #ifndef BTRFS_EXTENT_IO_TREE_H
 #define BTRFS_EXTENT_IO_TREE_H
 
+#include "misc.h"
+
 struct extent_changeset;
 struct io_failure_record;
 
 /* Bits for the extent state */
-#define EXTENT_DIRTY		(1U << 0)
-#define EXTENT_UPTODATE		(1U << 1)
-#define EXTENT_LOCKED		(1U << 2)
-#define EXTENT_NEW		(1U << 3)
-#define EXTENT_DELALLOC		(1U << 4)
-#define EXTENT_DEFRAG		(1U << 5)
-#define EXTENT_BOUNDARY		(1U << 6)
-#define EXTENT_NODATASUM	(1U << 7)
-#define EXTENT_CLEAR_META_RESV	(1U << 8)
-#define EXTENT_NEED_WAIT	(1U << 9)
-#define EXTENT_NORESERVE	(1U << 11)
-#define EXTENT_QGROUP_RESERVED	(1U << 12)
-#define EXTENT_CLEAR_DATA_RESV	(1U << 13)
-/*
- * Must be cleared only during ordered extent completion or on error paths if we
- * did not manage to submit bios and create the ordered extents for the range.
- * Should not be cleared during page release and page invalidation (if there is
- * an ordered extent in flight), that is left for the ordered extent completion.
- */
-#define EXTENT_DELALLOC_NEW	(1U << 14)
-/*
- * When an ordered extent successfully completes for a region marked as a new
- * delalloc range, use this flag when clearing a new delalloc range to indicate
- * that the VFS' inode number of bytes should be incremented and the inode's new
- * delalloc bytes decremented, in an atomic way to prevent races with stat(2).
- */
-#define EXTENT_ADD_INODE_BYTES  (1U << 15)
-
-/*
- * Set during truncate when we're clearing an entire range and we just want the
- * extent states to go away.
- */
-#define EXTENT_CLEAR_ALL_BITS	(1U << 16)
+enum {
+	ENUM_BIT(EXTENT_DIRTY),
+	ENUM_BIT(EXTENT_UPTODATE),
+	ENUM_BIT(EXTENT_LOCKED),
+	ENUM_BIT(EXTENT_NEW),
+	ENUM_BIT(EXTENT_DELALLOC),
+	ENUM_BIT(EXTENT_DEFRAG),
+	ENUM_BIT(EXTENT_BOUNDARY),
+	ENUM_BIT(EXTENT_NODATASUM),
+	ENUM_BIT(EXTENT_CLEAR_META_RESV),
+	ENUM_BIT(EXTENT_NEED_WAIT),
+	ENUM_BIT(EXTENT_NORESERVE),
+	ENUM_BIT(EXTENT_QGROUP_RESERVED),
+	ENUM_BIT(EXTENT_CLEAR_DATA_RESV),
+	/*
+	 * Must be cleared only during ordered extent completion or on error
+	 * paths if we did not manage to submit bios and create the ordered
+	 * extents for the range.  Should not be cleared during page release
+	 * and page invalidation (if there is an ordered extent in flight),
+	 * that is left for the ordered extent completion.
+	 */
+	ENUM_BIT(EXTENT_DELALLOC_NEW),
+	/*
+	 * When an ordered extent successfully completes for a region marked as
+	 * a new delalloc range, use this flag when clearing a new delalloc
+	 * range to indicate that the VFS' inode number of bytes should be
+	 * incremented and the inode's new delalloc bytes decremented, in an
+	 * atomic way to prevent races with stat(2).
+	 */
+	ENUM_BIT(EXTENT_ADD_INODE_BYTES),
+	/*
+	 * Set during truncate when we're clearing an entire range and we just
+	 * want the extent states to go away.
+	 */
+	ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
+};
 
 #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
 				 EXTENT_CLEAR_DATA_RESV)
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 5/6] btrfs: convert QGROUP_* defines to enum bits
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
                   ` (3 preceding siblings ...)
  2022-10-11 12:58 ` [PATCH 4/6] btrfs: convert EXTENT_* bits to enums David Sterba
@ 2022-10-11 12:58 ` David Sterba
  2022-10-11 12:58 ` [PATCH 6/6] btrfs: convert __TRANS_* " David Sterba
  2022-10-14 12:58 ` [PATCH 0/6] Enumerated bits in enum Josef Bacik
  6 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

The defines/enums are used only for tracepoints and are not part of the
on-disk format.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/qgroup.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/qgroup.h b/fs/btrfs/qgroup.h
index 578c77e94200..3fb5459c9309 100644
--- a/fs/btrfs/qgroup.h
+++ b/fs/btrfs/qgroup.h
@@ -11,6 +11,7 @@
 #include <linux/kobject.h>
 #include "ulist.h"
 #include "delayed-ref.h"
+#include "misc.h"
 
 /*
  * Btrfs qgroup overview
@@ -242,9 +243,11 @@ static inline u64 btrfs_qgroup_subvolid(u64 qgroupid)
 /*
  * For qgroup event trace points only
  */
-#define QGROUP_RESERVE		(1<<0)
-#define QGROUP_RELEASE		(1<<1)
-#define QGROUP_FREE		(1<<2)
+enum {
+	ENUM_BIT(QGROUP_RESERVE),
+	ENUM_BIT(QGROUP_RELEASE),
+	ENUM_BIT(QGROUP_FREE),
+};
 
 int btrfs_quota_enable(struct btrfs_fs_info *fs_info);
 int btrfs_quota_disable(struct btrfs_fs_info *fs_info);
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 6/6] btrfs: convert __TRANS_* defines to enum bits
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
                   ` (4 preceding siblings ...)
  2022-10-11 12:58 ` [PATCH 5/6] btrfs: convert QGROUP_* defines to enum bits David Sterba
@ 2022-10-11 12:58 ` David Sterba
  2022-10-14 12:58 ` [PATCH 0/6] Enumerated bits in enum Josef Bacik
  6 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-10-11 12:58 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

The base transaction bits can be defined as bits in a contiguous
sequence, although right now there's a hole from bit 1 to 8.

The bits are used for btrfs_trans_handle::type, and there's another set
of TRANS_STATE_* defines that are for btrfs_transaction::state. They are
mutually exclusive though the hole in the sequence looks like was made
for the states.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/transaction.h | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 1332f193b800..b39ebf98af60 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -10,6 +10,7 @@
 #include "btrfs_inode.h"
 #include "delayed-ref.h"
 #include "ctree.h"
+#include "misc.h"
 
 enum btrfs_trans_state {
 	TRANS_STATE_RUNNING,
@@ -98,14 +99,15 @@ struct btrfs_transaction {
 	struct list_head releasing_ebs;
 };
 
-#define __TRANS_FREEZABLE	(1U << 0)
-
-#define __TRANS_START		(1U << 9)
-#define __TRANS_ATTACH		(1U << 10)
-#define __TRANS_JOIN		(1U << 11)
-#define __TRANS_JOIN_NOLOCK	(1U << 12)
-#define __TRANS_DUMMY		(1U << 13)
-#define __TRANS_JOIN_NOSTART	(1U << 14)
+enum {
+	ENUM_BIT(__TRANS_FREEZABLE),
+	ENUM_BIT(__TRANS_START),
+	ENUM_BIT(__TRANS_ATTACH),
+	ENUM_BIT(__TRANS_JOIN),
+	ENUM_BIT(__TRANS_JOIN_NOLOCK),
+	ENUM_BIT(__TRANS_DUMMY),
+	ENUM_BIT(__TRANS_JOIN_NOSTART),
+};
 
 #define TRANS_START		(__TRANS_START | __TRANS_FREEZABLE)
 #define TRANS_ATTACH		(__TRANS_ATTACH)
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/6] Enumerated bits in enum
  2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
                   ` (5 preceding siblings ...)
  2022-10-11 12:58 ` [PATCH 6/6] btrfs: convert __TRANS_* " David Sterba
@ 2022-10-14 12:58 ` Josef Bacik
  6 siblings, 0 replies; 8+ messages in thread
From: Josef Bacik @ 2022-10-14 12:58 UTC (permalink / raw)
  To: David Sterba; +Cc: linux-btrfs

On Tue, Oct 11, 2022 at 02:58:45PM +0200, David Sterba wrote:
> With some macro magic it's possible to let enum autoincrement define all
> bits without manually specifying the value. How it's done is described
> in the first patch, the rest are example conversions.
> 
> David Sterba (6):
>   btrfs: add helper for bit enumeration
>   btrfs: convert BTRFS_ILOCK-* defines to enum bit
>   btrfs: convert extent_io page op defines to enum bits
>   btrfs: convert EXTENT_* bits to enums
>   btrfs: convert QGROUP_* defines to enum bits
>   btrfs: convert __TRANS_* defines to enum bits
> 
>  fs/btrfs/ctree.h          |  9 +++--
>  fs/btrfs/extent-io-tree.h | 71 +++++++++++++++++++++------------------
>  fs/btrfs/extent_io.h      | 17 ++++++----
>  fs/btrfs/misc.h           |  8 +++++
>  fs/btrfs/qgroup.h         |  9 +++--
>  fs/btrfs/transaction.h    | 18 +++++-----
>  6 files changed, 78 insertions(+), 54 deletions(-)
> 
>

I love it,

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef 

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-10-14 12:58 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-11 12:58 [PATCH 0/6] Enumerated bits in enum David Sterba
2022-10-11 12:58 ` [PATCH 1/6] btrfs: add helper for bit enumeration David Sterba
2022-10-11 12:58 ` [PATCH 2/6] btrfs: convert BTRFS_ILOCK-* defines to enum bit David Sterba
2022-10-11 12:58 ` [PATCH 3/6] btrfs: convert extent_io page op defines to enum bits David Sterba
2022-10-11 12:58 ` [PATCH 4/6] btrfs: convert EXTENT_* bits to enums David Sterba
2022-10-11 12:58 ` [PATCH 5/6] btrfs: convert QGROUP_* defines to enum bits David Sterba
2022-10-11 12:58 ` [PATCH 6/6] btrfs: convert __TRANS_* " David Sterba
2022-10-14 12:58 ` [PATCH 0/6] Enumerated bits in enum Josef Bacik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).