aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/file.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /fs/btrfs/file.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'fs/btrfs/file.c')
-rw-r--r--fs/btrfs/file.c937
1 files changed, 700 insertions, 237 deletions
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index e354c33df082..fa4ef18b66b1 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -24,6 +24,7 @@
24#include <linux/string.h> 24#include <linux/string.h>
25#include <linux/backing-dev.h> 25#include <linux/backing-dev.h>
26#include <linux/mpage.h> 26#include <linux/mpage.h>
27#include <linux/falloc.h>
27#include <linux/swap.h> 28#include <linux/swap.h>
28#include <linux/writeback.h> 29#include <linux/writeback.h>
29#include <linux/statfs.h> 30#include <linux/statfs.h>
@@ -39,16 +40,274 @@
39#include "locking.h" 40#include "locking.h"
40#include "compat.h" 41#include "compat.h"
41 42
43/*
44 * when auto defrag is enabled we
45 * queue up these defrag structs to remember which
46 * inodes need defragging passes
47 */
48struct inode_defrag {
49 struct rb_node rb_node;
50 /* objectid */
51 u64 ino;
52 /*
53 * transid where the defrag was added, we search for
54 * extents newer than this
55 */
56 u64 transid;
57
58 /* root objectid */
59 u64 root;
60
61 /* last offset we were able to defrag */
62 u64 last_offset;
63
64 /* if we've wrapped around back to zero once already */
65 int cycled;
66};
67
68/* pop a record for an inode into the defrag tree. The lock
69 * must be held already
70 *
71 * If you're inserting a record for an older transid than an
72 * existing record, the transid already in the tree is lowered
73 *
74 * If an existing record is found the defrag item you
75 * pass in is freed
76 */
77static int __btrfs_add_inode_defrag(struct inode *inode,
78 struct inode_defrag *defrag)
79{
80 struct btrfs_root *root = BTRFS_I(inode)->root;
81 struct inode_defrag *entry;
82 struct rb_node **p;
83 struct rb_node *parent = NULL;
84
85 p = &root->fs_info->defrag_inodes.rb_node;
86 while (*p) {
87 parent = *p;
88 entry = rb_entry(parent, struct inode_defrag, rb_node);
89
90 if (defrag->ino < entry->ino)
91 p = &parent->rb_left;
92 else if (defrag->ino > entry->ino)
93 p = &parent->rb_right;
94 else {
95 /* if we're reinserting an entry for
96 * an old defrag run, make sure to
97 * lower the transid of our existing record
98 */
99 if (defrag->transid < entry->transid)
100 entry->transid = defrag->transid;
101 if (defrag->last_offset > entry->last_offset)
102 entry->last_offset = defrag->last_offset;
103 goto exists;
104 }
105 }
106 BTRFS_I(inode)->in_defrag = 1;
107 rb_link_node(&defrag->rb_node, parent, p);
108 rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
109 return 0;
110
111exists:
112 kfree(defrag);
113 return 0;
114
115}
116
117/*
118 * insert a defrag record for this inode if auto defrag is
119 * enabled
120 */
121int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
122 struct inode *inode)
123{
124 struct btrfs_root *root = BTRFS_I(inode)->root;
125 struct inode_defrag *defrag;
126 int ret = 0;
127 u64 transid;
128
129 if (!btrfs_test_opt(root, AUTO_DEFRAG))
130 return 0;
131
132 if (btrfs_fs_closing(root->fs_info))
133 return 0;
134
135 if (BTRFS_I(inode)->in_defrag)
136 return 0;
137
138 if (trans)
139 transid = trans->transid;
140 else
141 transid = BTRFS_I(inode)->root->last_trans;
142
143 defrag = kzalloc(sizeof(*defrag), GFP_NOFS);
144 if (!defrag)
145 return -ENOMEM;
146
147 defrag->ino = btrfs_ino(inode);
148 defrag->transid = transid;
149 defrag->root = root->root_key.objectid;
150
151 spin_lock(&root->fs_info->defrag_inodes_lock);
152 if (!BTRFS_I(inode)->in_defrag)
153 ret = __btrfs_add_inode_defrag(inode, defrag);
154 spin_unlock(&root->fs_info->defrag_inodes_lock);
155 return ret;
156}
157
158/*
159 * must be called with the defrag_inodes lock held
160 */
161struct inode_defrag *btrfs_find_defrag_inode(struct btrfs_fs_info *info, u64 ino,
162 struct rb_node **next)
163{
164 struct inode_defrag *entry = NULL;
165 struct rb_node *p;
166 struct rb_node *parent = NULL;
167
168 p = info->defrag_inodes.rb_node;
169 while (p) {
170 parent = p;
171 entry = rb_entry(parent, struct inode_defrag, rb_node);
172
173 if (ino < entry->ino)
174 p = parent->rb_left;
175 else if (ino > entry->ino)
176 p = parent->rb_right;
177 else
178 return entry;
179 }
180
181 if (next) {
182 while (parent && ino > entry->ino) {
183 parent = rb_next(parent);
184 entry = rb_entry(parent, struct inode_defrag, rb_node);
185 }
186 *next = parent;
187 }
188 return NULL;
189}
190
191/*
192 * run through the list of inodes in the FS that need
193 * defragging
194 */
195int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
196{
197 struct inode_defrag *defrag;
198 struct btrfs_root *inode_root;
199 struct inode *inode;
200 struct rb_node *n;
201 struct btrfs_key key;
202 struct btrfs_ioctl_defrag_range_args range;
203 u64 first_ino = 0;
204 int num_defrag;
205 int defrag_batch = 1024;
206
207 memset(&range, 0, sizeof(range));
208 range.len = (u64)-1;
209
210 atomic_inc(&fs_info->defrag_running);
211 spin_lock(&fs_info->defrag_inodes_lock);
212 while(1) {
213 n = NULL;
214
215 /* find an inode to defrag */
216 defrag = btrfs_find_defrag_inode(fs_info, first_ino, &n);
217 if (!defrag) {
218 if (n)
219 defrag = rb_entry(n, struct inode_defrag, rb_node);
220 else if (first_ino) {
221 first_ino = 0;
222 continue;
223 } else {
224 break;
225 }
226 }
227
228 /* remove it from the rbtree */
229 first_ino = defrag->ino + 1;
230 rb_erase(&defrag->rb_node, &fs_info->defrag_inodes);
231
232 if (btrfs_fs_closing(fs_info))
233 goto next_free;
234
235 spin_unlock(&fs_info->defrag_inodes_lock);
236
237 /* get the inode */
238 key.objectid = defrag->root;
239 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
240 key.offset = (u64)-1;
241 inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
242 if (IS_ERR(inode_root))
243 goto next;
244
245 key.objectid = defrag->ino;
246 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
247 key.offset = 0;
248
249 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
250 if (IS_ERR(inode))
251 goto next;
252
253 /* do a chunk of defrag */
254 BTRFS_I(inode)->in_defrag = 0;
255 range.start = defrag->last_offset;
256 num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
257 defrag_batch);
258 /*
259 * if we filled the whole defrag batch, there
260 * must be more work to do. Queue this defrag
261 * again
262 */
263 if (num_defrag == defrag_batch) {
264 defrag->last_offset = range.start;
265 __btrfs_add_inode_defrag(inode, defrag);
266 /*
267 * we don't want to kfree defrag, we added it back to
268 * the rbtree
269 */
270 defrag = NULL;
271 } else if (defrag->last_offset && !defrag->cycled) {
272 /*
273 * we didn't fill our defrag batch, but
274 * we didn't start at zero. Make sure we loop
275 * around to the start of the file.
276 */
277 defrag->last_offset = 0;
278 defrag->cycled = 1;
279 __btrfs_add_inode_defrag(inode, defrag);
280 defrag = NULL;
281 }
282
283 iput(inode);
284next:
285 spin_lock(&fs_info->defrag_inodes_lock);
286next_free:
287 kfree(defrag);
288 }
289 spin_unlock(&fs_info->defrag_inodes_lock);
290
291 atomic_dec(&fs_info->defrag_running);
292
293 /*
294 * during unmount, we use the transaction_wait queue to
295 * wait for the defragger to stop
296 */
297 wake_up(&fs_info->transaction_wait);
298 return 0;
299}
42 300
43/* simple helper to fault in pages and copy. This should go away 301/* simple helper to fault in pages and copy. This should go away
44 * and be replaced with calls into generic code. 302 * and be replaced with calls into generic code.
45 */ 303 */
46static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, 304static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
47 int write_bytes, 305 size_t write_bytes,
48 struct page **prepared_pages, 306 struct page **prepared_pages,
49 struct iov_iter *i) 307 struct iov_iter *i)
50{ 308{
51 size_t copied; 309 size_t copied = 0;
310 size_t total_copied = 0;
52 int pg = 0; 311 int pg = 0;
53 int offset = pos & (PAGE_CACHE_SIZE - 1); 312 int offset = pos & (PAGE_CACHE_SIZE - 1);
54 313
@@ -56,23 +315,38 @@ static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
56 size_t count = min_t(size_t, 315 size_t count = min_t(size_t,
57 PAGE_CACHE_SIZE - offset, write_bytes); 316 PAGE_CACHE_SIZE - offset, write_bytes);
58 struct page *page = prepared_pages[pg]; 317 struct page *page = prepared_pages[pg];
59again: 318 /*
60 if (unlikely(iov_iter_fault_in_readable(i, count))) 319 * Copy data from userspace to the current page
61 return -EFAULT; 320 *
62 321 * Disable pagefault to avoid recursive lock since
63 /* Copy data from userspace to the current page */ 322 * the pages are already locked
64 copied = iov_iter_copy_from_user(page, i, offset, count); 323 */
324 pagefault_disable();
325 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
326 pagefault_enable();
65 327
66 /* Flush processor's dcache for this page */ 328 /* Flush processor's dcache for this page */
67 flush_dcache_page(page); 329 flush_dcache_page(page);
330
331 /*
332 * if we get a partial write, we can end up with
333 * partially up to date pages. These add
334 * a lot of complexity, so make sure they don't
335 * happen by forcing this copy to be retried.
336 *
337 * The rest of the btrfs_file_write code will fall
338 * back to page at a time copies after we return 0.
339 */
340 if (!PageUptodate(page) && copied < count)
341 copied = 0;
342
68 iov_iter_advance(i, copied); 343 iov_iter_advance(i, copied);
69 write_bytes -= copied; 344 write_bytes -= copied;
345 total_copied += copied;
70 346
71 if (unlikely(copied == 0)) { 347 /* Return to btrfs_file_aio_write to fault page */
72 count = min_t(size_t, PAGE_CACHE_SIZE - offset, 348 if (unlikely(copied == 0))
73 iov_iter_single_seg_count(i)); 349 break;
74 goto again;
75 }
76 350
77 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { 351 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
78 offset += copied; 352 offset += copied;
@@ -81,18 +355,16 @@ again:
81 offset = 0; 355 offset = 0;
82 } 356 }
83 } 357 }
84 return 0; 358 return total_copied;
85} 359}
86 360
87/* 361/*
88 * unlocks pages after btrfs_file_write is done with them 362 * unlocks pages after btrfs_file_write is done with them
89 */ 363 */
90static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages) 364void btrfs_drop_pages(struct page **pages, size_t num_pages)
91{ 365{
92 size_t i; 366 size_t i;
93 for (i = 0; i < num_pages; i++) { 367 for (i = 0; i < num_pages; i++) {
94 if (!pages[i])
95 break;
96 /* page checked is some magic around finding pages that 368 /* page checked is some magic around finding pages that
97 * have been modified without going through btrfs_set_page_dirty 369 * have been modified without going through btrfs_set_page_dirty
98 * clear it here 370 * clear it here
@@ -112,17 +384,13 @@ static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
112 * this also makes the decision about creating an inline extent vs 384 * this also makes the decision about creating an inline extent vs
113 * doing real data extents, marking pages dirty and delalloc as required. 385 * doing real data extents, marking pages dirty and delalloc as required.
114 */ 386 */
115static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, 387int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
116 struct btrfs_root *root, 388 struct page **pages, size_t num_pages,
117 struct file *file, 389 loff_t pos, size_t write_bytes,
118 struct page **pages, 390 struct extent_state **cached)
119 size_t num_pages,
120 loff_t pos,
121 size_t write_bytes)
122{ 391{
123 int err = 0; 392 int err = 0;
124 int i; 393 int i;
125 struct inode *inode = fdentry(file)->d_inode;
126 u64 num_bytes; 394 u64 num_bytes;
127 u64 start_pos; 395 u64 start_pos;
128 u64 end_of_last_block; 396 u64 end_of_last_block;
@@ -135,8 +403,9 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
135 403
136 end_of_last_block = start_pos + num_bytes - 1; 404 end_of_last_block = start_pos + num_bytes - 1;
137 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 405 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
138 NULL); 406 cached);
139 BUG_ON(err); 407 if (err)
408 return err;
140 409
141 for (i = 0; i < num_pages; i++) { 410 for (i = 0; i < num_pages; i++) {
142 struct page *p = pages[i]; 411 struct page *p = pages[i];
@@ -144,13 +413,14 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
144 ClearPageChecked(p); 413 ClearPageChecked(p);
145 set_page_dirty(p); 414 set_page_dirty(p);
146 } 415 }
147 if (end_pos > isize) { 416
417 /*
418 * we've only changed i_size in ram, and we haven't updated
419 * the disk i_size. There is no need to log the inode
420 * at this time.
421 */
422 if (end_pos > isize)
148 i_size_write(inode, end_pos); 423 i_size_write(inode, end_pos);
149 /* we've only changed i_size in ram, and we haven't updated
150 * the disk i_size. There is no need to log the inode
151 * at this time.
152 */
153 }
154 return 0; 424 return 0;
155} 425}
156 426
@@ -178,9 +448,10 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
178 } 448 }
179 while (1) { 449 while (1) {
180 if (!split) 450 if (!split)
181 split = alloc_extent_map(GFP_NOFS); 451 split = alloc_extent_map();
182 if (!split2) 452 if (!split2)
183 split2 = alloc_extent_map(GFP_NOFS); 453 split2 = alloc_extent_map();
454 BUG_ON(!split || !split2);
184 455
185 write_lock(&em_tree->lock); 456 write_lock(&em_tree->lock);
186 em = lookup_extent_mapping(em_tree, start, len); 457 em = lookup_extent_mapping(em_tree, start, len);
@@ -220,6 +491,7 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
220 491
221 split->bdev = em->bdev; 492 split->bdev = em->bdev;
222 split->flags = flags; 493 split->flags = flags;
494 split->compress_type = em->compress_type;
223 ret = add_extent_mapping(em_tree, split); 495 ret = add_extent_mapping(em_tree, split);
224 BUG_ON(ret); 496 BUG_ON(ret);
225 free_extent_map(split); 497 free_extent_map(split);
@@ -234,6 +506,7 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
234 split->len = em->start + em->len - (start + len); 506 split->len = em->start + em->len - (start + len);
235 split->bdev = em->bdev; 507 split->bdev = em->bdev;
236 split->flags = flags; 508 split->flags = flags;
509 split->compress_type = em->compress_type;
237 510
238 if (compressed) { 511 if (compressed) {
239 split->block_len = em->block_len; 512 split->block_len = em->block_len;
@@ -282,6 +555,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
282 struct btrfs_path *path; 555 struct btrfs_path *path;
283 struct btrfs_key key; 556 struct btrfs_key key;
284 struct btrfs_key new_key; 557 struct btrfs_key new_key;
558 u64 ino = btrfs_ino(inode);
285 u64 search_start = start; 559 u64 search_start = start;
286 u64 disk_bytenr = 0; 560 u64 disk_bytenr = 0;
287 u64 num_bytes = 0; 561 u64 num_bytes = 0;
@@ -302,14 +576,14 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
302 576
303 while (1) { 577 while (1) {
304 recow = 0; 578 recow = 0;
305 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 579 ret = btrfs_lookup_file_extent(trans, root, path, ino,
306 search_start, -1); 580 search_start, -1);
307 if (ret < 0) 581 if (ret < 0)
308 break; 582 break;
309 if (ret > 0 && path->slots[0] > 0 && search_start == start) { 583 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
310 leaf = path->nodes[0]; 584 leaf = path->nodes[0];
311 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); 585 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
312 if (key.objectid == inode->i_ino && 586 if (key.objectid == ino &&
313 key.type == BTRFS_EXTENT_DATA_KEY) 587 key.type == BTRFS_EXTENT_DATA_KEY)
314 path->slots[0]--; 588 path->slots[0]--;
315 } 589 }
@@ -330,7 +604,7 @@ next_slot:
330 } 604 }
331 605
332 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 606 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
333 if (key.objectid > inode->i_ino || 607 if (key.objectid > ino ||
334 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end) 608 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
335 break; 609 break;
336 610
@@ -360,7 +634,7 @@ next_slot:
360 634
361 search_start = max(key.offset, start); 635 search_start = max(key.offset, start);
362 if (recow) { 636 if (recow) {
363 btrfs_release_path(root, path); 637 btrfs_release_path(path);
364 continue; 638 continue;
365 } 639 }
366 640
@@ -377,7 +651,7 @@ next_slot:
377 ret = btrfs_duplicate_item(trans, root, path, 651 ret = btrfs_duplicate_item(trans, root, path,
378 &new_key); 652 &new_key);
379 if (ret == -EAGAIN) { 653 if (ret == -EAGAIN) {
380 btrfs_release_path(root, path); 654 btrfs_release_path(path);
381 continue; 655 continue;
382 } 656 }
383 if (ret < 0) 657 if (ret < 0)
@@ -500,7 +774,7 @@ next_slot:
500 del_nr = 0; 774 del_nr = 0;
501 del_slot = 0; 775 del_slot = 0;
502 776
503 btrfs_release_path(root, path); 777 btrfs_release_path(path);
504 continue; 778 continue;
505 } 779 }
506 780
@@ -576,6 +850,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
576 int del_slot = 0; 850 int del_slot = 0;
577 int recow; 851 int recow;
578 int ret; 852 int ret;
853 u64 ino = btrfs_ino(inode);
579 854
580 btrfs_drop_extent_cache(inode, start, end - 1, 0); 855 btrfs_drop_extent_cache(inode, start, end - 1, 0);
581 856
@@ -584,18 +859,19 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
584again: 859again:
585 recow = 0; 860 recow = 0;
586 split = start; 861 split = start;
587 key.objectid = inode->i_ino; 862 key.objectid = ino;
588 key.type = BTRFS_EXTENT_DATA_KEY; 863 key.type = BTRFS_EXTENT_DATA_KEY;
589 key.offset = split; 864 key.offset = split;
590 865
591 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 866 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
867 if (ret < 0)
868 goto out;
592 if (ret > 0 && path->slots[0] > 0) 869 if (ret > 0 && path->slots[0] > 0)
593 path->slots[0]--; 870 path->slots[0]--;
594 871
595 leaf = path->nodes[0]; 872 leaf = path->nodes[0];
596 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 873 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
597 BUG_ON(key.objectid != inode->i_ino || 874 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
598 key.type != BTRFS_EXTENT_DATA_KEY);
599 fi = btrfs_item_ptr(leaf, path->slots[0], 875 fi = btrfs_item_ptr(leaf, path->slots[0],
600 struct btrfs_file_extent_item); 876 struct btrfs_file_extent_item);
601 BUG_ON(btrfs_file_extent_type(leaf, fi) != 877 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
@@ -612,7 +888,7 @@ again:
612 other_start = 0; 888 other_start = 0;
613 other_end = start; 889 other_end = start;
614 if (extent_mergeable(leaf, path->slots[0] - 1, 890 if (extent_mergeable(leaf, path->slots[0] - 1,
615 inode->i_ino, bytenr, orig_offset, 891 ino, bytenr, orig_offset,
616 &other_start, &other_end)) { 892 &other_start, &other_end)) {
617 new_key.offset = end; 893 new_key.offset = end;
618 btrfs_set_item_key_safe(trans, root, path, &new_key); 894 btrfs_set_item_key_safe(trans, root, path, &new_key);
@@ -635,7 +911,7 @@ again:
635 other_start = end; 911 other_start = end;
636 other_end = 0; 912 other_end = 0;
637 if (extent_mergeable(leaf, path->slots[0] + 1, 913 if (extent_mergeable(leaf, path->slots[0] + 1,
638 inode->i_ino, bytenr, orig_offset, 914 ino, bytenr, orig_offset,
639 &other_start, &other_end)) { 915 &other_start, &other_end)) {
640 fi = btrfs_item_ptr(leaf, path->slots[0], 916 fi = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_file_extent_item); 917 struct btrfs_file_extent_item);
@@ -663,7 +939,7 @@ again:
663 new_key.offset = split; 939 new_key.offset = split;
664 ret = btrfs_duplicate_item(trans, root, path, &new_key); 940 ret = btrfs_duplicate_item(trans, root, path, &new_key);
665 if (ret == -EAGAIN) { 941 if (ret == -EAGAIN) {
666 btrfs_release_path(root, path); 942 btrfs_release_path(path);
667 goto again; 943 goto again;
668 } 944 }
669 BUG_ON(ret < 0); 945 BUG_ON(ret < 0);
@@ -684,7 +960,7 @@ again:
684 960
685 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, 961 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
686 root->root_key.objectid, 962 root->root_key.objectid,
687 inode->i_ino, orig_offset); 963 ino, orig_offset);
688 BUG_ON(ret); 964 BUG_ON(ret);
689 965
690 if (split == start) { 966 if (split == start) {
@@ -700,10 +976,10 @@ again:
700 other_start = end; 976 other_start = end;
701 other_end = 0; 977 other_end = 0;
702 if (extent_mergeable(leaf, path->slots[0] + 1, 978 if (extent_mergeable(leaf, path->slots[0] + 1,
703 inode->i_ino, bytenr, orig_offset, 979 ino, bytenr, orig_offset,
704 &other_start, &other_end)) { 980 &other_start, &other_end)) {
705 if (recow) { 981 if (recow) {
706 btrfs_release_path(root, path); 982 btrfs_release_path(path);
707 goto again; 983 goto again;
708 } 984 }
709 extent_end = other_end; 985 extent_end = other_end;
@@ -711,16 +987,16 @@ again:
711 del_nr++; 987 del_nr++;
712 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 988 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
713 0, root->root_key.objectid, 989 0, root->root_key.objectid,
714 inode->i_ino, orig_offset); 990 ino, orig_offset);
715 BUG_ON(ret); 991 BUG_ON(ret);
716 } 992 }
717 other_start = 0; 993 other_start = 0;
718 other_end = start; 994 other_end = start;
719 if (extent_mergeable(leaf, path->slots[0] - 1, 995 if (extent_mergeable(leaf, path->slots[0] - 1,
720 inode->i_ino, bytenr, orig_offset, 996 ino, bytenr, orig_offset,
721 &other_start, &other_end)) { 997 &other_start, &other_end)) {
722 if (recow) { 998 if (recow) {
723 btrfs_release_path(root, path); 999 btrfs_release_path(path);
724 goto again; 1000 goto again;
725 } 1001 }
726 key.offset = other_start; 1002 key.offset = other_start;
@@ -728,7 +1004,7 @@ again:
728 del_nr++; 1004 del_nr++;
729 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 1005 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
730 0, root->root_key.objectid, 1006 0, root->root_key.objectid,
731 inode->i_ino, orig_offset); 1007 ino, orig_offset);
732 BUG_ON(ret); 1008 BUG_ON(ret);
733 } 1009 }
734 if (del_nr == 0) { 1010 if (del_nr == 0) {
@@ -755,6 +1031,27 @@ out:
755} 1031}
756 1032
757/* 1033/*
1034 * on error we return an unlocked page and the error value
1035 * on success we return a locked page and 0
1036 */
1037static int prepare_uptodate_page(struct page *page, u64 pos)
1038{
1039 int ret = 0;
1040
1041 if ((pos & (PAGE_CACHE_SIZE - 1)) && !PageUptodate(page)) {
1042 ret = btrfs_readpage(NULL, page);
1043 if (ret)
1044 return ret;
1045 lock_page(page);
1046 if (!PageUptodate(page)) {
1047 unlock_page(page);
1048 return -EIO;
1049 }
1050 }
1051 return 0;
1052}
1053
1054/*
758 * this gets pages into the page cache and locks them down, it also properly 1055 * this gets pages into the page cache and locks them down, it also properly
759 * waits for data=ordered extents to finish before allowing the pages to be 1056 * waits for data=ordered extents to finish before allowing the pages to be
760 * modified. 1057 * modified.
@@ -769,6 +1066,7 @@ static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
769 unsigned long index = pos >> PAGE_CACHE_SHIFT; 1066 unsigned long index = pos >> PAGE_CACHE_SHIFT;
770 struct inode *inode = fdentry(file)->d_inode; 1067 struct inode *inode = fdentry(file)->d_inode;
771 int err = 0; 1068 int err = 0;
1069 int faili = 0;
772 u64 start_pos; 1070 u64 start_pos;
773 u64 last_pos; 1071 u64 last_pos;
774 1072
@@ -776,21 +1074,33 @@ static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
776 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; 1074 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
777 1075
778 if (start_pos > inode->i_size) { 1076 if (start_pos > inode->i_size) {
779 err = btrfs_cont_expand(inode, start_pos); 1077 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
780 if (err) 1078 if (err)
781 return err; 1079 return err;
782 } 1080 }
783 1081
784 memset(pages, 0, num_pages * sizeof(struct page *));
785again: 1082again:
786 for (i = 0; i < num_pages; i++) { 1083 for (i = 0; i < num_pages; i++) {
787 pages[i] = grab_cache_page(inode->i_mapping, index + i); 1084 pages[i] = grab_cache_page(inode->i_mapping, index + i);
788 if (!pages[i]) { 1085 if (!pages[i]) {
1086 faili = i - 1;
789 err = -ENOMEM; 1087 err = -ENOMEM;
790 BUG_ON(1); 1088 goto fail;
1089 }
1090
1091 if (i == 0)
1092 err = prepare_uptodate_page(pages[i], pos);
1093 if (i == num_pages - 1)
1094 err = prepare_uptodate_page(pages[i],
1095 pos + write_bytes);
1096 if (err) {
1097 page_cache_release(pages[i]);
1098 faili = i - 1;
1099 goto fail;
791 } 1100 }
792 wait_on_page_writeback(pages[i]); 1101 wait_on_page_writeback(pages[i]);
793 } 1102 }
1103 err = 0;
794 if (start_pos < inode->i_size) { 1104 if (start_pos < inode->i_size) {
795 struct btrfs_ordered_extent *ordered; 1105 struct btrfs_ordered_extent *ordered;
796 lock_extent_bits(&BTRFS_I(inode)->io_tree, 1106 lock_extent_bits(&BTRFS_I(inode)->io_tree,
@@ -830,199 +1140,264 @@ again:
830 WARN_ON(!PageLocked(pages[i])); 1140 WARN_ON(!PageLocked(pages[i]));
831 } 1141 }
832 return 0; 1142 return 0;
1143fail:
1144 while (faili >= 0) {
1145 unlock_page(pages[faili]);
1146 page_cache_release(pages[faili]);
1147 faili--;
1148 }
1149 return err;
1150
833} 1151}
834 1152
835static ssize_t btrfs_file_aio_write(struct kiocb *iocb, 1153static noinline ssize_t __btrfs_buffered_write(struct file *file,
836 const struct iovec *iov, 1154 struct iov_iter *i,
837 unsigned long nr_segs, loff_t pos) 1155 loff_t pos)
838{ 1156{
839 struct file *file = iocb->ki_filp;
840 struct inode *inode = fdentry(file)->d_inode; 1157 struct inode *inode = fdentry(file)->d_inode;
841 struct btrfs_root *root = BTRFS_I(inode)->root; 1158 struct btrfs_root *root = BTRFS_I(inode)->root;
842 struct page *pinned[2];
843 struct page **pages = NULL; 1159 struct page **pages = NULL;
844 struct iov_iter i;
845 loff_t *ppos = &iocb->ki_pos;
846 loff_t start_pos;
847 ssize_t num_written = 0;
848 ssize_t err = 0;
849 size_t count;
850 size_t ocount;
851 int ret = 0;
852 int nrptrs;
853 unsigned long first_index; 1160 unsigned long first_index;
854 unsigned long last_index; 1161 unsigned long last_index;
855 int will_write; 1162 size_t num_written = 0;
856 int buffered = 0; 1163 int nrptrs;
1164 int ret = 0;
857 1165
858 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) || 1166 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
859 (file->f_flags & O_DIRECT)); 1167 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1168 (sizeof(struct page *)));
1169 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
1170 if (!pages)
1171 return -ENOMEM;
860 1172
861 pinned[0] = NULL; 1173 first_index = pos >> PAGE_CACHE_SHIFT;
862 pinned[1] = NULL; 1174 last_index = (pos + iov_iter_count(i)) >> PAGE_CACHE_SHIFT;
863 1175
864 start_pos = pos; 1176 while (iov_iter_count(i) > 0) {
1177 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1178 size_t write_bytes = min(iov_iter_count(i),
1179 nrptrs * (size_t)PAGE_CACHE_SIZE -
1180 offset);
1181 size_t num_pages = (write_bytes + offset +
1182 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1183 size_t dirty_pages;
1184 size_t copied;
865 1185
866 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); 1186 WARN_ON(num_pages > nrptrs);
867 1187
868 mutex_lock(&inode->i_mutex); 1188 /*
1189 * Fault pages before locking them in prepare_pages
1190 * to avoid recursive lock
1191 */
1192 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
1193 ret = -EFAULT;
1194 break;
1195 }
869 1196
870 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); 1197 ret = btrfs_delalloc_reserve_space(inode,
871 if (err) 1198 num_pages << PAGE_CACHE_SHIFT);
872 goto out; 1199 if (ret)
873 count = ocount; 1200 break;
874 1201
875 current->backing_dev_info = inode->i_mapping->backing_dev_info; 1202 /*
876 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); 1203 * This is going to setup the pages array with the number of
877 if (err) 1204 * pages we want, so we don't really need to worry about the
878 goto out; 1205 * contents of pages from loop to loop
1206 */
1207 ret = prepare_pages(root, file, pages, num_pages,
1208 pos, first_index, last_index,
1209 write_bytes);
1210 if (ret) {
1211 btrfs_delalloc_release_space(inode,
1212 num_pages << PAGE_CACHE_SHIFT);
1213 break;
1214 }
879 1215
880 if (count == 0) 1216 copied = btrfs_copy_from_user(pos, num_pages,
881 goto out; 1217 write_bytes, pages, i);
882 1218
883 err = file_remove_suid(file); 1219 /*
884 if (err) 1220 * if we have trouble faulting in the pages, fall
885 goto out; 1221 * back to one page at a time
1222 */
1223 if (copied < write_bytes)
1224 nrptrs = 1;
886 1225
887 file_update_time(file); 1226 if (copied == 0)
888 BTRFS_I(inode)->sequence++; 1227 dirty_pages = 0;
1228 else
1229 dirty_pages = (copied + offset +
1230 PAGE_CACHE_SIZE - 1) >>
1231 PAGE_CACHE_SHIFT;
889 1232
890 if (unlikely(file->f_flags & O_DIRECT)) {
891 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
892 pos, ppos, count,
893 ocount);
894 /* 1233 /*
895 * the generic O_DIRECT will update in-memory i_size after the 1234 * If we had a short copy we need to release the excess delaloc
896 * DIOs are done. But our endio handlers that update the on 1235 * bytes we reserved. We need to increment outstanding_extents
897 * disk i_size never update past the in memory i_size. So we 1236 * because btrfs_delalloc_release_space will decrement it, but
898 * need one more update here to catch any additions to the 1237 * we still have an outstanding extent for the chunk we actually
899 * file 1238 * managed to copy.
900 */ 1239 */
901 if (inode->i_size != BTRFS_I(inode)->disk_i_size) { 1240 if (num_pages > dirty_pages) {
902 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 1241 if (copied > 0)
903 mark_inode_dirty(inode); 1242 atomic_inc(
1243 &BTRFS_I(inode)->outstanding_extents);
1244 btrfs_delalloc_release_space(inode,
1245 (num_pages - dirty_pages) <<
1246 PAGE_CACHE_SHIFT);
904 } 1247 }
905 1248
906 if (num_written < 0) { 1249 if (copied > 0) {
907 ret = num_written; 1250 ret = btrfs_dirty_pages(root, inode, pages,
908 num_written = 0; 1251 dirty_pages, pos, copied,
909 goto out; 1252 NULL);
910 } else if (num_written == count) { 1253 if (ret) {
911 /* pick up pos changes done by the generic code */ 1254 btrfs_delalloc_release_space(inode,
912 pos = *ppos; 1255 dirty_pages << PAGE_CACHE_SHIFT);
913 goto out; 1256 btrfs_drop_pages(pages, num_pages);
1257 break;
1258 }
914 } 1259 }
915 /* 1260
916 * We are going to do buffered for the rest of the range, so we 1261 btrfs_drop_pages(pages, num_pages);
917 * need to make sure to invalidate the buffered pages when we're 1262
918 * done. 1263 cond_resched();
919 */ 1264
920 buffered = 1; 1265 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
921 pos += num_written; 1266 dirty_pages);
1267 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1268 btrfs_btree_balance_dirty(root, 1);
1269 btrfs_throttle(root);
1270
1271 pos += copied;
1272 num_written += copied;
922 } 1273 }
923 1274
924 iov_iter_init(&i, iov, nr_segs, count, num_written); 1275 kfree(pages);
925 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
926 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
927 (sizeof(struct page *)));
928 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
929 1276
930 /* generic_write_checks can change our pos */ 1277 return num_written ? num_written : ret;
931 start_pos = pos; 1278}
932 1279
933 first_index = pos >> PAGE_CACHE_SHIFT; 1280static ssize_t __btrfs_direct_write(struct kiocb *iocb,
934 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT; 1281 const struct iovec *iov,
1282 unsigned long nr_segs, loff_t pos,
1283 loff_t *ppos, size_t count, size_t ocount)
1284{
1285 struct file *file = iocb->ki_filp;
1286 struct inode *inode = fdentry(file)->d_inode;
1287 struct iov_iter i;
1288 ssize_t written;
1289 ssize_t written_buffered;
1290 loff_t endbyte;
1291 int err;
1292
1293 written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
1294 count, ocount);
935 1295
936 /* 1296 /*
937 * there are lots of better ways to do this, but this code 1297 * the generic O_DIRECT will update in-memory i_size after the
938 * makes sure the first and last page in the file range are 1298 * DIOs are done. But our endio handlers that update the on
939 * up to date and ready for cow 1299 * disk i_size never update past the in memory i_size. So we
1300 * need one more update here to catch any additions to the
1301 * file
940 */ 1302 */
941 if ((pos & (PAGE_CACHE_SIZE - 1))) { 1303 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
942 pinned[0] = grab_cache_page(inode->i_mapping, first_index); 1304 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
943 if (!PageUptodate(pinned[0])) { 1305 mark_inode_dirty(inode);
944 ret = btrfs_readpage(NULL, pinned[0]);
945 BUG_ON(ret);
946 wait_on_page_locked(pinned[0]);
947 } else {
948 unlock_page(pinned[0]);
949 }
950 } 1306 }
951 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) { 1307
952 pinned[1] = grab_cache_page(inode->i_mapping, last_index); 1308 if (written < 0 || written == count)
953 if (!PageUptodate(pinned[1])) { 1309 return written;
954 ret = btrfs_readpage(NULL, pinned[1]); 1310
955 BUG_ON(ret); 1311 pos += written;
956 wait_on_page_locked(pinned[1]); 1312 count -= written;
957 } else { 1313 iov_iter_init(&i, iov, nr_segs, count, written);
958 unlock_page(pinned[1]); 1314 written_buffered = __btrfs_buffered_write(file, &i, pos);
959 } 1315 if (written_buffered < 0) {
1316 err = written_buffered;
1317 goto out;
960 } 1318 }
1319 endbyte = pos + written_buffered - 1;
1320 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1321 if (err)
1322 goto out;
1323 written += written_buffered;
1324 *ppos = pos + written_buffered;
1325 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1326 endbyte >> PAGE_CACHE_SHIFT);
1327out:
1328 return written ? written : err;
1329}
961 1330
962 while (iov_iter_count(&i) > 0) { 1331static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
963 size_t offset = pos & (PAGE_CACHE_SIZE - 1); 1332 const struct iovec *iov,
964 size_t write_bytes = min(iov_iter_count(&i), 1333 unsigned long nr_segs, loff_t pos)
965 nrptrs * (size_t)PAGE_CACHE_SIZE - 1334{
966 offset); 1335 struct file *file = iocb->ki_filp;
967 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> 1336 struct inode *inode = fdentry(file)->d_inode;
968 PAGE_CACHE_SHIFT; 1337 struct btrfs_root *root = BTRFS_I(inode)->root;
1338 loff_t *ppos = &iocb->ki_pos;
1339 ssize_t num_written = 0;
1340 ssize_t err = 0;
1341 size_t count, ocount;
969 1342
970 WARN_ON(num_pages > nrptrs); 1343 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
971 memset(pages, 0, sizeof(struct page *) * nrptrs);
972 1344
973 ret = btrfs_delalloc_reserve_space(inode, write_bytes); 1345 mutex_lock(&inode->i_mutex);
974 if (ret)
975 goto out;
976 1346
977 ret = prepare_pages(root, file, pages, num_pages, 1347 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
978 pos, first_index, last_index, 1348 if (err) {
979 write_bytes); 1349 mutex_unlock(&inode->i_mutex);
980 if (ret) { 1350 goto out;
981 btrfs_delalloc_release_space(inode, write_bytes); 1351 }
982 goto out; 1352 count = ocount;
983 }
984 1353
985 ret = btrfs_copy_from_user(pos, num_pages, 1354 current->backing_dev_info = inode->i_mapping->backing_dev_info;
986 write_bytes, pages, &i); 1355 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
987 if (ret == 0) { 1356 if (err) {
988 dirty_and_release_pages(NULL, root, file, pages, 1357 mutex_unlock(&inode->i_mutex);
989 num_pages, pos, write_bytes); 1358 goto out;
990 } 1359 }
991 1360
992 btrfs_drop_pages(pages, num_pages); 1361 if (count == 0) {
993 if (ret) { 1362 mutex_unlock(&inode->i_mutex);
994 btrfs_delalloc_release_space(inode, write_bytes); 1363 goto out;
995 goto out; 1364 }
996 }
997 1365
998 if (will_write) { 1366 err = file_remove_suid(file);
999 filemap_fdatawrite_range(inode->i_mapping, pos, 1367 if (err) {
1000 pos + write_bytes - 1); 1368 mutex_unlock(&inode->i_mutex);
1001 } else { 1369 goto out;
1002 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1370 }
1003 num_pages);
1004 if (num_pages <
1005 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1006 btrfs_btree_balance_dirty(root, 1);
1007 btrfs_throttle(root);
1008 }
1009 1371
1010 pos += write_bytes; 1372 /*
1011 num_written += write_bytes; 1373 * If BTRFS flips readonly due to some impossible error
1374 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1375 * although we have opened a file as writable, we have
1376 * to stop this write operation to ensure FS consistency.
1377 */
1378 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
1379 mutex_unlock(&inode->i_mutex);
1380 err = -EROFS;
1381 goto out;
1382 }
1012 1383
1013 cond_resched(); 1384 file_update_time(file);
1385 BTRFS_I(inode)->sequence++;
1386
1387 if (unlikely(file->f_flags & O_DIRECT)) {
1388 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1389 pos, ppos, count, ocount);
1390 } else {
1391 struct iov_iter i;
1392
1393 iov_iter_init(&i, iov, nr_segs, count, num_written);
1394
1395 num_written = __btrfs_buffered_write(file, &i, pos);
1396 if (num_written > 0)
1397 *ppos = pos + num_written;
1014 } 1398 }
1015out:
1016 mutex_unlock(&inode->i_mutex);
1017 if (ret)
1018 err = ret;
1019 1399
1020 kfree(pages); 1400 mutex_unlock(&inode->i_mutex);
1021 if (pinned[0])
1022 page_cache_release(pinned[0]);
1023 if (pinned[1])
1024 page_cache_release(pinned[1]);
1025 *ppos = pos;
1026 1401
1027 /* 1402 /*
1028 * we want to make sure fsync finds this change 1403 * we want to make sure fsync finds this change
@@ -1037,36 +1412,12 @@ out:
1037 * one running right now. 1412 * one running right now.
1038 */ 1413 */
1039 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; 1414 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1040 1415 if (num_written > 0 || num_written == -EIOCBQUEUED) {
1041 if (num_written > 0 && will_write) { 1416 err = generic_write_sync(file, pos, num_written);
1042 struct btrfs_trans_handle *trans; 1417 if (err < 0 && num_written > 0)
1043
1044 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1045 if (err)
1046 num_written = err; 1418 num_written = err;
1047
1048 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
1049 trans = btrfs_start_transaction(root, 0);
1050 ret = btrfs_log_dentry_safe(trans, root,
1051 file->f_dentry);
1052 if (ret == 0) {
1053 ret = btrfs_sync_log(trans, root);
1054 if (ret == 0)
1055 btrfs_end_transaction(trans, root);
1056 else
1057 btrfs_commit_transaction(trans, root);
1058 } else if (ret != BTRFS_NO_LOG_SYNC) {
1059 btrfs_commit_transaction(trans, root);
1060 } else {
1061 btrfs_end_transaction(trans, root);
1062 }
1063 }
1064 if (file->f_flags & O_DIRECT && buffered) {
1065 invalidate_mapping_pages(inode->i_mapping,
1066 start_pos >> PAGE_CACHE_SHIFT,
1067 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1068 }
1069 } 1419 }
1420out:
1070 current->backing_dev_info = NULL; 1421 current->backing_dev_info = NULL;
1071 return num_written ? num_written : err; 1422 return num_written ? num_written : err;
1072} 1423}
@@ -1109,6 +1460,7 @@ int btrfs_sync_file(struct file *file, int datasync)
1109 int ret = 0; 1460 int ret = 0;
1110 struct btrfs_trans_handle *trans; 1461 struct btrfs_trans_handle *trans;
1111 1462
1463 trace_btrfs_sync_file(file, datasync);
1112 1464
1113 /* we wait first, since the writeback may change the inode */ 1465 /* we wait first, since the writeback may change the inode */
1114 root->log_batch++; 1466 root->log_batch++;
@@ -1128,14 +1480,12 @@ int btrfs_sync_file(struct file *file, int datasync)
1128 * the current transaction, we can bail out now without any 1480 * the current transaction, we can bail out now without any
1129 * syncing 1481 * syncing
1130 */ 1482 */
1131 mutex_lock(&root->fs_info->trans_mutex); 1483 smp_mb();
1132 if (BTRFS_I(inode)->last_trans <= 1484 if (BTRFS_I(inode)->last_trans <=
1133 root->fs_info->last_trans_committed) { 1485 root->fs_info->last_trans_committed) {
1134 BTRFS_I(inode)->last_trans = 0; 1486 BTRFS_I(inode)->last_trans = 0;
1135 mutex_unlock(&root->fs_info->trans_mutex);
1136 goto out; 1487 goto out;
1137 } 1488 }
1138 mutex_unlock(&root->fs_info->trans_mutex);
1139 1489
1140 /* 1490 /*
1141 * ok we haven't committed the transaction yet, lets do a commit 1491 * ok we haven't committed the transaction yet, lets do a commit
@@ -1202,6 +1552,118 @@ static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1202 return 0; 1552 return 0;
1203} 1553}
1204 1554
1555static long btrfs_fallocate(struct file *file, int mode,
1556 loff_t offset, loff_t len)
1557{
1558 struct inode *inode = file->f_path.dentry->d_inode;
1559 struct extent_state *cached_state = NULL;
1560 u64 cur_offset;
1561 u64 last_byte;
1562 u64 alloc_start;
1563 u64 alloc_end;
1564 u64 alloc_hint = 0;
1565 u64 locked_end;
1566 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1567 struct extent_map *em;
1568 int ret;
1569
1570 alloc_start = offset & ~mask;
1571 alloc_end = (offset + len + mask) & ~mask;
1572
1573 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1574 if (mode & ~FALLOC_FL_KEEP_SIZE)
1575 return -EOPNOTSUPP;
1576
1577 /*
1578 * wait for ordered IO before we have any locks. We'll loop again
1579 * below with the locks held.
1580 */
1581 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
1582
1583 mutex_lock(&inode->i_mutex);
1584 ret = inode_newsize_ok(inode, alloc_end);
1585 if (ret)
1586 goto out;
1587
1588 if (alloc_start > inode->i_size) {
1589 ret = btrfs_cont_expand(inode, i_size_read(inode),
1590 alloc_start);
1591 if (ret)
1592 goto out;
1593 }
1594
1595 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
1596 if (ret)
1597 goto out;
1598
1599 locked_end = alloc_end - 1;
1600 while (1) {
1601 struct btrfs_ordered_extent *ordered;
1602
1603 /* the extent lock is ordered inside the running
1604 * transaction
1605 */
1606 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
1607 locked_end, 0, &cached_state, GFP_NOFS);
1608 ordered = btrfs_lookup_first_ordered_extent(inode,
1609 alloc_end - 1);
1610 if (ordered &&
1611 ordered->file_offset + ordered->len > alloc_start &&
1612 ordered->file_offset < alloc_end) {
1613 btrfs_put_ordered_extent(ordered);
1614 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1615 alloc_start, locked_end,
1616 &cached_state, GFP_NOFS);
1617 /*
1618 * we can't wait on the range with the transaction
1619 * running or with the extent lock held
1620 */
1621 btrfs_wait_ordered_range(inode, alloc_start,
1622 alloc_end - alloc_start);
1623 } else {
1624 if (ordered)
1625 btrfs_put_ordered_extent(ordered);
1626 break;
1627 }
1628 }
1629
1630 cur_offset = alloc_start;
1631 while (1) {
1632 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
1633 alloc_end - cur_offset, 0);
1634 BUG_ON(IS_ERR_OR_NULL(em));
1635 last_byte = min(extent_map_end(em), alloc_end);
1636 last_byte = (last_byte + mask) & ~mask;
1637 if (em->block_start == EXTENT_MAP_HOLE ||
1638 (cur_offset >= inode->i_size &&
1639 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
1640 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
1641 last_byte - cur_offset,
1642 1 << inode->i_blkbits,
1643 offset + len,
1644 &alloc_hint);
1645 if (ret < 0) {
1646 free_extent_map(em);
1647 break;
1648 }
1649 }
1650 free_extent_map(em);
1651
1652 cur_offset = last_byte;
1653 if (cur_offset >= alloc_end) {
1654 ret = 0;
1655 break;
1656 }
1657 }
1658 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
1659 &cached_state, GFP_NOFS);
1660
1661 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
1662out:
1663 mutex_unlock(&inode->i_mutex);
1664 return ret;
1665}
1666
1205const struct file_operations btrfs_file_operations = { 1667const struct file_operations btrfs_file_operations = {
1206 .llseek = generic_file_llseek, 1668 .llseek = generic_file_llseek,
1207 .read = do_sync_read, 1669 .read = do_sync_read,
@@ -1213,6 +1675,7 @@ const struct file_operations btrfs_file_operations = {
1213 .open = generic_file_open, 1675 .open = generic_file_open,
1214 .release = btrfs_release_file, 1676 .release = btrfs_release_file,
1215 .fsync = btrfs_sync_file, 1677 .fsync = btrfs_sync_file,
1678 .fallocate = btrfs_fallocate,
1216 .unlocked_ioctl = btrfs_ioctl, 1679 .unlocked_ioctl = btrfs_ioctl,
1217#ifdef CONFIG_COMPAT 1680#ifdef CONFIG_COMPAT
1218 .compat_ioctl = btrfs_ioctl, 1681 .compat_ioctl = btrfs_ioctl,