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