@@ -770,30 +770,27 @@ static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
770
770
return ret ? ret : ret1 ;
771
771
}
772
772
773
- static inline bool should_fault_in_pages (ssize_t ret , struct iov_iter * i ,
773
+ static inline bool should_fault_in_pages (struct iov_iter * i ,
774
+ struct kiocb * iocb ,
774
775
size_t * prev_count ,
775
776
size_t * window_size )
776
777
{
777
778
size_t count = iov_iter_count (i );
778
779
size_t size , offs ;
779
780
780
- if (likely (!count ))
781
- return false;
782
- if (ret <= 0 && ret != - EFAULT )
781
+ if (!count )
783
782
return false;
784
783
if (!iter_is_iovec (i ))
785
784
return false;
786
785
787
786
size = PAGE_SIZE ;
788
- offs = offset_in_page (i -> iov [ 0 ]. iov_base + i -> iov_offset );
787
+ offs = offset_in_page (iocb -> ki_pos );
789
788
if (* prev_count != count || !* window_size ) {
790
789
size_t nr_dirtied ;
791
790
792
- size = ALIGN (offs + count , PAGE_SIZE );
793
- size = min_t (size_t , size , SZ_1M );
794
791
nr_dirtied = max (current -> nr_dirtied_pause -
795
792
current -> nr_dirtied , 8 );
796
- size = min ( size , nr_dirtied << PAGE_SHIFT );
793
+ size = min_t ( size_t , SZ_1M , nr_dirtied << PAGE_SHIFT );
797
794
}
798
795
799
796
* prev_count = count ;
@@ -807,7 +804,7 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
807
804
struct file * file = iocb -> ki_filp ;
808
805
struct gfs2_inode * ip = GFS2_I (file -> f_mapping -> host );
809
806
size_t prev_count = 0 , window_size = 0 ;
810
- size_t written = 0 ;
807
+ size_t read = 0 ;
811
808
ssize_t ret ;
812
809
813
810
/*
@@ -835,35 +832,31 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
835
832
ret = gfs2_glock_nq (gh );
836
833
if (ret )
837
834
goto out_uninit ;
838
- retry_under_glock :
839
835
pagefault_disable ();
840
836
to -> nofault = true;
841
837
ret = iomap_dio_rw (iocb , to , & gfs2_iomap_ops , NULL ,
842
- IOMAP_DIO_PARTIAL , written );
838
+ IOMAP_DIO_PARTIAL , read );
843
839
to -> nofault = false;
844
840
pagefault_enable ();
841
+ if (ret <= 0 && ret != - EFAULT )
842
+ goto out_unlock ;
845
843
if (ret > 0 )
846
- written = ret ;
847
-
848
- if (should_fault_in_pages (ret , to , & prev_count , & window_size )) {
849
- size_t leftover ;
844
+ read = ret ;
850
845
851
- gfs2_holder_allow_demote (gh );
852
- leftover = fault_in_iov_iter_writeable (to , window_size );
853
- gfs2_holder_disallow_demote (gh );
854
- if (leftover != window_size ) {
855
- if (gfs2_holder_queued (gh ))
856
- goto retry_under_glock ;
846
+ if (should_fault_in_pages (to , iocb , & prev_count , & window_size )) {
847
+ gfs2_glock_dq (gh );
848
+ window_size -= fault_in_iov_iter_writeable (to , window_size );
849
+ if (window_size )
857
850
goto retry ;
858
- }
859
851
}
852
+ out_unlock :
860
853
if (gfs2_holder_queued (gh ))
861
854
gfs2_glock_dq (gh );
862
855
out_uninit :
863
856
gfs2_holder_uninit (gh );
864
857
if (ret < 0 )
865
858
return ret ;
866
- return written ;
859
+ return read ;
867
860
}
868
861
869
862
static ssize_t gfs2_file_direct_write (struct kiocb * iocb , struct iov_iter * from ,
@@ -873,7 +866,7 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
873
866
struct inode * inode = file -> f_mapping -> host ;
874
867
struct gfs2_inode * ip = GFS2_I (inode );
875
868
size_t prev_count = 0 , window_size = 0 ;
876
- size_t read = 0 ;
869
+ size_t written = 0 ;
877
870
ssize_t ret ;
878
871
879
872
/*
@@ -901,47 +894,43 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
901
894
goto out_uninit ;
902
895
/* Silently fall back to buffered I/O when writing beyond EOF */
903
896
if (iocb -> ki_pos + iov_iter_count (from ) > i_size_read (& ip -> i_inode ))
904
- goto out ;
905
- retry_under_glock :
897
+ goto out_unlock ;
906
898
907
899
from -> nofault = true;
908
900
ret = iomap_dio_rw (iocb , from , & gfs2_iomap_ops , NULL ,
909
- IOMAP_DIO_PARTIAL , read );
901
+ IOMAP_DIO_PARTIAL , written );
910
902
from -> nofault = false;
911
-
912
- if (ret == - ENOTBLK )
913
- ret = 0 ;
903
+ if (ret <= 0 ) {
904
+ if (ret == - ENOTBLK )
905
+ ret = 0 ;
906
+ if (ret != - EFAULT )
907
+ goto out_unlock ;
908
+ }
914
909
if (ret > 0 )
915
- read = ret ;
916
-
917
- if (should_fault_in_pages (ret , from , & prev_count , & window_size )) {
918
- size_t leftover ;
910
+ written = ret ;
919
911
920
- gfs2_holder_allow_demote (gh );
921
- leftover = fault_in_iov_iter_readable (from , window_size );
922
- gfs2_holder_disallow_demote (gh );
923
- if (leftover != window_size ) {
924
- if (gfs2_holder_queued (gh ))
925
- goto retry_under_glock ;
912
+ if (should_fault_in_pages (from , iocb , & prev_count , & window_size )) {
913
+ gfs2_glock_dq (gh );
914
+ window_size -= fault_in_iov_iter_readable (from , window_size );
915
+ if (window_size )
926
916
goto retry ;
927
- }
928
917
}
929
- out :
918
+ out_unlock :
930
919
if (gfs2_holder_queued (gh ))
931
920
gfs2_glock_dq (gh );
932
921
out_uninit :
933
922
gfs2_holder_uninit (gh );
934
923
if (ret < 0 )
935
924
return ret ;
936
- return read ;
925
+ return written ;
937
926
}
938
927
939
928
static ssize_t gfs2_file_read_iter (struct kiocb * iocb , struct iov_iter * to )
940
929
{
941
930
struct gfs2_inode * ip ;
942
931
struct gfs2_holder gh ;
943
932
size_t prev_count = 0 , window_size = 0 ;
944
- size_t written = 0 ;
933
+ size_t read = 0 ;
945
934
ssize_t ret ;
946
935
947
936
/*
@@ -962,7 +951,7 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
962
951
if (ret >= 0 ) {
963
952
if (!iov_iter_count (to ))
964
953
return ret ;
965
- written = ret ;
954
+ read = ret ;
966
955
} else if (ret != - EFAULT ) {
967
956
if (ret != - EAGAIN )
968
957
return ret ;
@@ -975,30 +964,26 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
975
964
ret = gfs2_glock_nq (& gh );
976
965
if (ret )
977
966
goto out_uninit ;
978
- retry_under_glock :
979
967
pagefault_disable ();
980
968
ret = generic_file_read_iter (iocb , to );
981
969
pagefault_enable ();
970
+ if (ret <= 0 && ret != - EFAULT )
971
+ goto out_unlock ;
982
972
if (ret > 0 )
983
- written += ret ;
984
-
985
- if (should_fault_in_pages (ret , to , & prev_count , & window_size )) {
986
- size_t leftover ;
973
+ read += ret ;
987
974
988
- gfs2_holder_allow_demote (& gh );
989
- leftover = fault_in_iov_iter_writeable (to , window_size );
990
- gfs2_holder_disallow_demote (& gh );
991
- if (leftover != window_size ) {
992
- if (gfs2_holder_queued (& gh ))
993
- goto retry_under_glock ;
975
+ if (should_fault_in_pages (to , iocb , & prev_count , & window_size )) {
976
+ gfs2_glock_dq (& gh );
977
+ window_size -= fault_in_iov_iter_writeable (to , window_size );
978
+ if (window_size )
994
979
goto retry ;
995
- }
996
980
}
981
+ out_unlock :
997
982
if (gfs2_holder_queued (& gh ))
998
983
gfs2_glock_dq (& gh );
999
984
out_uninit :
1000
985
gfs2_holder_uninit (& gh );
1001
- return written ? written : ret ;
986
+ return read ? read : ret ;
1002
987
}
1003
988
1004
989
static ssize_t gfs2_file_buffered_write (struct kiocb * iocb ,
@@ -1012,7 +997,7 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
1012
997
struct gfs2_holder * statfs_gh = NULL ;
1013
998
size_t prev_count = 0 , window_size = 0 ;
1014
999
size_t orig_count = iov_iter_count (from );
1015
- size_t read = 0 ;
1000
+ size_t written = 0 ;
1016
1001
ssize_t ret ;
1017
1002
1018
1003
/*
@@ -1030,10 +1015,18 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
1030
1015
1031
1016
gfs2_holder_init (ip -> i_gl , LM_ST_EXCLUSIVE , 0 , gh );
1032
1017
retry :
1018
+ if (should_fault_in_pages (from , iocb , & prev_count , & window_size )) {
1019
+ window_size -= fault_in_iov_iter_readable (from , window_size );
1020
+ if (!window_size ) {
1021
+ ret = - EFAULT ;
1022
+ goto out_uninit ;
1023
+ }
1024
+ from -> count = min (from -> count , window_size );
1025
+ }
1033
1026
ret = gfs2_glock_nq (gh );
1034
1027
if (ret )
1035
1028
goto out_uninit ;
1036
- retry_under_glock :
1029
+
1037
1030
if (inode == sdp -> sd_rindex ) {
1038
1031
struct gfs2_inode * m_ip = GFS2_I (sdp -> sd_statfs_inode );
1039
1032
@@ -1050,25 +1043,19 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
1050
1043
current -> backing_dev_info = NULL ;
1051
1044
if (ret > 0 ) {
1052
1045
iocb -> ki_pos += ret ;
1053
- read += ret ;
1046
+ written += ret ;
1054
1047
}
1055
1048
1056
1049
if (inode == sdp -> sd_rindex )
1057
1050
gfs2_glock_dq_uninit (statfs_gh );
1058
1051
1059
- from -> count = orig_count - read ;
1060
- if (should_fault_in_pages (ret , from , & prev_count , & window_size )) {
1061
- size_t leftover ;
1062
-
1063
- gfs2_holder_allow_demote (gh );
1064
- leftover = fault_in_iov_iter_readable (from , window_size );
1065
- gfs2_holder_disallow_demote (gh );
1066
- if (leftover != window_size ) {
1067
- from -> count = min (from -> count , window_size - leftover );
1068
- if (gfs2_holder_queued (gh ))
1069
- goto retry_under_glock ;
1070
- goto retry ;
1071
- }
1052
+ if (ret <= 0 && ret != - EFAULT )
1053
+ goto out_unlock ;
1054
+
1055
+ from -> count = orig_count - written ;
1056
+ if (should_fault_in_pages (from , iocb , & prev_count , & window_size )) {
1057
+ gfs2_glock_dq (gh );
1058
+ goto retry ;
1072
1059
}
1073
1060
out_unlock :
1074
1061
if (gfs2_holder_queued (gh ))
@@ -1077,8 +1064,8 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
1077
1064
gfs2_holder_uninit (gh );
1078
1065
if (statfs_gh )
1079
1066
kfree (statfs_gh );
1080
- from -> count = orig_count - read ;
1081
- return read ? read : ret ;
1067
+ from -> count = orig_count - written ;
1068
+ return written ? written : ret ;
1082
1069
}
1083
1070
1084
1071
/**
0 commit comments