Skip to content

Commit edf064e

Browse files
goldwynraxboe
authored andcommitted
btrfs: nowait aio support
Return EAGAIN if any of the following checks fail + i_rwsem is not lockable + NODATACOW or PREALLOC is not set + Cannot nocow at the desired location + Writing beyond end of file which is not allocated Acked-by: David Sterba <dsterba@suse.com> Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 29a5d29 commit edf064e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

fs/btrfs/file.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,12 +1875,29 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
18751875
ssize_t num_written = 0;
18761876
bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
18771877
ssize_t err;
1878-
loff_t pos;
1879-
size_t count;
1878+
loff_t pos = iocb->ki_pos;
1879+
size_t count = iov_iter_count(from);
18801880
loff_t oldsize;
18811881
int clean_page = 0;
18821882

1883-
inode_lock(inode);
1883+
if ((iocb->ki_flags & IOCB_NOWAIT) &&
1884+
(iocb->ki_flags & IOCB_DIRECT)) {
1885+
/* Don't sleep on inode rwsem */
1886+
if (!inode_trylock(inode))
1887+
return -EAGAIN;
1888+
/*
1889+
* We will allocate space in case nodatacow is not set,
1890+
* so bail
1891+
*/
1892+
if (!(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1893+
BTRFS_INODE_PREALLOC)) ||
1894+
check_can_nocow(BTRFS_I(inode), pos, &count) <= 0) {
1895+
inode_unlock(inode);
1896+
return -EAGAIN;
1897+
}
1898+
} else
1899+
inode_lock(inode);
1900+
18841901
err = generic_write_checks(iocb, from);
18851902
if (err <= 0) {
18861903
inode_unlock(inode);
@@ -1914,8 +1931,6 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
19141931
*/
19151932
update_time_for_write(inode);
19161933

1917-
pos = iocb->ki_pos;
1918-
count = iov_iter_count(from);
19191934
start_pos = round_down(pos, fs_info->sectorsize);
19201935
oldsize = i_size_read(inode);
19211936
if (start_pos > oldsize) {
@@ -3071,13 +3086,19 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
30713086
return offset;
30723087
}
30733088

3089+
static int btrfs_file_open(struct inode *inode, struct file *filp)
3090+
{
3091+
filp->f_mode |= FMODE_AIO_NOWAIT;
3092+
return generic_file_open(inode, filp);
3093+
}
3094+
30743095
const struct file_operations btrfs_file_operations = {
30753096
.llseek = btrfs_file_llseek,
30763097
.read_iter = generic_file_read_iter,
30773098
.splice_read = generic_file_splice_read,
30783099
.write_iter = btrfs_file_write_iter,
30793100
.mmap = btrfs_file_mmap,
3080-
.open = generic_file_open,
3101+
.open = btrfs_file_open,
30813102
.release = btrfs_release_file,
30823103
.fsync = btrfs_sync_file,
30833104
.fallocate = btrfs_fallocate,

fs/btrfs/inode.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8754,6 +8754,9 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
87548754
dio_data.overwrite = 1;
87558755
inode_unlock(inode);
87568756
relock = true;
8757+
} else if (iocb->ki_flags & IOCB_NOWAIT) {
8758+
ret = -EAGAIN;
8759+
goto out;
87578760
}
87588761
ret = btrfs_delalloc_reserve_space(inode, offset, count);
87598762
if (ret)

0 commit comments

Comments
 (0)