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> |
79154b1b CM |
23 | #include "ctree.h" |
24 | #include "disk-io.h" | |
25 | #include "transaction.h" | |
925baedd | 26 | #include "locking.h" |
31153d81 | 27 | #include "ref-cache.h" |
79154b1b | 28 | |
78fae27e | 29 | static int total_trans = 0; |
2c90e5d6 CM |
30 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
31 | extern struct kmem_cache *btrfs_transaction_cachep; | |
32 | ||
0f7d52f4 CM |
33 | #define BTRFS_ROOT_TRANS_TAG 0 |
34 | ||
31153d81 YZ |
35 | struct dirty_root { |
36 | struct list_head list; | |
37 | struct btrfs_root *root; | |
38 | struct btrfs_root *latest_root; | |
31153d81 YZ |
39 | }; |
40 | ||
80b6794d | 41 | static noinline void put_transaction(struct btrfs_transaction *transaction) |
79154b1b | 42 | { |
2c90e5d6 | 43 | WARN_ON(transaction->use_count == 0); |
79154b1b | 44 | transaction->use_count--; |
78fae27e CM |
45 | if (transaction->use_count == 0) { |
46 | WARN_ON(total_trans == 0); | |
47 | total_trans--; | |
8fd17795 | 48 | list_del_init(&transaction->list); |
2c90e5d6 CM |
49 | memset(transaction, 0, sizeof(*transaction)); |
50 | kmem_cache_free(btrfs_transaction_cachep, transaction); | |
78fae27e | 51 | } |
79154b1b CM |
52 | } |
53 | ||
80b6794d | 54 | static noinline int join_transaction(struct btrfs_root *root) |
79154b1b CM |
55 | { |
56 | struct btrfs_transaction *cur_trans; | |
57 | cur_trans = root->fs_info->running_transaction; | |
58 | if (!cur_trans) { | |
2c90e5d6 CM |
59 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
60 | GFP_NOFS); | |
78fae27e | 61 | total_trans++; |
79154b1b | 62 | BUG_ON(!cur_trans); |
0f7d52f4 | 63 | root->fs_info->generation++; |
e18e4809 | 64 | root->fs_info->last_alloc = 0; |
4529ba49 | 65 | root->fs_info->last_data_alloc = 0; |
15ee9bc7 JB |
66 | cur_trans->num_writers = 1; |
67 | cur_trans->num_joined = 0; | |
0f7d52f4 | 68 | cur_trans->transid = root->fs_info->generation; |
79154b1b CM |
69 | init_waitqueue_head(&cur_trans->writer_wait); |
70 | init_waitqueue_head(&cur_trans->commit_wait); | |
71 | cur_trans->in_commit = 0; | |
f9295749 | 72 | cur_trans->blocked = 0; |
d5719762 | 73 | cur_trans->use_count = 1; |
79154b1b | 74 | cur_trans->commit_done = 0; |
08607c1b | 75 | cur_trans->start_time = get_seconds(); |
3063d29f | 76 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
8fd17795 | 77 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
d1310b2e | 78 | extent_io_tree_init(&cur_trans->dirty_pages, |
5f39d397 CM |
79 | root->fs_info->btree_inode->i_mapping, |
80 | GFP_NOFS); | |
48ec2cf8 CM |
81 | spin_lock(&root->fs_info->new_trans_lock); |
82 | root->fs_info->running_transaction = cur_trans; | |
83 | spin_unlock(&root->fs_info->new_trans_lock); | |
15ee9bc7 JB |
84 | } else { |
85 | cur_trans->num_writers++; | |
86 | cur_trans->num_joined++; | |
79154b1b | 87 | } |
15ee9bc7 | 88 | |
79154b1b CM |
89 | return 0; |
90 | } | |
91 | ||
80b6794d | 92 | static noinline int record_root_in_trans(struct btrfs_root *root) |
6702ed49 | 93 | { |
31153d81 | 94 | struct dirty_root *dirty; |
6702ed49 CM |
95 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
96 | if (root->ref_cows && root->last_trans < running_trans_id) { | |
97 | WARN_ON(root == root->fs_info->extent_root); | |
98 | if (root->root_item.refs != 0) { | |
99 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, | |
100 | (unsigned long)root->root_key.objectid, | |
101 | BTRFS_ROOT_TRANS_TAG); | |
31153d81 YZ |
102 | |
103 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
104 | BUG_ON(!dirty); | |
105 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); | |
106 | BUG_ON(!dirty->root); | |
107 | ||
108 | dirty->latest_root = root; | |
109 | INIT_LIST_HEAD(&dirty->list); | |
31153d81 | 110 | |
925baedd | 111 | root->commit_root = btrfs_root_node(root); |
017e5369 | 112 | root->dirty_root = dirty; |
31153d81 YZ |
113 | |
114 | memcpy(dirty->root, root, sizeof(*root)); | |
017e5369 CM |
115 | dirty->root->ref_tree = &root->ref_tree_struct; |
116 | ||
31153d81 YZ |
117 | spin_lock_init(&dirty->root->node_lock); |
118 | mutex_init(&dirty->root->objectid_mutex); | |
119 | dirty->root->node = root->commit_root; | |
120 | dirty->root->commit_root = NULL; | |
6702ed49 CM |
121 | } else { |
122 | WARN_ON(1); | |
123 | } | |
124 | root->last_trans = running_trans_id; | |
125 | } | |
126 | return 0; | |
127 | } | |
128 | ||
f9295749 CM |
129 | struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, |
130 | int num_blocks, int join) | |
79154b1b | 131 | { |
2c90e5d6 CM |
132 | struct btrfs_trans_handle *h = |
133 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); | |
f9295749 | 134 | struct btrfs_transaction *cur_trans; |
79154b1b CM |
135 | int ret; |
136 | ||
137 | mutex_lock(&root->fs_info->trans_mutex); | |
f9295749 CM |
138 | cur_trans = root->fs_info->running_transaction; |
139 | if (cur_trans && cur_trans->blocked && !join) { | |
140 | DEFINE_WAIT(wait); | |
141 | cur_trans->use_count++; | |
142 | while(1) { | |
143 | prepare_to_wait(&root->fs_info->transaction_wait, &wait, | |
144 | TASK_UNINTERRUPTIBLE); | |
145 | if (cur_trans->blocked) { | |
146 | mutex_unlock(&root->fs_info->trans_mutex); | |
147 | schedule(); | |
148 | mutex_lock(&root->fs_info->trans_mutex); | |
149 | finish_wait(&root->fs_info->transaction_wait, | |
150 | &wait); | |
151 | } else { | |
152 | finish_wait(&root->fs_info->transaction_wait, | |
153 | &wait); | |
154 | break; | |
155 | } | |
156 | } | |
157 | put_transaction(cur_trans); | |
158 | } | |
79154b1b CM |
159 | ret = join_transaction(root); |
160 | BUG_ON(ret); | |
0f7d52f4 | 161 | |
6702ed49 CM |
162 | record_root_in_trans(root); |
163 | h->transid = root->fs_info->running_transaction->transid; | |
79154b1b CM |
164 | h->transaction = root->fs_info->running_transaction; |
165 | h->blocks_reserved = num_blocks; | |
166 | h->blocks_used = 0; | |
31f3c99b | 167 | h->block_group = NULL; |
26b8003f CM |
168 | h->alloc_exclude_nr = 0; |
169 | h->alloc_exclude_start = 0; | |
79154b1b CM |
170 | root->fs_info->running_transaction->use_count++; |
171 | mutex_unlock(&root->fs_info->trans_mutex); | |
172 | return h; | |
173 | } | |
174 | ||
f9295749 CM |
175 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
176 | int num_blocks) | |
177 | { | |
178 | return start_transaction(root, num_blocks, 0); | |
179 | } | |
180 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, | |
181 | int num_blocks) | |
182 | { | |
183 | return start_transaction(root, num_blocks, 1); | |
184 | } | |
185 | ||
89ce8a63 CM |
186 | static noinline int wait_for_commit(struct btrfs_root *root, |
187 | struct btrfs_transaction *commit) | |
188 | { | |
189 | DEFINE_WAIT(wait); | |
190 | mutex_lock(&root->fs_info->trans_mutex); | |
191 | while(!commit->commit_done) { | |
192 | prepare_to_wait(&commit->commit_wait, &wait, | |
193 | TASK_UNINTERRUPTIBLE); | |
194 | if (commit->commit_done) | |
195 | break; | |
196 | mutex_unlock(&root->fs_info->trans_mutex); | |
197 | schedule(); | |
198 | mutex_lock(&root->fs_info->trans_mutex); | |
199 | } | |
200 | mutex_unlock(&root->fs_info->trans_mutex); | |
201 | finish_wait(&commit->commit_wait, &wait); | |
202 | return 0; | |
203 | } | |
204 | ||
ab78c84d CM |
205 | void btrfs_throttle(struct btrfs_root *root) |
206 | { | |
207 | struct btrfs_fs_info *info = root->fs_info; | |
208 | ||
209 | harder: | |
210 | if (atomic_read(&info->throttles)) { | |
211 | DEFINE_WAIT(wait); | |
212 | int thr; | |
213 | int harder_count = 0; | |
214 | thr = atomic_read(&info->throttle_gen); | |
215 | ||
216 | do { | |
217 | prepare_to_wait(&info->transaction_throttle, | |
218 | &wait, TASK_UNINTERRUPTIBLE); | |
219 | if (!atomic_read(&info->throttles)) { | |
220 | finish_wait(&info->transaction_throttle, &wait); | |
221 | break; | |
222 | } | |
223 | schedule(); | |
224 | finish_wait(&info->transaction_throttle, &wait); | |
225 | } while (thr == atomic_read(&info->throttle_gen)); | |
226 | ||
227 | if (harder_count < 5 && | |
228 | info->total_ref_cache_size > 5 * 1024 * 1024) { | |
229 | harder_count++; | |
230 | goto harder; | |
231 | } | |
232 | ||
233 | if (harder_count < 10 && | |
234 | info->total_ref_cache_size > 10 * 1024 * 1024) { | |
235 | harder_count++; | |
236 | goto harder; | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
89ce8a63 CM |
241 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
242 | struct btrfs_root *root, int throttle) | |
79154b1b CM |
243 | { |
244 | struct btrfs_transaction *cur_trans; | |
ab78c84d | 245 | struct btrfs_fs_info *info = root->fs_info; |
d6e4a428 | 246 | |
ab78c84d CM |
247 | mutex_lock(&info->trans_mutex); |
248 | cur_trans = info->running_transaction; | |
ccd467d6 | 249 | WARN_ON(cur_trans != trans->transaction); |
d5719762 | 250 | WARN_ON(cur_trans->num_writers < 1); |
ccd467d6 | 251 | cur_trans->num_writers--; |
89ce8a63 | 252 | |
79154b1b CM |
253 | if (waitqueue_active(&cur_trans->writer_wait)) |
254 | wake_up(&cur_trans->writer_wait); | |
79154b1b | 255 | put_transaction(cur_trans); |
ab78c84d | 256 | mutex_unlock(&info->trans_mutex); |
d6025579 | 257 | memset(trans, 0, sizeof(*trans)); |
2c90e5d6 | 258 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
ab78c84d CM |
259 | |
260 | if (throttle) | |
261 | btrfs_throttle(root); | |
262 | ||
79154b1b CM |
263 | return 0; |
264 | } | |
265 | ||
89ce8a63 CM |
266 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
267 | struct btrfs_root *root) | |
268 | { | |
269 | return __btrfs_end_transaction(trans, root, 0); | |
270 | } | |
271 | ||
272 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, | |
273 | struct btrfs_root *root) | |
274 | { | |
275 | return __btrfs_end_transaction(trans, root, 1); | |
276 | } | |
277 | ||
79154b1b CM |
278 | |
279 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, | |
280 | struct btrfs_root *root) | |
281 | { | |
7c4452b9 | 282 | int ret; |
7c4452b9 CM |
283 | int err; |
284 | int werr = 0; | |
d1310b2e | 285 | struct extent_io_tree *dirty_pages; |
7c4452b9 | 286 | struct page *page; |
7c4452b9 | 287 | struct inode *btree_inode = root->fs_info->btree_inode; |
5f39d397 CM |
288 | u64 start; |
289 | u64 end; | |
290 | unsigned long index; | |
7c4452b9 CM |
291 | |
292 | if (!trans || !trans->transaction) { | |
293 | return filemap_write_and_wait(btree_inode->i_mapping); | |
294 | } | |
295 | dirty_pages = &trans->transaction->dirty_pages; | |
296 | while(1) { | |
5f39d397 CM |
297 | ret = find_first_extent_bit(dirty_pages, 0, &start, &end, |
298 | EXTENT_DIRTY); | |
299 | if (ret) | |
7c4452b9 | 300 | break; |
5f39d397 CM |
301 | clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); |
302 | while(start <= end) { | |
303 | index = start >> PAGE_CACHE_SHIFT; | |
35ebb934 | 304 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
5f39d397 | 305 | page = find_lock_page(btree_inode->i_mapping, index); |
7c4452b9 CM |
306 | if (!page) |
307 | continue; | |
6702ed49 CM |
308 | if (PageWriteback(page)) { |
309 | if (PageDirty(page)) | |
310 | wait_on_page_writeback(page); | |
311 | else { | |
312 | unlock_page(page); | |
313 | page_cache_release(page); | |
314 | continue; | |
315 | } | |
316 | } | |
7c4452b9 CM |
317 | err = write_one_page(page, 0); |
318 | if (err) | |
319 | werr = err; | |
320 | page_cache_release(page); | |
321 | } | |
322 | } | |
323 | err = filemap_fdatawait(btree_inode->i_mapping); | |
324 | if (err) | |
325 | werr = err; | |
326 | return werr; | |
79154b1b CM |
327 | } |
328 | ||
0b86a832 CM |
329 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
330 | struct btrfs_root *root) | |
79154b1b CM |
331 | { |
332 | int ret; | |
0b86a832 CM |
333 | u64 old_root_bytenr; |
334 | struct btrfs_root *tree_root = root->fs_info->tree_root; | |
79154b1b | 335 | |
0b86a832 | 336 | btrfs_write_dirty_block_groups(trans, root); |
79154b1b | 337 | while(1) { |
0b86a832 CM |
338 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
339 | if (old_root_bytenr == root->node->start) | |
79154b1b | 340 | break; |
0b86a832 CM |
341 | btrfs_set_root_bytenr(&root->root_item, |
342 | root->node->start); | |
343 | btrfs_set_root_level(&root->root_item, | |
344 | btrfs_header_level(root->node)); | |
79154b1b | 345 | ret = btrfs_update_root(trans, tree_root, |
0b86a832 CM |
346 | &root->root_key, |
347 | &root->root_item); | |
79154b1b | 348 | BUG_ON(ret); |
0b86a832 CM |
349 | btrfs_write_dirty_block_groups(trans, root); |
350 | } | |
351 | return 0; | |
352 | } | |
353 | ||
354 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, | |
355 | struct btrfs_root *root) | |
356 | { | |
357 | struct btrfs_fs_info *fs_info = root->fs_info; | |
358 | struct list_head *next; | |
359 | ||
360 | while(!list_empty(&fs_info->dirty_cowonly_roots)) { | |
361 | next = fs_info->dirty_cowonly_roots.next; | |
362 | list_del_init(next); | |
363 | root = list_entry(next, struct btrfs_root, dirty_list); | |
364 | update_cowonly_root(trans, root); | |
017e5369 CM |
365 | if (root->fs_info->closing) |
366 | btrfs_remove_leaf_refs(root); | |
79154b1b CM |
367 | } |
368 | return 0; | |
369 | } | |
370 | ||
5ce14bbc CM |
371 | int btrfs_add_dead_root(struct btrfs_root *root, |
372 | struct btrfs_root *latest, | |
373 | struct list_head *dead_list) | |
5eda7b5e CM |
374 | { |
375 | struct dirty_root *dirty; | |
376 | ||
377 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
378 | if (!dirty) | |
379 | return -ENOMEM; | |
5eda7b5e | 380 | dirty->root = root; |
5ce14bbc | 381 | dirty->latest_root = latest; |
5eda7b5e CM |
382 | list_add(&dirty->list, dead_list); |
383 | return 0; | |
384 | } | |
385 | ||
80b6794d CM |
386 | static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, |
387 | struct radix_tree_root *radix, | |
388 | struct list_head *list) | |
0f7d52f4 CM |
389 | { |
390 | struct dirty_root *dirty; | |
391 | struct btrfs_root *gang[8]; | |
392 | struct btrfs_root *root; | |
393 | int i; | |
394 | int ret; | |
54aa1f4d | 395 | int err = 0; |
5eda7b5e | 396 | u32 refs; |
54aa1f4d | 397 | |
0f7d52f4 CM |
398 | while(1) { |
399 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, | |
400 | ARRAY_SIZE(gang), | |
401 | BTRFS_ROOT_TRANS_TAG); | |
402 | if (ret == 0) | |
403 | break; | |
404 | for (i = 0; i < ret; i++) { | |
405 | root = gang[i]; | |
2619ba1f CM |
406 | radix_tree_tag_clear(radix, |
407 | (unsigned long)root->root_key.objectid, | |
408 | BTRFS_ROOT_TRANS_TAG); | |
31153d81 YZ |
409 | |
410 | BUG_ON(!root->ref_tree); | |
017e5369 | 411 | dirty = root->dirty_root; |
31153d81 | 412 | |
0f7d52f4 | 413 | if (root->commit_root == root->node) { |
db94535d CM |
414 | WARN_ON(root->node->start != |
415 | btrfs_root_bytenr(&root->root_item)); | |
31153d81 | 416 | |
5f39d397 | 417 | free_extent_buffer(root->commit_root); |
0f7d52f4 | 418 | root->commit_root = NULL; |
31153d81 YZ |
419 | |
420 | kfree(dirty->root); | |
421 | kfree(dirty); | |
58176a96 JB |
422 | |
423 | /* make sure to update the root on disk | |
424 | * so we get any updates to the block used | |
425 | * counts | |
426 | */ | |
427 | err = btrfs_update_root(trans, | |
428 | root->fs_info->tree_root, | |
429 | &root->root_key, | |
430 | &root->root_item); | |
0f7d52f4 CM |
431 | continue; |
432 | } | |
9f3a7427 CM |
433 | |
434 | memset(&root->root_item.drop_progress, 0, | |
435 | sizeof(struct btrfs_disk_key)); | |
436 | root->root_item.drop_level = 0; | |
0f7d52f4 | 437 | root->commit_root = NULL; |
0f7d52f4 | 438 | root->root_key.offset = root->fs_info->generation; |
db94535d CM |
439 | btrfs_set_root_bytenr(&root->root_item, |
440 | root->node->start); | |
441 | btrfs_set_root_level(&root->root_item, | |
442 | btrfs_header_level(root->node)); | |
0f7d52f4 CM |
443 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
444 | &root->root_key, | |
445 | &root->root_item); | |
54aa1f4d CM |
446 | if (err) |
447 | break; | |
9f3a7427 CM |
448 | |
449 | refs = btrfs_root_refs(&dirty->root->root_item); | |
450 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); | |
5eda7b5e | 451 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
9f3a7427 CM |
452 | &dirty->root->root_key, |
453 | &dirty->root->root_item); | |
5eda7b5e CM |
454 | |
455 | BUG_ON(err); | |
9f3a7427 | 456 | if (refs == 1) { |
5eda7b5e | 457 | list_add(&dirty->list, list); |
9f3a7427 CM |
458 | } else { |
459 | WARN_ON(1); | |
31153d81 | 460 | free_extent_buffer(dirty->root->node); |
9f3a7427 | 461 | kfree(dirty->root); |
5eda7b5e | 462 | kfree(dirty); |
9f3a7427 | 463 | } |
0f7d52f4 CM |
464 | } |
465 | } | |
54aa1f4d | 466 | return err; |
0f7d52f4 CM |
467 | } |
468 | ||
e9d0b13b CM |
469 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
470 | { | |
471 | struct btrfs_fs_info *info = root->fs_info; | |
472 | int ret; | |
473 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 474 | unsigned long nr; |
e9d0b13b | 475 | |
a2135011 | 476 | smp_mb(); |
e9d0b13b CM |
477 | if (root->defrag_running) |
478 | return 0; | |
e9d0b13b | 479 | trans = btrfs_start_transaction(root, 1); |
6b80053d | 480 | while (1) { |
e9d0b13b CM |
481 | root->defrag_running = 1; |
482 | ret = btrfs_defrag_leaves(trans, root, cacheonly); | |
d3c2fdcf | 483 | nr = trans->blocks_used; |
e9d0b13b | 484 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 485 | btrfs_btree_balance_dirty(info->tree_root, nr); |
e9d0b13b CM |
486 | cond_resched(); |
487 | ||
e9d0b13b | 488 | trans = btrfs_start_transaction(root, 1); |
3f157a2f | 489 | if (root->fs_info->closing || ret != -EAGAIN) |
e9d0b13b CM |
490 | break; |
491 | } | |
492 | root->defrag_running = 0; | |
a2135011 | 493 | smp_mb(); |
e9d0b13b CM |
494 | btrfs_end_transaction(trans, root); |
495 | return 0; | |
496 | } | |
497 | ||
80b6794d CM |
498 | static noinline int drop_dirty_roots(struct btrfs_root *tree_root, |
499 | struct list_head *list) | |
0f7d52f4 CM |
500 | { |
501 | struct dirty_root *dirty; | |
502 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 503 | unsigned long nr; |
db94535d CM |
504 | u64 num_bytes; |
505 | u64 bytes_used; | |
54aa1f4d | 506 | int ret = 0; |
9f3a7427 CM |
507 | int err; |
508 | ||
0f7d52f4 | 509 | while(!list_empty(list)) { |
58176a96 JB |
510 | struct btrfs_root *root; |
511 | ||
017e5369 | 512 | dirty = list_entry(list->prev, struct dirty_root, list); |
0f7d52f4 | 513 | list_del_init(&dirty->list); |
5eda7b5e | 514 | |
db94535d | 515 | num_bytes = btrfs_root_used(&dirty->root->root_item); |
58176a96 | 516 | root = dirty->latest_root; |
a2135011 | 517 | atomic_inc(&root->fs_info->throttles); |
58176a96 | 518 | |
a2135011 | 519 | mutex_lock(&root->fs_info->drop_mutex); |
9f3a7427 CM |
520 | while(1) { |
521 | trans = btrfs_start_transaction(tree_root, 1); | |
522 | ret = btrfs_drop_snapshot(trans, dirty->root); | |
523 | if (ret != -EAGAIN) { | |
524 | break; | |
525 | } | |
58176a96 | 526 | |
9f3a7427 CM |
527 | err = btrfs_update_root(trans, |
528 | tree_root, | |
529 | &dirty->root->root_key, | |
530 | &dirty->root->root_item); | |
531 | if (err) | |
532 | ret = err; | |
d3c2fdcf | 533 | nr = trans->blocks_used; |
017e5369 | 534 | ret = btrfs_end_transaction(trans, tree_root); |
9f3a7427 | 535 | BUG_ON(ret); |
a2135011 CM |
536 | |
537 | mutex_unlock(&root->fs_info->drop_mutex); | |
d3c2fdcf | 538 | btrfs_btree_balance_dirty(tree_root, nr); |
4dc11904 | 539 | cond_resched(); |
a2135011 | 540 | mutex_lock(&root->fs_info->drop_mutex); |
9f3a7427 | 541 | } |
0f7d52f4 | 542 | BUG_ON(ret); |
a2135011 | 543 | atomic_dec(&root->fs_info->throttles); |
017e5369 | 544 | wake_up(&root->fs_info->transaction_throttle); |
58176a96 | 545 | |
a2135011 | 546 | mutex_lock(&root->fs_info->alloc_mutex); |
db94535d CM |
547 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
548 | bytes_used = btrfs_root_used(&root->root_item); | |
549 | if (num_bytes) { | |
58176a96 | 550 | record_root_in_trans(root); |
5f39d397 | 551 | btrfs_set_root_used(&root->root_item, |
db94535d | 552 | bytes_used - num_bytes); |
58176a96 | 553 | } |
a2135011 CM |
554 | mutex_unlock(&root->fs_info->alloc_mutex); |
555 | ||
9f3a7427 | 556 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
58176a96 JB |
557 | if (ret) { |
558 | BUG(); | |
54aa1f4d | 559 | break; |
58176a96 | 560 | } |
a2135011 CM |
561 | mutex_unlock(&root->fs_info->drop_mutex); |
562 | ||
d3c2fdcf | 563 | nr = trans->blocks_used; |
0f7d52f4 CM |
564 | ret = btrfs_end_transaction(trans, tree_root); |
565 | BUG_ON(ret); | |
5eda7b5e | 566 | |
f510cfec | 567 | free_extent_buffer(dirty->root->node); |
9f3a7427 | 568 | kfree(dirty->root); |
0f7d52f4 | 569 | kfree(dirty); |
d3c2fdcf CM |
570 | |
571 | btrfs_btree_balance_dirty(tree_root, nr); | |
4dc11904 | 572 | cond_resched(); |
0f7d52f4 | 573 | } |
54aa1f4d | 574 | return ret; |
0f7d52f4 CM |
575 | } |
576 | ||
80b6794d | 577 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
3063d29f CM |
578 | struct btrfs_fs_info *fs_info, |
579 | struct btrfs_pending_snapshot *pending) | |
580 | { | |
581 | struct btrfs_key key; | |
80b6794d | 582 | struct btrfs_root_item *new_root_item; |
3063d29f CM |
583 | struct btrfs_root *tree_root = fs_info->tree_root; |
584 | struct btrfs_root *root = pending->root; | |
585 | struct extent_buffer *tmp; | |
925baedd | 586 | struct extent_buffer *old; |
3063d29f | 587 | int ret; |
3b96362c | 588 | int namelen; |
3063d29f CM |
589 | u64 objectid; |
590 | ||
80b6794d CM |
591 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
592 | if (!new_root_item) { | |
593 | ret = -ENOMEM; | |
594 | goto fail; | |
595 | } | |
3063d29f CM |
596 | ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); |
597 | if (ret) | |
598 | goto fail; | |
599 | ||
80b6794d | 600 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
3063d29f CM |
601 | |
602 | key.objectid = objectid; | |
603 | key.offset = 1; | |
604 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
605 | ||
925baedd CM |
606 | old = btrfs_lock_root_node(root); |
607 | btrfs_cow_block(trans, root, old, NULL, 0, &old); | |
3063d29f | 608 | |
925baedd CM |
609 | btrfs_copy_root(trans, root, old, &tmp, objectid); |
610 | btrfs_tree_unlock(old); | |
611 | free_extent_buffer(old); | |
3063d29f | 612 | |
80b6794d CM |
613 | btrfs_set_root_bytenr(new_root_item, tmp->start); |
614 | btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); | |
3063d29f | 615 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
80b6794d | 616 | new_root_item); |
925baedd | 617 | btrfs_tree_unlock(tmp); |
3063d29f CM |
618 | free_extent_buffer(tmp); |
619 | if (ret) | |
620 | goto fail; | |
621 | ||
622 | /* | |
623 | * insert the directory item | |
624 | */ | |
625 | key.offset = (u64)-1; | |
3b96362c | 626 | namelen = strlen(pending->name); |
3063d29f | 627 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
3b96362c | 628 | pending->name, namelen, |
3063d29f | 629 | root->fs_info->sb->s_root->d_inode->i_ino, |
aec7477b | 630 | &key, BTRFS_FT_DIR, 0); |
3063d29f CM |
631 | |
632 | if (ret) | |
633 | goto fail; | |
634 | ||
635 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, | |
636 | pending->name, strlen(pending->name), objectid, | |
aec7477b | 637 | root->fs_info->sb->s_root->d_inode->i_ino, 0); |
3b96362c SW |
638 | |
639 | /* Invalidate existing dcache entry for new snapshot. */ | |
640 | btrfs_invalidate_dcache_root(root, pending->name, namelen); | |
641 | ||
3063d29f | 642 | fail: |
80b6794d | 643 | kfree(new_root_item); |
3063d29f CM |
644 | return ret; |
645 | } | |
646 | ||
80b6794d CM |
647 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
648 | struct btrfs_fs_info *fs_info) | |
3063d29f CM |
649 | { |
650 | struct btrfs_pending_snapshot *pending; | |
651 | struct list_head *head = &trans->transaction->pending_snapshots; | |
652 | int ret; | |
653 | ||
654 | while(!list_empty(head)) { | |
655 | pending = list_entry(head->next, | |
656 | struct btrfs_pending_snapshot, list); | |
657 | ret = create_pending_snapshot(trans, fs_info, pending); | |
658 | BUG_ON(ret); | |
659 | list_del(&pending->list); | |
660 | kfree(pending->name); | |
661 | kfree(pending); | |
662 | } | |
dc17ff8f CM |
663 | return 0; |
664 | } | |
665 | ||
79154b1b CM |
666 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
667 | struct btrfs_root *root) | |
668 | { | |
15ee9bc7 JB |
669 | unsigned long joined = 0; |
670 | unsigned long timeout = 1; | |
79154b1b | 671 | struct btrfs_transaction *cur_trans; |
8fd17795 | 672 | struct btrfs_transaction *prev_trans = NULL; |
0b86a832 | 673 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
0f7d52f4 | 674 | struct list_head dirty_fs_roots; |
d1310b2e | 675 | struct extent_io_tree *pinned_copy; |
79154b1b | 676 | DEFINE_WAIT(wait); |
15ee9bc7 | 677 | int ret; |
79154b1b | 678 | |
0f7d52f4 | 679 | INIT_LIST_HEAD(&dirty_fs_roots); |
d6e4a428 | 680 | |
79154b1b CM |
681 | mutex_lock(&root->fs_info->trans_mutex); |
682 | if (trans->transaction->in_commit) { | |
683 | cur_trans = trans->transaction; | |
684 | trans->transaction->use_count++; | |
ccd467d6 | 685 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b | 686 | btrfs_end_transaction(trans, root); |
ccd467d6 | 687 | |
79154b1b CM |
688 | ret = wait_for_commit(root, cur_trans); |
689 | BUG_ON(ret); | |
15ee9bc7 JB |
690 | |
691 | mutex_lock(&root->fs_info->trans_mutex); | |
79154b1b | 692 | put_transaction(cur_trans); |
15ee9bc7 JB |
693 | mutex_unlock(&root->fs_info->trans_mutex); |
694 | ||
79154b1b CM |
695 | return 0; |
696 | } | |
4313b399 CM |
697 | |
698 | pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); | |
699 | if (!pinned_copy) | |
700 | return -ENOMEM; | |
701 | ||
d1310b2e | 702 | extent_io_tree_init(pinned_copy, |
4313b399 CM |
703 | root->fs_info->btree_inode->i_mapping, GFP_NOFS); |
704 | ||
2c90e5d6 | 705 | trans->transaction->in_commit = 1; |
f9295749 | 706 | trans->transaction->blocked = 1; |
ccd467d6 CM |
707 | cur_trans = trans->transaction; |
708 | if (cur_trans->list.prev != &root->fs_info->trans_list) { | |
709 | prev_trans = list_entry(cur_trans->list.prev, | |
710 | struct btrfs_transaction, list); | |
711 | if (!prev_trans->commit_done) { | |
712 | prev_trans->use_count++; | |
ccd467d6 CM |
713 | mutex_unlock(&root->fs_info->trans_mutex); |
714 | ||
715 | wait_for_commit(root, prev_trans); | |
ccd467d6 | 716 | |
ccd467d6 | 717 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 | 718 | put_transaction(prev_trans); |
ccd467d6 CM |
719 | } |
720 | } | |
15ee9bc7 JB |
721 | |
722 | do { | |
723 | joined = cur_trans->num_joined; | |
2c90e5d6 | 724 | WARN_ON(cur_trans != trans->transaction); |
15ee9bc7 | 725 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
79154b1b | 726 | TASK_UNINTERRUPTIBLE); |
15ee9bc7 JB |
727 | |
728 | if (cur_trans->num_writers > 1) | |
729 | timeout = MAX_SCHEDULE_TIMEOUT; | |
730 | else | |
731 | timeout = 1; | |
732 | ||
79154b1b | 733 | mutex_unlock(&root->fs_info->trans_mutex); |
15ee9bc7 JB |
734 | |
735 | schedule_timeout(timeout); | |
736 | ||
79154b1b | 737 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 JB |
738 | finish_wait(&cur_trans->writer_wait, &wait); |
739 | } while (cur_trans->num_writers > 1 || | |
740 | (cur_trans->num_joined != joined)); | |
741 | ||
3063d29f CM |
742 | ret = create_pending_snapshots(trans, root->fs_info); |
743 | BUG_ON(ret); | |
744 | ||
2c90e5d6 | 745 | WARN_ON(cur_trans != trans->transaction); |
dc17ff8f | 746 | |
54aa1f4d CM |
747 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
748 | &dirty_fs_roots); | |
749 | BUG_ON(ret); | |
750 | ||
79154b1b CM |
751 | ret = btrfs_commit_tree_roots(trans, root); |
752 | BUG_ON(ret); | |
54aa1f4d | 753 | |
78fae27e | 754 | cur_trans = root->fs_info->running_transaction; |
cee36a03 | 755 | spin_lock(&root->fs_info->new_trans_lock); |
78fae27e | 756 | root->fs_info->running_transaction = NULL; |
cee36a03 | 757 | spin_unlock(&root->fs_info->new_trans_lock); |
4b52dff6 CM |
758 | btrfs_set_super_generation(&root->fs_info->super_copy, |
759 | cur_trans->transid); | |
760 | btrfs_set_super_root(&root->fs_info->super_copy, | |
db94535d CM |
761 | root->fs_info->tree_root->node->start); |
762 | btrfs_set_super_root_level(&root->fs_info->super_copy, | |
763 | btrfs_header_level(root->fs_info->tree_root->node)); | |
5f39d397 | 764 | |
0b86a832 CM |
765 | btrfs_set_super_chunk_root(&root->fs_info->super_copy, |
766 | chunk_root->node->start); | |
767 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, | |
768 | btrfs_header_level(chunk_root->node)); | |
a061fc8d CM |
769 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
770 | sizeof(root->fs_info->super_copy)); | |
ccd467d6 | 771 | |
4313b399 | 772 | btrfs_copy_pinned(root, pinned_copy); |
ccd467d6 | 773 | |
f9295749 | 774 | trans->transaction->blocked = 0; |
e6dcd2dc | 775 | wake_up(&root->fs_info->transaction_throttle); |
f9295749 | 776 | wake_up(&root->fs_info->transaction_wait); |
e6dcd2dc | 777 | |
78fae27e | 778 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b CM |
779 | ret = btrfs_write_and_wait_transaction(trans, root); |
780 | BUG_ON(ret); | |
79154b1b | 781 | write_ctree_super(trans, root); |
4313b399 | 782 | |
4313b399 | 783 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
78fae27e | 784 | mutex_lock(&root->fs_info->trans_mutex); |
4313b399 CM |
785 | |
786 | kfree(pinned_copy); | |
787 | ||
2c90e5d6 | 788 | cur_trans->commit_done = 1; |
15ee9bc7 | 789 | root->fs_info->last_trans_committed = cur_trans->transid; |
2c90e5d6 | 790 | wake_up(&cur_trans->commit_wait); |
78fae27e | 791 | put_transaction(cur_trans); |
79154b1b | 792 | put_transaction(cur_trans); |
58176a96 | 793 | |
facda1e7 CM |
794 | if (root->fs_info->closing) |
795 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); | |
796 | else | |
797 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); | |
58176a96 | 798 | |
78fae27e | 799 | mutex_unlock(&root->fs_info->trans_mutex); |
2c90e5d6 | 800 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
79154b1b | 801 | |
facda1e7 | 802 | if (root->fs_info->closing) { |
facda1e7 | 803 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
facda1e7 | 804 | } |
79154b1b CM |
805 | return ret; |
806 | } | |
807 | ||
e9d0b13b CM |
808 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
809 | { | |
810 | struct list_head dirty_roots; | |
811 | INIT_LIST_HEAD(&dirty_roots); | |
a74a4b97 | 812 | again: |
e9d0b13b CM |
813 | mutex_lock(&root->fs_info->trans_mutex); |
814 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); | |
815 | mutex_unlock(&root->fs_info->trans_mutex); | |
816 | ||
817 | if (!list_empty(&dirty_roots)) { | |
818 | drop_dirty_roots(root, &dirty_roots); | |
a74a4b97 | 819 | goto again; |
e9d0b13b CM |
820 | } |
821 | return 0; | |
822 | } | |
08607c1b | 823 |