btrfs: use unsigned types for constants defined as bit shifts

The unsigned type is a recommended practice (CWE-190, CWE-194) for bit
shifts to avoid problems with potential unwanted sign extensions.
Although there are no such cases in btrfs codebase, follow the
recommendation.

Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2025-04-22 17:55:41 +02:00
parent d4d788a776
commit 05a6ec865d
8 changed files with 19 additions and 20 deletions

View file

@ -423,8 +423,8 @@ struct btrfs_backref_node *btrfs_backref_alloc_node(
struct btrfs_backref_edge *btrfs_backref_alloc_edge( struct btrfs_backref_edge *btrfs_backref_alloc_edge(
struct btrfs_backref_cache *cache); struct btrfs_backref_cache *cache);
#define LINK_LOWER (1 << 0) #define LINK_LOWER (1U << 0)
#define LINK_UPPER (1 << 1) #define LINK_UPPER (1U << 1)
void btrfs_backref_link_edge(struct btrfs_backref_edge *edge, void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
struct btrfs_backref_node *lower, struct btrfs_backref_node *lower,

View file

@ -151,8 +151,8 @@ static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
} }
ordered = btrfs_alloc_ordered_extent(inode, start, file_extent, ordered = btrfs_alloc_ordered_extent(inode, start, file_extent,
(1 << type) | (1U << type) |
(1 << BTRFS_ORDERED_DIRECT)); (1U << BTRFS_ORDERED_DIRECT));
if (IS_ERR(ordered)) { if (IS_ERR(ordered)) {
if (em) { if (em) {
btrfs_free_extent_map(em); btrfs_free_extent_map(em);

View file

@ -73,7 +73,7 @@ enum {
* single word in a bitmap may straddle two pages in the extent buffer. * single word in a bitmap may straddle two pages in the extent buffer.
*/ */
#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE) #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
#define BYTE_MASK ((1 << BITS_PER_BYTE) - 1) #define BYTE_MASK ((1U << BITS_PER_BYTE) - 1)
#define BITMAP_FIRST_BYTE_MASK(start) \ #define BITMAP_FIRST_BYTE_MASK(start) \
((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK) ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
#define BITMAP_LAST_BYTE_MASK(nbits) \ #define BITMAP_LAST_BYTE_MASK(nbits) \

View file

@ -1154,7 +1154,7 @@ static void submit_one_async_extent(struct async_chunk *async_chunk,
btrfs_free_extent_map(em); btrfs_free_extent_map(em);
ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent, ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
1 << BTRFS_ORDERED_COMPRESSED); 1U << BTRFS_ORDERED_COMPRESSED);
if (IS_ERR(ordered)) { if (IS_ERR(ordered)) {
btrfs_drop_extent_map_range(inode, start, end, false); btrfs_drop_extent_map_range(inode, start, end, false);
ret = PTR_ERR(ordered); ret = PTR_ERR(ordered);
@ -1401,7 +1401,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
btrfs_free_extent_map(em); btrfs_free_extent_map(em);
ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent, ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
1 << BTRFS_ORDERED_REGULAR); 1U << BTRFS_ORDERED_REGULAR);
if (IS_ERR(ordered)) { if (IS_ERR(ordered)) {
btrfs_unlock_extent(&inode->io_tree, start, btrfs_unlock_extent(&inode->io_tree, start,
start + cur_alloc_size - 1, &cached); start + cur_alloc_size - 1, &cached);
@ -1981,8 +1981,8 @@ static int nocow_one_range(struct btrfs_inode *inode, struct folio *locked_folio
ordered = btrfs_alloc_ordered_extent(inode, file_pos, &nocow_args->file_extent, ordered = btrfs_alloc_ordered_extent(inode, file_pos, &nocow_args->file_extent,
is_prealloc is_prealloc
? (1 << BTRFS_ORDERED_PREALLOC) ? (1U << BTRFS_ORDERED_PREALLOC)
: (1 << BTRFS_ORDERED_NOCOW)); : (1U << BTRFS_ORDERED_NOCOW));
if (IS_ERR(ordered)) { if (IS_ERR(ordered)) {
if (is_prealloc) if (is_prealloc)
btrfs_drop_extent_map_range(inode, file_pos, end, false); btrfs_drop_extent_map_range(inode, file_pos, end, false);
@ -9690,8 +9690,8 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
btrfs_free_extent_map(em); btrfs_free_extent_map(em);
ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent, ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
(1 << BTRFS_ORDERED_ENCODED) | (1U << BTRFS_ORDERED_ENCODED) |
(1 << BTRFS_ORDERED_COMPRESSED)); (1U << BTRFS_ORDERED_COMPRESSED));
if (IS_ERR(ordered)) { if (IS_ERR(ordered)) {
btrfs_drop_extent_map_range(inode, start, end, false); btrfs_drop_extent_map_range(inode, start, end, false);
ret = PTR_ERR(ordered); ret = PTR_ERR(ordered);

View file

@ -155,7 +155,7 @@ static struct btrfs_ordered_extent *alloc_ordered_extent(
u64 qgroup_rsv = 0; u64 qgroup_rsv = 0;
if (flags & if (flags &
((1 << BTRFS_ORDERED_NOCOW) | (1 << BTRFS_ORDERED_PREALLOC))) { ((1U << BTRFS_ORDERED_NOCOW) | (1U << BTRFS_ORDERED_PREALLOC))) {
/* For nocow write, we can release the qgroup rsv right now */ /* For nocow write, we can release the qgroup rsv right now */
ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes, &qgroup_rsv); ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes, &qgroup_rsv);
if (ret < 0) if (ret < 0)
@ -253,7 +253,7 @@ static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
* @disk_bytenr: Offset of extent on disk. * @disk_bytenr: Offset of extent on disk.
* @disk_num_bytes: Size of extent on disk. * @disk_num_bytes: Size of extent on disk.
* @offset: Offset into unencoded data where file data starts. * @offset: Offset into unencoded data where file data starts.
* @flags: Flags specifying type of extent (1 << BTRFS_ORDERED_*). * @flags: Flags specifying type of extent (1U << BTRFS_ORDERED_*).
* @compress_type: Compression algorithm used for data. * @compress_type: Compression algorithm used for data.
* *
* Most of these parameters correspond to &struct btrfs_file_extent_item. The * Most of these parameters correspond to &struct btrfs_file_extent_item. The

View file

@ -203,8 +203,7 @@ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
struct btrfs_stripe_hash_table *x; struct btrfs_stripe_hash_table *x;
struct btrfs_stripe_hash *cur; struct btrfs_stripe_hash *cur;
struct btrfs_stripe_hash *h; struct btrfs_stripe_hash *h;
int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS; unsigned int num_entries = 1U << BTRFS_STRIPE_HASH_TABLE_BITS;
int i;
if (info->stripe_hash_table) if (info->stripe_hash_table)
return 0; return 0;
@ -225,7 +224,7 @@ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
h = table->table; h = table->table;
for (i = 0; i < num_entries; i++) { for (unsigned int i = 0; i < num_entries; i++) {
cur = h + i; cur = h + i;
INIT_LIST_HEAD(&cur->hash_list); INIT_LIST_HEAD(&cur->hash_list);
spin_lock_init(&cur->lock); spin_lock_init(&cur->lock);

View file

@ -14,9 +14,9 @@
#include "../disk-io.h" #include "../disk-io.h"
#include "../btrfs_inode.h" #include "../btrfs_inode.h"
#define PROCESS_UNLOCK (1 << 0) #define PROCESS_UNLOCK (1U << 0)
#define PROCESS_RELEASE (1 << 1) #define PROCESS_RELEASE (1U << 1)
#define PROCESS_TEST_LOCKED (1 << 2) #define PROCESS_TEST_LOCKED (1U << 2)
static noinline int process_page_range(struct inode *inode, u64 start, u64 end, static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
unsigned long flags) unsigned long flags)

View file

@ -24,7 +24,7 @@
#include "super.h" #include "super.h"
#define ZSTD_BTRFS_MAX_WINDOWLOG 17 #define ZSTD_BTRFS_MAX_WINDOWLOG 17
#define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG) #define ZSTD_BTRFS_MAX_INPUT (1U << ZSTD_BTRFS_MAX_WINDOWLOG)
#define ZSTD_BTRFS_DEFAULT_LEVEL 3 #define ZSTD_BTRFS_DEFAULT_LEVEL 3
#define ZSTD_BTRFS_MIN_LEVEL -15 #define ZSTD_BTRFS_MIN_LEVEL -15
#define ZSTD_BTRFS_MAX_LEVEL 15 #define ZSTD_BTRFS_MAX_LEVEL 15