Skip to content

Commit 38851cc

Browse files
Miao XieJosef Bacik
authored andcommitted
Btrfs: implement unlocked dio write
This idea is from ext4. By this patch, we can make the dio write parallel, and improve the performance. But because we can not update isize without i_mutex, the unlocked dio write just can be done in front of the EOF. We needn't worry about the race between dio write and truncate, because the truncate need wait untill all the dio write end. And we also needn't worry about the race between dio write and punch hole, because we have extent lock to protect our operation. I ran fio to test the performance of this feature. == Hardware == CPU: Intel(R) Core(TM)2 Duo CPU E7500 @ 2.93GHz Mem: 2GB SSD: Intel X25-M 120GB (Test Partition: 60GB) == config file == [global] ioengine=psync direct=1 bs=4k size=32G runtime=60 directory=/mnt/btrfs/ filename=testfile group_reporting thread [file1] numjobs=1 # 2 4 rw=randwrite == result (KBps) == write 1 2 4 lock 24936 24738 24726 nolock 24962 30866 32101 == result (iops) == write 1 2 4 lock 6234 6184 6181 nolock 6240 7716 8025 Signed-off-by: Miao Xie <[email protected]> Signed-off-by: Josef Bacik <[email protected]>
1 parent 2e60a51 commit 38851cc

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

fs/btrfs/inode.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6677,28 +6677,36 @@ static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
66776677
struct inode *inode = file->f_mapping->host;
66786678
size_t count = 0;
66796679
int flags = 0;
6680-
bool wakeup = false;
6680+
bool wakeup = true;
6681+
bool relock = false;
66816682
ssize_t ret;
66826683

66836684
if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
66846685
offset, nr_segs))
66856686
return 0;
66866687

6688+
atomic_inc(&inode->i_dio_count);
6689+
smp_mb__after_atomic_inc();
6690+
66876691
if (rw & WRITE) {
66886692
count = iov_length(iov, nr_segs);
6693+
/*
6694+
* If the write DIO is beyond the EOF, we need update
6695+
* the isize, but it is protected by i_mutex. So we can
6696+
* not unlock the i_mutex at this case.
6697+
*/
6698+
if (offset + count <= inode->i_size) {
6699+
mutex_unlock(&inode->i_mutex);
6700+
relock = true;
6701+
}
66896702
ret = btrfs_delalloc_reserve_space(inode, count);
66906703
if (ret)
6691-
return ret;
6692-
} else {
6693-
atomic_inc(&inode->i_dio_count);
6694-
smp_mb__after_atomic_inc();
6695-
if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
6696-
&BTRFS_I(inode)->runtime_flags))) {
6697-
inode_dio_done(inode);
6698-
flags = DIO_LOCKING | DIO_SKIP_HOLES;
6699-
} else {
6700-
wakeup = true;
6701-
}
6704+
goto out;
6705+
} else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
6706+
&BTRFS_I(inode)->runtime_flags))) {
6707+
inode_dio_done(inode);
6708+
flags = DIO_LOCKING | DIO_SKIP_HOLES;
6709+
wakeup = false;
67026710
}
67036711

67046712
ret = __blockdev_direct_IO(rw, iocb, inode,
@@ -6717,8 +6725,11 @@ static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
67176725
}
67186726
btrfs_delalloc_release_metadata(inode, 0);
67196727
}
6728+
out:
67206729
if (wakeup)
67216730
inode_dio_done(inode);
6731+
if (relock)
6732+
mutex_lock(&inode->i_mutex);
67226733

67236734
return ret;
67246735
}

0 commit comments

Comments
 (0)