@@ -1488,10 +1488,7 @@ static int mpage_da_submit_io(struct mpage_da_data *mpd,
14881488 struct ext4_io_submit io_submit ;
14891489
14901490 BUG_ON (mpd -> next_page <= mpd -> first_page );
1491- ext4_io_submit_init (& io_submit , mpd -> wbc );
1492- io_submit .io_end = ext4_init_io_end (inode , GFP_NOFS );
1493- if (!io_submit .io_end )
1494- return - ENOMEM ;
1491+ memset (& io_submit , 0 , sizeof (io_submit ));
14951492 /*
14961493 * We need to start from the first_page to the next_page - 1
14971494 * to make sure we also write the mapped dirty buffer_heads.
@@ -1579,8 +1576,6 @@ static int mpage_da_submit_io(struct mpage_da_data *mpd,
15791576 pagevec_release (& pvec );
15801577 }
15811578 ext4_io_submit (& io_submit );
1582- /* Drop io_end reference we got from init */
1583- ext4_put_io_end_defer (io_submit .io_end );
15841579 return ret ;
15851580}
15861581
@@ -2239,16 +2234,9 @@ static int ext4_writepage(struct page *page,
22392234 */
22402235 return __ext4_journalled_writepage (page , len );
22412236
2242- ext4_io_submit_init (& io_submit , wbc );
2243- io_submit .io_end = ext4_init_io_end (inode , GFP_NOFS );
2244- if (!io_submit .io_end ) {
2245- redirty_page_for_writepage (wbc , page );
2246- return - ENOMEM ;
2247- }
2237+ memset (& io_submit , 0 , sizeof (io_submit ));
22482238 ret = ext4_bio_write_page (& io_submit , page , len , wbc );
22492239 ext4_io_submit (& io_submit );
2250- /* Drop io_end reference we got from init */
2251- ext4_put_io_end_defer (io_submit .io_end );
22522240 return ret ;
22532241}
22542242
@@ -3079,27 +3067,35 @@ static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
30793067 struct inode * inode = file_inode (iocb -> ki_filp );
30803068 ext4_io_end_t * io_end = iocb -> private ;
30813069
3082- /* if not async direct IO just return */
3083- if (!io_end ) {
3084- inode_dio_done (inode );
3085- if (is_async )
3086- aio_complete (iocb , ret , 0 );
3087- return ;
3088- }
3070+ /* if not async direct IO or dio with 0 bytes write, just return */
3071+ if (!io_end || !size )
3072+ goto out ;
30893073
30903074 ext_debug ("ext4_end_io_dio(): io_end 0x%p "
30913075 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n" ,
30923076 iocb -> private , io_end -> inode -> i_ino , iocb , offset ,
30933077 size );
30943078
30953079 iocb -> private = NULL ;
3080+
3081+ /* if not aio dio with unwritten extents, just free io and return */
3082+ if (!(io_end -> flag & EXT4_IO_END_UNWRITTEN )) {
3083+ ext4_free_io_end (io_end );
3084+ out :
3085+ inode_dio_done (inode );
3086+ if (is_async )
3087+ aio_complete (iocb , ret , 0 );
3088+ return ;
3089+ }
3090+
30963091 io_end -> offset = offset ;
30973092 io_end -> size = size ;
30983093 if (is_async ) {
30993094 io_end -> iocb = iocb ;
31003095 io_end -> result = ret ;
31013096 }
3102- ext4_put_io_end_defer (io_end );
3097+
3098+ ext4_add_complete_io (io_end );
31033099}
31043100
31053101/*
@@ -3133,7 +3129,6 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
31333129 get_block_t * get_block_func = NULL ;
31343130 int dio_flags = 0 ;
31353131 loff_t final_size = offset + count ;
3136- ext4_io_end_t * io_end = NULL ;
31373132
31383133 /* Use the old path for reads and writes beyond i_size. */
31393134 if (rw != WRITE || final_size > inode -> i_size )
@@ -3172,16 +3167,13 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
31723167 iocb -> private = NULL ;
31733168 ext4_inode_aio_set (inode , NULL );
31743169 if (!is_sync_kiocb (iocb )) {
3175- io_end = ext4_init_io_end (inode , GFP_NOFS );
3170+ ext4_io_end_t * io_end = ext4_init_io_end (inode , GFP_NOFS );
31763171 if (!io_end ) {
31773172 ret = - ENOMEM ;
31783173 goto retake_lock ;
31793174 }
31803175 io_end -> flag |= EXT4_IO_END_DIRECT ;
3181- /*
3182- * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3183- */
3184- iocb -> private = ext4_get_io_end (io_end );
3176+ iocb -> private = io_end ;
31853177 /*
31863178 * we save the io structure for current async direct
31873179 * IO, so that later ext4_map_blocks() could flag the
@@ -3205,27 +3197,26 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
32053197 NULL ,
32063198 dio_flags );
32073199
3200+ if (iocb -> private )
3201+ ext4_inode_aio_set (inode , NULL );
32083202 /*
3209- * Put our reference to io_end. This can free the io_end structure e.g.
3210- * in sync IO case or in case of error. It can even perform extent
3211- * conversion if all bios we submitted finished before we got here.
3212- * Note that in that case iocb->private can be already set to NULL
3213- * here.
3203+ * The io_end structure takes a reference to the inode, that
3204+ * structure needs to be destroyed and the reference to the
3205+ * inode need to be dropped, when IO is complete, even with 0
3206+ * byte write, or failed.
3207+ *
3208+ * In the successful AIO DIO case, the io_end structure will
3209+ * be destroyed and the reference to the inode will be dropped
3210+ * after the end_io call back function is called.
3211+ *
3212+ * In the case there is 0 byte write, or error case, since VFS
3213+ * direct IO won't invoke the end_io call back function, we
3214+ * need to free the end_io structure here.
32143215 */
3215- if (io_end ) {
3216- ext4_inode_aio_set (inode , NULL );
3217- ext4_put_io_end (io_end );
3218- /*
3219- * In case of error or no write ext4_end_io_dio() was not
3220- * called so we have to put iocb's reference.
3221- */
3222- if (ret <= 0 && ret != - EIOCBQUEUED ) {
3223- WARN_ON (iocb -> private != io_end );
3224- ext4_put_io_end (io_end );
3225- iocb -> private = NULL ;
3226- }
3227- }
3228- if (ret > 0 && !overwrite && ext4_test_inode_state (inode ,
3216+ if (ret != - EIOCBQUEUED && ret <= 0 && iocb -> private ) {
3217+ ext4_free_io_end (iocb -> private );
3218+ iocb -> private = NULL ;
3219+ } else if (ret > 0 && !overwrite && ext4_test_inode_state (inode ,
32293220 EXT4_STATE_DIO_UNWRITTEN )) {
32303221 int err ;
32313222 /*
0 commit comments