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