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