btrfs: pass checksum type via BTRFS_IOC_FS_INFO ioctl
authorJohannes Thumshirn <johannes.thumshirn@wdc.com>
Mon, 13 Jul 2020 12:28:58 +0000 (21:28 +0900)
committerDavid Sterba <dsterba@suse.com>
Mon, 27 Jul 2020 10:55:43 +0000 (12:55 +0200)
commit137c541821a83debb63b3fa8abdd1cbc41bdf3a1
tree8fd2102531589a900315edbc3516ab90806446a5
parentadca4d945c8dca28a85df45c5b117e6dac2e77f1
btrfs: pass checksum type via BTRFS_IOC_FS_INFO ioctl

With the recent addition of filesystem checksum types other than CRC32c,
it is not anymore hard-coded which checksum type a btrfs filesystem uses.

Up to now there is no good way to read the filesystem checksum, apart from
reading the filesystem UUID and then query sysfs for the checksum type.

Add a new csum_type and csum_size fields to the BTRFS_IOC_FS_INFO ioctl
command which usually is used to query filesystem features. Also add a
flags member indicating that the kernel responded with a set csum_type and
csum_size field.

For compatibility reasons, only return the csum_type and csum_size if
the BTRFS_FS_INFO_FLAG_CSUM_INFO flag was passed to the kernel. Also
clear any unknown flags so we don't pass false positives to user-space
newer than the kernel.

To simplify further additions to the ioctl, also switch the padding to a
u8 array. Pahole was used to verify the result of this switch:

The csum members are added before flags, which might look odd, but this
is to keep the alignment requirements and not to introduce holes in the
structure.

  $ pahole -C btrfs_ioctl_fs_info_args fs/btrfs/btrfs.ko
  struct btrfs_ioctl_fs_info_args {
  __u64                      max_id;               /*     0     8 */
  __u64                      num_devices;          /*     8     8 */
  __u8                       fsid[16];             /*    16    16 */
  __u32                      nodesize;             /*    32     4 */
  __u32                      sectorsize;           /*    36     4 */
  __u32                      clone_alignment;      /*    40     4 */
  __u16                      csum_type;            /*    44     2 */
  __u16                      csum_size;            /*    46     2 */
  __u64                      flags;                /*    48     8 */
  __u8                       reserved[968];        /*    56   968 */

  /* size: 1024, cachelines: 16, members: 10 */
  };

Fixes: 3951e7f050ac ("btrfs: add xxhash64 to checksumming algorithms")
Fixes: 3831bf0094ab ("btrfs: add sha256 to checksumming algorithm")
CC: stable@vger.kernel.org # 5.5+
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c
include/uapi/linux/btrfs.h