blob: b739e3abebb929e01d3a7941156c20e2175b7417 [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 Masondc17ff82008-01-08 15:46:30 -050022#include "ctree.h"
23#include "transaction.h"
24#include "btrfs_inode.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040025#include "extent_io.h"
Chris Masondc17ff82008-01-08 15:46:30 -050026
Chris Masondc17ff82008-01-08 15:46:30 -050027
Chris Masone6dcd2d2008-07-17 12:53:50 -040028static u64 entry_end(struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -050029{
Chris Masone6dcd2d2008-07-17 12:53:50 -040030 if (entry->file_offset + entry->len < entry->file_offset)
31 return (u64)-1;
32 return entry->file_offset + entry->len;
Chris Masondc17ff82008-01-08 15:46:30 -050033}
34
Chris Masone6dcd2d2008-07-17 12:53:50 -040035static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
36 struct rb_node *node)
Chris Masondc17ff82008-01-08 15:46:30 -050037{
38 struct rb_node ** p = &root->rb_node;
39 struct rb_node * parent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040040 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -050041
42 while(*p) {
43 parent = *p;
Chris Masone6dcd2d2008-07-17 12:53:50 -040044 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050045
Chris Masone6dcd2d2008-07-17 12:53:50 -040046 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050047 p = &(*p)->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040048 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050049 p = &(*p)->rb_right;
50 else
51 return parent;
52 }
53
54 rb_link_node(node, parent, p);
55 rb_insert_color(node, root);
56 return NULL;
57}
58
Chris Masone6dcd2d2008-07-17 12:53:50 -040059static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
60 struct rb_node **prev_ret)
Chris Masondc17ff82008-01-08 15:46:30 -050061{
62 struct rb_node * n = root->rb_node;
63 struct rb_node *prev = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040064 struct rb_node *test;
65 struct btrfs_ordered_extent *entry;
66 struct btrfs_ordered_extent *prev_entry = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -050067
68 while(n) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040069 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050070 prev = n;
71 prev_entry = entry;
Chris Masondc17ff82008-01-08 15:46:30 -050072
Chris Masone6dcd2d2008-07-17 12:53:50 -040073 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050074 n = n->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040075 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050076 n = n->rb_right;
77 else
78 return n;
79 }
80 if (!prev_ret)
81 return NULL;
82
Chris Masone6dcd2d2008-07-17 12:53:50 -040083 while(prev && file_offset >= entry_end(prev_entry)) {
84 test = rb_next(prev);
85 if (!test)
86 break;
87 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
88 rb_node);
89 if (file_offset < entry_end(prev_entry))
90 break;
91
92 prev = test;
93 }
94 if (prev)
95 prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
96 rb_node);
97 while(prev && file_offset < entry_end(prev_entry)) {
98 test = rb_prev(prev);
99 if (!test)
100 break;
101 prev_entry = rb_entry(test, struct btrfs_ordered_extent,
102 rb_node);
103 prev = test;
Chris Masondc17ff82008-01-08 15:46:30 -0500104 }
105 *prev_ret = prev;
106 return NULL;
107}
108
Chris Masone6dcd2d2008-07-17 12:53:50 -0400109static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -0500110{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400111 if (file_offset < entry->file_offset ||
112 entry->file_offset + entry->len <= file_offset)
113 return 0;
114 return 1;
115}
116
117static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
118 u64 file_offset)
119{
120 struct rb_root *root = &tree->tree;
Chris Masondc17ff82008-01-08 15:46:30 -0500121 struct rb_node *prev;
122 struct rb_node *ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400123 struct btrfs_ordered_extent *entry;
124
125 if (tree->last) {
126 entry = rb_entry(tree->last, struct btrfs_ordered_extent,
127 rb_node);
128 if (offset_in_entry(entry, file_offset))
129 return tree->last;
130 }
131 ret = __tree_search(root, file_offset, &prev);
Chris Masondc17ff82008-01-08 15:46:30 -0500132 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400133 ret = prev;
134 if (ret)
135 tree->last = ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500136 return ret;
137}
138
Chris Masone6dcd2d2008-07-17 12:53:50 -0400139int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
140 u64 start, u64 len)
Chris Masondc17ff82008-01-08 15:46:30 -0500141{
Chris Masondc17ff82008-01-08 15:46:30 -0500142 struct btrfs_ordered_inode_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400143 struct rb_node *node;
144 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -0500145
Chris Masone6dcd2d2008-07-17 12:53:50 -0400146 tree = &BTRFS_I(inode)->ordered_tree;
147 entry = kzalloc(sizeof(*entry), GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500148 if (!entry)
149 return -ENOMEM;
150
Chris Masone6dcd2d2008-07-17 12:53:50 -0400151 mutex_lock(&tree->mutex);
152 entry->file_offset = file_offset;
153 entry->start = start;
154 entry->len = len;
Chris Mason4d5e74b2008-01-16 16:09:22 -0500155 entry->inode = inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400156 /* one ref for the tree */
157 atomic_set(&entry->refs, 1);
158 init_waitqueue_head(&entry->wait);
159 INIT_LIST_HEAD(&entry->list);
Chris Masondc17ff82008-01-08 15:46:30 -0500160
Chris Masone6dcd2d2008-07-17 12:53:50 -0400161 node = tree_insert(&tree->tree, file_offset,
162 &entry->rb_node);
163 if (node) {
164 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
165 atomic_inc(&entry->refs);
166 }
167 set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset,
168 entry_end(entry) - 1, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500169
Chris Masone6dcd2d2008-07-17 12:53:50 -0400170 set_bit(BTRFS_ORDERED_START, &entry->flags);
171 mutex_unlock(&tree->mutex);
172 BUG_ON(node);
173 return 0;
174}
Chris Masondc17ff82008-01-08 15:46:30 -0500175
Chris Masone6dcd2d2008-07-17 12:53:50 -0400176int btrfs_add_ordered_sum(struct inode *inode, struct btrfs_ordered_sum *sum)
177{
178 struct btrfs_ordered_inode_tree *tree;
179 struct rb_node *node;
180 struct btrfs_ordered_extent *entry;
Chris Mason1b1e2132008-06-25 16:01:31 -0400181
Chris Masone6dcd2d2008-07-17 12:53:50 -0400182 tree = &BTRFS_I(inode)->ordered_tree;
183 mutex_lock(&tree->mutex);
184 node = tree_search(tree, sum->file_offset);
185 if (!node) {
186search_fail:
187printk("add ordered sum failed to find a node for inode %lu offset %Lu\n", inode->i_ino, sum->file_offset);
188 node = rb_first(&tree->tree);
189 while(node) {
190 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
191 printk("entry %Lu %Lu %Lu\n", entry->file_offset, entry->file_offset + entry->len, entry->start);
192 node = rb_next(node);
193 }
194 BUG();
195 }
196 BUG_ON(!node);
197
198 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
199 if (!offset_in_entry(entry, sum->file_offset)) {
200 goto search_fail;
201 }
202
203 list_add_tail(&sum->list, &entry->list);
204 mutex_unlock(&tree->mutex);
205 return 0;
206}
207
208int btrfs_dec_test_ordered_pending(struct inode *inode,
209 u64 file_offset, u64 io_size)
210{
211 struct btrfs_ordered_inode_tree *tree;
212 struct rb_node *node;
213 struct btrfs_ordered_extent *entry;
214 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
215 int ret;
216
217 tree = &BTRFS_I(inode)->ordered_tree;
218 mutex_lock(&tree->mutex);
219 clear_extent_ordered(io_tree, file_offset, file_offset + io_size - 1,
220 GFP_NOFS);
221 node = tree_search(tree, file_offset);
222 if (!node) {
223 ret = 1;
224 goto out;
225 }
226
227 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
228 if (!offset_in_entry(entry, file_offset)) {
229 ret = 1;
230 goto out;
231 }
232
233 ret = test_range_bit(io_tree, entry->file_offset,
234 entry->file_offset + entry->len - 1,
235 EXTENT_ORDERED, 0);
236 if (!test_bit(BTRFS_ORDERED_START, &entry->flags)) {
237printk("inode %lu not ready yet for extent %Lu %Lu\n", inode->i_ino, entry->file_offset, entry_end(entry));
238 }
239 if (ret == 0)
240 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
241out:
242 mutex_unlock(&tree->mutex);
243 return ret == 0;
244}
245
246int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
247{
Chris Masonba1da2f2008-07-17 12:54:15 -0400248 struct list_head *cur;
249 struct btrfs_ordered_sum *sum;
250
251 if (atomic_dec_and_test(&entry->refs)) {
252 while(!list_empty(&entry->list)) {
253 cur = entry->list.next;
254 sum = list_entry(cur, struct btrfs_ordered_sum, list);
255 list_del(&sum->list);
256 kfree(sum);
257 }
Chris Masondc17ff82008-01-08 15:46:30 -0500258 kfree(entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400259 }
Chris Masondc17ff82008-01-08 15:46:30 -0500260 return 0;
261}
262
Chris Masone6dcd2d2008-07-17 12:53:50 -0400263int btrfs_remove_ordered_extent(struct inode *inode,
264 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500265{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400266 struct btrfs_ordered_inode_tree *tree;
Chris Masondc17ff82008-01-08 15:46:30 -0500267 struct rb_node *node;
268
Chris Masone6dcd2d2008-07-17 12:53:50 -0400269 tree = &BTRFS_I(inode)->ordered_tree;
270 mutex_lock(&tree->mutex);
271 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500272 rb_erase(node, &tree->tree);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400273 tree->last = NULL;
274 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
275 mutex_unlock(&tree->mutex);
276 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400277 return 0;
278}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400279
280void btrfs_wait_ordered_extent(struct inode *inode,
281 struct btrfs_ordered_extent *entry)
282{
283 u64 start = entry->file_offset;
284 u64 end = start + entry->len - 1;
285#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
286 do_sync_file_range(file, start, end, SYNC_FILE_RANGE_WRITE);
287#else
288 do_sync_mapping_range(inode->i_mapping, start, end,
289 SYNC_FILE_RANGE_WRITE);
290#endif
291 wait_event(entry->wait,
292 test_bit(BTRFS_ORDERED_COMPLETE, &entry->flags));
293}
294
295static void btrfs_start_ordered_extent(struct inode *inode,
296 struct btrfs_ordered_extent *entry, int wait)
297{
298 u64 start = entry->file_offset;
299 u64 end = start + entry->len - 1;
300
301#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
302 do_sync_file_range(file, start, end, SYNC_FILE_RANGE_WRITE);
303#else
304 do_sync_mapping_range(inode->i_mapping, start, end,
305 SYNC_FILE_RANGE_WRITE);
306#endif
307 if (wait)
308 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
309 &entry->flags));
310}
311
312void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
313{
314 u64 end;
315 struct btrfs_ordered_extent *ordered;
316 int found;
317 int should_wait = 0;
318
319again:
320 if (start + len < start)
321 end = (u64)-1;
322 else
323 end = start + len - 1;
324 found = 0;
325 while(1) {
326 ordered = btrfs_lookup_first_ordered_extent(inode, end);
327 if (!ordered) {
328 break;
329 }
330 if (ordered->file_offset >= start + len) {
331 btrfs_put_ordered_extent(ordered);
332 break;
333 }
334 if (ordered->file_offset + ordered->len < start) {
335 btrfs_put_ordered_extent(ordered);
336 break;
337 }
338 btrfs_start_ordered_extent(inode, ordered, should_wait);
339 found++;
340 end = ordered->file_offset;
341 btrfs_put_ordered_extent(ordered);
342 if (end == 0)
343 break;
344 end--;
345 }
346 if (should_wait && found) {
347 should_wait = 0;
348 goto again;
349 }
350}
351
352int btrfs_add_ordered_pending(struct inode *inode,
353 struct btrfs_ordered_extent *ordered,
354 u64 start, u64 len)
355{
356 WARN_ON(1);
357 return 0;
358#if 0
359 int ret;
360 struct btrfs_ordered_inode_tree *tree;
361 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
362
363 tree = &BTRFS_I(inode)->ordered_tree;
364 mutex_lock(&tree->mutex);
365 if (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags)) {
366 ret = -EAGAIN;
367 goto out;
368 }
369 set_extent_ordered(io_tree, start, start + len - 1, GFP_NOFS);
370 ret = 0;
371out:
372 mutex_unlock(&tree->mutex);
373 return ret;
374#endif
375}
376
377struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
378 u64 file_offset)
379{
380 struct btrfs_ordered_inode_tree *tree;
381 struct rb_node *node;
382 struct btrfs_ordered_extent *entry = NULL;
383
384 tree = &BTRFS_I(inode)->ordered_tree;
385 mutex_lock(&tree->mutex);
386 node = tree_search(tree, file_offset);
387 if (!node)
388 goto out;
389
390 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
391 if (!offset_in_entry(entry, file_offset))
392 entry = NULL;
393 if (entry)
394 atomic_inc(&entry->refs);
395out:
396 mutex_unlock(&tree->mutex);
397 return entry;
398}
399
400struct btrfs_ordered_extent *
401btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset)
402{
403 struct btrfs_ordered_inode_tree *tree;
404 struct rb_node *node;
405 struct btrfs_ordered_extent *entry = NULL;
406
407 tree = &BTRFS_I(inode)->ordered_tree;
408 mutex_lock(&tree->mutex);
409 node = tree_search(tree, file_offset);
410 if (!node)
411 goto out;
412
413 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
414 atomic_inc(&entry->refs);
415out:
416 mutex_unlock(&tree->mutex);
417 return entry;
418}
Chris Masondbe674a2008-07-17 12:54:05 -0400419
420int btrfs_ordered_update_i_size(struct inode *inode,
421 struct btrfs_ordered_extent *ordered)
422{
423 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
424 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
425 u64 disk_i_size;
426 u64 new_i_size;
427 u64 i_size_test;
428 struct rb_node *node;
429 struct btrfs_ordered_extent *test;
430
431 mutex_lock(&tree->mutex);
432 disk_i_size = BTRFS_I(inode)->disk_i_size;
433
434 /*
435 * if the disk i_size is already at the inode->i_size, or
436 * this ordered extent is inside the disk i_size, we're done
437 */
438 if (disk_i_size >= inode->i_size ||
439 ordered->file_offset + ordered->len <= disk_i_size) {
440 goto out;
441 }
442
443 /*
444 * we can't update the disk_isize if there are delalloc bytes
445 * between disk_i_size and this ordered extent
446 */
447 if (test_range_bit(io_tree, disk_i_size,
448 ordered->file_offset + ordered->len - 1,
449 EXTENT_DELALLOC, 0)) {
450 goto out;
451 }
452 /*
453 * walk backward from this ordered extent to disk_i_size.
454 * if we find an ordered extent then we can't update disk i_size
455 * yet
456 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400457 node = &ordered->rb_node;
Chris Masondbe674a2008-07-17 12:54:05 -0400458 while(1) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400459 node = rb_prev(node);
Chris Masondbe674a2008-07-17 12:54:05 -0400460 if (!node)
461 break;
462 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
463 if (test->file_offset + test->len <= disk_i_size)
464 break;
465 if (test->file_offset >= inode->i_size)
466 break;
467 if (test->file_offset >= disk_i_size)
468 goto out;
469 }
470 new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode));
471
472 /*
473 * at this point, we know we can safely update i_size to at least
474 * the offset from this ordered extent. But, we need to
475 * walk forward and see if ios from higher up in the file have
476 * finished.
477 */
478 node = rb_next(&ordered->rb_node);
479 i_size_test = 0;
480 if (node) {
481 /*
482 * do we have an area where IO might have finished
483 * between our ordered extent and the next one.
484 */
485 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
486 if (test->file_offset > entry_end(ordered)) {
487 i_size_test = test->file_offset - 1;
488 }
489 } else {
490 i_size_test = i_size_read(inode);
491 }
492
493 /*
494 * i_size_test is the end of a region after this ordered
495 * extent where there are no ordered extents. As long as there
496 * are no delalloc bytes in this area, it is safe to update
497 * disk_i_size to the end of the region.
498 */
499 if (i_size_test > entry_end(ordered) &&
500 !test_range_bit(io_tree, entry_end(ordered), i_size_test,
501 EXTENT_DELALLOC, 0)) {
502 new_i_size = min_t(u64, i_size_test, i_size_read(inode));
503 }
504 BTRFS_I(inode)->disk_i_size = new_i_size;
505out:
506 mutex_unlock(&tree->mutex);
507 return 0;
508}
Chris Masonba1da2f2008-07-17 12:54:15 -0400509
510int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
511{
512 struct btrfs_ordered_sum *ordered_sum;
513 struct btrfs_sector_sum *sector_sums;
514 struct btrfs_ordered_extent *ordered;
515 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
516 struct list_head *cur;
517 int ret = 1;
518 int index;
519
520 ordered = btrfs_lookup_ordered_extent(inode, offset);
521 if (!ordered)
522 return 1;
523
524 mutex_lock(&tree->mutex);
525 list_for_each_prev(cur, &ordered->list) {
526 ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list);
527 if (offset >= ordered_sum->file_offset &&
528 offset < ordered_sum->file_offset + ordered_sum->len) {
529 index = (offset - ordered_sum->file_offset) /
530 BTRFS_I(inode)->root->sectorsize;;
531 sector_sums = &ordered_sum->sums;
532 *sum = sector_sums[index].sum;
533 ret = 0;
534 goto out;
535 }
536 }
537out:
538 mutex_unlock(&tree->mutex);
539 return ret;
540}
541