blob: fc6840b53d9d0f70bdefe376cabc95e521e3e320 [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
Chris Masondc17ff82008-01-08 15:46:30 -050019#include <linux/slab.h>
Chris Masond6bfde82008-04-30 13:59:35 -040020#include <linux/blkdev.h>
Chris Masonf4219502008-07-22 11:18:09 -040021#include <linux/writeback.h>
22#include <linux/pagevec.h>
Chris Masondc17ff82008-01-08 15:46:30 -050023#include "ctree.h"
24#include "transaction.h"
25#include "btrfs_inode.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040026#include "extent_io.h"
Chris Masondc17ff82008-01-08 15:46:30 -050027
Miao Xie6352b912012-09-06 04:01:51 -060028static struct kmem_cache *btrfs_ordered_extent_cache;
29
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 Masond352ac62008-09-29 15:18:18 -040037/* returns NULL if the insertion worked, or it returns the node it did find
38 * in the tree
39 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040040static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
41 struct rb_node *node)
Chris Masondc17ff82008-01-08 15:46:30 -050042{
Chris Masond3977122009-01-05 21:25:51 -050043 struct rb_node **p = &root->rb_node;
44 struct rb_node *parent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040045 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -050046
Chris Masond3977122009-01-05 21:25:51 -050047 while (*p) {
Chris Masondc17ff82008-01-08 15:46:30 -050048 parent = *p;
Chris Masone6dcd2d2008-07-17 12:53:50 -040049 entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050050
Chris Masone6dcd2d2008-07-17 12:53:50 -040051 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050052 p = &(*p)->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040053 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050054 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
Jeff Mahoney43c04fb2011-10-03 23:22:33 -040064static 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
Chris Masond352ac62008-09-29 15:18:18 -040072/*
73 * look for a given offset in the tree, and if it can't be found return the
74 * first lesser offset
75 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040076static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
77 struct rb_node **prev_ret)
Chris Masondc17ff82008-01-08 15:46:30 -050078{
Chris Masond3977122009-01-05 21:25:51 -050079 struct rb_node *n = root->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -050080 struct rb_node *prev = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -040081 struct rb_node *test;
82 struct btrfs_ordered_extent *entry;
83 struct btrfs_ordered_extent *prev_entry = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -050084
Chris Masond3977122009-01-05 21:25:51 -050085 while (n) {
Chris Masone6dcd2d2008-07-17 12:53:50 -040086 entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
Chris Masondc17ff82008-01-08 15:46:30 -050087 prev = n;
88 prev_entry = entry;
Chris Masondc17ff82008-01-08 15:46:30 -050089
Chris Masone6dcd2d2008-07-17 12:53:50 -040090 if (file_offset < entry->file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -050091 n = n->rb_left;
Chris Masone6dcd2d2008-07-17 12:53:50 -040092 else if (file_offset >= entry_end(entry))
Chris Masondc17ff82008-01-08 15:46:30 -050093 n = n->rb_right;
94 else
95 return n;
96 }
97 if (!prev_ret)
98 return NULL;
99
Chris Masond3977122009-01-05 21:25:51 -0500100 while (prev && file_offset >= entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400101 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);
Chris Masond3977122009-01-05 21:25:51 -0500114 while (prev && file_offset < entry_end(prev_entry)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400115 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;
Chris Masondc17ff82008-01-08 15:46:30 -0500121 }
122 *prev_ret = prev;
123 return NULL;
124}
125
Chris Masond352ac62008-09-29 15:18:18 -0400126/*
127 * helper to check if a given offset is inside a given entry
128 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400129static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
Chris Masondc17ff82008-01-08 15:46:30 -0500130{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400131 if (file_offset < entry->file_offset ||
132 entry->file_offset + entry->len <= file_offset)
133 return 0;
134 return 1;
135}
136
Josef Bacik4b46fce2010-05-23 11:00:55 -0400137static 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
Chris Masond352ac62008-09-29 15:18:18 -0400146/*
147 * look find the first ordered struct that has this offset, otherwise
148 * the first one less than this offset
149 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400150static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
151 u64 file_offset)
152{
153 struct rb_root *root = &tree->tree;
Chris Masonc87fb6f2011-01-31 19:54:59 -0500154 struct rb_node *prev = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -0500155 struct rb_node *ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400156 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);
Chris Masondc17ff82008-01-08 15:46:30 -0500165 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166 ret = prev;
167 if (ret)
168 tree->last = ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500169 return ret;
170}
171
Chris Masoneb84ae02008-07-17 13:53:27 -0400172/* 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 *
Chris Masoneb84ae02008-07-17 13:53:27 -0400180 * The tree is given a single reference on the ordered extent that was
181 * inserted.
182 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400183static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
184 u64 start, u64 len, u64 disk_len,
Li Zefan261507a02010-12-17 14:21:50 +0800185 int type, int dio, int compress_type)
Chris Masondc17ff82008-01-08 15:46:30 -0500186{
Chris Masondc17ff82008-01-08 15:46:30 -0500187 struct btrfs_ordered_inode_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400188 struct rb_node *node;
189 struct btrfs_ordered_extent *entry;
Chris Masondc17ff82008-01-08 15:46:30 -0500190
Chris Masone6dcd2d2008-07-17 12:53:50 -0400191 tree = &BTRFS_I(inode)->ordered_tree;
Miao Xie6352b912012-09-06 04:01:51 -0600192 entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
Chris Masondc17ff82008-01-08 15:46:30 -0500193 if (!entry)
194 return -ENOMEM;
195
Chris Masone6dcd2d2008-07-17 12:53:50 -0400196 entry->file_offset = file_offset;
197 entry->start = start;
198 entry->len = len;
Chris Masonc8b97812008-10-29 14:49:59 -0400199 entry->disk_len = disk_len;
Chris Mason8b62b722009-09-02 16:53:46 -0400200 entry->bytes_left = len;
Josef Bacik5fd02042012-05-02 14:00:54 -0400201 entry->inode = igrab(inode);
Li Zefan261507a02010-12-17 14:21:50 +0800202 entry->compress_type = compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -0400203 if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
Yan Zheng80ff3852008-10-30 14:20:02 -0400204 set_bit(type, &entry->flags);
Chris Mason3eaa2882008-07-24 11:57:52 -0400205
Josef Bacik4b46fce2010-05-23 11:00:55 -0400206 if (dio)
207 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
208
Chris Masone6dcd2d2008-07-17 12:53:50 -0400209 /* one ref for the tree */
210 atomic_set(&entry->refs, 1);
211 init_waitqueue_head(&entry->wait);
212 INIT_LIST_HEAD(&entry->list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400213 INIT_LIST_HEAD(&entry->root_extent_list);
Miao Xie9afab882012-10-25 09:41:36 +0000214 INIT_LIST_HEAD(&entry->work_list);
215 init_completion(&entry->completion);
Chris Masondc17ff82008-01-08 15:46:30 -0500216
liubo1abe9b82011-03-24 11:18:59 +0000217 trace_btrfs_ordered_extent_add(inode, entry);
218
Josef Bacik5fd02042012-05-02 14:00:54 -0400219 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400220 node = tree_insert(&tree->tree, file_offset,
221 &entry->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400222 if (node)
223 ordered_data_tree_panic(inode, -EEXIST, file_offset);
Josef Bacik5fd02042012-05-02 14:00:54 -0400224 spin_unlock_irq(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500225
Chris Mason3eaa2882008-07-24 11:57:52 -0400226 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
227 list_add_tail(&entry->root_extent_list,
228 &BTRFS_I(inode)->root->fs_info->ordered_extents);
229 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
230
Chris Masone6dcd2d2008-07-17 12:53:50 -0400231 return 0;
232}
Chris Masondc17ff82008-01-08 15:46:30 -0500233
Josef Bacik4b46fce2010-05-23 11:00:55 -0400234int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
235 u64 start, u64 len, u64 disk_len, int type)
236{
237 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
Li Zefan261507a02010-12-17 14:21:50 +0800238 disk_len, type, 0,
239 BTRFS_COMPRESS_NONE);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400240}
241
242int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
243 u64 start, u64 len, u64 disk_len, int type)
244{
245 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
Li Zefan261507a02010-12-17 14:21:50 +0800246 disk_len, type, 1,
247 BTRFS_COMPRESS_NONE);
248}
249
250int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
251 u64 start, u64 len, u64 disk_len,
252 int type, int compress_type)
253{
254 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
255 disk_len, type, 0,
256 compress_type);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400257}
258
Chris Masoneb84ae02008-07-17 13:53:27 -0400259/*
260 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400261 * when an ordered extent is finished. If the list covers more than one
262 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400263 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100264void btrfs_add_ordered_sum(struct inode *inode,
265 struct btrfs_ordered_extent *entry,
266 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400267{
268 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400269
Chris Masone6dcd2d2008-07-17 12:53:50 -0400270 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400271 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400272 list_add_tail(&sum->list, &entry->list);
Josef Bacik5fd02042012-05-02 14:00:54 -0400273 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400274}
275
Chris Masoneb84ae02008-07-17 13:53:27 -0400276/*
277 * this is used to account for finished IO across a given range
Chris Mason163cf092010-11-28 19:56:33 -0500278 * of the file. The IO may span ordered extents. If
279 * a given ordered_extent is completely done, 1 is returned, otherwise
280 * 0.
281 *
282 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
283 * to make sure this function only returns 1 once for a given ordered extent.
284 *
285 * file_offset is updated to one byte past the range that is recorded as
286 * complete. This allows you to walk forward in the file.
287 */
288int btrfs_dec_test_first_ordered_pending(struct inode *inode,
289 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400290 u64 *file_offset, u64 io_size, int uptodate)
Chris Mason163cf092010-11-28 19:56:33 -0500291{
292 struct btrfs_ordered_inode_tree *tree;
293 struct rb_node *node;
294 struct btrfs_ordered_extent *entry = NULL;
295 int ret;
Josef Bacik5fd02042012-05-02 14:00:54 -0400296 unsigned long flags;
Chris Mason163cf092010-11-28 19:56:33 -0500297 u64 dec_end;
298 u64 dec_start;
299 u64 to_dec;
300
301 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400302 spin_lock_irqsave(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500303 node = tree_search(tree, *file_offset);
304 if (!node) {
305 ret = 1;
306 goto out;
307 }
308
309 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
310 if (!offset_in_entry(entry, *file_offset)) {
311 ret = 1;
312 goto out;
313 }
314
315 dec_start = max(*file_offset, entry->file_offset);
316 dec_end = min(*file_offset + io_size, entry->file_offset +
317 entry->len);
318 *file_offset = dec_end;
319 if (dec_start > dec_end) {
320 printk(KERN_CRIT "bad ordering dec_start %llu end %llu\n",
321 (unsigned long long)dec_start,
322 (unsigned long long)dec_end);
323 }
324 to_dec = dec_end - dec_start;
325 if (to_dec > entry->bytes_left) {
326 printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
327 (unsigned long long)entry->bytes_left,
328 (unsigned long long)to_dec);
329 }
330 entry->bytes_left -= to_dec;
Josef Bacik5fd02042012-05-02 14:00:54 -0400331 if (!uptodate)
332 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
333
Chris Mason163cf092010-11-28 19:56:33 -0500334 if (entry->bytes_left == 0)
335 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
336 else
337 ret = 1;
338out:
339 if (!ret && cached && entry) {
340 *cached = entry;
341 atomic_inc(&entry->refs);
342 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400343 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500344 return ret == 0;
345}
346
347/*
348 * this is used to account for finished IO across a given range
Chris Masoneb84ae02008-07-17 13:53:27 -0400349 * of the file. The IO should not span ordered extents. If
350 * a given ordered_extent is completely done, 1 is returned, otherwise
351 * 0.
352 *
353 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
354 * to make sure this function only returns 1 once for a given ordered extent.
355 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400356int btrfs_dec_test_ordered_pending(struct inode *inode,
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000357 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400358 u64 file_offset, u64 io_size, int uptodate)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400359{
360 struct btrfs_ordered_inode_tree *tree;
361 struct rb_node *node;
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000362 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400363 unsigned long flags;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400364 int ret;
365
366 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400367 spin_lock_irqsave(&tree->lock, flags);
368 if (cached && *cached) {
369 entry = *cached;
370 goto have_entry;
371 }
372
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373 node = tree_search(tree, file_offset);
374 if (!node) {
375 ret = 1;
376 goto out;
377 }
378
379 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400380have_entry:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400381 if (!offset_in_entry(entry, file_offset)) {
382 ret = 1;
383 goto out;
384 }
385
Chris Mason8b62b722009-09-02 16:53:46 -0400386 if (io_size > entry->bytes_left) {
387 printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
388 (unsigned long long)entry->bytes_left,
389 (unsigned long long)io_size);
390 }
391 entry->bytes_left -= io_size;
Josef Bacik5fd02042012-05-02 14:00:54 -0400392 if (!uptodate)
393 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
394
Chris Mason8b62b722009-09-02 16:53:46 -0400395 if (entry->bytes_left == 0)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
Chris Mason8b62b722009-09-02 16:53:46 -0400397 else
398 ret = 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400399out:
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000400 if (!ret && cached && entry) {
401 *cached = entry;
402 atomic_inc(&entry->refs);
403 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400404 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 return ret == 0;
406}
407
Chris Masoneb84ae02008-07-17 13:53:27 -0400408/*
409 * used to drop a reference on an ordered extent. This will free
410 * the extent if the last reference is dropped
411 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100412void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400413{
Chris Masonba1da2f2008-07-17 12:54:15 -0400414 struct list_head *cur;
415 struct btrfs_ordered_sum *sum;
416
liubo1abe9b82011-03-24 11:18:59 +0000417 trace_btrfs_ordered_extent_put(entry->inode, entry);
418
Chris Masonba1da2f2008-07-17 12:54:15 -0400419 if (atomic_dec_and_test(&entry->refs)) {
Josef Bacik5fd02042012-05-02 14:00:54 -0400420 if (entry->inode)
421 btrfs_add_delayed_iput(entry->inode);
Chris Masond3977122009-01-05 21:25:51 -0500422 while (!list_empty(&entry->list)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400423 cur = entry->list.next;
424 sum = list_entry(cur, struct btrfs_ordered_sum, list);
425 list_del(&sum->list);
426 kfree(sum);
427 }
Miao Xie6352b912012-09-06 04:01:51 -0600428 kmem_cache_free(btrfs_ordered_extent_cache, entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400429 }
Chris Masondc17ff82008-01-08 15:46:30 -0500430}
431
Chris Masoneb84ae02008-07-17 13:53:27 -0400432/*
433 * remove an ordered extent from the tree. No references are dropped
Josef Bacik5fd02042012-05-02 14:00:54 -0400434 * and waiters are woken up.
Chris Masoneb84ae02008-07-17 13:53:27 -0400435 */
Josef Bacik5fd02042012-05-02 14:00:54 -0400436void btrfs_remove_ordered_extent(struct inode *inode,
437 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500438{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400439 struct btrfs_ordered_inode_tree *tree;
Josef Bacik287a0ab2010-03-19 18:07:23 +0000440 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondc17ff82008-01-08 15:46:30 -0500441 struct rb_node *node;
442
Chris Masone6dcd2d2008-07-17 12:53:50 -0400443 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400444 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400445 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500446 rb_erase(node, &tree->tree);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400447 tree->last = NULL;
448 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Josef Bacik5fd02042012-05-02 14:00:54 -0400449 spin_unlock_irq(&tree->lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400450
Josef Bacik287a0ab2010-03-19 18:07:23 +0000451 spin_lock(&root->fs_info->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400452 list_del_init(&entry->root_extent_list);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400453
liubo1abe9b82011-03-24 11:18:59 +0000454 trace_btrfs_ordered_extent_remove(inode, entry);
455
Chris Mason5a3f23d2009-03-31 13:27:11 -0400456 /*
457 * we have no more ordered extents for this inode and
458 * no dirty pages. We can safely remove it from the
459 * list of ordered extents
460 */
461 if (RB_EMPTY_ROOT(&tree->tree) &&
462 !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
463 list_del_init(&BTRFS_I(inode)->ordered_operations);
464 }
Josef Bacik287a0ab2010-03-19 18:07:23 +0000465 spin_unlock(&root->fs_info->ordered_extent_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400466 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400467}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400468
Miao Xie9afab882012-10-25 09:41:36 +0000469static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
470{
471 struct btrfs_ordered_extent *ordered;
472
473 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
474 btrfs_start_ordered_extent(ordered->inode, ordered, 1);
475 complete(&ordered->completion);
476}
477
Chris Masond352ac62008-09-29 15:18:18 -0400478/*
479 * wait for all the ordered extents in a root. This is done when balancing
480 * space between drives.
481 */
Liu Bo6bbe3a92012-09-14 02:58:07 -0600482void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput)
Chris Mason3eaa2882008-07-24 11:57:52 -0400483{
Miao Xie9afab882012-10-25 09:41:36 +0000484 struct list_head splice, works;
Chris Mason3eaa2882008-07-24 11:57:52 -0400485 struct list_head *cur;
Miao Xie9afab882012-10-25 09:41:36 +0000486 struct btrfs_ordered_extent *ordered, *next;
Chris Mason3eaa2882008-07-24 11:57:52 -0400487 struct inode *inode;
488
489 INIT_LIST_HEAD(&splice);
Miao Xie9afab882012-10-25 09:41:36 +0000490 INIT_LIST_HEAD(&works);
Chris Mason3eaa2882008-07-24 11:57:52 -0400491
492 spin_lock(&root->fs_info->ordered_extent_lock);
493 list_splice_init(&root->fs_info->ordered_extents, &splice);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400494 while (!list_empty(&splice)) {
Chris Mason3eaa2882008-07-24 11:57:52 -0400495 cur = splice.next;
496 ordered = list_entry(cur, struct btrfs_ordered_extent,
497 root_extent_list);
498 list_del_init(&ordered->root_extent_list);
499 atomic_inc(&ordered->refs);
Chris Mason3eaa2882008-07-24 11:57:52 -0400500
501 /*
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400502 * the inode may be getting freed (in sys_unlink path).
Chris Mason3eaa2882008-07-24 11:57:52 -0400503 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400504 inode = igrab(ordered->inode);
505
Chris Mason3eaa2882008-07-24 11:57:52 -0400506 spin_unlock(&root->fs_info->ordered_extent_lock);
507
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400508 if (inode) {
Miao Xie9afab882012-10-25 09:41:36 +0000509 ordered->flush_work.func = btrfs_run_ordered_extent_work;
510 list_add_tail(&ordered->work_list, &works);
511 btrfs_queue_worker(&root->fs_info->flush_workers,
512 &ordered->flush_work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400513 } else {
514 btrfs_put_ordered_extent(ordered);
515 }
Chris Mason3eaa2882008-07-24 11:57:52 -0400516
Miao Xie9afab882012-10-25 09:41:36 +0000517 cond_resched();
Chris Mason3eaa2882008-07-24 11:57:52 -0400518 spin_lock(&root->fs_info->ordered_extent_lock);
519 }
520 spin_unlock(&root->fs_info->ordered_extent_lock);
Miao Xie9afab882012-10-25 09:41:36 +0000521
522 list_for_each_entry_safe(ordered, next, &works, work_list) {
523 list_del_init(&ordered->work_list);
524 wait_for_completion(&ordered->completion);
525
526 inode = ordered->inode;
527 btrfs_put_ordered_extent(ordered);
528 if (delay_iput)
529 btrfs_add_delayed_iput(inode);
530 else
531 iput(inode);
532
533 cond_resched();
534 }
Chris Mason3eaa2882008-07-24 11:57:52 -0400535}
536
Chris Masoneb84ae02008-07-17 13:53:27 -0400537/*
Chris Mason5a3f23d2009-03-31 13:27:11 -0400538 * this is used during transaction commit to write all the inodes
539 * added to the ordered operation list. These files must be fully on
540 * disk before the transaction commits.
541 *
542 * we have two modes here, one is to just start the IO via filemap_flush
543 * and the other is to wait for all the io. When we wait, we have an
544 * extra check to make sure the ordered operation list really is empty
545 * before we return
546 */
Miao Xie25287e02012-10-25 09:31:03 +0000547int btrfs_run_ordered_operations(struct btrfs_root *root, int wait)
Chris Mason5a3f23d2009-03-31 13:27:11 -0400548{
549 struct btrfs_inode *btrfs_inode;
550 struct inode *inode;
551 struct list_head splice;
Miao Xie25287e02012-10-25 09:31:03 +0000552 struct list_head works;
553 struct btrfs_delalloc_work *work, *next;
554 int ret = 0;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400555
556 INIT_LIST_HEAD(&splice);
Miao Xie25287e02012-10-25 09:31:03 +0000557 INIT_LIST_HEAD(&works);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400558
559 mutex_lock(&root->fs_info->ordered_operations_mutex);
560 spin_lock(&root->fs_info->ordered_extent_lock);
561again:
562 list_splice_init(&root->fs_info->ordered_operations, &splice);
563
564 while (!list_empty(&splice)) {
Miao Xie25287e02012-10-25 09:31:03 +0000565
Chris Mason5a3f23d2009-03-31 13:27:11 -0400566 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
567 ordered_operations);
568
569 inode = &btrfs_inode->vfs_inode;
570
571 list_del_init(&btrfs_inode->ordered_operations);
572
573 /*
574 * the inode may be getting freed (in sys_unlink path).
575 */
576 inode = igrab(inode);
577
578 if (!wait && inode) {
579 list_add_tail(&BTRFS_I(inode)->ordered_operations,
580 &root->fs_info->ordered_operations);
581 }
Miao Xie25287e02012-10-25 09:31:03 +0000582
583 if (!inode)
584 continue;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400585 spin_unlock(&root->fs_info->ordered_extent_lock);
586
Miao Xie25287e02012-10-25 09:31:03 +0000587 work = btrfs_alloc_delalloc_work(inode, wait, 1);
588 if (!work) {
589 if (list_empty(&BTRFS_I(inode)->ordered_operations))
590 list_add_tail(&btrfs_inode->ordered_operations,
591 &splice);
592 spin_lock(&root->fs_info->ordered_extent_lock);
593 list_splice_tail(&splice,
594 &root->fs_info->ordered_operations);
595 spin_unlock(&root->fs_info->ordered_extent_lock);
596 ret = -ENOMEM;
597 goto out;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400598 }
Miao Xie25287e02012-10-25 09:31:03 +0000599 list_add_tail(&work->list, &works);
600 btrfs_queue_worker(&root->fs_info->flush_workers,
601 &work->work);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400602
603 cond_resched();
604 spin_lock(&root->fs_info->ordered_extent_lock);
605 }
606 if (wait && !list_empty(&root->fs_info->ordered_operations))
607 goto again;
608
609 spin_unlock(&root->fs_info->ordered_extent_lock);
Miao Xie25287e02012-10-25 09:31:03 +0000610out:
611 list_for_each_entry_safe(work, next, &works, list) {
612 list_del_init(&work->list);
613 btrfs_wait_and_free_delalloc_work(work);
614 }
Chris Mason5a3f23d2009-03-31 13:27:11 -0400615 mutex_unlock(&root->fs_info->ordered_operations_mutex);
Miao Xie25287e02012-10-25 09:31:03 +0000616 return ret;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400617}
618
619/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400620 * Used to start IO or wait for a given ordered extent to finish.
621 *
622 * If wait is one, this effectively waits on page writeback for all the pages
623 * in the extent, and it waits on the io completion code to insert
624 * metadata into the btree corresponding to the extent
625 */
626void btrfs_start_ordered_extent(struct inode *inode,
627 struct btrfs_ordered_extent *entry,
628 int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400629{
630 u64 start = entry->file_offset;
631 u64 end = start + entry->len - 1;
632
liubo1abe9b82011-03-24 11:18:59 +0000633 trace_btrfs_ordered_extent_start(inode, entry);
634
Chris Masoneb84ae02008-07-17 13:53:27 -0400635 /*
636 * pages in the range can be dirty, clean or writeback. We
637 * start IO on any dirty ones so the wait doesn't stall waiting
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300638 * for the flusher thread to find them
Chris Masoneb84ae02008-07-17 13:53:27 -0400639 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400640 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
641 filemap_fdatawrite_range(inode->i_mapping, start, end);
Chris Masonc8b97812008-10-29 14:49:59 -0400642 if (wait) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400643 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
644 &entry->flags));
Chris Masonc8b97812008-10-29 14:49:59 -0400645 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400646}
647
Chris Masoneb84ae02008-07-17 13:53:27 -0400648/*
649 * Used to wait on ordered extents across a large range of bytes.
650 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100651void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400652{
653 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400654 u64 orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400655 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400656
Chris Masone5a22172008-07-18 20:42:20 -0400657 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400658 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400659 } else {
660 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400661 if (orig_end > INT_LIMIT(loff_t))
662 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400663 }
Josef Bacik551ebb22012-04-23 14:41:09 -0400664
Chris Masone5a22172008-07-18 20:42:20 -0400665 /* start IO across the range first to instantiate any delalloc
666 * extents
667 */
Josef Bacik7ddf5a42012-06-08 15:26:47 -0400668 filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
669
670 /*
671 * So with compression we will find and lock a dirty page and clear the
672 * first one as dirty, setup an async extent, and immediately return
673 * with the entire range locked but with nobody actually marked with
674 * writeback. So we can't just filemap_write_and_wait_range() and
675 * expect it to work since it will just kick off a thread to do the
676 * actual work. So we need to call filemap_fdatawrite_range _again_
677 * since it will wait on the page lock, which won't be unlocked until
678 * after the pages have been marked as writeback and so we're good to go
679 * from there. We have to do this otherwise we'll miss the ordered
680 * extents and that results in badness. Please Josef, do not think you
681 * know better and pull this out at some point in the future, it is
682 * right and you are wrong.
683 */
684 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
685 &BTRFS_I(inode)->runtime_flags))
686 filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
687
688 filemap_fdatawait_range(inode->i_mapping, start, orig_end);
Chris Masonf4219502008-07-22 11:18:09 -0400689
690 end = orig_end;
Chris Masond3977122009-01-05 21:25:51 -0500691 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400692 ordered = btrfs_lookup_first_ordered_extent(inode, end);
Chris Masond3977122009-01-05 21:25:51 -0500693 if (!ordered)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400694 break;
Chris Masone5a22172008-07-18 20:42:20 -0400695 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400696 btrfs_put_ordered_extent(ordered);
697 break;
698 }
699 if (ordered->file_offset + ordered->len < start) {
700 btrfs_put_ordered_extent(ordered);
701 break;
702 }
Chris Masone5a22172008-07-18 20:42:20 -0400703 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400704 end = ordered->file_offset;
705 btrfs_put_ordered_extent(ordered);
Chris Masone5a22172008-07-18 20:42:20 -0400706 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400707 break;
708 end--;
709 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400710}
711
Chris Masoneb84ae02008-07-17 13:53:27 -0400712/*
713 * find an ordered extent corresponding to file_offset. return NULL if
714 * nothing is found, otherwise take a reference on the extent and return it
715 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400716struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
717 u64 file_offset)
718{
719 struct btrfs_ordered_inode_tree *tree;
720 struct rb_node *node;
721 struct btrfs_ordered_extent *entry = NULL;
722
723 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400724 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400725 node = tree_search(tree, file_offset);
726 if (!node)
727 goto out;
728
729 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
730 if (!offset_in_entry(entry, file_offset))
731 entry = NULL;
732 if (entry)
733 atomic_inc(&entry->refs);
734out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400735 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400736 return entry;
737}
738
Josef Bacik4b46fce2010-05-23 11:00:55 -0400739/* Since the DIO code tries to lock a wide area we need to look for any ordered
740 * extents that exist in the range, rather than just the start of the range.
741 */
742struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
743 u64 file_offset,
744 u64 len)
745{
746 struct btrfs_ordered_inode_tree *tree;
747 struct rb_node *node;
748 struct btrfs_ordered_extent *entry = NULL;
749
750 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400751 spin_lock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400752 node = tree_search(tree, file_offset);
753 if (!node) {
754 node = tree_search(tree, file_offset + len);
755 if (!node)
756 goto out;
757 }
758
759 while (1) {
760 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
761 if (range_overlaps(entry, file_offset, len))
762 break;
763
764 if (entry->file_offset >= file_offset + len) {
765 entry = NULL;
766 break;
767 }
768 entry = NULL;
769 node = rb_next(node);
770 if (!node)
771 break;
772 }
773out:
774 if (entry)
775 atomic_inc(&entry->refs);
Josef Bacik5fd02042012-05-02 14:00:54 -0400776 spin_unlock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400777 return entry;
778}
779
Chris Masoneb84ae02008-07-17 13:53:27 -0400780/*
781 * lookup and return any extent before 'file_offset'. NULL is returned
782 * if none is found
783 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400784struct btrfs_ordered_extent *
Chris Masond3977122009-01-05 21:25:51 -0500785btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400786{
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;
Josef Bacik5fd02042012-05-02 14:00:54 -0400792 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400793 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 atomic_inc(&entry->refs);
799out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400800 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 return entry;
802}
Chris Masondbe674a2008-07-17 12:54:05 -0400803
Chris Masoneb84ae02008-07-17 13:53:27 -0400804/*
805 * After an extent is done, call this to conditionally update the on disk
806 * i_size. i_size is updated to cover any fully written part of the file.
807 */
Yan, Zhengc2167752009-11-12 09:34:21 +0000808int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
Chris Masondbe674a2008-07-17 12:54:05 -0400809 struct btrfs_ordered_extent *ordered)
810{
811 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
Chris Masondbe674a2008-07-17 12:54:05 -0400812 u64 disk_i_size;
813 u64 new_i_size;
Yan, Zhengc2167752009-11-12 09:34:21 +0000814 u64 i_size = i_size_read(inode);
Chris Masondbe674a2008-07-17 12:54:05 -0400815 struct rb_node *node;
Yan, Zhengc2167752009-11-12 09:34:21 +0000816 struct rb_node *prev = NULL;
Chris Masondbe674a2008-07-17 12:54:05 -0400817 struct btrfs_ordered_extent *test;
Yan, Zhengc2167752009-11-12 09:34:21 +0000818 int ret = 1;
819
820 if (ordered)
821 offset = entry_end(ordered);
Yan, Zhenga038fab02009-12-28 05:01:58 +0000822 else
823 offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
Chris Masondbe674a2008-07-17 12:54:05 -0400824
Josef Bacik5fd02042012-05-02 14:00:54 -0400825 spin_lock_irq(&tree->lock);
Chris Masondbe674a2008-07-17 12:54:05 -0400826 disk_i_size = BTRFS_I(inode)->disk_i_size;
827
Yan, Zhengc2167752009-11-12 09:34:21 +0000828 /* truncate file */
829 if (disk_i_size > i_size) {
830 BTRFS_I(inode)->disk_i_size = i_size;
831 ret = 0;
832 goto out;
833 }
834
Chris Masondbe674a2008-07-17 12:54:05 -0400835 /*
836 * if the disk i_size is already at the inode->i_size, or
837 * this ordered extent is inside the disk i_size, we're done
838 */
Josef Bacik5d1f4022013-01-30 14:17:31 -0500839 if (disk_i_size == i_size)
Chris Masondbe674a2008-07-17 12:54:05 -0400840 goto out;
Josef Bacik5d1f4022013-01-30 14:17:31 -0500841
842 /*
843 * We still need to update disk_i_size if outstanding_isize is greater
844 * than disk_i_size.
845 */
846 if (offset <= disk_i_size &&
847 (!ordered || ordered->outstanding_isize <= disk_i_size))
848 goto out;
Chris Masondbe674a2008-07-17 12:54:05 -0400849
850 /*
Chris Masondbe674a2008-07-17 12:54:05 -0400851 * walk backward from this ordered extent to disk_i_size.
852 * if we find an ordered extent then we can't update disk i_size
853 * yet
854 */
Yan, Zhengc2167752009-11-12 09:34:21 +0000855 if (ordered) {
856 node = rb_prev(&ordered->rb_node);
857 } else {
858 prev = tree_search(tree, offset);
859 /*
860 * we insert file extents without involving ordered struct,
861 * so there should be no ordered struct cover this offset
862 */
863 if (prev) {
864 test = rb_entry(prev, struct btrfs_ordered_extent,
865 rb_node);
866 BUG_ON(offset_in_entry(test, offset));
867 }
868 node = prev;
869 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400870 for (; node; node = rb_prev(node)) {
Chris Masondbe674a2008-07-17 12:54:05 -0400871 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400872
873 /* We treat this entry as if it doesnt exist */
874 if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
875 continue;
Chris Masondbe674a2008-07-17 12:54:05 -0400876 if (test->file_offset + test->len <= disk_i_size)
877 break;
Yan, Zhengc2167752009-11-12 09:34:21 +0000878 if (test->file_offset >= i_size)
Chris Masondbe674a2008-07-17 12:54:05 -0400879 break;
Miao Xieb9a8cc52012-09-06 04:01:21 -0600880 if (test->file_offset >= disk_i_size) {
881 /*
882 * we don't update disk_i_size now, so record this
883 * undealt i_size. Or we will not know the real
884 * i_size.
885 */
886 if (test->outstanding_isize < offset)
887 test->outstanding_isize = offset;
888 if (ordered &&
889 ordered->outstanding_isize >
890 test->outstanding_isize)
891 test->outstanding_isize =
892 ordered->outstanding_isize;
Chris Masondbe674a2008-07-17 12:54:05 -0400893 goto out;
Miao Xieb9a8cc52012-09-06 04:01:21 -0600894 }
Chris Masondbe674a2008-07-17 12:54:05 -0400895 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000896 new_i_size = min_t(u64, offset, i_size);
Chris Masondbe674a2008-07-17 12:54:05 -0400897
898 /*
Miao Xieb9a8cc52012-09-06 04:01:21 -0600899 * Some ordered extents may completed before the current one, and
900 * we hold the real i_size in ->outstanding_isize.
Chris Masondbe674a2008-07-17 12:54:05 -0400901 */
Miao Xieb9a8cc52012-09-06 04:01:21 -0600902 if (ordered && ordered->outstanding_isize > new_i_size)
903 new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
Chris Masondbe674a2008-07-17 12:54:05 -0400904 BTRFS_I(inode)->disk_i_size = new_i_size;
Yan, Zhengc2167752009-11-12 09:34:21 +0000905 ret = 0;
Chris Masondbe674a2008-07-17 12:54:05 -0400906out:
Yan, Zhengc2167752009-11-12 09:34:21 +0000907 /*
Josef Bacik5fd02042012-05-02 14:00:54 -0400908 * We need to do this because we can't remove ordered extents until
909 * after the i_disk_size has been updated and then the inode has been
910 * updated to reflect the change, so we need to tell anybody who finds
911 * this ordered extent that we've already done all the real work, we
912 * just haven't completed all the other work.
Yan, Zhengc2167752009-11-12 09:34:21 +0000913 */
914 if (ordered)
Josef Bacik5fd02042012-05-02 14:00:54 -0400915 set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
916 spin_unlock_irq(&tree->lock);
Yan, Zhengc2167752009-11-12 09:34:21 +0000917 return ret;
Chris Masondbe674a2008-07-17 12:54:05 -0400918}
Chris Masonba1da2f2008-07-17 12:54:15 -0400919
Chris Masoneb84ae02008-07-17 13:53:27 -0400920/*
921 * search the ordered extents for one corresponding to 'offset' and
922 * try to find a checksum. This is used because we allow pages to
923 * be reclaimed before their checksum is actually put into the btree
924 */
Chris Masond20f7042008-12-08 16:58:54 -0500925int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
926 u32 *sum)
Chris Masonba1da2f2008-07-17 12:54:15 -0400927{
928 struct btrfs_ordered_sum *ordered_sum;
929 struct btrfs_sector_sum *sector_sums;
930 struct btrfs_ordered_extent *ordered;
931 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
Chris Mason3edf7d32008-07-18 06:17:13 -0400932 unsigned long num_sectors;
933 unsigned long i;
934 u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
Chris Masonba1da2f2008-07-17 12:54:15 -0400935 int ret = 1;
Chris Masonba1da2f2008-07-17 12:54:15 -0400936
937 ordered = btrfs_lookup_ordered_extent(inode, offset);
938 if (!ordered)
939 return 1;
940
Josef Bacik5fd02042012-05-02 14:00:54 -0400941 spin_lock_irq(&tree->lock);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500942 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
Chris Masond20f7042008-12-08 16:58:54 -0500943 if (disk_bytenr >= ordered_sum->bytenr) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400944 num_sectors = ordered_sum->len / sectorsize;
Chris Masoned98b562008-07-22 23:06:42 -0400945 sector_sums = ordered_sum->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400946 for (i = 0; i < num_sectors; i++) {
Chris Masond20f7042008-12-08 16:58:54 -0500947 if (sector_sums[i].bytenr == disk_bytenr) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400948 *sum = sector_sums[i].sum;
949 ret = 0;
950 goto out;
951 }
952 }
Chris Masonba1da2f2008-07-17 12:54:15 -0400953 }
954 }
955out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400956 spin_unlock_irq(&tree->lock);
Chris Mason89642222008-07-24 09:41:53 -0400957 btrfs_put_ordered_extent(ordered);
Chris Masonba1da2f2008-07-17 12:54:15 -0400958 return ret;
959}
960
Chris Masonf4219502008-07-22 11:18:09 -0400961
Chris Mason5a3f23d2009-03-31 13:27:11 -0400962/*
963 * add a given inode to the list of inodes that must be fully on
964 * disk before a transaction commit finishes.
965 *
966 * This basically gives us the ext3 style data=ordered mode, and it is mostly
967 * used to make sure renamed files are fully on disk.
968 *
969 * It is a noop if the inode is already fully on disk.
970 *
971 * If trans is not null, we'll do a friendly check for a transaction that
972 * is already flushing things and force the IO down ourselves.
973 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100974void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
975 struct btrfs_root *root, struct inode *inode)
Chris Mason5a3f23d2009-03-31 13:27:11 -0400976{
977 u64 last_mod;
978
979 last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
980
981 /*
982 * if this file hasn't been changed since the last transaction
983 * commit, we can safely return without doing anything
984 */
985 if (last_mod < root->fs_info->last_trans_committed)
Jeff Mahoney143bede2012-03-01 14:56:26 +0100986 return;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400987
Chris Mason5a3f23d2009-03-31 13:27:11 -0400988 spin_lock(&root->fs_info->ordered_extent_lock);
989 if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
990 list_add_tail(&BTRFS_I(inode)->ordered_operations,
991 &root->fs_info->ordered_operations);
992 }
993 spin_unlock(&root->fs_info->ordered_extent_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400994}
Miao Xie6352b912012-09-06 04:01:51 -0600995
996int __init ordered_data_init(void)
997{
998 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
999 sizeof(struct btrfs_ordered_extent), 0,
1000 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
1001 NULL);
1002 if (!btrfs_ordered_extent_cache)
1003 return -ENOMEM;
Miao Xie25287e02012-10-25 09:31:03 +00001004
Miao Xie6352b912012-09-06 04:01:51 -06001005 return 0;
1006}
1007
1008void ordered_data_exit(void)
1009{
1010 if (btrfs_ordered_extent_cache)
1011 kmem_cache_destroy(btrfs_ordered_extent_cache);
1012}