Skip to content

Commit 5fe878a

Browse files
Christoph Hellwigtorvalds
authored andcommitted
direct-io: cleanup blockdev_direct_IO locking
Currently the locking in blockdev_direct_IO is a mess, we have three different locking types and very confusing checks for some of them. The most complicated one is DIO_OWN_LOCKING for reads, which happens to not actually be used. This patch gets rid of the DIO_OWN_LOCKING - as mentioned above the read case is unused anyway, and the write side is almost identical to DIO_NO_LOCKING. The difference is that DIO_NO_LOCKING always sets the create argument for the get_blocks callback to zero, but we can easily move that to the actual get_blocks callbacks. There are four users of the DIO_NO_LOCKING mode: gfs already ignores the create argument and thus is fine with the new version, ocfs2 only errors out if create were ever set, and we can remove this dead code now, the block device code only ever uses create for an error message if we are fully beyond the device which can never happen, and last but not least XFS will need the new behavour for writes. Now we can replace the lock_type variable with a flags one, where no flag means the DIO_NO_LOCKING behaviour and DIO_LOCKING is kept as the first flag. Separate out the check for not allowing to fill holes into a separate flag, although for now both flags always get set at the same time. Also revamp the documentation of the locking scheme to actually make sense. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Christoph Hellwig <hch@lst.de> Cc: Dave Chinner <david@fromorbit.com> Cc: Badari Pulavarty <pbadari@us.ibm.com> Cc: Jeff Moyer <jmoyer@redhat.com> Cc: Jens Axboe <jens.axboe@oracle.com> Cc: Zach Brown <zach.brown@oracle.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Alex Elder <aelder@sgi.com> Cc: Mark Fasheh <mfasheh@suse.com> Cc: Joel Becker <joel.becker@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 23aee09 commit 5fe878a

4 files changed

Lines changed: 71 additions & 134 deletions

File tree

‎fs/direct-io.c‎

Lines changed: 52 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@
5353
*
5454
* If blkfactor is zero then the user's request was aligned to the filesystem's
5555
* blocksize.
56-
*
57-
* lock_type is DIO_LOCKING for regular files on direct-IO-naive filesystems.
58-
* This determines whether we need to do the fancy locking which prevents
59-
* direct-IO from being able to read uninitialised disk blocks. If its zero
60-
* (blockdev) this locking is not done, and if it is DIO_OWN_LOCKING i_mutex is
61-
* not held for the entire direct write (taken briefly, initially, during a
62-
* direct read though, but its never held for the duration of a direct-IO).
6356
*/
6457

