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