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