History

btrfs: add definitions and documentation for encoded I/O ioctls

In order to allow sending and receiving compressed data without decompressing it, we need an interface to write pre-compressed data directly to the filesystem and the matching interface to read compressed data without decompressing it. This adds the definitions for ioctls to do that and detailed explanations of how to use them.

Omar Sandoval committed 5 years ago

Commit dcb77a91 parent d9496e81 file · +132 −0Browse files

Unified · Split

include/uapi/linux/btrfs.h+132−0
@@ -869,6 +869,134 @@ struct btrfs_ioctl_get_subvol_rootref_args { · 868 unmodified lines
869 __u8 align[7];869 __u8 align[7];
870};870};
871 871
872/*
873 * Data and metadata for an encoded read or write.
874 *
875 * Encoded I/O bypasses any encoding automatically done by the filesystem (e.g.,
876 * compression). This can be used to read the compressed contents of a file or
877 * write pre-compressed data directly to a file.
878 *
879 * BTRFS_IOC_ENCODED_READ and BTRFS_IOC_ENCODED_WRITE are essentially
880 * preadv/pwritev with additional metadata about how the data is encoded and the
881 * size of the unencoded data.
882 *
883 * BTRFS_IOC_ENCODED_READ fills the given iovecs with the encoded data, fills
884 * the metadata fields, and returns the size of the encoded data. It reads one
885 * extent per call. It can also read data which is not encoded.
886 *
887 * BTRFS_IOC_ENCODED_WRITE uses the metadata fields, writes the encoded data
888 * from the iovecs, and returns the size of the encoded data. Note that the
889 * encoded data is not validated when it is written; if it is not valid (e.g.,
890 * it cannot be decompressed), then a subsequent read may return an error.
891 *
892 * Since the filesystem page cache contains decoded data, encoded I/O bypasses
893 * the page cache. Encoded I/O requires CAP_SYS_ADMIN.
894 */
895struct btrfs_ioctl_encoded_io_args {
896 /* Input parameters for both reads and writes. */
897
898 /*
899 * iovecs containing encoded data.
900 *
901 * For reads, if the size of the encoded data is larger than the sum of
902 * iov[n].iov_len for 0 <= n < iovcnt, then the ioctl fails with
903 * ENOBUFS.
904 *
905 * For writes, the size of the encoded data is the sum of iov[n].iov_len
906 * for 0 <= n < iovcnt. This must be less than 128 KiB (this limit may
907 * increase in the future). This must also be less than or equal to
908 * unencoded_len.
909 */
910 const struct iovec __user *iov;
911 /* Number of iovecs. */
912 unsigned long iovcnt;
913 /*
914 * Offset in file.
915 *
916 * For writes, must be aligned to the sector size of the filesystem.
917 */
918 __s64 offset;
919 /* Currently must be zero. */
920 __u64 flags;
921
922 /*
923 * For reads, the following members are output parameters that will
924 * contain the returned metadata for the encoded data.
925 * For writes, the following members must be set to the metadata for the
926 * encoded data.
927 */
928
929 /*
930 * Length of the data in the file.
931 *
932 * Must be less than or equal to unencoded_len - unencoded_offset. For
933 * writes, must be aligned to the sector size of the filesystem unless
934 * the data ends at or beyond the current end of the file.
935 */
936 __u64 len;
937 /*
938 * Length of the unencoded (i.e., decrypted and decompressed) data.
939 *
940 * For writes, must be no more than 128 KiB (this limit may increase in
941 * the future). If the unencoded data is actually longer than
942 * unencoded_len, then it is truncated; if it is shorter, then it is
943 * extended with zeroes.
944 */
945 __u64 unencoded_len;
946 /*
947 * Offset from the first byte of the unencoded data to the first byte of
948 * logical data in the file.
949 *
950 * Must be less than unencoded_len.
951 */
952 __u64 unencoded_offset;
953 /*
954 * BTRFS_ENCODED_IO_COMPRESSION_* type.
955 *
956 * For writes, must not be BTRFS_ENCODED_IO_COMPRESSION_NONE.
957 */
958 __u32 compression;
959 /* Currently always BTRFS_ENCODED_IO_ENCRYPTION_NONE. */
960 __u32 encryption;
961 /*
962 * Reserved for future expansion.
963 *
964 * For reads, always returned as zero. Users should check for non-zero
965 * bytes. If there are any, then the kernel has a newer version of this
966 * structure with additional information that the user definition is
967 * missing.
968 *
969 * For writes, must be zeroed.
970 */
971 __u8 reserved[64];
972};
973
974/* Data is not compressed. */
975#define BTRFS_ENCODED_IO_COMPRESSION_NONE 0
976/* Data is compressed as a single zlib stream. */
977#define BTRFS_ENCODED_IO_COMPRESSION_ZLIB 1
978/*
979 * Data is compressed as a single zstd frame with the windowLog compression
980 * parameter set to no more than 17.
981 */
982#define BTRFS_ENCODED_IO_COMPRESSION_ZSTD 2
983/*
984 * Data is compressed sector by sector (using the sector size indicated by the
985 * name of the constant) with LZO1X and wrapped in the format documented in
986 * fs/btrfs/lzo.c. For writes, the compression sector size must match the
987 * filesystem sector size.
988 */
989#define BTRFS_ENCODED_IO_COMPRESSION_LZO_4K 3
990#define BTRFS_ENCODED_IO_COMPRESSION_LZO_8K 4
991#define BTRFS_ENCODED_IO_COMPRESSION_LZO_16K 5
992#define BTRFS_ENCODED_IO_COMPRESSION_LZO_32K 6
993#define BTRFS_ENCODED_IO_COMPRESSION_LZO_64K 7
994#define BTRFS_ENCODED_IO_COMPRESSION_TYPES 8
995
996/* Data is not encrypted. */
997#define BTRFS_ENCODED_IO_ENCRYPTION_NONE 0
998#define BTRFS_ENCODED_IO_ENCRYPTION_TYPES 1
999
872/* Error codes as returned by the kernel */1000/* Error codes as returned by the kernel */
873enum btrfs_err_code {1001enum btrfs_err_code {
874 BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1,1002 BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1,
@@ -997,5 +1125,9 @@ enum btrfs_err_code { · 122 unmodified lines
997 struct btrfs_ioctl_ino_lookup_user_args)1125 struct btrfs_ioctl_ino_lookup_user_args)
998#define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \1126#define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \
999 struct btrfs_ioctl_vol_args_v2)1127 struct btrfs_ioctl_vol_args_v2)
1128#define BTRFS_IOC_ENCODED_READ _IOR(BTRFS_IOCTL_MAGIC, 64, \
1129 struct btrfs_ioctl_encoded_io_args)
1130#define BTRFS_IOC_ENCODED_WRITE _IOW(BTRFS_IOCTL_MAGIC, 64, \
1131 struct btrfs_ioctl_encoded_io_args)
1000 1132
1001#endif /* _UAPI_LINUX_BTRFS_H */1133#endif /* _UAPI_LINUX_BTRFS_H */