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