Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
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 | ||
79154b1b | 19 | #include <linux/fs.h> |
34088780 | 20 | #include <linux/sched.h> |
d3c2fdcf | 21 | #include <linux/writeback.h> |
5f39d397 | 22 | #include <linux/pagemap.h> |
5f2cc086 | 23 | #include <linux/blkdev.h> |
79154b1b CM |
24 | #include "ctree.h" |
25 | #include "disk-io.h" | |
26 | #include "transaction.h" | |
925baedd | 27 | #include "locking.h" |
31153d81 | 28 | #include "ref-cache.h" |
e02119d5 | 29 | #include "tree-log.h" |
79154b1b | 30 | |
0f7d52f4 CM |
31 | #define BTRFS_ROOT_TRANS_TAG 0 |
32 | ||
80b6794d | 33 | static noinline void put_transaction(struct btrfs_transaction *transaction) |
79154b1b | 34 | { |
2c90e5d6 | 35 | WARN_ON(transaction->use_count == 0); |
79154b1b | 36 | transaction->use_count--; |
78fae27e | 37 | if (transaction->use_count == 0) { |
8fd17795 | 38 | list_del_init(&transaction->list); |
2c90e5d6 CM |
39 | memset(transaction, 0, sizeof(*transaction)); |
40 | kmem_cache_free(btrfs_transaction_cachep, transaction); | |
78fae27e | 41 | } |
79154b1b CM |
42 | } |
43 | ||
d352ac68 CM |
44 | /* |
45 | * either allocate a new transaction or hop into the existing one | |
46 | */ | |
80b6794d | 47 | static noinline int join_transaction(struct btrfs_root *root) |
79154b1b CM |
48 | { |
49 | struct btrfs_transaction *cur_trans; | |
50 | cur_trans = root->fs_info->running_transaction; | |
51 | if (!cur_trans) { | |
2c90e5d6 CM |
52 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
53 | GFP_NOFS); | |
79154b1b | 54 | BUG_ON(!cur_trans); |
0f7d52f4 | 55 | root->fs_info->generation++; |
15ee9bc7 JB |
56 | cur_trans->num_writers = 1; |
57 | cur_trans->num_joined = 0; | |
0f7d52f4 | 58 | cur_trans->transid = root->fs_info->generation; |
79154b1b CM |
59 | init_waitqueue_head(&cur_trans->writer_wait); |
60 | init_waitqueue_head(&cur_trans->commit_wait); | |
61 | cur_trans->in_commit = 0; | |
f9295749 | 62 | cur_trans->blocked = 0; |
d5719762 | 63 | cur_trans->use_count = 1; |
79154b1b | 64 | cur_trans->commit_done = 0; |
08607c1b | 65 | cur_trans->start_time = get_seconds(); |
56bec294 CM |
66 | |
67 | cur_trans->delayed_refs.root.rb_node = NULL; | |
68 | cur_trans->delayed_refs.num_entries = 0; | |
c3e69d58 CM |
69 | cur_trans->delayed_refs.num_heads_ready = 0; |
70 | cur_trans->delayed_refs.num_heads = 0; | |
56bec294 | 71 | cur_trans->delayed_refs.flushing = 0; |
c3e69d58 | 72 | cur_trans->delayed_refs.run_delayed_start = 0; |
56bec294 CM |
73 | spin_lock_init(&cur_trans->delayed_refs.lock); |
74 | ||
3063d29f | 75 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
8fd17795 | 76 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
d1310b2e | 77 | extent_io_tree_init(&cur_trans->dirty_pages, |
5f39d397 CM |
78 | root->fs_info->btree_inode->i_mapping, |
79 | GFP_NOFS); | |
48ec2cf8 CM |
80 | spin_lock(&root->fs_info->new_trans_lock); |
81 | root->fs_info->running_transaction = cur_trans; | |
82 | spin_unlock(&root->fs_info->new_trans_lock); | |
15ee9bc7 JB |
83 | } else { |
84 | cur_trans->num_writers++; | |
85 | cur_trans->num_joined++; | |
79154b1b | 86 | } |
15ee9bc7 | 87 | |
79154b1b CM |
88 | return 0; |
89 | } | |
90 | ||
d352ac68 | 91 | /* |
d397712b CM |
92 | * this does all the record keeping required to make sure that a reference |
93 | * counted root is properly recorded in a given transaction. This is required | |
94 | * to make sure the old root from before we joined the transaction is deleted | |
95 | * when the transaction commits | |
d352ac68 | 96 | */ |
e02119d5 | 97 | noinline int btrfs_record_root_in_trans(struct btrfs_root *root) |
6702ed49 | 98 | { |
f321e491 | 99 | struct btrfs_dirty_root *dirty; |
6702ed49 CM |
100 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
101 | if (root->ref_cows && root->last_trans < running_trans_id) { | |
102 | WARN_ON(root == root->fs_info->extent_root); | |
103 | if (root->root_item.refs != 0) { | |
104 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, | |
105 | (unsigned long)root->root_key.objectid, | |
106 | BTRFS_ROOT_TRANS_TAG); | |
31153d81 YZ |
107 | |
108 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
109 | BUG_ON(!dirty); | |
110 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); | |
111 | BUG_ON(!dirty->root); | |
31153d81 YZ |
112 | dirty->latest_root = root; |
113 | INIT_LIST_HEAD(&dirty->list); | |
31153d81 | 114 | |
925baedd | 115 | root->commit_root = btrfs_root_node(root); |
31153d81 YZ |
116 | |
117 | memcpy(dirty->root, root, sizeof(*root)); | |
118 | spin_lock_init(&dirty->root->node_lock); | |
bcc63abb | 119 | spin_lock_init(&dirty->root->list_lock); |
31153d81 | 120 | mutex_init(&dirty->root->objectid_mutex); |
5b21f2ed | 121 | mutex_init(&dirty->root->log_mutex); |
bcc63abb | 122 | INIT_LIST_HEAD(&dirty->root->dead_list); |
31153d81 YZ |
123 | dirty->root->node = root->commit_root; |
124 | dirty->root->commit_root = NULL; | |
bcc63abb Y |
125 | |
126 | spin_lock(&root->list_lock); | |
127 | list_add(&dirty->root->dead_list, &root->dead_list); | |
128 | spin_unlock(&root->list_lock); | |
129 | ||
130 | root->dirty_root = dirty; | |
6702ed49 CM |
131 | } else { |
132 | WARN_ON(1); | |
133 | } | |
134 | root->last_trans = running_trans_id; | |
135 | } | |
136 | return 0; | |
137 | } | |
138 | ||
d352ac68 CM |
139 | /* wait for commit against the current transaction to become unblocked |
140 | * when this is done, it is safe to start a new transaction, but the current | |
141 | * transaction might not be fully on disk. | |
142 | */ | |
37d1aeee | 143 | static void wait_current_trans(struct btrfs_root *root) |
79154b1b | 144 | { |
f9295749 | 145 | struct btrfs_transaction *cur_trans; |
79154b1b | 146 | |
f9295749 | 147 | cur_trans = root->fs_info->running_transaction; |
37d1aeee | 148 | if (cur_trans && cur_trans->blocked) { |
f9295749 CM |
149 | DEFINE_WAIT(wait); |
150 | cur_trans->use_count++; | |
d397712b | 151 | while (1) { |
f9295749 CM |
152 | prepare_to_wait(&root->fs_info->transaction_wait, &wait, |
153 | TASK_UNINTERRUPTIBLE); | |
154 | if (cur_trans->blocked) { | |
155 | mutex_unlock(&root->fs_info->trans_mutex); | |
156 | schedule(); | |
157 | mutex_lock(&root->fs_info->trans_mutex); | |
158 | finish_wait(&root->fs_info->transaction_wait, | |
159 | &wait); | |
160 | } else { | |
161 | finish_wait(&root->fs_info->transaction_wait, | |
162 | &wait); | |
163 | break; | |
164 | } | |
165 | } | |
166 | put_transaction(cur_trans); | |
167 | } | |
37d1aeee CM |
168 | } |
169 | ||
e02119d5 | 170 | static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, |
9ca9ee09 | 171 | int num_blocks, int wait) |
37d1aeee CM |
172 | { |
173 | struct btrfs_trans_handle *h = | |
174 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); | |
175 | int ret; | |
176 | ||
177 | mutex_lock(&root->fs_info->trans_mutex); | |
4bef0848 CM |
178 | if (!root->fs_info->log_root_recovering && |
179 | ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) | |
37d1aeee | 180 | wait_current_trans(root); |
79154b1b CM |
181 | ret = join_transaction(root); |
182 | BUG_ON(ret); | |
0f7d52f4 | 183 | |
e02119d5 | 184 | btrfs_record_root_in_trans(root); |
6702ed49 | 185 | h->transid = root->fs_info->running_transaction->transid; |
79154b1b CM |
186 | h->transaction = root->fs_info->running_transaction; |
187 | h->blocks_reserved = num_blocks; | |
188 | h->blocks_used = 0; | |
d2fb3437 | 189 | h->block_group = 0; |
26b8003f CM |
190 | h->alloc_exclude_nr = 0; |
191 | h->alloc_exclude_start = 0; | |
56bec294 | 192 | h->delayed_ref_updates = 0; |
b7ec40d7 | 193 | |
79154b1b CM |
194 | root->fs_info->running_transaction->use_count++; |
195 | mutex_unlock(&root->fs_info->trans_mutex); | |
196 | return h; | |
197 | } | |
198 | ||
f9295749 CM |
199 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
200 | int num_blocks) | |
201 | { | |
9ca9ee09 | 202 | return start_transaction(root, num_blocks, 1); |
f9295749 CM |
203 | } |
204 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, | |
205 | int num_blocks) | |
206 | { | |
9ca9ee09 | 207 | return start_transaction(root, num_blocks, 0); |
f9295749 CM |
208 | } |
209 | ||
9ca9ee09 SW |
210 | struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, |
211 | int num_blocks) | |
212 | { | |
213 | return start_transaction(r, num_blocks, 2); | |
214 | } | |
215 | ||
d352ac68 | 216 | /* wait for a transaction commit to be fully complete */ |
89ce8a63 CM |
217 | static noinline int wait_for_commit(struct btrfs_root *root, |
218 | struct btrfs_transaction *commit) | |
219 | { | |
220 | DEFINE_WAIT(wait); | |
221 | mutex_lock(&root->fs_info->trans_mutex); | |
d397712b | 222 | while (!commit->commit_done) { |
89ce8a63 CM |
223 | prepare_to_wait(&commit->commit_wait, &wait, |
224 | TASK_UNINTERRUPTIBLE); | |
225 | if (commit->commit_done) | |
226 | break; | |
227 | mutex_unlock(&root->fs_info->trans_mutex); | |
228 | schedule(); | |
229 | mutex_lock(&root->fs_info->trans_mutex); | |
230 | } | |
231 | mutex_unlock(&root->fs_info->trans_mutex); | |
232 | finish_wait(&commit->commit_wait, &wait); | |
233 | return 0; | |
234 | } | |
235 | ||
d352ac68 | 236 | /* |
d397712b CM |
237 | * rate limit against the drop_snapshot code. This helps to slow down new |
238 | * operations if the drop_snapshot code isn't able to keep up. | |
d352ac68 | 239 | */ |
37d1aeee | 240 | static void throttle_on_drops(struct btrfs_root *root) |
ab78c84d CM |
241 | { |
242 | struct btrfs_fs_info *info = root->fs_info; | |
2dd3e67b | 243 | int harder_count = 0; |
ab78c84d | 244 | |
2dd3e67b | 245 | harder: |
ab78c84d CM |
246 | if (atomic_read(&info->throttles)) { |
247 | DEFINE_WAIT(wait); | |
248 | int thr; | |
ab78c84d CM |
249 | thr = atomic_read(&info->throttle_gen); |
250 | ||
251 | do { | |
252 | prepare_to_wait(&info->transaction_throttle, | |
253 | &wait, TASK_UNINTERRUPTIBLE); | |
254 | if (!atomic_read(&info->throttles)) { | |
255 | finish_wait(&info->transaction_throttle, &wait); | |
256 | break; | |
257 | } | |
258 | schedule(); | |
259 | finish_wait(&info->transaction_throttle, &wait); | |
260 | } while (thr == atomic_read(&info->throttle_gen)); | |
2dd3e67b CM |
261 | harder_count++; |
262 | ||
263 | if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && | |
264 | harder_count < 2) | |
265 | goto harder; | |
266 | ||
267 | if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && | |
268 | harder_count < 10) | |
269 | goto harder; | |
270 | ||
271 | if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && | |
272 | harder_count < 20) | |
273 | goto harder; | |
ab78c84d CM |
274 | } |
275 | } | |
276 | ||
37d1aeee CM |
277 | void btrfs_throttle(struct btrfs_root *root) |
278 | { | |
279 | mutex_lock(&root->fs_info->trans_mutex); | |
9ca9ee09 SW |
280 | if (!root->fs_info->open_ioctl_trans) |
281 | wait_current_trans(root); | |
37d1aeee | 282 | mutex_unlock(&root->fs_info->trans_mutex); |
37d1aeee CM |
283 | throttle_on_drops(root); |
284 | } | |
285 | ||
89ce8a63 CM |
286 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
287 | struct btrfs_root *root, int throttle) | |
79154b1b CM |
288 | { |
289 | struct btrfs_transaction *cur_trans; | |
ab78c84d | 290 | struct btrfs_fs_info *info = root->fs_info; |
c3e69d58 CM |
291 | int count = 0; |
292 | ||
293 | while (count < 4) { | |
294 | unsigned long cur = trans->delayed_ref_updates; | |
295 | trans->delayed_ref_updates = 0; | |
296 | if (cur && | |
297 | trans->transaction->delayed_refs.num_heads_ready > 64) { | |
298 | trans->delayed_ref_updates = 0; | |
b7ec40d7 CM |
299 | |
300 | /* | |
301 | * do a full flush if the transaction is trying | |
302 | * to close | |
303 | */ | |
304 | if (trans->transaction->delayed_refs.flushing) | |
305 | cur = 0; | |
c3e69d58 CM |
306 | btrfs_run_delayed_refs(trans, root, cur); |
307 | } else { | |
308 | break; | |
309 | } | |
310 | count++; | |
56bec294 CM |
311 | } |
312 | ||
ab78c84d CM |
313 | mutex_lock(&info->trans_mutex); |
314 | cur_trans = info->running_transaction; | |
ccd467d6 | 315 | WARN_ON(cur_trans != trans->transaction); |
d5719762 | 316 | WARN_ON(cur_trans->num_writers < 1); |
ccd467d6 | 317 | cur_trans->num_writers--; |
89ce8a63 | 318 | |
79154b1b CM |
319 | if (waitqueue_active(&cur_trans->writer_wait)) |
320 | wake_up(&cur_trans->writer_wait); | |
79154b1b | 321 | put_transaction(cur_trans); |
ab78c84d | 322 | mutex_unlock(&info->trans_mutex); |
d6025579 | 323 | memset(trans, 0, sizeof(*trans)); |
2c90e5d6 | 324 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
ab78c84d CM |
325 | |
326 | if (throttle) | |
37d1aeee | 327 | throttle_on_drops(root); |
ab78c84d | 328 | |
79154b1b CM |
329 | return 0; |
330 | } | |
331 | ||
89ce8a63 CM |
332 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
333 | struct btrfs_root *root) | |
334 | { | |
335 | return __btrfs_end_transaction(trans, root, 0); | |
336 | } | |
337 | ||
338 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, | |
339 | struct btrfs_root *root) | |
340 | { | |
341 | return __btrfs_end_transaction(trans, root, 1); | |
342 | } | |
343 | ||
d352ac68 CM |
344 | /* |
345 | * when btree blocks are allocated, they have some corresponding bits set for | |
346 | * them in one of two extent_io trees. This is used to make sure all of | |
347 | * those extents are on disk for transaction or log commit | |
348 | */ | |
d0c803c4 CM |
349 | int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, |
350 | struct extent_io_tree *dirty_pages) | |
79154b1b | 351 | { |
7c4452b9 | 352 | int ret; |
777e6bd7 | 353 | int err = 0; |
7c4452b9 CM |
354 | int werr = 0; |
355 | struct page *page; | |
7c4452b9 | 356 | struct inode *btree_inode = root->fs_info->btree_inode; |
777e6bd7 | 357 | u64 start = 0; |
5f39d397 CM |
358 | u64 end; |
359 | unsigned long index; | |
7c4452b9 | 360 | |
d397712b | 361 | while (1) { |
777e6bd7 | 362 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, |
5f39d397 CM |
363 | EXTENT_DIRTY); |
364 | if (ret) | |
7c4452b9 | 365 | break; |
d397712b | 366 | while (start <= end) { |
777e6bd7 CM |
367 | cond_resched(); |
368 | ||
5f39d397 | 369 | index = start >> PAGE_CACHE_SHIFT; |
35ebb934 | 370 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
4bef0848 | 371 | page = find_get_page(btree_inode->i_mapping, index); |
7c4452b9 CM |
372 | if (!page) |
373 | continue; | |
4bef0848 CM |
374 | |
375 | btree_lock_page_hook(page); | |
376 | if (!page->mapping) { | |
377 | unlock_page(page); | |
378 | page_cache_release(page); | |
379 | continue; | |
380 | } | |
381 | ||
6702ed49 CM |
382 | if (PageWriteback(page)) { |
383 | if (PageDirty(page)) | |
384 | wait_on_page_writeback(page); | |
385 | else { | |
386 | unlock_page(page); | |
387 | page_cache_release(page); | |
388 | continue; | |
389 | } | |
390 | } | |
7c4452b9 CM |
391 | err = write_one_page(page, 0); |
392 | if (err) | |
393 | werr = err; | |
394 | page_cache_release(page); | |
395 | } | |
396 | } | |
d397712b | 397 | while (1) { |
777e6bd7 CM |
398 | ret = find_first_extent_bit(dirty_pages, 0, &start, &end, |
399 | EXTENT_DIRTY); | |
400 | if (ret) | |
401 | break; | |
402 | ||
403 | clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); | |
d397712b | 404 | while (start <= end) { |
777e6bd7 CM |
405 | index = start >> PAGE_CACHE_SHIFT; |
406 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; | |
407 | page = find_get_page(btree_inode->i_mapping, index); | |
408 | if (!page) | |
409 | continue; | |
410 | if (PageDirty(page)) { | |
4bef0848 CM |
411 | btree_lock_page_hook(page); |
412 | wait_on_page_writeback(page); | |
777e6bd7 CM |
413 | err = write_one_page(page, 0); |
414 | if (err) | |
415 | werr = err; | |
416 | } | |
105d931d | 417 | wait_on_page_writeback(page); |
777e6bd7 CM |
418 | page_cache_release(page); |
419 | cond_resched(); | |
420 | } | |
421 | } | |
7c4452b9 CM |
422 | if (err) |
423 | werr = err; | |
424 | return werr; | |
79154b1b CM |
425 | } |
426 | ||
d0c803c4 CM |
427 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
428 | struct btrfs_root *root) | |
429 | { | |
430 | if (!trans || !trans->transaction) { | |
431 | struct inode *btree_inode; | |
432 | btree_inode = root->fs_info->btree_inode; | |
433 | return filemap_write_and_wait(btree_inode->i_mapping); | |
434 | } | |
435 | return btrfs_write_and_wait_marked_extents(root, | |
436 | &trans->transaction->dirty_pages); | |
437 | } | |
438 | ||
d352ac68 CM |
439 | /* |
440 | * this is used to update the root pointer in the tree of tree roots. | |
441 | * | |
442 | * But, in the case of the extent allocation tree, updating the root | |
443 | * pointer may allocate blocks which may change the root of the extent | |
444 | * allocation tree. | |
445 | * | |
446 | * So, this loops and repeats and makes sure the cowonly root didn't | |
447 | * change while the root pointer was being updated in the metadata. | |
448 | */ | |
0b86a832 CM |
449 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
450 | struct btrfs_root *root) | |
79154b1b CM |
451 | { |
452 | int ret; | |
0b86a832 CM |
453 | u64 old_root_bytenr; |
454 | struct btrfs_root *tree_root = root->fs_info->tree_root; | |
79154b1b | 455 | |
0b86a832 | 456 | btrfs_write_dirty_block_groups(trans, root); |
56bec294 CM |
457 | |
458 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); | |
459 | BUG_ON(ret); | |
87ef2bb4 | 460 | |
d397712b | 461 | while (1) { |
0b86a832 CM |
462 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
463 | if (old_root_bytenr == root->node->start) | |
79154b1b | 464 | break; |
0b86a832 CM |
465 | btrfs_set_root_bytenr(&root->root_item, |
466 | root->node->start); | |
467 | btrfs_set_root_level(&root->root_item, | |
468 | btrfs_header_level(root->node)); | |
84234f3a | 469 | btrfs_set_root_generation(&root->root_item, trans->transid); |
87ef2bb4 | 470 | |
79154b1b | 471 | ret = btrfs_update_root(trans, tree_root, |
0b86a832 CM |
472 | &root->root_key, |
473 | &root->root_item); | |
79154b1b | 474 | BUG_ON(ret); |
0b86a832 | 475 | btrfs_write_dirty_block_groups(trans, root); |
56bec294 CM |
476 | |
477 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); | |
478 | BUG_ON(ret); | |
0b86a832 CM |
479 | } |
480 | return 0; | |
481 | } | |
482 | ||
d352ac68 CM |
483 | /* |
484 | * update all the cowonly tree roots on disk | |
485 | */ | |
0b86a832 CM |
486 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
487 | struct btrfs_root *root) | |
488 | { | |
489 | struct btrfs_fs_info *fs_info = root->fs_info; | |
490 | struct list_head *next; | |
84234f3a | 491 | struct extent_buffer *eb; |
56bec294 | 492 | int ret; |
84234f3a | 493 | |
56bec294 CM |
494 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
495 | BUG_ON(ret); | |
87ef2bb4 | 496 | |
84234f3a | 497 | eb = btrfs_lock_root_node(fs_info->tree_root); |
9fa8cfe7 | 498 | btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); |
84234f3a YZ |
499 | btrfs_tree_unlock(eb); |
500 | free_extent_buffer(eb); | |
0b86a832 | 501 | |
56bec294 CM |
502 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
503 | BUG_ON(ret); | |
87ef2bb4 | 504 | |
d397712b | 505 | while (!list_empty(&fs_info->dirty_cowonly_roots)) { |
0b86a832 CM |
506 | next = fs_info->dirty_cowonly_roots.next; |
507 | list_del_init(next); | |
508 | root = list_entry(next, struct btrfs_root, dirty_list); | |
87ef2bb4 | 509 | |
0b86a832 | 510 | update_cowonly_root(trans, root); |
56bec294 CM |
511 | |
512 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); | |
513 | BUG_ON(ret); | |
79154b1b CM |
514 | } |
515 | return 0; | |
516 | } | |
517 | ||
d352ac68 CM |
518 | /* |
519 | * dead roots are old snapshots that need to be deleted. This allocates | |
520 | * a dirty root struct and adds it into the list of dead roots that need to | |
521 | * be deleted | |
522 | */ | |
b48652c1 | 523 | int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) |
5eda7b5e | 524 | { |
f321e491 | 525 | struct btrfs_dirty_root *dirty; |
5eda7b5e CM |
526 | |
527 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
528 | if (!dirty) | |
529 | return -ENOMEM; | |
5eda7b5e | 530 | dirty->root = root; |
5ce14bbc | 531 | dirty->latest_root = latest; |
b48652c1 YZ |
532 | |
533 | mutex_lock(&root->fs_info->trans_mutex); | |
534 | list_add(&dirty->list, &latest->fs_info->dead_roots); | |
535 | mutex_unlock(&root->fs_info->trans_mutex); | |
5eda7b5e CM |
536 | return 0; |
537 | } | |
538 | ||
d352ac68 CM |
539 | /* |
540 | * at transaction commit time we need to schedule the old roots for | |
541 | * deletion via btrfs_drop_snapshot. This runs through all the | |
542 | * reference counted roots that were modified in the current | |
543 | * transaction and puts them into the drop list | |
544 | */ | |
80b6794d CM |
545 | static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, |
546 | struct radix_tree_root *radix, | |
547 | struct list_head *list) | |
0f7d52f4 | 548 | { |
f321e491 | 549 | struct btrfs_dirty_root *dirty; |
0f7d52f4 CM |
550 | struct btrfs_root *gang[8]; |
551 | struct btrfs_root *root; | |
552 | int i; | |
553 | int ret; | |
54aa1f4d | 554 | int err = 0; |
5eda7b5e | 555 | u32 refs; |
54aa1f4d | 556 | |
d397712b | 557 | while (1) { |
0f7d52f4 CM |
558 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
559 | ARRAY_SIZE(gang), | |
560 | BTRFS_ROOT_TRANS_TAG); | |
561 | if (ret == 0) | |
562 | break; | |
563 | for (i = 0; i < ret; i++) { | |
564 | root = gang[i]; | |
2619ba1f CM |
565 | radix_tree_tag_clear(radix, |
566 | (unsigned long)root->root_key.objectid, | |
567 | BTRFS_ROOT_TRANS_TAG); | |
31153d81 YZ |
568 | |
569 | BUG_ON(!root->ref_tree); | |
017e5369 | 570 | dirty = root->dirty_root; |
31153d81 | 571 | |
e02119d5 | 572 | btrfs_free_log(trans, root); |
f82d02d9 | 573 | btrfs_free_reloc_root(trans, root); |
e02119d5 | 574 | |
0f7d52f4 | 575 | if (root->commit_root == root->node) { |
db94535d CM |
576 | WARN_ON(root->node->start != |
577 | btrfs_root_bytenr(&root->root_item)); | |
31153d81 | 578 | |
5f39d397 | 579 | free_extent_buffer(root->commit_root); |
0f7d52f4 | 580 | root->commit_root = NULL; |
7ea394f1 | 581 | root->dirty_root = NULL; |
bcc63abb Y |
582 | |
583 | spin_lock(&root->list_lock); | |
584 | list_del_init(&dirty->root->dead_list); | |
585 | spin_unlock(&root->list_lock); | |
586 | ||
31153d81 YZ |
587 | kfree(dirty->root); |
588 | kfree(dirty); | |
58176a96 JB |
589 | |
590 | /* make sure to update the root on disk | |
591 | * so we get any updates to the block used | |
592 | * counts | |
593 | */ | |
594 | err = btrfs_update_root(trans, | |
595 | root->fs_info->tree_root, | |
596 | &root->root_key, | |
597 | &root->root_item); | |
0f7d52f4 CM |
598 | continue; |
599 | } | |
9f3a7427 CM |
600 | |
601 | memset(&root->root_item.drop_progress, 0, | |
602 | sizeof(struct btrfs_disk_key)); | |
603 | root->root_item.drop_level = 0; | |
0f7d52f4 | 604 | root->commit_root = NULL; |
7ea394f1 | 605 | root->dirty_root = NULL; |
0f7d52f4 | 606 | root->root_key.offset = root->fs_info->generation; |
db94535d CM |
607 | btrfs_set_root_bytenr(&root->root_item, |
608 | root->node->start); | |
609 | btrfs_set_root_level(&root->root_item, | |
610 | btrfs_header_level(root->node)); | |
84234f3a YZ |
611 | btrfs_set_root_generation(&root->root_item, |
612 | root->root_key.offset); | |
613 | ||
0f7d52f4 CM |
614 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
615 | &root->root_key, | |
616 | &root->root_item); | |
54aa1f4d CM |
617 | if (err) |
618 | break; | |
9f3a7427 CM |
619 | |
620 | refs = btrfs_root_refs(&dirty->root->root_item); | |
621 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); | |
5eda7b5e | 622 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
9f3a7427 CM |
623 | &dirty->root->root_key, |
624 | &dirty->root->root_item); | |
5eda7b5e CM |
625 | |
626 | BUG_ON(err); | |
9f3a7427 | 627 | if (refs == 1) { |
5eda7b5e | 628 | list_add(&dirty->list, list); |
9f3a7427 CM |
629 | } else { |
630 | WARN_ON(1); | |
31153d81 | 631 | free_extent_buffer(dirty->root->node); |
9f3a7427 | 632 | kfree(dirty->root); |
5eda7b5e | 633 | kfree(dirty); |
9f3a7427 | 634 | } |
0f7d52f4 CM |
635 | } |
636 | } | |
54aa1f4d | 637 | return err; |
0f7d52f4 CM |
638 | } |
639 | ||
d352ac68 CM |
640 | /* |
641 | * defrag a given btree. If cacheonly == 1, this won't read from the disk, | |
642 | * otherwise every leaf in the btree is read and defragged. | |
643 | */ | |
e9d0b13b CM |
644 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
645 | { | |
646 | struct btrfs_fs_info *info = root->fs_info; | |
647 | int ret; | |
648 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 649 | unsigned long nr; |
e9d0b13b | 650 | |
a2135011 | 651 | smp_mb(); |
e9d0b13b CM |
652 | if (root->defrag_running) |
653 | return 0; | |
e9d0b13b | 654 | trans = btrfs_start_transaction(root, 1); |
6b80053d | 655 | while (1) { |
e9d0b13b CM |
656 | root->defrag_running = 1; |
657 | ret = btrfs_defrag_leaves(trans, root, cacheonly); | |
d3c2fdcf | 658 | nr = trans->blocks_used; |
e9d0b13b | 659 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 660 | btrfs_btree_balance_dirty(info->tree_root, nr); |
e9d0b13b CM |
661 | cond_resched(); |
662 | ||
e9d0b13b | 663 | trans = btrfs_start_transaction(root, 1); |
3f157a2f | 664 | if (root->fs_info->closing || ret != -EAGAIN) |
e9d0b13b CM |
665 | break; |
666 | } | |
667 | root->defrag_running = 0; | |
a2135011 | 668 | smp_mb(); |
e9d0b13b CM |
669 | btrfs_end_transaction(trans, root); |
670 | return 0; | |
671 | } | |
672 | ||
b7ec40d7 CM |
673 | /* |
674 | * when dropping snapshots, we generate a ton of delayed refs, and it makes | |
675 | * sense not to join the transaction while it is trying to flush the current | |
676 | * queue of delayed refs out. | |
677 | * | |
678 | * This is used by the drop snapshot code only | |
679 | */ | |
680 | static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info) | |
681 | { | |
682 | DEFINE_WAIT(wait); | |
683 | ||
684 | mutex_lock(&info->trans_mutex); | |
685 | while (info->running_transaction && | |
686 | info->running_transaction->delayed_refs.flushing) { | |
687 | prepare_to_wait(&info->transaction_wait, &wait, | |
688 | TASK_UNINTERRUPTIBLE); | |
689 | mutex_unlock(&info->trans_mutex); | |
690 | schedule(); | |
691 | mutex_lock(&info->trans_mutex); | |
692 | finish_wait(&info->transaction_wait, &wait); | |
693 | } | |
694 | mutex_unlock(&info->trans_mutex); | |
695 | return 0; | |
696 | } | |
697 | ||
d352ac68 CM |
698 | /* |
699 | * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on | |
700 | * all of them | |
701 | */ | |
80b6794d CM |
702 | static noinline int drop_dirty_roots(struct btrfs_root *tree_root, |
703 | struct list_head *list) | |
0f7d52f4 | 704 | { |
f321e491 | 705 | struct btrfs_dirty_root *dirty; |
0f7d52f4 | 706 | struct btrfs_trans_handle *trans; |
d3c2fdcf | 707 | unsigned long nr; |
db94535d CM |
708 | u64 num_bytes; |
709 | u64 bytes_used; | |
bcc63abb | 710 | u64 max_useless; |
54aa1f4d | 711 | int ret = 0; |
9f3a7427 CM |
712 | int err; |
713 | ||
d397712b | 714 | while (!list_empty(list)) { |
58176a96 JB |
715 | struct btrfs_root *root; |
716 | ||
f321e491 | 717 | dirty = list_entry(list->prev, struct btrfs_dirty_root, list); |
0f7d52f4 | 718 | list_del_init(&dirty->list); |
5eda7b5e | 719 | |
db94535d | 720 | num_bytes = btrfs_root_used(&dirty->root->root_item); |
58176a96 | 721 | root = dirty->latest_root; |
a2135011 | 722 | atomic_inc(&root->fs_info->throttles); |
58176a96 | 723 | |
d397712b | 724 | while (1) { |
b7ec40d7 CM |
725 | /* |
726 | * we don't want to jump in and create a bunch of | |
727 | * delayed refs if the transaction is starting to close | |
728 | */ | |
729 | wait_transaction_pre_flush(tree_root->fs_info); | |
9f3a7427 | 730 | trans = btrfs_start_transaction(tree_root, 1); |
b7ec40d7 CM |
731 | |
732 | /* | |
733 | * we've joined a transaction, make sure it isn't | |
734 | * closing right now | |
735 | */ | |
736 | if (trans->transaction->delayed_refs.flushing) { | |
737 | btrfs_end_transaction(trans, tree_root); | |
738 | continue; | |
739 | } | |
740 | ||
5b21f2ed | 741 | mutex_lock(&root->fs_info->drop_mutex); |
9f3a7427 | 742 | ret = btrfs_drop_snapshot(trans, dirty->root); |
d397712b | 743 | if (ret != -EAGAIN) |
9f3a7427 | 744 | break; |
5b21f2ed | 745 | mutex_unlock(&root->fs_info->drop_mutex); |
58176a96 | 746 | |
9f3a7427 CM |
747 | err = btrfs_update_root(trans, |
748 | tree_root, | |
749 | &dirty->root->root_key, | |
750 | &dirty->root->root_item); | |
751 | if (err) | |
752 | ret = err; | |
d3c2fdcf | 753 | nr = trans->blocks_used; |
017e5369 | 754 | ret = btrfs_end_transaction(trans, tree_root); |
9f3a7427 | 755 | BUG_ON(ret); |
a2135011 | 756 | |
d3c2fdcf | 757 | btrfs_btree_balance_dirty(tree_root, nr); |
4dc11904 | 758 | cond_resched(); |
9f3a7427 | 759 | } |
0f7d52f4 | 760 | BUG_ON(ret); |
a2135011 | 761 | atomic_dec(&root->fs_info->throttles); |
017e5369 | 762 | wake_up(&root->fs_info->transaction_throttle); |
58176a96 | 763 | |
db94535d CM |
764 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
765 | bytes_used = btrfs_root_used(&root->root_item); | |
766 | if (num_bytes) { | |
24562425 | 767 | mutex_lock(&root->fs_info->trans_mutex); |
e02119d5 | 768 | btrfs_record_root_in_trans(root); |
24562425 | 769 | mutex_unlock(&root->fs_info->trans_mutex); |
5f39d397 | 770 | btrfs_set_root_used(&root->root_item, |
db94535d | 771 | bytes_used - num_bytes); |
58176a96 | 772 | } |
a2135011 | 773 | |
9f3a7427 | 774 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
58176a96 JB |
775 | if (ret) { |
776 | BUG(); | |
54aa1f4d | 777 | break; |
58176a96 | 778 | } |
a2135011 CM |
779 | mutex_unlock(&root->fs_info->drop_mutex); |
780 | ||
bcc63abb Y |
781 | spin_lock(&root->list_lock); |
782 | list_del_init(&dirty->root->dead_list); | |
783 | if (!list_empty(&root->dead_list)) { | |
784 | struct btrfs_root *oldest; | |
785 | oldest = list_entry(root->dead_list.prev, | |
786 | struct btrfs_root, dead_list); | |
787 | max_useless = oldest->root_key.offset - 1; | |
788 | } else { | |
789 | max_useless = root->root_key.offset - 1; | |
790 | } | |
791 | spin_unlock(&root->list_lock); | |
792 | ||
d3c2fdcf | 793 | nr = trans->blocks_used; |
0f7d52f4 CM |
794 | ret = btrfs_end_transaction(trans, tree_root); |
795 | BUG_ON(ret); | |
5eda7b5e | 796 | |
e4657689 | 797 | ret = btrfs_remove_leaf_refs(root, max_useless, 0); |
bcc63abb Y |
798 | BUG_ON(ret); |
799 | ||
f510cfec | 800 | free_extent_buffer(dirty->root->node); |
9f3a7427 | 801 | kfree(dirty->root); |
0f7d52f4 | 802 | kfree(dirty); |
d3c2fdcf CM |
803 | |
804 | btrfs_btree_balance_dirty(tree_root, nr); | |
4dc11904 | 805 | cond_resched(); |
0f7d52f4 | 806 | } |
54aa1f4d | 807 | return ret; |
0f7d52f4 CM |
808 | } |
809 | ||
d352ac68 CM |
810 | /* |
811 | * new snapshots need to be created at a very specific time in the | |
812 | * transaction commit. This does the actual creation | |
813 | */ | |
80b6794d | 814 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
3063d29f CM |
815 | struct btrfs_fs_info *fs_info, |
816 | struct btrfs_pending_snapshot *pending) | |
817 | { | |
818 | struct btrfs_key key; | |
80b6794d | 819 | struct btrfs_root_item *new_root_item; |
3063d29f CM |
820 | struct btrfs_root *tree_root = fs_info->tree_root; |
821 | struct btrfs_root *root = pending->root; | |
822 | struct extent_buffer *tmp; | |
925baedd | 823 | struct extent_buffer *old; |
3063d29f CM |
824 | int ret; |
825 | u64 objectid; | |
826 | ||
80b6794d CM |
827 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
828 | if (!new_root_item) { | |
829 | ret = -ENOMEM; | |
830 | goto fail; | |
831 | } | |
3063d29f CM |
832 | ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); |
833 | if (ret) | |
834 | goto fail; | |
835 | ||
80ff3856 YZ |
836 | btrfs_record_root_in_trans(root); |
837 | btrfs_set_root_last_snapshot(&root->root_item, trans->transid); | |
80b6794d | 838 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
3063d29f CM |
839 | |
840 | key.objectid = objectid; | |
5b21f2ed | 841 | key.offset = trans->transid; |
3063d29f CM |
842 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
843 | ||
925baedd | 844 | old = btrfs_lock_root_node(root); |
9fa8cfe7 | 845 | btrfs_cow_block(trans, root, old, NULL, 0, &old); |
3063d29f | 846 | |
925baedd CM |
847 | btrfs_copy_root(trans, root, old, &tmp, objectid); |
848 | btrfs_tree_unlock(old); | |
849 | free_extent_buffer(old); | |
3063d29f | 850 | |
80b6794d CM |
851 | btrfs_set_root_bytenr(new_root_item, tmp->start); |
852 | btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); | |
84234f3a | 853 | btrfs_set_root_generation(new_root_item, trans->transid); |
3063d29f | 854 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
80b6794d | 855 | new_root_item); |
925baedd | 856 | btrfs_tree_unlock(tmp); |
3063d29f CM |
857 | free_extent_buffer(tmp); |
858 | if (ret) | |
859 | goto fail; | |
860 | ||
3de4586c CM |
861 | key.offset = (u64)-1; |
862 | memcpy(&pending->root_key, &key, sizeof(key)); | |
863 | fail: | |
864 | kfree(new_root_item); | |
865 | return ret; | |
866 | } | |
867 | ||
868 | static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info, | |
869 | struct btrfs_pending_snapshot *pending) | |
870 | { | |
871 | int ret; | |
872 | int namelen; | |
873 | u64 index = 0; | |
874 | struct btrfs_trans_handle *trans; | |
875 | struct inode *parent_inode; | |
876 | struct inode *inode; | |
0660b5af | 877 | struct btrfs_root *parent_root; |
3de4586c | 878 | |
3394e160 | 879 | parent_inode = pending->dentry->d_parent->d_inode; |
0660b5af | 880 | parent_root = BTRFS_I(parent_inode)->root; |
180591bc | 881 | trans = btrfs_join_transaction(parent_root, 1); |
3de4586c | 882 | |
3063d29f CM |
883 | /* |
884 | * insert the directory item | |
885 | */ | |
3b96362c | 886 | namelen = strlen(pending->name); |
3de4586c | 887 | ret = btrfs_set_inode_index(parent_inode, &index); |
0660b5af | 888 | ret = btrfs_insert_dir_item(trans, parent_root, |
3de4586c CM |
889 | pending->name, namelen, |
890 | parent_inode->i_ino, | |
891 | &pending->root_key, BTRFS_FT_DIR, index); | |
3063d29f CM |
892 | |
893 | if (ret) | |
894 | goto fail; | |
0660b5af | 895 | |
52c26179 YZ |
896 | btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); |
897 | ret = btrfs_update_inode(trans, parent_root, parent_inode); | |
898 | BUG_ON(ret); | |
899 | ||
0660b5af CM |
900 | /* add the backref first */ |
901 | ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, | |
902 | pending->root_key.objectid, | |
903 | BTRFS_ROOT_BACKREF_KEY, | |
904 | parent_root->root_key.objectid, | |
905 | parent_inode->i_ino, index, pending->name, | |
906 | namelen); | |
907 | ||
908 | BUG_ON(ret); | |
909 | ||
910 | /* now add the forward ref */ | |
911 | ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, | |
912 | parent_root->root_key.objectid, | |
913 | BTRFS_ROOT_REF_KEY, | |
914 | pending->root_key.objectid, | |
915 | parent_inode->i_ino, index, pending->name, | |
916 | namelen); | |
917 | ||
3de4586c CM |
918 | inode = btrfs_lookup_dentry(parent_inode, pending->dentry); |
919 | d_instantiate(pending->dentry, inode); | |
3063d29f | 920 | fail: |
3de4586c | 921 | btrfs_end_transaction(trans, fs_info->fs_root); |
3063d29f CM |
922 | return ret; |
923 | } | |
924 | ||
d352ac68 CM |
925 | /* |
926 | * create all the snapshots we've scheduled for creation | |
927 | */ | |
80b6794d CM |
928 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
929 | struct btrfs_fs_info *fs_info) | |
3de4586c CM |
930 | { |
931 | struct btrfs_pending_snapshot *pending; | |
932 | struct list_head *head = &trans->transaction->pending_snapshots; | |
3de4586c CM |
933 | int ret; |
934 | ||
c6e30871 | 935 | list_for_each_entry(pending, head, list) { |
3de4586c CM |
936 | ret = create_pending_snapshot(trans, fs_info, pending); |
937 | BUG_ON(ret); | |
938 | } | |
939 | return 0; | |
940 | } | |
941 | ||
942 | static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans, | |
943 | struct btrfs_fs_info *fs_info) | |
3063d29f CM |
944 | { |
945 | struct btrfs_pending_snapshot *pending; | |
946 | struct list_head *head = &trans->transaction->pending_snapshots; | |
947 | int ret; | |
948 | ||
d397712b | 949 | while (!list_empty(head)) { |
3063d29f CM |
950 | pending = list_entry(head->next, |
951 | struct btrfs_pending_snapshot, list); | |
3de4586c | 952 | ret = finish_pending_snapshot(fs_info, pending); |
3063d29f CM |
953 | BUG_ON(ret); |
954 | list_del(&pending->list); | |
955 | kfree(pending->name); | |
956 | kfree(pending); | |
957 | } | |
dc17ff8f CM |
958 | return 0; |
959 | } | |
960 | ||
79154b1b CM |
961 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
962 | struct btrfs_root *root) | |
963 | { | |
15ee9bc7 JB |
964 | unsigned long joined = 0; |
965 | unsigned long timeout = 1; | |
79154b1b | 966 | struct btrfs_transaction *cur_trans; |
8fd17795 | 967 | struct btrfs_transaction *prev_trans = NULL; |
0b86a832 | 968 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
0f7d52f4 | 969 | struct list_head dirty_fs_roots; |
d1310b2e | 970 | struct extent_io_tree *pinned_copy; |
79154b1b | 971 | DEFINE_WAIT(wait); |
15ee9bc7 | 972 | int ret; |
89573b9c CM |
973 | int should_grow = 0; |
974 | unsigned long now = get_seconds(); | |
79154b1b | 975 | |
5a3f23d5 CM |
976 | btrfs_run_ordered_operations(root, 0); |
977 | ||
56bec294 CM |
978 | /* make a pass through all the delayed refs we have so far |
979 | * any runnings procs may add more while we are here | |
980 | */ | |
981 | ret = btrfs_run_delayed_refs(trans, root, 0); | |
982 | BUG_ON(ret); | |
983 | ||
b7ec40d7 | 984 | cur_trans = trans->transaction; |
56bec294 CM |
985 | /* |
986 | * set the flushing flag so procs in this transaction have to | |
987 | * start sending their work down. | |
988 | */ | |
b7ec40d7 | 989 | cur_trans->delayed_refs.flushing = 1; |
56bec294 | 990 | |
c3e69d58 | 991 | ret = btrfs_run_delayed_refs(trans, root, 0); |
56bec294 CM |
992 | BUG_ON(ret); |
993 | ||
79154b1b | 994 | mutex_lock(&root->fs_info->trans_mutex); |
b7ec40d7 CM |
995 | INIT_LIST_HEAD(&dirty_fs_roots); |
996 | if (cur_trans->in_commit) { | |
997 | cur_trans->use_count++; | |
ccd467d6 | 998 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b | 999 | btrfs_end_transaction(trans, root); |
ccd467d6 | 1000 | |
79154b1b CM |
1001 | ret = wait_for_commit(root, cur_trans); |
1002 | BUG_ON(ret); | |
15ee9bc7 JB |
1003 | |
1004 | mutex_lock(&root->fs_info->trans_mutex); | |
79154b1b | 1005 | put_transaction(cur_trans); |
15ee9bc7 JB |
1006 | mutex_unlock(&root->fs_info->trans_mutex); |
1007 | ||
79154b1b CM |
1008 | return 0; |
1009 | } | |
4313b399 CM |
1010 | |
1011 | pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); | |
1012 | if (!pinned_copy) | |
1013 | return -ENOMEM; | |
1014 | ||
d1310b2e | 1015 | extent_io_tree_init(pinned_copy, |
4313b399 CM |
1016 | root->fs_info->btree_inode->i_mapping, GFP_NOFS); |
1017 | ||
2c90e5d6 | 1018 | trans->transaction->in_commit = 1; |
f9295749 | 1019 | trans->transaction->blocked = 1; |
ccd467d6 CM |
1020 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
1021 | prev_trans = list_entry(cur_trans->list.prev, | |
1022 | struct btrfs_transaction, list); | |
1023 | if (!prev_trans->commit_done) { | |
1024 | prev_trans->use_count++; | |
ccd467d6 CM |
1025 | mutex_unlock(&root->fs_info->trans_mutex); |
1026 | ||
1027 | wait_for_commit(root, prev_trans); | |
ccd467d6 | 1028 | |
ccd467d6 | 1029 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 | 1030 | put_transaction(prev_trans); |
ccd467d6 CM |
1031 | } |
1032 | } | |
15ee9bc7 | 1033 | |
89573b9c CM |
1034 | if (now < cur_trans->start_time || now - cur_trans->start_time < 1) |
1035 | should_grow = 1; | |
1036 | ||
15ee9bc7 | 1037 | do { |
7ea394f1 | 1038 | int snap_pending = 0; |
15ee9bc7 | 1039 | joined = cur_trans->num_joined; |
7ea394f1 YZ |
1040 | if (!list_empty(&trans->transaction->pending_snapshots)) |
1041 | snap_pending = 1; | |
1042 | ||
2c90e5d6 | 1043 | WARN_ON(cur_trans != trans->transaction); |
15ee9bc7 | 1044 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
79154b1b | 1045 | TASK_UNINTERRUPTIBLE); |
15ee9bc7 JB |
1046 | |
1047 | if (cur_trans->num_writers > 1) | |
1048 | timeout = MAX_SCHEDULE_TIMEOUT; | |
89573b9c | 1049 | else if (should_grow) |
15ee9bc7 JB |
1050 | timeout = 1; |
1051 | ||
79154b1b | 1052 | mutex_unlock(&root->fs_info->trans_mutex); |
15ee9bc7 | 1053 | |
7ea394f1 YZ |
1054 | if (snap_pending) { |
1055 | ret = btrfs_wait_ordered_extents(root, 1); | |
1056 | BUG_ON(ret); | |
1057 | } | |
1058 | ||
5a3f23d5 CM |
1059 | /* |
1060 | * rename don't use btrfs_join_transaction, so, once we | |
1061 | * set the transaction to blocked above, we aren't going | |
1062 | * to get any new ordered operations. We can safely run | |
1063 | * it here and no for sure that nothing new will be added | |
1064 | * to the list | |
1065 | */ | |
1066 | btrfs_run_ordered_operations(root, 1); | |
1067 | ||
89573b9c CM |
1068 | smp_mb(); |
1069 | if (cur_trans->num_writers > 1 || should_grow) | |
1070 | schedule_timeout(timeout); | |
15ee9bc7 | 1071 | |
79154b1b | 1072 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 JB |
1073 | finish_wait(&cur_trans->writer_wait, &wait); |
1074 | } while (cur_trans->num_writers > 1 || | |
89573b9c | 1075 | (should_grow && cur_trans->num_joined != joined)); |
15ee9bc7 | 1076 | |
3063d29f CM |
1077 | ret = create_pending_snapshots(trans, root->fs_info); |
1078 | BUG_ON(ret); | |
1079 | ||
56bec294 CM |
1080 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
1081 | BUG_ON(ret); | |
1082 | ||
2c90e5d6 | 1083 | WARN_ON(cur_trans != trans->transaction); |
dc17ff8f | 1084 | |
e02119d5 CM |
1085 | /* btrfs_commit_tree_roots is responsible for getting the |
1086 | * various roots consistent with each other. Every pointer | |
1087 | * in the tree of tree roots has to point to the most up to date | |
1088 | * root for every subvolume and other tree. So, we have to keep | |
1089 | * the tree logging code from jumping in and changing any | |
1090 | * of the trees. | |
1091 | * | |
1092 | * At this point in the commit, there can't be any tree-log | |
1093 | * writers, but a little lower down we drop the trans mutex | |
1094 | * and let new people in. By holding the tree_log_mutex | |
1095 | * from now until after the super is written, we avoid races | |
1096 | * with the tree-log code. | |
1097 | */ | |
1098 | mutex_lock(&root->fs_info->tree_log_mutex); | |
1a40e23b ZY |
1099 | /* |
1100 | * keep tree reloc code from adding new reloc trees | |
1101 | */ | |
1102 | mutex_lock(&root->fs_info->tree_reloc_mutex); | |
1103 | ||
e02119d5 | 1104 | |
54aa1f4d CM |
1105 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
1106 | &dirty_fs_roots); | |
1107 | BUG_ON(ret); | |
1108 | ||
e02119d5 CM |
1109 | /* add_dirty_roots gets rid of all the tree log roots, it is now |
1110 | * safe to free the root of tree log roots | |
1111 | */ | |
1112 | btrfs_free_log_root_tree(trans, root->fs_info); | |
1113 | ||
79154b1b CM |
1114 | ret = btrfs_commit_tree_roots(trans, root); |
1115 | BUG_ON(ret); | |
54aa1f4d | 1116 | |
78fae27e | 1117 | cur_trans = root->fs_info->running_transaction; |
cee36a03 | 1118 | spin_lock(&root->fs_info->new_trans_lock); |
78fae27e | 1119 | root->fs_info->running_transaction = NULL; |
cee36a03 | 1120 | spin_unlock(&root->fs_info->new_trans_lock); |
4b52dff6 CM |
1121 | btrfs_set_super_generation(&root->fs_info->super_copy, |
1122 | cur_trans->transid); | |
1123 | btrfs_set_super_root(&root->fs_info->super_copy, | |
db94535d CM |
1124 | root->fs_info->tree_root->node->start); |
1125 | btrfs_set_super_root_level(&root->fs_info->super_copy, | |
1126 | btrfs_header_level(root->fs_info->tree_root->node)); | |
5f39d397 | 1127 | |
0b86a832 CM |
1128 | btrfs_set_super_chunk_root(&root->fs_info->super_copy, |
1129 | chunk_root->node->start); | |
1130 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, | |
1131 | btrfs_header_level(chunk_root->node)); | |
84234f3a YZ |
1132 | btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy, |
1133 | btrfs_header_generation(chunk_root->node)); | |
e02119d5 CM |
1134 | |
1135 | if (!root->fs_info->log_root_recovering) { | |
1136 | btrfs_set_super_log_root(&root->fs_info->super_copy, 0); | |
1137 | btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); | |
1138 | } | |
1139 | ||
a061fc8d CM |
1140 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
1141 | sizeof(root->fs_info->super_copy)); | |
ccd467d6 | 1142 | |
4313b399 | 1143 | btrfs_copy_pinned(root, pinned_copy); |
ccd467d6 | 1144 | |
f9295749 | 1145 | trans->transaction->blocked = 0; |
b7ec40d7 | 1146 | |
e6dcd2dc | 1147 | wake_up(&root->fs_info->transaction_throttle); |
f9295749 | 1148 | wake_up(&root->fs_info->transaction_wait); |
e6dcd2dc | 1149 | |
78fae27e | 1150 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b CM |
1151 | ret = btrfs_write_and_wait_transaction(trans, root); |
1152 | BUG_ON(ret); | |
a512bbf8 | 1153 | write_ctree_super(trans, root, 0); |
4313b399 | 1154 | |
e02119d5 CM |
1155 | /* |
1156 | * the super is written, we can safely allow the tree-loggers | |
1157 | * to go about their business | |
1158 | */ | |
1159 | mutex_unlock(&root->fs_info->tree_log_mutex); | |
1160 | ||
4313b399 | 1161 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
4313b399 CM |
1162 | kfree(pinned_copy); |
1163 | ||
1a40e23b ZY |
1164 | btrfs_drop_dead_reloc_roots(root); |
1165 | mutex_unlock(&root->fs_info->tree_reloc_mutex); | |
1166 | ||
3de4586c CM |
1167 | /* do the directory inserts of any pending snapshot creations */ |
1168 | finish_pending_snapshots(trans, root->fs_info); | |
1169 | ||
1a40e23b ZY |
1170 | mutex_lock(&root->fs_info->trans_mutex); |
1171 | ||
2c90e5d6 | 1172 | cur_trans->commit_done = 1; |
b7ec40d7 | 1173 | |
15ee9bc7 | 1174 | root->fs_info->last_trans_committed = cur_trans->transid; |
2c90e5d6 | 1175 | wake_up(&cur_trans->commit_wait); |
3de4586c | 1176 | |
78fae27e | 1177 | put_transaction(cur_trans); |
79154b1b | 1178 | put_transaction(cur_trans); |
58176a96 | 1179 | |
bcc63abb | 1180 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
facda1e7 CM |
1181 | if (root->fs_info->closing) |
1182 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); | |
58176a96 | 1183 | |
78fae27e | 1184 | mutex_unlock(&root->fs_info->trans_mutex); |
3de4586c | 1185 | |
2c90e5d6 | 1186 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
79154b1b | 1187 | |
d397712b | 1188 | if (root->fs_info->closing) |
facda1e7 | 1189 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
79154b1b CM |
1190 | return ret; |
1191 | } | |
1192 | ||
d352ac68 CM |
1193 | /* |
1194 | * interface function to delete all the snapshots we have scheduled for deletion | |
1195 | */ | |
e9d0b13b CM |
1196 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
1197 | { | |
1198 | struct list_head dirty_roots; | |
1199 | INIT_LIST_HEAD(&dirty_roots); | |
a74a4b97 | 1200 | again: |
e9d0b13b CM |
1201 | mutex_lock(&root->fs_info->trans_mutex); |
1202 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); | |
1203 | mutex_unlock(&root->fs_info->trans_mutex); | |
1204 | ||
1205 | if (!list_empty(&dirty_roots)) { | |
1206 | drop_dirty_roots(root, &dirty_roots); | |
a74a4b97 | 1207 | goto again; |
e9d0b13b CM |
1208 | } |
1209 | return 0; | |
1210 | } |