diff options
Diffstat (limited to 'fs/btrfs/file.c')
-rw-r--r-- | fs/btrfs/file.c | 388 |
1 files changed, 199 insertions, 189 deletions
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index f447b783bb84..656bc0a892b1 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c | |||
@@ -45,14 +45,14 @@ | |||
45 | * and be replaced with calls into generic code. | 45 | * and be replaced with calls into generic code. |
46 | */ | 46 | */ |
47 | static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, | 47 | static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, |
48 | int write_bytes, | 48 | size_t write_bytes, |
49 | struct page **prepared_pages, | 49 | struct page **prepared_pages, |
50 | struct iov_iter *i) | 50 | struct iov_iter *i) |
51 | { | 51 | { |
52 | size_t copied = 0; | 52 | size_t copied = 0; |
53 | size_t total_copied = 0; | ||
53 | int pg = 0; | 54 | int pg = 0; |
54 | int offset = pos & (PAGE_CACHE_SIZE - 1); | 55 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
55 | int total_copied = 0; | ||
56 | 56 | ||
57 | while (write_bytes > 0) { | 57 | while (write_bytes > 0) { |
58 | size_t count = min_t(size_t, | 58 | size_t count = min_t(size_t, |
@@ -88,9 +88,8 @@ static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, | |||
88 | total_copied += copied; | 88 | total_copied += copied; |
89 | 89 | ||
90 | /* Return to btrfs_file_aio_write to fault page */ | 90 | /* Return to btrfs_file_aio_write to fault page */ |
91 | if (unlikely(copied == 0)) { | 91 | if (unlikely(copied == 0)) |
92 | break; | 92 | break; |
93 | } | ||
94 | 93 | ||
95 | if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { | 94 | if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { |
96 | offset += copied; | 95 | offset += copied; |
@@ -109,8 +108,6 @@ static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages) | |||
109 | { | 108 | { |
110 | size_t i; | 109 | size_t i; |
111 | for (i = 0; i < num_pages; i++) { | 110 | for (i = 0; i < num_pages; i++) { |
112 | if (!pages[i]) | ||
113 | break; | ||
114 | /* page checked is some magic around finding pages that | 111 | /* page checked is some magic around finding pages that |
115 | * have been modified without going through btrfs_set_page_dirty | 112 | * have been modified without going through btrfs_set_page_dirty |
116 | * clear it here | 113 | * clear it here |
@@ -130,13 +127,12 @@ static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages) | |||
130 | * this also makes the decision about creating an inline extent vs | 127 | * this also makes the decision about creating an inline extent vs |
131 | * doing real data extents, marking pages dirty and delalloc as required. | 128 | * doing real data extents, marking pages dirty and delalloc as required. |
132 | */ | 129 | */ |
133 | static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, | 130 | static noinline int dirty_and_release_pages(struct btrfs_root *root, |
134 | struct btrfs_root *root, | 131 | struct file *file, |
135 | struct file *file, | 132 | struct page **pages, |
136 | struct page **pages, | 133 | size_t num_pages, |
137 | size_t num_pages, | 134 | loff_t pos, |
138 | loff_t pos, | 135 | size_t write_bytes) |
139 | size_t write_bytes) | ||
140 | { | 136 | { |
141 | int err = 0; | 137 | int err = 0; |
142 | int i; | 138 | int i; |
@@ -154,7 +150,8 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, | |||
154 | end_of_last_block = start_pos + num_bytes - 1; | 150 | end_of_last_block = start_pos + num_bytes - 1; |
155 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, | 151 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, |
156 | NULL); | 152 | NULL); |
157 | BUG_ON(err); | 153 | if (err) |
154 | return err; | ||
158 | 155 | ||
159 | for (i = 0; i < num_pages; i++) { | 156 | for (i = 0; i < num_pages; i++) { |
160 | struct page *p = pages[i]; | 157 | struct page *p = pages[i]; |
@@ -162,13 +159,14 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, | |||
162 | ClearPageChecked(p); | 159 | ClearPageChecked(p); |
163 | set_page_dirty(p); | 160 | set_page_dirty(p); |
164 | } | 161 | } |
165 | if (end_pos > isize) { | 162 | |
163 | /* | ||
164 | * we've only changed i_size in ram, and we haven't updated | ||
165 | * the disk i_size. There is no need to log the inode | ||
166 | * at this time. | ||
167 | */ | ||
168 | if (end_pos > isize) | ||
166 | i_size_write(inode, end_pos); | 169 | i_size_write(inode, end_pos); |
167 | /* we've only changed i_size in ram, and we haven't updated | ||
168 | * the disk i_size. There is no need to log the inode | ||
169 | * at this time. | ||
170 | */ | ||
171 | } | ||
172 | return 0; | 170 | return 0; |
173 | } | 171 | } |
174 | 172 | ||
@@ -610,6 +608,8 @@ again: | |||
610 | key.offset = split; | 608 | key.offset = split; |
611 | 609 | ||
612 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | 610 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
611 | if (ret < 0) | ||
612 | goto out; | ||
613 | if (ret > 0 && path->slots[0] > 0) | 613 | if (ret > 0 && path->slots[0] > 0) |
614 | path->slots[0]--; | 614 | path->slots[0]--; |
615 | 615 | ||
@@ -819,12 +819,11 @@ static noinline int prepare_pages(struct btrfs_root *root, struct file *file, | |||
819 | last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; | 819 | last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; |
820 | 820 | ||
821 | if (start_pos > inode->i_size) { | 821 | if (start_pos > inode->i_size) { |
822 | err = btrfs_cont_expand(inode, start_pos); | 822 | err = btrfs_cont_expand(inode, i_size_read(inode), start_pos); |
823 | if (err) | 823 | if (err) |
824 | return err; | 824 | return err; |
825 | } | 825 | } |
826 | 826 | ||
827 | memset(pages, 0, num_pages * sizeof(struct page *)); | ||
828 | again: | 827 | again: |
829 | for (i = 0; i < num_pages; i++) { | 828 | for (i = 0; i < num_pages; i++) { |
830 | pages[i] = grab_cache_page(inode->i_mapping, index + i); | 829 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
@@ -896,156 +895,71 @@ fail: | |||
896 | 895 | ||
897 | } | 896 | } |
898 | 897 | ||
899 | static ssize_t btrfs_file_aio_write(struct kiocb *iocb, | 898 | static noinline ssize_t __btrfs_buffered_write(struct file *file, |
900 | const struct iovec *iov, | 899 | struct iov_iter *i, |
901 | unsigned long nr_segs, loff_t pos) | 900 | loff_t pos) |
902 | { | 901 | { |
903 | struct file *file = iocb->ki_filp; | ||
904 | struct inode *inode = fdentry(file)->d_inode; | 902 | struct inode *inode = fdentry(file)->d_inode; |
905 | struct btrfs_root *root = BTRFS_I(inode)->root; | 903 | struct btrfs_root *root = BTRFS_I(inode)->root; |
906 | struct page **pages = NULL; | 904 | struct page **pages = NULL; |
907 | struct iov_iter i; | ||
908 | loff_t *ppos = &iocb->ki_pos; | ||
909 | loff_t start_pos; | ||
910 | ssize_t num_written = 0; | ||
911 | ssize_t err = 0; | ||
912 | size_t count; | ||
913 | size_t ocount; | ||
914 | int ret = 0; | ||
915 | int nrptrs; | ||
916 | unsigned long first_index; | 905 | unsigned long first_index; |
917 | unsigned long last_index; | 906 | unsigned long last_index; |
918 | int will_write; | 907 | size_t num_written = 0; |
919 | int buffered = 0; | 908 | int nrptrs; |
920 | int copied = 0; | 909 | int ret; |
921 | int dirty_pages = 0; | ||
922 | |||
923 | will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) || | ||
924 | (file->f_flags & O_DIRECT)); | ||
925 | |||
926 | start_pos = pos; | ||
927 | |||
928 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | ||
929 | |||
930 | mutex_lock(&inode->i_mutex); | ||
931 | |||
932 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); | ||
933 | if (err) | ||
934 | goto out; | ||
935 | count = ocount; | ||
936 | |||
937 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | ||
938 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | ||
939 | if (err) | ||
940 | goto out; | ||
941 | |||
942 | if (count == 0) | ||
943 | goto out; | ||
944 | |||
945 | err = file_remove_suid(file); | ||
946 | if (err) | ||
947 | goto out; | ||
948 | |||
949 | /* | ||
950 | * If BTRFS flips readonly due to some impossible error | ||
951 | * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), | ||
952 | * although we have opened a file as writable, we have | ||
953 | * to stop this write operation to ensure FS consistency. | ||
954 | */ | ||
955 | if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { | ||
956 | err = -EROFS; | ||
957 | goto out; | ||
958 | } | ||
959 | |||
960 | file_update_time(file); | ||
961 | BTRFS_I(inode)->sequence++; | ||
962 | |||
963 | if (unlikely(file->f_flags & O_DIRECT)) { | ||
964 | num_written = generic_file_direct_write(iocb, iov, &nr_segs, | ||
965 | pos, ppos, count, | ||
966 | ocount); | ||
967 | /* | ||
968 | * the generic O_DIRECT will update in-memory i_size after the | ||
969 | * DIOs are done. But our endio handlers that update the on | ||
970 | * disk i_size never update past the in memory i_size. So we | ||
971 | * need one more update here to catch any additions to the | ||
972 | * file | ||
973 | */ | ||
974 | if (inode->i_size != BTRFS_I(inode)->disk_i_size) { | ||
975 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); | ||
976 | mark_inode_dirty(inode); | ||
977 | } | ||
978 | |||
979 | if (num_written < 0) { | ||
980 | ret = num_written; | ||
981 | num_written = 0; | ||
982 | goto out; | ||
983 | } else if (num_written == count) { | ||
984 | /* pick up pos changes done by the generic code */ | ||
985 | pos = *ppos; | ||
986 | goto out; | ||
987 | } | ||
988 | /* | ||
989 | * We are going to do buffered for the rest of the range, so we | ||
990 | * need to make sure to invalidate the buffered pages when we're | ||
991 | * done. | ||
992 | */ | ||
993 | buffered = 1; | ||
994 | pos += num_written; | ||
995 | } | ||
996 | 910 | ||
997 | iov_iter_init(&i, iov, nr_segs, count, num_written); | 911 | nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) / |
998 | nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) / | ||
999 | PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / | 912 | PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / |
1000 | (sizeof(struct page *))); | 913 | (sizeof(struct page *))); |
1001 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); | 914 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); |
1002 | if (!pages) { | 915 | if (!pages) |
1003 | ret = -ENOMEM; | 916 | return -ENOMEM; |
1004 | goto out; | ||
1005 | } | ||
1006 | |||
1007 | /* generic_write_checks can change our pos */ | ||
1008 | start_pos = pos; | ||
1009 | 917 | ||
1010 | first_index = pos >> PAGE_CACHE_SHIFT; | 918 | first_index = pos >> PAGE_CACHE_SHIFT; |
1011 | last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT; | 919 | last_index = (pos + iov_iter_count(i)) >> PAGE_CACHE_SHIFT; |
1012 | 920 | ||
1013 | while (iov_iter_count(&i) > 0) { | 921 | while (iov_iter_count(i) > 0) { |
1014 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); | 922 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
1015 | size_t write_bytes = min(iov_iter_count(&i), | 923 | size_t write_bytes = min(iov_iter_count(i), |
1016 | nrptrs * (size_t)PAGE_CACHE_SIZE - | 924 | nrptrs * (size_t)PAGE_CACHE_SIZE - |
1017 | offset); | 925 | offset); |
1018 | size_t num_pages = (write_bytes + offset + | 926 | size_t num_pages = (write_bytes + offset + |
1019 | PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | 927 | PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
928 | size_t dirty_pages; | ||
929 | size_t copied; | ||
1020 | 930 | ||
1021 | WARN_ON(num_pages > nrptrs); | 931 | WARN_ON(num_pages > nrptrs); |
1022 | memset(pages, 0, sizeof(struct page *) * nrptrs); | ||
1023 | 932 | ||
1024 | /* | 933 | /* |
1025 | * Fault pages before locking them in prepare_pages | 934 | * Fault pages before locking them in prepare_pages |
1026 | * to avoid recursive lock | 935 | * to avoid recursive lock |
1027 | */ | 936 | */ |
1028 | if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) { | 937 | if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) { |
1029 | ret = -EFAULT; | 938 | ret = -EFAULT; |
1030 | goto out; | 939 | break; |
1031 | } | 940 | } |
1032 | 941 | ||
1033 | ret = btrfs_delalloc_reserve_space(inode, | 942 | ret = btrfs_delalloc_reserve_space(inode, |
1034 | num_pages << PAGE_CACHE_SHIFT); | 943 | num_pages << PAGE_CACHE_SHIFT); |
1035 | if (ret) | 944 | if (ret) |
1036 | goto out; | 945 | break; |
1037 | 946 | ||
947 | /* | ||
948 | * This is going to setup the pages array with the number of | ||
949 | * pages we want, so we don't really need to worry about the | ||
950 | * contents of pages from loop to loop | ||
951 | */ | ||
1038 | ret = prepare_pages(root, file, pages, num_pages, | 952 | ret = prepare_pages(root, file, pages, num_pages, |
1039 | pos, first_index, last_index, | 953 | pos, first_index, last_index, |
1040 | write_bytes); | 954 | write_bytes); |
1041 | if (ret) { | 955 | if (ret) { |
1042 | btrfs_delalloc_release_space(inode, | 956 | btrfs_delalloc_release_space(inode, |
1043 | num_pages << PAGE_CACHE_SHIFT); | 957 | num_pages << PAGE_CACHE_SHIFT); |
1044 | goto out; | 958 | break; |
1045 | } | 959 | } |
1046 | 960 | ||
1047 | copied = btrfs_copy_from_user(pos, num_pages, | 961 | copied = btrfs_copy_from_user(pos, num_pages, |
1048 | write_bytes, pages, &i); | 962 | write_bytes, pages, i); |
1049 | 963 | ||
1050 | /* | 964 | /* |
1051 | * if we have trouble faulting in the pages, fall | 965 | * if we have trouble faulting in the pages, fall |
@@ -1061,6 +975,13 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb, | |||
1061 | PAGE_CACHE_SIZE - 1) >> | 975 | PAGE_CACHE_SIZE - 1) >> |
1062 | PAGE_CACHE_SHIFT; | 976 | PAGE_CACHE_SHIFT; |
1063 | 977 | ||
978 | /* | ||
979 | * If we had a short copy we need to release the excess delaloc | ||
980 | * bytes we reserved. We need to increment outstanding_extents | ||
981 | * because btrfs_delalloc_release_space will decrement it, but | ||
982 | * we still have an outstanding extent for the chunk we actually | ||
983 | * managed to copy. | ||
984 | */ | ||
1064 | if (num_pages > dirty_pages) { | 985 | if (num_pages > dirty_pages) { |
1065 | if (copied > 0) | 986 | if (copied > 0) |
1066 | atomic_inc( | 987 | atomic_inc( |
@@ -1071,39 +992,157 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb, | |||
1071 | } | 992 | } |
1072 | 993 | ||
1073 | if (copied > 0) { | 994 | if (copied > 0) { |
1074 | dirty_and_release_pages(NULL, root, file, pages, | 995 | ret = dirty_and_release_pages(root, file, pages, |
1075 | dirty_pages, pos, copied); | 996 | dirty_pages, pos, |
997 | copied); | ||
998 | if (ret) { | ||
999 | btrfs_delalloc_release_space(inode, | ||
1000 | dirty_pages << PAGE_CACHE_SHIFT); | ||
1001 | btrfs_drop_pages(pages, num_pages); | ||
1002 | break; | ||
1003 | } | ||
1076 | } | 1004 | } |
1077 | 1005 | ||
1078 | btrfs_drop_pages(pages, num_pages); | 1006 | btrfs_drop_pages(pages, num_pages); |
1079 | 1007 | ||
1080 | if (copied > 0) { | 1008 | cond_resched(); |
1081 | if (will_write) { | 1009 | |
1082 | filemap_fdatawrite_range(inode->i_mapping, pos, | 1010 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, |
1083 | pos + copied - 1); | 1011 | dirty_pages); |
1084 | } else { | 1012 | if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1) |
1085 | balance_dirty_pages_ratelimited_nr( | 1013 | btrfs_btree_balance_dirty(root, 1); |
1086 | inode->i_mapping, | 1014 | btrfs_throttle(root); |
1087 | dirty_pages); | ||
1088 | if (dirty_pages < | ||
1089 | (root->leafsize >> PAGE_CACHE_SHIFT) + 1) | ||
1090 | btrfs_btree_balance_dirty(root, 1); | ||
1091 | btrfs_throttle(root); | ||
1092 | } | ||
1093 | } | ||
1094 | 1015 | ||
1095 | pos += copied; | 1016 | pos += copied; |
1096 | num_written += copied; | 1017 | num_written += copied; |
1018 | } | ||
1097 | 1019 | ||
1098 | cond_resched(); | 1020 | kfree(pages); |
1021 | |||
1022 | return num_written ? num_written : ret; | ||
1023 | } | ||
1024 | |||
1025 | static ssize_t __btrfs_direct_write(struct kiocb *iocb, | ||
1026 | const struct iovec *iov, | ||
1027 | unsigned long nr_segs, loff_t pos, | ||
1028 | loff_t *ppos, size_t count, size_t ocount) | ||
1029 | { | ||
1030 | struct file *file = iocb->ki_filp; | ||
1031 | struct inode *inode = fdentry(file)->d_inode; | ||
1032 | struct iov_iter i; | ||
1033 | ssize_t written; | ||
1034 | ssize_t written_buffered; | ||
1035 | loff_t endbyte; | ||
1036 | int err; | ||
1037 | |||
1038 | written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos, | ||
1039 | count, ocount); | ||
1040 | |||
1041 | /* | ||
1042 | * the generic O_DIRECT will update in-memory i_size after the | ||
1043 | * DIOs are done. But our endio handlers that update the on | ||
1044 | * disk i_size never update past the in memory i_size. So we | ||
1045 | * need one more update here to catch any additions to the | ||
1046 | * file | ||
1047 | */ | ||
1048 | if (inode->i_size != BTRFS_I(inode)->disk_i_size) { | ||
1049 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); | ||
1050 | mark_inode_dirty(inode); | ||
1099 | } | 1051 | } |
1052 | |||
1053 | if (written < 0 || written == count) | ||
1054 | return written; | ||
1055 | |||
1056 | pos += written; | ||
1057 | count -= written; | ||
1058 | iov_iter_init(&i, iov, nr_segs, count, written); | ||
1059 | written_buffered = __btrfs_buffered_write(file, &i, pos); | ||
1060 | if (written_buffered < 0) { | ||
1061 | err = written_buffered; | ||
1062 | goto out; | ||
1063 | } | ||
1064 | endbyte = pos + written_buffered - 1; | ||
1065 | err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte); | ||
1066 | if (err) | ||
1067 | goto out; | ||
1068 | written += written_buffered; | ||
1069 | *ppos = pos + written_buffered; | ||
1070 | invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT, | ||
1071 | endbyte >> PAGE_CACHE_SHIFT); | ||
1100 | out: | 1072 | out: |
1101 | mutex_unlock(&inode->i_mutex); | 1073 | return written ? written : err; |
1102 | if (ret) | 1074 | } |
1103 | err = ret; | ||
1104 | 1075 | ||
1105 | kfree(pages); | 1076 | static ssize_t btrfs_file_aio_write(struct kiocb *iocb, |
1106 | *ppos = pos; | 1077 | const struct iovec *iov, |
1078 | unsigned long nr_segs, loff_t pos) | ||
1079 | { | ||
1080 | struct file *file = iocb->ki_filp; | ||
1081 | struct inode *inode = fdentry(file)->d_inode; | ||
1082 | struct btrfs_root *root = BTRFS_I(inode)->root; | ||
1083 | loff_t *ppos = &iocb->ki_pos; | ||
1084 | ssize_t num_written = 0; | ||
1085 | ssize_t err = 0; | ||
1086 | size_t count, ocount; | ||
1087 | |||
1088 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | ||
1089 | |||
1090 | mutex_lock(&inode->i_mutex); | ||
1091 | |||
1092 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); | ||
1093 | if (err) { | ||
1094 | mutex_unlock(&inode->i_mutex); | ||
1095 | goto out; | ||
1096 | } | ||
1097 | count = ocount; | ||
1098 | |||
1099 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | ||
1100 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | ||
1101 | if (err) { | ||
1102 | mutex_unlock(&inode->i_mutex); | ||
1103 | goto out; | ||
1104 | } | ||
1105 | |||
1106 | if (count == 0) { | ||
1107 | mutex_unlock(&inode->i_mutex); | ||
1108 | goto out; | ||
1109 | } | ||
1110 | |||
1111 | err = file_remove_suid(file); | ||
1112 | if (err) { | ||
1113 | mutex_unlock(&inode->i_mutex); | ||
1114 | goto out; | ||
1115 | } | ||
1116 | |||
1117 | /* | ||
1118 | * If BTRFS flips readonly due to some impossible error | ||
1119 | * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), | ||
1120 | * although we have opened a file as writable, we have | ||
1121 | * to stop this write operation to ensure FS consistency. | ||
1122 | */ | ||
1123 | if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { | ||
1124 | mutex_unlock(&inode->i_mutex); | ||
1125 | err = -EROFS; | ||
1126 | goto out; | ||
1127 | } | ||
1128 | |||
1129 | file_update_time(file); | ||
1130 | BTRFS_I(inode)->sequence++; | ||
1131 | |||
1132 | if (unlikely(file->f_flags & O_DIRECT)) { | ||
1133 | num_written = __btrfs_direct_write(iocb, iov, nr_segs, | ||
1134 | pos, ppos, count, ocount); | ||
1135 | } else { | ||
1136 | struct iov_iter i; | ||
1137 | |||
1138 | iov_iter_init(&i, iov, nr_segs, count, num_written); | ||
1139 | |||
1140 | num_written = __btrfs_buffered_write(file, &i, pos); | ||
1141 | if (num_written > 0) | ||
1142 | *ppos = pos + num_written; | ||
1143 | } | ||
1144 | |||
1145 | mutex_unlock(&inode->i_mutex); | ||
1107 | 1146 | ||
1108 | /* | 1147 | /* |
1109 | * we want to make sure fsync finds this change | 1148 | * we want to make sure fsync finds this change |
@@ -1118,43 +1157,12 @@ out: | |||
1118 | * one running right now. | 1157 | * one running right now. |
1119 | */ | 1158 | */ |
1120 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; | 1159 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; |
1121 | 1160 | if (num_written > 0 || num_written == -EIOCBQUEUED) { | |
1122 | if (num_written > 0 && will_write) { | 1161 | err = generic_write_sync(file, pos, num_written); |
1123 | struct btrfs_trans_handle *trans; | 1162 | if (err < 0 && num_written > 0) |
1124 | |||
1125 | err = btrfs_wait_ordered_range(inode, start_pos, num_written); | ||
1126 | if (err) | ||
1127 | num_written = err; | 1163 | num_written = err; |
1128 | |||
1129 | if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { | ||
1130 | trans = btrfs_start_transaction(root, 0); | ||
1131 | if (IS_ERR(trans)) { | ||
1132 | num_written = PTR_ERR(trans); | ||
1133 | goto done; | ||
1134 | } | ||
1135 | mutex_lock(&inode->i_mutex); | ||
1136 | ret = btrfs_log_dentry_safe(trans, root, | ||
1137 | file->f_dentry); | ||
1138 | mutex_unlock(&inode->i_mutex); | ||
1139 | if (ret == 0) { | ||
1140 | ret = btrfs_sync_log(trans, root); | ||
1141 | if (ret == 0) | ||
1142 | btrfs_end_transaction(trans, root); | ||
1143 | else | ||
1144 | btrfs_commit_transaction(trans, root); | ||
1145 | } else if (ret != BTRFS_NO_LOG_SYNC) { | ||
1146 | btrfs_commit_transaction(trans, root); | ||
1147 | } else { | ||
1148 | btrfs_end_transaction(trans, root); | ||
1149 | } | ||
1150 | } | ||
1151 | if (file->f_flags & O_DIRECT && buffered) { | ||
1152 | invalidate_mapping_pages(inode->i_mapping, | ||
1153 | start_pos >> PAGE_CACHE_SHIFT, | ||
1154 | (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT); | ||
1155 | } | ||
1156 | } | 1164 | } |
1157 | done: | 1165 | out: |
1158 | current->backing_dev_info = NULL; | 1166 | current->backing_dev_info = NULL; |
1159 | return num_written ? num_written : err; | 1167 | return num_written ? num_written : err; |
1160 | } | 1168 | } |
@@ -1197,6 +1205,7 @@ int btrfs_sync_file(struct file *file, int datasync) | |||
1197 | int ret = 0; | 1205 | int ret = 0; |
1198 | struct btrfs_trans_handle *trans; | 1206 | struct btrfs_trans_handle *trans; |
1199 | 1207 | ||
1208 | trace_btrfs_sync_file(file, datasync); | ||
1200 | 1209 | ||
1201 | /* we wait first, since the writeback may change the inode */ | 1210 | /* we wait first, since the writeback may change the inode */ |
1202 | root->log_batch++; | 1211 | root->log_batch++; |
@@ -1324,7 +1333,8 @@ static long btrfs_fallocate(struct file *file, int mode, | |||
1324 | goto out; | 1333 | goto out; |
1325 | 1334 | ||
1326 | if (alloc_start > inode->i_size) { | 1335 | if (alloc_start > inode->i_size) { |
1327 | ret = btrfs_cont_expand(inode, alloc_start); | 1336 | ret = btrfs_cont_expand(inode, i_size_read(inode), |
1337 | alloc_start); | ||
1328 | if (ret) | 1338 | if (ret) |
1329 | goto out; | 1339 | goto out; |
1330 | } | 1340 | } |