1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/slab.h>
28#include <linux/highmem.h>
29#include <linux/pagemap.h>
30#include <linux/task_io_accounting_ops.h>
31#include <linux/bio.h>
32#include <linux/wait.h>
33#include <linux/err.h>
34#include <linux/blkdev.h>
35#include <linux/buffer_head.h>
36#include <linux/rwsem.h>
37#include <linux/uio.h>
38#include <linux/atomic.h>
39#include <linux/prefetch.h>
40
41
42
43
44
45#define DIO_PAGES 64
46
47
48
49
50#define DIO_COMPLETE_ASYNC 0x01
51#define DIO_COMPLETE_INVALIDATE 0x02
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67struct dio_submit {
68 struct bio *bio;
69 unsigned blkbits;
70 unsigned blkfactor;
71
72
73
74
75 unsigned start_zero_done;
76
77
78 int pages_in_io;
79 sector_t block_in_file;
80
81 unsigned blocks_available;
82 int reap_counter;
83 sector_t final_block_in_request;
84 int boundary;
85 get_block_t *get_block;
86 dio_submit_t *submit_io;
87
88 loff_t logical_offset_in_bio;
89 sector_t final_block_in_bio;
90 sector_t next_block_for_io;
91
92
93
94
95
96
97
98 struct page *cur_page;
99 unsigned cur_page_offset;
100 unsigned cur_page_len;
101 sector_t cur_page_block;
102 loff_t cur_page_fs_offset;
103
104 struct iov_iter *iter;
105
106
107
108
109 unsigned head;
110 unsigned tail;
111 size_t from, to;
112};
113
114
115struct dio {
116 int flags;
117 int op;
118 int op_flags;
119 blk_qc_t bio_cookie;
120 struct gendisk *bio_disk;
121 struct inode *inode;
122 loff_t i_size;
123 dio_iodone_t *end_io;
124
125 void *private;
126
127
128 spinlock_t bio_lock;
129 int page_errors;
130 int is_async;
131 bool defer_completion;
132 bool should_dirty;
133 int io_error;
134 unsigned long refcount;
135 struct bio *bio_list;
136 struct task_struct *waiter;
137
138
139 struct kiocb *iocb;
140 ssize_t result;
141
142
143
144
145
146
147 union {
148 struct page *pages[DIO_PAGES];
149 struct work_struct complete_work;
150 };
151} ____cacheline_aligned_in_smp;
152
153static struct kmem_cache *dio_cache __read_mostly;
154
155
156
157
158static inline unsigned dio_pages_present(struct dio_submit *sdio)
159{
160 return sdio->tail - sdio->head;
161}
162
163
164
165
166static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
167{
168 ssize_t ret;
169
170 ret = iov_iter_get_pages(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES,
171 &sdio->from);
172
173 if (ret < 0 && sdio->blocks_available && (dio->op == REQ_OP_WRITE)) {
174 struct page *page = ZERO_PAGE(0);
175
176
177
178
179
180 if (dio->page_errors == 0)
181 dio->page_errors = ret;
182 get_page(page);
183 dio->pages[0] = page;
184 sdio->head = 0;
185 sdio->tail = 1;
186 sdio->from = 0;
187 sdio->to = PAGE_SIZE;
188 return 0;
189 }
190
191 if (ret >= 0) {
192 iov_iter_advance(sdio->iter, ret);
193 ret += sdio->from;
194 sdio->head = 0;
195 sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
196 sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
197 return 0;
198 }
199 return ret;
200}
201
202
203
204
205
206
207
208static inline struct page *dio_get_page(struct dio *dio,
209 struct dio_submit *sdio)
210{
211 if (dio_pages_present(sdio) == 0) {
212 int ret;
213
214 ret = dio_refill_pages(dio, sdio);
215 if (ret)
216 return ERR_PTR(ret);
217 BUG_ON(dio_pages_present(sdio) == 0);
218 }
219 return dio->pages[sdio->head];
220}
221
222
223
224
225void dio_warn_stale_pagecache(struct file *filp)
226{
227 static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
228 char pathname[128];
229 struct inode *inode = file_inode(filp);
230 char *path;
231
232 errseq_set(&inode->i_mapping->wb_err, -EIO);
233 if (__ratelimit(&_rs)) {
234 path = file_path(filp, pathname, sizeof(pathname));
235 if (IS_ERR(path))
236 path = "(unknown)";
237 pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n");
238 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
239 current->comm);
240 }
241}
242
243
244
245
246
247
248
249
250
251
252
253
254
255static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags)
256{
257 loff_t offset = dio->iocb->ki_pos;
258 ssize_t transferred = 0;
259 int err;
260
261
262
263
264
265
266
267 if (ret == -EIOCBQUEUED)
268 ret = 0;
269
270 if (dio->result) {
271 transferred = dio->result;
272
273
274 if ((dio->op == REQ_OP_READ) &&
275 ((offset + transferred) > dio->i_size))
276 transferred = dio->i_size - offset;
277
278 if (unlikely(ret == -EFAULT) && transferred)
279 ret = 0;
280 }
281
282 if (ret == 0)
283 ret = dio->page_errors;
284 if (ret == 0)
285 ret = dio->io_error;
286 if (ret == 0)
287 ret = transferred;
288
289 if (dio->end_io) {
290
291 err = dio->end_io(dio->iocb, offset, ret, dio->private);
292 if (err)
293 ret = err;
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308 if (flags & DIO_COMPLETE_INVALIDATE &&
309 ret > 0 && dio->op == REQ_OP_WRITE &&
310 dio->inode->i_mapping->nrpages) {
311 err = invalidate_inode_pages2_range(dio->inode->i_mapping,
312 offset >> PAGE_SHIFT,
313 (offset + ret - 1) >> PAGE_SHIFT);
314 if (err)
315 dio_warn_stale_pagecache(dio->iocb->ki_filp);
316 }
317
318 inode_dio_end(dio->inode);
319
320 if (flags & DIO_COMPLETE_ASYNC) {
321
322
323
324
325
326 dio->iocb->ki_pos += transferred;
327
328 if (ret > 0 && dio->op == REQ_OP_WRITE)
329 ret = generic_write_sync(dio->iocb, ret);
330 dio->iocb->ki_complete(dio->iocb, ret, 0);
331 }
332
333 kmem_cache_free(dio_cache, dio);
334 return ret;
335}
336
337static void dio_aio_complete_work(struct work_struct *work)
338{
339 struct dio *dio = container_of(work, struct dio, complete_work);
340
341 dio_complete(dio, 0, DIO_COMPLETE_ASYNC | DIO_COMPLETE_INVALIDATE);
342}
343
344static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio);
345
346
347
348
349static void dio_bio_end_aio(struct bio *bio)
350{
351 struct dio *dio = bio->bi_private;
352 unsigned long remaining;
353 unsigned long flags;
354 bool defer_completion = false;
355
356
357 dio_bio_complete(dio, bio);
358
359 spin_lock_irqsave(&dio->bio_lock, flags);
360 remaining = --dio->refcount;
361 if (remaining == 1 && dio->waiter)
362 wake_up_process(dio->waiter);
363 spin_unlock_irqrestore(&dio->bio_lock, flags);
364
365 if (remaining == 0) {
366
367
368
369
370
371
372
373
374 if (dio->result)
375 defer_completion = dio->defer_completion ||
376 (dio->op == REQ_OP_WRITE &&
377 dio->inode->i_mapping->nrpages);
378 if (defer_completion) {
379 INIT_WORK(&dio->complete_work, dio_aio_complete_work);
380 queue_work(dio->inode->i_sb->s_dio_done_wq,
381 &dio->complete_work);
382 } else {
383 dio_complete(dio, 0, DIO_COMPLETE_ASYNC);
384 }
385 }
386}
387
388
389
390
391
392
393
394
395static void dio_bio_end_io(struct bio *bio)
396{
397 struct dio *dio = bio->bi_private;
398 unsigned long flags;
399
400 spin_lock_irqsave(&dio->bio_lock, flags);
401 bio->bi_private = dio->bio_list;
402 dio->bio_list = bio;
403 if (--dio->refcount == 1 && dio->waiter)
404 wake_up_process(dio->waiter);
405 spin_unlock_irqrestore(&dio->bio_lock, flags);
406}
407
408
409
410
411
412
413
414
415
416void dio_end_io(struct bio *bio)
417{
418 struct dio *dio = bio->bi_private;
419
420 if (dio->is_async)
421 dio_bio_end_aio(bio);
422 else
423 dio_bio_end_io(bio);
424}
425EXPORT_SYMBOL_GPL(dio_end_io);
426
427static inline void
428dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
429 struct block_device *bdev,
430 sector_t first_sector, int nr_vecs)
431{
432 struct bio *bio;
433
434
435
436
437
438 bio = bio_alloc(GFP_KERNEL, nr_vecs);
439
440 bio_set_dev(bio, bdev);
441 bio->bi_iter.bi_sector = first_sector;
442 bio_set_op_attrs(bio, dio->op, dio->op_flags);
443 if (dio->is_async)
444 bio->bi_end_io = dio_bio_end_aio;
445 else
446 bio->bi_end_io = dio_bio_end_io;
447
448 bio->bi_write_hint = dio->iocb->ki_hint;
449
450 sdio->bio = bio;
451 sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
452}
453
454
455
456
457
458
459
460
461static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)
462{
463 struct bio *bio = sdio->bio;
464 unsigned long flags;
465
466 bio->bi_private = dio;
467
468 spin_lock_irqsave(&dio->bio_lock, flags);
469 dio->refcount++;
470 spin_unlock_irqrestore(&dio->bio_lock, flags);
471
472 if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty)
473 bio_set_pages_dirty(bio);
474
475 dio->bio_disk = bio->bi_disk;
476
477 if (sdio->submit_io) {
478 sdio->submit_io(bio, dio->inode, sdio->logical_offset_in_bio);
479 dio->bio_cookie = BLK_QC_T_NONE;
480 } else
481 dio->bio_cookie = submit_bio(bio);
482
483 sdio->bio = NULL;
484 sdio->boundary = 0;
485 sdio->logical_offset_in_bio = 0;
486}
487
488
489
490
491static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio)
492{
493 while (sdio->head < sdio->tail)
494 put_page(dio->pages[sdio->head++]);
495}
496
497
498
499
500
501
502
503static struct bio *dio_await_one(struct dio *dio)
504{
505 unsigned long flags;
506 struct bio *bio = NULL;
507
508 spin_lock_irqsave(&dio->bio_lock, flags);
509
510
511
512
513
514
515
516 while (dio->refcount > 1 && dio->bio_list == NULL) {
517 __set_current_state(TASK_UNINTERRUPTIBLE);
518 dio->waiter = current;
519 spin_unlock_irqrestore(&dio->bio_lock, flags);
520 if (!(dio->iocb->ki_flags & IOCB_HIPRI) ||
521 !blk_poll(dio->bio_disk->queue, dio->bio_cookie, true))
522 blk_io_schedule();
523
524 spin_lock_irqsave(&dio->bio_lock, flags);
525 dio->waiter = NULL;
526 }
527 if (dio->bio_list) {
528 bio = dio->bio_list;
529 dio->bio_list = bio->bi_private;
530 }
531 spin_unlock_irqrestore(&dio->bio_lock, flags);
532 return bio;
533}
534
535
536
537
538static blk_status_t dio_bio_complete(struct dio *dio, struct bio *bio)
539{
540 struct bio_vec *bvec;
541 unsigned i;
542 blk_status_t err = bio->bi_status;
543
544 if (err) {
545 if (err == BLK_STS_AGAIN && (bio->bi_opf & REQ_NOWAIT))
546 dio->io_error = -EAGAIN;
547 else
548 dio->io_error = -EIO;
549 }
550
551 if (dio->is_async && dio->op == REQ_OP_READ && dio->should_dirty) {
552 bio_check_pages_dirty(bio);
553 } else {
554 bio_for_each_segment_all(bvec, bio, i) {
555 struct page *page = bvec->bv_page;
556
557 if (dio->op == REQ_OP_READ && !PageCompound(page) &&
558 dio->should_dirty)
559 set_page_dirty_lock(page);
560 put_page(page);
561 }
562 bio_put(bio);
563 }
564 return err;
565}
566
567
568
569
570
571
572
573
574static void dio_await_completion(struct dio *dio)
575{
576 struct bio *bio;
577 do {
578 bio = dio_await_one(dio);
579 if (bio)
580 dio_bio_complete(dio, bio);
581 } while (bio);
582}
583
584
585
586
587
588
589
590
591static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
592{
593 int ret = 0;
594
595 if (sdio->reap_counter++ >= 64) {
596 while (dio->bio_list) {
597 unsigned long flags;
598 struct bio *bio;
599 int ret2;
600
601 spin_lock_irqsave(&dio->bio_lock, flags);
602 bio = dio->bio_list;
603 dio->bio_list = bio->bi_private;
604 spin_unlock_irqrestore(&dio->bio_lock, flags);
605 ret2 = blk_status_to_errno(dio_bio_complete(dio, bio));
606 if (ret == 0)
607 ret = ret2;
608 }
609 sdio->reap_counter = 0;
610 }
611 return ret;
612}
613
614
615
616
617
618
619
620int sb_init_dio_done_wq(struct super_block *sb)
621{
622 struct workqueue_struct *old;
623 struct workqueue_struct *wq = alloc_workqueue("dio/%s",
624 WQ_MEM_RECLAIM, 0,
625 sb->s_id);
626 if (!wq)
627 return -ENOMEM;
628
629
630
631 old = cmpxchg(&sb->s_dio_done_wq, NULL, wq);
632
633 if (old)
634 destroy_workqueue(wq);
635 return 0;
636}
637
638static int dio_set_defer_completion(struct dio *dio)
639{
640 struct super_block *sb = dio->inode->i_sb;
641
642 if (dio->defer_completion)
643 return 0;
644 dio->defer_completion = true;
645 if (!sb->s_dio_done_wq)
646 return sb_init_dio_done_wq(sb);
647 return 0;
648}
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673static int get_more_blocks(struct dio *dio, struct dio_submit *sdio,
674 struct buffer_head *map_bh)
675{
676 int ret;
677 sector_t fs_startblk;
678 sector_t fs_endblk;
679 unsigned long fs_count;
680 int create;
681 unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
682
683
684
685
686
687 ret = dio->page_errors;
688 if (ret == 0) {
689 BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
690 fs_startblk = sdio->block_in_file >> sdio->blkfactor;
691 fs_endblk = (sdio->final_block_in_request - 1) >>
692 sdio->blkfactor;
693 fs_count = fs_endblk - fs_startblk + 1;
694
695 map_bh->b_state = 0;
696 map_bh->b_size = fs_count << i_blkbits;
697
698
699
700
701
702
703
704
705
706
707
708
709 create = dio->op == REQ_OP_WRITE;
710 if (dio->flags & DIO_SKIP_HOLES) {
711 if (fs_startblk <= ((i_size_read(dio->inode) - 1) >>
712 i_blkbits))
713 create = 0;
714 }
715
716 ret = (*sdio->get_block)(dio->inode, fs_startblk,
717 map_bh, create);
718
719
720 dio->private = map_bh->b_private;
721
722 if (ret == 0 && buffer_defer_completion(map_bh))
723 ret = dio_set_defer_completion(dio);
724 }
725 return ret;
726}
727
728
729
730
731static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
732 sector_t start_sector, struct buffer_head *map_bh)
733{
734 sector_t sector;
735 int ret, nr_pages;
736
737 ret = dio_bio_reap(dio, sdio);
738 if (ret)
739 goto out;
740 sector = start_sector << (sdio->blkbits - 9);
741 nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES);
742 BUG_ON(nr_pages <= 0);
743 dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
744 sdio->boundary = 0;
745out:
746 return ret;
747}
748
749
750
751
752
753
754
755
756static inline int dio_bio_add_page(struct dio_submit *sdio)
757{
758 int ret;
759
760 ret = bio_add_page(sdio->bio, sdio->cur_page,
761 sdio->cur_page_len, sdio->cur_page_offset);
762 if (ret == sdio->cur_page_len) {
763
764
765
766 if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
767 sdio->pages_in_io--;
768 get_page(sdio->cur_page);
769 sdio->final_block_in_bio = sdio->cur_page_block +
770 (sdio->cur_page_len >> sdio->blkbits);
771 ret = 0;
772 } else {
773 ret = 1;
774 }
775 return ret;
776}
777
778
779
780
781
782
783
784
785
786
787
788static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
789 struct buffer_head *map_bh)
790{
791 int ret = 0;
792
793 if (sdio->bio) {
794 loff_t cur_offset = sdio->cur_page_fs_offset;
795 loff_t bio_next_offset = sdio->logical_offset_in_bio +
796 sdio->bio->bi_iter.bi_size;
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812 if (sdio->final_block_in_bio != sdio->cur_page_block ||
813 cur_offset != bio_next_offset)
814 dio_bio_submit(dio, sdio);
815 }
816
817 if (sdio->bio == NULL) {
818 ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
819 if (ret)
820 goto out;
821 }
822
823 if (dio_bio_add_page(sdio) != 0) {
824 dio_bio_submit(dio, sdio);
825 ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
826 if (ret == 0) {
827 ret = dio_bio_add_page(sdio);
828 BUG_ON(ret != 0);
829 }
830 }
831out:
832 return ret;
833}
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852static inline int
853submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
854 unsigned offset, unsigned len, sector_t blocknr,
855 struct buffer_head *map_bh)
856{
857 int ret = 0;
858
859 if (dio->op == REQ_OP_WRITE) {
860
861
862
863 task_io_account_write(len);
864 }
865
866
867
868
869 if (sdio->cur_page == page &&
870 sdio->cur_page_offset + sdio->cur_page_len == offset &&
871 sdio->cur_page_block +
872 (sdio->cur_page_len >> sdio->blkbits) == blocknr) {
873 sdio->cur_page_len += len;
874 goto out;
875 }
876
877
878
879
880 if (sdio->cur_page) {
881 ret = dio_send_cur_page(dio, sdio, map_bh);
882 put_page(sdio->cur_page);
883 sdio->cur_page = NULL;
884 if (ret)
885 return ret;
886 }
887
888 get_page(page);
889 sdio->cur_page = page;
890 sdio->cur_page_offset = offset;
891 sdio->cur_page_len = len;
892 sdio->cur_page_block = blocknr;
893 sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
894out:
895
896
897
898
899 if (sdio->boundary) {
900 ret = dio_send_cur_page(dio, sdio, map_bh);
901 if (sdio->bio)
902 dio_bio_submit(dio, sdio);
903 put_page(sdio->cur_page);
904 sdio->cur_page = NULL;
905 }
906 return ret;
907}
908
909
910
911
912
913
914
915
916
917
918static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
919 int end, struct buffer_head *map_bh)
920{
921 unsigned dio_blocks_per_fs_block;
922 unsigned this_chunk_blocks;
923 unsigned this_chunk_bytes;
924 struct page *page;
925
926 sdio->start_zero_done = 1;
927 if (!sdio->blkfactor || !buffer_new(map_bh))
928 return;
929
930 dio_blocks_per_fs_block = 1 << sdio->blkfactor;
931 this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
932
933 if (!this_chunk_blocks)
934 return;
935
936
937
938
939
940 if (end)
941 this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
942
943 this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
944
945 page = ZERO_PAGE(0);
946 if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
947 sdio->next_block_for_io, map_bh))
948 return;
949
950 sdio->next_block_for_io += this_chunk_blocks;
951}
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969static int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
970 struct buffer_head *map_bh)
971{
972 const unsigned blkbits = sdio->blkbits;
973 const unsigned i_blkbits = blkbits + sdio->blkfactor;
974 int ret = 0;
975
976 while (sdio->block_in_file < sdio->final_block_in_request) {
977 struct page *page;
978 size_t from, to;
979
980 page = dio_get_page(dio, sdio);
981 if (IS_ERR(page)) {
982 ret = PTR_ERR(page);
983 goto out;
984 }
985 from = sdio->head ? 0 : sdio->from;
986 to = (sdio->head == sdio->tail - 1) ? sdio->to : PAGE_SIZE;
987 sdio->head++;
988
989 while (from < to) {
990 unsigned this_chunk_bytes;
991 unsigned this_chunk_blocks;
992 unsigned u;
993
994 if (sdio->blocks_available == 0) {
995
996
997
998 unsigned long blkmask;
999 unsigned long dio_remainder;
1000
1001 ret = get_more_blocks(dio, sdio, map_bh);
1002 if (ret) {
1003 put_page(page);
1004 goto out;
1005 }
1006 if (!buffer_mapped(map_bh))
1007 goto do_holes;
1008
1009 sdio->blocks_available =
1010 map_bh->b_size >> blkbits;
1011 sdio->next_block_for_io =
1012 map_bh->b_blocknr << sdio->blkfactor;
1013 if (buffer_new(map_bh)) {
1014 clean_bdev_aliases(
1015 map_bh->b_bdev,
1016 map_bh->b_blocknr,
1017 map_bh->b_size >> i_blkbits);
1018 }
1019
1020 if (!sdio->blkfactor)
1021 goto do_holes;
1022
1023 blkmask = (1 << sdio->blkfactor) - 1;
1024 dio_remainder = (sdio->block_in_file & blkmask);
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 if (!buffer_new(map_bh))
1038 sdio->next_block_for_io += dio_remainder;
1039 sdio->blocks_available -= dio_remainder;
1040 }
1041do_holes:
1042
1043 if (!buffer_mapped(map_bh)) {
1044 loff_t i_size_aligned;
1045
1046
1047 if (dio->op == REQ_OP_WRITE) {
1048 put_page(page);
1049 return -ENOTBLK;
1050 }
1051
1052
1053
1054
1055
1056 i_size_aligned = ALIGN(i_size_read(dio->inode),
1057 1 << blkbits);
1058 if (sdio->block_in_file >=
1059 i_size_aligned >> blkbits) {
1060
1061 put_page(page);
1062 goto out;
1063 }
1064 zero_user(page, from, 1 << blkbits);
1065 sdio->block_in_file++;
1066 from += 1 << blkbits;
1067 dio->result += 1 << blkbits;
1068 goto next_block;
1069 }
1070
1071
1072
1073
1074
1075
1076 if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
1077 dio_zero_block(dio, sdio, 0, map_bh);
1078
1079
1080
1081
1082
1083 this_chunk_blocks = sdio->blocks_available;
1084 u = (to - from) >> blkbits;
1085 if (this_chunk_blocks > u)
1086 this_chunk_blocks = u;
1087 u = sdio->final_block_in_request - sdio->block_in_file;
1088 if (this_chunk_blocks > u)
1089 this_chunk_blocks = u;
1090 this_chunk_bytes = this_chunk_blocks << blkbits;
1091 BUG_ON(this_chunk_bytes == 0);
1092
1093 if (this_chunk_blocks == sdio->blocks_available)
1094 sdio->boundary = buffer_boundary(map_bh);
1095 ret = submit_page_section(dio, sdio, page,
1096 from,
1097 this_chunk_bytes,
1098 sdio->next_block_for_io,
1099 map_bh);
1100 if (ret) {
1101 put_page(page);
1102 goto out;
1103 }
1104 sdio->next_block_for_io += this_chunk_blocks;
1105
1106 sdio->block_in_file += this_chunk_blocks;
1107 from += this_chunk_bytes;
1108 dio->result += this_chunk_bytes;
1109 sdio->blocks_available -= this_chunk_blocks;
1110next_block:
1111 BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
1112 if (sdio->block_in_file == sdio->final_block_in_request)
1113 break;
1114 }
1115
1116
1117 put_page(page);
1118 }
1119out:
1120 return ret;
1121}
1122
1123static inline int drop_refcount(struct dio *dio)
1124{
1125 int ret2;
1126 unsigned long flags;
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 spin_lock_irqsave(&dio->bio_lock, flags);
1140 ret2 = --dio->refcount;
1141 spin_unlock_irqrestore(&dio->bio_lock, flags);
1142 return ret2;
1143}
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170static inline ssize_t
1171do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
1172 struct block_device *bdev, struct iov_iter *iter,
1173 get_block_t get_block, dio_iodone_t end_io,
1174 dio_submit_t submit_io, int flags)
1175{
1176 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
1177 unsigned blkbits = i_blkbits;
1178 unsigned blocksize_mask = (1 << blkbits) - 1;
1179 ssize_t retval = -EINVAL;
1180 const size_t count = iov_iter_count(iter);
1181 loff_t offset = iocb->ki_pos;
1182 const loff_t end = offset + count;
1183 struct dio *dio;
1184 struct dio_submit sdio = { 0, };
1185 struct buffer_head map_bh = { 0, };
1186 struct blk_plug plug;
1187 unsigned long align = offset | iov_iter_alignment(iter);
1188
1189
1190
1191
1192
1193
1194 if (align & blocksize_mask) {
1195 if (bdev)
1196 blkbits = blksize_bits(bdev_logical_block_size(bdev));
1197 blocksize_mask = (1 << blkbits) - 1;
1198 if (align & blocksize_mask)
1199 goto out;
1200 }
1201
1202
1203 if (iov_iter_rw(iter) == READ && !count)
1204 return 0;
1205
1206 dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
1207 retval = -ENOMEM;
1208 if (!dio)
1209 goto out;
1210
1211
1212
1213
1214
1215 memset(dio, 0, offsetof(struct dio, pages));
1216
1217 dio->flags = flags;
1218 if (dio->flags & DIO_LOCKING) {
1219 if (iov_iter_rw(iter) == READ) {
1220 struct address_space *mapping =
1221 iocb->ki_filp->f_mapping;
1222
1223
1224 inode_lock(inode);
1225
1226 retval = filemap_write_and_wait_range(mapping, offset,
1227 end - 1);
1228 if (retval) {
1229 inode_unlock(inode);
1230 kmem_cache_free(dio_cache, dio);
1231 goto out;
1232 }
1233 }
1234 }
1235
1236
1237 dio->i_size = i_size_read(inode);
1238 if (iov_iter_rw(iter) == READ && offset >= dio->i_size) {
1239 if (dio->flags & DIO_LOCKING)
1240 inode_unlock(inode);
1241 kmem_cache_free(dio_cache, dio);
1242 retval = 0;
1243 goto out;
1244 }
1245
1246
1247
1248
1249
1250
1251
1252 if (is_sync_kiocb(iocb))
1253 dio->is_async = false;
1254 else if (iov_iter_rw(iter) == WRITE && end > i_size_read(inode))
1255 dio->is_async = false;
1256 else
1257 dio->is_async = true;
1258
1259 dio->inode = inode;
1260 if (iov_iter_rw(iter) == WRITE) {
1261 dio->op = REQ_OP_WRITE;
1262 dio->op_flags = REQ_SYNC | REQ_IDLE;
1263 if (iocb->ki_flags & IOCB_NOWAIT)
1264 dio->op_flags |= REQ_NOWAIT;
1265 } else {
1266 dio->op = REQ_OP_READ;
1267 }
1268 if (iocb->ki_flags & IOCB_HIPRI)
1269 dio->op_flags |= REQ_HIPRI;
1270
1271
1272
1273
1274
1275 if (dio->is_async && iov_iter_rw(iter) == WRITE) {
1276 retval = 0;
1277 if (iocb->ki_flags & IOCB_DSYNC)
1278 retval = dio_set_defer_completion(dio);
1279 else if (!dio->inode->i_sb->s_dio_done_wq) {
1280
1281
1282
1283
1284
1285 retval = sb_init_dio_done_wq(dio->inode->i_sb);
1286 }
1287 if (retval) {
1288
1289
1290
1291
1292 kmem_cache_free(dio_cache, dio);
1293 goto out;
1294 }
1295 }
1296
1297
1298
1299
1300 inode_dio_begin(inode);
1301
1302 retval = 0;
1303 sdio.blkbits = blkbits;
1304 sdio.blkfactor = i_blkbits - blkbits;
1305 sdio.block_in_file = offset >> blkbits;
1306
1307 sdio.get_block = get_block;
1308 dio->end_io = end_io;
1309 sdio.submit_io = submit_io;
1310 sdio.final_block_in_bio = -1;
1311 sdio.next_block_for_io = -1;
1312
1313 dio->iocb = iocb;
1314
1315 spin_lock_init(&dio->bio_lock);
1316 dio->refcount = 1;
1317
1318 dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;
1319 sdio.iter = iter;
1320 sdio.final_block_in_request = end >> blkbits;
1321
1322
1323
1324
1325
1326 if (unlikely(sdio.blkfactor))
1327 sdio.pages_in_io = 2;
1328
1329 sdio.pages_in_io += iov_iter_npages(iter, INT_MAX);
1330
1331 blk_start_plug(&plug);
1332
1333 retval = do_direct_IO(dio, &sdio, &map_bh);
1334 if (retval)
1335 dio_cleanup(dio, &sdio);
1336
1337 if (retval == -ENOTBLK) {
1338
1339
1340
1341
1342 retval = 0;
1343 }
1344
1345
1346
1347
1348 dio_zero_block(dio, &sdio, 1, &map_bh);
1349
1350 if (sdio.cur_page) {
1351 ssize_t ret2;
1352
1353 ret2 = dio_send_cur_page(dio, &sdio, &map_bh);
1354 if (retval == 0)
1355 retval = ret2;
1356 put_page(sdio.cur_page);
1357 sdio.cur_page = NULL;
1358 }
1359 if (sdio.bio)
1360 dio_bio_submit(dio, &sdio);
1361
1362 blk_finish_plug(&plug);
1363
1364
1365
1366
1367
1368 dio_cleanup(dio, &sdio);
1369
1370
1371
1372
1373
1374
1375 if (iov_iter_rw(iter) == READ && (dio->flags & DIO_LOCKING))
1376 inode_unlock(dio->inode);
1377
1378
1379
1380
1381
1382
1383
1384
1385 BUG_ON(retval == -EIOCBQUEUED);
1386 if (dio->is_async && retval == 0 && dio->result &&
1387 (iov_iter_rw(iter) == READ || dio->result == count))
1388 retval = -EIOCBQUEUED;
1389 else
1390 dio_await_completion(dio);
1391
1392 if (drop_refcount(dio) == 0) {
1393 retval = dio_complete(dio, retval, DIO_COMPLETE_INVALIDATE);
1394 } else
1395 BUG_ON(retval != -EIOCBQUEUED);
1396
1397out:
1398 return retval;
1399}
1400
1401ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
1402 struct block_device *bdev, struct iov_iter *iter,
1403 get_block_t get_block,
1404 dio_iodone_t end_io, dio_submit_t submit_io,
1405 int flags)
1406{
1407
1408
1409
1410
1411
1412
1413
1414
1415 prefetch(&bdev->bd_disk->part_tbl);
1416 prefetch(bdev->bd_queue);
1417 prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES);
1418
1419 return do_blockdev_direct_IO(iocb, inode, bdev, iter, get_block,
1420 end_io, submit_io, flags);
1421}
1422
1423EXPORT_SYMBOL(__blockdev_direct_IO);
1424
1425static __init int dio_init(void)
1426{
1427 dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
1428 return 0;
1429}
1430module_init(dio_init)
1431