Skip to content

Commit 4b46fce

Browse files
Josef Bacikchrismason-xx
authored andcommitted
Btrfs: add basic DIO read/write support
This provides basic DIO support for reading and writing. It does not do the work to recover from mismatching checksums, that will come later. A few design changes have been made from Jim's code (sorry Jim!) 1) Use the generic direct-io code. Jim originally re-wrote all the generic DIO code in order to account for all of BTRFS's oddities, but thanks to that work it seems like the best bet is to just ignore compression and such and just opt to fallback on buffered IO. 2) Fallback on buffered IO for compressed or inline extents. Jim's code did it's own buffering to make dio with compressed extents work. Now we just fallback onto normal buffered IO. 3) Use ordered extents for the writes so that all of the lock_extent() lookup_ordered() type checks continue to work. 4) Do the lock_extent() lookup_ordered() loop in readpage so we don't race with DIO writes. I've tested this with fsx and everything works great. This patch depends on my dio and filemap.c patches to work. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent c2c6ca4 commit 4b46fce

6 files changed

Lines changed: 631 additions & 36 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,8 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
23172317
struct btrfs_root *root, u64 bytenr, u64 len);
23182318
int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
23192319
struct bio *bio, u32 *dst);
2320+
int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
2321+
struct bio *bio, u64 logical_offset, u32 *dst);
23202322
int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
23212323
struct btrfs_root *root,
23222324
u64 objectid, u64 pos,

‎fs/btrfs/file-item.c‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
149149
}
150150

151151

152-
int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
153-
struct bio *bio, u32 *dst)
152+
static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
153+
struct inode *inode, struct bio *bio,
154+
u64 logical_offset, u32 *dst, int dio)
154155
{
155156
u32 sum;
156157
struct bio_vec *bvec = bio->bi_io_vec;
157158
int bio_index = 0;
158-
u64 offset;
159+
u64 offset = 0;
159160
u64 item_start_offset = 0;
160161
u64 item_last_offset = 0;
161162
u64 disk_bytenr;
@@ -174,8 +175,11 @@ int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
174175
WARN_ON(bio->bi_vcnt <= 0);
175176

176177
disk_bytenr = (u64)bio->bi_sector << 9;
178+
if (dio)
179+
offset = logical_offset;
177180
while (bio_index < bio->bi_vcnt) {
178-
offset = page_offset(bvec->bv_page) + bvec->bv_offset;
181+
if (!dio)
182+
offset = page_offset(bvec->bv_page) + bvec->bv_offset;
179183
ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
180184
if (ret == 0)
181185
goto found;
@@ -238,13 +242,26 @@ int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
238242
else
239243
set_state_private(io_tree, offset, sum);
240244
disk_bytenr += bvec->bv_len;
245+
offset += bvec->bv_len;
241246
bio_index++;
242247
bvec++;
243248
}
244249
btrfs_free_path(path);
245250
return 0;
246251
}
247252

253+
int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
254+
struct bio *bio, u32 *dst)
255+
{
256+
return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
257+
}
258+
259+
int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
260+
struct bio *bio, u64 offset, u32 *dst)
261+
{
262+
return __btrfs_lookup_bio_sums(root, inode, bio, offset, dst, 1);
263+
}
264+
248265
int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
249266
struct list_head *list)
250267
{

‎fs/btrfs/file.c‎

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,47 @@ static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
822822
return 0;
823823
}
824824

825+
/* Copied from read-write.c */
826+
static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
827+
{
828+
set_current_state(TASK_UNINTERRUPTIBLE);
829+
if (!kiocbIsKicked(iocb))
830+
schedule();
831+
else
832+
kiocbClearKicked(iocb);
833+
__set_current_state(TASK_RUNNING);
834+
}
835+
836+
/*
837+
* Just a copy of what do_sync_write does.
838+
*/
839+
static ssize_t __btrfs_direct_write(struct file *file, const char __user *buf,
840+
size_t count, loff_t pos, loff_t *ppos)
841+
{
842+
struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
843+
unsigned long nr_segs = 1;
844+
struct kiocb kiocb;
845+
ssize_t ret;
846+
847+
init_sync_kiocb(&kiocb, file);
848+
kiocb.ki_pos = pos;
849+
kiocb.ki_left = count;
850+
kiocb.ki_nbytes = count;
851+
852+
while (1) {
853+
ret = generic_file_direct_write(&kiocb, &iov, &nr_segs, pos,
854+
ppos, count, count);
855+
if (ret != -EIOCBRETRY)
856+
break;
857+
wait_on_retry_sync_kiocb(&kiocb);
858+
}
859+
860+
if (ret == -EIOCBQUEUED)
861+
ret = wait_on_sync_kiocb(&kiocb);
862+
*ppos = kiocb.ki_pos;
863+
return ret;
864+
}
865+
825866
static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
826867
size_t count, loff_t *ppos)
827868
{
@@ -838,12 +879,11 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
838879
unsigned long first_index;
839880
unsigned long last_index;
840881
int will_write;
882+
int buffered = 0;
841883

842884
will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
843885
(file->f_flags & O_DIRECT));
844886

845-
nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
846-
PAGE_CACHE_SIZE / (sizeof(struct page *)));
847887
pinned[0] = NULL;
848888
pinned[1] = NULL;
849889

@@ -867,13 +907,34 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
867907
goto out;
868908

869909
file_update_time(file);
910+
BTRFS_I(inode)->sequence++;
911+
912+
if (unlikely(file->f_flags & O_DIRECT)) {
913+
num_written = __btrfs_direct_write(file, buf, count, pos,
914+
ppos);
915+
pos += num_written;
916+
count -= num_written;
917+
918+
/* We've written everything we wanted to, exit */
919+
if (num_written < 0 || !count)
920+
goto out;
870921

922+
/*
923+
* We are going to do buffered for the rest of the range, so we
924+
* need to make sure to invalidate the buffered pages when we're
925+
* done.
926+
*/
927+
buffered = 1;
928+
buf += num_written;
929+
}
930+
931+
nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
932+
PAGE_CACHE_SIZE / (sizeof(struct page *)));
871933
pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
872934

873935
/* generic_write_checks can change our pos */
874936
start_pos = pos;
875937

876-
BTRFS_I(inode)->sequence++;
877938
first_index = pos >> PAGE_CACHE_SHIFT;
878939
last_index = (pos + count) >> PAGE_CACHE_SHIFT;
879940

@@ -1007,7 +1068,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
10071068
btrfs_end_transaction(trans, root);
10081069
}
10091070
}
1010-
if (file->f_flags & O_DIRECT) {
1071+
if (file->f_flags & O_DIRECT && buffered) {
10111072
invalidate_mapping_pages(inode->i_mapping,
10121073
start_pos >> PAGE_CACHE_SHIFT,
10131074
(start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);

0 commit comments

Comments
 (0)