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