blob: e42fd233e04cdef068acb94df92303b1aa09dc75 [file] [log] [blame]
Chris Masondc17ff82008-01-08 15:46:30 -05001/*
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/gfp.h>
20#include <linux/slab.h>
Chris Masond6bfde82008-04-30 13:59:35 -040021#include <linux/blkdev.h>
Chris Masonf4219502008-07-22 11:18:09 -040022#include <linux/writeback.h>
23#include <linux/pagevec.h>
Chris Masondc17ff82008-01-08 15:46:30 -050024#include "ctree.h"
25#include "transaction.h"
26#include "btrfs_inode.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040027#include "extent_io.h"
Chris Masondc17ff82008-01-08 15:46:30 -050028
Chris Masondc17ff82008-01-08 15:46:30 -050029
Chris Masone6dcd2d2008-07-17 12:53:50 -040030static u64 entry_end(struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -050031{
Chris Masone6dcd2d2008-07-17 12:53:50 -040032 if (entry->file_offset + entry->len < entry->file_offset)
33 return (u64)-1;
34 return entry->file_offset + entry->len;
Chris Masondc17ff82008-01-08 15:46:30 -050035}
36
Chris Masone6dcd2d2008-07-17 12:53:50 -040037static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
38 struct rb_node *node)
Chris Masondc17ff82008-01-08 15:46:30 -050039{
40 struct rb_node ** p = &root->rb_node;
41 struct rb_node * parent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040042 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -050043
44 while(*p) {
45 parent = *p;
Chris Masone6dcd2d2008-07-17 12:53:50 -040046 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050047
Chris Masone6dcd2d2008-07-17 12:53:50 -040048 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050049 p = &(*p)->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040050 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050051 p = &(*p)->rb_right;
52 else
53 return parent;
54 }
55
56 rb_link_node(node, parent, p);
57 rb_insert_color(node, root);
58 return NULL;
59}
60
Chris Masone6dcd2d2008-07-17 12:53:50 -040061static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
62 struct rb_node **prev_ret)
Chris Masondc17ff82008-01-08 15:46:30 -050063{
64 struct rb_node * n = root->rb_node;
65 struct rb_node *prev = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040066 struct rb_node *test;
67 struct btrfs_ordered_extent *entry;
68 struct btrfs_ordered_extent *prev_entry = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -050069
70 while(n) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040071 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050072 prev = n;
73 prev_entry = entry;
Chris Masondc17ff82008-01-08 15:46:30 -050074
Chris Masone6dcd2d2008-07-17 12:53:50 -040075 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050076 n = n->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040077 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050078 n = n->rb_right;
79 else
80 return n;
81 }
82 if (!prev_ret)
83 return NULL;
84
Chris Masone6dcd2d2008-07-17 12:53:50 -040085 while(prev && file_offset >= entry_end(prev_entry)) {
86 test = rb_next(prev);
87 if (!test)
88 break;
89 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
90 rb_node);
91 if (file_offset < entry_end(prev_entry))
92 break;
93
94 prev = test;
95 }
96 if (prev)
97 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
98 rb_node);
99 while(prev && file_offset < entry_end(prev_entry)) {
100 test = rb_prev(prev);
101 if (!test)
102 break;
103 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
104 rb_node);
105 prev = test;
Chris Masondc17ff82008-01-08 15:46:30 -0500106 }
107 *prev_ret = prev;
108 return NULL;
109}
110
Chris Masone6dcd2d2008-07-17 12:53:50 -0400111static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -0500112{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400113 if (file_offset < entry->file_offset ||
114 entry->file_offset + entry->len <= file_offset)
115 return 0;
116 return 1;
117}
118
119static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
120 u64 file_offset)
121{
122 struct rb_root *root = &tree->tree;
Chris Masondc17ff82008-01-08 15:46:30 -0500123 struct rb_node *prev;
124 struct rb_node *ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400125 struct btrfs_ordered_extent *entry;
126
127 if (tree->last) {
128 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
129 rb_node);
130 if (offset_in_entry(entry, file_offset))
131 return tree->last;
132 }
133 ret = __tree_search(root, file_offset, &prev);
Chris Masondc17ff82008-01-08 15:46:30 -0500134 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 ret = prev;
136 if (ret)
137 tree->last = ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500138 return ret;
139}
140
Chris Masoneb84ae02008-07-17 13:53:27 -0400141/* allocate and add a new ordered_extent into the per-inode tree.
142 * file_offset is the logical offset in the file
143 *
144 * start is the disk block number of an extent already reserved in the
145 * extent allocation tree
146 *
147 * len is the length of the extent
148 *
149 * This also sets the EXTENT_ORDERED bit on the range in the inode.
150 *
151 * The tree is given a single reference on the ordered extent that was
152 * inserted.
153 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
155 u64 start, u64 len)
Chris Masondc17ff82008-01-08 15:46:30 -0500156{
Chris Masondc17ff82008-01-08 15:46:30 -0500157 struct btrfs_ordered_inode_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400158 struct rb_node *node;
159 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -0500160
Chris Masone6dcd2d2008-07-17 12:53:50 -0400161 tree = &BTRFS_I(inode)->ordered_tree;
162 entry = kzalloc(sizeof(*entry), GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500163 if (!entry)
164 return -ENOMEM;
165
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166 mutex_lock(&tree->mutex);
167 entry->file_offset = file_offset;
168 entry->start = start;
169 entry->len = len;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400170 /* one ref for the tree */
171 atomic_set(&entry->refs, 1);
172 init_waitqueue_head(&entry->wait);
173 INIT_LIST_HEAD(&entry->list);
Chris Masondc17ff82008-01-08 15:46:30 -0500174
Chris Masone6dcd2d2008-07-17 12:53:50 -0400175 node = tree_insert(&tree->tree, file_offset,
176 &entry->rb_node);
177 if (node) {
178 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
179 atomic_inc(&entry->refs);
180 }
181 set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset,
182 entry_end(entry) - 1, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500183
Chris Masone6dcd2d2008-07-17 12:53:50 -0400184 mutex_unlock(&tree->mutex);
185 BUG_ON(node);
186 return 0;
187}
Chris Masondc17ff82008-01-08 15:46:30 -0500188
Chris Masoneb84ae02008-07-17 13:53:27 -0400189/*
190 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400191 * when an ordered extent is finished. If the list covers more than one
192 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400193 */
Chris Mason3edf7d32008-07-18 06:17:13 -0400194int btrfs_add_ordered_sum(struct inode *inode,
195 struct btrfs_ordered_extent *entry,
196 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400197{
198 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400199
Chris Masone6dcd2d2008-07-17 12:53:50 -0400200 tree = &BTRFS_I(inode)->ordered_tree;
201 mutex_lock(&tree->mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400202 list_add_tail(&sum->list, &entry->list);
203 mutex_unlock(&tree->mutex);
204 return 0;
205}
206
Chris Masoneb84ae02008-07-17 13:53:27 -0400207/*
208 * this is used to account for finished IO across a given range
209 * of the file. The IO should not span ordered extents. If
210 * a given ordered_extent is completely done, 1 is returned, otherwise
211 * 0.
212 *
213 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
214 * to make sure this function only returns 1 once for a given ordered extent.
215 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400216int btrfs_dec_test_ordered_pending(struct inode *inode,
217 u64 file_offset, u64 io_size)
218{
219 struct btrfs_ordered_inode_tree *tree;
220 struct rb_node *node;
221 struct btrfs_ordered_extent *entry;
222 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
223 int ret;
224
225 tree = &BTRFS_I(inode)->ordered_tree;
226 mutex_lock(&tree->mutex);
227 clear_extent_ordered(io_tree, file_offset, file_offset + io_size - 1,
228 GFP_NOFS);
229 node = tree_search(tree, file_offset);
230 if (!node) {
231 ret = 1;
232 goto out;
233 }
234
235 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
236 if (!offset_in_entry(entry, file_offset)) {
237 ret = 1;
238 goto out;
239 }
240
241 ret = test_range_bit(io_tree, entry->file_offset,
242 entry->file_offset + entry->len - 1,
243 EXTENT_ORDERED, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400244 if (ret == 0)
245 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
246out:
247 mutex_unlock(&tree->mutex);
248 return ret == 0;
249}
250
Chris Masoneb84ae02008-07-17 13:53:27 -0400251/*
252 * used to drop a reference on an ordered extent. This will free
253 * the extent if the last reference is dropped
254 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400255int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
256{
Chris Masonba1da2f2008-07-17 12:54:15 -0400257 struct list_head *cur;
258 struct btrfs_ordered_sum *sum;
259
260 if (atomic_dec_and_test(&entry->refs)) {
261 while(!list_empty(&entry->list)) {
262 cur = entry->list.next;
263 sum = list_entry(cur, struct btrfs_ordered_sum, list);
264 list_del(&sum->list);
265 kfree(sum);
266 }
Chris Masondc17ff82008-01-08 15:46:30 -0500267 kfree(entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400268 }
Chris Masondc17ff82008-01-08 15:46:30 -0500269 return 0;
270}
271
Chris Masoneb84ae02008-07-17 13:53:27 -0400272/*
273 * remove an ordered extent from the tree. No references are dropped
274 * but, anyone waiting on this extent is woken up.
275 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400276int btrfs_remove_ordered_extent(struct inode *inode,
277 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500278{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400279 struct btrfs_ordered_inode_tree *tree;
Chris Masondc17ff82008-01-08 15:46:30 -0500280 struct rb_node *node;
281
Chris Masone6dcd2d2008-07-17 12:53:50 -0400282 tree = &BTRFS_I(inode)->ordered_tree;
283 mutex_lock(&tree->mutex);
284 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500285 rb_erase(node, &tree->tree);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400286 tree->last = NULL;
287 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
288 mutex_unlock(&tree->mutex);
289 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400290 return 0;
291}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400292
Chris Masoneb84ae02008-07-17 13:53:27 -0400293/*
294 * Used to start IO or wait for a given ordered extent to finish.
295 *
296 * If wait is one, this effectively waits on page writeback for all the pages
297 * in the extent, and it waits on the io completion code to insert
298 * metadata into the btree corresponding to the extent
299 */
300void btrfs_start_ordered_extent(struct inode *inode,
301 struct btrfs_ordered_extent *entry,
302 int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400303{
304 u64 start = entry->file_offset;
305 u64 end = start + entry->len - 1;
306
Chris Masoneb84ae02008-07-17 13:53:27 -0400307 /*
308 * pages in the range can be dirty, clean or writeback. We
309 * start IO on any dirty ones so the wait doesn't stall waiting
310 * for pdflush to find them
311 */
Chris Masonf4219502008-07-22 11:18:09 -0400312 btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_NONE);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400313 if (wait)
314 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
315 &entry->flags));
316}
317
Chris Masoneb84ae02008-07-17 13:53:27 -0400318/*
319 * Used to wait on ordered extents across a large range of bytes.
320 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400321void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
322{
323 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400324 u64 orig_end;
325 u64 wait_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400326 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400327
Chris Masone5a22172008-07-18 20:42:20 -0400328 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400329 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400330 } else {
331 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400332 if (orig_end > INT_LIMIT(loff_t))
333 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400334 }
Chris Masonf4219502008-07-22 11:18:09 -0400335 wait_end = orig_end;
Chris Mason4a096752008-07-21 10:29:44 -0400336again:
Chris Masone5a22172008-07-18 20:42:20 -0400337 /* start IO across the range first to instantiate any delalloc
338 * extents
339 */
Chris Masonf4219502008-07-22 11:18:09 -0400340 btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_NONE);
Chris Masone5a22172008-07-18 20:42:20 -0400341
Chris Masonf4219502008-07-22 11:18:09 -0400342 btrfs_wait_on_page_writeback_range(inode->i_mapping,
343 start >> PAGE_CACHE_SHIFT,
344 orig_end >> PAGE_CACHE_SHIFT);
345
346 end = orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400347 while(1) {
348 ordered = btrfs_lookup_first_ordered_extent(inode, end);
349 if (!ordered) {
350 break;
351 }
Chris Masone5a22172008-07-18 20:42:20 -0400352 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400353 btrfs_put_ordered_extent(ordered);
354 break;
355 }
356 if (ordered->file_offset + ordered->len < start) {
357 btrfs_put_ordered_extent(ordered);
358 break;
359 }
Chris Masone5a22172008-07-18 20:42:20 -0400360 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400361 end = ordered->file_offset;
362 btrfs_put_ordered_extent(ordered);
Chris Masone5a22172008-07-18 20:42:20 -0400363 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400364 break;
365 end--;
366 }
Chris Mason4a096752008-07-21 10:29:44 -0400367 if (test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
368 EXTENT_ORDERED | EXTENT_DELALLOC, 0)) {
369 printk("inode %lu still ordered or delalloc after wait "
370 "%llu %llu\n", inode->i_ino,
371 (unsigned long long)start,
372 (unsigned long long)orig_end);
373 goto again;
374 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400375}
376
Chris Masoneb84ae02008-07-17 13:53:27 -0400377/*
378 * find an ordered extent corresponding to file_offset. return NULL if
379 * nothing is found, otherwise take a reference on the extent and return it
380 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400381struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
382 u64 file_offset)
383{
384 struct btrfs_ordered_inode_tree *tree;
385 struct rb_node *node;
386 struct btrfs_ordered_extent *entry = NULL;
387
388 tree = &BTRFS_I(inode)->ordered_tree;
389 mutex_lock(&tree->mutex);
390 node = tree_search(tree, file_offset);
391 if (!node)
392 goto out;
393
394 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
395 if (!offset_in_entry(entry, file_offset))
396 entry = NULL;
397 if (entry)
398 atomic_inc(&entry->refs);
399out:
400 mutex_unlock(&tree->mutex);
401 return entry;
402}
403
Chris Masoneb84ae02008-07-17 13:53:27 -0400404/*
405 * lookup and return any extent before 'file_offset'. NULL is returned
406 * if none is found
407 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400408struct btrfs_ordered_extent *
409btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset)
410{
411 struct btrfs_ordered_inode_tree *tree;
412 struct rb_node *node;
413 struct btrfs_ordered_extent *entry = NULL;
414
415 tree = &BTRFS_I(inode)->ordered_tree;
416 mutex_lock(&tree->mutex);
417 node = tree_search(tree, file_offset);
418 if (!node)
419 goto out;
420
421 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
422 atomic_inc(&entry->refs);
423out:
424 mutex_unlock(&tree->mutex);
425 return entry;
426}
Chris Masondbe674a2008-07-17 12:54:05 -0400427
Chris Masoneb84ae02008-07-17 13:53:27 -0400428/*
429 * After an extent is done, call this to conditionally update the on disk
430 * i_size. i_size is updated to cover any fully written part of the file.
431 */
Chris Masondbe674a2008-07-17 12:54:05 -0400432int btrfs_ordered_update_i_size(struct inode *inode,
433 struct btrfs_ordered_extent *ordered)
434{
435 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
436 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
437 u64 disk_i_size;
438 u64 new_i_size;
439 u64 i_size_test;
440 struct rb_node *node;
441 struct btrfs_ordered_extent *test;
442
443 mutex_lock(&tree->mutex);
444 disk_i_size = BTRFS_I(inode)->disk_i_size;
445
446 /*
447 * if the disk i_size is already at the inode->i_size, or
448 * this ordered extent is inside the disk i_size, we're done
449 */
450 if (disk_i_size >= inode->i_size ||
451 ordered->file_offset + ordered->len <= disk_i_size) {
452 goto out;
453 }
454
455 /*
456 * we can't update the disk_isize if there are delalloc bytes
457 * between disk_i_size and this ordered extent
458 */
459 if (test_range_bit(io_tree, disk_i_size,
460 ordered->file_offset + ordered->len - 1,
461 EXTENT_DELALLOC, 0)) {
462 goto out;
463 }
464 /*
465 * walk backward from this ordered extent to disk_i_size.
466 * if we find an ordered extent then we can't update disk i_size
467 * yet
468 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400469 node = &ordered->rb_node;
Chris Masondbe674a2008-07-17 12:54:05 -0400470 while(1) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400471 node = rb_prev(node);
Chris Masondbe674a2008-07-17 12:54:05 -0400472 if (!node)
473 break;
474 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
475 if (test->file_offset + test->len <= disk_i_size)
476 break;
477 if (test->file_offset >= inode->i_size)
478 break;
479 if (test->file_offset >= disk_i_size)
480 goto out;
481 }
482 new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode));
483
484 /*
485 * at this point, we know we can safely update i_size to at least
486 * the offset from this ordered extent. But, we need to
487 * walk forward and see if ios from higher up in the file have
488 * finished.
489 */
490 node = rb_next(&ordered->rb_node);
491 i_size_test = 0;
492 if (node) {
493 /*
494 * do we have an area where IO might have finished
495 * between our ordered extent and the next one.
496 */
497 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
498 if (test->file_offset > entry_end(ordered)) {
499 i_size_test = test->file_offset - 1;
500 }
501 } else {
502 i_size_test = i_size_read(inode);
503 }
504
505 /*
506 * i_size_test is the end of a region after this ordered
507 * extent where there are no ordered extents. As long as there
508 * are no delalloc bytes in this area, it is safe to update
509 * disk_i_size to the end of the region.
510 */
511 if (i_size_test > entry_end(ordered) &&
512 !test_range_bit(io_tree, entry_end(ordered), i_size_test,
513 EXTENT_DELALLOC, 0)) {
514 new_i_size = min_t(u64, i_size_test, i_size_read(inode));
515 }
516 BTRFS_I(inode)->disk_i_size = new_i_size;
517out:
518 mutex_unlock(&tree->mutex);
519 return 0;
520}
Chris Masonba1da2f2008-07-17 12:54:15 -0400521
Chris Masoneb84ae02008-07-17 13:53:27 -0400522/*
523 * search the ordered extents for one corresponding to 'offset' and
524 * try to find a checksum. This is used because we allow pages to
525 * be reclaimed before their checksum is actually put into the btree
526 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400527int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
528{
529 struct btrfs_ordered_sum *ordered_sum;
530 struct btrfs_sector_sum *sector_sums;
531 struct btrfs_ordered_extent *ordered;
532 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
533 struct list_head *cur;
Chris Mason3edf7d32008-07-18 06:17:13 -0400534 unsigned long num_sectors;
535 unsigned long i;
536 u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
Chris Masonba1da2f2008-07-17 12:54:15 -0400537 int ret = 1;
Chris Masonba1da2f2008-07-17 12:54:15 -0400538
539 ordered = btrfs_lookup_ordered_extent(inode, offset);
540 if (!ordered)
541 return 1;
542
543 mutex_lock(&tree->mutex);
544 list_for_each_prev(cur, &ordered->list) {
545 ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Mason3edf7d32008-07-18 06:17:13 -0400546 if (offset >= ordered_sum->file_offset) {
547 num_sectors = ordered_sum->len / sectorsize;
Chris Masoned98b562008-07-22 23:06:42 -0400548 sector_sums = ordered_sum->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400549 for (i = 0; i < num_sectors; i++) {
550 if (sector_sums[i].offset == offset) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400551 *sum = sector_sums[i].sum;
552 ret = 0;
553 goto out;
554 }
555 }
Chris Masonba1da2f2008-07-17 12:54:15 -0400556 }
557 }
558out:
559 mutex_unlock(&tree->mutex);
Chris Mason89642222008-07-24 09:41:53 -0400560 btrfs_put_ordered_extent(ordered);
Chris Masonba1da2f2008-07-17 12:54:15 -0400561 return ret;
562}
563
Chris Masonf4219502008-07-22 11:18:09 -0400564
565/**
566 * taken from mm/filemap.c because it isn't exported
567 *
568 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
569 * @mapping: address space structure to write
570 * @start: offset in bytes where the range starts
571 * @end: offset in bytes where the range ends (inclusive)
572 * @sync_mode: enable synchronous operation
573 *
574 * Start writeback against all of a mapping's dirty pages that lie
575 * within the byte offsets <start, end> inclusive.
576 *
577 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
578 * opposed to a regular memory cleansing writeback. The difference between
579 * these two operations is that if a dirty page/buffer is encountered, it must
580 * be waited upon, and not just skipped over.
581 */
582int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start,
583 loff_t end, int sync_mode)
584{
585 struct writeback_control wbc = {
586 .sync_mode = sync_mode,
587 .nr_to_write = mapping->nrpages * 2,
588 .range_start = start,
589 .range_end = end,
590 .for_writepages = 1,
591 };
592 return btrfs_writepages(mapping, &wbc);
593}
594
595/**
596 * taken from mm/filemap.c because it isn't exported
597 *
598 * wait_on_page_writeback_range - wait for writeback to complete
599 * @mapping: target address_space
600 * @start: beginning page index
601 * @end: ending page index
602 *
603 * Wait for writeback to complete against pages indexed by start->end
604 * inclusive
605 */
606int btrfs_wait_on_page_writeback_range(struct address_space *mapping,
607 pgoff_t start, pgoff_t end)
608{
609 struct pagevec pvec;
610 int nr_pages;
611 int ret = 0;
612 pgoff_t index;
613
614 if (end < start)
615 return 0;
616
617 pagevec_init(&pvec, 0);
618 index = start;
619 while ((index <= end) &&
620 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
621 PAGECACHE_TAG_WRITEBACK,
622 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
623 unsigned i;
624
625 for (i = 0; i < nr_pages; i++) {
626 struct page *page = pvec.pages[i];
627
628 /* until radix tree lookup accepts end_index */
629 if (page->index > end)
630 continue;
631
632 wait_on_page_writeback(page);
633 if (PageError(page))
634 ret = -EIO;
635 }
636 pagevec_release(&pvec);
637 cond_resched();
638 }
639
640 /* Check for outstanding write errors */
641 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
642 ret = -ENOSPC;
643 if (test_and_clear_bit(AS_EIO, &mapping->flags))
644 ret = -EIO;
645
646 return ret;
647}