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 <asm/atomic.h>
39
40
41
42
43
44#define DIO_PAGES 64
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65struct dio {
66
67 struct bio *bio;
68 struct inode *inode;
69 int rw;
70 loff_t i_size;
71 int lock_type;
72 unsigned blkbits;
73 unsigned blkfactor;
74
75
76
77
78 unsigned start_zero_done;
79
80
81 int pages_in_io;
82 size_t size;
83 sector_t block_in_file;
84
85 unsigned blocks_available;
86 sector_t final_block_in_request;
87 unsigned first_block_in_page;
88 int boundary;
89 int reap_counter;
90 get_block_t *get_block;
91 dio_iodone_t *end_io;
92 sector_t final_block_in_bio;
93 sector_t next_block_for_io;
94
95 struct buffer_head map_bh;
96
97
98
99
100
101
102 struct page *cur_page;
103 unsigned cur_page_offset;
104 unsigned cur_page_len;
105 sector_t cur_page_block;
106
107
108
109
110 int curr_page;
111 int total_pages;
112 unsigned long curr_user_address;
113
114
115
116
117
118 struct page *pages[DIO_PAGES];
119 unsigned head;
120 unsigned tail;
121 int page_errors;
122
123
124 spinlock_t bio_lock;
125 unsigned long refcount;
126 struct bio *bio_list;
127 struct task_struct *waiter;
128
129
130 struct kiocb *iocb;
131 int is_async;
132 int io_error;
133 ssize_t result;
134};
135
136
137
138
139static inline unsigned dio_pages_present(struct dio *dio)
140{
141 return dio->tail - dio->head;
142}
143
144
145
146
147static int dio_refill_pages(struct dio *dio)
148{
149 int ret;
150 int nr_pages;
151
152 nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
153 down_read(¤t->mm->mmap_sem);
154 ret = get_user_pages(
155 current,
156 current->mm,
157 dio->curr_user_address,
158 nr_pages,
159 dio->rw == READ,
160 0,
161 &dio->pages[0],
162 NULL);
163 up_read(¤t->mm->mmap_sem);
164
165 if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) {
166 struct page *page = ZERO_PAGE(dio->curr_user_address);
167
168
169
170
171
172 if (dio->page_errors == 0)
173 dio->page_errors = ret;
174 page_cache_get(page);
175 dio->pages[0] = page;
176 dio->head = 0;
177 dio->tail = 1;
178 ret = 0;
179 goto out;
180 }
181
182 if (ret >= 0) {
183 dio->curr_user_address += ret * PAGE_SIZE;
184 dio->curr_page += ret;
185 dio->head = 0;
186 dio->tail = ret;
187 ret = 0;
188 }
189out:
190 return ret;
191}
192
193
194
195
196
197
198
199static struct page *dio_get_page(struct dio *dio)
200{
201 if (dio_pages_present(dio) == 0) {
202 int ret;
203
204 ret = dio_refill_pages(dio);
205 if (ret)
206 return ERR_PTR(ret);
207 BUG_ON(dio_pages_present(dio) == 0);
208 }
209 return dio->pages[dio->head++];
210}
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225static int dio_complete(struct dio *dio, loff_t offset, int ret)
226{
227 ssize_t transferred = 0;
228
229
230
231
232
233
234
235 if (ret == -EIOCBQUEUED)
236 ret = 0;
237
238 if (dio->result) {
239 transferred = dio->result;
240
241
242 if ((dio->rw == READ) && ((offset + transferred) > dio->i_size))
243 transferred = dio->i_size - offset;
244 }
245
246 if (dio->end_io && dio->result)
247 dio->end_io(dio->iocb, offset, transferred,
248 dio->map_bh.b_private);
249 if (dio->lock_type == DIO_LOCKING)
250
251 up_read_non_owner(&dio->inode->i_alloc_sem);
252
253 if (ret == 0)
254 ret = dio->page_errors;
255 if (ret == 0)
256 ret = dio->io_error;
257 if (ret == 0)
258 ret = transferred;
259
260 return ret;
261}
262
263static int dio_bio_complete(struct dio *dio, struct bio *bio);
264
265
266
267static int dio_bio_end_aio(struct bio *bio, unsigned int bytes_done, int error)
268{
269 struct dio *dio = bio->bi_private;
270 unsigned long remaining;
271 unsigned long flags;
272
273 if (bio->bi_size)
274 return 1;
275
276
277 dio_bio_complete(dio, bio);
278
279 spin_lock_irqsave(&dio->bio_lock, flags);
280 remaining = --dio->refcount;
281 if (remaining == 1 && dio->waiter)
282 wake_up_process(dio->waiter);
283 spin_unlock_irqrestore(&dio->bio_lock, flags);
284
285 if (remaining == 0) {
286 int ret = dio_complete(dio, dio->iocb->ki_pos, 0);
287 aio_complete(dio->iocb, ret, 0);
288 kfree(dio);
289 }
290
291 return 0;
292}
293
294
295
296
297
298
299
300
301static int dio_bio_end_io(struct bio *bio, unsigned int bytes_done, int error)
302{
303 struct dio *dio = bio->bi_private;
304 unsigned long flags;
305
306 if (bio->bi_size)
307 return 1;
308
309 spin_lock_irqsave(&dio->bio_lock, flags);
310 bio->bi_private = dio->bio_list;
311 dio->bio_list = bio;
312 if (--dio->refcount == 1 && dio->waiter)
313 wake_up_process(dio->waiter);
314 spin_unlock_irqrestore(&dio->bio_lock, flags);
315 return 0;
316}
317
318static int
319dio_bio_alloc(struct dio *dio, struct block_device *bdev,
320 sector_t first_sector, int nr_vecs)
321{
322 struct bio *bio;
323
324 bio = bio_alloc(GFP_KERNEL, nr_vecs);
325 if (bio == NULL)
326 return -ENOMEM;
327
328 bio->bi_bdev = bdev;
329 bio->bi_sector = first_sector;
330 if (dio->is_async)
331 bio->bi_end_io = dio_bio_end_aio;
332 else
333 bio->bi_end_io = dio_bio_end_io;
334
335 dio->bio = bio;
336 return 0;
337}
338
339
340
341
342
343
344
345
346static void dio_bio_submit(struct dio *dio)
347{
348 struct bio *bio = dio->bio;
349 unsigned long flags;
350
351 bio->bi_private = dio;
352
353 spin_lock_irqsave(&dio->bio_lock, flags);
354 dio->refcount++;
355 spin_unlock_irqrestore(&dio->bio_lock, flags);
356
357 if (dio->is_async && dio->rw == READ)
358 bio_set_pages_dirty(bio);
359
360 submit_bio(dio->rw, bio);
361
362 dio->bio = NULL;
363 dio->boundary = 0;
364}
365
366
367
368
369static void dio_cleanup(struct dio *dio)
370{
371 while (dio_pages_present(dio))
372 page_cache_release(dio_get_page(dio));
373}
374
375
376
377
378
379
380
381static struct bio *dio_await_one(struct dio *dio)
382{
383 unsigned long flags;
384 struct bio *bio = NULL;
385
386 spin_lock_irqsave(&dio->bio_lock, flags);
387
388
389
390
391
392
393
394 while (dio->refcount > 1 && dio->bio_list == NULL) {
395 __set_current_state(TASK_UNINTERRUPTIBLE);
396 dio->waiter = current;
397 spin_unlock_irqrestore(&dio->bio_lock, flags);
398 io_schedule();
399
400 spin_lock_irqsave(&dio->bio_lock, flags);
401 dio->waiter = NULL;
402 }
403 if (dio->bio_list) {
404 bio = dio->bio_list;
405 dio->bio_list = bio->bi_private;
406 }
407 spin_unlock_irqrestore(&dio->bio_lock, flags);
408 return bio;
409}
410
411
412
413
414static int dio_bio_complete(struct dio *dio, struct bio *bio)
415{
416 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
417 struct bio_vec *bvec = bio->bi_io_vec;
418 int page_no;
419
420 if (!uptodate)
421 dio->io_error = -EIO;
422
423 if (dio->is_async && dio->rw == READ) {
424 bio_check_pages_dirty(bio);
425 } else {
426 for (page_no = 0; page_no < bio->bi_vcnt; page_no++) {
427 struct page *page = bvec[page_no].bv_page;
428
429 if (dio->rw == READ && !PageCompound(page))
430 set_page_dirty_lock(page);
431 page_cache_release(page);
432 }
433 bio_put(bio);
434 }
435 return uptodate ? 0 : -EIO;
436}
437
438
439
440
441
442
443
444
445static void dio_await_completion(struct dio *dio)
446{
447 struct bio *bio;
448 do {
449 bio = dio_await_one(dio);
450 if (bio)
451 dio_bio_complete(dio, bio);
452 } while (bio);
453}
454
455
456
457
458
459
460
461
462static int dio_bio_reap(struct dio *dio)
463{
464 int ret = 0;
465
466 if (dio->reap_counter++ >= 64) {
467 while (dio->bio_list) {
468 unsigned long flags;
469 struct bio *bio;
470 int ret2;
471
472 spin_lock_irqsave(&dio->bio_lock, flags);
473 bio = dio->bio_list;
474 dio->bio_list = bio->bi_private;
475 spin_unlock_irqrestore(&dio->bio_lock, flags);
476 ret2 = dio_bio_complete(dio, bio);
477 if (ret == 0)
478 ret = ret2;
479 }
480 dio->reap_counter = 0;
481 }
482 return ret;
483}
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508static int get_more_blocks(struct dio *dio)
509{
510 int ret;
511 struct buffer_head *map_bh = &dio->map_bh;
512 sector_t fs_startblk;
513 unsigned long fs_count;
514 unsigned long dio_count;
515 unsigned long blkmask;
516 int create;
517
518
519
520
521
522 ret = dio->page_errors;
523 if (ret == 0) {
524 BUG_ON(dio->block_in_file >= dio->final_block_in_request);
525 fs_startblk = dio->block_in_file >> dio->blkfactor;
526 dio_count = dio->final_block_in_request - dio->block_in_file;
527 fs_count = dio_count >> dio->blkfactor;
528 blkmask = (1 << dio->blkfactor) - 1;
529 if (dio_count & blkmask)
530 fs_count++;
531
532 map_bh->b_state = 0;
533 map_bh->b_size = fs_count << dio->inode->i_blkbits;
534
535 create = dio->rw & WRITE;
536 if (dio->lock_type == DIO_LOCKING) {
537 if (dio->block_in_file < (i_size_read(dio->inode) >>
538 dio->blkbits))
539 create = 0;
540 } else if (dio->lock_type == DIO_NO_LOCKING) {
541 create = 0;
542 }
543
544
545
546
547
548
549
550 ret = (*dio->get_block)(dio->inode, fs_startblk,
551 map_bh, create);
552 }
553 return ret;
554}
555
556
557
558
559static int dio_new_bio(struct dio *dio, sector_t start_sector)
560{
561 sector_t sector;
562 int ret, nr_pages;
563
564 ret = dio_bio_reap(dio);
565 if (ret)
566 goto out;
567 sector = start_sector << (dio->blkbits - 9);
568 nr_pages = min(dio->pages_in_io, bio_get_nr_vecs(dio->map_bh.b_bdev));
569 BUG_ON(nr_pages <= 0);
570 ret = dio_bio_alloc(dio, dio->map_bh.b_bdev, sector, nr_pages);
571 dio->boundary = 0;
572out:
573 return ret;
574}
575
576
577
578
579
580
581
582
583static int dio_bio_add_page(struct dio *dio)
584{
585 int ret;
586
587 ret = bio_add_page(dio->bio, dio->cur_page,
588 dio->cur_page_len, dio->cur_page_offset);
589 if (ret == dio->cur_page_len) {
590
591
592
593 if ((dio->cur_page_len + dio->cur_page_offset) == PAGE_SIZE)
594 dio->pages_in_io--;
595 page_cache_get(dio->cur_page);
596 dio->final_block_in_bio = dio->cur_page_block +
597 (dio->cur_page_len >> dio->blkbits);
598 ret = 0;
599 } else {
600 ret = 1;
601 }
602 return ret;
603}
604
605
606
607
608
609
610
611
612
613
614
615static int dio_send_cur_page(struct dio *dio)
616{
617 int ret = 0;
618
619 if (dio->bio) {
620
621
622
623 if (dio->final_block_in_bio != dio->cur_page_block)
624 dio_bio_submit(dio);
625
626
627
628
629 if (dio->boundary)
630 dio_bio_submit(dio);
631 }
632
633 if (dio->bio == NULL) {
634 ret = dio_new_bio(dio, dio->cur_page_block);
635 if (ret)
636 goto out;
637 }
638
639 if (dio_bio_add_page(dio) != 0) {
640 dio_bio_submit(dio);
641 ret = dio_new_bio(dio, dio->cur_page_block);
642 if (ret == 0) {
643 ret = dio_bio_add_page(dio);
644 BUG_ON(ret != 0);
645 }
646 }
647out:
648 return ret;
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668static int
669submit_page_section(struct dio *dio, struct page *page,
670 unsigned offset, unsigned len, sector_t blocknr)
671{
672 int ret = 0;
673
674 if (dio->rw & WRITE) {
675
676
677
678 task_io_account_write(len);
679 }
680
681
682
683
684 if ( (dio->cur_page == page) &&
685 (dio->cur_page_offset + dio->cur_page_len == offset) &&
686 (dio->cur_page_block +
687 (dio->cur_page_len >> dio->blkbits) == blocknr)) {
688 dio->cur_page_len += len;
689
690
691
692
693
694 if (dio->boundary) {
695 ret = dio_send_cur_page(dio);
696 page_cache_release(dio->cur_page);
697 dio->cur_page = NULL;
698 }
699 goto out;
700 }
701
702
703
704
705 if (dio->cur_page) {
706 ret = dio_send_cur_page(dio);
707 page_cache_release(dio->cur_page);
708 dio->cur_page = NULL;
709 if (ret)
710 goto out;
711 }
712
713 page_cache_get(page);
714 dio->cur_page = page;
715 dio->cur_page_offset = offset;
716 dio->cur_page_len = len;
717 dio->cur_page_block = blocknr;
718out:
719 return ret;
720}
721
722
723
724
725
726
727static void clean_blockdev_aliases(struct dio *dio)
728{
729 unsigned i;
730 unsigned nblocks;
731
732 nblocks = dio->map_bh.b_size >> dio->inode->i_blkbits;
733
734 for (i = 0; i < nblocks; i++) {
735 unmap_underlying_metadata(dio->map_bh.b_bdev,
736 dio->map_bh.b_blocknr + i);
737 }
738}
739
740
741
742
743
744
745
746
747
748
749static void dio_zero_block(struct dio *dio, int end)
750{
751 unsigned dio_blocks_per_fs_block;
752 unsigned this_chunk_blocks;
753 unsigned this_chunk_bytes;
754 struct page *page;
755
756 dio->start_zero_done = 1;
757 if (!dio->blkfactor || !buffer_new(&dio->map_bh))
758 return;
759
760 dio_blocks_per_fs_block = 1 << dio->blkfactor;
761 this_chunk_blocks = dio->block_in_file & (dio_blocks_per_fs_block - 1);
762
763 if (!this_chunk_blocks)
764 return;
765
766
767
768
769
770 if (end)
771 this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
772
773 this_chunk_bytes = this_chunk_blocks << dio->blkbits;
774
775 page = ZERO_PAGE(dio->curr_user_address);
776 if (submit_page_section(dio, page, 0, this_chunk_bytes,
777 dio->next_block_for_io))
778 return;
779
780 dio->next_block_for_io += this_chunk_blocks;
781}
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799static int do_direct_IO(struct dio *dio)
800{
801 const unsigned blkbits = dio->blkbits;
802 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
803 struct page *page;
804 unsigned block_in_page;
805 struct buffer_head *map_bh = &dio->map_bh;
806 int ret = 0;
807
808
809 block_in_page = dio->first_block_in_page;
810
811 while (dio->block_in_file < dio->final_block_in_request) {
812 page = dio_get_page(dio);
813 if (IS_ERR(page)) {
814 ret = PTR_ERR(page);
815 goto out;
816 }
817
818 while (block_in_page < blocks_per_page) {
819 unsigned offset_in_page = block_in_page << blkbits;
820 unsigned this_chunk_bytes;
821 unsigned this_chunk_blocks;
822 unsigned u;
823
824 if (dio->blocks_available == 0) {
825
826
827
828 unsigned long blkmask;
829 unsigned long dio_remainder;
830
831 ret = get_more_blocks(dio);
832 if (ret) {
833 page_cache_release(page);
834 goto out;
835 }
836 if (!buffer_mapped(map_bh))
837 goto do_holes;
838
839 dio->blocks_available =
840 map_bh->b_size >> dio->blkbits;
841 dio->next_block_for_io =
842 map_bh->b_blocknr << dio->blkfactor;
843 if (buffer_new(map_bh))
844 clean_blockdev_aliases(dio);
845
846 if (!dio->blkfactor)
847 goto do_holes;
848
849 blkmask = (1 << dio->blkfactor) - 1;
850 dio_remainder = (dio->block_in_file & blkmask);
851
852
853
854
855
856
857
858
859
860
861
862
863 if (!buffer_new(map_bh))
864 dio->next_block_for_io += dio_remainder;
865 dio->blocks_available -= dio_remainder;
866 }
867do_holes:
868
869 if (!buffer_mapped(map_bh)) {
870 loff_t i_size_aligned;
871
872
873 if (dio->rw & WRITE) {
874 page_cache_release(page);
875 return -ENOTBLK;
876 }
877
878
879
880
881
882 i_size_aligned = ALIGN(i_size_read(dio->inode),
883 1 << blkbits);
884 if (dio->block_in_file >=
885 i_size_aligned >> blkbits) {
886
887 page_cache_release(page);
888 goto out;
889 }
890 zero_user_page(page, block_in_page << blkbits,
891 1 << blkbits, KM_USER0);
892 dio->block_in_file++;
893 block_in_page++;
894 goto next_block;
895 }
896
897
898
899
900
901
902 if (unlikely(dio->blkfactor && !dio->start_zero_done))
903 dio_zero_block(dio, 0);
904
905
906
907
908
909 this_chunk_blocks = dio->blocks_available;
910 u = (PAGE_SIZE - offset_in_page) >> blkbits;
911 if (this_chunk_blocks > u)
912 this_chunk_blocks = u;
913 u = dio->final_block_in_request - dio->block_in_file;
914 if (this_chunk_blocks > u)
915 this_chunk_blocks = u;
916 this_chunk_bytes = this_chunk_blocks << blkbits;
917 BUG_ON(this_chunk_bytes == 0);
918
919 dio->boundary = buffer_boundary(map_bh);
920 ret = submit_page_section(dio, page, offset_in_page,
921 this_chunk_bytes, dio->next_block_for_io);
922 if (ret) {
923 page_cache_release(page);
924 goto out;
925 }
926 dio->next_block_for_io += this_chunk_blocks;
927
928 dio->block_in_file += this_chunk_blocks;
929 block_in_page += this_chunk_blocks;
930 dio->blocks_available -= this_chunk_blocks;
931next_block:
932 BUG_ON(dio->block_in_file > dio->final_block_in_request);
933 if (dio->block_in_file == dio->final_block_in_request)
934 break;
935 }
936
937
938 page_cache_release(page);
939 block_in_page = 0;
940 }
941out:
942 return ret;
943}
944
945
946
947
948static ssize_t
949direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
950 const struct iovec *iov, loff_t offset, unsigned long nr_segs,
951 unsigned blkbits, get_block_t get_block, dio_iodone_t end_io,
952 struct dio *dio)
953{
954 unsigned long user_addr;
955 unsigned long flags;
956 int seg;
957 ssize_t ret = 0;
958 ssize_t ret2;
959 size_t bytes;
960
961 dio->bio = NULL;
962 dio->inode = inode;
963 dio->rw = rw;
964 dio->blkbits = blkbits;
965 dio->blkfactor = inode->i_blkbits - blkbits;
966 dio->start_zero_done = 0;
967 dio->size = 0;
968 dio->block_in_file = offset >> blkbits;
969 dio->blocks_available = 0;
970 dio->cur_page = NULL;
971
972 dio->boundary = 0;
973 dio->reap_counter = 0;
974 dio->get_block = get_block;
975 dio->end_io = end_io;
976 dio->map_bh.b_private = NULL;
977 dio->map_bh.b_state = 0;
978 dio->final_block_in_bio = -1;
979 dio->next_block_for_io = -1;
980
981 dio->page_errors = 0;
982 dio->io_error = 0;
983 dio->result = 0;
984 dio->iocb = iocb;
985 dio->i_size = i_size_read(inode);
986
987 spin_lock_init(&dio->bio_lock);
988 dio->refcount = 1;
989 dio->bio_list = NULL;
990 dio->waiter = NULL;
991
992
993
994
995
996 if (unlikely(dio->blkfactor))
997 dio->pages_in_io = 2;
998 else
999 dio->pages_in_io = 0;
1000
1001 for (seg = 0; seg < nr_segs; seg++) {
1002 user_addr = (unsigned long)iov[seg].iov_base;
1003 dio->pages_in_io +=
1004 ((user_addr+iov[seg].iov_len +PAGE_SIZE-1)/PAGE_SIZE
1005 - user_addr/PAGE_SIZE);
1006 }
1007
1008 for (seg = 0; seg < nr_segs; seg++) {
1009 user_addr = (unsigned long)iov[seg].iov_base;
1010 dio->size += bytes = iov[seg].iov_len;
1011
1012
1013 dio->first_block_in_page = (user_addr & ~PAGE_MASK) >> blkbits;
1014 dio->final_block_in_request = dio->block_in_file +
1015 (bytes >> blkbits);
1016
1017 dio->head = 0;
1018 dio->tail = 0;
1019 dio->curr_page = 0;
1020
1021 dio->total_pages = 0;
1022 if (user_addr & (PAGE_SIZE-1)) {
1023 dio->total_pages++;
1024 bytes -= PAGE_SIZE - (user_addr & (PAGE_SIZE - 1));
1025 }
1026 dio->total_pages += (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1027 dio->curr_user_address = user_addr;
1028
1029 ret = do_direct_IO(dio);
1030
1031 dio->result += iov[seg].iov_len -
1032 ((dio->final_block_in_request - dio->block_in_file) <<
1033 blkbits);
1034
1035 if (ret) {
1036 dio_cleanup(dio);
1037 break;
1038 }
1039 }
1040
1041 if (ret == -ENOTBLK && (rw & WRITE)) {
1042
1043
1044
1045
1046 ret = 0;
1047 }
1048
1049
1050
1051
1052 dio_zero_block(dio, 1);
1053
1054 if (dio->cur_page) {
1055 ret2 = dio_send_cur_page(dio);
1056 if (ret == 0)
1057 ret = ret2;
1058 page_cache_release(dio->cur_page);
1059 dio->cur_page = NULL;
1060 }
1061 if (dio->bio)
1062 dio_bio_submit(dio);
1063
1064
1065 blk_run_address_space(inode->i_mapping);
1066
1067
1068
1069
1070
1071 dio_cleanup(dio);
1072
1073
1074
1075
1076
1077
1078 if ((rw == READ) && (dio->lock_type == DIO_LOCKING))
1079 mutex_unlock(&dio->inode->i_mutex);
1080
1081
1082
1083
1084
1085
1086
1087
1088 BUG_ON(ret == -EIOCBQUEUED);
1089 if (dio->is_async && ret == 0 && dio->result &&
1090 ((rw & READ) || (dio->result == dio->size)))
1091 ret = -EIOCBQUEUED;
1092
1093 if (ret != -EIOCBQUEUED)
1094 dio_await_completion(dio);
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107 spin_lock_irqsave(&dio->bio_lock, flags);
1108 ret2 = --dio->refcount;
1109 spin_unlock_irqrestore(&dio->bio_lock, flags);
1110
1111 if (ret2 == 0) {
1112 ret = dio_complete(dio, offset, ret);
1113 kfree(dio);
1114 } else
1115 BUG_ON(ret != -EIOCBQUEUED);
1116
1117 return ret;
1118}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141ssize_t
1142__blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
1143 struct block_device *bdev, const struct iovec *iov, loff_t offset,
1144 unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
1145 int dio_lock_type)
1146{
1147 int seg;
1148 size_t size;
1149 unsigned long addr;
1150 unsigned blkbits = inode->i_blkbits;
1151 unsigned bdev_blkbits = 0;
1152 unsigned blocksize_mask = (1 << blkbits) - 1;
1153 ssize_t retval = -EINVAL;
1154 loff_t end = offset;
1155 struct dio *dio;
1156 int release_i_mutex = 0;
1157 int acquire_i_mutex = 0;
1158
1159 if (rw & WRITE)
1160 rw = WRITE_SYNC;
1161
1162 if (bdev)
1163 bdev_blkbits = blksize_bits(bdev_hardsect_size(bdev));
1164
1165 if (offset & blocksize_mask) {
1166 if (bdev)
1167 blkbits = bdev_blkbits;
1168 blocksize_mask = (1 << blkbits) - 1;
1169 if (offset & blocksize_mask)
1170 goto out;
1171 }
1172
1173
1174 for (seg = 0; seg < nr_segs; seg++) {
1175 addr = (unsigned long)iov[seg].iov_base;
1176 size = iov[seg].iov_len;
1177 end += size;
1178 if ((addr & blocksize_mask) || (size & blocksize_mask)) {
1179 if (bdev)
1180 blkbits = bdev_blkbits;
1181 blocksize_mask = (1 << blkbits) - 1;
1182 if ((addr & blocksize_mask) || (size & blocksize_mask))
1183 goto out;
1184 }
1185 }
1186
1187 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
1188 retval = -ENOMEM;
1189 if (!dio)
1190 goto out;
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201 dio->lock_type = dio_lock_type;
1202 if (dio_lock_type != DIO_NO_LOCKING) {
1203
1204 if (rw == READ && end > offset) {
1205 struct address_space *mapping;
1206
1207 mapping = iocb->ki_filp->f_mapping;
1208 if (dio_lock_type != DIO_OWN_LOCKING) {
1209 mutex_lock(&inode->i_mutex);
1210 release_i_mutex = 1;
1211 }
1212
1213 retval = filemap_write_and_wait_range(mapping, offset,
1214 end - 1);
1215 if (retval) {
1216 kfree(dio);
1217 goto out;
1218 }
1219
1220 if (dio_lock_type == DIO_OWN_LOCKING) {
1221 mutex_unlock(&inode->i_mutex);
1222 acquire_i_mutex = 1;
1223 }
1224 }
1225
1226 if (dio_lock_type == DIO_LOCKING)
1227
1228 down_read_non_owner(&inode->i_alloc_sem);
1229 }
1230
1231
1232
1233
1234
1235
1236
1237 dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) &&
1238 (end > i_size_read(inode)));
1239
1240 retval = direct_io_worker(rw, iocb, inode, iov, offset,
1241 nr_segs, blkbits, get_block, end_io, dio);
1242
1243 if (rw == READ && dio_lock_type == DIO_LOCKING)
1244 release_i_mutex = 0;
1245
1246out:
1247 if (release_i_mutex)
1248 mutex_unlock(&inode->i_mutex);
1249 else if (acquire_i_mutex)
1250 mutex_lock(&inode->i_mutex);
1251 return retval;
1252}
1253EXPORT_SYMBOL(__blockdev_direct_IO);
1254