@@ -768,6 +768,60 @@ int ext4_get_block(struct inode *inode, sector_t iblock,
768768 create ? EXT4_GET_BLOCKS_CREATE : 0 );
769769}
770770
771+ /*
772+ * Get block function used when preparing for buffered write if we require
773+ * creating an unwritten extent if blocks haven't been allocated. The extent
774+ * will be converted to written after the IO is complete.
775+ */
776+ int ext4_get_block_unwritten (struct inode * inode , sector_t iblock ,
777+ struct buffer_head * bh_result , int create )
778+ {
779+ ext4_debug ("ext4_get_block_unwritten: inode %lu, create flag %d\n" ,
780+ inode -> i_ino , create );
781+ return _ext4_get_block (inode , iblock , bh_result ,
782+ EXT4_GET_BLOCKS_IO_CREATE_EXT );
783+ }
784+
785+ /* Get block function for DIO reads and writes to inodes without extents */
786+ int ext4_dio_get_block (struct inode * inode , sector_t iblock ,
787+ struct buffer_head * bh , int create )
788+ {
789+ return _ext4_get_block (inode , iblock , bh ,
790+ create ? EXT4_GET_BLOCKS_CREATE : 0 );
791+ }
792+
793+ /*
794+ * Get block function for DIO writes when we create unwritten extent if
795+ * blocks are not allocated yet. The extent will be converted to written
796+ * after IO is complete.
797+ */
798+ static int ext4_dio_get_block_unwritten (struct inode * inode , sector_t iblock ,
799+ struct buffer_head * bh_result , int create )
800+ {
801+ ext4_debug ("ext4_dio_get_block_unwritten: inode %lu, create flag %d\n" ,
802+ inode -> i_ino , create );
803+ return _ext4_get_block (inode , iblock , bh_result ,
804+ EXT4_GET_BLOCKS_IO_CREATE_EXT );
805+ }
806+
807+ static int ext4_dio_get_block_overwrite (struct inode * inode , sector_t iblock ,
808+ struct buffer_head * bh_result , int create )
809+ {
810+ int ret ;
811+
812+ ext4_debug ("ext4_dio_get_block_overwrite: inode %lu, create flag %d\n" ,
813+ inode -> i_ino , create );
814+ ret = _ext4_get_block (inode , iblock , bh_result , 0 );
815+ /*
816+ * Blocks should have been preallocated! ext4_file_write_iter() checks
817+ * that.
818+ */
819+ WARN_ON_ONCE (!buffer_mapped (bh_result ));
820+
821+ return ret ;
822+ }
823+
824+
771825/*
772826 * `handle' can be NULL if create is zero
773827 */
@@ -1079,13 +1133,14 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
10791133#ifdef CONFIG_EXT4_FS_ENCRYPTION
10801134 if (ext4_should_dioread_nolock (inode ))
10811135 ret = ext4_block_write_begin (page , pos , len ,
1082- ext4_get_block_write );
1136+ ext4_get_block_unwritten );
10831137 else
10841138 ret = ext4_block_write_begin (page , pos , len ,
10851139 ext4_get_block );
10861140#else
10871141 if (ext4_should_dioread_nolock (inode ))
1088- ret = __block_write_begin (page , pos , len , ext4_get_block_write );
1142+ ret = __block_write_begin (page , pos , len ,
1143+ ext4_get_block_unwritten );
10891144 else
10901145 ret = __block_write_begin (page , pos , len , ext4_get_block );
10911146#endif
@@ -3084,37 +3139,6 @@ static int ext4_releasepage(struct page *page, gfp_t wait)
30843139 return try_to_free_buffers (page );
30853140}
30863141
3087- /*
3088- * ext4_get_block used when preparing for a DIO write or buffer write.
3089- * We allocate an uinitialized extent if blocks haven't been allocated.
3090- * The extent will be converted to initialized after the IO is complete.
3091- */
3092- int ext4_get_block_write (struct inode * inode , sector_t iblock ,
3093- struct buffer_head * bh_result , int create )
3094- {
3095- ext4_debug ("ext4_get_block_write: inode %lu, create flag %d\n" ,
3096- inode -> i_ino , create );
3097- return _ext4_get_block (inode , iblock , bh_result ,
3098- EXT4_GET_BLOCKS_IO_CREATE_EXT );
3099- }
3100-
3101- static int ext4_get_block_overwrite (struct inode * inode , sector_t iblock ,
3102- struct buffer_head * bh_result , int create )
3103- {
3104- int ret ;
3105-
3106- ext4_debug ("ext4_get_block_overwrite: inode %lu, create flag %d\n" ,
3107- inode -> i_ino , create );
3108- ret = _ext4_get_block (inode , iblock , bh_result , 0 );
3109- /*
3110- * Blocks should have been preallocated! ext4_file_write_iter() checks
3111- * that.
3112- */
3113- WARN_ON_ONCE (!buffer_mapped (bh_result ));
3114-
3115- return ret ;
3116- }
3117-
31183142#ifdef CONFIG_FS_DAX
31193143int ext4_dax_mmap_get_block (struct inode * inode , sector_t iblock ,
31203144 struct buffer_head * bh_result , int create )
@@ -3282,7 +3306,7 @@ static ssize_t ext4_ext_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
32823306 */
32833307 iocb -> private = NULL ;
32843308 if (overwrite ) {
3285- get_block_func = ext4_get_block_overwrite ;
3309+ get_block_func = ext4_dio_get_block_overwrite ;
32863310 } else {
32873311 ext4_inode_aio_set (inode , NULL );
32883312 if (!is_sync_kiocb (iocb )) {
@@ -3304,7 +3328,7 @@ static ssize_t ext4_ext_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
33043328 */
33053329 ext4_inode_aio_set (inode , io_end );
33063330 }
3307- get_block_func = ext4_get_block_write ;
3331+ get_block_func = ext4_dio_get_block_unwritten ;
33083332 dio_flags = DIO_LOCKING ;
33093333 }
33103334#ifdef CONFIG_EXT4_FS_ENCRYPTION
@@ -5498,7 +5522,7 @@ int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
54985522 unlock_page (page );
54995523 /* OK, we need to fill the hole... */
55005524 if (ext4_should_dioread_nolock (inode ))
5501- get_block = ext4_get_block_write ;
5525+ get_block = ext4_get_block_unwritten ;
55025526 else
55035527 get_block = ext4_get_block ;
55045528retry_alloc :
0 commit comments