Skip to content

Commit df2d6f2

Browse files
Christoph HellwigAl Viro
authored andcommitted
fs: always maintain i_dio_count
Maintain i_dio_count for all filesystems, not just those using DIO_LOCKING. This these filesystems to also protect truncate against direct I/O requests by using common code. Right now the only non-DIO_LOCKING filesystem that appears to do so is XFS, which uses an opencoded variant of the i_dio_count scheme. Behaviour doesn't change for filesystems never calling inode_dio_wait. For ext4 behaviour changes when using the dioread_nonlock option, which previously was missing any protection between truncate and direct I/O reads. For ocfs2 that handcrafted i_dio_count manipulations are replaced with the common code now enable. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 562c72a commit df2d6f2

3 files changed

Lines changed: 17 additions & 24 deletions

File tree

‎fs/direct-io.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ static ssize_t dio_complete(struct dio *dio, loff_t offset, ssize_t ret, bool is
297297
aio_complete(dio->iocb, ret, 0);
298298
}
299299

300-
if (dio->flags & DIO_LOCKING)
301-
inode_dio_done(dio->inode);
300+
inode_dio_done(dio->inode);
302301
return ret;
303302
}
304303

@@ -1185,14 +1184,16 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
11851184
* For writes this function is called under i_mutex and returns with
11861185
* i_mutex held, for reads, i_mutex is not held on entry, but it is
11871186
* taken and dropped again before returning.
1188-
* The i_dio_count counter keeps track of the number of outstanding
1189-
* direct I/O requests, and truncate waits for it to reach zero.
1190-
* New references to i_dio_count must only be grabbed with i_mutex
1191-
* held.
1192-
*
11931187
* - if the flags value does NOT contain DIO_LOCKING we don't use any
11941188
* internal locking but rather rely on the filesystem to synchronize
11951189
* direct I/O reads/writes versus each other and truncate.
1190+
*
1191+
* To help with locking against truncate we incremented the i_dio_count
1192+
* counter before starting direct I/O, and decrement it once we are done.
1193+
* Truncate can wait for it to reach zero to provide exclusion. It is
1194+
* expected that filesystem provide exclusion between new direct I/O
1195+
* and truncates. For DIO_LOCKING filesystems this is done by i_mutex,
1196+
* but other filesystems need to take care of this on their own.
11961197
*/
11971198
ssize_t
11981199
__blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
@@ -1270,13 +1271,13 @@ __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
12701271
goto out;
12711272
}
12721273
}
1273-
1274-
/*
1275-
* Will be decremented at I/O completion time.
1276-
*/
1277-
atomic_inc(&inode->i_dio_count);
12781274
}
12791275

1276+
/*
1277+
* Will be decremented at I/O completion time.
1278+
*/
1279+
atomic_inc(&inode->i_dio_count);
1280+
12801281
/*
12811282
* For file extending writes updating i_size before data
12821283
* writeouts complete can expose uninitialized blocks. So

‎fs/ocfs2/aops.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,8 @@ static void ocfs2_dio_end_io(struct kiocb *iocb,
567567
/* this io's submitter should not have unlocked this before we could */
568568
BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
569569

570-
if (ocfs2_iocb_is_sem_locked(iocb)) {
571-
inode_dio_done(inode);
570+
if (ocfs2_iocb_is_sem_locked(iocb))
572571
ocfs2_iocb_clear_sem_locked(iocb);
573-
}
574572

575573
ocfs2_iocb_clear_rw_locked(iocb);
576574

‎fs/ocfs2/file.c‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,6 @@ static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
22402240
relock:
22412241
/* to match setattr's i_mutex -> rw_lock ordering */
22422242
if (direct_io) {
2243-
atomic_inc(&inode->i_dio_count);
22442243
have_alloc_sem = 1;
22452244
/* communicate with ocfs2_dio_end_io */
22462245
ocfs2_iocb_set_sem_locked(iocb);
@@ -2292,7 +2291,6 @@ static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
22922291
*/
22932292
if (direct_io && !can_do_direct) {
22942293
ocfs2_rw_unlock(inode, rw_level);
2295-
inode_dio_done(inode);
22962294

22972295
have_alloc_sem = 0;
22982296
rw_level = -1;
@@ -2379,10 +2377,8 @@ static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
23792377
ocfs2_rw_unlock(inode, rw_level);
23802378

23812379
out_sems:
2382-
if (have_alloc_sem) {
2383-
inode_dio_done(inode);
2380+
if (have_alloc_sem)
23842381
ocfs2_iocb_clear_sem_locked(iocb);
2385-
}
23862382

23872383
mutex_unlock(&inode->i_mutex);
23882384

@@ -2533,7 +2529,6 @@ static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
25332529
*/
25342530
if (filp->f_flags & O_DIRECT) {
25352531
have_alloc_sem = 1;
2536-
atomic_inc(&inode->i_dio_count);
25372532
ocfs2_iocb_set_sem_locked(iocb);
25382533

25392534
ret = ocfs2_rw_lock(inode, 0);
@@ -2575,10 +2570,9 @@ static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
25752570
}
25762571

25772572
bail:
2578-
if (have_alloc_sem) {
2579-
inode_dio_done(inode);
2573+
if (have_alloc_sem)
25802574
ocfs2_iocb_clear_sem_locked(iocb);
2581-
}
2575+
25822576
if (rw_level != -1)
25832577
ocfs2_rw_unlock(inode, rw_level);
25842578

0 commit comments

Comments
 (0)