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