blob: db200e6baf7ee6efc130ddcb02872160487b9cc7 [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 Mason3eaa2882008-07-24 11:57:52 -0400170 entry->inode = inode;
171
Chris Masone6dcd2d2008-07-17 12:53:50 -0400172 /* one ref for the tree */
173 atomic_set(&entry->refs, 1);
174 init_waitqueue_head(&entry->wait);
175 INIT_LIST_HEAD(&entry->list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400176 INIT_LIST_HEAD(&entry->root_extent_list);
Chris Masondc17ff82008-01-08 15:46:30 -0500177
Chris Masone6dcd2d2008-07-17 12:53:50 -0400178 node = tree_insert(&tree->tree, file_offset,
179 &entry->rb_node);
180 if (node) {
Chris Mason3eaa2882008-07-24 11:57:52 -0400181 printk("warning dup entry from add_ordered_extent\n");
182 BUG();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400183 }
184 set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset,
185 entry_end(entry) - 1, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500186
Chris Mason3eaa2882008-07-24 11:57:52 -0400187 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
188 list_add_tail(&entry->root_extent_list,
189 &BTRFS_I(inode)->root->fs_info->ordered_extents);
190 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
191
Chris Masone6dcd2d2008-07-17 12:53:50 -0400192 mutex_unlock(&tree->mutex);
193 BUG_ON(node);
194 return 0;
195}
Chris Masondc17ff82008-01-08 15:46:30 -0500196
Chris Masoneb84ae02008-07-17 13:53:27 -0400197/*
198 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400199 * when an ordered extent is finished. If the list covers more than one
200 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400201 */
Chris Mason3edf7d32008-07-18 06:17:13 -0400202int btrfs_add_ordered_sum(struct inode *inode,
203 struct btrfs_ordered_extent *entry,
204 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400205{
206 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400207
Chris Masone6dcd2d2008-07-17 12:53:50 -0400208 tree = &BTRFS_I(inode)->ordered_tree;
209 mutex_lock(&tree->mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400210 list_add_tail(&sum->list, &entry->list);
211 mutex_unlock(&tree->mutex);
212 return 0;
213}
214
Chris Masoneb84ae02008-07-17 13:53:27 -0400215/*
216 * this is used to account for finished IO across a given range
217 * of the file. The IO should not span ordered extents. If
218 * a given ordered_extent is completely done, 1 is returned, otherwise
219 * 0.
220 *
221 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
222 * to make sure this function only returns 1 once for a given ordered extent.
223 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400224int btrfs_dec_test_ordered_pending(struct inode *inode,
225 u64 file_offset, u64 io_size)
226{
227 struct btrfs_ordered_inode_tree *tree;
228 struct rb_node *node;
229 struct btrfs_ordered_extent *entry;
230 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
231 int ret;
232
233 tree = &BTRFS_I(inode)->ordered_tree;
234 mutex_lock(&tree->mutex);
235 clear_extent_ordered(io_tree, file_offset, file_offset + io_size - 1,
236 GFP_NOFS);
237 node = tree_search(tree, file_offset);
238 if (!node) {
239 ret = 1;
240 goto out;
241 }
242
243 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
244 if (!offset_in_entry(entry, file_offset)) {
245 ret = 1;
246 goto out;
247 }
248
249 ret = test_range_bit(io_tree, entry->file_offset,
250 entry->file_offset + entry->len - 1,
251 EXTENT_ORDERED, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400252 if (ret == 0)
253 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
254out:
255 mutex_unlock(&tree->mutex);
256 return ret == 0;
257}
258
Chris Masoneb84ae02008-07-17 13:53:27 -0400259/*
260 * used to drop a reference on an ordered extent. This will free
261 * the extent if the last reference is dropped
262 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400263int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
264{
Chris Masonba1da2f2008-07-17 12:54:15 -0400265 struct list_head *cur;
266 struct btrfs_ordered_sum *sum;
267
268 if (atomic_dec_and_test(&entry->refs)) {
269 while(!list_empty(&entry->list)) {
270 cur = entry->list.next;
271 sum = list_entry(cur, struct btrfs_ordered_sum, list);
272 list_del(&sum->list);
273 kfree(sum);
274 }
Chris Masondc17ff82008-01-08 15:46:30 -0500275 kfree(entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400276 }
Chris Masondc17ff82008-01-08 15:46:30 -0500277 return 0;
278}
279
Chris Masoneb84ae02008-07-17 13:53:27 -0400280/*
281 * remove an ordered extent from the tree. No references are dropped
282 * but, anyone waiting on this extent is woken up.
283 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400284int btrfs_remove_ordered_extent(struct inode *inode,
285 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500286{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400287 struct btrfs_ordered_inode_tree *tree;
Chris Masondc17ff82008-01-08 15:46:30 -0500288 struct rb_node *node;
289
Chris Masone6dcd2d2008-07-17 12:53:50 -0400290 tree = &BTRFS_I(inode)->ordered_tree;
291 mutex_lock(&tree->mutex);
292 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500293 rb_erase(node, &tree->tree);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400294 tree->last = NULL;
295 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Chris Mason3eaa2882008-07-24 11:57:52 -0400296
297 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
298 list_del_init(&entry->root_extent_list);
299 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
300
Chris Masone6dcd2d2008-07-17 12:53:50 -0400301 mutex_unlock(&tree->mutex);
302 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400303 return 0;
304}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400305
Chris Mason3eaa2882008-07-24 11:57:52 -0400306int btrfs_wait_ordered_extents(struct btrfs_root *root)
307{
308 struct list_head splice;
309 struct list_head *cur;
310 struct btrfs_ordered_extent *ordered;
311 struct inode *inode;
312
313 INIT_LIST_HEAD(&splice);
314
315 spin_lock(&root->fs_info->ordered_extent_lock);
316 list_splice_init(&root->fs_info->ordered_extents, &splice);
317 while(!list_empty(&splice)) {
318 cur = splice.next;
319 ordered = list_entry(cur, struct btrfs_ordered_extent,
320 root_extent_list);
321 list_del_init(&ordered->root_extent_list);
322 atomic_inc(&ordered->refs);
323 inode = ordered->inode;
324
325 /*
326 * the inode can't go away until all the pages are gone
327 * and the pages won't go away while there is still
328 * an ordered extent and the ordered extent won't go
329 * away until it is off this list. So, we can safely
330 * increment i_count here and call iput later
331 */
332 atomic_inc(&inode->i_count);
333 spin_unlock(&root->fs_info->ordered_extent_lock);
334
335 btrfs_start_ordered_extent(inode, ordered, 1);
336 btrfs_put_ordered_extent(ordered);
337 iput(inode);
338
339 spin_lock(&root->fs_info->ordered_extent_lock);
340 }
341 spin_unlock(&root->fs_info->ordered_extent_lock);
342 return 0;
343}
344
Chris Masoneb84ae02008-07-17 13:53:27 -0400345/*
346 * Used to start IO or wait for a given ordered extent to finish.
347 *
348 * If wait is one, this effectively waits on page writeback for all the pages
349 * in the extent, and it waits on the io completion code to insert
350 * metadata into the btree corresponding to the extent
351 */
352void btrfs_start_ordered_extent(struct inode *inode,
353 struct btrfs_ordered_extent *entry,
354 int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400355{
356 u64 start = entry->file_offset;
357 u64 end = start + entry->len - 1;
358
Chris Masoneb84ae02008-07-17 13:53:27 -0400359 /*
360 * pages in the range can be dirty, clean or writeback. We
361 * start IO on any dirty ones so the wait doesn't stall waiting
362 * for pdflush to find them
363 */
Chris Masonf4219502008-07-22 11:18:09 -0400364 btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_NONE);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400365 if (wait)
366 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
367 &entry->flags));
368}
369
Chris Masoneb84ae02008-07-17 13:53:27 -0400370/*
371 * Used to wait on ordered extents across a large range of bytes.
372 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
374{
375 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400376 u64 orig_end;
377 u64 wait_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400378 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400379
Chris Masone5a22172008-07-18 20:42:20 -0400380 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400381 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400382 } else {
383 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400384 if (orig_end > INT_LIMIT(loff_t))
385 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400386 }
Chris Masonf4219502008-07-22 11:18:09 -0400387 wait_end = orig_end;
Chris Mason4a096752008-07-21 10:29:44 -0400388again:
Chris Masone5a22172008-07-18 20:42:20 -0400389 /* start IO across the range first to instantiate any delalloc
390 * extents
391 */
Chris Masonf4219502008-07-22 11:18:09 -0400392 btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_NONE);
Chris Masone5a22172008-07-18 20:42:20 -0400393
Chris Masonf4219502008-07-22 11:18:09 -0400394 btrfs_wait_on_page_writeback_range(inode->i_mapping,
395 start >> PAGE_CACHE_SHIFT,
396 orig_end >> PAGE_CACHE_SHIFT);
397
398 end = orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400399 while(1) {
400 ordered = btrfs_lookup_first_ordered_extent(inode, end);
401 if (!ordered) {
402 break;
403 }
Chris Masone5a22172008-07-18 20:42:20 -0400404 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 btrfs_put_ordered_extent(ordered);
406 break;
407 }
408 if (ordered->file_offset + ordered->len < start) {
409 btrfs_put_ordered_extent(ordered);
410 break;
411 }
Chris Masone5a22172008-07-18 20:42:20 -0400412 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400413 end = ordered->file_offset;
414 btrfs_put_ordered_extent(ordered);
Chris Masone5a22172008-07-18 20:42:20 -0400415 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400416 break;
417 end--;
418 }
Chris Mason4a096752008-07-21 10:29:44 -0400419 if (test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
420 EXTENT_ORDERED | EXTENT_DELALLOC, 0)) {
421 printk("inode %lu still ordered or delalloc after wait "
422 "%llu %llu\n", inode->i_ino,
423 (unsigned long long)start,
424 (unsigned long long)orig_end);
425 goto again;
426 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400427}
428
Chris Masoneb84ae02008-07-17 13:53:27 -0400429/*
430 * find an ordered extent corresponding to file_offset. return NULL if
431 * nothing is found, otherwise take a reference on the extent and return it
432 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400433struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
434 u64 file_offset)
435{
436 struct btrfs_ordered_inode_tree *tree;
437 struct rb_node *node;
438 struct btrfs_ordered_extent *entry = NULL;
439
440 tree = &BTRFS_I(inode)->ordered_tree;
441 mutex_lock(&tree->mutex);
442 node = tree_search(tree, file_offset);
443 if (!node)
444 goto out;
445
446 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
447 if (!offset_in_entry(entry, file_offset))
448 entry = NULL;
449 if (entry)
450 atomic_inc(&entry->refs);
451out:
452 mutex_unlock(&tree->mutex);
453 return entry;
454}
455
Chris Masoneb84ae02008-07-17 13:53:27 -0400456/*
457 * lookup and return any extent before 'file_offset'. NULL is returned
458 * if none is found
459 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400460struct btrfs_ordered_extent *
461btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset)
462{
463 struct btrfs_ordered_inode_tree *tree;
464 struct rb_node *node;
465 struct btrfs_ordered_extent *entry = NULL;
466
467 tree = &BTRFS_I(inode)->ordered_tree;
468 mutex_lock(&tree->mutex);
469 node = tree_search(tree, file_offset);
470 if (!node)
471 goto out;
472
473 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
474 atomic_inc(&entry->refs);
475out:
476 mutex_unlock(&tree->mutex);
477 return entry;
478}
Chris Masondbe674a2008-07-17 12:54:05 -0400479
Chris Masoneb84ae02008-07-17 13:53:27 -0400480/*
481 * After an extent is done, call this to conditionally update the on disk
482 * i_size. i_size is updated to cover any fully written part of the file.
483 */
Chris Masondbe674a2008-07-17 12:54:05 -0400484int btrfs_ordered_update_i_size(struct inode *inode,
485 struct btrfs_ordered_extent *ordered)
486{
487 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
488 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
489 u64 disk_i_size;
490 u64 new_i_size;
491 u64 i_size_test;
492 struct rb_node *node;
493 struct btrfs_ordered_extent *test;
494
495 mutex_lock(&tree->mutex);
496 disk_i_size = BTRFS_I(inode)->disk_i_size;
497
498 /*
499 * if the disk i_size is already at the inode->i_size, or
500 * this ordered extent is inside the disk i_size, we're done
501 */
502 if (disk_i_size >= inode->i_size ||
503 ordered->file_offset + ordered->len <= disk_i_size) {
504 goto out;
505 }
506
507 /*
508 * we can't update the disk_isize if there are delalloc bytes
509 * between disk_i_size and this ordered extent
510 */
511 if (test_range_bit(io_tree, disk_i_size,
512 ordered->file_offset + ordered->len - 1,
513 EXTENT_DELALLOC, 0)) {
514 goto out;
515 }
516 /*
517 * walk backward from this ordered extent to disk_i_size.
518 * if we find an ordered extent then we can't update disk i_size
519 * yet
520 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400521 node = &ordered->rb_node;
Chris Masondbe674a2008-07-17 12:54:05 -0400522 while(1) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400523 node = rb_prev(node);
Chris Masondbe674a2008-07-17 12:54:05 -0400524 if (!node)
525 break;
526 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
527 if (test->file_offset + test->len <= disk_i_size)
528 break;
529 if (test->file_offset >= inode->i_size)
530 break;
531 if (test->file_offset >= disk_i_size)
532 goto out;
533 }
534 new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode));
535
536 /*
537 * at this point, we know we can safely update i_size to at least
538 * the offset from this ordered extent. But, we need to
539 * walk forward and see if ios from higher up in the file have
540 * finished.
541 */
542 node = rb_next(&ordered->rb_node);
543 i_size_test = 0;
544 if (node) {
545 /*
546 * do we have an area where IO might have finished
547 * between our ordered extent and the next one.
548 */
549 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
550 if (test->file_offset > entry_end(ordered)) {
Yan Zhengb48652c2008-08-04 23:23:47 -0400551 i_size_test = test->file_offset;
Chris Masondbe674a2008-07-17 12:54:05 -0400552 }
553 } else {
554 i_size_test = i_size_read(inode);
555 }
556
557 /*
558 * i_size_test is the end of a region after this ordered
559 * extent where there are no ordered extents. As long as there
560 * are no delalloc bytes in this area, it is safe to update
561 * disk_i_size to the end of the region.
562 */
563 if (i_size_test > entry_end(ordered) &&
Yan Zhengb48652c2008-08-04 23:23:47 -0400564 !test_range_bit(io_tree, entry_end(ordered), i_size_test - 1,
Chris Masondbe674a2008-07-17 12:54:05 -0400565 EXTENT_DELALLOC, 0)) {
566 new_i_size = min_t(u64, i_size_test, i_size_read(inode));
567 }
568 BTRFS_I(inode)->disk_i_size = new_i_size;
569out:
570 mutex_unlock(&tree->mutex);
571 return 0;
572}
Chris Masonba1da2f2008-07-17 12:54:15 -0400573
Chris Masoneb84ae02008-07-17 13:53:27 -0400574/*
575 * search the ordered extents for one corresponding to 'offset' and
576 * try to find a checksum. This is used because we allow pages to
577 * be reclaimed before their checksum is actually put into the btree
578 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400579int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
580{
581 struct btrfs_ordered_sum *ordered_sum;
582 struct btrfs_sector_sum *sector_sums;
583 struct btrfs_ordered_extent *ordered;
584 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
585 struct list_head *cur;
Chris Mason3edf7d32008-07-18 06:17:13 -0400586 unsigned long num_sectors;
587 unsigned long i;
588 u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
Chris Masonba1da2f2008-07-17 12:54:15 -0400589 int ret = 1;
Chris Masonba1da2f2008-07-17 12:54:15 -0400590
591 ordered = btrfs_lookup_ordered_extent(inode, offset);
592 if (!ordered)
593 return 1;
594
595 mutex_lock(&tree->mutex);
596 list_for_each_prev(cur, &ordered->list) {
597 ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Mason3edf7d32008-07-18 06:17:13 -0400598 if (offset >= ordered_sum->file_offset) {
599 num_sectors = ordered_sum->len / sectorsize;
Chris Masoned98b562008-07-22 23:06:42 -0400600 sector_sums = ordered_sum->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400601 for (i = 0; i < num_sectors; i++) {
602 if (sector_sums[i].offset == offset) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400603 *sum = sector_sums[i].sum;
604 ret = 0;
605 goto out;
606 }
607 }
Chris Masonba1da2f2008-07-17 12:54:15 -0400608 }
609 }
610out:
611 mutex_unlock(&tree->mutex);
Chris Mason89642222008-07-24 09:41:53 -0400612 btrfs_put_ordered_extent(ordered);
Chris Masonba1da2f2008-07-17 12:54:15 -0400613 return ret;
614}
615
Chris Masonf4219502008-07-22 11:18:09 -0400616
617/**
618 * taken from mm/filemap.c because it isn't exported
619 *
620 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
621 * @mapping: address space structure to write
622 * @start: offset in bytes where the range starts
623 * @end: offset in bytes where the range ends (inclusive)
624 * @sync_mode: enable synchronous operation
625 *
626 * Start writeback against all of a mapping's dirty pages that lie
627 * within the byte offsets <start, end> inclusive.
628 *
629 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
630 * opposed to a regular memory cleansing writeback. The difference between
631 * these two operations is that if a dirty page/buffer is encountered, it must
632 * be waited upon, and not just skipped over.
633 */
634int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start,
635 loff_t end, int sync_mode)
636{
637 struct writeback_control wbc = {
638 .sync_mode = sync_mode,
639 .nr_to_write = mapping->nrpages * 2,
640 .range_start = start,
641 .range_end = end,
642 .for_writepages = 1,
643 };
644 return btrfs_writepages(mapping, &wbc);
645}
646
647/**
648 * taken from mm/filemap.c because it isn't exported
649 *
650 * wait_on_page_writeback_range - wait for writeback to complete
651 * @mapping: target address_space
652 * @start: beginning page index
653 * @end: ending page index
654 *
655 * Wait for writeback to complete against pages indexed by start->end
656 * inclusive
657 */
658int btrfs_wait_on_page_writeback_range(struct address_space *mapping,
659 pgoff_t start, pgoff_t end)
660{
661 struct pagevec pvec;
662 int nr_pages;
663 int ret = 0;
664 pgoff_t index;
665
666 if (end < start)
667 return 0;
668
669 pagevec_init(&pvec, 0);
670 index = start;
671 while ((index <= end) &&
672 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
673 PAGECACHE_TAG_WRITEBACK,
674 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
675 unsigned i;
676
677 for (i = 0; i < nr_pages; i++) {
678 struct page *page = pvec.pages[i];
679
680 /* until radix tree lookup accepts end_index */
681 if (page->index > end)
682 continue;
683
684 wait_on_page_writeback(page);
685 if (PageError(page))
686 ret = -EIO;
687 }
688 pagevec_release(&pvec);
689 cond_resched();
690 }
691
692 /* Check for outstanding write errors */
693 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
694 ret = -ENOSPC;
695 if (test_and_clear_bit(AS_EIO, &mapping->flags))
696 ret = -EIO;
697
698 return ret;
699}