Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btrfs: rework BTRFS_I as macro to preserve parameter const

Currently BTRFS_I is a static inline function that takes a const inode
and returns btrfs inode, dropping the 'const' qualifier. This can break
assumptions of compiler though it seems there's no real case.

To make the parameter and return type consistent regardint const we can
use the container_of_const() that preserves it. However this would not
check the parameter type. To fix that use the same _Generic construct
but implement only the two expected types.

Signed-off-by: David Sterba <dsterba@suse.com>

+6 -4
+6 -4
fs/btrfs/btrfs_inode.h
··· 350 350 WRITE_ONCE(inode->first_dir_index_to_log, index); 351 351 } 352 352 353 - static inline struct btrfs_inode *BTRFS_I(const struct inode *inode) 354 - { 355 - return container_of(inode, struct btrfs_inode, vfs_inode); 356 - } 353 + /* Type checked and const-preserving VFS inode -> btrfs inode. */ 354 + #define BTRFS_I(_inode) \ 355 + _Generic(_inode, \ 356 + struct inode *: container_of(_inode, struct btrfs_inode, vfs_inode), \ 357 + const struct inode *: (const struct btrfs_inode *)container_of( \ 358 + _inode, const struct btrfs_inode, vfs_inode)) 357 359 358 360 static inline unsigned long btrfs_inode_hash(u64 objectid, 359 361 const struct btrfs_root *root)