6558
struct dio {
@@ -68,7 +61,7 @@ struct dio {
6861
struct inode *inode;
6962
int rw;
7063
loff_t i_size; /* i_size when submitted */
71-
int lock_type; /* doesn't change */
64+
int flags; /* doesn't change */
7265
unsigned blkbits; /* doesn't change */
7366
unsigned blkfactor; /* When we're using an alignment which
7467
is finer than the filesystem's soft
@@ -246,7 +239,8 @@ static int dio_complete(struct dio *dio, loff_t offset, int ret)
246239
if (dio->end_io && dio->result)
247240
dio->end_io(dio->iocb, offset, transferred,
248241
dio->map_bh.b_private);
249-
if (dio->lock_type == DIO_LOCKING)
242+
243+
if (dio->flags & DIO_LOCKING)
250244
/* lockdep: non-owner release */
251245
up_read_non_owner(&dio->inode->i_alloc_sem);
252246

@@ -521,21 +515,24 @@ static int get_more_blocks(struct dio *dio)
521515
map_bh->b_state = 0;
522516
map_bh->b_size = fs_count << dio->inode->i_blkbits;
523517

518+
/*
519+
* For writes inside i_size on a DIO_SKIP_HOLES filesystem we
520+
* forbid block creations: only overwrites are permitted.
521+
* We will return early to the caller once we see an
522+
* unmapped buffer head returned, and the caller will fall
523+
* back to buffered I/O.
524+
*
525+
* Otherwise the decision is left to the get_blocks method,
526+
* which may decide to handle it or also return an unmapped
527+
* buffer head.
528+
*/
524529
create = dio->rw & WRITE;
525-
if (dio->lock_type == DIO_LOCKING) {
530+
if (dio->flags & DIO_SKIP_HOLES) {
526531
if (dio->block_in_file < (i_size_read(dio->inode) >>
527532
dio->blkbits))
528533
create = 0;
529-
} else if (dio->lock_type == DIO_NO_LOCKING) {
530-
create = 0;
531534
}
532535

533-
/*
534-
* For writes inside i_size we forbid block creations: only
535-
* overwrites are permitted. We fall back to buffered writes
536-
* at a higher level for inside-i_size block-instantiating
537-
* writes.
538-
*/
539536
ret = (*dio->get_block)(dio->inode, fs_startblk,
540537
map_bh, create);
541538
}
@@ -1045,7 +1042,7 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
10451042
* we can let i_mutex go now that its achieved its purpose
10461043
* of protecting us from looking up uninitialized blocks.
10471044
*/
1048-
if ((rw == READ) && (dio->lock_type == DIO_LOCKING))
1045+
if (rw == READ && (dio->flags & DIO_LOCKING))
10491046
mutex_unlock(&dio->inode->i_mutex);
10501047

10511048
/*
@@ -1092,30 +1089,28 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
10921089

10931090
/*
10941091
* This is a library function for use by filesystem drivers.
1095-
* The locking rules are governed by the dio_lock_type parameter.
10961092
*
1097-
* DIO_NO_LOCKING (no locking, for raw block device access)
1098-
* For writes, i_mutex is not held on entry; it is never taken.
1093+
* The locking rules are governed by the flags parameter:
1094+
* - if the flags value contains DIO_LOCKING we use a fancy locking
1095+
* scheme for dumb filesystems.
1096+
* For writes this function is called under i_mutex and returns with
1097+
* i_mutex held, for reads, i_mutex is not held on entry, but it is
1098+
* taken and dropped again before returning.
1099+
* For reads and writes i_alloc_sem is taken in shared mode and released
1100+
* on I/O completion (which may happen asynchronously after returning to
1101+
* the caller).
10991102
*
1100-
* DIO_LOCKING (simple locking for regular files)
1101-
* For writes we are called under i_mutex and return with i_mutex held, even
1102-
* though it is internally dropped.
1103-
* For reads, i_mutex is not held on entry, but it is taken and dropped before
1104-
* returning.
1105-
*
1106-
* DIO_OWN_LOCKING (filesystem provides synchronisation and handling of
1107-
* uninitialised data, allowing parallel direct readers and writers)
1108-
* For writes we are called without i_mutex, return without it, never touch it.
1109-
* For reads we are called under i_mutex and return with i_mutex held, even
1110-
* though it may be internally dropped.
1111-
*
1112-
* Additional i_alloc_sem locking requirements described inline below.
1103+
* - if the flags value does NOT contain DIO_LOCKING we don't use any
1104+
* internal locking but rather rely on the filesystem to synchronize
1105+
* direct I/O reads/writes versus each other and truncate.
1106+
* For reads and writes both i_mutex and i_alloc_sem are not held on
1107+
* entry and are never taken.
11131108
*/
11141109
ssize_t
11151110
__blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
11161111
struct block_device *bdev, const struct iovec *iov, loff_t offset,
11171112
unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
1118-
int dio_lock_type)
1113+
int flags)
11191114
{
11201115
int seg;
11211116
size_t size;
@@ -1126,8 +1121,6 @@ __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
11261121
ssize_t retval = -EINVAL;
11271122
loff_t end = offset;
11281123
struct dio *dio;
1129-
int release_i_mutex = 0;
1130-
int acquire_i_mutex = 0;
11311124

11321125
if (rw & WRITE)
11331126
rw = WRITE_ODIRECT_PLUG;
@@ -1168,43 +1161,30 @@ __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
11681161
*/
11691162
memset(dio, 0, offsetof(struct dio, pages));
11701163

1171-
/*
1172-
* For block device access DIO_NO_LOCKING is used,
1173-
* neither readers nor writers do any locking at all
1174-
* For regular files using DIO_LOCKING,
1175-
* readers need to grab i_mutex and i_alloc_sem
1176-
* writers need to grab i_alloc_sem only (i_mutex is already held)
1177-
* For regular files using DIO_OWN_LOCKING,
1178-
* neither readers nor writers take any locks here
1179-
*/
1180-
dio->lock_type = dio_lock_type;
1181-
if (dio_lock_type != DIO_NO_LOCKING) {
1164+
dio->flags = flags;
1165+
if (dio->flags & DIO_LOCKING) {
11821166
/* watch out for a 0 len io from a tricksy fs */
11831167
if (rw == READ && end > offset) {
1184-
struct address_space *mapping;
1168+
struct address_space *mapping =
1169+
iocb->ki_filp->f_mapping;
11851170

1186-
mapping = iocb->ki_filp->f_mapping;
1187-
if (dio_lock_type != DIO_OWN_LOCKING) {
1188-
mutex_lock(&inode->i_mutex);
1189-
release_i_mutex = 1;
1190-
}
1171+
/* will be released by direct_io_worker */
1172+
mutex_lock(&inode->i_mutex);
11911173

11921174
retval = filemap_write_and_wait_range(mapping, offset,
11931175
end - 1);
11941176
if (retval) {
1177+
mutex_unlock(&inode->i_mutex);
11951178
kfree(dio);
11961179
goto out;
11971180
}
1198-
1199-
if (dio_lock_type == DIO_OWN_LOCKING) {
1200-
mutex_unlock(&inode->i_mutex);
1201-
acquire_i_mutex = 1;
1202-
}
12031181
}
12041182

1205-
if (dio_lock_type == DIO_LOCKING)
1206-
/* lockdep: not the owner will release it */
1207-
down_read_non_owner(&inode->i_alloc_sem);
1183+
/*
1184+
* Will be released at I/O completion, possibly in a
1185+
* different thread.
1186+
*/
1187+
down_read_non_owner(&inode->i_alloc_sem);
12081188
}
12091189

12101190
/*
@@ -1222,24 +1202,19 @@ __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
12221202
/*
12231203
* In case of error extending write may have instantiated a few
12241204
* blocks outside i_size. Trim these off again for DIO_LOCKING.
1225-
* NOTE: DIO_NO_LOCK/DIO_OWN_LOCK callers have to handle this by
1226-
* it's own meaner.
1205+
*
1206+
* NOTE: filesystems with their own locking have to handle this
1207+
* on their own.
12271208
*/
1228-
if (unlikely(retval < 0 && (rw & WRITE))) {
1229-
loff_t isize = i_size_read(inode);
1230-
1231-
if (end > isize && dio_lock_type == DIO_LOCKING)
1232-
vmtruncate(inode, isize);
1209+
if (dio->flags & DIO_LOCKING) {
1210+
if (unlikely((rw & WRITE) && retval < 0)) {
1211+
loff_t isize = i_size_read(inode);
1212+
if (end > isize)
1213+
vmtruncate(inode, isize);
1214+
}
12331215
}
12341216

1235-
if (rw == READ && dio_lock_type == DIO_LOCKING)
1236-
release_i_mutex = 0;
1237-
12381217
out:
1239-
if (release_i_mutex)
1240-
mutex_unlock(&inode->i_mutex);
1241-
else if (acquire_i_mutex)
1242-
mutex_lock(&inode->i_mutex);
12431218
return retval;
12441219
}
12451220
EXPORT_SYMBOL(__blockdev_direct_IO);

‎fs/ocfs2/aops.c‎

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
547547
*
548548
* called like this: dio->get_blocks(dio->inode, fs_startblk,
549549
* fs_count, map_bh, dio->rw == WRITE);
550+
*
551+
* Note that we never bother to allocate blocks here, and thus ignore the
552+
* create argument.
550553
*/
551554
static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
552555
struct buffer_head *bh_result, int create)
@@ -563,14 +566,6 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
563566

564567
inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
565568

566-
/*
567-
* Any write past EOF is not allowed because we'd be extending.
568-
*/
569-
if (create && (iblock + max_blocks) > inode_blocks) {
570-
ret = -EIO;
571-
goto bail;
572-
}
573-
574569
/* This figures out the size of the next contiguous block, and
575570
* our logical offset */
576571
ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
@@ -582,15 +577,6 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
582577
goto bail;
583578
}
584579

