Blame view
fs/btrfs/transaction.c
65.6 KB
6cbd55707
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* * Copyright (C) 2007 Oracle. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License v2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. */ |
79154b1b5
|
18 |
#include <linux/fs.h> |
5a0e3ad6a
|
19 |
#include <linux/slab.h> |
340887809
|
20 |
#include <linux/sched.h> |
d3c2fdcf7
|
21 |
#include <linux/writeback.h> |
5f39d397d
|
22 |
#include <linux/pagemap.h> |
5f2cc086c
|
23 |
#include <linux/blkdev.h> |
8ea05e3a4
|
24 |
#include <linux/uuid.h> |
79154b1b5
|
25 26 27 |
#include "ctree.h" #include "disk-io.h" #include "transaction.h" |
925baeddc
|
28 |
#include "locking.h" |
e02119d5a
|
29 |
#include "tree-log.h" |
581bb0509
|
30 |
#include "inode-map.h" |
733f4fbbc
|
31 |
#include "volumes.h" |
8dabb7420
|
32 |
#include "dev-replace.h" |
fcebe4562
|
33 |
#include "qgroup.h" |
79154b1b5
|
34 |
|
0f7d52f44
|
35 |
#define BTRFS_ROOT_TRANS_TAG 0 |
e8c9f1860
|
36 |
static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { |
4a9d8bdee
|
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
[TRANS_STATE_RUNNING] = 0U, [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | __TRANS_START), [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | __TRANS_START | __TRANS_ATTACH), [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | __TRANS_START | __TRANS_ATTACH | __TRANS_JOIN), [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | __TRANS_START | __TRANS_ATTACH | __TRANS_JOIN | __TRANS_JOIN_NOLOCK), [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | __TRANS_START | __TRANS_ATTACH | __TRANS_JOIN | __TRANS_JOIN_NOLOCK), }; |
724e2315d
|
58 |
void btrfs_put_transaction(struct btrfs_transaction *transaction) |
79154b1b5
|
59 |
{ |
13c5a93e7
|
60 61 |
WARN_ON(atomic_read(&transaction->use_count) == 0); if (atomic_dec_and_test(&transaction->use_count)) { |
a4abeea41
|
62 |
BUG_ON(!list_empty(&transaction->list)); |
c46effa60
|
63 |
WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); |
1262133b8
|
64 |
if (transaction->delayed_refs.pending_csums) |
ab8d0fc48
|
65 66 67 |
btrfs_err(transaction->fs_info, "pending csums is %llu", transaction->delayed_refs.pending_csums); |
6df9a95e6
|
68 69 70 71 72 73 74 75 |
while (!list_empty(&transaction->pending_chunks)) { struct extent_map *em; em = list_first_entry(&transaction->pending_chunks, struct extent_map, list); list_del_init(&em->list); free_extent_map(em); } |
7785a663c
|
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/* * If any block groups are found in ->deleted_bgs then it's * because the transaction was aborted and a commit did not * happen (things failed before writing the new superblock * and calling btrfs_finish_extent_commit()), so we can not * discard the physical locations of the block groups. */ while (!list_empty(&transaction->deleted_bgs)) { struct btrfs_block_group_cache *cache; cache = list_first_entry(&transaction->deleted_bgs, struct btrfs_block_group_cache, bg_list); list_del_init(&cache->bg_list); btrfs_put_block_group_trimming(cache); btrfs_put_block_group(cache); } |
2c90e5d65
|
93 |
kmem_cache_free(btrfs_transaction_cachep, transaction); |
78fae27eb
|
94 |
} |
79154b1b5
|
95 |
} |
663dfbb07
|
96 97 98 |
static void clear_btree_io_tree(struct extent_io_tree *tree) { spin_lock(&tree->lock); |
b666a9cd9
|
99 100 101 102 103 104 |
/* * Do a single barrier for the waitqueue_active check here, the state * of the waitqueue should not change once clear_btree_io_tree is * called. */ smp_mb(); |
663dfbb07
|
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
while (!RB_EMPTY_ROOT(&tree->state)) { struct rb_node *node; struct extent_state *state; node = rb_first(&tree->state); state = rb_entry(node, struct extent_state, rb_node); rb_erase(&state->rb_node, &tree->state); RB_CLEAR_NODE(&state->rb_node); /* * btree io trees aren't supposed to have tasks waiting for * changes in the flags of extent states ever. */ ASSERT(!waitqueue_active(&state->wq)); free_extent_state(state); |
351810c1d
|
119 120 |
cond_resched_lock(&tree->lock); |
663dfbb07
|
121 122 123 |
} spin_unlock(&tree->lock); } |
9e351cc86
|
124 125 |
static noinline void switch_commit_roots(struct btrfs_transaction *trans, struct btrfs_fs_info *fs_info) |
817d52f8d
|
126 |
{ |
9e351cc86
|
127 128 129 130 131 132 133 134 135 136 |
struct btrfs_root *root, *tmp; down_write(&fs_info->commit_root_sem); list_for_each_entry_safe(root, tmp, &trans->switch_commits, dirty_list) { list_del_init(&root->dirty_list); free_extent_buffer(root->commit_root); root->commit_root = btrfs_root_node(root); if (is_fstree(root->objectid)) btrfs_unpin_free_ino(root); |
663dfbb07
|
137 |
clear_btree_io_tree(&root->dirty_log_pages); |
9e351cc86
|
138 |
} |
2b9dbef27
|
139 140 141 142 143 144 145 146 147 148 149 150 |
/* We can free old roots now. */ spin_lock(&trans->dropped_roots_lock); while (!list_empty(&trans->dropped_roots)) { root = list_first_entry(&trans->dropped_roots, struct btrfs_root, root_list); list_del_init(&root->root_list); spin_unlock(&trans->dropped_roots_lock); btrfs_drop_and_free_fs_root(fs_info, root); spin_lock(&trans->dropped_roots_lock); } spin_unlock(&trans->dropped_roots_lock); |
9e351cc86
|
151 |
up_write(&fs_info->commit_root_sem); |
817d52f8d
|
152 |
} |
0860adfdb
|
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
static inline void extwriter_counter_inc(struct btrfs_transaction *trans, unsigned int type) { if (type & TRANS_EXTWRITERS) atomic_inc(&trans->num_extwriters); } static inline void extwriter_counter_dec(struct btrfs_transaction *trans, unsigned int type) { if (type & TRANS_EXTWRITERS) atomic_dec(&trans->num_extwriters); } static inline void extwriter_counter_init(struct btrfs_transaction *trans, unsigned int type) { atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); } static inline int extwriter_counter_read(struct btrfs_transaction *trans) { return atomic_read(&trans->num_extwriters); |
178260b2c
|
176 |
} |
d352ac681
|
177 178 179 |
/* * either allocate a new transaction or hop into the existing one */ |
0860adfdb
|
180 |
static noinline int join_transaction(struct btrfs_root *root, unsigned int type) |
79154b1b5
|
181 182 |
{ struct btrfs_transaction *cur_trans; |
19ae4e813
|
183 |
struct btrfs_fs_info *fs_info = root->fs_info; |
a4abeea41
|
184 |
|
19ae4e813
|
185 |
spin_lock(&fs_info->trans_lock); |
d43317dcd
|
186 |
loop: |
49b25e054
|
187 |
/* The file system has been taken offline. No new transactions. */ |
87533c475
|
188 |
if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { |
19ae4e813
|
189 |
spin_unlock(&fs_info->trans_lock); |
49b25e054
|
190 191 |
return -EROFS; } |
19ae4e813
|
192 |
cur_trans = fs_info->running_transaction; |
a4abeea41
|
193 |
if (cur_trans) { |
871383be5
|
194 |
if (cur_trans->aborted) { |
19ae4e813
|
195 |
spin_unlock(&fs_info->trans_lock); |
49b25e054
|
196 |
return cur_trans->aborted; |
871383be5
|
197 |
} |
4a9d8bdee
|
198 |
if (btrfs_blocked_trans_types[cur_trans->state] & type) { |
178260b2c
|
199 200 201 |
spin_unlock(&fs_info->trans_lock); return -EBUSY; } |
a4abeea41
|
202 |
atomic_inc(&cur_trans->use_count); |
13c5a93e7
|
203 |
atomic_inc(&cur_trans->num_writers); |
0860adfdb
|
204 |
extwriter_counter_inc(cur_trans, type); |
19ae4e813
|
205 |
spin_unlock(&fs_info->trans_lock); |
a4abeea41
|
206 |
return 0; |
79154b1b5
|
207 |
} |
19ae4e813
|
208 |
spin_unlock(&fs_info->trans_lock); |
a4abeea41
|
209 |
|
354aa0fb6
|
210 211 212 213 214 215 |
/* * If we are ATTACH, we just want to catch the current transaction, * and commit it. If there is no transaction, just return ENOENT. */ if (type == TRANS_ATTACH) return -ENOENT; |
4a9d8bdee
|
216 217 218 219 220 |
/* * JOIN_NOLOCK only happens during the transaction commit, so * it is impossible that ->running_transaction is NULL */ BUG_ON(type == TRANS_JOIN_NOLOCK); |
a4abeea41
|
221 222 223 |
cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); if (!cur_trans) return -ENOMEM; |
d43317dcd
|
224 |
|
19ae4e813
|
225 226 |
spin_lock(&fs_info->trans_lock); if (fs_info->running_transaction) { |
d43317dcd
|
227 228 |
/* * someone started a transaction after we unlocked. Make sure |
4a9d8bdee
|
229 |
* to redo the checks above |
d43317dcd
|
230 |
*/ |
a4abeea41
|
231 |
kmem_cache_free(btrfs_transaction_cachep, cur_trans); |
d43317dcd
|
232 |
goto loop; |
87533c475
|
233 |
} else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { |
e4b50e14c
|
234 |
spin_unlock(&fs_info->trans_lock); |
7b8b92af5
|
235 236 |
kmem_cache_free(btrfs_transaction_cachep, cur_trans); return -EROFS; |
79154b1b5
|
237 |
} |
d43317dcd
|
238 |
|
ab8d0fc48
|
239 |
cur_trans->fs_info = fs_info; |
a4abeea41
|
240 |
atomic_set(&cur_trans->num_writers, 1); |
0860adfdb
|
241 |
extwriter_counter_init(cur_trans, type); |
a4abeea41
|
242 243 |
init_waitqueue_head(&cur_trans->writer_wait); init_waitqueue_head(&cur_trans->commit_wait); |
161c3549b
|
244 |
init_waitqueue_head(&cur_trans->pending_wait); |
4a9d8bdee
|
245 |
cur_trans->state = TRANS_STATE_RUNNING; |
a4abeea41
|
246 247 248 249 250 |
/* * One for this trans handle, one so it will live on until we * commit the transaction. */ atomic_set(&cur_trans->use_count, 2); |
161c3549b
|
251 |
atomic_set(&cur_trans->pending_ordered, 0); |
3204d33cd
|
252 |
cur_trans->flags = 0; |
a4abeea41
|
253 |
cur_trans->start_time = get_seconds(); |
a099d0fdb
|
254 |
memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); |
c46effa60
|
255 |
cur_trans->delayed_refs.href_root = RB_ROOT; |
3368d001b
|
256 |
cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; |
d7df2c796
|
257 |
atomic_set(&cur_trans->delayed_refs.num_entries, 0); |
20b297d62
|
258 259 260 261 262 263 |
/* * although the tree mod log is per file system and not per transaction, * the log must never go across transaction boundaries. */ smp_mb(); |
31b1a2bd7
|
264 |
if (!list_empty(&fs_info->tree_mod_seq_list)) |
5d163e0e6
|
265 266 |
WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction "); |
31b1a2bd7
|
267 |
if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) |
5d163e0e6
|
268 269 |
WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction "); |
fc36ed7e0
|
270 |
atomic64_set(&fs_info->tree_mod_seq, 0); |
20b297d62
|
271 |
|
a4abeea41
|
272 273 274 |
spin_lock_init(&cur_trans->delayed_refs.lock); INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
6df9a95e6
|
275 |
INIT_LIST_HEAD(&cur_trans->pending_chunks); |
9e351cc86
|
276 |
INIT_LIST_HEAD(&cur_trans->switch_commits); |
ce93ec548
|
277 |
INIT_LIST_HEAD(&cur_trans->dirty_bgs); |
1bbc621ef
|
278 |
INIT_LIST_HEAD(&cur_trans->io_bgs); |
2b9dbef27
|
279 |
INIT_LIST_HEAD(&cur_trans->dropped_roots); |
1bbc621ef
|
280 |
mutex_init(&cur_trans->cache_write_mutex); |
cb723e491
|
281 |
cur_trans->num_dirty_bgs = 0; |
ce93ec548
|
282 |
spin_lock_init(&cur_trans->dirty_bgs_lock); |
e33e17ee1
|
283 |
INIT_LIST_HEAD(&cur_trans->deleted_bgs); |
2b9dbef27
|
284 |
spin_lock_init(&cur_trans->dropped_roots_lock); |
19ae4e813
|
285 |
list_add_tail(&cur_trans->list, &fs_info->trans_list); |
a4abeea41
|
286 |
extent_io_tree_init(&cur_trans->dirty_pages, |
19ae4e813
|
287 288 289 290 |
fs_info->btree_inode->i_mapping); fs_info->generation++; cur_trans->transid = fs_info->generation; fs_info->running_transaction = cur_trans; |
49b25e054
|
291 |
cur_trans->aborted = 0; |
19ae4e813
|
292 |
spin_unlock(&fs_info->trans_lock); |
15ee9bc7e
|
293 |
|
79154b1b5
|
294 295 |
return 0; } |
d352ac681
|
296 |
/* |
d397712bc
|
297 298 299 300 |
* this does all the record keeping required to make sure that a reference * counted root is properly recorded in a given transaction. This is required * to make sure the old root from before we joined the transaction is deleted * when the transaction commits |
d352ac681
|
301 |
*/ |
7585717f3
|
302 |
static int record_root_in_trans(struct btrfs_trans_handle *trans, |
6426c7ad6
|
303 304 |
struct btrfs_root *root, int force) |
6702ed490
|
305 |
{ |
6426c7ad6
|
306 307 |
if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && root->last_trans < trans->transid) || force) { |
6702ed490
|
308 |
WARN_ON(root == root->fs_info->extent_root); |
5d4f98a28
|
309 |
WARN_ON(root->commit_root != root->node); |
7585717f3
|
310 |
/* |
27cdeb709
|
311 |
* see below for IN_TRANS_SETUP usage rules |
7585717f3
|
312 313 314 |
* we have the reloc mutex held now, so there * is only one writer in this function */ |
27cdeb709
|
315 |
set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); |
7585717f3
|
316 |
|
27cdeb709
|
317 |
/* make sure readers find IN_TRANS_SETUP before |
7585717f3
|
318 319 320 |
* they find our root->last_trans update */ smp_wmb(); |
a4abeea41
|
321 |
spin_lock(&root->fs_info->fs_roots_radix_lock); |
6426c7ad6
|
322 |
if (root->last_trans == trans->transid && !force) { |
a4abeea41
|
323 324 325 |
spin_unlock(&root->fs_info->fs_roots_radix_lock); return 0; } |
5d4f98a28
|
326 327 328 |
radix_tree_tag_set(&root->fs_info->fs_roots_radix, (unsigned long)root->root_key.objectid, BTRFS_ROOT_TRANS_TAG); |
a4abeea41
|
329 |
spin_unlock(&root->fs_info->fs_roots_radix_lock); |
7585717f3
|
330 331 332 333 334 335 336 337 338 339 340 341 342 |
root->last_trans = trans->transid; /* this is pretty tricky. We don't want to * take the relocation lock in btrfs_record_root_in_trans * unless we're really doing the first setup for this root in * this transaction. * * Normally we'd use root->last_trans as a flag to decide * if we want to take the expensive mutex. * * But, we have to set root->last_trans before we * init the relocation root, otherwise, we trip over warnings * in ctree.c. The solution used here is to flag ourselves |
27cdeb709
|
343 |
* with root IN_TRANS_SETUP. When this is 1, we're still |
7585717f3
|
344 345 346 347 348 349 350 |
* fixing up the reloc trees and everyone must wait. * * When this is zero, they can trust root->last_trans and fly * through btrfs_record_root_in_trans without having to take the * lock. smp_wmb() makes sure that all the writes above are * done before we pop in the zero below */ |
5d4f98a28
|
351 |
btrfs_init_reloc_root(trans, root); |
c7548af69
|
352 |
smp_mb__before_atomic(); |
27cdeb709
|
353 |
clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); |
5d4f98a28
|
354 355 356 |
} return 0; } |
bcc63abbf
|
357 |
|
7585717f3
|
358 |
|
2b9dbef27
|
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, struct btrfs_root *root) { struct btrfs_transaction *cur_trans = trans->transaction; /* Add ourselves to the transaction dropped list */ spin_lock(&cur_trans->dropped_roots_lock); list_add_tail(&root->root_list, &cur_trans->dropped_roots); spin_unlock(&cur_trans->dropped_roots_lock); /* Make sure we don't try to update the root at commit time */ spin_lock(&root->fs_info->fs_roots_radix_lock); radix_tree_tag_clear(&root->fs_info->fs_roots_radix, (unsigned long)root->root_key.objectid, BTRFS_ROOT_TRANS_TAG); spin_unlock(&root->fs_info->fs_roots_radix_lock); } |
7585717f3
|
376 377 378 |
int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root) { |
27cdeb709
|
379 |
if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
7585717f3
|
380 381 382 |
return 0; /* |
27cdeb709
|
383 |
* see record_root_in_trans for comments about IN_TRANS_SETUP usage |
7585717f3
|
384 385 386 387 |
* and barriers */ smp_rmb(); if (root->last_trans == trans->transid && |
27cdeb709
|
388 |
!test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) |
7585717f3
|
389 390 391 |
return 0; mutex_lock(&root->fs_info->reloc_mutex); |
6426c7ad6
|
392 |
record_root_in_trans(trans, root, 0); |
7585717f3
|
393 394 395 396 |
mutex_unlock(&root->fs_info->reloc_mutex); return 0; } |
4a9d8bdee
|
397 398 399 |
static inline int is_transaction_blocked(struct btrfs_transaction *trans) { return (trans->state >= TRANS_STATE_BLOCKED && |
501407aab
|
400 401 |
trans->state < TRANS_STATE_UNBLOCKED && !trans->aborted); |
4a9d8bdee
|
402 |
} |
d352ac681
|
403 404 405 406 |
/* wait for commit against the current transaction to become unblocked * when this is done, it is safe to start a new transaction, but the current * transaction might not be fully on disk. */ |
37d1aeee3
|
407 |
static void wait_current_trans(struct btrfs_root *root) |
79154b1b5
|
408 |
{ |
f92957493
|
409 |
struct btrfs_transaction *cur_trans; |
79154b1b5
|
410 |
|
a4abeea41
|
411 |
spin_lock(&root->fs_info->trans_lock); |
f92957493
|
412 |
cur_trans = root->fs_info->running_transaction; |
4a9d8bdee
|
413 |
if (cur_trans && is_transaction_blocked(cur_trans)) { |
13c5a93e7
|
414 |
atomic_inc(&cur_trans->use_count); |
a4abeea41
|
415 |
spin_unlock(&root->fs_info->trans_lock); |
72d63ed64
|
416 417 |
wait_event(root->fs_info->transaction_wait, |
501407aab
|
418 419 |
cur_trans->state >= TRANS_STATE_UNBLOCKED || cur_trans->aborted); |
724e2315d
|
420 |
btrfs_put_transaction(cur_trans); |
a4abeea41
|
421 422 |
} else { spin_unlock(&root->fs_info->trans_lock); |
f92957493
|
423 |
} |
37d1aeee3
|
424 |
} |
a22285a6a
|
425 426 |
static int may_wait_transaction(struct btrfs_root *root, int type) { |
afcdd129e
|
427 |
if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) |
a4abeea41
|
428 429 430 431 432 433 434 |
return 0; if (type == TRANS_USERSPACE) return 1; if (type == TRANS_START && !atomic_read(&root->fs_info->open_ioctl_trans)) |
a22285a6a
|
435 |
return 1; |
a4abeea41
|
436 |
|
a22285a6a
|
437 438 |
return 0; } |
20dd2cbf0
|
439 440 441 |
static inline bool need_reserve_reloc_root(struct btrfs_root *root) { if (!root->fs_info->reloc_ctl || |
27cdeb709
|
442 |
!test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
20dd2cbf0
|
443 444 445 446 447 448 |
root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || root->reloc_root) return false; return true; } |
08e007d2e
|
449 |
static struct btrfs_trans_handle * |
5aed1dd8b
|
450 451 |
start_transaction(struct btrfs_root *root, unsigned int num_items, unsigned int type, enum btrfs_reserve_flush_enum flush) |
37d1aeee3
|
452 |
{ |
a22285a6a
|
453 454 |
struct btrfs_trans_handle *h; struct btrfs_transaction *cur_trans; |
b5009945b
|
455 |
u64 num_bytes = 0; |
c55672379
|
456 |
u64 qgroup_reserved = 0; |
20dd2cbf0
|
457 458 |
bool reloc_reserved = false; int ret; |
acce952b0
|
459 |
|
46c4e71e9
|
460 |
/* Send isn't supposed to start transactions. */ |
2755a0de6
|
461 |
ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); |
46c4e71e9
|
462 |
|
87533c475
|
463 |
if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
acce952b0
|
464 |
return ERR_PTR(-EROFS); |
2a1eb4614
|
465 |
|
46c4e71e9
|
466 |
if (current->journal_info) { |
0860adfdb
|
467 |
WARN_ON(type & TRANS_EXTWRITERS); |
2a1eb4614
|
468 469 |
h = current->journal_info; h->use_count++; |
b7d5b0a81
|
470 |
WARN_ON(h->use_count > 2); |
2a1eb4614
|
471 472 473 474 |
h->orig_rsv = h->block_rsv; h->block_rsv = NULL; goto got_it; } |
b5009945b
|
475 476 477 478 479 480 |
/* * Do the reservation before we join the transaction so we can do all * the appropriate flushing if need be. */ if (num_items > 0 && root != root->fs_info->chunk_root) { |
7174109c6
|
481 482 483 484 |
qgroup_reserved = num_items * root->nodesize; ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved); if (ret) return ERR_PTR(ret); |
c55672379
|
485 |
|
b5009945b
|
486 |
num_bytes = btrfs_calc_trans_metadata_size(root, num_items); |
20dd2cbf0
|
487 488 489 |
/* * Do the reservation for the relocation root creation */ |
ee39b432b
|
490 |
if (need_reserve_reloc_root(root)) { |
20dd2cbf0
|
491 492 493 |
num_bytes += root->nodesize; reloc_reserved = true; } |
08e007d2e
|
494 495 496 |
ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, num_bytes, flush); |
b5009945b
|
497 |
if (ret) |
843fcf357
|
498 |
goto reserve_fail; |
b5009945b
|
499 |
} |
a22285a6a
|
500 |
again: |
f2f767e73
|
501 |
h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); |
843fcf357
|
502 503 504 505 |
if (!h) { ret = -ENOMEM; goto alloc_fail; } |
37d1aeee3
|
506 |
|
98114659e
|
507 508 509 510 511 512 |
/* * If we are JOIN_NOLOCK we're already committing a transaction and * waiting on this guy, so we don't need to do the sb_start_intwrite * because we're already holding a ref. We need this because we could * have raced in and did an fsync() on a file which can kick a commit * and then we deadlock with somebody doing a freeze. |
354aa0fb6
|
513 514 515 |
* * If we are ATTACH, it means we just want to catch the current * transaction and commit it, so we needn't do sb_start_intwrite(). |
98114659e
|
516 |
*/ |
0860adfdb
|
517 |
if (type & __TRANS_FREEZABLE) |
60376ce4a
|
518 |
sb_start_intwrite(root->fs_info->sb); |
b2b5ef5c8
|
519 |
|
a22285a6a
|
520 |
if (may_wait_transaction(root, type)) |
37d1aeee3
|
521 |
wait_current_trans(root); |
a22285a6a
|
522 |
|
a4abeea41
|
523 |
do { |
354aa0fb6
|
524 |
ret = join_transaction(root, type); |
178260b2c
|
525 |
if (ret == -EBUSY) { |
a4abeea41
|
526 |
wait_current_trans(root); |
178260b2c
|
527 528 529 |
if (unlikely(type == TRANS_ATTACH)) ret = -ENOENT; } |
a4abeea41
|
530 |
} while (ret == -EBUSY); |
a43f7f820
|
531 |
if (ret < 0) |
843fcf357
|
532 |
goto join_fail; |
0f7d52f44
|
533 |
|
a22285a6a
|
534 |
cur_trans = root->fs_info->running_transaction; |
a22285a6a
|
535 536 537 |
h->transid = cur_trans->transid; h->transaction = cur_trans; |
d13603ef6
|
538 |
h->root = root; |
2a1eb4614
|
539 |
h->use_count = 1; |
64b635807
|
540 |
h->fs_info = root->fs_info; |
7174109c6
|
541 |
|
a698d0755
|
542 |
h->type = type; |
d9a0540a7
|
543 |
h->can_flush_pending_bgs = true; |
bed92eae2
|
544 |
INIT_LIST_HEAD(&h->qgroup_ref_list); |
ea658badc
|
545 |
INIT_LIST_HEAD(&h->new_bgs); |
b7ec40d78
|
546 |
|
a22285a6a
|
547 |
smp_mb(); |
4a9d8bdee
|
548 549 |
if (cur_trans->state >= TRANS_STATE_BLOCKED && may_wait_transaction(root, type)) { |
abdd2e80a
|
550 |
current->journal_info = h; |
a22285a6a
|
551 552 553 |
btrfs_commit_transaction(h, root); goto again; } |
b5009945b
|
554 |
if (num_bytes) { |
8c2a3ca20
|
555 |
trace_btrfs_space_reservation(root->fs_info, "transaction", |
2bcc0328c
|
556 |
h->transid, num_bytes, 1); |
b5009945b
|
557 558 |
h->block_rsv = &root->fs_info->trans_block_rsv; h->bytes_reserved = num_bytes; |
20dd2cbf0
|
559 |
h->reloc_reserved = reloc_reserved; |
a22285a6a
|
560 |
} |
9ed74f2db
|
561 |
|
2a1eb4614
|
562 |
got_it: |
a4abeea41
|
563 |
btrfs_record_root_in_trans(h, root); |
a22285a6a
|
564 565 566 |
if (!current->journal_info && type != TRANS_USERSPACE) current->journal_info = h; |
79154b1b5
|
567 |
return h; |
843fcf357
|
568 569 |
join_fail: |
0860adfdb
|
570 |
if (type & __TRANS_FREEZABLE) |
843fcf357
|
571 572 573 574 575 576 577 |
sb_end_intwrite(root->fs_info->sb); kmem_cache_free(btrfs_trans_handle_cachep, h); alloc_fail: if (num_bytes) btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, num_bytes); reserve_fail: |
7174109c6
|
578 |
btrfs_qgroup_free_meta(root, qgroup_reserved); |
843fcf357
|
579 |
return ERR_PTR(ret); |
79154b1b5
|
580 |
} |
f92957493
|
581 |
struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
5aed1dd8b
|
582 |
unsigned int num_items) |
f92957493
|
583 |
{ |
08e007d2e
|
584 585 |
return start_transaction(root, num_items, TRANS_START, BTRFS_RESERVE_FLUSH_ALL); |
f92957493
|
586 |
} |
8eab77ff1
|
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( struct btrfs_root *root, unsigned int num_items, int min_factor) { struct btrfs_trans_handle *trans; u64 num_bytes; int ret; trans = btrfs_start_transaction(root, num_items); if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) return trans; trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) return trans; num_bytes = btrfs_calc_trans_metadata_size(root, num_items); ret = btrfs_cond_migrate_bytes(root->fs_info, &root->fs_info->trans_block_rsv, num_bytes, min_factor); if (ret) { btrfs_end_transaction(trans, root); return ERR_PTR(ret); } trans->block_rsv = &root->fs_info->trans_block_rsv; trans->bytes_reserved = num_bytes; |
88d3a5aaf
|
616 617 |
trace_btrfs_space_reservation(root->fs_info, "transaction", trans->transid, num_bytes, 1); |
8eab77ff1
|
618 619 620 |
return trans; } |
8407aa464
|
621 |
|
08e007d2e
|
622 |
struct btrfs_trans_handle *btrfs_start_transaction_lflush( |
5aed1dd8b
|
623 624 |
struct btrfs_root *root, unsigned int num_items) |
8407aa464
|
625 |
{ |
08e007d2e
|
626 627 |
return start_transaction(root, num_items, TRANS_START, BTRFS_RESERVE_FLUSH_LIMIT); |
8407aa464
|
628 |
} |
7a7eaa40a
|
629 |
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) |
f92957493
|
630 |
{ |
575a75d6f
|
631 632 |
return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH); |
f92957493
|
633 |
} |
7a7eaa40a
|
634 |
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) |
0af3d00ba
|
635 |
{ |
575a75d6f
|
636 637 |
return start_transaction(root, 0, TRANS_JOIN_NOLOCK, BTRFS_RESERVE_NO_FLUSH); |
0af3d00ba
|
638 |
} |
7a7eaa40a
|
639 |
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) |
9ca9ee09c
|
640 |
{ |
575a75d6f
|
641 642 |
return start_transaction(root, 0, TRANS_USERSPACE, BTRFS_RESERVE_NO_FLUSH); |
9ca9ee09c
|
643 |
} |
d4edf39bd
|
644 645 646 647 648 649 650 651 652 653 654 655 656 |
/* * btrfs_attach_transaction() - catch the running transaction * * It is used when we want to commit the current the transaction, but * don't want to start a new one. * * Note: If this function return -ENOENT, it just means there is no * running transaction. But it is possible that the inactive transaction * is still in the memory, not fully on disk. If you hope there is no * inactive transaction in the fs when -ENOENT is returned, you should * invoke * btrfs_attach_transaction_barrier() */ |
354aa0fb6
|
657 |
struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) |
60376ce4a
|
658 |
{ |
575a75d6f
|
659 660 |
return start_transaction(root, 0, TRANS_ATTACH, BTRFS_RESERVE_NO_FLUSH); |
60376ce4a
|
661 |
} |
d4edf39bd
|
662 |
/* |
90b6d2830
|
663 |
* btrfs_attach_transaction_barrier() - catch the running transaction |
d4edf39bd
|
664 665 666 667 668 669 670 671 672 |
* * It is similar to the above function, the differentia is this one * will wait for all the inactive transactions until they fully * complete. */ struct btrfs_trans_handle * btrfs_attach_transaction_barrier(struct btrfs_root *root) { struct btrfs_trans_handle *trans; |
575a75d6f
|
673 674 |
trans = start_transaction(root, 0, TRANS_ATTACH, BTRFS_RESERVE_NO_FLUSH); |
d4edf39bd
|
675 676 677 678 679 |
if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) btrfs_wait_for_commit(root, 0); return trans; } |
d352ac681
|
680 |
/* wait for a transaction commit to be fully complete */ |
b9c8300c2
|
681 |
static noinline void wait_for_commit(struct btrfs_root *root, |
89ce8a63d
|
682 683 |
struct btrfs_transaction *commit) { |
4a9d8bdee
|
684 |
wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); |
89ce8a63d
|
685 |
} |
462045928
|
686 687 688 |
int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) { struct btrfs_transaction *cur_trans = NULL, *t; |
8cd2807f7
|
689 |
int ret = 0; |
462045928
|
690 |
|
462045928
|
691 692 |
if (transid) { if (transid <= root->fs_info->last_trans_committed) |
a4abeea41
|
693 |
goto out; |
462045928
|
694 695 |
/* find specified transaction */ |
a4abeea41
|
696 |
spin_lock(&root->fs_info->trans_lock); |
462045928
|
697 698 699 |
list_for_each_entry(t, &root->fs_info->trans_list, list) { if (t->transid == transid) { cur_trans = t; |
a4abeea41
|
700 |
atomic_inc(&cur_trans->use_count); |
8cd2807f7
|
701 |
ret = 0; |
462045928
|
702 703 |
break; } |
8cd2807f7
|
704 705 |
if (t->transid > transid) { ret = 0; |
462045928
|
706 |
break; |
8cd2807f7
|
707 |
} |
462045928
|
708 |
} |
a4abeea41
|
709 |
spin_unlock(&root->fs_info->trans_lock); |
42383020b
|
710 711 712 713 714 715 716 717 |
/* * The specified transaction doesn't exist, or we * raced with btrfs_commit_transaction */ if (!cur_trans) { if (transid > root->fs_info->last_trans_committed) ret = -EINVAL; |
8cd2807f7
|
718 |
goto out; |
42383020b
|
719 |
} |
462045928
|
720 721 |
} else { /* find newest transaction that is committing | committed */ |
a4abeea41
|
722 |
spin_lock(&root->fs_info->trans_lock); |
462045928
|
723 724 |
list_for_each_entry_reverse(t, &root->fs_info->trans_list, list) { |
4a9d8bdee
|
725 726 |
if (t->state >= TRANS_STATE_COMMIT_START) { if (t->state == TRANS_STATE_COMPLETED) |
3473f3c06
|
727 |
break; |
462045928
|
728 |
cur_trans = t; |
a4abeea41
|
729 |
atomic_inc(&cur_trans->use_count); |
462045928
|
730 731 732 |
break; } } |
a4abeea41
|
733 |
spin_unlock(&root->fs_info->trans_lock); |
462045928
|
734 |
if (!cur_trans) |
a4abeea41
|
735 |
goto out; /* nothing committing|committed */ |
462045928
|
736 |
} |
462045928
|
737 |
wait_for_commit(root, cur_trans); |
724e2315d
|
738 |
btrfs_put_transaction(cur_trans); |
a4abeea41
|
739 |
out: |
462045928
|
740 741 |
return ret; } |
37d1aeee3
|
742 743 |
void btrfs_throttle(struct btrfs_root *root) { |
a4abeea41
|
744 |
if (!atomic_read(&root->fs_info->open_ioctl_trans)) |
9ca9ee09c
|
745 |
wait_current_trans(root); |
37d1aeee3
|
746 |
} |
8929ecfa5
|
747 748 749 |
static int should_end_transaction(struct btrfs_trans_handle *trans, struct btrfs_root *root) { |
1be41b78b
|
750 |
if (root->fs_info->global_block_rsv.space_info->full && |
0a2b2a844
|
751 |
btrfs_check_space_for_delayed_refs(trans, root)) |
1be41b78b
|
752 |
return 1; |
36ba022ac
|
753 |
|
1be41b78b
|
754 |
return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); |
8929ecfa5
|
755 756 757 758 759 760 761 |
} int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, struct btrfs_root *root) { struct btrfs_transaction *cur_trans = trans->transaction; int updates; |
49b25e054
|
762 |
int err; |
8929ecfa5
|
763 |
|
a4abeea41
|
764 |
smp_mb(); |
4a9d8bdee
|
765 766 |
if (cur_trans->state >= TRANS_STATE_BLOCKED || cur_trans->delayed_refs.flushing) |
8929ecfa5
|
767 768 769 770 |
return 1; updates = trans->delayed_ref_updates; trans->delayed_ref_updates = 0; |
49b25e054
|
771 |
if (updates) { |
28ed1345a
|
772 |
err = btrfs_run_delayed_refs(trans, root, updates * 2); |
49b25e054
|
773 774 775 |
if (err) /* Error code will also eval true */ return err; } |
8929ecfa5
|
776 777 778 |
return should_end_transaction(trans, root); } |
89ce8a63d
|
779 |
static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
a698d0755
|
780 |
struct btrfs_root *root, int throttle) |
79154b1b5
|
781 |
{ |
8929ecfa5
|
782 |
struct btrfs_transaction *cur_trans = trans->transaction; |
ab78c84de
|
783 |
struct btrfs_fs_info *info = root->fs_info; |
31b9655f4
|
784 |
u64 transid = trans->transid; |
1be41b78b
|
785 |
unsigned long cur = trans->delayed_ref_updates; |
a698d0755
|
786 |
int lock = (trans->type != TRANS_JOIN_NOLOCK); |
4edc2ca38
|
787 |
int err = 0; |
a79b7d4b3
|
788 |
int must_run_delayed_refs = 0; |
c3e69d58e
|
789 |
|
3bbb24b20
|
790 791 |
if (trans->use_count > 1) { trans->use_count--; |
2a1eb4614
|
792 793 794 |
trans->block_rsv = trans->orig_rsv; return 0; } |
b24e03db0
|
795 |
btrfs_trans_release_metadata(trans, root); |
4c13d758b
|
796 |
trans->block_rsv = NULL; |
c55672379
|
797 |
|
ea658badc
|
798 799 |
if (!list_empty(&trans->new_bgs)) btrfs_create_pending_block_groups(trans, root); |
1be41b78b
|
800 |
trans->delayed_ref_updates = 0; |
a79b7d4b3
|
801 802 803 |
if (!trans->sync) { must_run_delayed_refs = btrfs_should_throttle_delayed_refs(trans, root); |
0a2b2a844
|
804 |
cur = max_t(unsigned long, cur, 32); |
a79b7d4b3
|
805 806 807 808 809 810 811 812 |
/* * don't make the caller wait if they are from a NOLOCK * or ATTACH transaction, it will deadlock with commit */ if (must_run_delayed_refs == 1 && (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) must_run_delayed_refs = 2; |
56bec294d
|
813 |
} |
bb721703a
|
814 |
|
0e7211069
|
815 816 |
btrfs_trans_release_metadata(trans, root); trans->block_rsv = NULL; |
56bec294d
|
817 |
|
ea658badc
|
818 819 |
if (!list_empty(&trans->new_bgs)) btrfs_create_pending_block_groups(trans, root); |
4fbcdf669
|
820 |
btrfs_trans_release_chunk_metadata(trans); |
a4abeea41
|
821 |
if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && |
4a9d8bdee
|
822 823 824 825 826 827 |
should_end_transaction(trans, root) && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { spin_lock(&info->trans_lock); if (cur_trans->state == TRANS_STATE_RUNNING) cur_trans->state = TRANS_STATE_BLOCKED; spin_unlock(&info->trans_lock); |
a4abeea41
|
828 |
} |
8929ecfa5
|
829 |
|
4a9d8bdee
|
830 |
if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { |
3bbb24b20
|
831 |
if (throttle) |
8929ecfa5
|
832 |
return btrfs_commit_transaction(trans, root); |
3bbb24b20
|
833 |
else |
8929ecfa5
|
834 835 |
wake_up_process(info->transaction_kthread); } |
0860adfdb
|
836 |
if (trans->type & __TRANS_FREEZABLE) |
98114659e
|
837 |
sb_end_intwrite(root->fs_info->sb); |
6df7881a8
|
838 |
|
8929ecfa5
|
839 |
WARN_ON(cur_trans != info->running_transaction); |
13c5a93e7
|
840 841 |
WARN_ON(atomic_read(&cur_trans->num_writers) < 1); atomic_dec(&cur_trans->num_writers); |
0860adfdb
|
842 |
extwriter_counter_dec(cur_trans, trans->type); |
89ce8a63d
|
843 |
|
a83342aa0
|
844 845 846 |
/* * Make sure counter is updated before we wake up waiters. */ |
99d16cbca
|
847 |
smp_mb(); |
79154b1b5
|
848 849 |
if (waitqueue_active(&cur_trans->writer_wait)) wake_up(&cur_trans->writer_wait); |
724e2315d
|
850 |
btrfs_put_transaction(cur_trans); |
9ed74f2db
|
851 852 853 |
if (current->journal_info == trans) current->journal_info = NULL; |
ab78c84de
|
854 |
|
24bbcf044
|
855 856 |
if (throttle) btrfs_run_delayed_iputs(root); |
49b25e054
|
857 |
if (trans->aborted || |
4e121c06a
|
858 859 |
test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { wake_up_process(info->transaction_kthread); |
4edc2ca38
|
860 |
err = -EIO; |
4e121c06a
|
861 |
} |
edf39272d
|
862 |
assert_qgroups_uptodate(trans); |
49b25e054
|
863 |
|
4edc2ca38
|
864 |
kmem_cache_free(btrfs_trans_handle_cachep, trans); |
a79b7d4b3
|
865 |
if (must_run_delayed_refs) { |
31b9655f4
|
866 |
btrfs_async_run_delayed_refs(root, cur, transid, |
a79b7d4b3
|
867 868 |
must_run_delayed_refs == 1); } |
4edc2ca38
|
869 |
return err; |
79154b1b5
|
870 |
} |
89ce8a63d
|
871 872 873 |
int btrfs_end_transaction(struct btrfs_trans_handle *trans, struct btrfs_root *root) { |
98ad43be0
|
874 |
return __btrfs_end_transaction(trans, root, 0); |
89ce8a63d
|
875 876 877 878 879 |
} int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, struct btrfs_root *root) { |
98ad43be0
|
880 |
return __btrfs_end_transaction(trans, root, 1); |
16cdcec73
|
881 |
} |
d352ac681
|
882 883 884 |
/* * when btree blocks are allocated, they have some corresponding bits set for * them in one of two extent_io trees. This is used to make sure all of |
690587d10
|
885 |
* those extents are sent to disk but does not wait on them |
d352ac681
|
886 |
*/ |
690587d10
|
887 |
int btrfs_write_marked_extents(struct btrfs_root *root, |
8cef4e160
|
888 |
struct extent_io_tree *dirty_pages, int mark) |
79154b1b5
|
889 |
{ |
777e6bd70
|
890 |
int err = 0; |
7c4452b9a
|
891 |
int werr = 0; |
1728366ef
|
892 |
struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
e6138876a
|
893 |
struct extent_state *cached_state = NULL; |
777e6bd70
|
894 |
u64 start = 0; |
5f39d397d
|
895 |
u64 end; |
7c4452b9a
|
896 |
|
1728366ef
|
897 |
while (!find_first_extent_bit(dirty_pages, start, &start, &end, |
e6138876a
|
898 |
mark, &cached_state)) { |
663dfbb07
|
899 900 901 902 |
bool wait_writeback = false; err = convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, |
210aa2776
|
903 |
mark, &cached_state); |
663dfbb07
|
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
/* * convert_extent_bit can return -ENOMEM, which is most of the * time a temporary error. So when it happens, ignore the error * and wait for writeback of this range to finish - because we * failed to set the bit EXTENT_NEED_WAIT for the range, a call * to btrfs_wait_marked_extents() would not know that writeback * for this range started and therefore wouldn't wait for it to * finish - we don't want to commit a superblock that points to * btree nodes/leafs for which writeback hasn't finished yet * (and without errors). * We cleanup any entries left in the io tree when committing * the transaction (through clear_btree_io_tree()). */ if (err == -ENOMEM) { err = 0; wait_writeback = true; } if (!err) err = filemap_fdatawrite_range(mapping, start, end); |
1728366ef
|
923 924 |
if (err) werr = err; |
663dfbb07
|
925 926 |
else if (wait_writeback) werr = filemap_fdatawait_range(mapping, start, end); |
e38e2ed70
|
927 |
free_extent_state(cached_state); |
663dfbb07
|
928 |
cached_state = NULL; |
1728366ef
|
929 930 |
cond_resched(); start = end + 1; |
7c4452b9a
|
931 |
} |
690587d10
|
932 933 934 935 936 937 938 939 940 941 |
return werr; } /* * when btree blocks are allocated, they have some corresponding bits set for * them in one of two extent_io trees. This is used to make sure all of * those extents are on disk for transaction or log commit. We wait * on all the pages and clear them from the dirty pages state tree */ int btrfs_wait_marked_extents(struct btrfs_root *root, |
8cef4e160
|
942 |
struct extent_io_tree *dirty_pages, int mark) |
690587d10
|
943 |
{ |
690587d10
|
944 945 |
int err = 0; int werr = 0; |
1728366ef
|
946 |
struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
e6138876a
|
947 |
struct extent_state *cached_state = NULL; |
690587d10
|
948 949 |
u64 start = 0; u64 end; |
656f30dba
|
950 |
bool errors = false; |
777e6bd70
|
951 |
|
1728366ef
|
952 |
while (!find_first_extent_bit(dirty_pages, start, &start, &end, |
e6138876a
|
953 |
EXTENT_NEED_WAIT, &cached_state)) { |
663dfbb07
|
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
/* * Ignore -ENOMEM errors returned by clear_extent_bit(). * When committing the transaction, we'll remove any entries * left in the io tree. For a log commit, we don't remove them * after committing the log because the tree can be accessed * concurrently - we do it only at transaction commit time when * it's safe to do it (through clear_btree_io_tree()). */ err = clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 0, 0, &cached_state, GFP_NOFS); if (err == -ENOMEM) err = 0; if (!err) err = filemap_fdatawait_range(mapping, start, end); |
1728366ef
|
969 970 |
if (err) werr = err; |
e38e2ed70
|
971 972 |
free_extent_state(cached_state); cached_state = NULL; |
1728366ef
|
973 974 |
cond_resched(); start = end + 1; |
777e6bd70
|
975 |
} |
7c4452b9a
|
976 977 |
if (err) werr = err; |
656f30dba
|
978 979 980 |
if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { if ((mark & EXTENT_DIRTY) && |
afcdd129e
|
981 982 |
test_and_clear_bit(BTRFS_FS_LOG1_ERR, &root->fs_info->flags)) |
656f30dba
|
983 984 985 |
errors = true; if ((mark & EXTENT_NEW) && |
afcdd129e
|
986 987 |
test_and_clear_bit(BTRFS_FS_LOG2_ERR, &root->fs_info->flags)) |
656f30dba
|
988 989 |
errors = true; } else { |
afcdd129e
|
990 991 |
if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &root->fs_info->flags)) |
656f30dba
|
992 993 994 995 996 |
errors = true; } if (errors && !werr) werr = -EIO; |
7c4452b9a
|
997 |
return werr; |
79154b1b5
|
998 |
} |
690587d10
|
999 1000 1001 1002 1003 |
/* * when btree blocks are allocated, they have some corresponding bits set for * them in one of two extent_io trees. This is used to make sure all of * those extents are on disk for transaction or log commit */ |
171170c1c
|
1004 |
static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, |
8cef4e160
|
1005 |
struct extent_io_tree *dirty_pages, int mark) |
690587d10
|
1006 1007 1008 |
{ int ret; int ret2; |
c6adc9cc0
|
1009 |
struct blk_plug plug; |
690587d10
|
1010 |
|
c6adc9cc0
|
1011 |
blk_start_plug(&plug); |
8cef4e160
|
1012 |
ret = btrfs_write_marked_extents(root, dirty_pages, mark); |
c6adc9cc0
|
1013 |
blk_finish_plug(&plug); |
8cef4e160
|
1014 |
ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); |
bf0da8c18
|
1015 1016 1017 1018 1019 1020 |
if (ret) return ret; if (ret2) return ret2; return 0; |
690587d10
|
1021 |
} |
663dfbb07
|
1022 |
static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
d0c803c40
|
1023 1024 |
struct btrfs_root *root) { |
663dfbb07
|
1025 1026 1027 |
int ret; ret = btrfs_write_and_wait_marked_extents(root, |
8cef4e160
|
1028 1029 |
&trans->transaction->dirty_pages, EXTENT_DIRTY); |
663dfbb07
|
1030 1031 1032 |
clear_btree_io_tree(&trans->transaction->dirty_pages); return ret; |
d0c803c40
|
1033 |
} |
d352ac681
|
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
/* * this is used to update the root pointer in the tree of tree roots. * * But, in the case of the extent allocation tree, updating the root * pointer may allocate blocks which may change the root of the extent * allocation tree. * * So, this loops and repeats and makes sure the cowonly root didn't * change while the root pointer was being updated in the metadata. */ |
0b86a832a
|
1044 1045 |
static int update_cowonly_root(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
79154b1b5
|
1046 1047 |
{ int ret; |
0b86a832a
|
1048 |
u64 old_root_bytenr; |
86b9f2eca
|
1049 |
u64 old_root_used; |
0b86a832a
|
1050 |
struct btrfs_root *tree_root = root->fs_info->tree_root; |
79154b1b5
|
1051 |
|
86b9f2eca
|
1052 |
old_root_used = btrfs_root_used(&root->root_item); |
56bec294d
|
1053 |
|
d397712bc
|
1054 |
while (1) { |
0b86a832a
|
1055 |
old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
86b9f2eca
|
1056 |
if (old_root_bytenr == root->node->start && |
ea526d189
|
1057 |
old_root_used == btrfs_root_used(&root->root_item)) |
79154b1b5
|
1058 |
break; |
87ef2bb46
|
1059 |
|
5d4f98a28
|
1060 |
btrfs_set_root_node(&root->root_item, root->node); |
79154b1b5
|
1061 |
ret = btrfs_update_root(trans, tree_root, |
0b86a832a
|
1062 1063 |
&root->root_key, &root->root_item); |
49b25e054
|
1064 1065 |
if (ret) return ret; |
56bec294d
|
1066 |
|
86b9f2eca
|
1067 |
old_root_used = btrfs_root_used(&root->root_item); |
0b86a832a
|
1068 |
} |
276e680d1
|
1069 |
|
0b86a832a
|
1070 1071 |
return 0; } |
d352ac681
|
1072 1073 |
/* * update all the cowonly tree roots on disk |
49b25e054
|
1074 1075 1076 1077 |
* * The error handling in this function may not be obvious. Any of the * failures will cause the file system to go offline. We still need * to clean up the delayed refs. |
d352ac681
|
1078 |
*/ |
5d4f98a28
|
1079 1080 |
static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
0b86a832a
|
1081 1082 |
{ struct btrfs_fs_info *fs_info = root->fs_info; |
ea526d189
|
1083 |
struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; |
1bbc621ef
|
1084 |
struct list_head *io_bgs = &trans->transaction->io_bgs; |
0b86a832a
|
1085 |
struct list_head *next; |
84234f3a1
|
1086 |
struct extent_buffer *eb; |
56bec294d
|
1087 |
int ret; |
84234f3a1
|
1088 1089 |
eb = btrfs_lock_root_node(fs_info->tree_root); |
49b25e054
|
1090 1091 |
ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); |
84234f3a1
|
1092 1093 |
btrfs_tree_unlock(eb); free_extent_buffer(eb); |
0b86a832a
|
1094 |
|
49b25e054
|
1095 1096 |
if (ret) return ret; |
56bec294d
|
1097 |
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
49b25e054
|
1098 1099 |
if (ret) return ret; |
87ef2bb46
|
1100 |
|
733f4fbbc
|
1101 |
ret = btrfs_run_dev_stats(trans, root->fs_info); |
c16ce1901
|
1102 1103 |
if (ret) return ret; |
8dabb7420
|
1104 |
ret = btrfs_run_dev_replace(trans, root->fs_info); |
c16ce1901
|
1105 1106 |
if (ret) return ret; |
546adb0d8
|
1107 |
ret = btrfs_run_qgroups(trans, root->fs_info); |
c16ce1901
|
1108 1109 |
if (ret) return ret; |
546adb0d8
|
1110 |
|
dcdf7f6dd
|
1111 1112 1113 |
ret = btrfs_setup_space_cache(trans, root); if (ret) return ret; |
546adb0d8
|
1114 1115 |
/* run_qgroups might have added some more refs */ ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
c16ce1901
|
1116 1117 |
if (ret) return ret; |
ea526d189
|
1118 |
again: |
d397712bc
|
1119 |
while (!list_empty(&fs_info->dirty_cowonly_roots)) { |
0b86a832a
|
1120 1121 1122 |
next = fs_info->dirty_cowonly_roots.next; list_del_init(next); root = list_entry(next, struct btrfs_root, dirty_list); |
e7070be19
|
1123 |
clear_bit(BTRFS_ROOT_DIRTY, &root->state); |
87ef2bb46
|
1124 |
|
9e351cc86
|
1125 1126 1127 |
if (root != fs_info->extent_root) list_add_tail(&root->dirty_list, &trans->transaction->switch_commits); |
49b25e054
|
1128 1129 1130 |
ret = update_cowonly_root(trans, root); if (ret) return ret; |
ea526d189
|
1131 1132 1133 |
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); if (ret) return ret; |
79154b1b5
|
1134 |
} |
276e680d1
|
1135 |
|
1bbc621ef
|
1136 |
while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { |
ea526d189
|
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 |
ret = btrfs_write_dirty_block_groups(trans, root); if (ret) return ret; ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); if (ret) return ret; } if (!list_empty(&fs_info->dirty_cowonly_roots)) goto again; |
9e351cc86
|
1147 1148 |
list_add_tail(&fs_info->extent_root->dirty_list, &trans->transaction->switch_commits); |
8dabb7420
|
1149 |
btrfs_after_dev_replace_commit(fs_info); |
79154b1b5
|
1150 1151 |
return 0; } |
d352ac681
|
1152 1153 1154 1155 1156 |
/* * dead roots are old snapshots that need to be deleted. This allocates * a dirty root struct and adds it into the list of dead roots that need to * be deleted */ |
cfad392b2
|
1157 |
void btrfs_add_dead_root(struct btrfs_root *root) |
5eda7b5e9
|
1158 |
{ |
a4abeea41
|
1159 |
spin_lock(&root->fs_info->trans_lock); |
cfad392b2
|
1160 1161 |
if (list_empty(&root->root_list)) list_add_tail(&root->root_list, &root->fs_info->dead_roots); |
a4abeea41
|
1162 |
spin_unlock(&root->fs_info->trans_lock); |
5eda7b5e9
|
1163 |
} |
d352ac681
|
1164 |
/* |
5d4f98a28
|
1165 |
* update all the cowonly tree roots on disk |
d352ac681
|
1166 |
*/ |
5d4f98a28
|
1167 1168 |
static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, struct btrfs_root *root) |
0f7d52f44
|
1169 |
{ |
0f7d52f44
|
1170 |
struct btrfs_root *gang[8]; |
5d4f98a28
|
1171 |
struct btrfs_fs_info *fs_info = root->fs_info; |
0f7d52f44
|
1172 1173 |
int i; int ret; |
54aa1f4df
|
1174 |
int err = 0; |
a4abeea41
|
1175 |
spin_lock(&fs_info->fs_roots_radix_lock); |
d397712bc
|
1176 |
while (1) { |
5d4f98a28
|
1177 1178 |
ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, (void **)gang, 0, |
0f7d52f44
|
1179 1180 1181 1182 1183 1184 |
ARRAY_SIZE(gang), BTRFS_ROOT_TRANS_TAG); if (ret == 0) break; for (i = 0; i < ret; i++) { root = gang[i]; |
5d4f98a28
|
1185 1186 1187 |
radix_tree_tag_clear(&fs_info->fs_roots_radix, (unsigned long)root->root_key.objectid, BTRFS_ROOT_TRANS_TAG); |
a4abeea41
|
1188 |
spin_unlock(&fs_info->fs_roots_radix_lock); |
31153d812
|
1189 |
|
e02119d5a
|
1190 |
btrfs_free_log(trans, root); |
5d4f98a28
|
1191 |
btrfs_update_reloc_root(trans, root); |
d68fc57b7
|
1192 |
btrfs_orphan_commit_root(trans, root); |
bcc63abbf
|
1193 |
|
82d5902d9
|
1194 |
btrfs_save_ino_cache(root, trans); |
f1ebcc74d
|
1195 |
/* see comments in should_cow_block() */ |
27cdeb709
|
1196 |
clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); |
c7548af69
|
1197 |
smp_mb__after_atomic(); |
f1ebcc74d
|
1198 |
|
978d910d3
|
1199 |
if (root->commit_root != root->node) { |
9e351cc86
|
1200 1201 |
list_add_tail(&root->dirty_list, &trans->transaction->switch_commits); |
978d910d3
|
1202 1203 1204 |
btrfs_set_root_node(&root->root_item, root->node); } |
5d4f98a28
|
1205 |
|
5d4f98a28
|
1206 |
err = btrfs_update_root(trans, fs_info->tree_root, |
0f7d52f44
|
1207 1208 |
&root->root_key, &root->root_item); |
a4abeea41
|
1209 |
spin_lock(&fs_info->fs_roots_radix_lock); |
54aa1f4df
|
1210 1211 |
if (err) break; |
7174109c6
|
1212 |
btrfs_qgroup_free_meta_all(root); |
0f7d52f44
|
1213 1214 |
} } |
a4abeea41
|
1215 |
spin_unlock(&fs_info->fs_roots_radix_lock); |
54aa1f4df
|
1216 |
return err; |
0f7d52f44
|
1217 |
} |
d352ac681
|
1218 |
/* |
de78b51a2
|
1219 1220 |
* defrag a given btree. * Every leaf in the btree is read and defragged. |
d352ac681
|
1221 |
*/ |
de78b51a2
|
1222 |
int btrfs_defrag_root(struct btrfs_root *root) |
e9d0b13b5
|
1223 1224 |
{ struct btrfs_fs_info *info = root->fs_info; |
e9d0b13b5
|
1225 |
struct btrfs_trans_handle *trans; |
8929ecfa5
|
1226 |
int ret; |
e9d0b13b5
|
1227 |
|
27cdeb709
|
1228 |
if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) |
e9d0b13b5
|
1229 |
return 0; |
8929ecfa5
|
1230 |
|
6b80053d0
|
1231 |
while (1) { |
8929ecfa5
|
1232 1233 1234 |
trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) return PTR_ERR(trans); |
de78b51a2
|
1235 |
ret = btrfs_defrag_leaves(trans, root); |
8929ecfa5
|
1236 |
|
e9d0b13b5
|
1237 |
btrfs_end_transaction(trans, root); |
b53d3f5db
|
1238 |
btrfs_btree_balance_dirty(info->tree_root); |
e9d0b13b5
|
1239 |
cond_resched(); |
ab8d0fc48
|
1240 |
if (btrfs_fs_closing(info) || ret != -EAGAIN) |
e9d0b13b5
|
1241 |
break; |
210549ebe
|
1242 |
|
ab8d0fc48
|
1243 1244 |
if (btrfs_defrag_cancelled(info)) { btrfs_debug(info, "defrag_root cancelled"); |
210549ebe
|
1245 1246 1247 |
ret = -EAGAIN; break; } |
e9d0b13b5
|
1248 |
} |
27cdeb709
|
1249 |
clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); |
8929ecfa5
|
1250 |
return ret; |
e9d0b13b5
|
1251 |
} |
d352ac681
|
1252 |
/* |
6426c7ad6
|
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 |
* Do all special snapshot related qgroup dirty hack. * * Will do all needed qgroup inherit and dirty hack like switch commit * roots inside one transaction and write all btree into disk, to make * qgroup works. */ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root *src, struct btrfs_root *parent, struct btrfs_qgroup_inherit *inherit, u64 dst_objectid) { struct btrfs_fs_info *fs_info = src->fs_info; int ret; /* * Save some performance in the case that qgroups are not * enabled. If this check races with the ioctl, rescan will * kick in anyway. */ mutex_lock(&fs_info->qgroup_ioctl_lock); |
afcdd129e
|
1274 |
if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
6426c7ad6
|
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 |
mutex_unlock(&fs_info->qgroup_ioctl_lock); return 0; } mutex_unlock(&fs_info->qgroup_ioctl_lock); /* * We are going to commit transaction, see btrfs_commit_transaction() * comment for reason locking tree_log_mutex */ mutex_lock(&fs_info->tree_log_mutex); ret = commit_fs_roots(trans, src); if (ret) goto out; ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); if (ret < 0) goto out; ret = btrfs_qgroup_account_extents(trans, fs_info); if (ret < 0) goto out; /* Now qgroup are all updated, we can inherit it to new qgroups */ ret = btrfs_qgroup_inherit(trans, fs_info, src->root_key.objectid, dst_objectid, inherit); if (ret < 0) goto out; /* * Now we do a simplified commit transaction, which will: * 1) commit all subvolume and extent tree * To ensure all subvolume and extent tree have a valid * commit_root to accounting later insert_dir_item() * 2) write all btree blocks onto disk * This is to make sure later btree modification will be cowed * Or commit_root can be populated and cause wrong qgroup numbers * In this simplified commit, we don't really care about other trees * like chunk and root tree, as they won't affect qgroup. * And we don't write super to avoid half committed status. */ ret = commit_cowonly_roots(trans, src); if (ret) goto out; switch_commit_roots(trans->transaction, fs_info); ret = btrfs_write_and_wait_transaction(trans, src); if (ret) |
f7af3934c
|
1321 |
btrfs_handle_fs_error(fs_info, ret, |
6426c7ad6
|
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 |
"Error while writing out transaction for qgroup"); out: mutex_unlock(&fs_info->tree_log_mutex); /* * Force parent root to be updated, as we recorded it before so its * last_trans == cur_transid. * Or it won't be committed again onto disk after later * insert_dir_item() */ if (!ret) record_root_in_trans(trans, parent, 1); return ret; } /* |
d352ac681
|
1339 |
* new snapshots need to be created at a very specific time in the |
aec8030a8
|
1340 1341 1342 1343 1344 1345 |
* transaction commit. This does the actual creation. * * Note: * If the error which may affect the commitment of the current transaction * happens, we should return the error number. If the error which just affect * the creation of the pending snapshots, just return 0. |
d352ac681
|
1346 |
*/ |
80b6794d1
|
1347 |
static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
3063d29f2
|
1348 1349 1350 1351 |
struct btrfs_fs_info *fs_info, struct btrfs_pending_snapshot *pending) { struct btrfs_key key; |
80b6794d1
|
1352 |
struct btrfs_root_item *new_root_item; |
3063d29f2
|
1353 1354 |
struct btrfs_root *tree_root = fs_info->tree_root; struct btrfs_root *root = pending->root; |
6bdb72ded
|
1355 |
struct btrfs_root *parent_root; |
98c9942ac
|
1356 |
struct btrfs_block_rsv *rsv; |
6bdb72ded
|
1357 |
struct inode *parent_inode; |
42874b3db
|
1358 1359 |
struct btrfs_path *path; struct btrfs_dir_item *dir_item; |
a22285a6a
|
1360 |
struct dentry *dentry; |
3063d29f2
|
1361 |
struct extent_buffer *tmp; |
925baeddc
|
1362 |
struct extent_buffer *old; |
04b285f35
|
1363 |
struct timespec cur_time; |
aec8030a8
|
1364 |
int ret = 0; |
d68fc57b7
|
1365 |
u64 to_reserve = 0; |
6bdb72ded
|
1366 |
u64 index = 0; |
a22285a6a
|
1367 |
u64 objectid; |
b83cc9693
|
1368 |
u64 root_flags; |
8ea05e3a4
|
1369 |
uuid_le new_uuid; |
3063d29f2
|
1370 |
|
8546b5705
|
1371 1372 |
ASSERT(pending->path); path = pending->path; |
42874b3db
|
1373 |
|
b0c0ea633
|
1374 1375 |
ASSERT(pending->root_item); new_root_item = pending->root_item; |
a22285a6a
|
1376 |
|
aec8030a8
|
1377 1378 |
pending->error = btrfs_find_free_objectid(tree_root, &objectid); if (pending->error) |
6fa9700e7
|
1379 |
goto no_free_objectid; |
3063d29f2
|
1380 |
|
d67263354
|
1381 1382 1383 1384 1385 |
/* * Make qgroup to skip current new snapshot's qgroupid, as it is * accounted by later btrfs_qgroup_inherit(). */ btrfs_set_skip_qgroup(trans, objectid); |
147d256e0
|
1386 |
btrfs_reloc_pre_snapshot(pending, &to_reserve); |
d68fc57b7
|
1387 1388 |
if (to_reserve > 0) { |
aec8030a8
|
1389 1390 1391 1392 1393 |
pending->error = btrfs_block_rsv_add(root, &pending->block_rsv, to_reserve, BTRFS_RESERVE_NO_FLUSH); if (pending->error) |
d67263354
|
1394 |
goto clear_skip_qgroup; |
d68fc57b7
|
1395 |
} |
3063d29f2
|
1396 |
key.objectid = objectid; |
a22285a6a
|
1397 1398 |
key.offset = (u64)-1; key.type = BTRFS_ROOT_ITEM_KEY; |
3063d29f2
|
1399 |
|
6fa9700e7
|
1400 |
rsv = trans->block_rsv; |
a22285a6a
|
1401 |
trans->block_rsv = &pending->block_rsv; |
2382c5cc7
|
1402 |
trans->bytes_reserved = trans->block_rsv->reserved; |
88d3a5aaf
|
1403 1404 1405 |
trace_btrfs_space_reservation(root->fs_info, "transaction", trans->transid, trans->bytes_reserved, 1); |
a22285a6a
|
1406 |
dentry = pending->dentry; |
e9662f701
|
1407 |
parent_inode = pending->dir; |
a22285a6a
|
1408 |
parent_root = BTRFS_I(parent_inode)->root; |
6426c7ad6
|
1409 |
record_root_in_trans(trans, parent_root, 0); |
a22285a6a
|
1410 |
|
c2050a454
|
1411 |
cur_time = current_time(parent_inode); |
04b285f35
|
1412 |
|
3063d29f2
|
1413 1414 1415 |
/* * insert the directory item */ |
3de4586c5
|
1416 |
ret = btrfs_set_inode_index(parent_inode, &index); |
49b25e054
|
1417 |
BUG_ON(ret); /* -ENOMEM */ |
42874b3db
|
1418 1419 1420 1421 1422 1423 1424 |
/* check if there is a file/dir which has the same name. */ dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, btrfs_ino(parent_inode), dentry->d_name.name, dentry->d_name.len, 0); if (dir_item != NULL && !IS_ERR(dir_item)) { |
fe66a05a0
|
1425 |
pending->error = -EEXIST; |
aec8030a8
|
1426 |
goto dir_item_existed; |
42874b3db
|
1427 1428 |
} else if (IS_ERR(dir_item)) { ret = PTR_ERR(dir_item); |
66642832f
|
1429 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1430 |
goto fail; |
79787eaab
|
1431 |
} |
42874b3db
|
1432 |
btrfs_release_path(path); |
52c261799
|
1433 |
|
e999376f0
|
1434 1435 1436 1437 1438 1439 1440 |
/* * pull in the delayed directory update * and the delayed inode item * otherwise we corrupt the FS during * snapshot */ ret = btrfs_run_delayed_items(trans, root); |
8732d44f8
|
1441 |
if (ret) { /* Transaction aborted */ |
66642832f
|
1442 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1443 1444 |
goto fail; } |
e999376f0
|
1445 |
|
6426c7ad6
|
1446 |
record_root_in_trans(trans, root, 0); |
6bdb72ded
|
1447 1448 |
btrfs_set_root_last_snapshot(&root->root_item, trans->transid); memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
08fe4db17
|
1449 |
btrfs_check_and_init_root_item(new_root_item); |
6bdb72ded
|
1450 |
|
b83cc9693
|
1451 1452 1453 1454 1455 1456 |
root_flags = btrfs_root_flags(new_root_item); if (pending->readonly) root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; else root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; btrfs_set_root_flags(new_root_item, root_flags); |
8ea05e3a4
|
1457 1458 1459 1460 1461 1462 |
btrfs_set_root_generation_v2(new_root_item, trans->transid); uuid_le_gen(&new_uuid); memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); memcpy(new_root_item->parent_uuid, root->root_item.uuid, BTRFS_UUID_SIZE); |
70023da27
|
1463 1464 1465 1466 1467 1468 1469 1470 |
if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { memset(new_root_item->received_uuid, 0, sizeof(new_root_item->received_uuid)); memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); btrfs_set_root_stransid(new_root_item, 0); btrfs_set_root_rtransid(new_root_item, 0); } |
3cae210fa
|
1471 1472 |
btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); |
8ea05e3a4
|
1473 |
btrfs_set_root_otransid(new_root_item, trans->transid); |
8ea05e3a4
|
1474 |
|
6bdb72ded
|
1475 |
old = btrfs_lock_root_node(root); |
49b25e054
|
1476 |
ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); |
79787eaab
|
1477 1478 1479 |
if (ret) { btrfs_tree_unlock(old); free_extent_buffer(old); |
66642832f
|
1480 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1481 |
goto fail; |
79787eaab
|
1482 |
} |
49b25e054
|
1483 |
|
6bdb72ded
|
1484 |
btrfs_set_lock_blocking(old); |
49b25e054
|
1485 |
ret = btrfs_copy_root(trans, root, old, &tmp, objectid); |
79787eaab
|
1486 |
/* clean up in any case */ |
6bdb72ded
|
1487 1488 |
btrfs_tree_unlock(old); free_extent_buffer(old); |
8732d44f8
|
1489 |
if (ret) { |
66642832f
|
1490 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1491 1492 |
goto fail; } |
f1ebcc74d
|
1493 |
/* see comments in should_cow_block() */ |
27cdeb709
|
1494 |
set_bit(BTRFS_ROOT_FORCE_COW, &root->state); |
f1ebcc74d
|
1495 |
smp_wmb(); |
6bdb72ded
|
1496 |
btrfs_set_root_node(new_root_item, tmp); |
a22285a6a
|
1497 1498 1499 |
/* record when the snapshot was created in key.offset */ key.offset = trans->transid; ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); |
6bdb72ded
|
1500 1501 |
btrfs_tree_unlock(tmp); free_extent_buffer(tmp); |
8732d44f8
|
1502 |
if (ret) { |
66642832f
|
1503 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1504 1505 |
goto fail; } |
6bdb72ded
|
1506 |
|
a22285a6a
|
1507 1508 1509 1510 |
/* * insert root back/forward references */ ret = btrfs_add_root_ref(trans, tree_root, objectid, |
0660b5af3
|
1511 |
parent_root->root_key.objectid, |
33345d015
|
1512 |
btrfs_ino(parent_inode), index, |
a22285a6a
|
1513 |
dentry->d_name.name, dentry->d_name.len); |
8732d44f8
|
1514 |
if (ret) { |
66642832f
|
1515 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1516 1517 |
goto fail; } |
0660b5af3
|
1518 |
|
a22285a6a
|
1519 1520 |
key.offset = (u64)-1; pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); |
79787eaab
|
1521 1522 |
if (IS_ERR(pending->snap)) { ret = PTR_ERR(pending->snap); |
66642832f
|
1523 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1524 |
goto fail; |
79787eaab
|
1525 |
} |
d68fc57b7
|
1526 |
|
49b25e054
|
1527 |
ret = btrfs_reloc_post_snapshot(trans, pending); |
8732d44f8
|
1528 |
if (ret) { |
66642832f
|
1529 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1530 1531 |
goto fail; } |
361048f58
|
1532 1533 |
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
8732d44f8
|
1534 |
if (ret) { |
66642832f
|
1535 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1536 1537 |
goto fail; } |
42874b3db
|
1538 |
|
6426c7ad6
|
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 |
/* * Do special qgroup accounting for snapshot, as we do some qgroup * snapshot hack to do fast snapshot. * To co-operate with that hack, we do hack again. * Or snapshot will be greatly slowed down by a subtree qgroup rescan */ ret = qgroup_account_snapshot(trans, root, parent_root, pending->inherit, objectid); if (ret < 0) goto fail; |
42874b3db
|
1549 1550 1551 1552 1553 |
ret = btrfs_insert_dir_item(trans, parent_root, dentry->d_name.name, dentry->d_name.len, parent_inode, &key, BTRFS_FT_DIR, index); /* We have check then name at the beginning, so it is impossible. */ |
9c52057c6
|
1554 |
BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); |
8732d44f8
|
1555 |
if (ret) { |
66642832f
|
1556 |
btrfs_abort_transaction(trans, ret); |
8732d44f8
|
1557 1558 |
goto fail; } |
42874b3db
|
1559 1560 1561 |
btrfs_i_size_write(parent_inode, parent_inode->i_size + dentry->d_name.len * 2); |
04b285f35
|
1562 |
parent_inode->i_mtime = parent_inode->i_ctime = |
c2050a454
|
1563 |
current_time(parent_inode); |
be6aef604
|
1564 |
ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); |
dd5f9615f
|
1565 |
if (ret) { |
66642832f
|
1566 |
btrfs_abort_transaction(trans, ret); |
dd5f9615f
|
1567 1568 1569 1570 1571 |
goto fail; } ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, objectid); if (ret) { |
66642832f
|
1572 |
btrfs_abort_transaction(trans, ret); |
dd5f9615f
|
1573 1574 1575 1576 1577 1578 1579 1580 |
goto fail; } if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_root_item->received_uuid, BTRFS_UUID_KEY_RECEIVED_SUBVOL, objectid); if (ret && ret != -EEXIST) { |
66642832f
|
1581 |
btrfs_abort_transaction(trans, ret); |
dd5f9615f
|
1582 1583 1584 |
goto fail; } } |
d67263354
|
1585 1586 1587 |
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); if (ret) { |
66642832f
|
1588 |
btrfs_abort_transaction(trans, ret); |
d67263354
|
1589 1590 |
goto fail; } |
3063d29f2
|
1591 |
fail: |
aec8030a8
|
1592 1593 |
pending->error = ret; dir_item_existed: |
98c9942ac
|
1594 |
trans->block_rsv = rsv; |
2382c5cc7
|
1595 |
trans->bytes_reserved = 0; |
d67263354
|
1596 1597 |
clear_skip_qgroup: btrfs_clear_skip_qgroup(trans); |
6fa9700e7
|
1598 1599 |
no_free_objectid: kfree(new_root_item); |
b0c0ea633
|
1600 |
pending->root_item = NULL; |
42874b3db
|
1601 |
btrfs_free_path(path); |
8546b5705
|
1602 |
pending->path = NULL; |
49b25e054
|
1603 |
return ret; |
3063d29f2
|
1604 |
} |
d352ac681
|
1605 1606 1607 |
/* * create all the snapshots we've scheduled for creation */ |
80b6794d1
|
1608 1609 |
static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info) |
3063d29f2
|
1610 |
{ |
aec8030a8
|
1611 |
struct btrfs_pending_snapshot *pending, *next; |
3063d29f2
|
1612 |
struct list_head *head = &trans->transaction->pending_snapshots; |
aec8030a8
|
1613 |
int ret = 0; |
3de4586c5
|
1614 |
|
aec8030a8
|
1615 1616 1617 1618 1619 1620 1621 |
list_for_each_entry_safe(pending, next, head, list) { list_del(&pending->list); ret = create_pending_snapshot(trans, fs_info, pending); if (ret) break; } return ret; |
3de4586c5
|
1622 |
} |
5d4f98a28
|
1623 1624 1625 1626 |
static void update_super_roots(struct btrfs_root *root) { struct btrfs_root_item *root_item; struct btrfs_super_block *super; |
6c41761fc
|
1627 |
super = root->fs_info->super_copy; |
5d4f98a28
|
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 |
root_item = &root->fs_info->chunk_root->root_item; super->chunk_root = root_item->bytenr; super->chunk_root_generation = root_item->generation; super->chunk_root_level = root_item->level; root_item = &root->fs_info->tree_root->root_item; super->root = root_item->bytenr; super->generation = root_item->generation; super->root_level = root_item->level; |
3cdde2240
|
1638 |
if (btrfs_test_opt(root->fs_info, SPACE_CACHE)) |
0af3d00ba
|
1639 |
super->cache_generation = root_item->generation; |
afcdd129e
|
1640 |
if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &root->fs_info->flags)) |
70f801754
|
1641 |
super->uuid_tree_generation = root_item->generation; |
5d4f98a28
|
1642 |
} |
f36f3042e
|
1643 1644 |
int btrfs_transaction_in_commit(struct btrfs_fs_info *info) { |
4a9d8bdee
|
1645 |
struct btrfs_transaction *trans; |
f36f3042e
|
1646 |
int ret = 0; |
4a9d8bdee
|
1647 |
|
a4abeea41
|
1648 |
spin_lock(&info->trans_lock); |
4a9d8bdee
|
1649 1650 1651 |
trans = info->running_transaction; if (trans) ret = (trans->state >= TRANS_STATE_COMMIT_START); |
a4abeea41
|
1652 |
spin_unlock(&info->trans_lock); |
f36f3042e
|
1653 1654 |
return ret; } |
8929ecfa5
|
1655 1656 |
int btrfs_transaction_blocked(struct btrfs_fs_info *info) { |
4a9d8bdee
|
1657 |
struct btrfs_transaction *trans; |
8929ecfa5
|
1658 |
int ret = 0; |
4a9d8bdee
|
1659 |
|
a4abeea41
|
1660 |
spin_lock(&info->trans_lock); |
4a9d8bdee
|
1661 1662 1663 |
trans = info->running_transaction; if (trans) ret = is_transaction_blocked(trans); |
a4abeea41
|
1664 |
spin_unlock(&info->trans_lock); |
8929ecfa5
|
1665 1666 |
return ret; } |
bb9c12c94
|
1667 1668 1669 1670 1671 1672 1673 |
/* * wait for the current transaction commit to start and block subsequent * transaction joins */ static void wait_current_trans_commit_start(struct btrfs_root *root, struct btrfs_transaction *trans) { |
4a9d8bdee
|
1674 |
wait_event(root->fs_info->transaction_blocked_wait, |
501407aab
|
1675 1676 |
trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); |
bb9c12c94
|
1677 1678 1679 1680 1681 1682 1683 1684 1685 |
} /* * wait for the current transaction to start and then become unblocked. * caller holds ref. */ static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, struct btrfs_transaction *trans) { |
72d63ed64
|
1686 |
wait_event(root->fs_info->transaction_wait, |
501407aab
|
1687 1688 |
trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); |
bb9c12c94
|
1689 1690 1691 1692 1693 1694 1695 1696 1697 |
} /* * commit transactions asynchronously. once btrfs_commit_transaction_async * returns, any subsequent transaction will not be allowed to join. */ struct btrfs_async_commit { struct btrfs_trans_handle *newtrans; struct btrfs_root *root; |
7892b5afe
|
1698 |
struct work_struct work; |
bb9c12c94
|
1699 1700 1701 1702 1703 |
}; static void do_async_commit(struct work_struct *work) { struct btrfs_async_commit *ac = |
7892b5afe
|
1704 |
container_of(work, struct btrfs_async_commit, work); |
bb9c12c94
|
1705 |
|
6fc4e3548
|
1706 1707 1708 1709 |
/* * We've got freeze protection passed with the transaction. * Tell lockdep about it. */ |
b1a06a4b5
|
1710 |
if (ac->newtrans->type & __TRANS_FREEZABLE) |
bee9182d9
|
1711 |
__sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); |
6fc4e3548
|
1712 |
|
e209db7ac
|
1713 |
current->journal_info = ac->newtrans; |
bb9c12c94
|
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 |
btrfs_commit_transaction(ac->newtrans, ac->root); kfree(ac); } int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, struct btrfs_root *root, int wait_for_unblock) { struct btrfs_async_commit *ac; struct btrfs_transaction *cur_trans; ac = kmalloc(sizeof(*ac), GFP_NOFS); |
db5b493ac
|
1726 1727 |
if (!ac) return -ENOMEM; |
bb9c12c94
|
1728 |
|
7892b5afe
|
1729 |
INIT_WORK(&ac->work, do_async_commit); |
bb9c12c94
|
1730 |
ac->root = root; |
7a7eaa40a
|
1731 |
ac->newtrans = btrfs_join_transaction(root); |
3612b4959
|
1732 1733 1734 1735 1736 |
if (IS_ERR(ac->newtrans)) { int err = PTR_ERR(ac->newtrans); kfree(ac); return err; } |
bb9c12c94
|
1737 1738 |
/* take transaction reference */ |
bb9c12c94
|
1739 |
cur_trans = trans->transaction; |
13c5a93e7
|
1740 |
atomic_inc(&cur_trans->use_count); |
bb9c12c94
|
1741 1742 |
btrfs_end_transaction(trans, root); |
6fc4e3548
|
1743 1744 1745 1746 1747 |
/* * Tell lockdep we've released the freeze rwsem, since the * async commit thread will be the one to unlock it. */ |
b1a06a4b5
|
1748 |
if (ac->newtrans->type & __TRANS_FREEZABLE) |
bee9182d9
|
1749 |
__sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); |
6fc4e3548
|
1750 |
|
7892b5afe
|
1751 |
schedule_work(&ac->work); |
bb9c12c94
|
1752 1753 |
/* wait for transaction to start and unblock */ |
bb9c12c94
|
1754 1755 1756 1757 |
if (wait_for_unblock) wait_current_trans_commit_start_and_unblock(root, cur_trans); else wait_current_trans_commit_start(root, cur_trans); |
bb9c12c94
|
1758 |
|
38e880540
|
1759 1760 |
if (current->journal_info == trans) current->journal_info = NULL; |
724e2315d
|
1761 |
btrfs_put_transaction(cur_trans); |
bb9c12c94
|
1762 1763 |
return 0; } |
49b25e054
|
1764 1765 |
static void cleanup_transaction(struct btrfs_trans_handle *trans, |
7b8b92af5
|
1766 |
struct btrfs_root *root, int err) |
49b25e054
|
1767 1768 |
{ struct btrfs_transaction *cur_trans = trans->transaction; |
f094ac32a
|
1769 |
DEFINE_WAIT(wait); |
49b25e054
|
1770 1771 |
WARN_ON(trans->use_count > 1); |
66642832f
|
1772 |
btrfs_abort_transaction(trans, err); |
7b8b92af5
|
1773 |
|
49b25e054
|
1774 |
spin_lock(&root->fs_info->trans_lock); |
66b6135b7
|
1775 |
|
25d8c284c
|
1776 1777 1778 1779 1780 1781 |
/* * If the transaction is removed from the list, it means this * transaction has been committed successfully, so it is impossible * to call the cleanup function. */ BUG_ON(list_empty(&cur_trans->list)); |
66b6135b7
|
1782 |
|
49b25e054
|
1783 |
list_del_init(&cur_trans->list); |
d7096fc3e
|
1784 |
if (cur_trans == root->fs_info->running_transaction) { |
4a9d8bdee
|
1785 |
cur_trans->state = TRANS_STATE_COMMIT_DOING; |
f094ac32a
|
1786 1787 1788 1789 1790 |
spin_unlock(&root->fs_info->trans_lock); wait_event(cur_trans->writer_wait, atomic_read(&cur_trans->num_writers) == 1); spin_lock(&root->fs_info->trans_lock); |
d7096fc3e
|
1791 |
} |
49b25e054
|
1792 1793 1794 |
spin_unlock(&root->fs_info->trans_lock); btrfs_cleanup_one_transaction(trans->transaction, root); |
4a9d8bdee
|
1795 1796 1797 1798 |
spin_lock(&root->fs_info->trans_lock); if (cur_trans == root->fs_info->running_transaction) root->fs_info->running_transaction = NULL; spin_unlock(&root->fs_info->trans_lock); |
e0228285a
|
1799 1800 |
if (trans->type & __TRANS_FREEZABLE) sb_end_intwrite(root->fs_info->sb); |
724e2315d
|
1801 1802 |
btrfs_put_transaction(cur_trans); btrfs_put_transaction(cur_trans); |
49b25e054
|
1803 1804 |
trace_btrfs_transaction_commit(root); |
49b25e054
|
1805 1806 |
if (current->journal_info == trans) current->journal_info = NULL; |
c0af8f0b1
|
1807 |
btrfs_scrub_cancel(root->fs_info); |
49b25e054
|
1808 1809 1810 |
kmem_cache_free(btrfs_trans_handle_cachep, trans); } |
824366177
|
1811 1812 |
static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) { |
3cdde2240
|
1813 |
if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) |
6c255e67c
|
1814 |
return btrfs_start_delalloc_roots(fs_info, 1, -1); |
824366177
|
1815 1816 1817 1818 1819 |
return 0; } static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) { |
3cdde2240
|
1820 |
if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) |
578def7c5
|
1821 |
btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1); |
824366177
|
1822 |
} |
50d9aa99b
|
1823 |
static inline void |
161c3549b
|
1824 |
btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) |
50d9aa99b
|
1825 |
{ |
161c3549b
|
1826 1827 |
wait_event(cur_trans->pending_wait, atomic_read(&cur_trans->pending_ordered) == 0); |
50d9aa99b
|
1828 |
} |
79154b1b5
|
1829 1830 1831 |
int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct btrfs_root *root) { |
49b25e054
|
1832 |
struct btrfs_transaction *cur_trans = trans->transaction; |
8fd17795b
|
1833 |
struct btrfs_transaction *prev_trans = NULL; |
25287e0a1
|
1834 |
int ret; |
79154b1b5
|
1835 |
|
8d25a086e
|
1836 1837 |
/* Stop the commit early if ->aborted is set */ if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { |
25287e0a1
|
1838 |
ret = cur_trans->aborted; |
e4a2bcaca
|
1839 1840 |
btrfs_end_transaction(trans, root); return ret; |
25287e0a1
|
1841 |
} |
49b25e054
|
1842 |
|
56bec294d
|
1843 1844 1845 1846 |
/* make a pass through all the delayed refs we have so far * any runnings procs may add more while we are here */ ret = btrfs_run_delayed_refs(trans, root, 0); |
e4a2bcaca
|
1847 1848 1849 1850 |
if (ret) { btrfs_end_transaction(trans, root); return ret; } |
56bec294d
|
1851 |
|
0e7211069
|
1852 1853 |
btrfs_trans_release_metadata(trans, root); trans->block_rsv = NULL; |
b7ec40d78
|
1854 |
cur_trans = trans->transaction; |
49b25e054
|
1855 |
|
56bec294d
|
1856 1857 1858 1859 |
/* * set the flushing flag so procs in this transaction have to * start sending their work down. */ |
b7ec40d78
|
1860 |
cur_trans->delayed_refs.flushing = 1; |
1be41b78b
|
1861 |
smp_wmb(); |
56bec294d
|
1862 |
|
ea658badc
|
1863 1864 |
if (!list_empty(&trans->new_bgs)) btrfs_create_pending_block_groups(trans, root); |
c3e69d58e
|
1865 |
ret = btrfs_run_delayed_refs(trans, root, 0); |
e4a2bcaca
|
1866 1867 1868 1869 |
if (ret) { btrfs_end_transaction(trans, root); return ret; } |
56bec294d
|
1870 |
|
3204d33cd
|
1871 |
if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { |
1bbc621ef
|
1872 1873 1874 1875 1876 1877 1878 1879 |
int run_it = 0; /* this mutex is also taken before trying to set * block groups readonly. We need to make sure * that nobody has set a block group readonly * after a extents from that block group have been * allocated for cache files. btrfs_set_block_group_ro * will wait for the transaction to commit if it |
3204d33cd
|
1880 |
* finds BTRFS_TRANS_DIRTY_BG_RUN set. |
1bbc621ef
|
1881 |
* |
3204d33cd
|
1882 1883 |
* The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure * only one process starts all the block group IO. It wouldn't |
1bbc621ef
|
1884 1885 1886 1887 |
* hurt to have more than one go through, but there's no * real advantage to it either. */ mutex_lock(&root->fs_info->ro_block_group_mutex); |
3204d33cd
|
1888 1889 |
if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) |
1bbc621ef
|
1890 |
run_it = 1; |
1bbc621ef
|
1891 1892 1893 1894 1895 1896 1897 1898 1899 |
mutex_unlock(&root->fs_info->ro_block_group_mutex); if (run_it) ret = btrfs_start_dirty_block_groups(trans, root); } if (ret) { btrfs_end_transaction(trans, root); return ret; } |
4a9d8bdee
|
1900 1901 1902 |
spin_lock(&root->fs_info->trans_lock); if (cur_trans->state >= TRANS_STATE_COMMIT_START) { spin_unlock(&root->fs_info->trans_lock); |
13c5a93e7
|
1903 |
atomic_inc(&cur_trans->use_count); |
49b25e054
|
1904 |
ret = btrfs_end_transaction(trans, root); |
ccd467d60
|
1905 |
|
b9c8300c2
|
1906 |
wait_for_commit(root, cur_trans); |
15ee9bc7e
|
1907 |
|
b4924a0fa
|
1908 1909 |
if (unlikely(cur_trans->aborted)) ret = cur_trans->aborted; |
724e2315d
|
1910 |
btrfs_put_transaction(cur_trans); |
15ee9bc7e
|
1911 |
|
49b25e054
|
1912 |
return ret; |
79154b1b5
|
1913 |
} |
4313b3994
|
1914 |
|
4a9d8bdee
|
1915 |
cur_trans->state = TRANS_STATE_COMMIT_START; |
bb9c12c94
|
1916 |
wake_up(&root->fs_info->transaction_blocked_wait); |
ccd467d60
|
1917 1918 1919 |
if (cur_trans->list.prev != &root->fs_info->trans_list) { prev_trans = list_entry(cur_trans->list.prev, struct btrfs_transaction, list); |
4a9d8bdee
|
1920 |
if (prev_trans->state != TRANS_STATE_COMPLETED) { |
13c5a93e7
|
1921 |
atomic_inc(&prev_trans->use_count); |
a4abeea41
|
1922 |
spin_unlock(&root->fs_info->trans_lock); |
ccd467d60
|
1923 1924 |
wait_for_commit(root, prev_trans); |
1f9b8c8fb
|
1925 |
ret = prev_trans->aborted; |
ccd467d60
|
1926 |
|
724e2315d
|
1927 |
btrfs_put_transaction(prev_trans); |
1f9b8c8fb
|
1928 1929 |
if (ret) goto cleanup_transaction; |
a4abeea41
|
1930 1931 |
} else { spin_unlock(&root->fs_info->trans_lock); |
ccd467d60
|
1932 |
} |
a4abeea41
|
1933 1934 |
} else { spin_unlock(&root->fs_info->trans_lock); |
ccd467d60
|
1935 |
} |
15ee9bc7e
|
1936 |
|
0860adfdb
|
1937 |
extwriter_counter_dec(cur_trans, trans->type); |
824366177
|
1938 1939 1940 |
ret = btrfs_start_delalloc_flush(root->fs_info); if (ret) goto cleanup_transaction; |
8d875f95d
|
1941 |
ret = btrfs_run_delayed_items(trans, root); |
581227d0d
|
1942 1943 |
if (ret) goto cleanup_transaction; |
15ee9bc7e
|
1944 |
|
581227d0d
|
1945 1946 |
wait_event(cur_trans->writer_wait, extwriter_counter_read(cur_trans) == 0); |
15ee9bc7e
|
1947 |
|
581227d0d
|
1948 |
/* some pending stuffs might be added after the previous flush. */ |
8d875f95d
|
1949 |
ret = btrfs_run_delayed_items(trans, root); |
ca4696371
|
1950 1951 |
if (ret) goto cleanup_transaction; |
824366177
|
1952 |
btrfs_wait_delalloc_flush(root->fs_info); |
cb7ab0215
|
1953 |
|
161c3549b
|
1954 |
btrfs_wait_pending_ordered(cur_trans); |
50d9aa99b
|
1955 |
|
cb7ab0215
|
1956 |
btrfs_scrub_pause(root); |
7585717f3
|
1957 |
/* |
ed0ca1402
|
1958 1959 |
* Ok now we need to make sure to block out any other joins while we * commit the transaction. We could have started a join before setting |
4a9d8bdee
|
1960 |
* COMMIT_DOING so make sure to wait for num_writers to == 1 again. |
ed0ca1402
|
1961 1962 |
*/ spin_lock(&root->fs_info->trans_lock); |
4a9d8bdee
|
1963 |
cur_trans->state = TRANS_STATE_COMMIT_DOING; |
ed0ca1402
|
1964 1965 1966 |
spin_unlock(&root->fs_info->trans_lock); wait_event(cur_trans->writer_wait, atomic_read(&cur_trans->num_writers) == 1); |
2cba30f17
|
1967 1968 1969 |
/* ->aborted might be set after the previous check, so check it */ if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { ret = cur_trans->aborted; |
6cf7f77e6
|
1970 |
goto scrub_continue; |
2cba30f17
|
1971 |
} |
e038dca80
|
1972 |
/* |
7585717f3
|
1973 1974 1975 1976 1977 |
* the reloc mutex makes sure that we stop * the balancing code from coming in and moving * extents around in the middle of the commit */ mutex_lock(&root->fs_info->reloc_mutex); |
42874b3db
|
1978 1979 1980 1981 1982 1983 |
/* * We needn't worry about the delayed items because we will * deal with them in create_pending_snapshot(), which is the * core function of the snapshot creation. */ ret = create_pending_snapshots(trans, root->fs_info); |
49b25e054
|
1984 1985 |
if (ret) { mutex_unlock(&root->fs_info->reloc_mutex); |
6cf7f77e6
|
1986 |
goto scrub_continue; |
49b25e054
|
1987 |
} |
3063d29f2
|
1988 |
|
42874b3db
|
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 |
/* * We insert the dir indexes of the snapshots and update the inode * of the snapshots' parents after the snapshot creation, so there * are some delayed items which are not dealt with. Now deal with * them. * * We needn't worry that this operation will corrupt the snapshots, * because all the tree which are snapshoted will be forced to COW * the nodes and leaves. */ ret = btrfs_run_delayed_items(trans, root); |
49b25e054
|
2000 2001 |
if (ret) { mutex_unlock(&root->fs_info->reloc_mutex); |
6cf7f77e6
|
2002 |
goto scrub_continue; |
49b25e054
|
2003 |
} |
16cdcec73
|
2004 |
|
56bec294d
|
2005 |
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
49b25e054
|
2006 2007 |
if (ret) { mutex_unlock(&root->fs_info->reloc_mutex); |
6cf7f77e6
|
2008 |
goto scrub_continue; |
49b25e054
|
2009 |
} |
56bec294d
|
2010 |
|
0ed4792af
|
2011 2012 2013 2014 2015 2016 |
/* Reocrd old roots for later qgroup accounting */ ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); if (ret) { mutex_unlock(&root->fs_info->reloc_mutex); goto scrub_continue; } |
e999376f0
|
2017 2018 2019 2020 2021 |
/* * make sure none of the code above managed to slip in a * delayed item */ btrfs_assert_delayed_root_empty(root); |
2c90e5d65
|
2022 |
WARN_ON(cur_trans != trans->transaction); |
dc17ff8f1
|
2023 |
|
e02119d5a
|
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 |
/* btrfs_commit_tree_roots is responsible for getting the * various roots consistent with each other. Every pointer * in the tree of tree roots has to point to the most up to date * root for every subvolume and other tree. So, we have to keep * the tree logging code from jumping in and changing any * of the trees. * * At this point in the commit, there can't be any tree-log * writers, but a little lower down we drop the trans mutex * and let new people in. By holding the tree_log_mutex * from now until after the super is written, we avoid races * with the tree-log code. */ mutex_lock(&root->fs_info->tree_log_mutex); |
5d4f98a28
|
2038 |
ret = commit_fs_roots(trans, root); |
49b25e054
|
2039 2040 |
if (ret) { mutex_unlock(&root->fs_info->tree_log_mutex); |
871383be5
|
2041 |
mutex_unlock(&root->fs_info->reloc_mutex); |
6cf7f77e6
|
2042 |
goto scrub_continue; |
49b25e054
|
2043 |
} |
54aa1f4df
|
2044 |
|
3818aea27
|
2045 |
/* |
7e1876aca
|
2046 2047 |
* Since the transaction is done, we can apply the pending changes * before the next transaction. |
3818aea27
|
2048 |
*/ |
572d9ab78
|
2049 |
btrfs_apply_pending_changes(root->fs_info); |
3818aea27
|
2050 |
|
5d4f98a28
|
2051 |
/* commit_fs_roots gets rid of all the tree log roots, it is now |
e02119d5a
|
2052 2053 2054 |
* safe to free the root of tree log roots */ btrfs_free_log_root_tree(trans, root->fs_info); |
0ed4792af
|
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 |
/* * Since fs roots are all committed, we can get a quite accurate * new_roots. So let's do quota accounting. */ ret = btrfs_qgroup_account_extents(trans, root->fs_info); if (ret < 0) { mutex_unlock(&root->fs_info->tree_log_mutex); mutex_unlock(&root->fs_info->reloc_mutex); goto scrub_continue; } |
5d4f98a28
|
2065 |
ret = commit_cowonly_roots(trans, root); |
49b25e054
|
2066 2067 |
if (ret) { mutex_unlock(&root->fs_info->tree_log_mutex); |
871383be5
|
2068 |
mutex_unlock(&root->fs_info->reloc_mutex); |
6cf7f77e6
|
2069 |
goto scrub_continue; |
49b25e054
|
2070 |
} |
54aa1f4df
|
2071 |
|
2cba30f17
|
2072 2073 2074 2075 2076 2077 2078 2079 |
/* * The tasks which save the space cache and inode cache may also * update ->aborted, check it. */ if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { ret = cur_trans->aborted; mutex_unlock(&root->fs_info->tree_log_mutex); mutex_unlock(&root->fs_info->reloc_mutex); |
6cf7f77e6
|
2080 |
goto scrub_continue; |
2cba30f17
|
2081 |
} |
11833d66b
|
2082 |
btrfs_prepare_extent_commit(trans, root); |
78fae27eb
|
2083 |
cur_trans = root->fs_info->running_transaction; |
5d4f98a28
|
2084 2085 2086 |
btrfs_set_root_node(&root->fs_info->tree_root->root_item, root->fs_info->tree_root->node); |
9e351cc86
|
2087 2088 |
list_add_tail(&root->fs_info->tree_root->dirty_list, &cur_trans->switch_commits); |
5d4f98a28
|
2089 2090 2091 |
btrfs_set_root_node(&root->fs_info->chunk_root->root_item, root->fs_info->chunk_root->node); |
9e351cc86
|
2092 2093 2094 2095 |
list_add_tail(&root->fs_info->chunk_root->dirty_list, &cur_trans->switch_commits); switch_commit_roots(cur_trans, root->fs_info); |
5d4f98a28
|
2096 |
|
edf39272d
|
2097 |
assert_qgroups_uptodate(trans); |
ce93ec548
|
2098 |
ASSERT(list_empty(&cur_trans->dirty_bgs)); |
1bbc621ef
|
2099 |
ASSERT(list_empty(&cur_trans->io_bgs)); |
5d4f98a28
|
2100 |
update_super_roots(root); |
e02119d5a
|
2101 |
|
60e7cd3a4
|
2102 2103 |
btrfs_set_super_log_root(root->fs_info->super_copy, 0); btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); |
6c41761fc
|
2104 2105 |
memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, sizeof(*root->fs_info->super_copy)); |
ccd467d60
|
2106 |
|
935e5cc93
|
2107 |
btrfs_update_commit_device_size(root->fs_info); |
ce7213c70
|
2108 |
btrfs_update_commit_device_bytes_used(root, cur_trans); |
935e5cc93
|
2109 |
|
afcdd129e
|
2110 2111 |
clear_bit(BTRFS_FS_LOG1_ERR, &root->fs_info->flags); clear_bit(BTRFS_FS_LOG2_ERR, &root->fs_info->flags); |
656f30dba
|
2112 |
|
4fbcdf669
|
2113 |
btrfs_trans_release_chunk_metadata(trans); |
a4abeea41
|
2114 |
spin_lock(&root->fs_info->trans_lock); |
4a9d8bdee
|
2115 |
cur_trans->state = TRANS_STATE_UNBLOCKED; |
a4abeea41
|
2116 |
root->fs_info->running_transaction = NULL; |
a4abeea41
|
2117 |
spin_unlock(&root->fs_info->trans_lock); |
7585717f3
|
2118 |
mutex_unlock(&root->fs_info->reloc_mutex); |
b7ec40d78
|
2119 |
|
f92957493
|
2120 |
wake_up(&root->fs_info->transaction_wait); |
e6dcd2dc9
|
2121 |
|
79154b1b5
|
2122 |
ret = btrfs_write_and_wait_transaction(trans, root); |
49b25e054
|
2123 |
if (ret) { |
34d970070
|
2124 |
btrfs_handle_fs_error(root->fs_info, ret, |
087488109
|
2125 |
"Error while writing out transaction"); |
49b25e054
|
2126 |
mutex_unlock(&root->fs_info->tree_log_mutex); |
6cf7f77e6
|
2127 |
goto scrub_continue; |
49b25e054
|
2128 2129 2130 2131 2132 |
} ret = write_ctree_super(trans, root, 0); if (ret) { mutex_unlock(&root->fs_info->tree_log_mutex); |
6cf7f77e6
|
2133 |
goto scrub_continue; |
49b25e054
|
2134 |
} |
4313b3994
|
2135 |
|
e02119d5a
|
2136 2137 2138 2139 2140 |
/* * the super is written, we can safely allow the tree-loggers * to go about their business */ mutex_unlock(&root->fs_info->tree_log_mutex); |
11833d66b
|
2141 |
btrfs_finish_extent_commit(trans, root); |
4313b3994
|
2142 |
|
3204d33cd
|
2143 |
if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) |
13212b54d
|
2144 |
btrfs_clear_space_info_full(root->fs_info); |
15ee9bc7e
|
2145 |
root->fs_info->last_trans_committed = cur_trans->transid; |
4a9d8bdee
|
2146 2147 2148 2149 2150 |
/* * We needn't acquire the lock here because there is no other task * which can change it. */ cur_trans->state = TRANS_STATE_COMPLETED; |
2c90e5d65
|
2151 |
wake_up(&cur_trans->commit_wait); |
3de4586c5
|
2152 |
|
a4abeea41
|
2153 |
spin_lock(&root->fs_info->trans_lock); |
13c5a93e7
|
2154 |
list_del_init(&cur_trans->list); |
a4abeea41
|
2155 |
spin_unlock(&root->fs_info->trans_lock); |
724e2315d
|
2156 2157 |
btrfs_put_transaction(cur_trans); btrfs_put_transaction(cur_trans); |
58176a960
|
2158 |
|
0860adfdb
|
2159 |
if (trans->type & __TRANS_FREEZABLE) |
354aa0fb6
|
2160 |
sb_end_intwrite(root->fs_info->sb); |
b2b5ef5c8
|
2161 |
|
1abe9b8a1
|
2162 |
trace_btrfs_transaction_commit(root); |
a2de733c7
|
2163 |
btrfs_scrub_continue(root); |
9ed74f2db
|
2164 2165 |
if (current->journal_info == trans) current->journal_info = NULL; |
2c90e5d65
|
2166 |
kmem_cache_free(btrfs_trans_handle_cachep, trans); |
24bbcf044
|
2167 |
|
9e7cc91a6
|
2168 2169 2170 2171 |
/* * If fs has been frozen, we can not handle delayed iputs, otherwise * it'll result in deadlock about SB_FREEZE_FS. */ |
8a7330130
|
2172 |
if (current != root->fs_info->transaction_kthread && |
9e7cc91a6
|
2173 2174 |
current != root->fs_info->cleaner_kthread && !root->fs_info->fs_frozen) |
24bbcf044
|
2175 |
btrfs_run_delayed_iputs(root); |
79154b1b5
|
2176 |
return ret; |
49b25e054
|
2177 |
|
6cf7f77e6
|
2178 2179 |
scrub_continue: btrfs_scrub_continue(root); |
49b25e054
|
2180 |
cleanup_transaction: |
0e7211069
|
2181 |
btrfs_trans_release_metadata(trans, root); |
4fbcdf669
|
2182 |
btrfs_trans_release_chunk_metadata(trans); |
0e7211069
|
2183 |
trans->block_rsv = NULL; |
c2cf52eb7
|
2184 |
btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); |
49b25e054
|
2185 2186 |
if (current->journal_info == trans) current->journal_info = NULL; |
7b8b92af5
|
2187 |
cleanup_transaction(trans, root, ret); |
49b25e054
|
2188 2189 |
return ret; |
79154b1b5
|
2190 |
} |
d352ac681
|
2191 |
/* |
9d1a2a3ad
|
2192 2193 2194 2195 2196 2197 2198 2199 |
* return < 0 if error * 0 if there are no more dead_roots at the time of call * 1 there are more to be processed, call me again * * The return value indicates there are certainly more snapshots to delete, but * if there comes a new one during processing, it may return 0. We don't mind, * because btrfs_commit_super will poke cleaner thread and it will process it a * few seconds later. |
d352ac681
|
2200 |
*/ |
9d1a2a3ad
|
2201 |
int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) |
e9d0b13b5
|
2202 |
{ |
9d1a2a3ad
|
2203 |
int ret; |
5d4f98a28
|
2204 |
struct btrfs_fs_info *fs_info = root->fs_info; |
a4abeea41
|
2205 |
spin_lock(&fs_info->trans_lock); |
9d1a2a3ad
|
2206 2207 2208 2209 2210 2211 |
if (list_empty(&fs_info->dead_roots)) { spin_unlock(&fs_info->trans_lock); return 0; } root = list_first_entry(&fs_info->dead_roots, struct btrfs_root, root_list); |
cfad392b2
|
2212 |
list_del_init(&root->root_list); |
a4abeea41
|
2213 |
spin_unlock(&fs_info->trans_lock); |
e9d0b13b5
|
2214 |
|
ab8d0fc48
|
2215 |
btrfs_debug(fs_info, "cleaner removing %llu", root->objectid); |
76dda93c6
|
2216 |
|
9d1a2a3ad
|
2217 |
btrfs_kill_all_delayed_nodes(root); |
16cdcec73
|
2218 |
|
9d1a2a3ad
|
2219 2220 2221 2222 2223 |
if (btrfs_header_backref_rev(root->node) < BTRFS_MIXED_BACKREF_REV) ret = btrfs_drop_snapshot(root, NULL, 0, 0); else ret = btrfs_drop_snapshot(root, NULL, 1, 0); |
32471dc2b
|
2224 |
|
6596a9281
|
2225 |
return (ret < 0) ? 0 : 1; |
e9d0b13b5
|
2226 |
} |
572d9ab78
|
2227 2228 2229 2230 2231 |
void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) { unsigned long prev; unsigned long bit; |
6c9fe14f9
|
2232 |
prev = xchg(&fs_info->pending_changes, 0); |
572d9ab78
|
2233 2234 |
if (!prev) return; |
7e1876aca
|
2235 2236 2237 2238 2239 2240 2241 2242 2243 |
bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; if (prev & bit) btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); prev &= ~bit; bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; if (prev & bit) btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); prev &= ~bit; |
d51033d05
|
2244 2245 2246 2247 |
bit = 1 << BTRFS_PENDING_COMMIT; if (prev & bit) btrfs_debug(fs_info, "pending commit done"); prev &= ~bit; |
572d9ab78
|
2248 2249 2250 2251 |
if (prev) btrfs_warn(fs_info, "unknown pending changes left 0x%lx, ignoring", prev); } |