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 | |
78fae27e | 31 | static int total_trans = 0; |
2c90e5d6 CM |
32 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
33 | extern struct kmem_cache *btrfs_transaction_cachep; | |
34 | ||
0f7d52f4 CM |
35 | #define BTRFS_ROOT_TRANS_TAG 0 |
36 | ||
80b6794d | 37 | static noinline void put_transaction(struct btrfs_transaction *transaction) |
79154b1b | 38 | { |
2c90e5d6 | 39 | WARN_ON(transaction->use_count == 0); |
79154b1b | 40 | transaction->use_count--; |
78fae27e CM |
41 | if (transaction->use_count == 0) { |
42 | WARN_ON(total_trans == 0); | |
43 | total_trans--; | |
8fd17795 | 44 | list_del_init(&transaction->list); |
2c90e5d6 CM |
45 | memset(transaction, 0, sizeof(*transaction)); |
46 | kmem_cache_free(btrfs_transaction_cachep, transaction); | |
78fae27e | 47 | } |
79154b1b CM |
48 | } |
49 | ||
d352ac68 CM |
50 | /* |
51 | * either allocate a new transaction or hop into the existing one | |
52 | */ | |
80b6794d | 53 | static noinline int join_transaction(struct btrfs_root *root) |
79154b1b CM |
54 | { |
55 | struct btrfs_transaction *cur_trans; | |
56 | cur_trans = root->fs_info->running_transaction; | |
57 | if (!cur_trans) { | |
2c90e5d6 CM |
58 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
59 | GFP_NOFS); | |
78fae27e | 60 | total_trans++; |
79154b1b | 61 | BUG_ON(!cur_trans); |
0f7d52f4 | 62 | root->fs_info->generation++; |
e18e4809 | 63 | root->fs_info->last_alloc = 0; |
4529ba49 | 64 | root->fs_info->last_data_alloc = 0; |
15ee9bc7 JB |
65 | cur_trans->num_writers = 1; |
66 | cur_trans->num_joined = 0; | |
0f7d52f4 | 67 | cur_trans->transid = root->fs_info->generation; |
79154b1b CM |
68 | init_waitqueue_head(&cur_trans->writer_wait); |
69 | init_waitqueue_head(&cur_trans->commit_wait); | |
70 | cur_trans->in_commit = 0; | |
f9295749 | 71 | cur_trans->blocked = 0; |
d5719762 | 72 | cur_trans->use_count = 1; |
79154b1b | 73 | cur_trans->commit_done = 0; |
08607c1b | 74 | cur_trans->start_time = get_seconds(); |
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 CM |
91 | /* |
92 | * this does all the record keeping required to make sure that a | |
93 | * reference counted root is properly recorded in a given transaction. | |
94 | * This is required to make sure the old root from before we joined the transaction | |
95 | * is deleted when the transaction commits | |
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++; | |
151 | while(1) { | |
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; | |
31f3c99b | 189 | h->block_group = NULL; |
26b8003f CM |
190 | h->alloc_exclude_nr = 0; |
191 | h->alloc_exclude_start = 0; | |
79154b1b CM |
192 | root->fs_info->running_transaction->use_count++; |
193 | mutex_unlock(&root->fs_info->trans_mutex); | |
194 | return h; | |
195 | } | |
196 | ||
f9295749 CM |
197 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
198 | int num_blocks) | |
199 | { | |
9ca9ee09 | 200 | return start_transaction(root, num_blocks, 1); |
f9295749 CM |
201 | } |
202 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, | |
203 | int num_blocks) | |
204 | { | |
9ca9ee09 | 205 | return start_transaction(root, num_blocks, 0); |
f9295749 CM |
206 | } |
207 | ||
9ca9ee09 SW |
208 | struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, |
209 | int num_blocks) | |
210 | { | |
211 | return start_transaction(r, num_blocks, 2); | |
212 | } | |
213 | ||
d352ac68 | 214 | /* wait for a transaction commit to be fully complete */ |
89ce8a63 CM |
215 | static noinline int wait_for_commit(struct btrfs_root *root, |
216 | struct btrfs_transaction *commit) | |
217 | { | |
218 | DEFINE_WAIT(wait); | |
219 | mutex_lock(&root->fs_info->trans_mutex); | |
220 | while(!commit->commit_done) { | |
221 | prepare_to_wait(&commit->commit_wait, &wait, | |
222 | TASK_UNINTERRUPTIBLE); | |
223 | if (commit->commit_done) | |
224 | break; | |
225 | mutex_unlock(&root->fs_info->trans_mutex); | |
226 | schedule(); | |
227 | mutex_lock(&root->fs_info->trans_mutex); | |
228 | } | |
229 | mutex_unlock(&root->fs_info->trans_mutex); | |
230 | finish_wait(&commit->commit_wait, &wait); | |
231 | return 0; | |
232 | } | |
233 | ||
d352ac68 CM |
234 | /* |
235 | * rate limit against the drop_snapshot code. This helps to slow down new operations | |
236 | * if the drop_snapshot code isn't able to keep up. | |
237 | */ | |
37d1aeee | 238 | static void throttle_on_drops(struct btrfs_root *root) |
ab78c84d CM |
239 | { |
240 | struct btrfs_fs_info *info = root->fs_info; | |
2dd3e67b | 241 | int harder_count = 0; |
ab78c84d | 242 | |
2dd3e67b | 243 | harder: |
ab78c84d CM |
244 | if (atomic_read(&info->throttles)) { |
245 | DEFINE_WAIT(wait); | |
246 | int thr; | |
ab78c84d CM |
247 | thr = atomic_read(&info->throttle_gen); |
248 | ||
249 | do { | |
250 | prepare_to_wait(&info->transaction_throttle, | |
251 | &wait, TASK_UNINTERRUPTIBLE); | |
252 | if (!atomic_read(&info->throttles)) { | |
253 | finish_wait(&info->transaction_throttle, &wait); | |
254 | break; | |
255 | } | |
256 | schedule(); | |
257 | finish_wait(&info->transaction_throttle, &wait); | |
258 | } while (thr == atomic_read(&info->throttle_gen)); | |
2dd3e67b CM |
259 | harder_count++; |
260 | ||
261 | if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && | |
262 | harder_count < 2) | |
263 | goto harder; | |
264 | ||
265 | if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && | |
266 | harder_count < 10) | |
267 | goto harder; | |
268 | ||
269 | if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && | |
270 | harder_count < 20) | |
271 | goto harder; | |
ab78c84d CM |
272 | } |
273 | } | |
274 | ||
37d1aeee CM |
275 | void btrfs_throttle(struct btrfs_root *root) |
276 | { | |
277 | mutex_lock(&root->fs_info->trans_mutex); | |
9ca9ee09 SW |
278 | if (!root->fs_info->open_ioctl_trans) |
279 | wait_current_trans(root); | |
37d1aeee CM |
280 | mutex_unlock(&root->fs_info->trans_mutex); |
281 | ||
282 | throttle_on_drops(root); | |
283 | } | |
284 | ||
89ce8a63 CM |
285 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
286 | struct btrfs_root *root, int throttle) | |
79154b1b CM |
287 | { |
288 | struct btrfs_transaction *cur_trans; | |
ab78c84d | 289 | struct btrfs_fs_info *info = root->fs_info; |
d6e4a428 | 290 | |
ab78c84d CM |
291 | mutex_lock(&info->trans_mutex); |
292 | cur_trans = info->running_transaction; | |
ccd467d6 | 293 | WARN_ON(cur_trans != trans->transaction); |
d5719762 | 294 | WARN_ON(cur_trans->num_writers < 1); |
ccd467d6 | 295 | cur_trans->num_writers--; |
89ce8a63 | 296 | |
79154b1b CM |
297 | if (waitqueue_active(&cur_trans->writer_wait)) |
298 | wake_up(&cur_trans->writer_wait); | |
79154b1b | 299 | put_transaction(cur_trans); |
ab78c84d | 300 | mutex_unlock(&info->trans_mutex); |
d6025579 | 301 | memset(trans, 0, sizeof(*trans)); |
2c90e5d6 | 302 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
ab78c84d CM |
303 | |
304 | if (throttle) | |
37d1aeee | 305 | throttle_on_drops(root); |
ab78c84d | 306 | |
79154b1b CM |
307 | return 0; |
308 | } | |
309 | ||
89ce8a63 CM |
310 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
311 | struct btrfs_root *root) | |
312 | { | |
313 | return __btrfs_end_transaction(trans, root, 0); | |
314 | } | |
315 | ||
316 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, | |
317 | struct btrfs_root *root) | |
318 | { | |
319 | return __btrfs_end_transaction(trans, root, 1); | |
320 | } | |
321 | ||
d352ac68 CM |
322 | /* |
323 | * when btree blocks are allocated, they have some corresponding bits set for | |
324 | * them in one of two extent_io trees. This is used to make sure all of | |
325 | * those extents are on disk for transaction or log commit | |
326 | */ | |
d0c803c4 CM |
327 | int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, |
328 | struct extent_io_tree *dirty_pages) | |
79154b1b | 329 | { |
7c4452b9 | 330 | int ret; |
777e6bd7 | 331 | int err = 0; |
7c4452b9 CM |
332 | int werr = 0; |
333 | struct page *page; | |
7c4452b9 | 334 | struct inode *btree_inode = root->fs_info->btree_inode; |
5f2cc086 | 335 | struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; |
777e6bd7 | 336 | u64 start = 0; |
5f39d397 CM |
337 | u64 end; |
338 | unsigned long index; | |
7c4452b9 | 339 | |
7c4452b9 | 340 | while(1) { |
777e6bd7 | 341 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, |
5f39d397 CM |
342 | EXTENT_DIRTY); |
343 | if (ret) | |
7c4452b9 | 344 | break; |
5f39d397 | 345 | while(start <= end) { |
777e6bd7 CM |
346 | cond_resched(); |
347 | ||
5f39d397 | 348 | index = start >> PAGE_CACHE_SHIFT; |
35ebb934 | 349 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
4bef0848 | 350 | page = find_get_page(btree_inode->i_mapping, index); |
7c4452b9 CM |
351 | if (!page) |
352 | continue; | |
4bef0848 CM |
353 | |
354 | btree_lock_page_hook(page); | |
355 | if (!page->mapping) { | |
356 | unlock_page(page); | |
357 | page_cache_release(page); | |
358 | continue; | |
359 | } | |
360 | ||
6702ed49 CM |
361 | if (PageWriteback(page)) { |
362 | if (PageDirty(page)) | |
363 | wait_on_page_writeback(page); | |
364 | else { | |
365 | unlock_page(page); | |
366 | page_cache_release(page); | |
367 | continue; | |
368 | } | |
369 | } | |
7c4452b9 CM |
370 | err = write_one_page(page, 0); |
371 | if (err) | |
372 | werr = err; | |
373 | page_cache_release(page); | |
374 | } | |
375 | } | |
5f2cc086 CM |
376 | /* |
377 | * we unplug once and then use the wait_on_extent_bit for | |
378 | * everything else | |
379 | */ | |
380 | blk_run_address_space(btree_inode->i_mapping); | |
777e6bd7 CM |
381 | while(1) { |
382 | ret = find_first_extent_bit(dirty_pages, 0, &start, &end, | |
383 | EXTENT_DIRTY); | |
384 | if (ret) | |
385 | break; | |
386 | ||
387 | clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); | |
388 | while(start <= end) { | |
389 | index = start >> PAGE_CACHE_SHIFT; | |
390 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; | |
391 | page = find_get_page(btree_inode->i_mapping, index); | |
392 | if (!page) | |
393 | continue; | |
394 | if (PageDirty(page)) { | |
4bef0848 CM |
395 | btree_lock_page_hook(page); |
396 | wait_on_page_writeback(page); | |
777e6bd7 CM |
397 | err = write_one_page(page, 0); |
398 | if (err) | |
399 | werr = err; | |
400 | } | |
5f2cc086 CM |
401 | if (PageWriteback(page)) { |
402 | /* | |
403 | * we don't wait on the page writeback bit | |
404 | * because that triggers a lot of unplugs. | |
405 | * The extent bits are much nicer to | |
406 | * the disks, but come with a slightly | |
407 | * higher latency because we aren't forcing | |
408 | * unplugs. | |
409 | */ | |
410 | wait_on_extent_writeback(io_tree, | |
411 | page_offset(page), | |
412 | page_offset(page) + | |
413 | PAGE_CACHE_SIZE - 1); | |
414 | } | |
415 | if (PageWriteback(page)) { | |
416 | /* | |
417 | * the state bits get cleared before the | |
418 | * page bits, lets add some extra | |
419 | * paranoia here | |
420 | */ | |
421 | wait_on_page_writeback(page); | |
422 | } | |
777e6bd7 CM |
423 | page_cache_release(page); |
424 | cond_resched(); | |
425 | } | |
426 | } | |
7c4452b9 CM |
427 | if (err) |
428 | werr = err; | |
429 | return werr; | |
79154b1b CM |
430 | } |
431 | ||
d0c803c4 CM |
432 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
433 | struct btrfs_root *root) | |
434 | { | |
435 | if (!trans || !trans->transaction) { | |
436 | struct inode *btree_inode; | |
437 | btree_inode = root->fs_info->btree_inode; | |
438 | return filemap_write_and_wait(btree_inode->i_mapping); | |
439 | } | |
440 | return btrfs_write_and_wait_marked_extents(root, | |
441 | &trans->transaction->dirty_pages); | |
442 | } | |
443 | ||
d352ac68 CM |
444 | /* |
445 | * this is used to update the root pointer in the tree of tree roots. | |
446 | * | |
447 | * But, in the case of the extent allocation tree, updating the root | |
448 | * pointer may allocate blocks which may change the root of the extent | |
449 | * allocation tree. | |
450 | * | |
451 | * So, this loops and repeats and makes sure the cowonly root didn't | |
452 | * change while the root pointer was being updated in the metadata. | |
453 | */ | |
0b86a832 CM |
454 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
455 | struct btrfs_root *root) | |
79154b1b CM |
456 | { |
457 | int ret; | |
0b86a832 CM |
458 | u64 old_root_bytenr; |
459 | struct btrfs_root *tree_root = root->fs_info->tree_root; | |
79154b1b | 460 | |
87ef2bb4 | 461 | btrfs_extent_post_op(trans, root); |
0b86a832 | 462 | btrfs_write_dirty_block_groups(trans, root); |
87ef2bb4 CM |
463 | btrfs_extent_post_op(trans, root); |
464 | ||
79154b1b | 465 | while(1) { |
0b86a832 CM |
466 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
467 | if (old_root_bytenr == root->node->start) | |
79154b1b | 468 | break; |
0b86a832 CM |
469 | btrfs_set_root_bytenr(&root->root_item, |
470 | root->node->start); | |
471 | btrfs_set_root_level(&root->root_item, | |
472 | btrfs_header_level(root->node)); | |
84234f3a | 473 | btrfs_set_root_generation(&root->root_item, trans->transid); |
87ef2bb4 CM |
474 | |
475 | btrfs_extent_post_op(trans, root); | |
476 | ||
79154b1b | 477 | ret = btrfs_update_root(trans, tree_root, |
0b86a832 CM |
478 | &root->root_key, |
479 | &root->root_item); | |
79154b1b | 480 | BUG_ON(ret); |
0b86a832 | 481 | btrfs_write_dirty_block_groups(trans, root); |
87ef2bb4 | 482 | btrfs_extent_post_op(trans, root); |
0b86a832 CM |
483 | } |
484 | return 0; | |
485 | } | |
486 | ||
d352ac68 CM |
487 | /* |
488 | * update all the cowonly tree roots on disk | |
489 | */ | |
0b86a832 CM |
490 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
491 | struct btrfs_root *root) | |
492 | { | |
493 | struct btrfs_fs_info *fs_info = root->fs_info; | |
494 | struct list_head *next; | |
84234f3a YZ |
495 | struct extent_buffer *eb; |
496 | ||
87ef2bb4 CM |
497 | btrfs_extent_post_op(trans, fs_info->tree_root); |
498 | ||
84234f3a YZ |
499 | eb = btrfs_lock_root_node(fs_info->tree_root); |
500 | btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0); | |
501 | btrfs_tree_unlock(eb); | |
502 | free_extent_buffer(eb); | |
0b86a832 | 503 | |
87ef2bb4 CM |
504 | btrfs_extent_post_op(trans, fs_info->tree_root); |
505 | ||
0b86a832 CM |
506 | while(!list_empty(&fs_info->dirty_cowonly_roots)) { |
507 | next = fs_info->dirty_cowonly_roots.next; | |
508 | list_del_init(next); | |
509 | root = list_entry(next, struct btrfs_root, dirty_list); | |
87ef2bb4 | 510 | |
0b86a832 | 511 | update_cowonly_root(trans, root); |
79154b1b CM |
512 | } |
513 | return 0; | |
514 | } | |
515 | ||
d352ac68 CM |
516 | /* |
517 | * dead roots are old snapshots that need to be deleted. This allocates | |
518 | * a dirty root struct and adds it into the list of dead roots that need to | |
519 | * be deleted | |
520 | */ | |
b48652c1 | 521 | int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) |
5eda7b5e | 522 | { |
f321e491 | 523 | struct btrfs_dirty_root *dirty; |
5eda7b5e CM |
524 | |
525 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
526 | if (!dirty) | |
527 | return -ENOMEM; | |
5eda7b5e | 528 | dirty->root = root; |
5ce14bbc | 529 | dirty->latest_root = latest; |
b48652c1 YZ |
530 | |
531 | mutex_lock(&root->fs_info->trans_mutex); | |
532 | list_add(&dirty->list, &latest->fs_info->dead_roots); | |
533 | mutex_unlock(&root->fs_info->trans_mutex); | |
5eda7b5e CM |
534 | return 0; |
535 | } | |
536 | ||
d352ac68 CM |
537 | /* |
538 | * at transaction commit time we need to schedule the old roots for | |
539 | * deletion via btrfs_drop_snapshot. This runs through all the | |
540 | * reference counted roots that were modified in the current | |
541 | * transaction and puts them into the drop list | |
542 | */ | |
80b6794d CM |
543 | static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, |
544 | struct radix_tree_root *radix, | |
545 | struct list_head *list) | |
0f7d52f4 | 546 | { |
f321e491 | 547 | struct btrfs_dirty_root *dirty; |
0f7d52f4 CM |
548 | struct btrfs_root *gang[8]; |
549 | struct btrfs_root *root; | |
550 | int i; | |
551 | int ret; | |
54aa1f4d | 552 | int err = 0; |
5eda7b5e | 553 | u32 refs; |
54aa1f4d | 554 | |
0f7d52f4 CM |
555 | while(1) { |
556 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, | |
557 | ARRAY_SIZE(gang), | |
558 | BTRFS_ROOT_TRANS_TAG); | |
559 | if (ret == 0) | |
560 | break; | |
561 | for (i = 0; i < ret; i++) { | |
562 | root = gang[i]; | |
2619ba1f CM |
563 | radix_tree_tag_clear(radix, |
564 | (unsigned long)root->root_key.objectid, | |
565 | BTRFS_ROOT_TRANS_TAG); | |
31153d81 YZ |
566 | |
567 | BUG_ON(!root->ref_tree); | |
017e5369 | 568 | dirty = root->dirty_root; |
31153d81 | 569 | |
e02119d5 | 570 | btrfs_free_log(trans, root); |
f82d02d9 | 571 | btrfs_free_reloc_root(trans, root); |
e02119d5 | 572 | |
0f7d52f4 | 573 | if (root->commit_root == root->node) { |
db94535d CM |
574 | WARN_ON(root->node->start != |
575 | btrfs_root_bytenr(&root->root_item)); | |
31153d81 | 576 | |
5f39d397 | 577 | free_extent_buffer(root->commit_root); |
0f7d52f4 | 578 | root->commit_root = NULL; |
7ea394f1 | 579 | root->dirty_root = NULL; |
bcc63abb Y |
580 | |
581 | spin_lock(&root->list_lock); | |
582 | list_del_init(&dirty->root->dead_list); | |
583 | spin_unlock(&root->list_lock); | |
584 | ||
31153d81 YZ |
585 | kfree(dirty->root); |
586 | kfree(dirty); | |
58176a96 JB |
587 | |
588 | /* make sure to update the root on disk | |
589 | * so we get any updates to the block used | |
590 | * counts | |
591 | */ | |
592 | err = btrfs_update_root(trans, | |
593 | root->fs_info->tree_root, | |
594 | &root->root_key, | |
595 | &root->root_item); | |
0f7d52f4 CM |
596 | continue; |
597 | } | |
9f3a7427 CM |
598 | |
599 | memset(&root->root_item.drop_progress, 0, | |
600 | sizeof(struct btrfs_disk_key)); | |
601 | root->root_item.drop_level = 0; | |
0f7d52f4 | 602 | root->commit_root = NULL; |
7ea394f1 | 603 | root->dirty_root = NULL; |
0f7d52f4 | 604 | root->root_key.offset = root->fs_info->generation; |
db94535d CM |
605 | btrfs_set_root_bytenr(&root->root_item, |
606 | root->node->start); | |
607 | btrfs_set_root_level(&root->root_item, | |
608 | btrfs_header_level(root->node)); | |
84234f3a YZ |
609 | btrfs_set_root_generation(&root->root_item, |
610 | root->root_key.offset); | |
611 | ||
0f7d52f4 CM |
612 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
613 | &root->root_key, | |
614 | &root->root_item); | |
54aa1f4d CM |
615 | if (err) |
616 | break; | |
9f3a7427 CM |
617 | |
618 | refs = btrfs_root_refs(&dirty->root->root_item); | |
619 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); | |
5eda7b5e | 620 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
9f3a7427 CM |
621 | &dirty->root->root_key, |
622 | &dirty->root->root_item); | |
5eda7b5e CM |
623 | |
624 | BUG_ON(err); | |
9f3a7427 | 625 | if (refs == 1) { |
5eda7b5e | 626 | list_add(&dirty->list, list); |
9f3a7427 CM |
627 | } else { |
628 | WARN_ON(1); | |
31153d81 | 629 | free_extent_buffer(dirty->root->node); |
9f3a7427 | 630 | kfree(dirty->root); |
5eda7b5e | 631 | kfree(dirty); |
9f3a7427 | 632 | } |
0f7d52f4 CM |
633 | } |
634 | } | |
54aa1f4d | 635 | return err; |
0f7d52f4 CM |
636 | } |
637 | ||
d352ac68 CM |
638 | /* |
639 | * defrag a given btree. If cacheonly == 1, this won't read from the disk, | |
640 | * otherwise every leaf in the btree is read and defragged. | |
641 | */ | |
e9d0b13b CM |
642 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
643 | { | |
644 | struct btrfs_fs_info *info = root->fs_info; | |
645 | int ret; | |
646 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 647 | unsigned long nr; |
e9d0b13b | 648 | |
a2135011 | 649 | smp_mb(); |
e9d0b13b CM |
650 | if (root->defrag_running) |
651 | return 0; | |
e9d0b13b | 652 | trans = btrfs_start_transaction(root, 1); |
6b80053d | 653 | while (1) { |
e9d0b13b CM |
654 | root->defrag_running = 1; |
655 | ret = btrfs_defrag_leaves(trans, root, cacheonly); | |
d3c2fdcf | 656 | nr = trans->blocks_used; |
e9d0b13b | 657 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 658 | btrfs_btree_balance_dirty(info->tree_root, nr); |
e9d0b13b CM |
659 | cond_resched(); |
660 | ||
e9d0b13b | 661 | trans = btrfs_start_transaction(root, 1); |
3f157a2f | 662 | if (root->fs_info->closing || ret != -EAGAIN) |
e9d0b13b CM |
663 | break; |
664 | } | |
665 | root->defrag_running = 0; | |
a2135011 | 666 | smp_mb(); |
e9d0b13b CM |
667 | btrfs_end_transaction(trans, root); |
668 | return 0; | |
669 | } | |
670 | ||
d352ac68 CM |
671 | /* |
672 | * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on | |
673 | * all of them | |
674 | */ | |
80b6794d CM |
675 | static noinline int drop_dirty_roots(struct btrfs_root *tree_root, |
676 | struct list_head *list) | |
0f7d52f4 | 677 | { |
f321e491 | 678 | struct btrfs_dirty_root *dirty; |
0f7d52f4 | 679 | struct btrfs_trans_handle *trans; |
d3c2fdcf | 680 | unsigned long nr; |
db94535d CM |
681 | u64 num_bytes; |
682 | u64 bytes_used; | |
bcc63abb | 683 | u64 max_useless; |
54aa1f4d | 684 | int ret = 0; |
9f3a7427 CM |
685 | int err; |
686 | ||
0f7d52f4 | 687 | while(!list_empty(list)) { |
58176a96 JB |
688 | struct btrfs_root *root; |
689 | ||
f321e491 | 690 | dirty = list_entry(list->prev, struct btrfs_dirty_root, list); |
0f7d52f4 | 691 | list_del_init(&dirty->list); |
5eda7b5e | 692 | |
db94535d | 693 | num_bytes = btrfs_root_used(&dirty->root->root_item); |
58176a96 | 694 | root = dirty->latest_root; |
a2135011 | 695 | atomic_inc(&root->fs_info->throttles); |
58176a96 | 696 | |
9f3a7427 CM |
697 | while(1) { |
698 | trans = btrfs_start_transaction(tree_root, 1); | |
5b21f2ed | 699 | mutex_lock(&root->fs_info->drop_mutex); |
9f3a7427 CM |
700 | ret = btrfs_drop_snapshot(trans, dirty->root); |
701 | if (ret != -EAGAIN) { | |
702 | break; | |
703 | } | |
5b21f2ed | 704 | mutex_unlock(&root->fs_info->drop_mutex); |
58176a96 | 705 | |
9f3a7427 CM |
706 | err = btrfs_update_root(trans, |
707 | tree_root, | |
708 | &dirty->root->root_key, | |
709 | &dirty->root->root_item); | |
710 | if (err) | |
711 | ret = err; | |
d3c2fdcf | 712 | nr = trans->blocks_used; |
017e5369 | 713 | ret = btrfs_end_transaction(trans, tree_root); |
9f3a7427 | 714 | BUG_ON(ret); |
a2135011 | 715 | |
d3c2fdcf | 716 | btrfs_btree_balance_dirty(tree_root, nr); |
4dc11904 | 717 | cond_resched(); |
9f3a7427 | 718 | } |
0f7d52f4 | 719 | BUG_ON(ret); |
a2135011 | 720 | atomic_dec(&root->fs_info->throttles); |
017e5369 | 721 | wake_up(&root->fs_info->transaction_throttle); |
58176a96 | 722 | |
db94535d CM |
723 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
724 | bytes_used = btrfs_root_used(&root->root_item); | |
725 | if (num_bytes) { | |
e02119d5 | 726 | btrfs_record_root_in_trans(root); |
5f39d397 | 727 | btrfs_set_root_used(&root->root_item, |
db94535d | 728 | bytes_used - num_bytes); |
58176a96 | 729 | } |
a2135011 | 730 | |
9f3a7427 | 731 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
58176a96 JB |
732 | if (ret) { |
733 | BUG(); | |
54aa1f4d | 734 | break; |
58176a96 | 735 | } |
a2135011 CM |
736 | mutex_unlock(&root->fs_info->drop_mutex); |
737 | ||
bcc63abb Y |
738 | spin_lock(&root->list_lock); |
739 | list_del_init(&dirty->root->dead_list); | |
740 | if (!list_empty(&root->dead_list)) { | |
741 | struct btrfs_root *oldest; | |
742 | oldest = list_entry(root->dead_list.prev, | |
743 | struct btrfs_root, dead_list); | |
744 | max_useless = oldest->root_key.offset - 1; | |
745 | } else { | |
746 | max_useless = root->root_key.offset - 1; | |
747 | } | |
748 | spin_unlock(&root->list_lock); | |
749 | ||
d3c2fdcf | 750 | nr = trans->blocks_used; |
0f7d52f4 CM |
751 | ret = btrfs_end_transaction(trans, tree_root); |
752 | BUG_ON(ret); | |
5eda7b5e | 753 | |
e4657689 | 754 | ret = btrfs_remove_leaf_refs(root, max_useless, 0); |
bcc63abb Y |
755 | BUG_ON(ret); |
756 | ||
f510cfec | 757 | free_extent_buffer(dirty->root->node); |
9f3a7427 | 758 | kfree(dirty->root); |
0f7d52f4 | 759 | kfree(dirty); |
d3c2fdcf CM |
760 | |
761 | btrfs_btree_balance_dirty(tree_root, nr); | |
4dc11904 | 762 | cond_resched(); |
0f7d52f4 | 763 | } |
54aa1f4d | 764 | return ret; |
0f7d52f4 CM |
765 | } |
766 | ||
d352ac68 CM |
767 | /* |
768 | * new snapshots need to be created at a very specific time in the | |
769 | * transaction commit. This does the actual creation | |
770 | */ | |
80b6794d | 771 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
3063d29f CM |
772 | struct btrfs_fs_info *fs_info, |
773 | struct btrfs_pending_snapshot *pending) | |
774 | { | |
775 | struct btrfs_key key; | |
80b6794d | 776 | struct btrfs_root_item *new_root_item; |
3063d29f CM |
777 | struct btrfs_root *tree_root = fs_info->tree_root; |
778 | struct btrfs_root *root = pending->root; | |
779 | struct extent_buffer *tmp; | |
925baedd | 780 | struct extent_buffer *old; |
3063d29f | 781 | int ret; |
3b96362c | 782 | int namelen; |
3063d29f CM |
783 | u64 objectid; |
784 | ||
80b6794d CM |
785 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
786 | if (!new_root_item) { | |
787 | ret = -ENOMEM; | |
788 | goto fail; | |
789 | } | |
3063d29f CM |
790 | ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); |
791 | if (ret) | |
792 | goto fail; | |
793 | ||
80ff3856 YZ |
794 | btrfs_record_root_in_trans(root); |
795 | btrfs_set_root_last_snapshot(&root->root_item, trans->transid); | |
80b6794d | 796 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
3063d29f CM |
797 | |
798 | key.objectid = objectid; | |
5b21f2ed | 799 | key.offset = trans->transid; |
3063d29f CM |
800 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
801 | ||
925baedd | 802 | old = btrfs_lock_root_node(root); |
65b51a00 | 803 | btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); |
3063d29f | 804 | |
925baedd CM |
805 | btrfs_copy_root(trans, root, old, &tmp, objectid); |
806 | btrfs_tree_unlock(old); | |
807 | free_extent_buffer(old); | |
3063d29f | 808 | |
80b6794d CM |
809 | btrfs_set_root_bytenr(new_root_item, tmp->start); |
810 | btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); | |
84234f3a | 811 | btrfs_set_root_generation(new_root_item, trans->transid); |
3063d29f | 812 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
80b6794d | 813 | new_root_item); |
925baedd | 814 | btrfs_tree_unlock(tmp); |
3063d29f CM |
815 | free_extent_buffer(tmp); |
816 | if (ret) | |
817 | goto fail; | |
818 | ||
819 | /* | |
820 | * insert the directory item | |
821 | */ | |
822 | key.offset = (u64)-1; | |
3b96362c | 823 | namelen = strlen(pending->name); |
3063d29f | 824 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
3b96362c | 825 | pending->name, namelen, |
3063d29f | 826 | root->fs_info->sb->s_root->d_inode->i_ino, |
aec7477b | 827 | &key, BTRFS_FT_DIR, 0); |
3063d29f CM |
828 | |
829 | if (ret) | |
830 | goto fail; | |
831 | ||
832 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, | |
833 | pending->name, strlen(pending->name), objectid, | |
aec7477b | 834 | root->fs_info->sb->s_root->d_inode->i_ino, 0); |
3b96362c SW |
835 | |
836 | /* Invalidate existing dcache entry for new snapshot. */ | |
837 | btrfs_invalidate_dcache_root(root, pending->name, namelen); | |
838 | ||
3063d29f | 839 | fail: |
80b6794d | 840 | kfree(new_root_item); |
3063d29f CM |
841 | return ret; |
842 | } | |
843 | ||
d352ac68 CM |
844 | /* |
845 | * create all the snapshots we've scheduled for creation | |
846 | */ | |
80b6794d CM |
847 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
848 | struct btrfs_fs_info *fs_info) | |
3063d29f CM |
849 | { |
850 | struct btrfs_pending_snapshot *pending; | |
851 | struct list_head *head = &trans->transaction->pending_snapshots; | |
852 | int ret; | |
853 | ||
854 | while(!list_empty(head)) { | |
855 | pending = list_entry(head->next, | |
856 | struct btrfs_pending_snapshot, list); | |
857 | ret = create_pending_snapshot(trans, fs_info, pending); | |
858 | BUG_ON(ret); | |
859 | list_del(&pending->list); | |
860 | kfree(pending->name); | |
861 | kfree(pending); | |
862 | } | |
dc17ff8f CM |
863 | return 0; |
864 | } | |
865 | ||
79154b1b CM |
866 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
867 | struct btrfs_root *root) | |
868 | { | |
15ee9bc7 JB |
869 | unsigned long joined = 0; |
870 | unsigned long timeout = 1; | |
79154b1b | 871 | struct btrfs_transaction *cur_trans; |
8fd17795 | 872 | struct btrfs_transaction *prev_trans = NULL; |
0b86a832 | 873 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
0f7d52f4 | 874 | struct list_head dirty_fs_roots; |
d1310b2e | 875 | struct extent_io_tree *pinned_copy; |
79154b1b | 876 | DEFINE_WAIT(wait); |
15ee9bc7 | 877 | int ret; |
79154b1b | 878 | |
0f7d52f4 | 879 | INIT_LIST_HEAD(&dirty_fs_roots); |
79154b1b CM |
880 | mutex_lock(&root->fs_info->trans_mutex); |
881 | if (trans->transaction->in_commit) { | |
882 | cur_trans = trans->transaction; | |
883 | trans->transaction->use_count++; | |
ccd467d6 | 884 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b | 885 | btrfs_end_transaction(trans, root); |
ccd467d6 | 886 | |
79154b1b CM |
887 | ret = wait_for_commit(root, cur_trans); |
888 | BUG_ON(ret); | |
15ee9bc7 JB |
889 | |
890 | mutex_lock(&root->fs_info->trans_mutex); | |
79154b1b | 891 | put_transaction(cur_trans); |
15ee9bc7 JB |
892 | mutex_unlock(&root->fs_info->trans_mutex); |
893 | ||
79154b1b CM |
894 | return 0; |
895 | } | |
4313b399 CM |
896 | |
897 | pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); | |
898 | if (!pinned_copy) | |
899 | return -ENOMEM; | |
900 | ||
d1310b2e | 901 | extent_io_tree_init(pinned_copy, |
4313b399 CM |
902 | root->fs_info->btree_inode->i_mapping, GFP_NOFS); |
903 | ||
2c90e5d6 | 904 | trans->transaction->in_commit = 1; |
f9295749 | 905 | trans->transaction->blocked = 1; |
ccd467d6 CM |
906 | cur_trans = trans->transaction; |
907 | if (cur_trans->list.prev != &root->fs_info->trans_list) { | |
908 | prev_trans = list_entry(cur_trans->list.prev, | |
909 | struct btrfs_transaction, list); | |
910 | if (!prev_trans->commit_done) { | |
911 | prev_trans->use_count++; | |
ccd467d6 CM |
912 | mutex_unlock(&root->fs_info->trans_mutex); |
913 | ||
914 | wait_for_commit(root, prev_trans); | |
ccd467d6 | 915 | |
ccd467d6 | 916 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 | 917 | put_transaction(prev_trans); |
ccd467d6 CM |
918 | } |
919 | } | |
15ee9bc7 JB |
920 | |
921 | do { | |
7ea394f1 | 922 | int snap_pending = 0; |
15ee9bc7 | 923 | joined = cur_trans->num_joined; |
7ea394f1 YZ |
924 | if (!list_empty(&trans->transaction->pending_snapshots)) |
925 | snap_pending = 1; | |
926 | ||
2c90e5d6 | 927 | WARN_ON(cur_trans != trans->transaction); |
15ee9bc7 | 928 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
79154b1b | 929 | TASK_UNINTERRUPTIBLE); |
15ee9bc7 JB |
930 | |
931 | if (cur_trans->num_writers > 1) | |
932 | timeout = MAX_SCHEDULE_TIMEOUT; | |
933 | else | |
934 | timeout = 1; | |
935 | ||
79154b1b | 936 | mutex_unlock(&root->fs_info->trans_mutex); |
15ee9bc7 | 937 | |
7ea394f1 YZ |
938 | if (snap_pending) { |
939 | ret = btrfs_wait_ordered_extents(root, 1); | |
940 | BUG_ON(ret); | |
941 | } | |
942 | ||
15ee9bc7 JB |
943 | schedule_timeout(timeout); |
944 | ||
79154b1b | 945 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 JB |
946 | finish_wait(&cur_trans->writer_wait, &wait); |
947 | } while (cur_trans->num_writers > 1 || | |
948 | (cur_trans->num_joined != joined)); | |
949 | ||
3063d29f CM |
950 | ret = create_pending_snapshots(trans, root->fs_info); |
951 | BUG_ON(ret); | |
952 | ||
2c90e5d6 | 953 | WARN_ON(cur_trans != trans->transaction); |
dc17ff8f | 954 | |
e02119d5 CM |
955 | /* btrfs_commit_tree_roots is responsible for getting the |
956 | * various roots consistent with each other. Every pointer | |
957 | * in the tree of tree roots has to point to the most up to date | |
958 | * root for every subvolume and other tree. So, we have to keep | |
959 | * the tree logging code from jumping in and changing any | |
960 | * of the trees. | |
961 | * | |
962 | * At this point in the commit, there can't be any tree-log | |
963 | * writers, but a little lower down we drop the trans mutex | |
964 | * and let new people in. By holding the tree_log_mutex | |
965 | * from now until after the super is written, we avoid races | |
966 | * with the tree-log code. | |
967 | */ | |
968 | mutex_lock(&root->fs_info->tree_log_mutex); | |
1a40e23b ZY |
969 | /* |
970 | * keep tree reloc code from adding new reloc trees | |
971 | */ | |
972 | mutex_lock(&root->fs_info->tree_reloc_mutex); | |
973 | ||
e02119d5 | 974 | |
54aa1f4d CM |
975 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
976 | &dirty_fs_roots); | |
977 | BUG_ON(ret); | |
978 | ||
e02119d5 CM |
979 | /* add_dirty_roots gets rid of all the tree log roots, it is now |
980 | * safe to free the root of tree log roots | |
981 | */ | |
982 | btrfs_free_log_root_tree(trans, root->fs_info); | |
983 | ||
79154b1b CM |
984 | ret = btrfs_commit_tree_roots(trans, root); |
985 | BUG_ON(ret); | |
54aa1f4d | 986 | |
78fae27e | 987 | cur_trans = root->fs_info->running_transaction; |
cee36a03 | 988 | spin_lock(&root->fs_info->new_trans_lock); |
78fae27e | 989 | root->fs_info->running_transaction = NULL; |
cee36a03 | 990 | spin_unlock(&root->fs_info->new_trans_lock); |
4b52dff6 CM |
991 | btrfs_set_super_generation(&root->fs_info->super_copy, |
992 | cur_trans->transid); | |
993 | btrfs_set_super_root(&root->fs_info->super_copy, | |
db94535d CM |
994 | root->fs_info->tree_root->node->start); |
995 | btrfs_set_super_root_level(&root->fs_info->super_copy, | |
996 | btrfs_header_level(root->fs_info->tree_root->node)); | |
5f39d397 | 997 | |
0b86a832 CM |
998 | btrfs_set_super_chunk_root(&root->fs_info->super_copy, |
999 | chunk_root->node->start); | |
1000 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, | |
1001 | btrfs_header_level(chunk_root->node)); | |
84234f3a YZ |
1002 | btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy, |
1003 | btrfs_header_generation(chunk_root->node)); | |
e02119d5 CM |
1004 | |
1005 | if (!root->fs_info->log_root_recovering) { | |
1006 | btrfs_set_super_log_root(&root->fs_info->super_copy, 0); | |
1007 | btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); | |
1008 | } | |
1009 | ||
a061fc8d CM |
1010 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
1011 | sizeof(root->fs_info->super_copy)); | |
ccd467d6 | 1012 | |
4313b399 | 1013 | btrfs_copy_pinned(root, pinned_copy); |
ccd467d6 | 1014 | |
f9295749 | 1015 | trans->transaction->blocked = 0; |
e6dcd2dc | 1016 | wake_up(&root->fs_info->transaction_throttle); |
f9295749 | 1017 | wake_up(&root->fs_info->transaction_wait); |
e6dcd2dc | 1018 | |
78fae27e | 1019 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b CM |
1020 | ret = btrfs_write_and_wait_transaction(trans, root); |
1021 | BUG_ON(ret); | |
79154b1b | 1022 | write_ctree_super(trans, root); |
4313b399 | 1023 | |
e02119d5 CM |
1024 | /* |
1025 | * the super is written, we can safely allow the tree-loggers | |
1026 | * to go about their business | |
1027 | */ | |
1028 | mutex_unlock(&root->fs_info->tree_log_mutex); | |
1029 | ||
4313b399 | 1030 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
4313b399 CM |
1031 | kfree(pinned_copy); |
1032 | ||
1a40e23b ZY |
1033 | btrfs_drop_dead_reloc_roots(root); |
1034 | mutex_unlock(&root->fs_info->tree_reloc_mutex); | |
1035 | ||
1036 | mutex_lock(&root->fs_info->trans_mutex); | |
1037 | ||
2c90e5d6 | 1038 | cur_trans->commit_done = 1; |
15ee9bc7 | 1039 | root->fs_info->last_trans_committed = cur_trans->transid; |
2c90e5d6 | 1040 | wake_up(&cur_trans->commit_wait); |
78fae27e | 1041 | put_transaction(cur_trans); |
79154b1b | 1042 | put_transaction(cur_trans); |
58176a96 | 1043 | |
bcc63abb | 1044 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
facda1e7 CM |
1045 | if (root->fs_info->closing) |
1046 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); | |
58176a96 | 1047 | |
78fae27e | 1048 | mutex_unlock(&root->fs_info->trans_mutex); |
2c90e5d6 | 1049 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
79154b1b | 1050 | |
facda1e7 | 1051 | if (root->fs_info->closing) { |
facda1e7 | 1052 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
facda1e7 | 1053 | } |
79154b1b CM |
1054 | return ret; |
1055 | } | |
1056 | ||
d352ac68 CM |
1057 | /* |
1058 | * interface function to delete all the snapshots we have scheduled for deletion | |
1059 | */ | |
e9d0b13b CM |
1060 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
1061 | { | |
1062 | struct list_head dirty_roots; | |
1063 | INIT_LIST_HEAD(&dirty_roots); | |
a74a4b97 | 1064 | again: |
e9d0b13b CM |
1065 | mutex_lock(&root->fs_info->trans_mutex); |
1066 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); | |
1067 | mutex_unlock(&root->fs_info->trans_mutex); | |
1068 | ||
1069 | if (!list_empty(&dirty_roots)) { | |
1070 | drop_dirty_roots(root, &dirty_roots); | |
a74a4b97 | 1071 | goto again; |
e9d0b13b CM |
1072 | } |
1073 | return 0; | |
1074 | } |