Direct IO for holes and fallocate
RFC PATCH 1/2 Direct IO for holes and fallocate
Currently DIO VFS code passes create = 0 flag for write to the middle of file. It does this to avoid block allocation for holes, to prevent expose stale data out when there is parallel buffered read (which does not hold the i_mutex lock) while direct IO write has not completed. DIO request on holes finally falls back to buffered IO for this reason.
Since preallocated extents are treated as holes when do a get_block() look up (buffer is not mapped), thus direct IO over fallocate also falls back to buffered IO. Thus ext4 actually silently falls back to buffered IO in above two cases.
The basic idea to fix the direct IO on fallocate issue is to map the fallocated extents on direct IO write before submit the IO, but wait until the direct IO complete then convert the unwritten extent to written extent, using an end_io call back function. We will need to split the unwritten extents before submit the IO to prevent later ENOSPC, and when IO complete, just mark the written part initialized.
With the end_io call back function, now it's possible to do direct IO on holes. For ext4 extent based file, since we support fallocate, we could fallocate blocks for holes, mark it mapped but unwritten. This way parallel buffered IO read returns zeros when parallel DIO write to the hole has not completed. When direct IO write complete, using the same end_io schemem to convert those fallocated hole to written extents.
For direct IO write to the end of file, we now also could get rid of using orphan list to protect expose stale data out after crash, when direct write to end of file isn't complete. We now fallocate blocks for the direct IO write to the end of file as well, and convert those fallocated blocks at the end of file to written when IO is complete. If fs crashed before the IO complete, it will only seen the file tail has been fallocated but won't get the stale data out.
1) Block allocation needed for DIO write are fallocated, including holes and file tail write, marked as unwritten extents after block allocation.
2) those unwritten extents, and fallocate extents, will be converted to written extents (and update disk size when write to end of file)when the IO is complete. The conversion is triggered using end_io call back function passing from ext4 fs to direct IO.
3) For already fallocated extent, at the time try to map the fallocated extent, we will split the fallocated extent as necessary, mark the to-write fallocated extent mapped but still remains unwritten, insert the splitted extents, to prevent ENOSPC later.
This first patch does 1) and 2), the second patch does 3)
Patch against ext4 patch queue.
Comments?
--- fs/ext4/ext4.h | 18 ++++ fs/ext4/inode.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- fs/ext4/super.c | 11 ++ 3 files changed, 237 insertions(+), 3 deletions(-)
fs/ext4/ext4.h +16 -2
@@ -111,6 +111,15 @@ struct ext4_allocation_request {unsigned int flags; }; +typedef struct ext4_io_end { +struct inode *inode;/* file being written to */ +unsigned int type;/* unwritten or written */ +int error;/* I/O error code */ +ext4_lblk_t offset;/* offset in the file */ +size_t size;/* size of the extent */ +struct work_struct work;/* data work queue */ +}ext4_io_end_t ; +/* * Special inodes numbers */
@@ -330,8 +339,8 @@ struct ext4_new_group_data {/* Call ext4_da_update_reserve_space() after successfully allocating the blocks */ #define EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE 0x0008 - - +#define EXT4_GET_BLOCKS_DIO_CREATE_EXT 0x0011 +#define EXT4_GET_BLOCKS_DIO_CONVERT_EXT 0x0021/* * ioctl commands */
@@ -960,6 +969,9 @@ struct ext4_sb_info {unsigned int s_log_groups_per_flex;struct flex_groups *s_flex_groups; + +/* workqueue for dio unwritten */ +struct workqueue_struct *dio_unwritten_wq; };static inline struct ext4_sb_info *EXT4_SB (struct super_block *sb)
@@ -1650,6 +1662,8 @@ extern void ext4_ext_init(struct super_bextern void ext4_ext_release (struct super_block *);extern long ext4_fallocate (struct inode *inode,int mode,loff_t offset,loff_t len); +extern int ext4_convert_unwritten_extents (struct inode *inode,loff_t offset, +loff_t len);extern int ext4_get_blocks (handle_t *handle,struct inode *inode,sector_t block,unsigned int max_blocks,struct buffer_head *bh,int flags);
fs/ext4/super.c +11 -0
@@ -578,6 +578,9 @@ static void ext4_put_super(struct super_struct ext4_super_block *es = sbi->s_es;int i, err; +flush_workqueue (sbi->dio_unwritten_wq); +destroy_workqueue (sbi->dio_unwritten_wq); +lock_super (sb);lock_kernel ();if (sb->s_dirt)
@@ -2781,6 +2784,12 @@ no_journal:clear_opt (sbi->s_mount_opt,NOBH ); } } +EXT4_SB (sb)->dio_unwritten_wq =create_workqueue ("ext4-dio-unwritten" ); +if (!EXT4_SB (sb)->dio_unwritten_wq) { +printk (KERN_ERR "EXT4-fs: failed to create DIO workqueue\n" ); + goto failed_mount_wq; + } +/* * The jbd2_journal_load will have done any necessary log recovery, * so we can safely mount the rest of the filesystem now.
@@ -2893,6 +2902,8 @@ cantfind_ext4: failed_mount4:ext4_msg (sb,KERN_ERR ,"mount failed" ); +destroy_workqueue (EXT4_SB (sb)->dio_unwritten_wq); +failed_mount_wq:ext4_release_system_zone (sb);if (sbi->s_journal) {jbd2_journal_destroy (sbi->s_journal);
fs/ext4/inode.c +210 -1
235 lines not on this page · raw message
Theodore Tso
maintainerOK, here are some comments on the patch; apologies for not getting to it sooner.
First of all, I suggest the following replacement for the patch description. I've rewritten it to make it clearer and more succint. Do you think I've left anything out?
---------------
ext4: Use end_io call back to avoid direct I/O fallback to buffered I/O
From: Mingming <[email protected]>
Currently the DIO VFS code passes create = 0 when writing to the middle of file. It does this to avoid block allocation for holes, so as not to expose stale data out when there is a parallel buffered read (which does not hold the i_mutex lock). Direct I/O writes into holes falls back to buffered IO for this reason.
Since preallocated extents are treated as holes when doing a get_block() look up (buffer is not mapped), direct IO over fallocate also falls back to buffered IO. Thus ext4 actually silently falls back to buffered IO in above two cases, which is undesirable.
To fix this, this patch creates unitialized extents when a direct I/O write needs to allocate blocks for writes that extend a file or writes into holes in sparse files, and registering an end_io callback which converts the uninitialized extent to an initialized extent after the I/O is completed.
-------------------
Secondly, the patch doesn't compile after applying just the first patch. The reason for it is that first patch references ext4_convert_unwritten_extents(), but it is only defined in the 2nd patch.
Other issues:
Quoted 1 line
+typedef struct ext4_io_end{
^^^ add a space
fs/ext4/ext4.h struct ext4_allocation_request { · line 121
Quoted 7 lines
fs/ext4/ext4.h +7
-0+struct inode *inode;/* file being written to */ +unsigned int type;/* unwritten or written */ +int error;/* I/O error code */ +ext4_lblk_t offset;/* offset in the file */ +size_t size;/* size of the extent */ +struct work_struct work;/* data work queue */ +}ext4_io_end_t;
^^^ add a space
fs/ext4/ext4.h struct ext4_new_group_data { · line 345
Quoted 6 lines
fs/ext4/ext4.h +2
-2- - +#define EXT4_GET_BLOCKS_DIO_CREATE_EXT 0x0011 +#define EXT4_GET_BLOCKS_DIO_CONVERT_EXT 0x0021/* * ioctl commands
Could you add a comment for EXT4_GET_BLOCKS_DIO_CREATE_EXT and EXT4_GET_BLOCKS_DIO_CONVERT_EXT, like the other EXT4_GET_BLOCKS #define's? And a empty line before the "ioctl commands" comment would be much appreciated.
Quoted 3 lines
/*
+ *O_DIRECTfor ext3 (or indirect map) based files
+ *
Probably better just to say "O_DIRECT for direct/indirect block mapped files"
Quoted 2 lines
+struct
workqueue_struct*ext4_unwritten_queue;
This doesn't appear to be used; it looks like you started with a single global workqueue, and then moved to having a separate workqueue for each filesystem.
Quoted 1 line
+static
ext4_io_end_t*ext4_init_io_end(struct inode *inode, unsigned int type)
^^^ remove space
ext4_init_io_end() is only called in one place; so maybe it would be better if it were inlined into ext4_ext_direct_IO? It also appears that the type field is never used, and so it can be removed from the ext4_io_end structure.
- Ted
Quoted 3 lines · Theodore Tso
OK, here are some comments on the patch; apologies for not getting to it sooner.
Not a problem. I appreciate your feedbacks..
Quoted 4 lines
First of all, I suggest the following replacement for the patch description. I've rewritten it to make it clearer and more succint. Do you think I've left anything out?
Looks cleaner and sane to me, thanks!
Quoted 26 lines
---------------
ext4: Use
end_iocall back to avoid direct I/O fallback to buffered I/OFrom: Mingming <[email protected]>
Currently the DIO VFS code passes create = 0 when writing to the middle of file. It does this to avoid block allocation for holes, so as not to expose stale data out when there is a parallel buffered read (which does not hold the
i_mutexlock). Direct I/O writes into holes falls back to buffered IO for this reason.Since preallocated extents are treated as holes when doing a
get_block()look up (buffer is not mapped), direct IO over fallocate also falls back to buffered IO. Thus ext4 actually silently falls back to buffered IO in above two cases, which is undesirable.To fix this, this patch creates unitialized extents when a direct I/O write needs to allocate blocks for writes that extend a file or writes into holes in sparse files, and registering an
end_iocallback which converts the uninitialized extent to an initialized extent after the I/O is completed.Singed-Off-By Mingming Cao <[email protected]>Signed-off-by "Theodore Ts'o" <[email protected]>
Quoted 7 lines
-------------------
Secondly, the patch doesn't compile after applying just the first patch. The reason for it is that first patch references
ext4_convert_unwritten_extents(), but it is only defined in the 2nd patch.
Oh, yes, the ext4_convert_unwritten_extents() function is implemented in the second patch. Drag that function into this patch will force to drag other functions into this patch too. Perhaps I could define a empty ext4_convert_unwritten_extents() in the first patch for now.
Quoted 4 lines
Other issues:
Quoted 1 line
+typedef struct ext4_io_end{
^^^ add a space
Quoted 9 lines
fs/ext4/ext4.h struct ext4_allocation_request { · line 121
Quoted 7 lines
fs/ext4/ext4.h +7
-0+struct inode *inode;/* file being written to */ +unsigned int type;/* unwritten or written */ +int error;/* I/O error code */ +ext4_lblk_t offset;/* offset in the file */ +size_t size;/* size of the extent */ +struct work_struct work;/* data work queue */ +}ext4_io_end_t;^^^ add a space
Sure.
Quoted 12 lines
fs/ext4/ext4.h struct ext4_new_group_data { · line 345
Quoted 6 lines
fs/ext4/ext4.h +2
-2- - +#define EXT4_GET_BLOCKS_DIO_CREATE_EXT 0x0011 +#define EXT4_GET_BLOCKS_DIO_CONVERT_EXT 0x0021/* * ioctl commands Could you add a comment for
EXT4_GET_BLOCKS_DIO_CREATE_EXTandEXT4_GET_BLOCKS_DIO_CONVERT_EXT, like the otherEXT4_GET_BLOCKS#define's? And a empty line before the "ioctl commands" comment would be much appreciated.
Will do.
Quoted 6 lines
Quoted 3 lines
/*
+ *O_DIRECTfor ext3 (or indirect map) based files
+ *Probably better just to say "
O_DIRECTfor direct/indirect block mapped files"
Sounds good.
Quoted 7 lines
Quoted 2 lines
+struct
workqueue_struct*ext4_unwritten_queue;This doesn't appear to be used; it looks like you started with a single global workqueue, and then moved to having a separate workqueue for each filesystem.
Ah, thanks for catching this. Yes, that was my initial intention. After moving this workqueue for each filesystem, I forget to remove the global one.
Quoted 6 lines
Quoted 1 line
+static
ext4_io_end_t*ext4_init_io_end(struct inode *inode, unsigned int type)^^^ remove space
ext4_init_io_end()is only called in one place; so maybe it would be better if it were inlined intoext4_ext_direct_IO?
okay, will do.
Quoted 4 lines
It also appears
that the type field is never used, and so it can be removed from theext4_io_endstructure.
I was thinking maybe in the future we could use the type for delalloc and guarded mode buffered IO ...so I define a type here, but we could remove it from the structure now, and add it later if needed for delalloc buffered IO.
Thanks for your review comments!
Quoted 5 lines
- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html