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> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
34088780 | 21 | #include <linux/sched.h> |
d3c2fdcf | 22 | #include <linux/writeback.h> |
5f39d397 | 23 | #include <linux/pagemap.h> |
5f2cc086 | 24 | #include <linux/blkdev.h> |
79154b1b CM |
25 | #include "ctree.h" |
26 | #include "disk-io.h" | |
27 | #include "transaction.h" | |
925baedd | 28 | #include "locking.h" |
e02119d5 | 29 | #include "tree-log.h" |
581bb050 | 30 | #include "inode-map.h" |
79154b1b | 31 | |
0f7d52f4 CM |
32 | #define BTRFS_ROOT_TRANS_TAG 0 |
33 | ||
80b6794d | 34 | static noinline void put_transaction(struct btrfs_transaction *transaction) |
79154b1b | 35 | { |
13c5a93e JB |
36 | WARN_ON(atomic_read(&transaction->use_count) == 0); |
37 | if (atomic_dec_and_test(&transaction->use_count)) { | |
2c90e5d6 CM |
38 | memset(transaction, 0, sizeof(*transaction)); |
39 | kmem_cache_free(btrfs_transaction_cachep, transaction); | |
78fae27e | 40 | } |
79154b1b CM |
41 | } |
42 | ||
817d52f8 JB |
43 | static noinline void switch_commit_root(struct btrfs_root *root) |
44 | { | |
817d52f8 JB |
45 | free_extent_buffer(root->commit_root); |
46 | root->commit_root = btrfs_root_node(root); | |
817d52f8 JB |
47 | } |
48 | ||
d352ac68 CM |
49 | /* |
50 | * either allocate a new transaction or hop into the existing one | |
51 | */ | |
80b6794d | 52 | static noinline int join_transaction(struct btrfs_root *root) |
79154b1b CM |
53 | { |
54 | struct btrfs_transaction *cur_trans; | |
55 | cur_trans = root->fs_info->running_transaction; | |
56 | if (!cur_trans) { | |
2c90e5d6 CM |
57 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
58 | GFP_NOFS); | |
db5b493a TI |
59 | if (!cur_trans) |
60 | return -ENOMEM; | |
0f7d52f4 | 61 | root->fs_info->generation++; |
13c5a93e | 62 | atomic_set(&cur_trans->num_writers, 1); |
15ee9bc7 | 63 | cur_trans->num_joined = 0; |
0f7d52f4 | 64 | cur_trans->transid = root->fs_info->generation; |
79154b1b CM |
65 | init_waitqueue_head(&cur_trans->writer_wait); |
66 | init_waitqueue_head(&cur_trans->commit_wait); | |
67 | cur_trans->in_commit = 0; | |
f9295749 | 68 | cur_trans->blocked = 0; |
13c5a93e | 69 | atomic_set(&cur_trans->use_count, 1); |
79154b1b | 70 | cur_trans->commit_done = 0; |
08607c1b | 71 | cur_trans->start_time = get_seconds(); |
56bec294 | 72 | |
6bef4d31 | 73 | cur_trans->delayed_refs.root = RB_ROOT; |
56bec294 | 74 | cur_trans->delayed_refs.num_entries = 0; |
c3e69d58 CM |
75 | cur_trans->delayed_refs.num_heads_ready = 0; |
76 | cur_trans->delayed_refs.num_heads = 0; | |
56bec294 | 77 | cur_trans->delayed_refs.flushing = 0; |
c3e69d58 | 78 | cur_trans->delayed_refs.run_delayed_start = 0; |
56bec294 CM |
79 | spin_lock_init(&cur_trans->delayed_refs.lock); |
80 | ||
3063d29f | 81 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
8fd17795 | 82 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
d1310b2e | 83 | extent_io_tree_init(&cur_trans->dirty_pages, |
5f39d397 CM |
84 | root->fs_info->btree_inode->i_mapping, |
85 | GFP_NOFS); | |
48ec2cf8 CM |
86 | spin_lock(&root->fs_info->new_trans_lock); |
87 | root->fs_info->running_transaction = cur_trans; | |
88 | spin_unlock(&root->fs_info->new_trans_lock); | |
15ee9bc7 | 89 | } else { |
13c5a93e | 90 | atomic_inc(&cur_trans->num_writers); |
15ee9bc7 | 91 | cur_trans->num_joined++; |
79154b1b | 92 | } |
15ee9bc7 | 93 | |
79154b1b CM |
94 | return 0; |
95 | } | |
96 | ||
d352ac68 | 97 | /* |
d397712b CM |
98 | * this does all the record keeping required to make sure that a reference |
99 | * counted root is properly recorded in a given transaction. This is required | |
100 | * to make sure the old root from before we joined the transaction is deleted | |
101 | * when the transaction commits | |
d352ac68 | 102 | */ |
5d4f98a2 YZ |
103 | static noinline int record_root_in_trans(struct btrfs_trans_handle *trans, |
104 | struct btrfs_root *root) | |
6702ed49 | 105 | { |
5d4f98a2 | 106 | if (root->ref_cows && root->last_trans < trans->transid) { |
6702ed49 | 107 | WARN_ON(root == root->fs_info->extent_root); |
5d4f98a2 YZ |
108 | WARN_ON(root->commit_root != root->node); |
109 | ||
110 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, | |
111 | (unsigned long)root->root_key.objectid, | |
112 | BTRFS_ROOT_TRANS_TAG); | |
113 | root->last_trans = trans->transid; | |
114 | btrfs_init_reloc_root(trans, root); | |
115 | } | |
116 | return 0; | |
117 | } | |
bcc63abb | 118 | |
5d4f98a2 YZ |
119 | int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, |
120 | struct btrfs_root *root) | |
121 | { | |
122 | if (!root->ref_cows) | |
123 | return 0; | |
bcc63abb | 124 | |
5d4f98a2 YZ |
125 | mutex_lock(&root->fs_info->trans_mutex); |
126 | if (root->last_trans == trans->transid) { | |
127 | mutex_unlock(&root->fs_info->trans_mutex); | |
128 | return 0; | |
6702ed49 | 129 | } |
5d4f98a2 YZ |
130 | |
131 | record_root_in_trans(trans, root); | |
132 | mutex_unlock(&root->fs_info->trans_mutex); | |
6702ed49 CM |
133 | return 0; |
134 | } | |
135 | ||
d352ac68 CM |
136 | /* wait for commit against the current transaction to become unblocked |
137 | * when this is done, it is safe to start a new transaction, but the current | |
138 | * transaction might not be fully on disk. | |
139 | */ | |
37d1aeee | 140 | static void wait_current_trans(struct btrfs_root *root) |
79154b1b | 141 | { |
f9295749 | 142 | struct btrfs_transaction *cur_trans; |
79154b1b | 143 | |
f9295749 | 144 | cur_trans = root->fs_info->running_transaction; |
37d1aeee | 145 | if (cur_trans && cur_trans->blocked) { |
f9295749 | 146 | DEFINE_WAIT(wait); |
13c5a93e | 147 | atomic_inc(&cur_trans->use_count); |
d397712b | 148 | while (1) { |
f9295749 CM |
149 | prepare_to_wait(&root->fs_info->transaction_wait, &wait, |
150 | TASK_UNINTERRUPTIBLE); | |
471fa17d | 151 | if (!cur_trans->blocked) |
f9295749 | 152 | break; |
471fa17d ZL |
153 | mutex_unlock(&root->fs_info->trans_mutex); |
154 | schedule(); | |
155 | mutex_lock(&root->fs_info->trans_mutex); | |
f9295749 | 156 | } |
471fa17d | 157 | finish_wait(&root->fs_info->transaction_wait, &wait); |
f9295749 CM |
158 | put_transaction(cur_trans); |
159 | } | |
37d1aeee CM |
160 | } |
161 | ||
249ac1e5 JB |
162 | enum btrfs_trans_type { |
163 | TRANS_START, | |
164 | TRANS_JOIN, | |
165 | TRANS_USERSPACE, | |
0af3d00b | 166 | TRANS_JOIN_NOLOCK, |
249ac1e5 JB |
167 | }; |
168 | ||
a22285a6 YZ |
169 | static int may_wait_transaction(struct btrfs_root *root, int type) |
170 | { | |
171 | if (!root->fs_info->log_root_recovering && | |
172 | ((type == TRANS_START && !root->fs_info->open_ioctl_trans) || | |
173 | type == TRANS_USERSPACE)) | |
174 | return 1; | |
175 | return 0; | |
176 | } | |
177 | ||
e02119d5 | 178 | static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, |
a22285a6 | 179 | u64 num_items, int type) |
37d1aeee | 180 | { |
a22285a6 YZ |
181 | struct btrfs_trans_handle *h; |
182 | struct btrfs_transaction *cur_trans; | |
06d5a589 | 183 | int retries = 0; |
37d1aeee | 184 | int ret; |
acce952b | 185 | |
186 | if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) | |
187 | return ERR_PTR(-EROFS); | |
a22285a6 YZ |
188 | again: |
189 | h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); | |
190 | if (!h) | |
191 | return ERR_PTR(-ENOMEM); | |
37d1aeee | 192 | |
0af3d00b JB |
193 | if (type != TRANS_JOIN_NOLOCK) |
194 | mutex_lock(&root->fs_info->trans_mutex); | |
a22285a6 | 195 | if (may_wait_transaction(root, type)) |
37d1aeee | 196 | wait_current_trans(root); |
a22285a6 | 197 | |
79154b1b | 198 | ret = join_transaction(root); |
db5b493a | 199 | if (ret < 0) { |
6e8df2ae | 200 | kmem_cache_free(btrfs_trans_handle_cachep, h); |
db5b493a TI |
201 | if (type != TRANS_JOIN_NOLOCK) |
202 | mutex_unlock(&root->fs_info->trans_mutex); | |
203 | return ERR_PTR(ret); | |
204 | } | |
0f7d52f4 | 205 | |
a22285a6 | 206 | cur_trans = root->fs_info->running_transaction; |
13c5a93e | 207 | atomic_inc(&cur_trans->use_count); |
0af3d00b JB |
208 | if (type != TRANS_JOIN_NOLOCK) |
209 | mutex_unlock(&root->fs_info->trans_mutex); | |
a22285a6 YZ |
210 | |
211 | h->transid = cur_trans->transid; | |
212 | h->transaction = cur_trans; | |
79154b1b | 213 | h->blocks_used = 0; |
d2fb3437 | 214 | h->block_group = 0; |
a22285a6 | 215 | h->bytes_reserved = 0; |
56bec294 | 216 | h->delayed_ref_updates = 0; |
f0486c68 | 217 | h->block_rsv = NULL; |
b7ec40d7 | 218 | |
a22285a6 YZ |
219 | smp_mb(); |
220 | if (cur_trans->blocked && may_wait_transaction(root, type)) { | |
221 | btrfs_commit_transaction(h, root); | |
222 | goto again; | |
223 | } | |
224 | ||
225 | if (num_items > 0) { | |
8bb8ab2e | 226 | ret = btrfs_trans_reserve_metadata(h, root, num_items); |
06d5a589 JB |
227 | if (ret == -EAGAIN && !retries) { |
228 | retries++; | |
a22285a6 YZ |
229 | btrfs_commit_transaction(h, root); |
230 | goto again; | |
06d5a589 JB |
231 | } else if (ret == -EAGAIN) { |
232 | /* | |
233 | * We have already retried and got EAGAIN, so really we | |
234 | * don't have space, so set ret to -ENOSPC. | |
235 | */ | |
236 | ret = -ENOSPC; | |
a22285a6 | 237 | } |
06d5a589 | 238 | |
a22285a6 YZ |
239 | if (ret < 0) { |
240 | btrfs_end_transaction(h, root); | |
241 | return ERR_PTR(ret); | |
242 | } | |
243 | } | |
9ed74f2d | 244 | |
0af3d00b JB |
245 | if (type != TRANS_JOIN_NOLOCK) |
246 | mutex_lock(&root->fs_info->trans_mutex); | |
5d4f98a2 | 247 | record_root_in_trans(h, root); |
0af3d00b JB |
248 | if (type != TRANS_JOIN_NOLOCK) |
249 | mutex_unlock(&root->fs_info->trans_mutex); | |
a22285a6 YZ |
250 | |
251 | if (!current->journal_info && type != TRANS_USERSPACE) | |
252 | current->journal_info = h; | |
79154b1b CM |
253 | return h; |
254 | } | |
255 | ||
f9295749 | 256 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
a22285a6 | 257 | int num_items) |
f9295749 | 258 | { |
a22285a6 | 259 | return start_transaction(root, num_items, TRANS_START); |
f9295749 CM |
260 | } |
261 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, | |
262 | int num_blocks) | |
263 | { | |
a22285a6 | 264 | return start_transaction(root, 0, TRANS_JOIN); |
f9295749 CM |
265 | } |
266 | ||
0af3d00b JB |
267 | struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root, |
268 | int num_blocks) | |
269 | { | |
270 | return start_transaction(root, 0, TRANS_JOIN_NOLOCK); | |
271 | } | |
272 | ||
9ca9ee09 SW |
273 | struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, |
274 | int num_blocks) | |
275 | { | |
a22285a6 | 276 | return start_transaction(r, 0, TRANS_USERSPACE); |
9ca9ee09 SW |
277 | } |
278 | ||
d352ac68 | 279 | /* wait for a transaction commit to be fully complete */ |
89ce8a63 CM |
280 | static noinline int wait_for_commit(struct btrfs_root *root, |
281 | struct btrfs_transaction *commit) | |
282 | { | |
283 | DEFINE_WAIT(wait); | |
284 | mutex_lock(&root->fs_info->trans_mutex); | |
d397712b | 285 | while (!commit->commit_done) { |
89ce8a63 CM |
286 | prepare_to_wait(&commit->commit_wait, &wait, |
287 | TASK_UNINTERRUPTIBLE); | |
288 | if (commit->commit_done) | |
289 | break; | |
290 | mutex_unlock(&root->fs_info->trans_mutex); | |
291 | schedule(); | |
292 | mutex_lock(&root->fs_info->trans_mutex); | |
293 | } | |
294 | mutex_unlock(&root->fs_info->trans_mutex); | |
295 | finish_wait(&commit->commit_wait, &wait); | |
296 | return 0; | |
297 | } | |
298 | ||
46204592 SW |
299 | int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) |
300 | { | |
301 | struct btrfs_transaction *cur_trans = NULL, *t; | |
302 | int ret; | |
303 | ||
304 | mutex_lock(&root->fs_info->trans_mutex); | |
305 | ||
306 | ret = 0; | |
307 | if (transid) { | |
308 | if (transid <= root->fs_info->last_trans_committed) | |
309 | goto out_unlock; | |
310 | ||
311 | /* find specified transaction */ | |
312 | list_for_each_entry(t, &root->fs_info->trans_list, list) { | |
313 | if (t->transid == transid) { | |
314 | cur_trans = t; | |
315 | break; | |
316 | } | |
317 | if (t->transid > transid) | |
318 | break; | |
319 | } | |
320 | ret = -EINVAL; | |
321 | if (!cur_trans) | |
322 | goto out_unlock; /* bad transid */ | |
323 | } else { | |
324 | /* find newest transaction that is committing | committed */ | |
325 | list_for_each_entry_reverse(t, &root->fs_info->trans_list, | |
326 | list) { | |
327 | if (t->in_commit) { | |
328 | if (t->commit_done) | |
329 | goto out_unlock; | |
330 | cur_trans = t; | |
331 | break; | |
332 | } | |
333 | } | |
334 | if (!cur_trans) | |
335 | goto out_unlock; /* nothing committing|committed */ | |
336 | } | |
337 | ||
13c5a93e | 338 | atomic_inc(&cur_trans->use_count); |
46204592 SW |
339 | mutex_unlock(&root->fs_info->trans_mutex); |
340 | ||
341 | wait_for_commit(root, cur_trans); | |
342 | ||
343 | mutex_lock(&root->fs_info->trans_mutex); | |
344 | put_transaction(cur_trans); | |
345 | ret = 0; | |
346 | out_unlock: | |
347 | mutex_unlock(&root->fs_info->trans_mutex); | |
348 | return ret; | |
349 | } | |
350 | ||
5d4f98a2 | 351 | #if 0 |
d352ac68 | 352 | /* |
d397712b CM |
353 | * rate limit against the drop_snapshot code. This helps to slow down new |
354 | * operations if the drop_snapshot code isn't able to keep up. | |
d352ac68 | 355 | */ |
37d1aeee | 356 | static void throttle_on_drops(struct btrfs_root *root) |
ab78c84d CM |
357 | { |
358 | struct btrfs_fs_info *info = root->fs_info; | |
2dd3e67b | 359 | int harder_count = 0; |
ab78c84d | 360 | |
2dd3e67b | 361 | harder: |
ab78c84d CM |
362 | if (atomic_read(&info->throttles)) { |
363 | DEFINE_WAIT(wait); | |
364 | int thr; | |
ab78c84d CM |
365 | thr = atomic_read(&info->throttle_gen); |
366 | ||
367 | do { | |
368 | prepare_to_wait(&info->transaction_throttle, | |
369 | &wait, TASK_UNINTERRUPTIBLE); | |
370 | if (!atomic_read(&info->throttles)) { | |
371 | finish_wait(&info->transaction_throttle, &wait); | |
372 | break; | |
373 | } | |
374 | schedule(); | |
375 | finish_wait(&info->transaction_throttle, &wait); | |
376 | } while (thr == atomic_read(&info->throttle_gen)); | |
2dd3e67b CM |
377 | harder_count++; |
378 | ||
379 | if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && | |
380 | harder_count < 2) | |
381 | goto harder; | |
382 | ||
383 | if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && | |
384 | harder_count < 10) | |
385 | goto harder; | |
386 | ||
387 | if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && | |
388 | harder_count < 20) | |
389 | goto harder; | |
ab78c84d CM |
390 | } |
391 | } | |
5d4f98a2 | 392 | #endif |
ab78c84d | 393 | |
37d1aeee CM |
394 | void btrfs_throttle(struct btrfs_root *root) |
395 | { | |
396 | mutex_lock(&root->fs_info->trans_mutex); | |
9ca9ee09 SW |
397 | if (!root->fs_info->open_ioctl_trans) |
398 | wait_current_trans(root); | |
37d1aeee | 399 | mutex_unlock(&root->fs_info->trans_mutex); |
37d1aeee CM |
400 | } |
401 | ||
8929ecfa YZ |
402 | static int should_end_transaction(struct btrfs_trans_handle *trans, |
403 | struct btrfs_root *root) | |
404 | { | |
405 | int ret; | |
406 | ret = btrfs_block_rsv_check(trans, root, | |
407 | &root->fs_info->global_block_rsv, 0, 5); | |
408 | return ret ? 1 : 0; | |
409 | } | |
410 | ||
411 | int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, | |
412 | struct btrfs_root *root) | |
413 | { | |
414 | struct btrfs_transaction *cur_trans = trans->transaction; | |
415 | int updates; | |
416 | ||
417 | if (cur_trans->blocked || cur_trans->delayed_refs.flushing) | |
418 | return 1; | |
419 | ||
420 | updates = trans->delayed_ref_updates; | |
421 | trans->delayed_ref_updates = 0; | |
422 | if (updates) | |
423 | btrfs_run_delayed_refs(trans, root, updates); | |
424 | ||
425 | return should_end_transaction(trans, root); | |
426 | } | |
427 | ||
89ce8a63 | 428 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
0af3d00b | 429 | struct btrfs_root *root, int throttle, int lock) |
79154b1b | 430 | { |
8929ecfa | 431 | struct btrfs_transaction *cur_trans = trans->transaction; |
ab78c84d | 432 | struct btrfs_fs_info *info = root->fs_info; |
c3e69d58 CM |
433 | int count = 0; |
434 | ||
435 | while (count < 4) { | |
436 | unsigned long cur = trans->delayed_ref_updates; | |
437 | trans->delayed_ref_updates = 0; | |
438 | if (cur && | |
439 | trans->transaction->delayed_refs.num_heads_ready > 64) { | |
440 | trans->delayed_ref_updates = 0; | |
b7ec40d7 CM |
441 | |
442 | /* | |
443 | * do a full flush if the transaction is trying | |
444 | * to close | |
445 | */ | |
446 | if (trans->transaction->delayed_refs.flushing) | |
447 | cur = 0; | |
c3e69d58 CM |
448 | btrfs_run_delayed_refs(trans, root, cur); |
449 | } else { | |
450 | break; | |
451 | } | |
452 | count++; | |
56bec294 CM |
453 | } |
454 | ||
a22285a6 YZ |
455 | btrfs_trans_release_metadata(trans, root); |
456 | ||
0af3d00b | 457 | if (lock && !root->fs_info->open_ioctl_trans && |
8929ecfa YZ |
458 | should_end_transaction(trans, root)) |
459 | trans->transaction->blocked = 1; | |
460 | ||
0af3d00b | 461 | if (lock && cur_trans->blocked && !cur_trans->in_commit) { |
8929ecfa YZ |
462 | if (throttle) |
463 | return btrfs_commit_transaction(trans, root); | |
464 | else | |
465 | wake_up_process(info->transaction_kthread); | |
466 | } | |
467 | ||
8929ecfa | 468 | WARN_ON(cur_trans != info->running_transaction); |
13c5a93e JB |
469 | WARN_ON(atomic_read(&cur_trans->num_writers) < 1); |
470 | atomic_dec(&cur_trans->num_writers); | |
89ce8a63 | 471 | |
99d16cbc | 472 | smp_mb(); |
79154b1b CM |
473 | if (waitqueue_active(&cur_trans->writer_wait)) |
474 | wake_up(&cur_trans->writer_wait); | |
79154b1b | 475 | put_transaction(cur_trans); |
9ed74f2d JB |
476 | |
477 | if (current->journal_info == trans) | |
478 | current->journal_info = NULL; | |
d6025579 | 479 | memset(trans, 0, sizeof(*trans)); |
2c90e5d6 | 480 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
ab78c84d | 481 | |
24bbcf04 YZ |
482 | if (throttle) |
483 | btrfs_run_delayed_iputs(root); | |
484 | ||
79154b1b CM |
485 | return 0; |
486 | } | |
487 | ||
89ce8a63 CM |
488 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
489 | struct btrfs_root *root) | |
490 | { | |
0af3d00b | 491 | return __btrfs_end_transaction(trans, root, 0, 1); |
89ce8a63 CM |
492 | } |
493 | ||
494 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, | |
495 | struct btrfs_root *root) | |
496 | { | |
0af3d00b JB |
497 | return __btrfs_end_transaction(trans, root, 1, 1); |
498 | } | |
499 | ||
500 | int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, | |
501 | struct btrfs_root *root) | |
502 | { | |
503 | return __btrfs_end_transaction(trans, root, 0, 0); | |
89ce8a63 CM |
504 | } |
505 | ||
d352ac68 CM |
506 | /* |
507 | * when btree blocks are allocated, they have some corresponding bits set for | |
508 | * them in one of two extent_io trees. This is used to make sure all of | |
690587d1 | 509 | * those extents are sent to disk but does not wait on them |
d352ac68 | 510 | */ |
690587d1 | 511 | int btrfs_write_marked_extents(struct btrfs_root *root, |
8cef4e16 | 512 | struct extent_io_tree *dirty_pages, int mark) |
79154b1b | 513 | { |
7c4452b9 | 514 | int ret; |
777e6bd7 | 515 | int err = 0; |
7c4452b9 CM |
516 | int werr = 0; |
517 | struct page *page; | |
7c4452b9 | 518 | struct inode *btree_inode = root->fs_info->btree_inode; |
777e6bd7 | 519 | u64 start = 0; |
5f39d397 CM |
520 | u64 end; |
521 | unsigned long index; | |
7c4452b9 | 522 | |
d397712b | 523 | while (1) { |
777e6bd7 | 524 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, |
8cef4e16 | 525 | mark); |
5f39d397 | 526 | if (ret) |
7c4452b9 | 527 | break; |
d397712b | 528 | while (start <= end) { |
777e6bd7 CM |
529 | cond_resched(); |
530 | ||
5f39d397 | 531 | index = start >> PAGE_CACHE_SHIFT; |
35ebb934 | 532 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
4bef0848 | 533 | page = find_get_page(btree_inode->i_mapping, index); |
7c4452b9 CM |
534 | if (!page) |
535 | continue; | |
4bef0848 CM |
536 | |
537 | btree_lock_page_hook(page); | |
538 | if (!page->mapping) { | |
539 | unlock_page(page); | |
540 | page_cache_release(page); | |
541 | continue; | |
542 | } | |
543 | ||
6702ed49 CM |
544 | if (PageWriteback(page)) { |
545 | if (PageDirty(page)) | |
546 | wait_on_page_writeback(page); | |
547 | else { | |
548 | unlock_page(page); | |
549 | page_cache_release(page); | |
550 | continue; | |
551 | } | |
552 | } | |
7c4452b9 CM |
553 | err = write_one_page(page, 0); |
554 | if (err) | |
555 | werr = err; | |
556 | page_cache_release(page); | |
557 | } | |
558 | } | |
690587d1 CM |
559 | if (err) |
560 | werr = err; | |
561 | return werr; | |
562 | } | |
563 | ||
564 | /* | |
565 | * when btree blocks are allocated, they have some corresponding bits set for | |
566 | * them in one of two extent_io trees. This is used to make sure all of | |
567 | * those extents are on disk for transaction or log commit. We wait | |
568 | * on all the pages and clear them from the dirty pages state tree | |
569 | */ | |
570 | int btrfs_wait_marked_extents(struct btrfs_root *root, | |
8cef4e16 | 571 | struct extent_io_tree *dirty_pages, int mark) |
690587d1 CM |
572 | { |
573 | int ret; | |
574 | int err = 0; | |
575 | int werr = 0; | |
576 | struct page *page; | |
577 | struct inode *btree_inode = root->fs_info->btree_inode; | |
578 | u64 start = 0; | |
579 | u64 end; | |
580 | unsigned long index; | |
581 | ||
d397712b | 582 | while (1) { |
8cef4e16 YZ |
583 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, |
584 | mark); | |
777e6bd7 CM |
585 | if (ret) |
586 | break; | |
587 | ||
8cef4e16 | 588 | clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); |
d397712b | 589 | while (start <= end) { |
777e6bd7 CM |
590 | index = start >> PAGE_CACHE_SHIFT; |
591 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; | |
592 | page = find_get_page(btree_inode->i_mapping, index); | |
593 | if (!page) | |
594 | continue; | |
595 | if (PageDirty(page)) { | |
4bef0848 CM |
596 | btree_lock_page_hook(page); |
597 | wait_on_page_writeback(page); | |
777e6bd7 CM |
598 | err = write_one_page(page, 0); |
599 | if (err) | |
600 | werr = err; | |
601 | } | |
105d931d | 602 | wait_on_page_writeback(page); |
777e6bd7 CM |
603 | page_cache_release(page); |
604 | cond_resched(); | |
605 | } | |
606 | } | |
7c4452b9 CM |
607 | if (err) |
608 | werr = err; | |
609 | return werr; | |
79154b1b CM |
610 | } |
611 | ||
690587d1 CM |
612 | /* |
613 | * when btree blocks are allocated, they have some corresponding bits set for | |
614 | * them in one of two extent_io trees. This is used to make sure all of | |
615 | * those extents are on disk for transaction or log commit | |
616 | */ | |
617 | int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, | |
8cef4e16 | 618 | struct extent_io_tree *dirty_pages, int mark) |
690587d1 CM |
619 | { |
620 | int ret; | |
621 | int ret2; | |
622 | ||
8cef4e16 YZ |
623 | ret = btrfs_write_marked_extents(root, dirty_pages, mark); |
624 | ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); | |
690587d1 CM |
625 | return ret || ret2; |
626 | } | |
627 | ||
d0c803c4 CM |
628 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
629 | struct btrfs_root *root) | |
630 | { | |
631 | if (!trans || !trans->transaction) { | |
632 | struct inode *btree_inode; | |
633 | btree_inode = root->fs_info->btree_inode; | |
634 | return filemap_write_and_wait(btree_inode->i_mapping); | |
635 | } | |
636 | return btrfs_write_and_wait_marked_extents(root, | |
8cef4e16 YZ |
637 | &trans->transaction->dirty_pages, |
638 | EXTENT_DIRTY); | |
d0c803c4 CM |
639 | } |
640 | ||
d352ac68 CM |
641 | /* |
642 | * this is used to update the root pointer in the tree of tree roots. | |
643 | * | |
644 | * But, in the case of the extent allocation tree, updating the root | |
645 | * pointer may allocate blocks which may change the root of the extent | |
646 | * allocation tree. | |
647 | * | |
648 | * So, this loops and repeats and makes sure the cowonly root didn't | |
649 | * change while the root pointer was being updated in the metadata. | |
650 | */ | |
0b86a832 CM |
651 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
652 | struct btrfs_root *root) | |
79154b1b CM |
653 | { |
654 | int ret; | |
0b86a832 | 655 | u64 old_root_bytenr; |
86b9f2ec | 656 | u64 old_root_used; |
0b86a832 | 657 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
79154b1b | 658 | |
86b9f2ec | 659 | old_root_used = btrfs_root_used(&root->root_item); |
0b86a832 | 660 | btrfs_write_dirty_block_groups(trans, root); |
56bec294 | 661 | |
d397712b | 662 | while (1) { |
0b86a832 | 663 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
86b9f2ec YZ |
664 | if (old_root_bytenr == root->node->start && |
665 | old_root_used == btrfs_root_used(&root->root_item)) | |
79154b1b | 666 | break; |
87ef2bb4 | 667 | |
5d4f98a2 | 668 | btrfs_set_root_node(&root->root_item, root->node); |
79154b1b | 669 | ret = btrfs_update_root(trans, tree_root, |
0b86a832 CM |
670 | &root->root_key, |
671 | &root->root_item); | |
79154b1b | 672 | BUG_ON(ret); |
56bec294 | 673 | |
86b9f2ec | 674 | old_root_used = btrfs_root_used(&root->root_item); |
4a8c9a62 | 675 | ret = btrfs_write_dirty_block_groups(trans, root); |
56bec294 | 676 | BUG_ON(ret); |
0b86a832 | 677 | } |
276e680d YZ |
678 | |
679 | if (root != root->fs_info->extent_root) | |
680 | switch_commit_root(root); | |
681 | ||
0b86a832 CM |
682 | return 0; |
683 | } | |
684 | ||
d352ac68 CM |
685 | /* |
686 | * update all the cowonly tree roots on disk | |
687 | */ | |
5d4f98a2 YZ |
688 | static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, |
689 | struct btrfs_root *root) | |
0b86a832 CM |
690 | { |
691 | struct btrfs_fs_info *fs_info = root->fs_info; | |
692 | struct list_head *next; | |
84234f3a | 693 | struct extent_buffer *eb; |
56bec294 | 694 | int ret; |
84234f3a | 695 | |
56bec294 CM |
696 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
697 | BUG_ON(ret); | |
87ef2bb4 | 698 | |
84234f3a | 699 | eb = btrfs_lock_root_node(fs_info->tree_root); |
9fa8cfe7 | 700 | btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); |
84234f3a YZ |
701 | btrfs_tree_unlock(eb); |
702 | free_extent_buffer(eb); | |
0b86a832 | 703 | |
56bec294 CM |
704 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
705 | BUG_ON(ret); | |
87ef2bb4 | 706 | |
d397712b | 707 | while (!list_empty(&fs_info->dirty_cowonly_roots)) { |
0b86a832 CM |
708 | next = fs_info->dirty_cowonly_roots.next; |
709 | list_del_init(next); | |
710 | root = list_entry(next, struct btrfs_root, dirty_list); | |
87ef2bb4 | 711 | |
0b86a832 | 712 | update_cowonly_root(trans, root); |
79154b1b | 713 | } |
276e680d YZ |
714 | |
715 | down_write(&fs_info->extent_commit_sem); | |
716 | switch_commit_root(fs_info->extent_root); | |
717 | up_write(&fs_info->extent_commit_sem); | |
718 | ||
79154b1b CM |
719 | return 0; |
720 | } | |
721 | ||
d352ac68 CM |
722 | /* |
723 | * dead roots are old snapshots that need to be deleted. This allocates | |
724 | * a dirty root struct and adds it into the list of dead roots that need to | |
725 | * be deleted | |
726 | */ | |
5d4f98a2 | 727 | int btrfs_add_dead_root(struct btrfs_root *root) |
5eda7b5e | 728 | { |
b48652c1 | 729 | mutex_lock(&root->fs_info->trans_mutex); |
5d4f98a2 | 730 | list_add(&root->root_list, &root->fs_info->dead_roots); |
b48652c1 | 731 | mutex_unlock(&root->fs_info->trans_mutex); |
5eda7b5e CM |
732 | return 0; |
733 | } | |
734 | ||
d352ac68 | 735 | /* |
5d4f98a2 | 736 | * update all the cowonly tree roots on disk |
d352ac68 | 737 | */ |
5d4f98a2 YZ |
738 | static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, |
739 | struct btrfs_root *root) | |
0f7d52f4 | 740 | { |
0f7d52f4 | 741 | struct btrfs_root *gang[8]; |
5d4f98a2 | 742 | struct btrfs_fs_info *fs_info = root->fs_info; |
0f7d52f4 CM |
743 | int i; |
744 | int ret; | |
54aa1f4d CM |
745 | int err = 0; |
746 | ||
d397712b | 747 | while (1) { |
5d4f98a2 YZ |
748 | ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, |
749 | (void **)gang, 0, | |
0f7d52f4 CM |
750 | ARRAY_SIZE(gang), |
751 | BTRFS_ROOT_TRANS_TAG); | |
752 | if (ret == 0) | |
753 | break; | |
754 | for (i = 0; i < ret; i++) { | |
755 | root = gang[i]; | |
5d4f98a2 YZ |
756 | radix_tree_tag_clear(&fs_info->fs_roots_radix, |
757 | (unsigned long)root->root_key.objectid, | |
758 | BTRFS_ROOT_TRANS_TAG); | |
31153d81 | 759 | |
e02119d5 | 760 | btrfs_free_log(trans, root); |
5d4f98a2 | 761 | btrfs_update_reloc_root(trans, root); |
d68fc57b | 762 | btrfs_orphan_commit_root(trans, root); |
bcc63abb | 763 | |
978d910d | 764 | if (root->commit_root != root->node) { |
581bb050 | 765 | mutex_lock(&root->fs_commit_mutex); |
817d52f8 | 766 | switch_commit_root(root); |
581bb050 LZ |
767 | btrfs_unpin_free_ino(root); |
768 | mutex_unlock(&root->fs_commit_mutex); | |
769 | ||
978d910d YZ |
770 | btrfs_set_root_node(&root->root_item, |
771 | root->node); | |
772 | } | |
5d4f98a2 | 773 | |
5d4f98a2 | 774 | err = btrfs_update_root(trans, fs_info->tree_root, |
0f7d52f4 CM |
775 | &root->root_key, |
776 | &root->root_item); | |
54aa1f4d CM |
777 | if (err) |
778 | break; | |
0f7d52f4 CM |
779 | } |
780 | } | |
54aa1f4d | 781 | return err; |
0f7d52f4 CM |
782 | } |
783 | ||
d352ac68 CM |
784 | /* |
785 | * defrag a given btree. If cacheonly == 1, this won't read from the disk, | |
786 | * otherwise every leaf in the btree is read and defragged. | |
787 | */ | |
e9d0b13b CM |
788 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
789 | { | |
790 | struct btrfs_fs_info *info = root->fs_info; | |
e9d0b13b | 791 | struct btrfs_trans_handle *trans; |
8929ecfa | 792 | int ret; |
d3c2fdcf | 793 | unsigned long nr; |
e9d0b13b | 794 | |
8929ecfa | 795 | if (xchg(&root->defrag_running, 1)) |
e9d0b13b | 796 | return 0; |
8929ecfa | 797 | |
6b80053d | 798 | while (1) { |
8929ecfa YZ |
799 | trans = btrfs_start_transaction(root, 0); |
800 | if (IS_ERR(trans)) | |
801 | return PTR_ERR(trans); | |
802 | ||
e9d0b13b | 803 | ret = btrfs_defrag_leaves(trans, root, cacheonly); |
8929ecfa | 804 | |
d3c2fdcf | 805 | nr = trans->blocks_used; |
e9d0b13b | 806 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 807 | btrfs_btree_balance_dirty(info->tree_root, nr); |
e9d0b13b CM |
808 | cond_resched(); |
809 | ||
3f157a2f | 810 | if (root->fs_info->closing || ret != -EAGAIN) |
e9d0b13b CM |
811 | break; |
812 | } | |
813 | root->defrag_running = 0; | |
8929ecfa | 814 | return ret; |
e9d0b13b CM |
815 | } |
816 | ||
2c47e605 | 817 | #if 0 |
b7ec40d7 CM |
818 | /* |
819 | * when dropping snapshots, we generate a ton of delayed refs, and it makes | |
820 | * sense not to join the transaction while it is trying to flush the current | |
821 | * queue of delayed refs out. | |
822 | * | |
823 | * This is used by the drop snapshot code only | |
824 | */ | |
825 | static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info) | |
826 | { | |
827 | DEFINE_WAIT(wait); | |
828 | ||
829 | mutex_lock(&info->trans_mutex); | |
830 | while (info->running_transaction && | |
831 | info->running_transaction->delayed_refs.flushing) { | |
832 | prepare_to_wait(&info->transaction_wait, &wait, | |
833 | TASK_UNINTERRUPTIBLE); | |
834 | mutex_unlock(&info->trans_mutex); | |
59bc5c75 | 835 | |
b7ec40d7 | 836 | schedule(); |
59bc5c75 | 837 | |
b7ec40d7 CM |
838 | mutex_lock(&info->trans_mutex); |
839 | finish_wait(&info->transaction_wait, &wait); | |
840 | } | |
841 | mutex_unlock(&info->trans_mutex); | |
842 | return 0; | |
843 | } | |
844 | ||
d352ac68 CM |
845 | /* |
846 | * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on | |
847 | * all of them | |
848 | */ | |
5d4f98a2 | 849 | int btrfs_drop_dead_root(struct btrfs_root *root) |
0f7d52f4 | 850 | { |
0f7d52f4 | 851 | struct btrfs_trans_handle *trans; |
5d4f98a2 | 852 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
d3c2fdcf | 853 | unsigned long nr; |
5d4f98a2 | 854 | int ret; |
58176a96 | 855 | |
5d4f98a2 YZ |
856 | while (1) { |
857 | /* | |
858 | * we don't want to jump in and create a bunch of | |
859 | * delayed refs if the transaction is starting to close | |
860 | */ | |
861 | wait_transaction_pre_flush(tree_root->fs_info); | |
862 | trans = btrfs_start_transaction(tree_root, 1); | |
a2135011 | 863 | |
5d4f98a2 YZ |
864 | /* |
865 | * we've joined a transaction, make sure it isn't | |
866 | * closing right now | |
867 | */ | |
868 | if (trans->transaction->delayed_refs.flushing) { | |
869 | btrfs_end_transaction(trans, tree_root); | |
870 | continue; | |
9f3a7427 | 871 | } |
58176a96 | 872 | |
5d4f98a2 YZ |
873 | ret = btrfs_drop_snapshot(trans, root); |
874 | if (ret != -EAGAIN) | |
875 | break; | |
a2135011 | 876 | |
5d4f98a2 YZ |
877 | ret = btrfs_update_root(trans, tree_root, |
878 | &root->root_key, | |
879 | &root->root_item); | |
880 | if (ret) | |
54aa1f4d | 881 | break; |
bcc63abb | 882 | |
d3c2fdcf | 883 | nr = trans->blocks_used; |
0f7d52f4 CM |
884 | ret = btrfs_end_transaction(trans, tree_root); |
885 | BUG_ON(ret); | |
5eda7b5e | 886 | |
d3c2fdcf | 887 | btrfs_btree_balance_dirty(tree_root, nr); |
4dc11904 | 888 | cond_resched(); |
0f7d52f4 | 889 | } |
5d4f98a2 YZ |
890 | BUG_ON(ret); |
891 | ||
892 | ret = btrfs_del_root(trans, tree_root, &root->root_key); | |
893 | BUG_ON(ret); | |
894 | ||
895 | nr = trans->blocks_used; | |
896 | ret = btrfs_end_transaction(trans, tree_root); | |
897 | BUG_ON(ret); | |
898 | ||
899 | free_extent_buffer(root->node); | |
900 | free_extent_buffer(root->commit_root); | |
901 | kfree(root); | |
902 | ||
903 | btrfs_btree_balance_dirty(tree_root, nr); | |
54aa1f4d | 904 | return ret; |
0f7d52f4 | 905 | } |
2c47e605 | 906 | #endif |
0f7d52f4 | 907 | |
d352ac68 CM |
908 | /* |
909 | * new snapshots need to be created at a very specific time in the | |
910 | * transaction commit. This does the actual creation | |
911 | */ | |
80b6794d | 912 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
3063d29f CM |
913 | struct btrfs_fs_info *fs_info, |
914 | struct btrfs_pending_snapshot *pending) | |
915 | { | |
916 | struct btrfs_key key; | |
80b6794d | 917 | struct btrfs_root_item *new_root_item; |
3063d29f CM |
918 | struct btrfs_root *tree_root = fs_info->tree_root; |
919 | struct btrfs_root *root = pending->root; | |
6bdb72de SW |
920 | struct btrfs_root *parent_root; |
921 | struct inode *parent_inode; | |
6a912213 | 922 | struct dentry *parent; |
a22285a6 | 923 | struct dentry *dentry; |
3063d29f | 924 | struct extent_buffer *tmp; |
925baedd | 925 | struct extent_buffer *old; |
3063d29f | 926 | int ret; |
d68fc57b | 927 | u64 to_reserve = 0; |
6bdb72de | 928 | u64 index = 0; |
a22285a6 | 929 | u64 objectid; |
b83cc969 | 930 | u64 root_flags; |
3063d29f | 931 | |
80b6794d CM |
932 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
933 | if (!new_root_item) { | |
a22285a6 | 934 | pending->error = -ENOMEM; |
80b6794d CM |
935 | goto fail; |
936 | } | |
a22285a6 | 937 | |
581bb050 | 938 | ret = btrfs_find_free_objectid(tree_root, &objectid); |
a22285a6 YZ |
939 | if (ret) { |
940 | pending->error = ret; | |
3063d29f | 941 | goto fail; |
a22285a6 | 942 | } |
3063d29f | 943 | |
3fd0a558 | 944 | btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); |
d68fc57b YZ |
945 | btrfs_orphan_pre_snapshot(trans, pending, &to_reserve); |
946 | ||
947 | if (to_reserve > 0) { | |
948 | ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv, | |
8bb8ab2e | 949 | to_reserve); |
d68fc57b YZ |
950 | if (ret) { |
951 | pending->error = ret; | |
952 | goto fail; | |
953 | } | |
954 | } | |
955 | ||
3063d29f | 956 | key.objectid = objectid; |
a22285a6 YZ |
957 | key.offset = (u64)-1; |
958 | key.type = BTRFS_ROOT_ITEM_KEY; | |
3063d29f | 959 | |
a22285a6 | 960 | trans->block_rsv = &pending->block_rsv; |
3de4586c | 961 | |
a22285a6 | 962 | dentry = pending->dentry; |
6a912213 JB |
963 | parent = dget_parent(dentry); |
964 | parent_inode = parent->d_inode; | |
a22285a6 | 965 | parent_root = BTRFS_I(parent_inode)->root; |
6bdb72de | 966 | record_root_in_trans(trans, parent_root); |
a22285a6 | 967 | |
3063d29f CM |
968 | /* |
969 | * insert the directory item | |
970 | */ | |
3de4586c | 971 | ret = btrfs_set_inode_index(parent_inode, &index); |
6bdb72de | 972 | BUG_ON(ret); |
0660b5af | 973 | ret = btrfs_insert_dir_item(trans, parent_root, |
a22285a6 YZ |
974 | dentry->d_name.name, dentry->d_name.len, |
975 | parent_inode->i_ino, &key, | |
976 | BTRFS_FT_DIR, index); | |
6bdb72de | 977 | BUG_ON(ret); |
0660b5af | 978 | |
a22285a6 YZ |
979 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
980 | dentry->d_name.len * 2); | |
52c26179 YZ |
981 | ret = btrfs_update_inode(trans, parent_root, parent_inode); |
982 | BUG_ON(ret); | |
983 | ||
6bdb72de SW |
984 | record_root_in_trans(trans, root); |
985 | btrfs_set_root_last_snapshot(&root->root_item, trans->transid); | |
986 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); | |
08fe4db1 | 987 | btrfs_check_and_init_root_item(new_root_item); |
6bdb72de | 988 | |
b83cc969 LZ |
989 | root_flags = btrfs_root_flags(new_root_item); |
990 | if (pending->readonly) | |
991 | root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; | |
992 | else | |
993 | root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; | |
994 | btrfs_set_root_flags(new_root_item, root_flags); | |
995 | ||
6bdb72de SW |
996 | old = btrfs_lock_root_node(root); |
997 | btrfs_cow_block(trans, root, old, NULL, 0, &old); | |
998 | btrfs_set_lock_blocking(old); | |
999 | ||
1000 | btrfs_copy_root(trans, root, old, &tmp, objectid); | |
1001 | btrfs_tree_unlock(old); | |
1002 | free_extent_buffer(old); | |
1003 | ||
1004 | btrfs_set_root_node(new_root_item, tmp); | |
a22285a6 YZ |
1005 | /* record when the snapshot was created in key.offset */ |
1006 | key.offset = trans->transid; | |
1007 | ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); | |
6bdb72de SW |
1008 | btrfs_tree_unlock(tmp); |
1009 | free_extent_buffer(tmp); | |
a22285a6 | 1010 | BUG_ON(ret); |
6bdb72de | 1011 | |
a22285a6 YZ |
1012 | /* |
1013 | * insert root back/forward references | |
1014 | */ | |
1015 | ret = btrfs_add_root_ref(trans, tree_root, objectid, | |
0660b5af | 1016 | parent_root->root_key.objectid, |
a22285a6 YZ |
1017 | parent_inode->i_ino, index, |
1018 | dentry->d_name.name, dentry->d_name.len); | |
0660b5af | 1019 | BUG_ON(ret); |
6a912213 | 1020 | dput(parent); |
0660b5af | 1021 | |
a22285a6 YZ |
1022 | key.offset = (u64)-1; |
1023 | pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); | |
1024 | BUG_ON(IS_ERR(pending->snap)); | |
d68fc57b | 1025 | |
3fd0a558 | 1026 | btrfs_reloc_post_snapshot(trans, pending); |
d68fc57b | 1027 | btrfs_orphan_post_snapshot(trans, pending); |
3063d29f | 1028 | fail: |
6bdb72de | 1029 | kfree(new_root_item); |
a22285a6 YZ |
1030 | btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); |
1031 | return 0; | |
3063d29f CM |
1032 | } |
1033 | ||
d352ac68 CM |
1034 | /* |
1035 | * create all the snapshots we've scheduled for creation | |
1036 | */ | |
80b6794d CM |
1037 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
1038 | struct btrfs_fs_info *fs_info) | |
3de4586c CM |
1039 | { |
1040 | struct btrfs_pending_snapshot *pending; | |
1041 | struct list_head *head = &trans->transaction->pending_snapshots; | |
3de4586c CM |
1042 | int ret; |
1043 | ||
c6e30871 | 1044 | list_for_each_entry(pending, head, list) { |
3de4586c CM |
1045 | ret = create_pending_snapshot(trans, fs_info, pending); |
1046 | BUG_ON(ret); | |
1047 | } | |
1048 | return 0; | |
1049 | } | |
1050 | ||
5d4f98a2 YZ |
1051 | static void update_super_roots(struct btrfs_root *root) |
1052 | { | |
1053 | struct btrfs_root_item *root_item; | |
1054 | struct btrfs_super_block *super; | |
1055 | ||
1056 | super = &root->fs_info->super_copy; | |
1057 | ||
1058 | root_item = &root->fs_info->chunk_root->root_item; | |
1059 | super->chunk_root = root_item->bytenr; | |
1060 | super->chunk_root_generation = root_item->generation; | |
1061 | super->chunk_root_level = root_item->level; | |
1062 | ||
1063 | root_item = &root->fs_info->tree_root->root_item; | |
1064 | super->root = root_item->bytenr; | |
1065 | super->generation = root_item->generation; | |
1066 | super->root_level = root_item->level; | |
0af3d00b JB |
1067 | if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE)) |
1068 | super->cache_generation = root_item->generation; | |
5d4f98a2 YZ |
1069 | } |
1070 | ||
f36f3042 CM |
1071 | int btrfs_transaction_in_commit(struct btrfs_fs_info *info) |
1072 | { | |
1073 | int ret = 0; | |
1074 | spin_lock(&info->new_trans_lock); | |
1075 | if (info->running_transaction) | |
1076 | ret = info->running_transaction->in_commit; | |
1077 | spin_unlock(&info->new_trans_lock); | |
1078 | return ret; | |
1079 | } | |
1080 | ||
8929ecfa YZ |
1081 | int btrfs_transaction_blocked(struct btrfs_fs_info *info) |
1082 | { | |
1083 | int ret = 0; | |
1084 | spin_lock(&info->new_trans_lock); | |
1085 | if (info->running_transaction) | |
1086 | ret = info->running_transaction->blocked; | |
1087 | spin_unlock(&info->new_trans_lock); | |
1088 | return ret; | |
1089 | } | |
1090 | ||
bb9c12c9 SW |
1091 | /* |
1092 | * wait for the current transaction commit to start and block subsequent | |
1093 | * transaction joins | |
1094 | */ | |
1095 | static void wait_current_trans_commit_start(struct btrfs_root *root, | |
1096 | struct btrfs_transaction *trans) | |
1097 | { | |
1098 | DEFINE_WAIT(wait); | |
1099 | ||
1100 | if (trans->in_commit) | |
1101 | return; | |
1102 | ||
1103 | while (1) { | |
1104 | prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait, | |
1105 | TASK_UNINTERRUPTIBLE); | |
1106 | if (trans->in_commit) { | |
1107 | finish_wait(&root->fs_info->transaction_blocked_wait, | |
1108 | &wait); | |
1109 | break; | |
1110 | } | |
1111 | mutex_unlock(&root->fs_info->trans_mutex); | |
1112 | schedule(); | |
1113 | mutex_lock(&root->fs_info->trans_mutex); | |
1114 | finish_wait(&root->fs_info->transaction_blocked_wait, &wait); | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | /* | |
1119 | * wait for the current transaction to start and then become unblocked. | |
1120 | * caller holds ref. | |
1121 | */ | |
1122 | static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, | |
1123 | struct btrfs_transaction *trans) | |
1124 | { | |
1125 | DEFINE_WAIT(wait); | |
1126 | ||
1127 | if (trans->commit_done || (trans->in_commit && !trans->blocked)) | |
1128 | return; | |
1129 | ||
1130 | while (1) { | |
1131 | prepare_to_wait(&root->fs_info->transaction_wait, &wait, | |
1132 | TASK_UNINTERRUPTIBLE); | |
1133 | if (trans->commit_done || | |
1134 | (trans->in_commit && !trans->blocked)) { | |
1135 | finish_wait(&root->fs_info->transaction_wait, | |
1136 | &wait); | |
1137 | break; | |
1138 | } | |
1139 | mutex_unlock(&root->fs_info->trans_mutex); | |
1140 | schedule(); | |
1141 | mutex_lock(&root->fs_info->trans_mutex); | |
1142 | finish_wait(&root->fs_info->transaction_wait, | |
1143 | &wait); | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | /* | |
1148 | * commit transactions asynchronously. once btrfs_commit_transaction_async | |
1149 | * returns, any subsequent transaction will not be allowed to join. | |
1150 | */ | |
1151 | struct btrfs_async_commit { | |
1152 | struct btrfs_trans_handle *newtrans; | |
1153 | struct btrfs_root *root; | |
1154 | struct delayed_work work; | |
1155 | }; | |
1156 | ||
1157 | static void do_async_commit(struct work_struct *work) | |
1158 | { | |
1159 | struct btrfs_async_commit *ac = | |
1160 | container_of(work, struct btrfs_async_commit, work.work); | |
1161 | ||
1162 | btrfs_commit_transaction(ac->newtrans, ac->root); | |
1163 | kfree(ac); | |
1164 | } | |
1165 | ||
1166 | int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, | |
1167 | struct btrfs_root *root, | |
1168 | int wait_for_unblock) | |
1169 | { | |
1170 | struct btrfs_async_commit *ac; | |
1171 | struct btrfs_transaction *cur_trans; | |
1172 | ||
1173 | ac = kmalloc(sizeof(*ac), GFP_NOFS); | |
db5b493a TI |
1174 | if (!ac) |
1175 | return -ENOMEM; | |
bb9c12c9 SW |
1176 | |
1177 | INIT_DELAYED_WORK(&ac->work, do_async_commit); | |
1178 | ac->root = root; | |
1179 | ac->newtrans = btrfs_join_transaction(root, 0); | |
3612b495 TI |
1180 | if (IS_ERR(ac->newtrans)) { |
1181 | int err = PTR_ERR(ac->newtrans); | |
1182 | kfree(ac); | |
1183 | return err; | |
1184 | } | |
bb9c12c9 SW |
1185 | |
1186 | /* take transaction reference */ | |
1187 | mutex_lock(&root->fs_info->trans_mutex); | |
1188 | cur_trans = trans->transaction; | |
13c5a93e | 1189 | atomic_inc(&cur_trans->use_count); |
bb9c12c9 SW |
1190 | mutex_unlock(&root->fs_info->trans_mutex); |
1191 | ||
1192 | btrfs_end_transaction(trans, root); | |
1193 | schedule_delayed_work(&ac->work, 0); | |
1194 | ||
1195 | /* wait for transaction to start and unblock */ | |
1196 | mutex_lock(&root->fs_info->trans_mutex); | |
1197 | if (wait_for_unblock) | |
1198 | wait_current_trans_commit_start_and_unblock(root, cur_trans); | |
1199 | else | |
1200 | wait_current_trans_commit_start(root, cur_trans); | |
1201 | put_transaction(cur_trans); | |
1202 | mutex_unlock(&root->fs_info->trans_mutex); | |
1203 | ||
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | /* | |
1208 | * btrfs_transaction state sequence: | |
1209 | * in_commit = 0, blocked = 0 (initial) | |
1210 | * in_commit = 1, blocked = 1 | |
1211 | * blocked = 0 | |
1212 | * commit_done = 1 | |
1213 | */ | |
79154b1b CM |
1214 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
1215 | struct btrfs_root *root) | |
1216 | { | |
15ee9bc7 | 1217 | unsigned long joined = 0; |
79154b1b | 1218 | struct btrfs_transaction *cur_trans; |
8fd17795 | 1219 | struct btrfs_transaction *prev_trans = NULL; |
79154b1b | 1220 | DEFINE_WAIT(wait); |
15ee9bc7 | 1221 | int ret; |
89573b9c CM |
1222 | int should_grow = 0; |
1223 | unsigned long now = get_seconds(); | |
dccae999 | 1224 | int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT); |
79154b1b | 1225 | |
5a3f23d5 CM |
1226 | btrfs_run_ordered_operations(root, 0); |
1227 | ||
56bec294 CM |
1228 | /* make a pass through all the delayed refs we have so far |
1229 | * any runnings procs may add more while we are here | |
1230 | */ | |
1231 | ret = btrfs_run_delayed_refs(trans, root, 0); | |
1232 | BUG_ON(ret); | |
1233 | ||
a22285a6 YZ |
1234 | btrfs_trans_release_metadata(trans, root); |
1235 | ||
b7ec40d7 | 1236 | cur_trans = trans->transaction; |
56bec294 CM |
1237 | /* |
1238 | * set the flushing flag so procs in this transaction have to | |
1239 | * start sending their work down. | |
1240 | */ | |
b7ec40d7 | 1241 | cur_trans->delayed_refs.flushing = 1; |
56bec294 | 1242 | |
c3e69d58 | 1243 | ret = btrfs_run_delayed_refs(trans, root, 0); |
56bec294 CM |
1244 | BUG_ON(ret); |
1245 | ||
79154b1b | 1246 | mutex_lock(&root->fs_info->trans_mutex); |
b7ec40d7 | 1247 | if (cur_trans->in_commit) { |
13c5a93e | 1248 | atomic_inc(&cur_trans->use_count); |
ccd467d6 | 1249 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b | 1250 | btrfs_end_transaction(trans, root); |
ccd467d6 | 1251 | |
79154b1b CM |
1252 | ret = wait_for_commit(root, cur_trans); |
1253 | BUG_ON(ret); | |
15ee9bc7 JB |
1254 | |
1255 | mutex_lock(&root->fs_info->trans_mutex); | |
79154b1b | 1256 | put_transaction(cur_trans); |
15ee9bc7 JB |
1257 | mutex_unlock(&root->fs_info->trans_mutex); |
1258 | ||
79154b1b CM |
1259 | return 0; |
1260 | } | |
4313b399 | 1261 | |
2c90e5d6 | 1262 | trans->transaction->in_commit = 1; |
f9295749 | 1263 | trans->transaction->blocked = 1; |
bb9c12c9 SW |
1264 | wake_up(&root->fs_info->transaction_blocked_wait); |
1265 | ||
ccd467d6 CM |
1266 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
1267 | prev_trans = list_entry(cur_trans->list.prev, | |
1268 | struct btrfs_transaction, list); | |
1269 | if (!prev_trans->commit_done) { | |
13c5a93e | 1270 | atomic_inc(&prev_trans->use_count); |
ccd467d6 CM |
1271 | mutex_unlock(&root->fs_info->trans_mutex); |
1272 | ||
1273 | wait_for_commit(root, prev_trans); | |
ccd467d6 | 1274 | |
ccd467d6 | 1275 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 | 1276 | put_transaction(prev_trans); |
ccd467d6 CM |
1277 | } |
1278 | } | |
15ee9bc7 | 1279 | |
89573b9c CM |
1280 | if (now < cur_trans->start_time || now - cur_trans->start_time < 1) |
1281 | should_grow = 1; | |
1282 | ||
15ee9bc7 | 1283 | do { |
7ea394f1 | 1284 | int snap_pending = 0; |
15ee9bc7 | 1285 | joined = cur_trans->num_joined; |
7ea394f1 YZ |
1286 | if (!list_empty(&trans->transaction->pending_snapshots)) |
1287 | snap_pending = 1; | |
1288 | ||
2c90e5d6 | 1289 | WARN_ON(cur_trans != trans->transaction); |
79154b1b | 1290 | mutex_unlock(&root->fs_info->trans_mutex); |
15ee9bc7 | 1291 | |
0bdb1db2 | 1292 | if (flush_on_commit || snap_pending) { |
24bbcf04 YZ |
1293 | btrfs_start_delalloc_inodes(root, 1); |
1294 | ret = btrfs_wait_ordered_extents(root, 0, 1); | |
ebecd3d9 | 1295 | BUG_ON(ret); |
7ea394f1 YZ |
1296 | } |
1297 | ||
5a3f23d5 CM |
1298 | /* |
1299 | * rename don't use btrfs_join_transaction, so, once we | |
1300 | * set the transaction to blocked above, we aren't going | |
1301 | * to get any new ordered operations. We can safely run | |
1302 | * it here and no for sure that nothing new will be added | |
1303 | * to the list | |
1304 | */ | |
1305 | btrfs_run_ordered_operations(root, 1); | |
1306 | ||
ed3b3d31 CM |
1307 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
1308 | TASK_UNINTERRUPTIBLE); | |
1309 | ||
89573b9c | 1310 | smp_mb(); |
13c5a93e | 1311 | if (atomic_read(&cur_trans->num_writers) > 1) |
99d16cbc SW |
1312 | schedule_timeout(MAX_SCHEDULE_TIMEOUT); |
1313 | else if (should_grow) | |
1314 | schedule_timeout(1); | |
15ee9bc7 | 1315 | |
79154b1b | 1316 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 | 1317 | finish_wait(&cur_trans->writer_wait, &wait); |
13c5a93e | 1318 | } while (atomic_read(&cur_trans->num_writers) > 1 || |
89573b9c | 1319 | (should_grow && cur_trans->num_joined != joined)); |
15ee9bc7 | 1320 | |
3063d29f CM |
1321 | ret = create_pending_snapshots(trans, root->fs_info); |
1322 | BUG_ON(ret); | |
1323 | ||
56bec294 CM |
1324 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
1325 | BUG_ON(ret); | |
1326 | ||
2c90e5d6 | 1327 | WARN_ON(cur_trans != trans->transaction); |
dc17ff8f | 1328 | |
e02119d5 CM |
1329 | /* btrfs_commit_tree_roots is responsible for getting the |
1330 | * various roots consistent with each other. Every pointer | |
1331 | * in the tree of tree roots has to point to the most up to date | |
1332 | * root for every subvolume and other tree. So, we have to keep | |
1333 | * the tree logging code from jumping in and changing any | |
1334 | * of the trees. | |
1335 | * | |
1336 | * At this point in the commit, there can't be any tree-log | |
1337 | * writers, but a little lower down we drop the trans mutex | |
1338 | * and let new people in. By holding the tree_log_mutex | |
1339 | * from now until after the super is written, we avoid races | |
1340 | * with the tree-log code. | |
1341 | */ | |
1342 | mutex_lock(&root->fs_info->tree_log_mutex); | |
1343 | ||
5d4f98a2 | 1344 | ret = commit_fs_roots(trans, root); |
54aa1f4d CM |
1345 | BUG_ON(ret); |
1346 | ||
5d4f98a2 | 1347 | /* commit_fs_roots gets rid of all the tree log roots, it is now |
e02119d5 CM |
1348 | * safe to free the root of tree log roots |
1349 | */ | |
1350 | btrfs_free_log_root_tree(trans, root->fs_info); | |
1351 | ||
5d4f98a2 | 1352 | ret = commit_cowonly_roots(trans, root); |
79154b1b | 1353 | BUG_ON(ret); |
54aa1f4d | 1354 | |
11833d66 YZ |
1355 | btrfs_prepare_extent_commit(trans, root); |
1356 | ||
78fae27e | 1357 | cur_trans = root->fs_info->running_transaction; |
cee36a03 | 1358 | spin_lock(&root->fs_info->new_trans_lock); |
78fae27e | 1359 | root->fs_info->running_transaction = NULL; |
cee36a03 | 1360 | spin_unlock(&root->fs_info->new_trans_lock); |
5d4f98a2 YZ |
1361 | |
1362 | btrfs_set_root_node(&root->fs_info->tree_root->root_item, | |
1363 | root->fs_info->tree_root->node); | |
817d52f8 | 1364 | switch_commit_root(root->fs_info->tree_root); |
5d4f98a2 YZ |
1365 | |
1366 | btrfs_set_root_node(&root->fs_info->chunk_root->root_item, | |
1367 | root->fs_info->chunk_root->node); | |
817d52f8 | 1368 | switch_commit_root(root->fs_info->chunk_root); |
5d4f98a2 YZ |
1369 | |
1370 | update_super_roots(root); | |
e02119d5 CM |
1371 | |
1372 | if (!root->fs_info->log_root_recovering) { | |
1373 | btrfs_set_super_log_root(&root->fs_info->super_copy, 0); | |
1374 | btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); | |
1375 | } | |
1376 | ||
a061fc8d CM |
1377 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
1378 | sizeof(root->fs_info->super_copy)); | |
ccd467d6 | 1379 | |
f9295749 | 1380 | trans->transaction->blocked = 0; |
b7ec40d7 | 1381 | |
f9295749 | 1382 | wake_up(&root->fs_info->transaction_wait); |
e6dcd2dc | 1383 | |
78fae27e | 1384 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b CM |
1385 | ret = btrfs_write_and_wait_transaction(trans, root); |
1386 | BUG_ON(ret); | |
a512bbf8 | 1387 | write_ctree_super(trans, root, 0); |
4313b399 | 1388 | |
e02119d5 CM |
1389 | /* |
1390 | * the super is written, we can safely allow the tree-loggers | |
1391 | * to go about their business | |
1392 | */ | |
1393 | mutex_unlock(&root->fs_info->tree_log_mutex); | |
1394 | ||
11833d66 | 1395 | btrfs_finish_extent_commit(trans, root); |
4313b399 | 1396 | |
1a40e23b ZY |
1397 | mutex_lock(&root->fs_info->trans_mutex); |
1398 | ||
2c90e5d6 | 1399 | cur_trans->commit_done = 1; |
b7ec40d7 | 1400 | |
15ee9bc7 | 1401 | root->fs_info->last_trans_committed = cur_trans->transid; |
817d52f8 | 1402 | |
2c90e5d6 | 1403 | wake_up(&cur_trans->commit_wait); |
3de4586c | 1404 | |
13c5a93e | 1405 | list_del_init(&cur_trans->list); |
78fae27e | 1406 | put_transaction(cur_trans); |
79154b1b | 1407 | put_transaction(cur_trans); |
58176a96 | 1408 | |
1abe9b8a | 1409 | trace_btrfs_transaction_commit(root); |
1410 | ||
78fae27e | 1411 | mutex_unlock(&root->fs_info->trans_mutex); |
3de4586c | 1412 | |
9ed74f2d JB |
1413 | if (current->journal_info == trans) |
1414 | current->journal_info = NULL; | |
1415 | ||
2c90e5d6 | 1416 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
24bbcf04 YZ |
1417 | |
1418 | if (current != root->fs_info->transaction_kthread) | |
1419 | btrfs_run_delayed_iputs(root); | |
1420 | ||
79154b1b CM |
1421 | return ret; |
1422 | } | |
1423 | ||
d352ac68 CM |
1424 | /* |
1425 | * interface function to delete all the snapshots we have scheduled for deletion | |
1426 | */ | |
e9d0b13b CM |
1427 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
1428 | { | |
5d4f98a2 YZ |
1429 | LIST_HEAD(list); |
1430 | struct btrfs_fs_info *fs_info = root->fs_info; | |
1431 | ||
1432 | mutex_lock(&fs_info->trans_mutex); | |
1433 | list_splice_init(&fs_info->dead_roots, &list); | |
1434 | mutex_unlock(&fs_info->trans_mutex); | |
e9d0b13b | 1435 | |
5d4f98a2 YZ |
1436 | while (!list_empty(&list)) { |
1437 | root = list_entry(list.next, struct btrfs_root, root_list); | |
76dda93c YZ |
1438 | list_del(&root->root_list); |
1439 | ||
1440 | if (btrfs_header_backref_rev(root->node) < | |
1441 | BTRFS_MIXED_BACKREF_REV) | |
3fd0a558 | 1442 | btrfs_drop_snapshot(root, NULL, 0); |
76dda93c | 1443 | else |
3fd0a558 | 1444 | btrfs_drop_snapshot(root, NULL, 1); |
e9d0b13b CM |
1445 | } |
1446 | return 0; | |
1447 | } |