585-
if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno && create) {
586-
ocfs2_error(inode->i_sb,
587-
"Inode %llu has a hole at block %llu\n",
588-
(unsigned long long)OCFS2_I(inode)->ip_blkno,
589-
(unsigned long long)iblock);
590-
ret = -EROFS;
591-
goto bail;
592-
}
593-
594580
/* We should already CoW the refcounted extent. */
595581
BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
596582
/*
@@ -601,20 +587,8 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
601587
*/
602588
if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
603589
map_bh(bh_result, inode->i_sb, p_blkno);
604-
else {
605-
/*
606-
* ocfs2_prepare_inode_for_write() should have caught
607-
* the case where we'd be filling a hole and triggered
608-
* a buffered write instead.
609-
*/
610-
if (create) {
611-
ret = -EIO;
612-
mlog_errno(ret);
613-
goto bail;
614-
}
615-
590+
else
616591
clear_buffer_mapped(bh_result);
617-
}
618592

619593
/* make sure we don't map more than max_blocks blocks here as
620594
that's all the kernel will handle at this point. */

‎fs/xfs/linux-2.6/xfs_aops.c‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,19 +1474,13 @@ xfs_vm_direct_IO(
14741474

14751475
bdev = xfs_find_bdev_for_inode(XFS_I(inode));
14761476

1477-
if (rw == WRITE) {
1478-
iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
1479-
ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
1480-
bdev, iov, offset, nr_segs,
1481-
xfs_get_blocks_direct,
1482-
xfs_end_io_direct);
1483-
} else {
1484-
iocb->private = xfs_alloc_ioend(inode, IOMAP_READ);
1485-
ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
1486-
bdev, iov, offset, nr_segs,
1487-
xfs_get_blocks_direct,
1488-
xfs_end_io_direct);
1489-
}
1477+
iocb->private = xfs_alloc_ioend(inode, rw == WRITE ?
1478+
IOMAP_UNWRITTEN : IOMAP_READ);
1479+
1480+
ret = blockdev_direct_IO_no_locking(rw, iocb, inode, bdev, iov,
1481+
offset, nr_segs,
1482+
xfs_get_blocks_direct,
1483+
xfs_end_io_direct);
14901484

