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