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