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