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