fs: interface for directly reading/writing compressed data
Subsystems (14)
v5 cover fs: interface for directly reading/writing compressed data
This series adds an API for reading compressed data on a filesystem without decompressing it as well as support for writing compressed data directly to the filesystem. As with the previous submissions, I've included a man page patch describing the API. I have test cases (including fsstress support) and example programs which I'll send up soon [1].
The main use-case is Btrfs send/receive: currently, when sending data from one compressed filesystem to another, the sending side decompresses the data and the receiving side recompresses it before writing it out. This is wasteful and can be avoided if we can just send and write compressed extents. Since the last posting of this series, I've implemented the send support and Boris Burkov has implemented the receive support using this interface; the speedup is significant. Those patches will be sent shortly.
Patches 1-3 add the VFS support and UAPI. Patches 4-7 are Btrfs prep patches. Patch 8 adds Btrfs encoded read support and patch 9 adds Btrfs encoded write support.
These patches are based on Dave Sterba's Btrfs misc-next branch [2], which is in turn based on v5.9-rc1.
Changes since v4 [3]:
- Rebased on kdave/misc-next.
- Clarified the man pages in several places.
1: https://github.com/osandov/xfstests/tree/rwf-encoded
2: https://github.com/kdave/btrfs-devel/tree/misc-next
3: https://lore.kernel.org/linux-fsdevel/[email protected]/
Omar Sandoval (9):
iov_iter: add copy_struct_from_iter()
fs: add O_ALLOW_ENCODED open flag
fs: add RWF_ENCODED for reading/writing compressed data
btrfs: don't advance offset for compressed bios in
btrfs_csum_one_bio()
btrfs: add ram_bytes and offset to btrfs_ordered_extent
btrfs: support different disk extent size for delalloc
btrfs: optionally extend i_size in cow_file_range_inline()
btrfs: implement RWF_ENCODED reads
btrfs: implement RWF_ENCODED writes
Documentation/filesystems/encoded_io.rst | 74 ++ Documentation/filesystems/index.rst | 1 + arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/btrfs/compression.c | 12 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 9 +- fs/btrfs/delalloc-space.c | 18 +- fs/btrfs/file-item.c | 35 +- fs/btrfs/file.c | 55 +- fs/btrfs/inode.c | 904 ++++++++++++++++++++--- fs/btrfs/ordered-data.c | 80 +- fs/btrfs/ordered-data.h | 18 +- fs/btrfs/relocation.c | 4 +- fs/fcntl.c | 10 +- fs/namei.c | 4 + include/linux/fcntl.h | 2 +- include/linux/fs.h | 16 + include/linux/uio.h | 2 + include/uapi/asm-generic/fcntl.h | 4 + include/uapi/linux/fs.h | 33 +- lib/iov_iter.c | 82 ++ mm/filemap.c | 166 ++++- 24 files changed, 1336 insertions(+), 202 deletions(-) create mode 100644 Documentation/filesystems/encoded_io.rst
Signature · 1 line
2.28.0
PATCH v5 1/1 Document encoded I/O
From: Omar Sandoval <[email protected]>
This adds a new page, encoded_io(7), providing an overview of encoded I/O and updates fcntl(2), open(2), and preadv2(2)/pwritev2(2) to reference it.
---
This feature is not yet upstream.
man2/fcntl.2 | 10 +- man2/open.2 | 13 ++ man2/readv.2 | 64 +++++++++ man7/encoded_io.7 | 347 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 man7/encoded_io.7
man2/fcntl.2 +9 -1
@@ -221,8 +221,9 @@ On Linux, this command can change only the .BR O_ASYNC , .BR O_DIRECT , .BR O_NOATIME , +.BR O_NONBLOCK , and -.B O_NONBLOCK +.B O_ALLOW_ENCODED flags. It is not possible to change the .BR O_DSYNC
@@ -1820,6 +1821,13 @@ Attempted to clear the flag on a file that has the append-only attribute set. .TP .B EPERM +Attempted to set the +.B O_ALLOW_ENCODED +flag and the calling process did not have the +.B CAP_SYS_ADMIN +capability. +.TP +.B EPERM .I cmd was .BR F_ADD_SEALS ,
man2/open.2 +13 -0
@@ -437,6 +437,14 @@ was followed by a call to .BR fdatasync (2)). .IR "See NOTES below" . .TP +.B O_ALLOW_ENCODED +Open the file with encoded I/O permissions; +see +.BR encoded_io (7). +The caller must have the +.B CAP_SYS_ADMIN +capability. +.TP .B O_EXCL Ensure that this call creates the file: if this flag is specified in conjunction with
@@ -1230,6 +1238,11 @@ did not match the owner of the file and the caller was not privileged. The operation was prevented by a file seal; see .BR fcntl (2). .TP +.B EPERM +The +.B O_ALLOW_ENCODED +flag was specified, but the caller was not privileged. +.TP .B EROFS .I pathname refers to a file on a read-only filesystem and write access was
man2/readv.2 +64 -0
82 lines not on this page · raw message
man7/encoded_io.7 new file +347 -0
347 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH man-pages v5] Document encoded I/O
Quoted 11 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
This adds a new page,
encoded_io(7), providing an overview of encoded I/O and updates fcntl(2), open(2), and preadv2(2)/pwritev2(2) to reference it.Cc Michael Kerrisk <[email protected]>Cc linux-man <[email protected]>Signed-off-by Omar Sandoval <[email protected]>---
Omar,
Thanks for making the clarifications. Some questions below.
[...]
Quoted 66 lines
+.PP
man7/encoded_io.7 new file +65
-0+As the filesystem page cache typically contains decoded data, +encoded I/O bypasses the page cache. +.SS Extent layout +By using +.IR len , +.IR unencoded_len , +and +.IR unencoded_offset , +it is possible to refer to a subset of an unencoded extent. +.PP +In the simplest case, +.I len +is equal to +.I unencoded_len +and +.I unencoded_offset +is zero. +This means that the entire unencoded extent is used. +.PP +However, suppose we read 50 bytes into a file +which contains a single compressed extent. +The filesystem must still return the entire compressed extent +for us to be able to decompress it, +so +.I unencoded_len +would be the length of the entire decompressed extent. +However, because the read was at offset 50, +the first 50 bytes should be ignored. +Therefore, +.I unencoded_offset +would be 50, +and +.I len +would accordingly be +.IR unencoded_len\ -\ 50 . +.PP +Additionally, suppose we want to create an encrypted file with length 500, +but the file is encrypted with a block cipher using a block size of 4096. +The unencoded data would therefore include the appropriate padding, +and +.I unencoded_len +would be 4096. +However, to represent the logical size of the file, +.I len +would be 500 +(and +.I unencoded_offset +would be 0). +.PP +Similar situations can arise in other cases: +.IP * 3 +If the filesystem pads data to the filesystem block size before compressing, +then compressed files with a size unaligned to the filesystem block size will +end with an extent with +.I len +< +.IR unencoded_len . +.IP * +Extents cloned from the middle of a larger encoded extent with +.B FICLONERANGE +may have a non-zero +.I unencoded_offset +and/or +.I len +<
man7/encoded_io.7 line 259
Quoted 10 lines
man7/encoded_io.7 new file +10
-0+.IR unencoded_len . +.IP * +If the middle of an encoded extent is overwritten, +the filesystem may create extents with a non-zero +.I unencoded_offset +and/or +.I len +< +.I unencoded_len +for the parts that were not overwritten.
So in this case, would the reader be getting extents "out of unencoded order"? e.g. unencoded range 0..4096 and then unencoded range 10..20?
Or would reader be reading the encoded full block twice, once for ragne 0..10 and once for range 20..4096?
Quoted 48 lines
+.SS Security
man7/encoded_io.7 new file +47
-0+Encoded I/O creates the potential for some security issues: +.IP * 3 +Encoded writes allow writing arbitrary data which the kernel will decode on +a subsequent read. Decompression algorithms are complex and may have bugs +which can be exploited by maliciously crafted data. +.IP * +Encoded reads may return data which is not logically present in the file +(see the discussion of +.I len +vs. +.I unencoded_len +above). +It may not be intended for this data to be readable. +.PP +Therefore, encoded I/O requires privilege. +Namely, the +.B RWF_ENCODED +flag may only be used when the file was opened with the +.B O_ALLOW_ENCODED +flag to +.BR open (2), +which requires the +.B CAP_SYS_ADMIN +capability. +.B O_ALLOW_ENCODED +may be set and cleared with +.BR fcntl (2). +Note that it is not cleared on +.BR fork (2) +or +.BR execve (2); +one may wish to use +.B O_CLOEXEC +with +.BR O_ALLOW_ENCODED . +.SS Filesystem support +Encoded I/O is supported on the following filesystems: +.TP +Btrfs (since Linux 5.10) +.IP +Btrfs supports encoded reads and writes of compressed data. +The data is encoded as follows: +.RS +.IP * 3 +If +.I compression +is
man7/encoded_io.7 line 317
Quoted 10 lines
man7/encoded_io.7 new file +10
-0+.BR ENCODED_IOV_COMPRESSION_ZLIB , +then the encoded data is a single zlib stream. +.IP * +If +.I compression +is +.BR ENCODED_IOV_COMPRESSION_LZO , +then the encoded data is compressed page by page with LZO1X +and wrapped in the format documented in the Linux kernel source file +.IR fs/btrfs/lzo.c .
:-/ So maybe call it ENCODED_IOV_COMPRESSION_BTRFS_LZO?
I understand why you want the encoding format not to be opaque, but I imagine the encoded data is not going to be migrated as is between different filesystems. So just call it for what it is - a private filesystem encoding
format. If you have a format that is standard and other filesystems are likely to use, fine, but let's not make an API that discourages using "private" encoding, just for the sake of it and make life harder for no good reason.
All the reader of this man page may be interested to know is which filesystems are expected to support which encoding types and a general description of what they mean (as you did).
Making this page wrongly appear as a standard for encoding formats is not going to play out well...
man7/encoded_io.7 line 325
Quoted 8 lines
+.IP *
man7/encoded_io.7 new file +7
-0+If +.I compression +is +.BR ENCODED_IOV_COMPRESSION_ZSTD , +then the encoded data is a single zstd frame compressed with the +.I windowLog +compression parameter set to no more than 17.
Even that small detail is a bit limiting to filesystems and should therefore be tagged as a private btrfs encoding IMO.
Thanks,
Amir.
Re: [PATCH man-pages v5] Document encoded I/O
Quoted 92 lines · Amir Goldstein
Quoted 11 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
This adds a new page,
encoded_io(7), providing an overview of encoded I/O and updates fcntl(2), open(2), and preadv2(2)/pwritev2(2) to reference it.Cc Michael Kerrisk <[email protected]>Cc linux-man <[email protected]>Signed-off-by Omar Sandoval <[email protected]>---
Omar,
Thanks for making the clarifications. Some questions below.
[...]
Quoted 73 lines
+.PP
man7/encoded_io.7 new file +72
-0+As the filesystem page cache typically contains decoded data, +encoded I/O bypasses the page cache. +.SS Extent layout +By using +.IR len , +.IR unencoded_len , +and +.IR unencoded_offset , +it is possible to refer to a subset of an unencoded extent. +.PP +In the simplest case, +.I len +is equal to +.I unencoded_len +and +.I unencoded_offset +is zero. +This means that the entire unencoded extent is used. +.PP +However, suppose we read 50 bytes into a file +which contains a single compressed extent. +The filesystem must still return the entire compressed extent +for us to be able to decompress it, +so +.I unencoded_len +would be the length of the entire decompressed extent. +However, because the read was at offset 50, +the first 50 bytes should be ignored. +Therefore, +.I unencoded_offset +would be 50, +and +.I len +would accordingly be +.IR unencoded_len\ -\ 50 . +.PP +Additionally, suppose we want to create an encrypted file with length 500, +but the file is encrypted with a block cipher using a block size of 4096. +The unencoded data would therefore include the appropriate padding, +and +.I unencoded_len +would be 4096. +However, to represent the logical size of the file, +.I len +would be 500 +(and +.I unencoded_offset +would be 0). +.PP +Similar situations can arise in other cases: +.IP * 3 +If the filesystem pads data to the filesystem block size before compressing, +then compressed files with a size unaligned to the filesystem block size will +end with an extent with +.I len +< +.IR unencoded_len . +.IP * +Extents cloned from the middle of a larger encoded extent with +.B FICLONERANGE +may have a non-zero +.I unencoded_offset +and/or +.I len +< +.IR unencoded_len . +.IP * +If the middle of an encoded extent is overwritten, +the filesystem may create extents with a non-zero +.I unencoded_offset +and/or +.I len
Quoted 8 lines
Quoted 3 lines
+<
+.Iunencoded_len
+for the parts that were not overwritten.So in this case, would the reader be getting extents "out of unencoded order"? e.g. unencoded range 0..4096 and then unencoded range 10..20?
Or would reader be reading the encoded full block twice, once for ragne 0..10 and once for range 20..4096?
The latter. If the file refers to the same encoded data twice, reading the file sequentially with RWF_ENCODED will return it twice (with different offsets each time). This is obviously not perfect, but it keeps the interface simpler: the abstraction is not "what exactly is the extent layout of the file" but rather "I want to read this logical range of data", even if that involves pulling in some details from the extent metadata.
Quoted 76 lines
Quoted 48 lines
+.SS Security
man7/encoded_io.7 new file +47
-0+Encoded I/O creates the potential for some security issues: +.IP * 3 +Encoded writes allow writing arbitrary data which the kernel will decode on +a subsequent read. Decompression algorithms are complex and may have bugs +which can be exploited by maliciously crafted data. +.IP * +Encoded reads may return data which is not logically present in the file +(see the discussion of +.I len +vs. +.I unencoded_len +above). +It may not be intended for this data to be readable. +.PP +Therefore, encoded I/O requires privilege. +Namely, the +.B RWF_ENCODED +flag may only be used when the file was opened with the +.B O_ALLOW_ENCODED +flag to +.BR open (2), +which requires the +.B CAP_SYS_ADMIN +capability. +.B O_ALLOW_ENCODED +may be set and cleared with +.BR fcntl (2). +Note that it is not cleared on +.BR fork (2) +or +.BR execve (2); +one may wish to use +.B O_CLOEXEC +with +.BR O_ALLOW_ENCODED . +.SS Filesystem support +Encoded I/O is supported on the following filesystems: +.TP +Btrfs (since Linux 5.10) +.IP +Btrfs supports encoded reads and writes of compressed data. +The data is encoded as follows: +.RS +.IP * 3 +If +.I compression +isman7/encoded_io.7 line 317
Quoted 10 lines
man7/encoded_io.7 new file +10
-0+.BR ENCODED_IOV_COMPRESSION_ZLIB , +then the encoded data is a single zlib stream. +.IP * +If +.I compression +is +.BR ENCODED_IOV_COMPRESSION_LZO , +then the encoded data is compressed page by page with LZO1X +and wrapped in the format documented in the Linux kernel source file +.IR fs/btrfs/lzo.c .:-/ So maybe call it
ENCODED_IOV_COMPRESSION_BTRFS_LZO?I understand why you want the encoding format not to be opaque, but I imagine the encoded data is not going to be migrated as is between different filesystems. So just call it for what it is - a private filesystem encoding
format. If you have a format that is standard and other filesystems are likely to use, fine, but let's not make an API that discourages using "private" encoding, just for the sake of it and make life harder for no good reason.All the reader of this man page may be interested to know is which filesystems are expected to support which encoding types and a general description of what they mean (as you did).
Making this page wrongly appear as a standard for encoding formats is not going to play out well...
Quoted 11 lines
man7/encoded_io.7 line 325
Quoted 8 lines
+.IP *
man7/encoded_io.7 new file +7
-0+If +.I compression +is +.BR ENCODED_IOV_COMPRESSION_ZSTD , +then the encoded data is a single zstd frame compressed with the +.I windowLog +compression parameter set to no more than 17.Even that small detail is a bit limiting to filesystems and should therefore be tagged as a private btrfs encoding IMO.
Agreed, I'll make the LZO and ZSTD encodings Btrfs-specific. My assumption was that decoders would look at the filesystem type from, say, statfs(2), but making it explicit in the encoding is much better. On the other hand, I think ENCODED_IOV_COMPRESSION_ZLIB is generic enough to be reused.
Thanks for taking a look!
PATCH v5 1/9 iov_iter: add copy_struct_from_iter()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
This is essentially copy_struct_from_user() but for an iov_iter.
--- include/linux/uio.h | 2 ++ lib/iov_iter.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)
include/linux/uio.h +2 -0
@@ -121,6 +121,8 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i);size_t copy_page_from_iter (struct page *page,size_t offset,size_t bytes,struct iov_iter *i); +int copy_struct_from_iter (void *dst,size_t ksize,struct iov_iter *i, +size_t usize);size_t _copy_to_iter (const void *addr,size_t bytes,struct iov_iter *i);size_t _copy_from_iter (void *addr,size_t bytes,struct iov_iter *i);
lib/iov_iter.c +82 -0
88 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v5 1/9] iov_iter: add copy_struct_from_iter()
Quoted 6 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
This is essentially
copy_struct_from_user()but for aniov_iter.Suggested-by Aleksa Sarai <[email protected]>Signed-off-by Omar Sandoval <[email protected]>
This took me a lot longer to grok than I'm proud of, but the idea is you'll have a single segment that represents the incoming encoded data, and then subsequent segments will be the read/write buffer, correct? The code looks fine to me,
Thanks,
Josef
Re: [PATCH v5 1/9] iov_iter: add copy_struct_from_iter()
Quoted 13 lines · Josef Bacik
Quoted 6 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
This is essentially
copy_struct_from_user()but for aniov_iter.Suggested-by Aleksa Sarai <[email protected]>Signed-off-by Omar Sandoval <[email protected]>This took me a lot longer to grok than I'm proud of, but the idea is you'll have a single segment that represents the incoming encoded data, and then subsequent segments will be the read/write buffer, correct? The code looks fine to me,
Quoted 5 lines
Reviewed-by Josef Bacik <[email protected]>Thanks,
Josef
Yes, that's the idea for RWF_ENCODED. This patch is the generic way to shove an extra metadata struct at the beginning of an iov_iter in a way that is backwards/forwards compatible.
Thanks!
PATCH v5 2/9 fs: add O_ALLOW_ENCODED open flag
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
The upcoming RWF_ENCODED operation introduces some security concerns:
1. Compressed writes will pass arbitrary data to decompression algorithms in the kernel.
2. Compressed reads can leak truncated/hole punched data.
Therefore, we need to require privilege for RWF_ENCODED. It's not possible to do the permissions checks at the time of the read or write because, e.g., io_uring submits IO from a worker thread. So, add an open flag which requires CAP_SYS_ADMIN. It can also be set and cleared with fcntl(). The flag is not cleared in any way on fork or exec; it should probably be used with O_CLOEXEC in most cases.
Note that the usual issue that unknown open flags are ignored doesn't really matter for O_ALLOW_ENCODED; if the kernel doesn't support O_ALLOW_ENCODED, then it doesn't support RWF_ENCODED, either.
--- arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/fcntl.c | 10 ++++++++-- fs/namei.c | 4 ++++ include/linux/fcntl.h | 2 +- include/uapi/asm-generic/fcntl.h | 4 ++++ 7 files changed, 20 insertions(+), 3 deletions(-)
arch/alpha/include/uapi/asm/fcntl.h +1 -0
@@ -34,6 +34,7 @@#define O_PATH 040000000#define __O_TMPFILE 0100000000 +#define O_ALLOW_ENCODED 0200000000#define F_GETLK 7#define F_SETLK 8
arch/parisc/include/uapi/asm/fcntl.h +1 -0
@@ -22,6 +22,7 @@#define O_PATH 020000000#define __O_TMPFILE 040000000 +#define O_ALLOW_ENCODED 100000000#define F_GETLK64 8#define F_SETLK64 9
arch/sparc/include/uapi/asm/fcntl.h +1 -0
@@ -37,6 +37,7 @@#define O_PATH 0x1000000#define __O_TMPFILE 0x2000000 +#define O_ALLOW_ENCODED 0x8000000#define F_GETOWN 5/* for sockets. */ #define F_SETOWN 6/* for sockets. */
fs/fcntl.c +8 -2
@@ -30,7 +30,8 @@#include <asm/siginfo.h> #include <linux/uaccess.h> -#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME) +#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME | \ + O_ALLOW_ENCODED)static int setfl (int fd,struct file * filp,unsigned long arg) {
@@ -49,6 +50,11 @@ static int setfl(int fd, struct file * filp, unsigned long arg)if (!inode_owner_or_capable (inode))return -EPERM ; +/* O_ALLOW_ENCODED can only be set by superuser */ +if ((arg &O_ALLOW_ENCODED ) && !(filp->f_flags &O_ALLOW_ENCODED ) && + !capable (CAP_SYS_ADMIN )) +return -EPERM ; +/* required for strict SunOS emulation */ if (O_NONBLOCK !=O_NDELAY )if (arg &O_NDELAY )
@@ -1033,7 +1039,7 @@ static int __init fcntl_init(void)* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ -BUILD_BUG_ON (21 -1 /* for O_RDONLY being 0 */ != +BUILD_BUG_ON (22 -1 /* for O_RDONLY being 0 */ !=HWEIGHT32 ( (VALID_OPEN_FLAGS & ~(O_NONBLOCK |O_NDELAY )) | __FMODE_EXEC | __FMODE_NONOTIFY));
fs/namei.c +4 -0
@@ -2889,6 +2889,10 @@ static int may_open(const struct path *path, int acc_mode, int flag)if (flag &O_NOATIME && !inode_owner_or_capable (inode))return -EPERM ; +/* O_ALLOW_ENCODED can only be set by superuser */ +if ((flag &O_ALLOW_ENCODED ) && !capable (CAP_SYS_ADMIN )) +return -EPERM ; +return 0 ; }
include/linux/fcntl.h +1 -1
@@ -10,7 +10,7 @@ (O_RDONLY |O_WRONLY |O_RDWR |O_CREAT |O_EXCL |O_NOCTTY |O_TRUNC | \O_APPEND |O_NDELAY |O_NONBLOCK |O_NDELAY | __O_SYNC |O_DSYNC | \FASYNC |O_DIRECT |O_LARGEFILE |O_DIRECTORY |O_NOFOLLOW | \ -O_NOATIME |O_CLOEXEC |O_PATH | __O_TMPFILE) +O_NOATIME |O_CLOEXEC |O_PATH | __O_TMPFILE |O_ALLOW_ENCODED ) /* List of all valid flags for the how->upgrade_mask argument: */#define VALID_UPGRADE_FLAGS \
include/uapi/asm-generic/fcntl.h +4 -0
@@ -89,6 +89,10 @@#define __O_TMPFILE 020000000#endif +#ifndef O_ALLOW_ENCODED +#define O_ALLOW_ENCODED 040000000 +#endif +/* a horrid kludge trying to make sure that this will fail on old kernels */ #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)#define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT)
Signature · 1 line
2.28.0
Re: [PATCH v5 2/9] fs: add O_ALLOW_ENCODED open flag
Quoted 15 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
The upcoming
RWF_ENCODEDoperation introduces some security concerns:1. Compressed writes will pass arbitrary data to decompression algorithms in the kernel.
2. Compressed reads can leak truncated/hole punched data.Therefore, we need to require privilege for
RWF_ENCODED. It's not possible to do the permissions checks at the time of the read or write because, e.g.,io_uringsubmits IO from a worker thread. So, add an open flag which requiresCAP_SYS_ADMIN. It can also be set and cleared withfcntl(). The flag is not cleared in any way on fork or exec; it should probably be used withO_CLOEXECin most cases.
Quoted 3 lines
Note that the usual issue that unknown open flags are ignored doesn't really matter for
O_ALLOW_ENCODED; if the kernel doesn't supportO_ALLOW_ENCODED, then it doesn't supportRWF_ENCODED, either.
It seemed like you agreed to require O_CLOEXEC to be set when using O_ALLOW_ENCODED in your last go around, what happened to that? I know I'd feel better if we had that requirement, and if we aren't I'd like to know why we can't. Thanks,
Josef
Re: [PATCH v5 2/9] fs: add O_ALLOW_ENCODED open flag
Quoted 20 lines · Josef Bacik
Quoted 15 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
The upcoming
RWF_ENCODEDoperation introduces some security concerns:1. Compressed writes will pass arbitrary data to decompression algorithms in the kernel.
2. Compressed reads can leak truncated/hole punched data.Therefore, we need to require privilege for
RWF_ENCODED. It's not possible to do the permissions checks at the time of the read or write because, e.g.,io_uringsubmits IO from a worker thread. So, add an open flag which requiresCAP_SYS_ADMIN. It can also be set and cleared withfcntl(). The flag is not cleared in any way on fork or exec; it should probably be used withO_CLOEXECin most cases.Quoted 3 lines
Note that the usual issue that unknown open flags are ignored doesn't really matter for
O_ALLOW_ENCODED; if the kernel doesn't supportO_ALLOW_ENCODED, then it doesn't supportRWF_ENCODED, either.
Quoted 6 lines
It seemed like you agreed to require
O_CLOEXECto be set when usingO_ALLOW_ENCODEDin your last go around, what happened to that? I know I'd feel better if we had that requirement, and if we aren't I'd like to know why we can't. Thanks,Josef
Yup I was still on the fence about it since it's a bit of an awkward requirement, but I'm convinced now that we might as well be safe and require it.
PATCH v5 3/9 fs: add RWF_ENCODED for reading/writing compressed data
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Btrfs supports transparent compression: data written by the user can be compressed when written to disk and decompressed when read back. However, we'd like to add an interface to write pre-compressed data directly to the filesystem, and the matching interface to read compressed data without decompressing it. This adds support for so-called "encoded I/O" via preadv2() and pwritev2().
A new RWF_ENCODED flags indicates that a read or write is "encoded". If this flag is set, iov[0].iov_base points to a struct encoded_iov which is used for metadata: namely, the compression algorithm, unencoded (i.e., decompressed) length, and what subrange of the unencoded data should be used (needed for truncated or hole-punched extents and when reading in the middle of an extent). For reads, the filesystem returns this information; for writes, the caller provides it to the filesystem. iov[0].iov_len must be set to sizeof(struct encoded_iov), which can be used to extend the interface in the future a la copy_struct_from_user(). The remaining iovecs contain the encoded extent.
This adds the VFS helpers for supporting encoded I/O and documentation for filesystem support.
--- Documentation/filesystems/encoded_io.rst | 74 ++++++++++ Documentation/filesystems/index.rst | 1 + include/linux/fs.h | 16 +++ include/uapi/linux/fs.h | 33 ++++- mm/filemap.c | 166 +++++++++++++++++++++-- 5 files changed, 276 insertions(+), 14 deletions(-) create mode 100644 Documentation/filesystems/encoded_io.rst
Documentation/filesystems/encoded_io.rst new file +74 -0
@@ -0,0 +1,74 @@ +=========== +Encoded I/O +=========== + +Encoded I/O is a mechanism for reading and writing encoded (e.g., compressed +and/or encrypted) data directly from/to the filesystem. The userspace interface +is thoroughly described in the :manpage:`encoded_io(7)` man page; this document +describes the requirements for filesystem support. + +First of all, a filesystem supporting encoded I/O must indicate this by setting +the ``FMODE_ENCODED_IO`` flag in its ``file_open`` file operation:: + + static int foo_file_open(struct inode *inode, struct file *filp) + { + ... + filep->f_mode |= FMODE_ENCODED_IO; + ... + } + +Encoded I/O goes through ``read_iter`` and ``write_iter``, designated by the +``IOCB_ENCODED`` flag in ``kiocb->ki_flags``. + +Reads +===== + +Encoded ``read_iter`` should: + +1. Call ``generic_encoded_read_checks()`` to validate the file and buffers + provided by userspace. +2. Initialize the ``encoded_iov`` appropriately. +3. Copy it to the user with ``copy_encoded_iov_to_iter()``. +4. Copy the encoded data to the user. +5. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +6. Return the size of the encoded data read, not including the ``encoded_iov``. + +There are a few details to be aware of: + +* Encoded ``read_iter`` should support reading unencoded data if the extent is + not encoded. +* If the buffers provided by the user are not large enough to contain an entire + encoded extent, then ``read_iter`` should return ``-ENOBUFS``. This is to + avoid confusing userspace with truncated data that cannot be properly + decoded. +* Reads in the middle of an encoded extent can be returned by setting + ``encoded_iov->unencoded_offset`` to non-zero. +* Truncated unencoded data (e.g., because the file does not end on a block + boundary) may be returned by setting ``encoded_iov->len`` to a value smaller + value than ``encoded_iov->unencoded_len - encoded_iov->unencoded_offset``. + +Writes +====== + +Encoded ``write_iter`` should (in addition to the usual accounting/checks done +by ``write_iter``): + +1. Call ``copy_encoded_iov_from_iter()`` to get and validate the + ``encoded_iov``. +2. Call ``generic_encoded_write_checks()`` instead of + ``generic_write_checks()``. +3. Check that the provided encoding in ``encoded_iov`` is supported. +4. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +5. Return the size of the encoded data written. + +Again, there are a few details: + +* Encoded ``write_iter`` doesn't need to support writing unencoded data. +* ``write_iter`` should either write all of the encoded data or none of it; it + must not do partial writes. +* ``write_iter`` doesn't need to validate the encoded data; a subsequent read + may return, e.g., ``-EIO`` if the data is not valid. +* The user may lie about the unencoded size of the data; a subsequent read + should truncate or zero-extend the unencoded data rather than returning an + error. +* Be careful of page cache coherency.
Documentation/filesystems/index.rst +1 -0
@@ -55,6 +55,7 @@ filesystem implementations. journalling fscrypt fsverity + encoded_io Filesystems ===========
include/linux/fs.h +16 -0
@@ -178,6 +178,9 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File supports async buffered reads */ #define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000) +/* File supports encoded IO */ +#define FMODE_ENCODED_IO ((__force fmode_t)0x80000000) +/* * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector * that indicates that they should check the contents of the iovec are
@@ -321,6 +324,7 @@ enum rw_hint {/* iocb->ki_waitq is valid */ #define IOCB_WAITQ (1 << 8)#define IOCB_NOIO (1 << 9) +#define IOCB_ENCODED (1 << 10)struct kiocb {struct file *ki_filp;
@@ -3006,6 +3010,13 @@ extern int sb_min_blocksize(struct super_block *, int);extern int generic_file_mmap (struct file *,struct vm_area_struct *);extern int generic_file_readonly_mmap (struct file *,struct vm_area_struct *);extern ssize_t generic_write_checks (struct kiocb *,struct iov_iter *); +struct encoded_iov ; +extern int generic_encoded_write_checks (struct kiocb *, +const struct encoded_iov *); +extern int copy_encoded_iov_from_iter (struct encoded_iov *,struct iov_iter *); +extern ssize_t generic_encoded_read_checks (struct kiocb *,struct iov_iter *); +extern int copy_encoded_iov_to_iter (const struct encoded_iov *, +struct iov_iter *);extern int generic_remap_checks (struct file *file_in,loff_t pos_in,struct file *file_out,loff_t pos_out,loff_t *count,unsigned int remap_flags);
@@ -3324,6 +3335,11 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)return -EOPNOTSUPP ; kiocb_flags |=IOCB_NOWAIT |IOCB_NOIO ; } +if (flags &RWF_ENCODED ) { +if (!(ki->ki_filp->f_mode &FMODE_ENCODED_IO )) +return -EOPNOTSUPP ; + kiocb_flags |=IOCB_ENCODED ; + }if (flags &RWF_HIPRI ) kiocb_flags |=IOCB_HIPRI ;if (flags &RWF_DSYNC )
include/uapi/linux/fs.h +32 -1
@@ -279,6 +279,34 @@ struct fsxattr { SYNC_FILE_RANGE_WAIT_BEFORE | \ SYNC_FILE_RANGE_WAIT_AFTER) +enum { +ENCODED_IOV_COMPRESSION_NONE , +#define ENCODED_IOV_COMPRESSION_NONE ENCODED_IOV_COMPRESSION_NONE + ENCODED_IOV_COMPRESSION_ZLIB, +#define ENCODED_IOV_COMPRESSION_ZLIB ENCODED_IOV_COMPRESSION_ZLIB + ENCODED_IOV_COMPRESSION_LZO, +#define ENCODED_IOV_COMPRESSION_LZO ENCODED_IOV_COMPRESSION_LZO + ENCODED_IOV_COMPRESSION_ZSTD, +#define ENCODED_IOV_COMPRESSION_ZSTD ENCODED_IOV_COMPRESSION_ZSTD + ENCODED_IOV_COMPRESSION_TYPES = ENCODED_IOV_COMPRESSION_ZSTD, +}; + +enum { +ENCODED_IOV_ENCRYPTION_NONE , +#define ENCODED_IOV_ENCRYPTION_NONE ENCODED_IOV_ENCRYPTION_NONE + ENCODED_IOV_ENCRYPTION_TYPES = ENCODED_IOV_ENCRYPTION_NONE, +}; + +struct encoded_iov { +__aligned_u64 len; +__aligned_u64 unencoded_len; +__aligned_u64 unencoded_offset; +__u32 compression; +__u32 encryption; +}; + +#define ENCODED_IOV_SIZE_VER0 32 +/* * Flags for preadv2/pwritev2: */
@@ -300,8 +328,11 @@ typedef int __bitwise __kernel_rwf_t;/* per-IO O_APPEND */ #define RWF_APPEND ((__force __kernel_rwf_t)0x00000010) +/* encoded (e.g., compressed and/or encrypted) IO */ +#define RWF_ENCODED ((__force __kernel_rwf_t)0x00000020) +/* mask of flags supported by the kernel */ #define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\ - RWF_APPEND) + RWF_APPEND | RWF_ENCODED)#endif /* _UAPI_LINUX_FS_H */
mm/filemap.c +153 -13
191 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v5 3/9] fs: add RWF_ENCODED for reading/writing compressed data
Quoted 104 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Btrfs supports transparent compression: data written by the user can be compressed when written to disk and decompressed when read back. However, we'd like to add an interface to write pre-compressed data directly to the filesystem, and the matching interface to read compressed data without decompressing it. This adds support for so-called "encoded I/O" via
preadv2()andpwritev2().A new
RWF_ENCODEDflags indicates that a read or write is "encoded". If this flag is set, iov[0].iov_base points to a structencoded_iovwhich is used for metadata: namely, the compression algorithm, unencoded (i.e., decompressed) length, and what subrange of the unencoded data should be used (needed for truncated or hole-punched extents and when reading in the middle of an extent). For reads, the filesystem returns this information; for writes, the caller provides it to the filesystem. iov[0].iov_len must be set tosizeof(struct encoded_iov), which can be used to extend the interface in the future a lacopy_struct_from_user(). The remaining iovecs contain the encoded extent.This adds the VFS helpers for supporting encoded I/O and documentation for filesystem support.
Signed-off-by Omar Sandoval <[email protected]>--- Documentation/filesystems/encoded_io.rst | 74 ++++++++++ Documentation/filesystems/index.rst | 1 + include/linux/fs.h | 16 +++ include/uapi/linux/fs.h | 33 ++++- mm/filemap.c | 166 +++++++++++++++++++++-- 5 files changed, 276 insertions(+), 14 deletions(-) create mode 100644 Documentation/filesystems/encoded_io.rstDocumentation/filesystems/encoded_io.rst new file +64
-0@@ -0,0 +1,74 @@ +=========== +Encoded I/O +=========== + +Encoded I/O is a mechanism for reading and writing encoded (e.g., compressed +and/or encrypted) data directly from/to the filesystem. The userspace interface +is thoroughly described in the :manpage:`encoded_io(7)` man page; this document +describes the requirements for filesystem support. + +First of all, a filesystem supporting encoded I/O must indicate this by setting +the ``FMODE_ENCODED_IO`` flag in its ``file_open`` file operation:: + + static int foo_file_open(struct inode *inode, struct file *filp) + { + ... + filep->f_mode |= FMODE_ENCODED_IO; + ... + } + +Encoded I/O goes through ``read_iter`` and ``write_iter``, designated by the +``IOCB_ENCODED`` flag in ``kiocb->ki_flags``. + +Reads +===== + +Encoded ``read_iter`` should: + +1. Call ``generic_encoded_read_checks()`` to validate the file and buffers + provided by userspace. +2. Initialize the ``encoded_iov`` appropriately. +3. Copy it to the user with ``copy_encoded_iov_to_iter()``. +4. Copy the encoded data to the user. +5. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +6. Return the size of the encoded data read, not including the ``encoded_iov``. + +There are a few details to be aware of: + +* Encoded ``read_iter`` should support reading unencoded data if the extent is + not encoded. +* If the buffers provided by the user are not large enough to contain an entire + encoded extent, then ``read_iter`` should return ``-ENOBUFS``. This is to + avoid confusing userspace with truncated data that cannot be properly + decoded. +* Reads in the middle of an encoded extent can be returned by setting + ``encoded_iov->unencoded_offset`` to non-zero. +* Truncated unencoded data (e.g., because the file does not end on a block + boundary) may be returned by setting ``encoded_iov->len`` to a value smaller + value than ``encoded_iov->unencoded_len - encoded_iov->unencoded_offset``. + +Writes +====== + +Encoded ``write_iter`` should (in addition to the usual accounting/checks done +by ``write_iter``): + +1. Call ``copy_encoded_iov_from_iter()`` to get and validate the + ``encoded_iov``. +2. Call ``generic_encoded_write_checks()`` instead of + ``generic_write_checks()``. +3. Check that the provided encoding in ``encoded_iov`` is supported. +4. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +5. Return the size of the encoded data written. + +Again, there are a few details:
Documentation/filesystems/encoded_io.rst line 74
Quoted 10 lines
Documentation/filesystems/encoded_io.rst new file +10
-0+ +* Encoded ``write_iter`` doesn't need to support writing unencoded data. +* ``write_iter`` should either write all of the encoded data or none of it; it + must not do partial writes. +* ``write_iter`` doesn't need to validate the encoded data; a subsequent read + may return, e.g., ``-EIO`` if the data is not valid. +* The user may lie about the unencoded size of the data; a subsequent read + should truncate or zero-extend the unencoded data rather than returning an + error. +* Be careful of page cache coherency.
Haha that rings in my head like the "Smoking kills!" warnings...
I find it a bit odd that you mix page cache at all when reading unencoded extents.
Feels like a file with FMODE_ENCODED_IO should stick to direct IO in all cases. I don't know how btrfs deals with mixing direct IO and page cache IO normally, but surely the rules could be made even stricter for an inode accessed with this new API?
Is there something I am misunderstanding?
Thanks,
Amir.
Re: [PATCH v5 3/9] fs: add RWF_ENCODED for reading/writing compressed data
Quoted 125 lines · Amir Goldstein
Quoted 104 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Btrfs supports transparent compression: data written by the user can be compressed when written to disk and decompressed when read back. However, we'd like to add an interface to write pre-compressed data directly to the filesystem, and the matching interface to read compressed data without decompressing it. This adds support for so-called "encoded I/O" via
preadv2()andpwritev2().A new
RWF_ENCODEDflags indicates that a read or write is "encoded". If this flag is set, iov[0].iov_base points to a structencoded_iovwhich is used for metadata: namely, the compression algorithm, unencoded (i.e., decompressed) length, and what subrange of the unencoded data should be used (needed for truncated or hole-punched extents and when reading in the middle of an extent). For reads, the filesystem returns this information; for writes, the caller provides it to the filesystem. iov[0].iov_len must be set tosizeof(struct encoded_iov), which can be used to extend the interface in the future a lacopy_struct_from_user(). The remaining iovecs contain the encoded extent.This adds the VFS helpers for supporting encoded I/O and documentation for filesystem support.
Signed-off-by Omar Sandoval <[email protected]>--- Documentation/filesystems/encoded_io.rst | 74 ++++++++++ Documentation/filesystems/index.rst | 1 + include/linux/fs.h | 16 +++ include/uapi/linux/fs.h | 33 ++++- mm/filemap.c | 166 +++++++++++++++++++++-- 5 files changed, 276 insertions(+), 14 deletions(-) create mode 100644 Documentation/filesystems/encoded_io.rstDocumentation/filesystems/encoded_io.rst new file +64
-0@@ -0,0 +1,74 @@ +=========== +Encoded I/O +=========== + +Encoded I/O is a mechanism for reading and writing encoded (e.g., compressed +and/or encrypted) data directly from/to the filesystem. The userspace interface +is thoroughly described in the :manpage:`encoded_io(7)` man page; this document +describes the requirements for filesystem support. + +First of all, a filesystem supporting encoded I/O must indicate this by setting +the ``FMODE_ENCODED_IO`` flag in its ``file_open`` file operation:: + + static int foo_file_open(struct inode *inode, struct file *filp) + { + ... + filep->f_mode |= FMODE_ENCODED_IO; + ... + } + +Encoded I/O goes through ``read_iter`` and ``write_iter``, designated by the +``IOCB_ENCODED`` flag in ``kiocb->ki_flags``. + +Reads +===== + +Encoded ``read_iter`` should: + +1. Call ``generic_encoded_read_checks()`` to validate the file and buffers + provided by userspace. +2. Initialize the ``encoded_iov`` appropriately. +3. Copy it to the user with ``copy_encoded_iov_to_iter()``. +4. Copy the encoded data to the user. +5. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +6. Return the size of the encoded data read, not including the ``encoded_iov``. + +There are a few details to be aware of: + +* Encoded ``read_iter`` should support reading unencoded data if the extent is + not encoded. +* If the buffers provided by the user are not large enough to contain an entire + encoded extent, then ``read_iter`` should return ``-ENOBUFS``. This is to + avoid confusing userspace with truncated data that cannot be properly + decoded. +* Reads in the middle of an encoded extent can be returned by setting + ``encoded_iov->unencoded_offset`` to non-zero. +* Truncated unencoded data (e.g., because the file does not end on a block + boundary) may be returned by setting ``encoded_iov->len`` to a value smaller + value than ``encoded_iov->unencoded_len - encoded_iov->unencoded_offset``. + +Writes +====== + +Encoded ``write_iter`` should (in addition to the usual accounting/checks done +by ``write_iter``): + +1. Call ``copy_encoded_iov_from_iter()`` to get and validate the + ``encoded_iov``. +2. Call ``generic_encoded_write_checks()`` instead of + ``generic_write_checks()``. +3. Check that the provided encoding in ``encoded_iov`` is supported. +4. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +5. Return the size of the encoded data written. + +Again, there are a few details:Documentation/filesystems/encoded_io.rst line 74
Quoted 10 lines
Documentation/filesystems/encoded_io.rst new file +10
-0+ +* Encoded ``write_iter`` doesn't need to support writing unencoded data. +* ``write_iter`` should either write all of the encoded data or none of it; it + must not do partial writes. +* ``write_iter`` doesn't need to validate the encoded data; a subsequent read + may return, e.g., ``-EIO`` if the data is not valid. +* The user may lie about the unencoded size of the data; a subsequent read + should truncate or zero-extend the unencoded data rather than returning an + error. +* Be careful of page cache coherency.Haha that rings in my head like the "Smoking kills!" warnings...
I find it a bit odd that you mix page cache at all when reading unencoded extents.
Feels like a file withFMODE_ENCODED_IOshould stick to direct IO in all cases. I don't know how btrfs deals with mixing direct IO and page cache IO normally, but surely the rules could be made even stricter for an inode accessed with this new API?
Quoted 4 lines
Is there something I am misunderstanding?
Thanks,
Amir.
I'm not completely following here, are you suggesting that if a file is open with O_ALLOW_ENCODED, buffered I/O to that file should return an error? Btrfs at least does the necessary range locking and page cache invalidation to ensure that direct I/O gets along with buffered I/O (and now encoded I/O).
Re: [PATCH v5 3/9] fs: add RWF_ENCODED for reading/writing compressed data
Quoted 127 lines · Omar Sandoval
Quoted 125 lines · Amir Goldstein
Quoted 104 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Btrfs supports transparent compression: data written by the user can be compressed when written to disk and decompressed when read back. However, we'd like to add an interface to write pre-compressed data directly to the filesystem, and the matching interface to read compressed data without decompressing it. This adds support for so-called "encoded I/O" via
preadv2()andpwritev2().A new
RWF_ENCODEDflags indicates that a read or write is "encoded". If this flag is set, iov[0].iov_base points to a structencoded_iovwhich is used for metadata: namely, the compression algorithm, unencoded (i.e., decompressed) length, and what subrange of the unencoded data should be used (needed for truncated or hole-punched extents and when reading in the middle of an extent). For reads, the filesystem returns this information; for writes, the caller provides it to the filesystem. iov[0].iov_len must be set tosizeof(struct encoded_iov), which can be used to extend the interface in the future a lacopy_struct_from_user(). The remaining iovecs contain the encoded extent.This adds the VFS helpers for supporting encoded I/O and documentation for filesystem support.
Signed-off-by Omar Sandoval <[email protected]>--- Documentation/filesystems/encoded_io.rst | 74 ++++++++++ Documentation/filesystems/index.rst | 1 + include/linux/fs.h | 16 +++ include/uapi/linux/fs.h | 33 ++++- mm/filemap.c | 166 +++++++++++++++++++++-- 5 files changed, 276 insertions(+), 14 deletions(-) create mode 100644 Documentation/filesystems/encoded_io.rstDocumentation/filesystems/encoded_io.rst new file +64
-0@@ -0,0 +1,74 @@ +=========== +Encoded I/O +=========== + +Encoded I/O is a mechanism for reading and writing encoded (e.g., compressed +and/or encrypted) data directly from/to the filesystem. The userspace interface +is thoroughly described in the :manpage:`encoded_io(7)` man page; this document +describes the requirements for filesystem support. + +First of all, a filesystem supporting encoded I/O must indicate this by setting +the ``FMODE_ENCODED_IO`` flag in its ``file_open`` file operation:: + + static int foo_file_open(struct inode *inode, struct file *filp) + { + ... + filep->f_mode |= FMODE_ENCODED_IO; + ... + } + +Encoded I/O goes through ``read_iter`` and ``write_iter``, designated by the +``IOCB_ENCODED`` flag in ``kiocb->ki_flags``. + +Reads +===== + +Encoded ``read_iter`` should: + +1. Call ``generic_encoded_read_checks()`` to validate the file and buffers + provided by userspace. +2. Initialize the ``encoded_iov`` appropriately. +3. Copy it to the user with ``copy_encoded_iov_to_iter()``. +4. Copy the encoded data to the user. +5. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +6. Return the size of the encoded data read, not including the ``encoded_iov``. + +There are a few details to be aware of: + +* Encoded ``read_iter`` should support reading unencoded data if the extent is + not encoded. +* If the buffers provided by the user are not large enough to contain an entire + encoded extent, then ``read_iter`` should return ``-ENOBUFS``. This is to + avoid confusing userspace with truncated data that cannot be properly + decoded. +* Reads in the middle of an encoded extent can be returned by setting + ``encoded_iov->unencoded_offset`` to non-zero. +* Truncated unencoded data (e.g., because the file does not end on a block + boundary) may be returned by setting ``encoded_iov->len`` to a value smaller + value than ``encoded_iov->unencoded_len - encoded_iov->unencoded_offset``. + +Writes +====== + +Encoded ``write_iter`` should (in addition to the usual accounting/checks done +by ``write_iter``): + +1. Call ``copy_encoded_iov_from_iter()`` to get and validate the + ``encoded_iov``. +2. Call ``generic_encoded_write_checks()`` instead of + ``generic_write_checks()``. +3. Check that the provided encoding in ``encoded_iov`` is supported. +4. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +5. Return the size of the encoded data written. + +Again, there are a few details:Documentation/filesystems/encoded_io.rst line 74
Quoted 10 lines
Documentation/filesystems/encoded_io.rst new file +10
-0+ +* Encoded ``write_iter`` doesn't need to support writing unencoded data. +* ``write_iter`` should either write all of the encoded data or none of it; it + must not do partial writes. +* ``write_iter`` doesn't need to validate the encoded data; a subsequent read + may return, e.g., ``-EIO`` if the data is not valid. +* The user may lie about the unencoded size of the data; a subsequent read + should truncate or zero-extend the unencoded data rather than returning an + error. +* Be careful of page cache coherency.Haha that rings in my head like the "Smoking kills!" warnings...
I find it a bit odd that you mix page cache at all when reading unencoded extents.
Feels like a file withFMODE_ENCODED_IOshould stick to direct IO in all cases. I don't know how btrfs deals with mixing direct IO and page cache IO normally, but surely the rules could be made even stricter for an inode accessed with this new API?
Quoted 8 lines
Quoted 4 lines
Is there something I am misunderstanding?
Thanks,
Amir.I'm not completely following here, are you suggesting that if a file is open with
O_ALLOW_ENCODED, buffered I/O to that file should return an error?
No. I don't.
Quoted 3 lines
Btrfs at least does the necessary range locking and page cache invalidation to ensure that direct I/O gets along with buffered I/O (and now encoded I/O).
That's a good start :-)
I saw btrfs_encoded_read_regular_fill_pages() and concluded that even in FMODE_ENCODED_IO, when reading an unencoded extent, you fill page cache with the unencoded data.
Is that correct? or did I miss read the code?
If correct, does it serve any purpose?
Seems more sensible to me to read/write FMODE_ENCODED_IO only in direct io regardless if the extent is encoded or not (for simpler code if nothing else).
Thanks,
Amir.
Re: [PATCH v5 3/9] fs: add RWF_ENCODED for reading/writing compressed data
Quoted 149 lines · Amir Goldstein
Quoted 127 lines · Omar Sandoval
Quoted 125 lines · Amir Goldstein
Quoted 104 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Btrfs supports transparent compression: data written by the user can be compressed when written to disk and decompressed when read back. However, we'd like to add an interface to write pre-compressed data directly to the filesystem, and the matching interface to read compressed data without decompressing it. This adds support for so-called "encoded I/O" via
preadv2()andpwritev2().A new
RWF_ENCODEDflags indicates that a read or write is "encoded". If this flag is set, iov[0].iov_base points to a structencoded_iovwhich is used for metadata: namely, the compression algorithm, unencoded (i.e., decompressed) length, and what subrange of the unencoded data should be used (needed for truncated or hole-punched extents and when reading in the middle of an extent). For reads, the filesystem returns this information; for writes, the caller provides it to the filesystem. iov[0].iov_len must be set tosizeof(struct encoded_iov), which can be used to extend the interface in the future a lacopy_struct_from_user(). The remaining iovecs contain the encoded extent.This adds the VFS helpers for supporting encoded I/O and documentation for filesystem support.
Signed-off-by Omar Sandoval <[email protected]>--- Documentation/filesystems/encoded_io.rst | 74 ++++++++++ Documentation/filesystems/index.rst | 1 + include/linux/fs.h | 16 +++ include/uapi/linux/fs.h | 33 ++++- mm/filemap.c | 166 +++++++++++++++++++++-- 5 files changed, 276 insertions(+), 14 deletions(-) create mode 100644 Documentation/filesystems/encoded_io.rstDocumentation/filesystems/encoded_io.rst new file +64
-0@@ -0,0 +1,74 @@ +=========== +Encoded I/O +=========== + +Encoded I/O is a mechanism for reading and writing encoded (e.g., compressed +and/or encrypted) data directly from/to the filesystem. The userspace interface +is thoroughly described in the :manpage:`encoded_io(7)` man page; this document +describes the requirements for filesystem support. + +First of all, a filesystem supporting encoded I/O must indicate this by setting +the ``FMODE_ENCODED_IO`` flag in its ``file_open`` file operation:: + + static int foo_file_open(struct inode *inode, struct file *filp) + { + ... + filep->f_mode |= FMODE_ENCODED_IO; + ... + } + +Encoded I/O goes through ``read_iter`` and ``write_iter``, designated by the +``IOCB_ENCODED`` flag in ``kiocb->ki_flags``. + +Reads +===== + +Encoded ``read_iter`` should: + +1. Call ``generic_encoded_read_checks()`` to validate the file and buffers + provided by userspace. +2. Initialize the ``encoded_iov`` appropriately. +3. Copy it to the user with ``copy_encoded_iov_to_iter()``. +4. Copy the encoded data to the user. +5. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +6. Return the size of the encoded data read, not including the ``encoded_iov``. + +There are a few details to be aware of: + +* Encoded ``read_iter`` should support reading unencoded data if the extent is + not encoded. +* If the buffers provided by the user are not large enough to contain an entire + encoded extent, then ``read_iter`` should return ``-ENOBUFS``. This is to + avoid confusing userspace with truncated data that cannot be properly + decoded. +* Reads in the middle of an encoded extent can be returned by setting + ``encoded_iov->unencoded_offset`` to non-zero. +* Truncated unencoded data (e.g., because the file does not end on a block + boundary) may be returned by setting ``encoded_iov->len`` to a value smaller + value than ``encoded_iov->unencoded_len - encoded_iov->unencoded_offset``. + +Writes +====== + +Encoded ``write_iter`` should (in addition to the usual accounting/checks done +by ``write_iter``): + +1. Call ``copy_encoded_iov_from_iter()`` to get and validate the + ``encoded_iov``. +2. Call ``generic_encoded_write_checks()`` instead of + ``generic_write_checks()``. +3. Check that the provided encoding in ``encoded_iov`` is supported. +4. Advance ``kiocb->ki_pos`` by ``encoded_iov->len``. +5. Return the size of the encoded data written. + +Again, there are a few details:Documentation/filesystems/encoded_io.rst line 74
Quoted 10 lines
Documentation/filesystems/encoded_io.rst new file +10
-0+ +* Encoded ``write_iter`` doesn't need to support writing unencoded data. +* ``write_iter`` should either write all of the encoded data or none of it; it + must not do partial writes. +* ``write_iter`` doesn't need to validate the encoded data; a subsequent read + may return, e.g., ``-EIO`` if the data is not valid. +* The user may lie about the unencoded size of the data; a subsequent read + should truncate or zero-extend the unencoded data rather than returning an + error. +* Be careful of page cache coherency.Haha that rings in my head like the "Smoking kills!" warnings...
I find it a bit odd that you mix page cache at all when reading unencoded extents.
Feels like a file withFMODE_ENCODED_IOshould stick to direct IO in all cases. I don't know how btrfs deals with mixing direct IO and page cache IO normally, but surely the rules could be made even stricter for an inode accessed with this new API?Quoted 8 lines
Quoted 4 lines
Is there something I am misunderstanding?
Thanks,
Amir.I'm not completely following here, are you suggesting that if a file is open with
O_ALLOW_ENCODED, buffered I/O to that file should return an error?No. I don't.
Quoted 3 lines
Btrfs at least does the necessary range locking and page cache invalidation to ensure that direct I/O gets along with buffered I/O (and now encoded I/O).
That's a good start :-)
I saw
btrfs_encoded_read_regular_fill_pages()and concluded that even inFMODE_ENCODED_IO, when reading an unencoded extent, you fill page cache with the unencoded data.
Quoted 7 lines
Is that correct? or did I miss read the code?
If correct, does it serve any purpose?
Seems more sensible to me to read/writeFMODE_ENCODED_IOonly in direct io regardless if the extent is encoded or not (for simpler code if nothing else).Thanks,
Amir.
Oh, I see. btrfs_encoded_read_regular_fill_pages() fills temporary pages allocated for the read, not page cache pages. RWF_ENCODED always bypasses the page cache, which I agree is the most sensible option.
Re: [PATCH v5 3/9] fs: add RWF_ENCODED for reading/writing compressed data
In reply to Omar SandovalQuoted 20 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Btrfs supports transparent compression: data written by the user can be compressed when written to disk and decompressed when read back. However, we'd like to add an interface to write pre-compressed data directly to the filesystem, and the matching interface to read compressed data without decompressing it. This adds support for so-called "encoded I/O" via
preadv2()andpwritev2().A new
RWF_ENCODEDflags indicates that a read or write is "encoded". If this flag is set, iov[0].iov_base points to a structencoded_iovwhich is used for metadata: namely, the compression algorithm, unencoded (i.e., decompressed) length, and what subrange of the unencoded data should be used (needed for truncated or hole-punched extents and when reading in the middle of an extent). For reads, the filesystem returns this information; for writes, the caller provides it to the filesystem. iov[0].iov_len must be set tosizeof(struct encoded_iov), which can be used to extend the interface in the future a lacopy_struct_from_user(). The remaining iovecs contain the encoded extent.
Quoted 4 lines
This adds the VFS helpers for supporting encoded I/O and documentation for filesystem support.
Signed-off-by Omar Sandoval <[email protected]>
Thanks,
Josef
PATCH v5 4/9 btrfs: don't advance offset for compressed bios in btrfs_csum_one_bio()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
btrfs_csum_one_bio() loops over each filesystem block in the bio while keeping a cursor of its current logical position in the file in order to look up the ordered extent to add the checksums to. However, this doesn't make much sense for compressed extents, as a sector on disk does not correspond to a sector of decompressed file data. It happens to work because 1) the compressed bio always covers one ordered extent and 2) the size of the bio is always less than the size of the ordered extent. However, the second point will not always be true for encoded writes.
Let's add a boolean parameter to btrfs_csum_one_bio() to indicate that it can assume that the bio only covers one ordered extent. Since we're already changing the signature, let's get rid of the contig parameter and make it implied by the offset parameter, similar to the change we recently made to btrfs_lookup_bio_sums(). Additionally, let's rename nr_sectors to blockcount to make it clear that it's the number of filesystem blocks, not the number of 512-byte sectors.
--- fs/btrfs/compression.c | 5 +++-- fs/btrfs/ctree.h | 2 +- fs/btrfs/file-item.c | 35 ++++++++++++++++------------------- fs/btrfs/inode.c | 8 ++++---- 4 files changed, 24 insertions(+), 26 deletions(-)
fs/btrfs/compression.c +3 -2
@@ -438,7 +438,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,BUG_ON (ret);/* -ENOMEM */ if (!skip_sum) { - ret =btrfs_csum_one_bio (inode, bio, start,1 ); + ret =btrfs_csum_one_bio (inode, bio, start, + true);BUG_ON (ret);/* -ENOMEM */ }
@@ -470,7 +471,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,BUG_ON (ret);/* -ENOMEM */ if (!skip_sum) { - ret =btrfs_csum_one_bio (inode, bio, start,1 ); + ret =btrfs_csum_one_bio (inode, bio, start, true);BUG_ON (ret);/* -ENOMEM */ }
fs/btrfs/ctree.h +1 -1
@@ -2917,7 +2917,7 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,struct btrfs_root *root, struct btrfs_ordered_sum *sums);blk_status_t btrfs_csum_one_bio (struct btrfs_inode *inode,struct bio *bio, -u64 file_start,int contig); +u64 offset,bool one_ordered);int btrfs_lookup_csums_range (struct btrfs_root *root,u64 start,u64 end,struct list_head *list,int search_commit); void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
fs/btrfs/file-item.c +16 -19
@@ -516,28 +516,28 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,* btrfs_csum_one_bio - Calculates checksums of the data contained inside a bio * @inode: Owner of the data inside the bio * @bio: Contains the data to be checksummed -* @file_start: offset in file this bio begins to describe -* @contig: Boolean. If true/1 means all bio vecs in this bio are -* contiguous and they begin at @file_start in the file. False/0 -* means this bio can contains potentially discontigous bio vecs -* so the logical offset of each should be calculated separately. +* @offset: If (u64)-1, @bio may contain discontiguous bio vecs, so the +* file offsets are determined from the page offsets in the bio. +* Otherwise, this is the starting file offset of the bio vecs in +* @bio, which must be contiguous. +* @one_ordered: If true, @bio only refers to one ordered extent. */ blk_status_t btrfs_csum_one_bio (struct btrfs_inode *inode,struct bio *bio, -u64 file_start,int contig) +u64 offset,bool one_ordered) {struct btrfs_fs_info *fs_info = inode->root->fs_info;SHASH_DESC_ON_STACK (shash, fs_info->csum_shash);struct btrfs_ordered_sum *sums;struct btrfs_ordered_extent *ordered =NULL ; +const bool page_offsets = (offset == (u64)-1 );char *data;struct bvec_iter iter;struct bio_vec bvec;int index; -int nr_sectors; +int blockcount;unsigned long total_bytes =0 ;unsigned long this_sum_bytes =0 ;int i; -u64 offset;unsigned nofs_flag;const u16 csum_size =btrfs_super_csum_size (fs_info->super_copy);
@@ -552,18 +552,13 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, sums->len = bio->bi_iter.bi_size;INIT_LIST_HEAD (&sums->list); -if (contig) - offset = file_start; -else - offset =0 ;/* shut up gcc */ - sums->bytenr = (u64 )bio->bi_iter.bi_sector <<9 ; index =0 ; shash->tfm = fs_info->csum_shash;bio_for_each_segment (bvec, bio, iter) { -if (!contig) +if (page_offsets) offset =page_offset (bvec.bv_page) + bvec.bv_offset;if (!ordered) {
@@ -571,13 +566,14 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio,BUG_ON (!ordered);/* Logic error */ } - nr_sectors =BTRFS_BYTES_TO_BLKS (fs_info, + blockcount =BTRFS_BYTES_TO_BLKS (fs_info, bvec.bv_len + fs_info->sectorsize -1 ); -for (i =0 ; i < nr_sectors; i++) { -if (offset >= ordered->file_offset + ordered->num_bytes || - offset < ordered->file_offset) { +for (i =0 ; i < blockcount; i++) { +if (!one_ordered && + (offset >= ordered->file_offset + ordered->num_bytes || + offset < ordered->file_offset)) {unsigned long bytes_left; sums->len = this_sum_bytes;
@@ -608,7 +604,8 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, sums->sums + index);kunmap_atomic (data); index += csum_size; - offset += fs_info->sectorsize; +if (!one_ordered) + offset += fs_info->sectorsize; this_sum_bytes += fs_info->sectorsize; total_bytes += fs_info->sectorsize; }
fs/btrfs/inode.c +4 -4
@@ -2162,7 +2162,7 @@ static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio, {struct inode *inode = private_data; -return btrfs_csum_one_bio (BTRFS_I (inode), bio,0 ,0 ); +return btrfs_csum_one_bio (BTRFS_I (inode), bio, (u64)-1 , false); }/*
@@ -2225,7 +2225,7 @@ static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, 0, inode, btrfs_submit_bio_start); goto out; }else if (!skip_sum) { - ret =btrfs_csum_one_bio (BTRFS_I (inode), bio,0 ,0 ); + ret =btrfs_csum_one_bio (BTRFS_I (inode), bio, (u64)-1 , false);if (ret) goto out; }
@@ -7609,7 +7609,7 @@ static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data, {struct inode *inode = private_data; -return btrfs_csum_one_bio (BTRFS_I (inode), bio, offset,1 ); +return btrfs_csum_one_bio (BTRFS_I (inode), bio, offset, false); }static void btrfs_end_dio_bio (struct bio *bio)
@@ -7668,7 +7668,7 @@ static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,* If we aren't doing async submit, calculate the csum of the * bio now. */ - ret =btrfs_csum_one_bio (BTRFS_I (inode), bio, file_offset,1 ); + ret =btrfs_csum_one_bio (BTRFS_I (inode), bio, file_offset, false);if (ret) goto err; }else {
Signature · 1 line
2.28.0
Re: [PATCH v5 4/9] btrfs: don't advance offset for compressed bios in btrfs_csum_one_bio()
Quoted 19 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
btrfs_csum_one_bio()loops over each filesystem block in the bio while keeping a cursor of its current logical position in the file in order to look up the ordered extent to add the checksums to. However, this doesn't make much sense for compressed extents, as a sector on disk does not correspond to a sector of decompressed file data. It happens to work because 1) the compressed bio always covers one ordered extent and 2) the size of the bio is always less than the size of the ordered extent. However, the second point will not always be true for encoded writes.Let's add a boolean parameter to
btrfs_csum_one_bio()to indicate that it can assume that the bio only covers one ordered extent. Since we're already changing the signature, let's get rid of the contig parameter and make it implied by the offset parameter, similar to the change we recently made tobtrfs_lookup_bio_sums(). Additionally, let's renamenr_sectorsto blockcount to make it clear that it's the number of filesystem blocks, not the number of 512-byte sectors.
Quoted 1 line
Signed-off-by Omar Sandoval <[email protected]>
Thanks,
Josef
PATCH v5 5/9 btrfs: add ram_bytes and offset to btrfs_ordered_extent
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Currently, we only create ordered extents when ram_bytes == num_bytes and offset == 0. However, RWF_ENCODED writes may create extents which only refer to a subset of the full unencoded extent, so we need to plumb these fields through the ordered extent infrastructure and pass them down to insert_reserved_file_extent().
Since we're changing the btrfs_add_ordered_extent* signature, let's get rid of the trivial wrappers and add a kernel-doc.
--- fs/btrfs/inode.c | 56 ++++++++++++++++++--------------- fs/btrfs/ordered-data.c | 68 ++++++++++++++++------------------------- fs/btrfs/ordered-data.h | 16 ++++------ 3 files changed, 64 insertions(+), 76 deletions(-)
fs/btrfs/inode.c +32 -24
@@ -869,13 +869,12 @@ static noinline void submit_compressed_extents(struct async_chunk *async_chunk) goto out_free_reserve;free_extent_map (em); - ret =btrfs_add_ordered_extent_compress (inode, - async_extent->start, - ins.objectid, - async_extent->ram_size, - ins.offset, -BTRFS_ORDERED_COMPRESSED , - async_extent->compress_type); + ret =btrfs_add_ordered_extent (inode, async_extent->start, + async_extent->ram_size, + async_extent->ram_size, + ins.objectid, ins.offset,0 , +1 <<BTRFS_ORDERED_COMPRESSED , + async_extent->compress_type);if (ret) { btrfs_drop_extent_cache(inode, async_extent->start, async_extent->start +
@@ -1083,8 +1082,9 @@ static noinline int cow_file_range(struct btrfs_inode *inode, }free_extent_map (em); - ret =btrfs_add_ordered_extent (inode, start, ins.objectid, - ram_size, cur_alloc_size,0 ); + ret =btrfs_add_ordered_extent (inode, start, ram_size, ram_size, + ins.objectid, cur_alloc_size,0 , +0 ,BTRFS_COMPRESS_NONE );if (ret) goto out_drop_extent_cache;
@@ -1716,10 +1716,11 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, goto error; }free_extent_map (em); - ret =btrfs_add_ordered_extent (inode, cur_offset, - disk_bytenr, num_bytes, - num_bytes, -BTRFS_ORDERED_PREALLOC ); + ret =btrfs_add_ordered_extent (inode, + cur_offset, num_bytes, num_bytes, + disk_bytenr, num_bytes,0 , +1 <<BTRFS_ORDERED_PREALLOC , +BTRFS_COMPRESS_NONE );if (ret) { btrfs_drop_extent_cache(inode, cur_offset, cur_offset + num_bytes - 1,
@@ -1728,9 +1729,11 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, } }else { ret =btrfs_add_ordered_extent (inode, cur_offset, + num_bytes, num_bytes, disk_bytenr, num_bytes, - num_bytes, -BTRFS_ORDERED_NOCOW ); +0 , +1 <<BTRFS_ORDERED_NOCOW , +BTRFS_COMPRESS_NONE );if (ret) goto error; }
@@ -2470,6 +2473,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,struct btrfs_key ins;u64 disk_num_bytes =btrfs_stack_file_extent_disk_num_bytes (stack_fi);u64 disk_bytenr =btrfs_stack_file_extent_disk_bytenr (stack_fi); +u64 offset =btrfs_stack_file_extent_offset (stack_fi);u64 num_bytes =btrfs_stack_file_extent_num_bytes (stack_fi);u64 ram_bytes =btrfs_stack_file_extent_ram_bytes (stack_fi);int extent_inserted =0 ;
@@ -2525,7 +2529,8 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, goto out; ret =btrfs_alloc_reserved_file_extent (trans, root,btrfs_ino (inode), - file_pos, qgroup_reserved, &ins); + file_pos - offset, + qgroup_reserved, &ins); out:btrfs_free_path (path);
@@ -2552,19 +2557,19 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, struct btrfs_ordered_extent *oe) {struct btrfs_file_extent_item stack_fi; -u64 logical_len; +u64 num_bytes = oe->num_bytes; +u64 ram_bytes = oe->ram_bytes;memset (&stack_fi,0 ,sizeof (stack_fi));btrfs_set_stack_file_extent_type (&stack_fi,BTRFS_FILE_EXTENT_REG );btrfs_set_stack_file_extent_disk_bytenr (&stack_fi, oe->disk_bytenr);btrfs_set_stack_file_extent_disk_num_bytes (&stack_fi, oe->disk_num_bytes); +btrfs_set_stack_file_extent_offset (&stack_fi, oe->offset);if (test_bit (BTRFS_ORDERED_TRUNCATED , &oe->flags)) - logical_len = oe->truncated_len; -else - logical_len = oe->num_bytes; -btrfs_set_stack_file_extent_num_bytes (&stack_fi, logical_len); -btrfs_set_stack_file_extent_ram_bytes (&stack_fi, logical_len); + num_bytes = ram_bytes = oe->truncated_len; +btrfs_set_stack_file_extent_num_bytes (&stack_fi, num_bytes); +btrfs_set_stack_file_extent_ram_bytes (&stack_fi, ram_bytes);btrfs_set_stack_file_extent_compression (&stack_fi, oe->compress_type);/* Encryption and other encoding is reserved and all 0 */
@@ -6892,8 +6897,11 @@ static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,if (IS_ERR (em)) goto out; } - ret =btrfs_add_ordered_extent_dio (inode, start, block_start, len, - block_len, type); + ret =btrfs_add_ordered_extent (inode, start, len, len, block_start, + block_len,0 , + (1 << type) | + (1 <<BTRFS_ORDERED_DIRECT ), +BTRFS_COMPRESS_NONE );if (ret) {if (em) {free_extent_map (em);
fs/btrfs/ordered-data.c +26 -42
104 lines not on this page · raw message
fs/btrfs/ordered-data.h +6 -10
@@ -72,9 +72,11 @@ struct btrfs_ordered_extent {* These fields directly correspond to the same fields in * btrfs_file_extent_item. */ -u64 disk_bytenr;u64 num_bytes; +u64 ram_bytes; +u64 disk_bytenr;u64 disk_num_bytes; +u64 offset;/* number of bytes that still need writing */ u64 bytes_left;
@@ -161,15 +163,9 @@ int btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode, u64 *file_offset, u64 io_size, int uptodate);int btrfs_add_ordered_extent (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes,u64 disk_num_bytes, -int type); -int btrfs_add_ordered_extent_dio (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes, -u64 disk_num_bytes,int type); -int btrfs_add_ordered_extent_compress (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes, -u64 disk_num_bytes,int type, -int compress_type); +u64 num_bytes,u64 ram_bytes,u64 disk_bytenr, +u64 disk_num_bytes,u64 offset,int flags, +int compress_type);void btrfs_add_ordered_sum (struct btrfs_ordered_extent *entry,struct btrfs_ordered_sum *sum); struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
Signature · 1 line
2.28.0
Re: [PATCH v5 5/9] btrfs: add ram_bytes and offset to btrfs_ordered_extent
Quoted 8 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Currently, we only create ordered extents when
ram_bytes==num_bytesand offset == 0. However,RWF_ENCODEDwrites may create extents which only refer to a subset of the full unencoded extent, so we need to plumb these fields through the ordered extent infrastructure and pass them down toinsert_reserved_file_extent().
Quoted 5 lines
Since we're changing the btrfs_add_ordered_extent* signature, let's get rid of the trivial wrappers and add a kernel-doc.
Reviewed-by Nikolay Borisov <[email protected]>Signed-off-by Omar Sandoval <[email protected]>
Thanks,
Josef
PATCH v5 6/9 btrfs: support different disk extent size for delalloc
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Currently, we always reserve the same extent size in the file and extent size on disk for delalloc because the former is the worst case for the latter. For RWF_ENCODED writes, we know the exact size of the extent on disk, which may be less than or greater than (for bookends) the size in the file. Add a disk_num_bytes parameter to btrfs_delalloc_reserve_metadata() so that we can reserve the correct amount of csum bytes. No functional change.
--- fs/btrfs/ctree.h | 3 ++- fs/btrfs/delalloc-space.c | 18 ++++++++++-------- fs/btrfs/file.c | 3 ++- fs/btrfs/inode.c | 3 ++- fs/btrfs/relocation.c | 4 ++-- 5 files changed, 18 insertions(+), 13 deletions(-)
fs/btrfs/ctree.h +2 -1
@@ -2626,7 +2626,8 @@ void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info, struct btrfs_block_rsv *rsv);void btrfs_delalloc_release_extents (struct btrfs_inode *inode,u64 num_bytes); -int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes); +int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes, +u64 disk_num_bytes);u64 btrfs_account_ro_block_groups_free_space (struct btrfs_space_info *sinfo);int btrfs_error_unpin_extent_range (struct btrfs_fs_info *fs_info,u64 start,u64 end);
fs/btrfs/delalloc-space.c +10 -8
@@ -265,11 +265,11 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info, }static void calc_inode_reservations (struct btrfs_fs_info *fs_info, -u64 num_bytes,u64 *meta_reserve, -u64 *qgroup_reserve) +u64 num_bytes,u64 disk_num_bytes, +u64 *meta_reserve,u64 *qgroup_reserve) {u64 nr_extents =count_max_extents (num_bytes); -u64 csum_leaves =btrfs_csum_bytes_to_leaves (fs_info, num_bytes); +u64 csum_leaves =btrfs_csum_bytes_to_leaves (fs_info, disk_num_bytes);u64 inode_update =btrfs_calc_metadata_size (fs_info,1 ); *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
@@ -283,7 +283,8 @@ static void calc_inode_reservations(struct btrfs_fs_info *fs_info, *qgroup_reserve = nr_extents * fs_info->nodesize; } -int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes) +int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes, +u64 disk_num_bytes) {struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info;
@@ -313,6 +314,7 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) } num_bytes =ALIGN (num_bytes, fs_info->sectorsize); + disk_num_bytes =ALIGN (disk_num_bytes, fs_info->sectorsize);/* * We always want to do it this way, every other way is wrong and ends
@@ -324,8 +326,8 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)* everything out and try again, which is bad. This way we just * over-reserve slightly, and clean up the mess when we are done. */ -calc_inode_reservations (fs_info, num_bytes, &meta_reserve, - &qgroup_reserve); +calc_inode_reservations (fs_info, num_bytes, disk_num_bytes, + &meta_reserve, &qgroup_reserve); ret =btrfs_qgroup_reserve_meta_prealloc (root, qgroup_reserve, true);if (ret)return ret;
@@ -344,7 +346,7 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)spin_lock (&inode->lock); nr_extents =count_max_extents (num_bytes);btrfs_mod_outstanding_extents (inode, nr_extents); - inode->csum_bytes += num_bytes; + inode->csum_bytes += disk_num_bytes;btrfs_calculate_inode_block_rsv_size (fs_info, inode);spin_unlock (&inode->lock);
@@ -448,7 +450,7 @@ int btrfs_delalloc_reserve_space(struct btrfs_inode *inode, ret =btrfs_check_data_free_space (inode, reserved, start, len);if (ret <0 )return ret; - ret =btrfs_delalloc_reserve_metadata (inode, len); + ret =btrfs_delalloc_reserve_metadata (inode, len, len);if (ret <0 )btrfs_free_reserved_data_space (inode, *reserved, start, len);return ret;
fs/btrfs/file.c +2 -1
@@ -1706,7 +1706,8 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,WARN_ON (reserve_bytes ==0 ); ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), - reserve_bytes); + reserve_bytes, + reserve_bytes);if (ret) {if (!only_release_metadata) btrfs_free_reserved_data_space(BTRFS_I(inode),
fs/btrfs/inode.c +2 -1
@@ -4546,7 +4546,8 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, goto out; } } - ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), blocksize); + ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), blocksize, + blocksize);if (ret <0 ) {if (!only_release_metadata) btrfs_free_reserved_data_space(BTRFS_I(inode),
fs/btrfs/relocation.c +2 -2
@@ -2705,8 +2705,8 @@ static int relocate_file_extent_cluster(struct inode *inode, index = (cluster->start - offset) >>PAGE_SHIFT ; last_index = (cluster->end - offset) >>PAGE_SHIFT ;while (index <= last_index) { - ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), -PAGE_SIZE ); + ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode),PAGE_SIZE , +PAGE_SIZE );if (ret) goto out;
Signature · 1 line
2.28.0
Re: [PATCH v5 6/9] btrfs: support different disk extent size for delalloc
Quoted 10 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Currently, we always reserve the same extent size in the file and extent size on disk for delalloc because the former is the worst case for the latter. For
RWF_ENCODEDwrites, we know the exact size of the extent on disk, which may be less than or greater than (for bookends) the size in the file. Add adisk_num_bytesparameter tobtrfs_delalloc_reserve_metadata()so that we can reserve the correct amount of csum bytes. No functional change.
Quoted 2 lines
Reviewed-by Nikolay Borisov <[email protected]>Signed-off-by Omar Sandoval <[email protected]>
Thanks,
Josef
PATCH v5 7/9 btrfs: optionally extend i_size in cow_file_range_inline()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Currently, an inline extent is always created after i_size is extended from btrfs_dirty_pages(). However, for encoded writes, we only want to update i_size after we successfully created the inline extent. Add an update_i_size parameter to cow_file_range_inline() and insert_inline_extent() and pass in the size of the extent rather than determining it from i_size. Since the start parameter is always passed as 0, get rid of it and simplify the logic in these two functions. While we're here, let's document the requirements for creating an inline extent.
--- fs/btrfs/inode.c | 100 +++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 52 deletions(-)
fs/btrfs/inode.c +48 -52
@@ -167,9 +167,10 @@ static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,static int insert_inline_extent (struct btrfs_trans_handle *trans,struct btrfs_path *path,int extent_inserted,struct btrfs_root *root,struct inode *inode, -u64 start,size_t size,size_t compressed_size, +size_t size,size_t compressed_size,int compress_type, -struct page **compressed_pages) +struct page **compressed_pages, +bool update_i_size) {struct extent_buffer *leaf;struct page *page =NULL ;
@@ -178,7 +179,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,struct btrfs_file_extent_item *ei;int ret;size_t cur_size = size; -unsigned long offset; +u64 i_size;ASSERT ((compressed_size >0 && compressed_pages) || (compressed_size ==0 && !compressed_pages));
@@ -193,7 +194,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,size_t datasize; key.objectid =btrfs_ino (BTRFS_I (inode)); - key.offset = start; + key.offset =0 ; key.type =BTRFS_EXTENT_DATA_KEY ; datasize =btrfs_file_extent_calc_inline_size (cur_size);
@@ -232,12 +233,10 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,btrfs_set_file_extent_compression (leaf, ei, compress_type); }else { - page =find_get_page (inode->i_mapping, - start >>PAGE_SHIFT ); + page =find_get_page (inode->i_mapping,0 );btrfs_set_file_extent_compression (leaf, ei,0 ); kaddr =kmap_atomic (page); - offset =offset_in_page (start); -write_extent_buffer (leaf, kaddr + offset, ptr, size); +write_extent_buffer (leaf, kaddr, ptr, size);kunmap_atomic (kaddr);put_page (page); }
@@ -248,8 +247,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* We align size to sectorsize for inline extents just for simplicity * sake. */ - size =ALIGN (size, root->fs_info->sectorsize); - ret =btrfs_inode_set_file_extent_range (BTRFS_I (inode), start, size); + ret =btrfs_inode_set_file_extent_range (BTRFS_I (inode),0 , +ALIGN (size, root->fs_info->sectorsize));if (ret) goto fail;
@@ -262,7 +261,12 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* before we unlock the pages. Otherwise we * could end up racing with unlink. */ -BTRFS_I (inode)->disk_i_size = inode->i_size; + i_size =i_size_read (inode); +if (update_i_size && size > i_size) { +i_size_write (inode, size); + i_size = size; + } +BTRFS_I (inode)->disk_i_size = i_size; ret =btrfs_update_inode (trans, root, inode); fail:
@@ -275,36 +279,32 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* does the checks required to make sure the data is small enough * to fit as an inline extent. */ -static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, -u64 end,size_t compressed_size, +static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, +size_t compressed_size,int compress_type, -struct page **compressed_pages) +struct page **compressed_pages, +bool update_i_size) {struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_trans_handle *trans; -u64 isize =i_size_read (&inode->vfs_inode); -u64 actual_end =min (end +1 , isize); -u64 inline_len = actual_end - start; -u64 aligned_end =ALIGN (end, fs_info->sectorsize); -u64 data_len = inline_len; +u64 data_len = compressed_size ? compressed_size : size;int ret;struct btrfs_path *path;int extent_inserted =0 ;u32 extent_item_size; -if (compressed_size) - data_len = compressed_size; - -if (start >0 || - actual_end > fs_info->sectorsize || +/* +* We can create an inline extent if it ends at or beyond the current +* i_size, is no larger than a sector (decompressed), and the (possibly +* compressed) data fits in a leaf and the configured maximum inline +* size. +*/ +if (size <i_size_read (&inode->vfs_inode) || + size > fs_info->sectorsize || data_len >BTRFS_MAX_INLINE_DATA_SIZE (fs_info) || - (!compressed_size && - (actual_end & (fs_info->sectorsize -1 )) ==0 ) || - end +1 < isize || - data_len > fs_info->max_inline) { + data_len > fs_info->max_inline)return 1 ; - } path =btrfs_alloc_path ();if (!path)
@@ -317,27 +317,19 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, } trans->block_rsv = &inode->block_rsv; -if (compressed_size && compressed_pages) - extent_item_size =btrfs_file_extent_calc_inline_size ( - compressed_size); -else - extent_item_size =btrfs_file_extent_calc_inline_size ( - inline_len); - - ret =__btrfs_drop_extents (trans, root, inode, path, start, aligned_end, -NULL ,1 ,1 , extent_item_size, - &extent_inserted); + extent_item_size =btrfs_file_extent_calc_inline_size (data_len); + ret =__btrfs_drop_extents (trans, root, inode, path,0 , + fs_info->sectorsize,NULL ,1 ,1 , + extent_item_size, &extent_inserted);if (ret) {btrfs_abort_transaction (trans, ret); goto out; } -if (isize > actual_end) - inline_len =min_t (u64, isize, actual_end); - ret =insert_inline_extent (trans, path, extent_inserted, - root, &inode->vfs_inode, start, - inline_len, compressed_size, - compress_type, compressed_pages); + ret =insert_inline_extent (trans, path, extent_inserted, root, + &inode->vfs_inode, size, compressed_size, + compress_type, compressed_pages, + update_i_size);if (ret && ret != -ENOSPC ) {btrfs_abort_transaction (trans, ret); goto out;
@@ -347,7 +339,7 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, }set_bit (BTRFS_INODE_NEEDS_FULL_SYNC , &inode->runtime_flags); -btrfs_drop_extent_cache (inode, start, aligned_end -1 ,0 ); +btrfs_drop_extent_cache (inode,0 , fs_info->sectorsize -1 ,0 ); out:/* * Don't forget to free the reserved space, as for inlined extent
@@ -618,14 +610,15 @@ static noinline int compress_file_range(struct async_chunk *async_chunk)/* we didn't compress the entire range, try * to make an uncompressed inline extent. */ - ret =cow_file_range_inline (BTRFS_I (inode), start, end, + ret =cow_file_range_inline (BTRFS_I (inode), actual_end,0 ,BTRFS_COMPRESS_NONE , -NULL ); +NULL , false); }else {/* try making a compressed inline extent */ - ret =cow_file_range_inline (BTRFS_I (inode), start, end, + ret =cow_file_range_inline (BTRFS_I (inode), actual_end, total_compressed, - compress_type, pages); + compress_type, pages, + false); }if (ret <=0 ) { unsigned long clear_flags = EXTENT_DELALLOC |
@@ -1013,9 +1006,12 @@ static noinline int cow_file_range(struct btrfs_inode *inode,inode_should_defrag (inode, start, end, num_bytes,SZ_64K );if (start ==0 ) { +u64 actual_end =min_t (u64,i_size_read (&inode->vfs_inode), + end +1 ); +/* lets try to make an inline extent */ - ret =cow_file_range_inline (inode, start, end,0 , -BTRFS_COMPRESS_NONE ,NULL ); + ret =cow_file_range_inline (inode, actual_end,0 , +BTRFS_COMPRESS_NONE ,NULL , false);if (ret ==0 ) {/* * We use DO_ACCOUNTING here because we need the
Signature · 1 line
2.28.0
Re: [PATCH v5 7/9] btrfs: optionally extend i_size in cow_file_range_inline()
Quoted 12 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Currently, an inline extent is always created after
i_sizeis extended frombtrfs_dirty_pages(). However, for encoded writes, we only want to updatei_sizeafter we successfully created the inline extent. Add anupdate_i_sizeparameter tocow_file_range_inline()andinsert_inline_extent()and pass in the size of the extent rather than determining it fromi_size. Since the start parameter is always passed as 0, get rid of it and simplify the logic in these two functions. While we're here, let's document the requirements for creating an inline extent.
Quoted 1 line
Signed-off-by Omar Sandoval <[email protected]>
Thanks,
Josef
PATCH v5 8/9 btrfs: implement RWF_ENCODED reads
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
There are 4 main cases:
1. Inline extents: we copy the data straight out of the extent buffer.
2. Hole/preallocated extents: we fill in zeroes.
3. Regular, uncompressed extents: we read the sectors we need directly from disk.
4. Regular, compressed extents: we read the entire compressed extent from disk and indicate what subset of the decompressed extent is in the file.
This initial implementation simplifies a few things that can be improved in the future:
- We hold the inode lock during the operation.
- Cases 1, 3, and 4 allocate temporary memory to read into before copying out to userspace.
--- fs/btrfs/ctree.h | 2 + fs/btrfs/file.c | 12 +- fs/btrfs/inode.c | 491 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 504 insertions(+), 1 deletion(-)
fs/btrfs/ctree.h +2 -0
@@ -3020,6 +3020,8 @@ int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_pageint btrfs_writepage_cow_fixup (struct page *page,u64 start,u64 end);void btrfs_writepage_endio_finish_ordered (struct page *page,u64 start,u64 end,int uptodate); +ssize_t btrfs_encoded_read (struct kiocb *iocb,struct iov_iter *iter); +extern const struct dentry_operations btrfs_dentry_operations;/* ioctl.c */
fs/btrfs/file.c +11 -1
@@ -379,6 +379,16 @@ int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)return 0 ; } +static ssize_t btrfs_file_read_iter (struct kiocb *iocb,struct iov_iter *iter) +{ +if (iocb->ki_flags &IOCB_ENCODED ) { +if (iocb->ki_flags &IOCB_NOWAIT ) +return -EOPNOTSUPP ; +return btrfs_encoded_read (iocb, iter); + } +return generic_file_read_iter (iocb, iter); +} +/* simple helper to fault in pages and copy. This should go away * and be replaced with calls into generic code. */
@@ -3571,7 +3581,7 @@ static int btrfs_file_open(struct inode *inode, struct file *filp)const struct file_operations btrfs_file_operations = { .llseek = btrfs_file_llseek, - .read_iter = generic_file_read_iter, + .read_iter = btrfs_file_read_iter, .splice_read = generic_file_splice_read, .write_iter = btrfs_file_write_iter, .splice_write = iter_file_splice_write,
fs/btrfs/inode.c +491 -0
497 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v5 8/9] btrfs: implement RWF_ENCODED reads
Quoted 20 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
There are 4 main cases:
1. Inline extents: we copy the data straight out of the extent buffer.
2. Hole/preallocated extents: we fill in zeroes.
3. Regular, uncompressed extents: we read the sectors we need directly from disk.
4. Regular, compressed extents: we read the entire compressed extent from disk and indicate what subset of the decompressed extent is in the file.This initial implementation simplifies a few things that can be improved in the future:
- We hold the inode lock during the operation.
- Cases 1, 3, and 4 allocate temporary memory to read into before copying out to userspace.Signed-off-by Omar Sandoval <[email protected]>
<snip>
fs/btrfs/inode.c void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) · line 9907
Quoted 7 lines
fs/btrfs/inode.c +7
-0+ tmp =kmalloc (count,GFP_NOFS ); +if (!tmp) { + ret = -ENOMEM ; + goto out; + } +read_extent_buffer (leaf, tmp, ptr, count); +btrfs_free_path (path);
Why not just btrfs_release_path(), you're freeing below anyway.
Also a mention that we're explicitly not handling read repairs and backup copies would be good in the changelog as well. Thanks,
Josef
Re: [PATCH v5 8/9] btrfs: implement RWF_ENCODED reads
Quoted 25 lines · Josef Bacik
Quoted 20 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
There are 4 main cases:
1. Inline extents: we copy the data straight out of the extent buffer.
2. Hole/preallocated extents: we fill in zeroes.
3. Regular, uncompressed extents: we read the sectors we need directly from disk.
4. Regular, compressed extents: we read the entire compressed extent from disk and indicate what subset of the decompressed extent is in the file.This initial implementation simplifies a few things that can be improved in the future:
- We hold the inode lock during the operation.
- Cases 1, 3, and 4 allocate temporary memory to read into before copying out to userspace.Signed-off-by Omar Sandoval <[email protected]><snip>
Quoted 9 lines
fs/btrfs/inode.c void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) · line 9907
Quoted 7 lines
fs/btrfs/inode.c +7
-0+ tmp =kmalloc (count,GFP_NOFS ); +if (!tmp) { + ret = -ENOMEM ; + goto out; + } +read_extent_buffer (leaf, tmp, ptr, count); +btrfs_free_path (path);Why not just
btrfs_release_path(), you're freeing below anyway.
Oh that's nicer, fixed.
Quoted 4 lines
Also a mention that we're explicitly not handling read repairs and backup copies would be good in the changelog as well. Thanks,
Josef
Will do, thanks.
PATCH v5 9/9 btrfs: implement RWF_ENCODED writes
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
The implementation resembles direct I/O: we have to flush any ordered extents, invalidate the page cache, and do the io tree/delalloc/extent map/ordered extent dance. From there, we can reuse the compression code with a minor modification to distinguish the write from writeback. This also creates inline extents when possible.
Now that read and write are implemented, this also sets the FMODE_ENCODED_IO flag in btrfs_file_open().
--- fs/btrfs/compression.c | 7 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 2 + fs/btrfs/file.c | 40 +++++-- fs/btrfs/inode.c | 246 +++++++++++++++++++++++++++++++++++++++- fs/btrfs/ordered-data.c | 12 +- fs/btrfs/ordered-data.h | 2 + 7 files changed, 298 insertions(+), 17 deletions(-)
fs/btrfs/compression.c +5 -2
@@ -338,7 +338,8 @@ static void end_compressed_bio_write(struct bio *bio) bio->bi_status == BLK_STS_OK); cb->compressed_pages[0 ]->mapping =NULL ; -end_compressed_writeback (inode, cb); +if (cb->writeback) +end_compressed_writeback (inode, cb);/* note, our inode could be gone now */ /*
@@ -374,7 +375,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,struct page **compressed_pages, unsigned long nr_pages, unsigned int write_flags, - struct cgroup_subsys_state *blkcg_css) + struct cgroup_subsys_state *blkcg_css, + bool writeback) { struct btrfs_fs_info *fs_info = inode->root->fs_info;struct bio *bio =NULL ;
@@ -398,6 +400,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start, cb->mirror_num =0 ; cb->compressed_pages = compressed_pages; cb->compressed_len = compressed_len; + cb->writeback = writeback; cb->orig_bio =NULL ; cb->nr_pages = nr_pages;
fs/btrfs/compression.h +5 -1
@@ -49,6 +49,9 @@ struct compressed_bio {/* the compression algorithm for this bio */ int compress_type; +/* Whether this is a write for writeback. */ +bool writeback; +/* number of compressed pages in the array */ unsigned long nr_pages;
@@ -96,7 +99,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,struct page **compressed_pages, unsigned long nr_pages, unsigned int write_flags, - struct cgroup_subsys_state *blkcg_css); + struct cgroup_subsys_state *blkcg_css, + bool writeback);blk_status_t btrfs_submit_compressed_read (struct inode *inode,struct bio *bio,int mirror_num,unsigned long bio_flags);
fs/btrfs/ctree.h +2 -0
@@ -3021,6 +3021,8 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end);void btrfs_writepage_endio_finish_ordered (struct page *page,u64 start,u64 end,int uptodate);ssize_t btrfs_encoded_read (struct kiocb *iocb,struct iov_iter *iter); +ssize_t btrfs_encoded_write (struct kiocb *iocb,struct iov_iter *from, +struct encoded_iov *encoded);extern const struct dentry_operations btrfs_dentry_operations;
fs/btrfs/file.c +31 -9
@@ -1929,8 +1929,7 @@ static void update_time_for_write(struct inode *inode)inode_inc_iversion (inode); } -static ssize_t btrfs_file_write_iter (struct kiocb *iocb, -struct iov_iter *from) +static ssize_t btrfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from) {struct file *file = iocb->ki_filp;struct inode *inode =file_inode (file);
@@ -1940,16 +1939,24 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,u64 end_pos;ssize_t num_written =0 ;const bool sync = iocb->ki_flags &IOCB_DSYNC ; +struct encoded_iov encoded;ssize_t err;loff_t pos;size_t count;loff_t oldsize;int clean_page =0 ; -if (!(iocb->ki_flags &IOCB_DIRECT ) && - (iocb->ki_flags &IOCB_NOWAIT )) +if ((iocb->ki_flags &IOCB_NOWAIT ) && + (!(iocb->ki_flags &IOCB_DIRECT ) || + (iocb->ki_flags &IOCB_ENCODED )))return -EOPNOTSUPP ; +if (iocb->ki_flags &IOCB_ENCODED ) { + err =copy_encoded_iov_from_iter (&encoded, from); +if (err) +return err; + } +if (iocb->ki_flags &IOCB_NOWAIT ) {if (!inode_trylock (inode))return -EAGAIN ;
@@ -1957,14 +1964,27 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,inode_lock (inode); } - err =generic_write_checks (iocb, from); -if (err <=0 ) { +if (iocb->ki_flags &IOCB_ENCODED ) { + err =generic_encoded_write_checks (iocb, &encoded); +if (err) { +inode_unlock (inode); +return err; + } + count = encoded.len; + }else { + err =generic_write_checks (iocb, from); +if (err <0 ) { +inode_unlock (inode); +return err; + } + count =iov_iter_count (from); + } +if (count ==0 ) {inode_unlock (inode);return err; } pos = iocb->ki_pos; - count =iov_iter_count (from);if (iocb->ki_flags &IOCB_NOWAIT ) {size_t nocow_bytes = count;
@@ -2033,7 +2053,9 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,if (sync)atomic_inc (&BTRFS_I (inode)->sync_writers); -if (iocb->ki_flags &IOCB_DIRECT ) { +if (iocb->ki_flags &IOCB_ENCODED ) { + num_written =btrfs_encoded_write (iocb, from, &encoded); + }else if (iocb->ki_flags &IOCB_DIRECT ) { num_written =__btrfs_direct_write (iocb, from); }else { num_written =btrfs_buffered_write (iocb, from);
@@ -3575,7 +3597,7 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)static int btrfs_file_open (struct inode *inode,struct file *filp) { - filp->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC ; + filp->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC |FMODE_ENCODED_IO ;return generic_file_open (inode, filp); }
fs/btrfs/inode.c +244 -2
264 lines not on this page · raw message
fs/btrfs/ordered-data.c +9 -3
@@ -462,9 +462,15 @@ void btrfs_remove_ordered_extent(struct inode *inode,spin_lock (&btrfs_inode->lock);btrfs_mod_outstanding_extents (btrfs_inode,-1 );spin_unlock (&btrfs_inode->lock); -if (root != fs_info->tree_root) -btrfs_delalloc_release_metadata (btrfs_inode, entry->num_bytes, - false); +if (root != fs_info->tree_root) { +u64 release; + +if (test_bit (BTRFS_ORDERED_ENCODED , &entry->flags)) + release = entry->disk_num_bytes; +else + release = entry->num_bytes; +btrfs_delalloc_release_metadata (btrfs_inode, release, false); + }if (test_bit (BTRFS_ORDERED_DIRECT , &entry->flags)) percpu_counter_add_batch(&fs_info->dio_bytes, -entry->num_bytes,
fs/btrfs/ordered-data.h +2 -0
@@ -62,6 +62,8 @@ enum {BTRFS_ORDERED_LOGGED_CSUM ,/* We wait for this extent to complete in the current transaction */ BTRFS_ORDERED_PENDING , +/* RWF_ENCODED I/O */ + BTRFS_ORDERED_ENCODED, }; struct btrfs_ordered_extent {
Signature · 1 line
2.28.0
Re: [PATCH v5 9/9] btrfs: implement RWF_ENCODED writes
Quoted 22 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
The implementation resembles direct I/O: we have to flush any ordered extents, invalidate the page cache, and do the io tree/delalloc/extent map/ordered extent dance. From there, we can reuse the compression code with a minor modification to distinguish the write from writeback. This also creates inline extents when possible.
Now that read and write are implemented, this also sets the
FMODE_ENCODED_IOflag inbtrfs_file_open().Signed-off-by Omar Sandoval <[email protected]>--- fs/btrfs/compression.c | 7 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 2 + fs/btrfs/file.c | 40 +++++-- fs/btrfs/inode.c | 246 +++++++++++++++++++++++++++++++++++++++- fs/btrfs/ordered-data.c | 12 +- fs/btrfs/ordered-data.h | 2 + 7 files changed, 298 insertions(+), 17 deletions(-)
<snip>
fs/btrfs/inode.c ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter) · line 10456
Quoted 12 lines
fs/btrfs/inode.c +12
-0+ + ret =btrfs_alloc_data_chunk_ondemand (BTRFS_I (inode), disk_num_bytes); +if (ret) + goto out_unlock; + ret =btrfs_qgroup_reserve_data (BTRFS_I (inode), &data_reserved, start, + num_bytes); +if (ret) + goto out_free_data_space; + ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), num_bytes, + disk_num_bytes); +if (ret) + goto out_qgroup_free_data;
This can just be btrfs_delalloc_reserve_space() and that way the error handling is much cleaner.
<snip>
fs/btrfs/inode.c ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter) · line 10526
Quoted 8 lines
fs/btrfs/inode.c +8
-0+ +out_free_reserved: +btrfs_dec_block_group_reservations (fs_info, ins.objectid); +btrfs_free_reserved_extent (fs_info, ins.objectid, ins.offset,1 ); +out_delalloc_release: +btrfs_delalloc_release_extents (BTRFS_I (inode), num_bytes); +btrfs_delalloc_release_metadata (BTRFS_I (inode), disk_num_bytes, + ret <0 );
Likewise this can all just be btrfs_free_reserved_data_space(). Thanks,
Josef
Re: [PATCH v5 9/9] btrfs: implement RWF_ENCODED writes
Quoted 54 lines · Josef Bacik
Quoted 22 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
The implementation resembles direct I/O: we have to flush any ordered extents, invalidate the page cache, and do the io tree/delalloc/extent map/ordered extent dance. From there, we can reuse the compression code with a minor modification to distinguish the write from writeback. This also creates inline extents when possible.
Now that read and write are implemented, this also sets the
FMODE_ENCODED_IOflag inbtrfs_file_open().Signed-off-by Omar Sandoval <[email protected]>--- fs/btrfs/compression.c | 7 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 2 + fs/btrfs/file.c | 40 +++++-- fs/btrfs/inode.c | 246 +++++++++++++++++++++++++++++++++++++++- fs/btrfs/ordered-data.c | 12 +- fs/btrfs/ordered-data.h | 2 + 7 files changed, 298 insertions(+), 17 deletions(-)<snip>
fs/btrfs/inode.c ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter) · line 10456
Quoted 12 lines
fs/btrfs/inode.c +12
-0+ + ret =btrfs_alloc_data_chunk_ondemand (BTRFS_I (inode), disk_num_bytes); +if (ret) + goto out_unlock; + ret =btrfs_qgroup_reserve_data (BTRFS_I (inode), &data_reserved, start, + num_bytes); +if (ret) + goto out_free_data_space; + ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), num_bytes, + disk_num_bytes); +if (ret) + goto out_qgroup_free_data;This can just be
btrfs_delalloc_reserve_space()and that way the error handling is much cleaner.<snip>
fs/btrfs/inode.c ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter) · line 10526
Quoted 8 lines
fs/btrfs/inode.c +8
-0+ +out_free_reserved: +btrfs_dec_block_group_reservations (fs_info, ins.objectid); +btrfs_free_reserved_extent (fs_info, ins.objectid, ins.offset,1 ); +out_delalloc_release: +btrfs_delalloc_release_extents (BTRFS_I (inode), num_bytes); +btrfs_delalloc_release_metadata (BTRFS_I (inode), disk_num_bytes, + ret <0 );Likewise this can all just be
btrfs_free_reserved_data_space(). Thanks,
Quoted 1 line
Josef
btrfs_delalloc_reserve_space() and btrfs_free_reserved_data_space() assume that num_bytes == disk_num_bytes, which isn't true for RWF_ENCODED.
I figured it'd be cleaner to open-code this special case in the one place that it's needed, but I could also add explicit num_bytes and disk_num_bytes arguments to btrfs_delalloc_reserve_space() and btrfs_free_reserved_data_space(). They'd just be equal everywhere except for here.
If you're fine with keeping it this way, I'll add a comment explaining why we can't use the higher-level helpers.