blob: dc08d77b717ea47f0eb7d43e351153f556c75eaa [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;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400199 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) &&
200 !(type == BTRFS_ORDERED_NOCOW))
201 entry->csum_bytes_left = disk_len;
Chris Masonc8b97812008-10-29 14:49:59 -0400202 entry->disk_len = disk_len;
Chris Mason8b62b722009-09-02 16:53:46 -0400203 entry->bytes_left = len;
Josef Bacik5fd02042012-05-02 14:00:54 -0400204 entry->inode = igrab(inode);
Li Zefan261507a02010-12-17 14:21:50 +0800205 entry->compress_type = compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -0400206 if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
Yan Zheng80ff3852008-10-30 14:20:02 -0400207 set_bit(type, &entry->flags);
Chris Mason3eaa2882008-07-24 11:57:52 -0400208
Josef Bacik4b46fce2010-05-23 11:00:55 -0400209 if (dio)
210 set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
211
Chris Masone6dcd2d2008-07-17 12:53:50 -0400212 /* one ref for the tree */
213 atomic_set(&entry->refs, 1);
214 init_waitqueue_head(&entry->wait);
215 INIT_LIST_HEAD(&entry->list);
Chris Mason3eaa2882008-07-24 11:57:52 -0400216 INIT_LIST_HEAD(&entry->root_extent_list);
Miao Xie9afab882012-10-25 09:41:36 +0000217 INIT_LIST_HEAD(&entry->work_list);
218 init_completion(&entry->completion);
Josef Bacik2ab28f32012-10-12 15:27:49 -0400219 INIT_LIST_HEAD(&entry->log_list);
Chris Masondc17ff82008-01-08 15:46:30 -0500220
liubo1abe9b82011-03-24 11:18:59 +0000221 trace_btrfs_ordered_extent_add(inode, entry);
222
Josef Bacik5fd02042012-05-02 14:00:54 -0400223 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400224 node = tree_insert(&tree->tree, file_offset,
225 &entry->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400226 if (node)
227 ordered_data_tree_panic(inode, -EEXIST, file_offset);
Josef Bacik5fd02042012-05-02 14:00:54 -0400228 spin_unlock_irq(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500229
Chris Mason3eaa2882008-07-24 11:57:52 -0400230 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
231 list_add_tail(&entry->root_extent_list,
232 &BTRFS_I(inode)->root->fs_info->ordered_extents);
233 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
234
Chris Masone6dcd2d2008-07-17 12:53:50 -0400235 return 0;
236}
Chris Masondc17ff82008-01-08 15:46:30 -0500237
Josef Bacik4b46fce2010-05-23 11:00:55 -0400238int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
239 u64 start, u64 len, u64 disk_len, int type)
240{
241 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
Li Zefan261507a02010-12-17 14:21:50 +0800242 disk_len, type, 0,
243 BTRFS_COMPRESS_NONE);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400244}
245
246int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
247 u64 start, u64 len, u64 disk_len, int type)
248{
249 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
Li Zefan261507a02010-12-17 14:21:50 +0800250 disk_len, type, 1,
251 BTRFS_COMPRESS_NONE);
252}
253
254int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
255 u64 start, u64 len, u64 disk_len,
256 int type, int compress_type)
257{
258 return __btrfs_add_ordered_extent(inode, file_offset, start, len,
259 disk_len, type, 0,
260 compress_type);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400261}
262
Chris Masoneb84ae02008-07-17 13:53:27 -0400263/*
264 * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
Chris Mason3edf7d32008-07-18 06:17:13 -0400265 * when an ordered extent is finished. If the list covers more than one
266 * ordered extent, it is split across multiples.
Chris Masoneb84ae02008-07-17 13:53:27 -0400267 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100268void btrfs_add_ordered_sum(struct inode *inode,
269 struct btrfs_ordered_extent *entry,
270 struct btrfs_ordered_sum *sum)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400271{
272 struct btrfs_ordered_inode_tree *tree;
Chris Mason1b1e2132008-06-25 16:01:31 -0400273
Chris Masone6dcd2d2008-07-17 12:53:50 -0400274 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400275 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400276 list_add_tail(&sum->list, &entry->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -0400277 WARN_ON(entry->csum_bytes_left < sum->len);
278 entry->csum_bytes_left -= sum->len;
279 if (entry->csum_bytes_left == 0)
280 wake_up(&entry->wait);
Josef Bacik5fd02042012-05-02 14:00:54 -0400281 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400282}
283
Chris Masoneb84ae02008-07-17 13:53:27 -0400284/*
285 * this is used to account for finished IO across a given range
Chris Mason163cf092010-11-28 19:56:33 -0500286 * of the file. The IO may span ordered extents. If
287 * a given ordered_extent is completely done, 1 is returned, otherwise
288 * 0.
289 *
290 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
291 * to make sure this function only returns 1 once for a given ordered extent.
292 *
293 * file_offset is updated to one byte past the range that is recorded as
294 * complete. This allows you to walk forward in the file.
295 */
296int btrfs_dec_test_first_ordered_pending(struct inode *inode,
297 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400298 u64 *file_offset, u64 io_size, int uptodate)
Chris Mason163cf092010-11-28 19:56:33 -0500299{
300 struct btrfs_ordered_inode_tree *tree;
301 struct rb_node *node;
302 struct btrfs_ordered_extent *entry = NULL;
303 int ret;
Josef Bacik5fd02042012-05-02 14:00:54 -0400304 unsigned long flags;
Chris Mason163cf092010-11-28 19:56:33 -0500305 u64 dec_end;
306 u64 dec_start;
307 u64 to_dec;
308
309 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400310 spin_lock_irqsave(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500311 node = tree_search(tree, *file_offset);
312 if (!node) {
313 ret = 1;
314 goto out;
315 }
316
317 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
318 if (!offset_in_entry(entry, *file_offset)) {
319 ret = 1;
320 goto out;
321 }
322
323 dec_start = max(*file_offset, entry->file_offset);
324 dec_end = min(*file_offset + io_size, entry->file_offset +
325 entry->len);
326 *file_offset = dec_end;
327 if (dec_start > dec_end) {
328 printk(KERN_CRIT "bad ordering dec_start %llu end %llu\n",
329 (unsigned long long)dec_start,
330 (unsigned long long)dec_end);
331 }
332 to_dec = dec_end - dec_start;
333 if (to_dec > entry->bytes_left) {
334 printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
335 (unsigned long long)entry->bytes_left,
336 (unsigned long long)to_dec);
337 }
338 entry->bytes_left -= to_dec;
Josef Bacik5fd02042012-05-02 14:00:54 -0400339 if (!uptodate)
340 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
341
Chris Mason163cf092010-11-28 19:56:33 -0500342 if (entry->bytes_left == 0)
343 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
344 else
345 ret = 1;
346out:
347 if (!ret && cached && entry) {
348 *cached = entry;
349 atomic_inc(&entry->refs);
350 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400351 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason163cf092010-11-28 19:56:33 -0500352 return ret == 0;
353}
354
355/*
356 * this is used to account for finished IO across a given range
Chris Masoneb84ae02008-07-17 13:53:27 -0400357 * of the file. The IO should not span ordered extents. If
358 * a given ordered_extent is completely done, 1 is returned, otherwise
359 * 0.
360 *
361 * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
362 * to make sure this function only returns 1 once for a given ordered extent.
363 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400364int btrfs_dec_test_ordered_pending(struct inode *inode,
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000365 struct btrfs_ordered_extent **cached,
Josef Bacik5fd02042012-05-02 14:00:54 -0400366 u64 file_offset, u64 io_size, int uptodate)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400367{
368 struct btrfs_ordered_inode_tree *tree;
369 struct rb_node *node;
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000370 struct btrfs_ordered_extent *entry = NULL;
Josef Bacik5fd02042012-05-02 14:00:54 -0400371 unsigned long flags;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400372 int ret;
373
374 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400375 spin_lock_irqsave(&tree->lock, flags);
376 if (cached && *cached) {
377 entry = *cached;
378 goto have_entry;
379 }
380
Chris Masone6dcd2d2008-07-17 12:53:50 -0400381 node = tree_search(tree, file_offset);
382 if (!node) {
383 ret = 1;
384 goto out;
385 }
386
387 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400388have_entry:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400389 if (!offset_in_entry(entry, file_offset)) {
390 ret = 1;
391 goto out;
392 }
393
Chris Mason8b62b722009-09-02 16:53:46 -0400394 if (io_size > entry->bytes_left) {
395 printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
396 (unsigned long long)entry->bytes_left,
397 (unsigned long long)io_size);
398 }
399 entry->bytes_left -= io_size;
Josef Bacik5fd02042012-05-02 14:00:54 -0400400 if (!uptodate)
401 set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
402
Chris Mason8b62b722009-09-02 16:53:46 -0400403 if (entry->bytes_left == 0)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400404 ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
Chris Mason8b62b722009-09-02 16:53:46 -0400405 else
406 ret = 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400407out:
Josef Bacik5a1a3df2010-02-02 20:51:14 +0000408 if (!ret && cached && entry) {
409 *cached = entry;
410 atomic_inc(&entry->refs);
411 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400412 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400413 return ret == 0;
414}
415
Josef Bacik2ab28f32012-10-12 15:27:49 -0400416/* Needs to either be called under a log transaction or the log_mutex */
417void btrfs_get_logged_extents(struct btrfs_root *log, struct inode *inode)
418{
419 struct btrfs_ordered_inode_tree *tree;
420 struct btrfs_ordered_extent *ordered;
421 struct rb_node *n;
422 int index = log->log_transid % 2;
423
424 tree = &BTRFS_I(inode)->ordered_tree;
425 spin_lock_irq(&tree->lock);
426 for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
427 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
428 spin_lock(&log->log_extents_lock[index]);
429 if (list_empty(&ordered->log_list)) {
430 list_add_tail(&ordered->log_list, &log->logged_list[index]);
431 atomic_inc(&ordered->refs);
432 }
433 spin_unlock(&log->log_extents_lock[index]);
434 }
435 spin_unlock_irq(&tree->lock);
436}
437
438void btrfs_wait_logged_extents(struct btrfs_root *log, u64 transid)
439{
440 struct btrfs_ordered_extent *ordered;
441 int index = transid % 2;
442
443 spin_lock_irq(&log->log_extents_lock[index]);
444 while (!list_empty(&log->logged_list[index])) {
445 ordered = list_first_entry(&log->logged_list[index],
446 struct btrfs_ordered_extent,
447 log_list);
448 list_del_init(&ordered->log_list);
449 spin_unlock_irq(&log->log_extents_lock[index]);
450 wait_event(ordered->wait, test_bit(BTRFS_ORDERED_IO_DONE,
451 &ordered->flags));
452 btrfs_put_ordered_extent(ordered);
453 spin_lock_irq(&log->log_extents_lock[index]);
454 }
455 spin_unlock_irq(&log->log_extents_lock[index]);
456}
457
458void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid)
459{
460 struct btrfs_ordered_extent *ordered;
461 int index = transid % 2;
462
463 spin_lock_irq(&log->log_extents_lock[index]);
464 while (!list_empty(&log->logged_list[index])) {
465 ordered = list_first_entry(&log->logged_list[index],
466 struct btrfs_ordered_extent,
467 log_list);
468 list_del_init(&ordered->log_list);
469 spin_unlock_irq(&log->log_extents_lock[index]);
470 btrfs_put_ordered_extent(ordered);
471 spin_lock_irq(&log->log_extents_lock[index]);
472 }
473 spin_unlock_irq(&log->log_extents_lock[index]);
474}
475
Chris Masoneb84ae02008-07-17 13:53:27 -0400476/*
477 * used to drop a reference on an ordered extent. This will free
478 * the extent if the last reference is dropped
479 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100480void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400481{
Chris Masonba1da2f2008-07-17 12:54:15 -0400482 struct list_head *cur;
483 struct btrfs_ordered_sum *sum;
484
liubo1abe9b82011-03-24 11:18:59 +0000485 trace_btrfs_ordered_extent_put(entry->inode, entry);
486
Chris Masonba1da2f2008-07-17 12:54:15 -0400487 if (atomic_dec_and_test(&entry->refs)) {
Josef Bacik5fd02042012-05-02 14:00:54 -0400488 if (entry->inode)
489 btrfs_add_delayed_iput(entry->inode);
Chris Masond3977122009-01-05 21:25:51 -0500490 while (!list_empty(&entry->list)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400491 cur = entry->list.next;
492 sum = list_entry(cur, struct btrfs_ordered_sum, list);
493 list_del(&sum->list);
494 kfree(sum);
495 }
Miao Xie6352b912012-09-06 04:01:51 -0600496 kmem_cache_free(btrfs_ordered_extent_cache, entry);
Chris Masonba1da2f2008-07-17 12:54:15 -0400497 }
Chris Masondc17ff82008-01-08 15:46:30 -0500498}
499
Chris Masoneb84ae02008-07-17 13:53:27 -0400500/*
501 * remove an ordered extent from the tree. No references are dropped
Josef Bacik5fd02042012-05-02 14:00:54 -0400502 * and waiters are woken up.
Chris Masoneb84ae02008-07-17 13:53:27 -0400503 */
Josef Bacik5fd02042012-05-02 14:00:54 -0400504void btrfs_remove_ordered_extent(struct inode *inode,
505 struct btrfs_ordered_extent *entry)
Chris Masondc17ff82008-01-08 15:46:30 -0500506{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400507 struct btrfs_ordered_inode_tree *tree;
Josef Bacik287a0ab2010-03-19 18:07:23 +0000508 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondc17ff82008-01-08 15:46:30 -0500509 struct rb_node *node;
510
Chris Masone6dcd2d2008-07-17 12:53:50 -0400511 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400512 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400513 node = &entry->rb_node;
Chris Masondc17ff82008-01-08 15:46:30 -0500514 rb_erase(node, &tree->tree);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400515 tree->last = NULL;
516 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
Josef Bacik5fd02042012-05-02 14:00:54 -0400517 spin_unlock_irq(&tree->lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400518
Josef Bacik287a0ab2010-03-19 18:07:23 +0000519 spin_lock(&root->fs_info->ordered_extent_lock);
Chris Mason3eaa2882008-07-24 11:57:52 -0400520 list_del_init(&entry->root_extent_list);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400521
liubo1abe9b82011-03-24 11:18:59 +0000522 trace_btrfs_ordered_extent_remove(inode, entry);
523
Chris Mason5a3f23d2009-03-31 13:27:11 -0400524 /*
525 * we have no more ordered extents for this inode and
526 * no dirty pages. We can safely remove it from the
527 * list of ordered extents
528 */
529 if (RB_EMPTY_ROOT(&tree->tree) &&
530 !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
531 list_del_init(&BTRFS_I(inode)->ordered_operations);
532 }
Josef Bacik287a0ab2010-03-19 18:07:23 +0000533 spin_unlock(&root->fs_info->ordered_extent_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400534 wake_up(&entry->wait);
Chris Mason81d7ed22008-04-25 08:51:48 -0400535}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400536
Miao Xie9afab882012-10-25 09:41:36 +0000537static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
538{
539 struct btrfs_ordered_extent *ordered;
540
541 ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
542 btrfs_start_ordered_extent(ordered->inode, ordered, 1);
543 complete(&ordered->completion);
544}
545
Chris Masond352ac62008-09-29 15:18:18 -0400546/*
547 * wait for all the ordered extents in a root. This is done when balancing
548 * space between drives.
549 */
Liu Bo6bbe3a92012-09-14 02:58:07 -0600550void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput)
Chris Mason3eaa2882008-07-24 11:57:52 -0400551{
Miao Xie9afab882012-10-25 09:41:36 +0000552 struct list_head splice, works;
Chris Mason3eaa2882008-07-24 11:57:52 -0400553 struct list_head *cur;
Miao Xie9afab882012-10-25 09:41:36 +0000554 struct btrfs_ordered_extent *ordered, *next;
Chris Mason3eaa2882008-07-24 11:57:52 -0400555 struct inode *inode;
556
557 INIT_LIST_HEAD(&splice);
Miao Xie9afab882012-10-25 09:41:36 +0000558 INIT_LIST_HEAD(&works);
Chris Mason3eaa2882008-07-24 11:57:52 -0400559
560 spin_lock(&root->fs_info->ordered_extent_lock);
561 list_splice_init(&root->fs_info->ordered_extents, &splice);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400562 while (!list_empty(&splice)) {
Chris Mason3eaa2882008-07-24 11:57:52 -0400563 cur = splice.next;
564 ordered = list_entry(cur, struct btrfs_ordered_extent,
565 root_extent_list);
566 list_del_init(&ordered->root_extent_list);
567 atomic_inc(&ordered->refs);
Chris Mason3eaa2882008-07-24 11:57:52 -0400568
569 /*
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400570 * the inode may be getting freed (in sys_unlink path).
Chris Mason3eaa2882008-07-24 11:57:52 -0400571 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400572 inode = igrab(ordered->inode);
573
Chris Mason3eaa2882008-07-24 11:57:52 -0400574 spin_unlock(&root->fs_info->ordered_extent_lock);
575
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400576 if (inode) {
Miao Xie9afab882012-10-25 09:41:36 +0000577 ordered->flush_work.func = btrfs_run_ordered_extent_work;
578 list_add_tail(&ordered->work_list, &works);
579 btrfs_queue_worker(&root->fs_info->flush_workers,
580 &ordered->flush_work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400581 } else {
582 btrfs_put_ordered_extent(ordered);
583 }
Chris Mason3eaa2882008-07-24 11:57:52 -0400584
Miao Xie9afab882012-10-25 09:41:36 +0000585 cond_resched();
Chris Mason3eaa2882008-07-24 11:57:52 -0400586 spin_lock(&root->fs_info->ordered_extent_lock);
587 }
588 spin_unlock(&root->fs_info->ordered_extent_lock);
Miao Xie9afab882012-10-25 09:41:36 +0000589
590 list_for_each_entry_safe(ordered, next, &works, work_list) {
591 list_del_init(&ordered->work_list);
592 wait_for_completion(&ordered->completion);
593
594 inode = ordered->inode;
595 btrfs_put_ordered_extent(ordered);
596 if (delay_iput)
597 btrfs_add_delayed_iput(inode);
598 else
599 iput(inode);
600
601 cond_resched();
602 }
Chris Mason3eaa2882008-07-24 11:57:52 -0400603}
604
Chris Masoneb84ae02008-07-17 13:53:27 -0400605/*
Chris Mason5a3f23d2009-03-31 13:27:11 -0400606 * this is used during transaction commit to write all the inodes
607 * added to the ordered operation list. These files must be fully on
608 * disk before the transaction commits.
609 *
610 * we have two modes here, one is to just start the IO via filemap_flush
611 * and the other is to wait for all the io. When we wait, we have an
612 * extra check to make sure the ordered operation list really is empty
613 * before we return
614 */
Josef Bacik569e0f32013-02-13 11:09:14 -0500615int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans,
616 struct btrfs_root *root, int wait)
Chris Mason5a3f23d2009-03-31 13:27:11 -0400617{
618 struct btrfs_inode *btrfs_inode;
619 struct inode *inode;
Josef Bacik569e0f32013-02-13 11:09:14 -0500620 struct btrfs_transaction *cur_trans = trans->transaction;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400621 struct list_head splice;
Miao Xie25287e02012-10-25 09:31:03 +0000622 struct list_head works;
623 struct btrfs_delalloc_work *work, *next;
624 int ret = 0;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400625
626 INIT_LIST_HEAD(&splice);
Miao Xie25287e02012-10-25 09:31:03 +0000627 INIT_LIST_HEAD(&works);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400628
629 mutex_lock(&root->fs_info->ordered_operations_mutex);
630 spin_lock(&root->fs_info->ordered_extent_lock);
Josef Bacik569e0f32013-02-13 11:09:14 -0500631 list_splice_init(&cur_trans->ordered_operations, &splice);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400632 while (!list_empty(&splice)) {
633 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
634 ordered_operations);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400635 inode = &btrfs_inode->vfs_inode;
636
637 list_del_init(&btrfs_inode->ordered_operations);
638
639 /*
640 * the inode may be getting freed (in sys_unlink path).
641 */
642 inode = igrab(inode);
Miao Xie25287e02012-10-25 09:31:03 +0000643 if (!inode)
644 continue;
Miao Xie5b947f12013-01-22 10:52:04 +0000645
646 if (!wait)
647 list_add_tail(&BTRFS_I(inode)->ordered_operations,
Josef Bacik569e0f32013-02-13 11:09:14 -0500648 &cur_trans->ordered_operations);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400649 spin_unlock(&root->fs_info->ordered_extent_lock);
650
Miao Xie25287e02012-10-25 09:31:03 +0000651 work = btrfs_alloc_delalloc_work(inode, wait, 1);
652 if (!work) {
Miao Xie5b947f12013-01-22 10:52:04 +0000653 spin_lock(&root->fs_info->ordered_extent_lock);
Miao Xie25287e02012-10-25 09:31:03 +0000654 if (list_empty(&BTRFS_I(inode)->ordered_operations))
655 list_add_tail(&btrfs_inode->ordered_operations,
656 &splice);
Miao Xie25287e02012-10-25 09:31:03 +0000657 list_splice_tail(&splice,
Josef Bacik569e0f32013-02-13 11:09:14 -0500658 &cur_trans->ordered_operations);
Miao Xie25287e02012-10-25 09:31:03 +0000659 spin_unlock(&root->fs_info->ordered_extent_lock);
660 ret = -ENOMEM;
661 goto out;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400662 }
Miao Xie25287e02012-10-25 09:31:03 +0000663 list_add_tail(&work->list, &works);
664 btrfs_queue_worker(&root->fs_info->flush_workers,
665 &work->work);
Chris Mason5a3f23d2009-03-31 13:27:11 -0400666
667 cond_resched();
668 spin_lock(&root->fs_info->ordered_extent_lock);
669 }
Chris Mason5a3f23d2009-03-31 13:27:11 -0400670 spin_unlock(&root->fs_info->ordered_extent_lock);
Miao Xie25287e02012-10-25 09:31:03 +0000671out:
672 list_for_each_entry_safe(work, next, &works, list) {
673 list_del_init(&work->list);
674 btrfs_wait_and_free_delalloc_work(work);
675 }
Chris Mason5a3f23d2009-03-31 13:27:11 -0400676 mutex_unlock(&root->fs_info->ordered_operations_mutex);
Miao Xie25287e02012-10-25 09:31:03 +0000677 return ret;
Chris Mason5a3f23d2009-03-31 13:27:11 -0400678}
679
680/*
Chris Masoneb84ae02008-07-17 13:53:27 -0400681 * Used to start IO or wait for a given ordered extent to finish.
682 *
683 * If wait is one, this effectively waits on page writeback for all the pages
684 * in the extent, and it waits on the io completion code to insert
685 * metadata into the btree corresponding to the extent
686 */
687void btrfs_start_ordered_extent(struct inode *inode,
688 struct btrfs_ordered_extent *entry,
689 int wait)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400690{
691 u64 start = entry->file_offset;
692 u64 end = start + entry->len - 1;
693
liubo1abe9b82011-03-24 11:18:59 +0000694 trace_btrfs_ordered_extent_start(inode, entry);
695
Chris Masoneb84ae02008-07-17 13:53:27 -0400696 /*
697 * pages in the range can be dirty, clean or writeback. We
698 * start IO on any dirty ones so the wait doesn't stall waiting
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300699 * for the flusher thread to find them
Chris Masoneb84ae02008-07-17 13:53:27 -0400700 */
Josef Bacik4b46fce2010-05-23 11:00:55 -0400701 if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
702 filemap_fdatawrite_range(inode->i_mapping, start, end);
Chris Masonc8b97812008-10-29 14:49:59 -0400703 if (wait) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400704 wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
705 &entry->flags));
Chris Masonc8b97812008-10-29 14:49:59 -0400706 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400707}
708
Chris Masoneb84ae02008-07-17 13:53:27 -0400709/*
710 * Used to wait on ordered extents across a large range of bytes.
711 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100712void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400713{
714 u64 end;
Chris Masone5a22172008-07-18 20:42:20 -0400715 u64 orig_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400716 struct btrfs_ordered_extent *ordered;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400717
Chris Masone5a22172008-07-18 20:42:20 -0400718 if (start + len < start) {
Chris Masonf4219502008-07-22 11:18:09 -0400719 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400720 } else {
721 orig_end = start + len - 1;
Chris Masonf4219502008-07-22 11:18:09 -0400722 if (orig_end > INT_LIMIT(loff_t))
723 orig_end = INT_LIMIT(loff_t);
Chris Masone5a22172008-07-18 20:42:20 -0400724 }
Josef Bacik551ebb22012-04-23 14:41:09 -0400725
Chris Masone5a22172008-07-18 20:42:20 -0400726 /* start IO across the range first to instantiate any delalloc
727 * extents
728 */
Josef Bacik7ddf5a42012-06-08 15:26:47 -0400729 filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
730
731 /*
732 * So with compression we will find and lock a dirty page and clear the
733 * first one as dirty, setup an async extent, and immediately return
734 * with the entire range locked but with nobody actually marked with
735 * writeback. So we can't just filemap_write_and_wait_range() and
736 * expect it to work since it will just kick off a thread to do the
737 * actual work. So we need to call filemap_fdatawrite_range _again_
738 * since it will wait on the page lock, which won't be unlocked until
739 * after the pages have been marked as writeback and so we're good to go
740 * from there. We have to do this otherwise we'll miss the ordered
741 * extents and that results in badness. Please Josef, do not think you
742 * know better and pull this out at some point in the future, it is
743 * right and you are wrong.
744 */
745 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
746 &BTRFS_I(inode)->runtime_flags))
747 filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
748
749 filemap_fdatawait_range(inode->i_mapping, start, orig_end);
Chris Masonf4219502008-07-22 11:18:09 -0400750
751 end = orig_end;
Chris Masond3977122009-01-05 21:25:51 -0500752 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 ordered = btrfs_lookup_first_ordered_extent(inode, end);
Chris Masond3977122009-01-05 21:25:51 -0500754 if (!ordered)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400755 break;
Chris Masone5a22172008-07-18 20:42:20 -0400756 if (ordered->file_offset > orig_end) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400757 btrfs_put_ordered_extent(ordered);
758 break;
759 }
760 if (ordered->file_offset + ordered->len < start) {
761 btrfs_put_ordered_extent(ordered);
762 break;
763 }
Chris Masone5a22172008-07-18 20:42:20 -0400764 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400765 end = ordered->file_offset;
766 btrfs_put_ordered_extent(ordered);
Chris Masone5a22172008-07-18 20:42:20 -0400767 if (end == 0 || end == start)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400768 break;
769 end--;
770 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400771}
772
Chris Masoneb84ae02008-07-17 13:53:27 -0400773/*
774 * find an ordered extent corresponding to file_offset. return NULL if
775 * nothing is found, otherwise take a reference on the extent and return it
776 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400777struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
778 u64 file_offset)
779{
780 struct btrfs_ordered_inode_tree *tree;
781 struct rb_node *node;
782 struct btrfs_ordered_extent *entry = NULL;
783
784 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400785 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400786 node = tree_search(tree, file_offset);
787 if (!node)
788 goto out;
789
790 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
791 if (!offset_in_entry(entry, file_offset))
792 entry = NULL;
793 if (entry)
794 atomic_inc(&entry->refs);
795out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400796 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400797 return entry;
798}
799
Josef Bacik4b46fce2010-05-23 11:00:55 -0400800/* Since the DIO code tries to lock a wide area we need to look for any ordered
801 * extents that exist in the range, rather than just the start of the range.
802 */
803struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
804 u64 file_offset,
805 u64 len)
806{
807 struct btrfs_ordered_inode_tree *tree;
808 struct rb_node *node;
809 struct btrfs_ordered_extent *entry = NULL;
810
811 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400812 spin_lock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400813 node = tree_search(tree, file_offset);
814 if (!node) {
815 node = tree_search(tree, file_offset + len);
816 if (!node)
817 goto out;
818 }
819
820 while (1) {
821 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
822 if (range_overlaps(entry, file_offset, len))
823 break;
824
825 if (entry->file_offset >= file_offset + len) {
826 entry = NULL;
827 break;
828 }
829 entry = NULL;
830 node = rb_next(node);
831 if (!node)
832 break;
833 }
834out:
835 if (entry)
836 atomic_inc(&entry->refs);
Josef Bacik5fd02042012-05-02 14:00:54 -0400837 spin_unlock_irq(&tree->lock);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400838 return entry;
839}
840
Chris Masoneb84ae02008-07-17 13:53:27 -0400841/*
842 * lookup and return any extent before 'file_offset'. NULL is returned
843 * if none is found
844 */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400845struct btrfs_ordered_extent *
Chris Masond3977122009-01-05 21:25:51 -0500846btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400847{
848 struct btrfs_ordered_inode_tree *tree;
849 struct rb_node *node;
850 struct btrfs_ordered_extent *entry = NULL;
851
852 tree = &BTRFS_I(inode)->ordered_tree;
Josef Bacik5fd02042012-05-02 14:00:54 -0400853 spin_lock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400854 node = tree_search(tree, file_offset);
855 if (!node)
856 goto out;
857
858 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
859 atomic_inc(&entry->refs);
860out:
Josef Bacik5fd02042012-05-02 14:00:54 -0400861 spin_unlock_irq(&tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400862 return entry;
863}
Chris Masondbe674a2008-07-17 12:54:05 -0400864
Chris Masoneb84ae02008-07-17 13:53:27 -0400865/*
866 * After an extent is done, call this to conditionally update the on disk
867 * i_size. i_size is updated to cover any fully written part of the file.
868 */
Yan, Zhengc2167752009-11-12 09:34:21 +0000869int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
Chris Masondbe674a2008-07-17 12:54:05 -0400870 struct btrfs_ordered_extent *ordered)
871{
872 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
Chris Masondbe674a2008-07-17 12:54:05 -0400873 u64 disk_i_size;
874 u64 new_i_size;
Yan, Zhengc2167752009-11-12 09:34:21 +0000875 u64 i_size = i_size_read(inode);
Chris Masondbe674a2008-07-17 12:54:05 -0400876 struct rb_node *node;
Yan, Zhengc2167752009-11-12 09:34:21 +0000877 struct rb_node *prev = NULL;
Chris Masondbe674a2008-07-17 12:54:05 -0400878 struct btrfs_ordered_extent *test;
Yan, Zhengc2167752009-11-12 09:34:21 +0000879 int ret = 1;
880
881 if (ordered)
882 offset = entry_end(ordered);
Yan, Zhenga038fab02009-12-28 05:01:58 +0000883 else
884 offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
Chris Masondbe674a2008-07-17 12:54:05 -0400885
Josef Bacik5fd02042012-05-02 14:00:54 -0400886 spin_lock_irq(&tree->lock);
Chris Masondbe674a2008-07-17 12:54:05 -0400887 disk_i_size = BTRFS_I(inode)->disk_i_size;
888
Yan, Zhengc2167752009-11-12 09:34:21 +0000889 /* truncate file */
890 if (disk_i_size > i_size) {
891 BTRFS_I(inode)->disk_i_size = i_size;
892 ret = 0;
893 goto out;
894 }
895
Chris Masondbe674a2008-07-17 12:54:05 -0400896 /*
897 * if the disk i_size is already at the inode->i_size, or
898 * this ordered extent is inside the disk i_size, we're done
899 */
Josef Bacik5d1f4022013-01-30 14:17:31 -0500900 if (disk_i_size == i_size)
Chris Masondbe674a2008-07-17 12:54:05 -0400901 goto out;
Josef Bacik5d1f4022013-01-30 14:17:31 -0500902
903 /*
904 * We still need to update disk_i_size if outstanding_isize is greater
905 * than disk_i_size.
906 */
907 if (offset <= disk_i_size &&
908 (!ordered || ordered->outstanding_isize <= disk_i_size))
909 goto out;
Chris Masondbe674a2008-07-17 12:54:05 -0400910
911 /*
Chris Masondbe674a2008-07-17 12:54:05 -0400912 * walk backward from this ordered extent to disk_i_size.
913 * if we find an ordered extent then we can't update disk i_size
914 * yet
915 */
Yan, Zhengc2167752009-11-12 09:34:21 +0000916 if (ordered) {
917 node = rb_prev(&ordered->rb_node);
918 } else {
919 prev = tree_search(tree, offset);
920 /*
921 * we insert file extents without involving ordered struct,
922 * so there should be no ordered struct cover this offset
923 */
924 if (prev) {
925 test = rb_entry(prev, struct btrfs_ordered_extent,
926 rb_node);
927 BUG_ON(offset_in_entry(test, offset));
928 }
929 node = prev;
930 }
Josef Bacik5fd02042012-05-02 14:00:54 -0400931 for (; node; node = rb_prev(node)) {
Chris Masondbe674a2008-07-17 12:54:05 -0400932 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
Josef Bacik5fd02042012-05-02 14:00:54 -0400933
934 /* We treat this entry as if it doesnt exist */
935 if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
936 continue;
Chris Masondbe674a2008-07-17 12:54:05 -0400937 if (test->file_offset + test->len <= disk_i_size)
938 break;
Yan, Zhengc2167752009-11-12 09:34:21 +0000939 if (test->file_offset >= i_size)
Chris Masondbe674a2008-07-17 12:54:05 -0400940 break;
Josef Bacik59fe4f42013-01-30 14:31:31 -0500941 if (entry_end(test) > disk_i_size) {
Miao Xieb9a8cc52012-09-06 04:01:21 -0600942 /*
943 * we don't update disk_i_size now, so record this
944 * undealt i_size. Or we will not know the real
945 * i_size.
946 */
947 if (test->outstanding_isize < offset)
948 test->outstanding_isize = offset;
949 if (ordered &&
950 ordered->outstanding_isize >
951 test->outstanding_isize)
952 test->outstanding_isize =
953 ordered->outstanding_isize;
Chris Masondbe674a2008-07-17 12:54:05 -0400954 goto out;
Miao Xieb9a8cc52012-09-06 04:01:21 -0600955 }
Chris Masondbe674a2008-07-17 12:54:05 -0400956 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000957 new_i_size = min_t(u64, offset, i_size);
Chris Masondbe674a2008-07-17 12:54:05 -0400958
959 /*
Miao Xieb9a8cc52012-09-06 04:01:21 -0600960 * Some ordered extents may completed before the current one, and
961 * we hold the real i_size in ->outstanding_isize.
Chris Masondbe674a2008-07-17 12:54:05 -0400962 */
Miao Xieb9a8cc52012-09-06 04:01:21 -0600963 if (ordered && ordered->outstanding_isize > new_i_size)
964 new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
Chris Masondbe674a2008-07-17 12:54:05 -0400965 BTRFS_I(inode)->disk_i_size = new_i_size;
Yan, Zhengc2167752009-11-12 09:34:21 +0000966 ret = 0;
Chris Masondbe674a2008-07-17 12:54:05 -0400967out:
Yan, Zhengc2167752009-11-12 09:34:21 +0000968 /*
Josef Bacik5fd02042012-05-02 14:00:54 -0400969 * We need to do this because we can't remove ordered extents until
970 * after the i_disk_size has been updated and then the inode has been
971 * updated to reflect the change, so we need to tell anybody who finds
972 * this ordered extent that we've already done all the real work, we
973 * just haven't completed all the other work.
Yan, Zhengc2167752009-11-12 09:34:21 +0000974 */
975 if (ordered)
Josef Bacik5fd02042012-05-02 14:00:54 -0400976 set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
977 spin_unlock_irq(&tree->lock);
Yan, Zhengc2167752009-11-12 09:34:21 +0000978 return ret;
Chris Masondbe674a2008-07-17 12:54:05 -0400979}
Chris Masonba1da2f2008-07-17 12:54:15 -0400980
Chris Masoneb84ae02008-07-17 13:53:27 -0400981/*
982 * search the ordered extents for one corresponding to 'offset' and
983 * try to find a checksum. This is used because we allow pages to
984 * be reclaimed before their checksum is actually put into the btree
985 */
Chris Masond20f7042008-12-08 16:58:54 -0500986int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
987 u32 *sum)
Chris Masonba1da2f2008-07-17 12:54:15 -0400988{
989 struct btrfs_ordered_sum *ordered_sum;
990 struct btrfs_sector_sum *sector_sums;
991 struct btrfs_ordered_extent *ordered;
992 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
Chris Mason3edf7d32008-07-18 06:17:13 -0400993 unsigned long num_sectors;
994 unsigned long i;
995 u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
Chris Masonba1da2f2008-07-17 12:54:15 -0400996 int ret = 1;
Chris Masonba1da2f2008-07-17 12:54:15 -0400997
998 ordered = btrfs_lookup_ordered_extent(inode, offset);
999 if (!ordered)
1000 return 1;
1001
Josef Bacik5fd02042012-05-02 14:00:54 -04001002 spin_lock_irq(&tree->lock);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001003 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001004 if (disk_bytenr >= ordered_sum->bytenr) {
Chris Mason3edf7d32008-07-18 06:17:13 -04001005 num_sectors = ordered_sum->len / sectorsize;
Chris Masoned98b562008-07-22 23:06:42 -04001006 sector_sums = ordered_sum->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -04001007 for (i = 0; i < num_sectors; i++) {
Chris Masond20f7042008-12-08 16:58:54 -05001008 if (sector_sums[i].bytenr == disk_bytenr) {
Chris Mason3edf7d32008-07-18 06:17:13 -04001009 *sum = sector_sums[i].sum;
1010 ret = 0;
1011 goto out;
1012 }
1013 }
Chris Masonba1da2f2008-07-17 12:54:15 -04001014 }
1015 }
1016out:
Josef Bacik5fd02042012-05-02 14:00:54 -04001017 spin_unlock_irq(&tree->lock);
Chris Mason89642222008-07-24 09:41:53 -04001018 btrfs_put_ordered_extent(ordered);
Chris Masonba1da2f2008-07-17 12:54:15 -04001019 return ret;
1020}
1021
Chris Masonf4219502008-07-22 11:18:09 -04001022
Chris Mason5a3f23d2009-03-31 13:27:11 -04001023/*
1024 * add a given inode to the list of inodes that must be fully on
1025 * disk before a transaction commit finishes.
1026 *
1027 * This basically gives us the ext3 style data=ordered mode, and it is mostly
1028 * used to make sure renamed files are fully on disk.
1029 *
1030 * It is a noop if the inode is already fully on disk.
1031 *
1032 * If trans is not null, we'll do a friendly check for a transaction that
1033 * is already flushing things and force the IO down ourselves.
1034 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001035void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
1036 struct btrfs_root *root, struct inode *inode)
Chris Mason5a3f23d2009-03-31 13:27:11 -04001037{
Josef Bacik569e0f32013-02-13 11:09:14 -05001038 struct btrfs_transaction *cur_trans = trans->transaction;
Chris Mason5a3f23d2009-03-31 13:27:11 -04001039 u64 last_mod;
1040
1041 last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
1042
1043 /*
1044 * if this file hasn't been changed since the last transaction
1045 * commit, we can safely return without doing anything
1046 */
1047 if (last_mod < root->fs_info->last_trans_committed)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001048 return;
Chris Mason5a3f23d2009-03-31 13:27:11 -04001049
Chris Mason5a3f23d2009-03-31 13:27:11 -04001050 spin_lock(&root->fs_info->ordered_extent_lock);
1051 if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
1052 list_add_tail(&BTRFS_I(inode)->ordered_operations,
Josef Bacik569e0f32013-02-13 11:09:14 -05001053 &cur_trans->ordered_operations);
Chris Mason5a3f23d2009-03-31 13:27:11 -04001054 }
1055 spin_unlock(&root->fs_info->ordered_extent_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04001056}
Miao Xie6352b912012-09-06 04:01:51 -06001057
1058int __init ordered_data_init(void)
1059{
1060 btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
1061 sizeof(struct btrfs_ordered_extent), 0,
1062 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
1063 NULL);
1064 if (!btrfs_ordered_extent_cache)
1065 return -ENOMEM;
Miao Xie25287e02012-10-25 09:31:03 +00001066
Miao Xie6352b912012-09-06 04:01:51 -06001067 return 0;
1068}
1069
1070void ordered_data_exit(void)
1071{
1072 if (btrfs_ordered_extent_cache)
1073 kmem_cache_destroy(btrfs_ordered_extent_cache);
1074}