Btrfs: wait on ordered extents at the last possible moment
[firefly-linux-kernel-4.4.55.git] / fs / btrfs / ordered-data.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/slab.h>
20 #include <linux/blkdev.h>
21 #include <linux/writeback.h>
22 #include <linux/pagevec.h>
23 #include "ctree.h"
24 #include "transaction.h"
25 #include "btrfs_inode.h"
26 #include "extent_io.h"
27
28 static struct kmem_cache *btrfs_ordered_extent_cache;
29
30 static u64 entry_end(struct btrfs_ordered_extent *entry)
31 {
32         if (entry->file_offset + entry->len < entry->file_offset)
33                 return (u64)-1;
34         return entry->file_offset + entry->len;
35 }
36
37 /* returns NULL if the insertion worked, or it returns the node it did find
38  * in the tree
39  */
40 static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
41                                    struct rb_node *node)
42 {
43         struct rb_node **p = &root->rb_node;
44         struct rb_node *parent = NULL;
45         struct btrfs_ordered_extent *entry;
46
47         while (*p) {
48                 parent = *p;
49                 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
50
51                 if (file_offset < entry->file_offset)
52                         p = &(*p)->rb_left;
53                 else if (file_offset >= entry_end(entry))
54                         p = &(*p)->rb_right;
55                 else
56                         return parent;
57         }
58
59         rb_link_node(node, parent, p);
60         rb_insert_color(node, root);
61         return NULL;
62 }
63
64 static void ordered_data_tree_panic(struct inode *inode, int errno,
65                                                u64 offset)
66 {
67         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
68         btrfs_panic(fs_info, errno, "Inconsistency in ordered tree at offset "
69                     "%llu\n", (unsigned long long)offset);
70 }
71
72 /*
73  * look for a given offset in the tree, and if it can't be found return the
74  * first lesser offset
75  */
76 static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
77                                      struct rb_node **prev_ret)
78 {
79         struct rb_node *n = root->rb_node;
80         struct rb_node *prev = NULL;
81         struct rb_node *test;
82         struct btrfs_ordered_extent *entry;
83         struct btrfs_ordered_extent *prev_entry = NULL;
84
85         while (n) {
86                 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
87                 prev = n;
88                 prev_entry = entry;
89
90                 if (file_offset < entry->file_offset)
91                         n = n->rb_left;
92                 else if (file_offset >= entry_end(entry))
93                         n = n->rb_right;
94                 else
95                         return n;
96         }
97         if (!prev_ret)
98                 return NULL;
99
100         while (prev && file_offset >= entry_end(prev_entry)) {
101                 test = rb_next(prev);
102                 if (!test)
103                         break;
104                 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
105                                       rb_node);
106                 if (file_offset < entry_end(prev_entry))
107                         break;
108
109                 prev = test;
110         }
111         if (prev)
112                 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
113                                       rb_node);
114         while (prev && file_offset < entry_end(prev_entry)) {
115                 test = rb_prev(prev);
116                 if (!test)
117                         break;
118                 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
119                                       rb_node);
120                 prev = test;
121         }
122         *prev_ret = prev;
123         return NULL;
124 }
125
126 /*
127  * helper to check if a given offset is inside a given entry
128  */
129 static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
130 {
131         if (file_offset < entry->file_offset ||
132             entry->file_offset + entry->len <= file_offset)
133                 return 0;
134         return 1;
135 }
136
137 static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
138                           u64 len)
139 {
140         if (file_offset + len <= entry->file_offset ||
141             entry->file_offset + entry->len <= file_offset)
142                 return 0;
143         return 1;
144 }
145
146 /*
147  * look find the first ordered struct that has this offset, otherwise
148  * the first one less than this offset
149  */
150 static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
151                                           u64 file_offset)
152 {
153         struct rb_root *root = &tree->tree;
154         struct rb_node *prev = NULL;
155         struct rb_node *ret;
156         struct btrfs_ordered_extent *entry;
157
158         if (tree->last) {
159                 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
160                                  rb_node);
161                 if (offset_in_entry(entry, file_offset))
162                         return tree->last;
163         }
164         ret = __tree_search(root, file_offset, &prev);
165         if (!ret)
166                 ret = prev;
167         if (ret)
168                 tree->last = ret;
169         return ret;
170 }
171
172 /* allocate and add a new ordered_extent into the per-inode tree.
173  * file_offset is the logical offset in the file
174  *
175  * start is the disk block number of an extent already reserved in the
176  * extent allocation tree
177  *
178  * len is the length of the extent
179  *
180  * The tree is given a single reference on the ordered extent that was
181  * inserted.
182  */
183 static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
184                                       u64 start, u64 len, u64 disk_len,
185                                       int type, int dio, int compress_type)
186 {
187         struct btrfs_ordered_inode_tree *tree;
188         struct rb_node *node;
189         struct btrfs_ordered_extent *entry;
190
191         tree = &BTRFS_I(inode)->ordered_tree;
192         entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
193         if (!entry)
194                 return -ENOMEM;
195
196         entry->file_offset = file_offset;
197         entry->start = start;
198         entry->len = len;
199         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) &&
200             !(type == BTRFS_ORDERED_NOCOW))
201                 entry->csum_bytes_left = disk_len;
202         entry->disk_len = disk_len;
203         entry->bytes_left = len;
204         entry->inode = igrab(inode);
205         entry->compress_type = compress_type;
206         if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
207                 set_bit(type, &entry->flags);
208
209         if (dio)
210                 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
211
212         /* one ref for the tree */
213         atomic_set(&entry->refs, 1);
214         init_waitqueue_head(&entry->wait);
215         INIT_LIST_HEAD(&entry->list);
216         INIT_LIST_HEAD(&entry->root_extent_list);
217         INIT_LIST_HEAD(&entry->work_list);
218         init_completion(&entry->completion);
219         INIT_LIST_HEAD(&entry->log_list);
220
221         trace_btrfs_ordered_extent_add(inode, entry);
222
223         spin_lock_irq(&tree->lock);
224         node = tree_insert(&tree->tree, file_offset,
225                            &entry->rb_node);
226         if (node)
227                 ordered_data_tree_panic(inode, -EEXIST, file_offset);
228         spin_unlock_irq(&tree->lock);
229
230         spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
231         list_add_tail(&entry->root_extent_list,
232                       &BTRFS_I(inode)->root->fs_info->ordered_extents);
233         spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
234
235         return 0;
236 }
237
238 int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
239                              u64 start, u64 len, u64 disk_len, int type)
240 {
241         return __btrfs_add_ordered_extent(inode, file_offset, start, len,
242                                           disk_len, type, 0,
243                                           BTRFS_COMPRESS_NONE);
244 }
245
246 int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
247                                  u64 start, u64 len, u64 disk_len, int type)
248 {
249         return __btrfs_add_ordered_extent(inode, file_offset, start, len,
250                                           disk_len, type, 1,
251                                           BTRFS_COMPRESS_NONE);
252 }
253
254 int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
255                                       u64 start, u64 len, u64 disk_len,
256                                       int type, int compress_type)
257 {
258         return __btrfs_add_ordered_extent(inode, file_offset, start, len,
259                                           disk_len, type, 0,
260                                           compress_type);
261 }
262
263 /*
264  * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
265  * when an ordered extent is finished.  If the list covers more than one
266  * ordered extent, it is split across multiples.
267  */
268 void btrfs_add_ordered_sum(struct inode *inode,
269                            struct btrfs_ordered_extent *entry,
270                            struct btrfs_ordered_sum *sum)
271 {
272         struct btrfs_ordered_inode_tree *tree;
273
274         tree = &BTRFS_I(inode)->ordered_tree;
275         spin_lock_irq(&tree->lock);
276         list_add_tail(&sum->list, &entry->list);
277         WARN_ON(entry->csum_bytes_left < sum->len);
278         entry->csum_bytes_left -= sum->len;
279         if (entry->csum_bytes_left == 0)
280                 wake_up(&entry->wait);
281         spin_unlock_irq(&tree->lock);
282 }
283
284 /*
285  * this is used to account for finished IO across a given range
286  * of the file.  The IO may span ordered extents.  If
287  * a given ordered_extent is completely done, 1 is returned, otherwise
288  * 0.
289  *
290  * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
291  * to make sure this function only returns 1 once for a given ordered extent.
292  *
293  * file_offset is updated to one byte past the range that is recorded as
294  * complete.  This allows you to walk forward in the file.
295  */
296 int btrfs_dec_test_first_ordered_pending(struct inode *inode,
297                                    struct btrfs_ordered_extent **cached,
298                                    u64 *file_offset, u64 io_size, int uptodate)
299 {
300         struct btrfs_ordered_inode_tree *tree;
301         struct rb_node *node;
302         struct btrfs_ordered_extent *entry = NULL;
303         int ret;
304         unsigned long flags;
305         u64 dec_end;
306         u64 dec_start;
307         u64 to_dec;
308
309         tree = &BTRFS_I(inode)->ordered_tree;
310         spin_lock_irqsave(&tree->lock, flags);
311         node = tree_search(tree, *file_offset);
312         if (!node) {
313                 ret = 1;
314                 goto out;
315         }
316
317         entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
318         if (!offset_in_entry(entry, *file_offset)) {
319                 ret = 1;
320                 goto out;
321         }
322
323         dec_start = max(*file_offset, entry->file_offset);
324         dec_end = min(*file_offset + io_size, entry->file_offset +
325                       entry->len);
326         *file_offset = dec_end;
327         if (dec_start > dec_end) {
328                 printk(KERN_CRIT "bad ordering dec_start %llu end %llu\n",
329                        (unsigned long long)dec_start,
330                        (unsigned long long)dec_end);
331         }
332         to_dec = dec_end - dec_start;
333         if (to_dec > entry->bytes_left) {
334                 printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
335                        (unsigned long long)entry->bytes_left,
336                        (unsigned long long)to_dec);
337         }
338         entry->bytes_left -= to_dec;
339         if (!uptodate)
340                 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
341
342         if (entry->bytes_left == 0)
343                 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
344         else
345                 ret = 1;
346 out:
347         if (!ret && cached && entry) {
348                 *cached = entry;
349                 atomic_inc(&entry->refs);
350         }
351         spin_unlock_irqrestore(&tree->lock, flags);
352         return ret == 0;
353 }
354
355 /*
356  * this is used to account for finished IO across a given range
357  * of the file.  The IO should not span ordered extents.  If
358  * a given ordered_extent is completely done, 1 is returned, otherwise
359  * 0.
360  *
361  * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
362  * to make sure this function only returns 1 once for a given ordered extent.
363  */
364 int btrfs_dec_test_ordered_pending(struct inode *inode,
365                                    struct btrfs_ordered_extent **cached,
366                                    u64 file_offset, u64 io_size, int uptodate)
367 {
368         struct btrfs_ordered_inode_tree *tree;
369         struct rb_node *node;
370         struct btrfs_ordered_extent *entry = NULL;
371         unsigned long flags;
372         int ret;
373
374         tree = &BTRFS_I(inode)->ordered_tree;
375         spin_lock_irqsave(&tree->lock, flags);
376         if (cached && *cached) {
377                 entry = *cached;
378                 goto have_entry;
379         }
380
381         node = tree_search(tree, file_offset);
382         if (!node) {
383                 ret = 1;
384                 goto out;
385         }
386
387         entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
388 have_entry:
389         if (!offset_in_entry(entry, file_offset)) {
390                 ret = 1;
391                 goto out;
392         }
393
394         if (io_size > entry->bytes_left) {
395                 printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
396                        (unsigned long long)entry->bytes_left,
397                        (unsigned long long)io_size);
398         }
399         entry->bytes_left -= io_size;
400         if (!uptodate)
401                 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
402
403         if (entry->bytes_left == 0)
404                 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
405         else
406                 ret = 1;
407 out:
408         if (!ret && cached && entry) {
409                 *cached = entry;
410                 atomic_inc(&entry->refs);
411         }
412         spin_unlock_irqrestore(&tree->lock, flags);
413         return ret == 0;
414 }
415
416 /* Needs to either be called under a log transaction or the log_mutex */
417 void btrfs_get_logged_extents(struct btrfs_root *log, struct inode *inode)
418 {
419         struct btrfs_ordered_inode_tree *tree;
420         struct btrfs_ordered_extent *ordered;
421         struct rb_node *n;
422         int index = log->log_transid % 2;
423
424         tree = &BTRFS_I(inode)->ordered_tree;
425         spin_lock_irq(&tree->lock);
426         for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
427                 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
428                 spin_lock(&log->log_extents_lock[index]);
429                 if (list_empty(&ordered->log_list)) {
430                         list_add_tail(&ordered->log_list, &log->logged_list[index]);
431                         atomic_inc(&ordered->refs);
432                 }
433                 spin_unlock(&log->log_extents_lock[index]);
434         }
435         spin_unlock_irq(&tree->lock);
436 }
437
438 void btrfs_wait_logged_extents(struct btrfs_root *log, u64 transid)
439 {
440         struct btrfs_ordered_extent *ordered;
441         int index = transid % 2;
442
443         spin_lock_irq(&log->log_extents_lock[index]);
444         while (!list_empty(&log->logged_list[index])) {
445                 ordered = list_first_entry(&log->logged_list[index],
446                                            struct btrfs_ordered_extent,
447                                            log_list);
448                 list_del_init(&ordered->log_list);
449                 spin_unlock_irq(&log->log_extents_lock[index]);
450                 wait_event(ordered->wait, test_bit(BTRFS_ORDERED_IO_DONE,
451                                                    &ordered->flags));
452                 btrfs_put_ordered_extent(ordered);
453                 spin_lock_irq(&log->log_extents_lock[index]);
454         }
455         spin_unlock_irq(&log->log_extents_lock[index]);
456 }
457
458 void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid)
459 {
460         struct btrfs_ordered_extent *ordered;
461         int index = transid % 2;
462
463         spin_lock_irq(&log->log_extents_lock[index]);
464         while (!list_empty(&log->logged_list[index])) {
465                 ordered = list_first_entry(&log->logged_list[index],
466                                            struct btrfs_ordered_extent,
467                                            log_list);
468                 list_del_init(&ordered->log_list);
469                 spin_unlock_irq(&log->log_extents_lock[index]);
470                 btrfs_put_ordered_extent(ordered);
471                 spin_lock_irq(&log->log_extents_lock[index]);
472         }
473         spin_unlock_irq(&log->log_extents_lock[index]);
474 }
475
476 /*
477  * used to drop a reference on an ordered extent.  This will free
478  * the extent if the last reference is dropped
479  */
480 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
481 {
482         struct list_head *cur;
483         struct btrfs_ordered_sum *sum;
484
485         trace_btrfs_ordered_extent_put(entry->inode, entry);
486
487         if (atomic_dec_and_test(&entry->refs)) {
488                 if (entry->inode)
489                         btrfs_add_delayed_iput(entry->inode);
490                 while (!list_empty(&entry->list)) {
491                         cur = entry->list.next;
492                         sum = list_entry(cur, struct btrfs_ordered_sum, list);
493                         list_del(&sum->list);
494                         kfree(sum);
495                 }
496                 kmem_cache_free(btrfs_ordered_extent_cache, entry);
497         }
498 }
499
500 /*
501  * remove an ordered extent from the tree.  No references are dropped
502  * and waiters are woken up.
503  */
504 void btrfs_remove_ordered_extent(struct inode *inode,
505                                  struct btrfs_ordered_extent *entry)
506 {
507         struct btrfs_ordered_inode_tree *tree;
508         struct btrfs_root *root = BTRFS_I(inode)->root;
509         struct rb_node *node;
510
511         tree = &BTRFS_I(inode)->ordered_tree;
512         spin_lock_irq(&tree->lock);
513         node = &entry->rb_node;
514         rb_erase(node, &tree->tree);
515         tree->last = NULL;
516         set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
517         spin_unlock_irq(&tree->lock);
518
519         spin_lock(&root->fs_info->ordered_extent_lock);
520         list_del_init(&entry->root_extent_list);
521
522         trace_btrfs_ordered_extent_remove(inode, entry);
523
524         /*
525          * we have no more ordered extents for this inode and
526          * no dirty pages.  We can safely remove it from the
527          * list of ordered extents
528          */
529         if (RB_EMPTY_ROOT(&tree->tree) &&
530             !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
531                 list_del_init(&BTRFS_I(inode)->ordered_operations);
532         }
533         spin_unlock(&root->fs_info->ordered_extent_lock);
534         wake_up(&entry->wait);
535 }
536
537 static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
538 {
539         struct btrfs_ordered_extent *ordered;
540
541         ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
542         btrfs_start_ordered_extent(ordered->inode, ordered, 1);
543         complete(&ordered->completion);
544 }
545
546 /*
547  * wait for all the ordered extents in a root.  This is done when balancing
548  * space between drives.
549  */
550 void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput)
551 {
552         struct list_head splice, works;
553         struct list_head *cur;
554         struct btrfs_ordered_extent *ordered, *next;
555         struct inode *inode;
556
557         INIT_LIST_HEAD(&splice);
558         INIT_LIST_HEAD(&works);
559
560         spin_lock(&root->fs_info->ordered_extent_lock);
561         list_splice_init(&root->fs_info->ordered_extents, &splice);
562         while (!list_empty(&splice)) {
563                 cur = splice.next;
564                 ordered = list_entry(cur, struct btrfs_ordered_extent,
565                                      root_extent_list);
566                 list_del_init(&ordered->root_extent_list);
567                 atomic_inc(&ordered->refs);
568
569                 /*
570                  * the inode may be getting freed (in sys_unlink path).
571                  */
572                 inode = igrab(ordered->inode);
573
574                 spin_unlock(&root->fs_info->ordered_extent_lock);
575
576                 if (inode) {
577                         ordered->flush_work.func = btrfs_run_ordered_extent_work;
578                         list_add_tail(&ordered->work_list, &works);
579                         btrfs_queue_worker(&root->fs_info->flush_workers,
580                                            &ordered->flush_work);
581                 } else {
582                         btrfs_put_ordered_extent(ordered);
583                 }
584
585                 cond_resched();
586                 spin_lock(&root->fs_info->ordered_extent_lock);
587         }
588         spin_unlock(&root->fs_info->ordered_extent_lock);
589
590         list_for_each_entry_safe(ordered, next, &works, work_list) {
591                 list_del_init(&ordered->work_list);
592                 wait_for_completion(&ordered->completion);
593
594                 inode = ordered->inode;
595                 btrfs_put_ordered_extent(ordered);
596                 if (delay_iput)
597                         btrfs_add_delayed_iput(inode);
598                 else
599                         iput(inode);
600
601                 cond_resched();
602         }
603 }
604
605 /*
606  * this is used during transaction commit to write all the inodes
607  * added to the ordered operation list.  These files must be fully on
608  * disk before the transaction commits.
609  *
610  * we have two modes here, one is to just start the IO via filemap_flush
611  * and the other is to wait for all the io.  When we wait, we have an
612  * extra check to make sure the ordered operation list really is empty
613  * before we return
614  */
615 int btrfs_run_ordered_operations(struct btrfs_root *root, int wait)
616 {
617         struct btrfs_inode *btrfs_inode;
618         struct inode *inode;
619         struct list_head splice;
620         struct list_head works;
621         struct btrfs_delalloc_work *work, *next;
622         int ret = 0;
623
624         INIT_LIST_HEAD(&splice);
625         INIT_LIST_HEAD(&works);
626
627         mutex_lock(&root->fs_info->ordered_operations_mutex);
628         spin_lock(&root->fs_info->ordered_extent_lock);
629 again:
630         list_splice_init(&root->fs_info->ordered_operations, &splice);
631
632         while (!list_empty(&splice)) {
633
634                 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
635                                    ordered_operations);
636
637                 inode = &btrfs_inode->vfs_inode;
638
639                 list_del_init(&btrfs_inode->ordered_operations);
640
641                 /*
642                  * the inode may be getting freed (in sys_unlink path).
643                  */
644                 inode = igrab(inode);
645
646                 if (!wait && inode) {
647                         list_add_tail(&BTRFS_I(inode)->ordered_operations,
648                               &root->fs_info->ordered_operations);
649                 }
650
651                 if (!inode)
652                         continue;
653                 spin_unlock(&root->fs_info->ordered_extent_lock);
654
655                 work = btrfs_alloc_delalloc_work(inode, wait, 1);
656                 if (!work) {
657                         if (list_empty(&BTRFS_I(inode)->ordered_operations))
658                                 list_add_tail(&btrfs_inode->ordered_operations,
659                                               &splice);
660                         spin_lock(&root->fs_info->ordered_extent_lock);
661                         list_splice_tail(&splice,
662                                          &root->fs_info->ordered_operations);
663                         spin_unlock(&root->fs_info->ordered_extent_lock);
664                         ret = -ENOMEM;
665                         goto out;
666                 }
667                 list_add_tail(&work->list, &works);
668                 btrfs_queue_worker(&root->fs_info->flush_workers,
669                                    &work->work);
670
671                 cond_resched();
672                 spin_lock(&root->fs_info->ordered_extent_lock);
673         }
674         if (wait && !list_empty(&root->fs_info->ordered_operations))
675                 goto again;
676
677         spin_unlock(&root->fs_info->ordered_extent_lock);
678 out:
679         list_for_each_entry_safe(work, next, &works, list) {
680                 list_del_init(&work->list);
681                 btrfs_wait_and_free_delalloc_work(work);
682         }
683         mutex_unlock(&root->fs_info->ordered_operations_mutex);
684         return ret;
685 }
686
687 /*
688  * Used to start IO or wait for a given ordered extent to finish.
689  *
690  * If wait is one, this effectively waits on page writeback for all the pages
691  * in the extent, and it waits on the io completion code to insert
692  * metadata into the btree corresponding to the extent
693  */
694 void btrfs_start_ordered_extent(struct inode *inode,
695                                        struct btrfs_ordered_extent *entry,
696                                        int wait)
697 {
698         u64 start = entry->file_offset;
699         u64 end = start + entry->len - 1;
700
701         trace_btrfs_ordered_extent_start(inode, entry);
702
703         /*
704          * pages in the range can be dirty, clean or writeback.  We
705          * start IO on any dirty ones so the wait doesn't stall waiting
706          * for the flusher thread to find them
707          */
708         if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
709                 filemap_fdatawrite_range(inode->i_mapping, start, end);
710         if (wait) {
711                 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
712                                                  &entry->flags));
713         }
714 }
715
716 /*
717  * Used to wait on ordered extents across a large range of bytes.
718  */
719 void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
720 {
721         u64 end;
722         u64 orig_end;
723         struct btrfs_ordered_extent *ordered;
724
725         if (start + len < start) {
726                 orig_end = INT_LIMIT(loff_t);
727         } else {
728                 orig_end = start + len - 1;
729                 if (orig_end > INT_LIMIT(loff_t))
730                         orig_end = INT_LIMIT(loff_t);
731         }
732
733         /* start IO across the range first to instantiate any delalloc
734          * extents
735          */
736         filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
737
738         /*
739          * So with compression we will find and lock a dirty page and clear the
740          * first one as dirty, setup an async extent, and immediately return
741          * with the entire range locked but with nobody actually marked with
742          * writeback.  So we can't just filemap_write_and_wait_range() and
743          * expect it to work since it will just kick off a thread to do the
744          * actual work.  So we need to call filemap_fdatawrite_range _again_
745          * since it will wait on the page lock, which won't be unlocked until
746          * after the pages have been marked as writeback and so we're good to go
747          * from there.  We have to do this otherwise we'll miss the ordered
748          * extents and that results in badness.  Please Josef, do not think you
749          * know better and pull this out at some point in the future, it is
750          * right and you are wrong.
751          */
752         if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
753                      &BTRFS_I(inode)->runtime_flags))
754                 filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
755
756         filemap_fdatawait_range(inode->i_mapping, start, orig_end);
757
758         end = orig_end;
759         while (1) {
760                 ordered = btrfs_lookup_first_ordered_extent(inode, end);
761                 if (!ordered)
762                         break;
763                 if (ordered->file_offset > orig_end) {
764                         btrfs_put_ordered_extent(ordered);
765                         break;
766                 }
767                 if (ordered->file_offset + ordered->len < start) {
768                         btrfs_put_ordered_extent(ordered);
769                         break;
770                 }
771                 btrfs_start_ordered_extent(inode, ordered, 1);
772                 end = ordered->file_offset;
773                 btrfs_put_ordered_extent(ordered);
774                 if (end == 0 || end == start)
775                         break;
776                 end--;
777         }
778 }
779
780 /*
781  * find an ordered extent corresponding to file_offset.  return NULL if
782  * nothing is found, otherwise take a reference on the extent and return it
783  */
784 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
785                                                          u64 file_offset)
786 {
787         struct btrfs_ordered_inode_tree *tree;
788         struct rb_node *node;
789         struct btrfs_ordered_extent *entry = NULL;
790
791         tree = &BTRFS_I(inode)->ordered_tree;
792         spin_lock_irq(&tree->lock);
793         node = tree_search(tree, file_offset);
794         if (!node)
795                 goto out;
796
797         entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
798         if (!offset_in_entry(entry, file_offset))
799                 entry = NULL;
800         if (entry)
801                 atomic_inc(&entry->refs);
802 out:
803         spin_unlock_irq(&tree->lock);
804         return entry;
805 }
806
807 /* Since the DIO code tries to lock a wide area we need to look for any ordered
808  * extents that exist in the range, rather than just the start of the range.
809  */
810 struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
811                                                         u64 file_offset,
812                                                         u64 len)
813 {
814         struct btrfs_ordered_inode_tree *tree;
815         struct rb_node *node;
816         struct btrfs_ordered_extent *entry = NULL;
817
818         tree = &BTRFS_I(inode)->ordered_tree;
819         spin_lock_irq(&tree->lock);
820         node = tree_search(tree, file_offset);
821         if (!node) {
822                 node = tree_search(tree, file_offset + len);
823                 if (!node)
824                         goto out;
825         }
826
827         while (1) {
828                 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
829                 if (range_overlaps(entry, file_offset, len))
830                         break;
831
832                 if (entry->file_offset >= file_offset + len) {
833                         entry = NULL;
834                         break;
835                 }
836                 entry = NULL;
837                 node = rb_next(node);
838                 if (!node)
839                         break;
840         }
841 out:
842         if (entry)
843                 atomic_inc(&entry->refs);
844         spin_unlock_irq(&tree->lock);
845         return entry;
846 }
847
848 /*
849  * lookup and return any extent before 'file_offset'.  NULL is returned
850  * if none is found
851  */
852 struct btrfs_ordered_extent *
853 btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
854 {
855         struct btrfs_ordered_inode_tree *tree;
856         struct rb_node *node;
857         struct btrfs_ordered_extent *entry = NULL;
858
859         tree = &BTRFS_I(inode)->ordered_tree;
860         spin_lock_irq(&tree->lock);
861         node = tree_search(tree, file_offset);
862         if (!node)
863                 goto out;
864
865         entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
866         atomic_inc(&entry->refs);
867 out:
868         spin_unlock_irq(&tree->lock);
869         return entry;
870 }
871
872 /*
873  * After an extent is done, call this to conditionally update the on disk
874  * i_size.  i_size is updated to cover any fully written part of the file.
875  */
876 int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
877                                 struct btrfs_ordered_extent *ordered)
878 {
879         struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
880         u64 disk_i_size;
881         u64 new_i_size;
882         u64 i_size = i_size_read(inode);
883         struct rb_node *node;
884         struct rb_node *prev = NULL;
885         struct btrfs_ordered_extent *test;
886         int ret = 1;
887
888         if (ordered)
889                 offset = entry_end(ordered);
890         else
891                 offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
892
893         spin_lock_irq(&tree->lock);
894         disk_i_size = BTRFS_I(inode)->disk_i_size;
895
896         /* truncate file */
897         if (disk_i_size > i_size) {
898                 BTRFS_I(inode)->disk_i_size = i_size;
899                 ret = 0;
900                 goto out;
901         }
902
903         /*
904          * if the disk i_size is already at the inode->i_size, or
905          * this ordered extent is inside the disk i_size, we're done
906          */
907         if (disk_i_size == i_size)
908                 goto out;
909
910         /*
911          * We still need to update disk_i_size if outstanding_isize is greater
912          * than disk_i_size.
913          */
914         if (offset <= disk_i_size &&
915             (!ordered || ordered->outstanding_isize <= disk_i_size))
916                 goto out;
917
918         /*
919          * walk backward from this ordered extent to disk_i_size.
920          * if we find an ordered extent then we can't update disk i_size
921          * yet
922          */
923         if (ordered) {
924                 node = rb_prev(&ordered->rb_node);
925         } else {
926                 prev = tree_search(tree, offset);
927                 /*
928                  * we insert file extents without involving ordered struct,
929                  * so there should be no ordered struct cover this offset
930                  */
931                 if (prev) {
932                         test = rb_entry(prev, struct btrfs_ordered_extent,
933                                         rb_node);
934                         BUG_ON(offset_in_entry(test, offset));
935                 }
936                 node = prev;
937         }
938         for (; node; node = rb_prev(node)) {
939                 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
940
941                 /* We treat this entry as if it doesnt exist */
942                 if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
943                         continue;
944                 if (test->file_offset + test->len <= disk_i_size)
945                         break;
946                 if (test->file_offset >= i_size)
947                         break;
948                 if (entry_end(test) > disk_i_size) {
949                         /*
950                          * we don't update disk_i_size now, so record this
951                          * undealt i_size. Or we will not know the real
952                          * i_size.
953                          */
954                         if (test->outstanding_isize < offset)
955                                 test->outstanding_isize = offset;
956                         if (ordered &&
957                             ordered->outstanding_isize >
958                             test->outstanding_isize)
959                                 test->outstanding_isize =
960                                                 ordered->outstanding_isize;
961                         goto out;
962                 }
963         }
964         new_i_size = min_t(u64, offset, i_size);
965
966         /*
967          * Some ordered extents may completed before the current one, and
968          * we hold the real i_size in ->outstanding_isize.
969          */
970         if (ordered && ordered->outstanding_isize > new_i_size)
971                 new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
972         BTRFS_I(inode)->disk_i_size = new_i_size;
973         ret = 0;
974 out:
975         /*
976          * We need to do this because we can't remove ordered extents until
977          * after the i_disk_size has been updated and then the inode has been
978          * updated to reflect the change, so we need to tell anybody who finds
979          * this ordered extent that we've already done all the real work, we
980          * just haven't completed all the other work.
981          */
982         if (ordered)
983                 set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
984         spin_unlock_irq(&tree->lock);
985         return ret;
986 }
987
988 /*
989  * search the ordered extents for one corresponding to 'offset' and
990  * try to find a checksum.  This is used because we allow pages to
991  * be reclaimed before their checksum is actually put into the btree
992  */
993 int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
994                            u32 *sum)
995 {
996         struct btrfs_ordered_sum *ordered_sum;
997         struct btrfs_sector_sum *sector_sums;
998         struct btrfs_ordered_extent *ordered;
999         struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
1000         unsigned long num_sectors;
1001         unsigned long i;
1002         u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
1003         int ret = 1;
1004
1005         ordered = btrfs_lookup_ordered_extent(inode, offset);
1006         if (!ordered)
1007                 return 1;
1008
1009         spin_lock_irq(&tree->lock);
1010         list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
1011                 if (disk_bytenr >= ordered_sum->bytenr) {
1012                         num_sectors = ordered_sum->len / sectorsize;
1013                         sector_sums = ordered_sum->sums;
1014                         for (i = 0; i < num_sectors; i++) {
1015                                 if (sector_sums[i].bytenr == disk_bytenr) {
1016                                         *sum = sector_sums[i].sum;
1017                                         ret = 0;
1018                                         goto out;
1019                                 }
1020                         }
1021                 }
1022         }
1023 out:
1024         spin_unlock_irq(&tree->lock);
1025         btrfs_put_ordered_extent(ordered);
1026         return ret;
1027 }
1028
1029
1030 /*
1031  * add a given inode to the list of inodes that must be fully on
1032  * disk before a transaction commit finishes.
1033  *
1034  * This basically gives us the ext3 style data=ordered mode, and it is mostly
1035  * used to make sure renamed files are fully on disk.
1036  *
1037  * It is a noop if the inode is already fully on disk.
1038  *
1039  * If trans is not null, we'll do a friendly check for a transaction that
1040  * is already flushing things and force the IO down ourselves.
1041  */
1042 void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
1043                                  struct btrfs_root *root, struct inode *inode)
1044 {
1045         u64 last_mod;
1046
1047         last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
1048
1049         /*
1050          * if this file hasn't been changed since the last transaction
1051          * commit, we can safely return without doing anything
1052          */
1053         if (last_mod < root->fs_info->last_trans_committed)
1054                 return;
1055
1056         spin_lock(&root->fs_info->ordered_extent_lock);
1057         if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
1058                 list_add_tail(&BTRFS_I(inode)->ordered_operations,
1059                               &root->fs_info->ordered_operations);
1060         }
1061         spin_unlock(&root->fs_info->ordered_extent_lock);
1062 }
1063
1064 int __init ordered_data_init(void)
1065 {
1066         btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1067                                      sizeof(struct btrfs_ordered_extent), 0,
1068                                      SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
1069                                      NULL);
1070         if (!btrfs_ordered_extent_cache)
1071                 return -ENOMEM;
1072
1073         return 0;
1074 }
1075
1076 void ordered_data_exit(void)
1077 {
1078         if (btrfs_ordered_extent_cache)
1079                 kmem_cache_destroy(btrfs_ordered_extent_cache);
1080 }