14911485
if (unlikely(ret != -EIOCBQUEUED && iocb->private))
14921486
xfs_destroy_ioend(iocb->private);

‎include/linux/fs.h‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,9 +2264,11 @@ ssize_t __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
22642264
int lock_type);
22652265

22662266
enum {
2267-
DIO_LOCKING = 1, /* need locking between buffered and direct access */
2268-
DIO_NO_LOCKING, /* bdev; no locking at all between buffered/direct */
2269-
DIO_OWN_LOCKING, /* filesystem locks buffered and direct internally */
2267+
/* need locking between buffered and direct access */
2268+
DIO_LOCKING = 0x01,
2269+
2270+
/* filesystem does not support filling holes */
2271+
DIO_SKIP_HOLES = 0x02,
22702272
};
22712273

22722274
static inline ssize_t blockdev_direct_IO(int rw, struct kiocb *iocb,
@@ -2275,7 +2277,8 @@ static inline ssize_t blockdev_direct_IO(int rw, struct kiocb *iocb,
22752277
dio_iodone_t end_io)
22762278
{
22772279
return __blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
2278-
nr_segs, get_block, end_io, DIO_LOCKING);
2280+
nr_segs, get_block, end_io,
2281+
DIO_LOCKING | DIO_SKIP_HOLES);
22792282
}
22802283

22812284
static inline ssize_t blockdev_direct_IO_no_locking(int rw, struct kiocb *iocb,
@@ -2284,16 +2287,7 @@ static inline ssize_t blockdev_direct_IO_no_locking(int rw, struct kiocb *iocb,
22842287
dio_iodone_t end_io)
22852288
{
22862289
return __blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
2287-
nr_segs, get_block, end_io, DIO_NO_LOCKING);
2288-
}
2289-
2290-
static inline ssize_t blockdev_direct_IO_own_locking(int rw, struct kiocb *iocb,
2291-
struct inode *inode, struct block_device *bdev, const struct iovec *iov,
2292-
loff_t offset, unsigned long nr_segs, get_block_t get_block,
2293-
dio_iodone_t end_io)
2294-
{
2295-
return __blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
2296-
nr_segs, get_block, end_io, DIO_OWN_LOCKING);
2290+
nr_segs, get_block, end_io, 0);
22972291
}
22982292
#endif
22992293

0 commit comments

Comments
 (0)