Branch data Line data Source code
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 : :
19 : : #include <linux/fs.h>
20 : : #include <linux/slab.h>
21 : : #include <linux/sched.h>
22 : : #include <linux/writeback.h>
23 : : #include <linux/pagemap.h>
24 : : #include <linux/blkdev.h>
25 : : #include <linux/uuid.h>
26 : : #include "ctree.h"
27 : : #include "disk-io.h"
28 : : #include "transaction.h"
29 : : #include "locking.h"
30 : : #include "tree-log.h"
31 : : #include "inode-map.h"
32 : : #include "volumes.h"
33 : : #include "dev-replace.h"
34 : :
35 : : #define BTRFS_ROOT_TRANS_TAG 0
36 : :
37 : : static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
38 : : [TRANS_STATE_RUNNING] = 0U,
39 : : [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE |
40 : : __TRANS_START),
41 : : [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE |
42 : : __TRANS_START |
43 : : __TRANS_ATTACH),
44 : : [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE |
45 : : __TRANS_START |
46 : : __TRANS_ATTACH |
47 : : __TRANS_JOIN),
48 : : [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE |
49 : : __TRANS_START |
50 : : __TRANS_ATTACH |
51 : : __TRANS_JOIN |
52 : : __TRANS_JOIN_NOLOCK),
53 : : [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE |
54 : : __TRANS_START |
55 : : __TRANS_ATTACH |
56 : : __TRANS_JOIN |
57 : : __TRANS_JOIN_NOLOCK),
58 : : };
59 : :
60 : 0 : void btrfs_put_transaction(struct btrfs_transaction *transaction)
61 : : {
62 [ # # ]: 0 : WARN_ON(atomic_read(&transaction->use_count) == 0);
63 [ # # ]: 0 : if (atomic_dec_and_test(&transaction->use_count)) {
64 [ # # ]: 0 : BUG_ON(!list_empty(&transaction->list));
65 [ # # ]: 0 : WARN_ON(transaction->delayed_refs.root.rb_node);
66 [ # # ]: 0 : while (!list_empty(&transaction->pending_chunks)) {
67 : : struct extent_map *em;
68 : :
69 : 0 : em = list_first_entry(&transaction->pending_chunks,
70 : : struct extent_map, list);
71 : 0 : list_del_init(&em->list);
72 : 0 : free_extent_map(em);
73 : : }
74 : 0 : kmem_cache_free(btrfs_transaction_cachep, transaction);
75 : : }
76 : 0 : }
77 : :
78 : 0 : static noinline void switch_commit_root(struct btrfs_root *root)
79 : : {
80 : 0 : free_extent_buffer(root->commit_root);
81 : 0 : root->commit_root = btrfs_root_node(root);
82 : 0 : }
83 : :
84 : : static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
85 : : unsigned int type)
86 : : {
87 [ # # ]: 0 : if (type & TRANS_EXTWRITERS)
88 : 0 : atomic_inc(&trans->num_extwriters);
89 : : }
90 : :
91 : : static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
92 : : unsigned int type)
93 : : {
94 [ # # # # ]: 0 : if (type & TRANS_EXTWRITERS)
95 : 0 : atomic_dec(&trans->num_extwriters);
96 : : }
97 : :
98 : : static inline void extwriter_counter_init(struct btrfs_transaction *trans,
99 : : unsigned int type)
100 : : {
101 : 0 : atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
102 : : }
103 : :
104 : : static inline int extwriter_counter_read(struct btrfs_transaction *trans)
105 : : {
106 : 0 : return atomic_read(&trans->num_extwriters);
107 : : }
108 : :
109 : : /*
110 : : * either allocate a new transaction or hop into the existing one
111 : : */
112 : 0 : static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
113 : : {
114 : : struct btrfs_transaction *cur_trans;
115 : 0 : struct btrfs_fs_info *fs_info = root->fs_info;
116 : :
117 : : spin_lock(&fs_info->trans_lock);
118 : : loop:
119 : : /* The file system has been taken offline. No new transactions. */
120 [ # # ]: 0 : if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
121 : : spin_unlock(&fs_info->trans_lock);
122 : : return -EROFS;
123 : : }
124 : :
125 : 0 : cur_trans = fs_info->running_transaction;
126 [ # # ]: 0 : if (cur_trans) {
127 [ # # ]: 0 : if (cur_trans->aborted) {
128 : : spin_unlock(&fs_info->trans_lock);
129 : 0 : return cur_trans->aborted;
130 : : }
131 [ # # ]: 0 : if (btrfs_blocked_trans_types[cur_trans->state] & type) {
132 : : spin_unlock(&fs_info->trans_lock);
133 : : return -EBUSY;
134 : : }
135 : 0 : atomic_inc(&cur_trans->use_count);
136 : 0 : atomic_inc(&cur_trans->num_writers);
137 : : extwriter_counter_inc(cur_trans, type);
138 : : spin_unlock(&fs_info->trans_lock);
139 : : return 0;
140 : : }
141 : : spin_unlock(&fs_info->trans_lock);
142 : :
143 : : /*
144 : : * If we are ATTACH, we just want to catch the current transaction,
145 : : * and commit it. If there is no transaction, just return ENOENT.
146 : : */
147 [ # # ]: 0 : if (type == TRANS_ATTACH)
148 : : return -ENOENT;
149 : :
150 : : /*
151 : : * JOIN_NOLOCK only happens during the transaction commit, so
152 : : * it is impossible that ->running_transaction is NULL
153 : : */
154 [ # # ]: 0 : BUG_ON(type == TRANS_JOIN_NOLOCK);
155 : :
156 : 0 : cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
157 [ # # ]: 0 : if (!cur_trans)
158 : : return -ENOMEM;
159 : :
160 : : spin_lock(&fs_info->trans_lock);
161 [ # # ]: 0 : if (fs_info->running_transaction) {
162 : : /*
163 : : * someone started a transaction after we unlocked. Make sure
164 : : * to redo the checks above
165 : : */
166 : 0 : kmem_cache_free(btrfs_transaction_cachep, cur_trans);
167 : : goto loop;
168 [ # # ]: 0 : } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
169 : : spin_unlock(&fs_info->trans_lock);
170 : 0 : kmem_cache_free(btrfs_transaction_cachep, cur_trans);
171 : : return -EROFS;
172 : : }
173 : :
174 : 0 : atomic_set(&cur_trans->num_writers, 1);
175 : : extwriter_counter_init(cur_trans, type);
176 : 0 : init_waitqueue_head(&cur_trans->writer_wait);
177 : 0 : init_waitqueue_head(&cur_trans->commit_wait);
178 : 0 : cur_trans->state = TRANS_STATE_RUNNING;
179 : : /*
180 : : * One for this trans handle, one so it will live on until we
181 : : * commit the transaction.
182 : : */
183 : 0 : atomic_set(&cur_trans->use_count, 2);
184 : 0 : cur_trans->start_time = get_seconds();
185 : :
186 : 0 : cur_trans->delayed_refs.root = RB_ROOT;
187 : 0 : cur_trans->delayed_refs.num_entries = 0;
188 : 0 : cur_trans->delayed_refs.num_heads_ready = 0;
189 : 0 : cur_trans->delayed_refs.num_heads = 0;
190 : 0 : cur_trans->delayed_refs.flushing = 0;
191 : 0 : cur_trans->delayed_refs.run_delayed_start = 0;
192 : :
193 : : /*
194 : : * although the tree mod log is per file system and not per transaction,
195 : : * the log must never go across transaction boundaries.
196 : : */
197 : 0 : smp_mb();
198 [ # # ]: 0 : if (!list_empty(&fs_info->tree_mod_seq_list))
199 : 0 : WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
200 : : "creating a fresh transaction\n");
201 [ # # ]: 0 : if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
202 : 0 : WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
203 : : "creating a fresh transaction\n");
204 : 0 : atomic64_set(&fs_info->tree_mod_seq, 0);
205 : :
206 : 0 : spin_lock_init(&cur_trans->delayed_refs.lock);
207 : 0 : atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
208 : 0 : atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
209 : 0 : init_waitqueue_head(&cur_trans->delayed_refs.wait);
210 : :
211 : 0 : INIT_LIST_HEAD(&cur_trans->pending_snapshots);
212 : 0 : INIT_LIST_HEAD(&cur_trans->ordered_operations);
213 : 0 : INIT_LIST_HEAD(&cur_trans->pending_chunks);
214 : 0 : list_add_tail(&cur_trans->list, &fs_info->trans_list);
215 : 0 : extent_io_tree_init(&cur_trans->dirty_pages,
216 : 0 : fs_info->btree_inode->i_mapping);
217 : 0 : fs_info->generation++;
218 : 0 : cur_trans->transid = fs_info->generation;
219 : 0 : fs_info->running_transaction = cur_trans;
220 : 0 : cur_trans->aborted = 0;
221 : : spin_unlock(&fs_info->trans_lock);
222 : :
223 : : return 0;
224 : : }
225 : :
226 : : /*
227 : : * this does all the record keeping required to make sure that a reference
228 : : * counted root is properly recorded in a given transaction. This is required
229 : : * to make sure the old root from before we joined the transaction is deleted
230 : : * when the transaction commits
231 : : */
232 : 0 : static int record_root_in_trans(struct btrfs_trans_handle *trans,
233 : : struct btrfs_root *root)
234 : : {
235 [ # # ][ # # ]: 0 : if (root->ref_cows && root->last_trans < trans->transid) {
236 [ # # ]: 0 : WARN_ON(root == root->fs_info->extent_root);
237 [ # # ]: 0 : WARN_ON(root->commit_root != root->node);
238 : :
239 : : /*
240 : : * see below for in_trans_setup usage rules
241 : : * we have the reloc mutex held now, so there
242 : : * is only one writer in this function
243 : : */
244 : 0 : root->in_trans_setup = 1;
245 : :
246 : : /* make sure readers find in_trans_setup before
247 : : * they find our root->last_trans update
248 : : */
249 : 0 : smp_wmb();
250 : :
251 : 0 : spin_lock(&root->fs_info->fs_roots_radix_lock);
252 [ # # ]: 0 : if (root->last_trans == trans->transid) {
253 : 0 : spin_unlock(&root->fs_info->fs_roots_radix_lock);
254 : 0 : return 0;
255 : : }
256 : 0 : radix_tree_tag_set(&root->fs_info->fs_roots_radix,
257 : 0 : (unsigned long)root->root_key.objectid,
258 : : BTRFS_ROOT_TRANS_TAG);
259 : 0 : spin_unlock(&root->fs_info->fs_roots_radix_lock);
260 : 0 : root->last_trans = trans->transid;
261 : :
262 : : /* this is pretty tricky. We don't want to
263 : : * take the relocation lock in btrfs_record_root_in_trans
264 : : * unless we're really doing the first setup for this root in
265 : : * this transaction.
266 : : *
267 : : * Normally we'd use root->last_trans as a flag to decide
268 : : * if we want to take the expensive mutex.
269 : : *
270 : : * But, we have to set root->last_trans before we
271 : : * init the relocation root, otherwise, we trip over warnings
272 : : * in ctree.c. The solution used here is to flag ourselves
273 : : * with root->in_trans_setup. When this is 1, we're still
274 : : * fixing up the reloc trees and everyone must wait.
275 : : *
276 : : * When this is zero, they can trust root->last_trans and fly
277 : : * through btrfs_record_root_in_trans without having to take the
278 : : * lock. smp_wmb() makes sure that all the writes above are
279 : : * done before we pop in the zero below
280 : : */
281 : 0 : btrfs_init_reloc_root(trans, root);
282 : 0 : smp_wmb();
283 : 0 : root->in_trans_setup = 0;
284 : : }
285 : : return 0;
286 : : }
287 : :
288 : :
289 : 0 : int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
290 : : struct btrfs_root *root)
291 : : {
292 [ # # ]: 0 : if (!root->ref_cows)
293 : : return 0;
294 : :
295 : : /*
296 : : * see record_root_in_trans for comments about in_trans_setup usage
297 : : * and barriers
298 : : */
299 : 0 : smp_rmb();
300 [ # # ][ # # ]: 0 : if (root->last_trans == trans->transid &&
301 : 0 : !root->in_trans_setup)
302 : : return 0;
303 : :
304 : 0 : mutex_lock(&root->fs_info->reloc_mutex);
305 : 0 : record_root_in_trans(trans, root);
306 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
307 : :
308 : 0 : return 0;
309 : : }
310 : :
311 : : static inline int is_transaction_blocked(struct btrfs_transaction *trans)
312 : : {
313 : 0 : return (trans->state >= TRANS_STATE_BLOCKED &&
314 [ # # ][ # # ]: 0 : trans->state < TRANS_STATE_UNBLOCKED &&
[ # # ][ # # ]
315 : 0 : !trans->aborted);
316 : : }
317 : :
318 : : /* wait for commit against the current transaction to become unblocked
319 : : * when this is done, it is safe to start a new transaction, but the current
320 : : * transaction might not be fully on disk.
321 : : */
322 : 0 : static void wait_current_trans(struct btrfs_root *root)
323 : : {
324 : 0 : struct btrfs_transaction *cur_trans;
325 : :
326 : 0 : spin_lock(&root->fs_info->trans_lock);
327 : 0 : cur_trans = root->fs_info->running_transaction;
328 [ # # ][ # # ]: 0 : if (cur_trans && is_transaction_blocked(cur_trans)) {
329 : 0 : atomic_inc(&cur_trans->use_count);
330 : 0 : spin_unlock(&root->fs_info->trans_lock);
331 : :
332 [ # # ][ # # ]: 0 : wait_event(root->fs_info->transaction_wait,
[ # # ][ # # ]
333 : : cur_trans->state >= TRANS_STATE_UNBLOCKED ||
334 : : cur_trans->aborted);
335 : 0 : btrfs_put_transaction(cur_trans);
336 : : } else {
337 : : spin_unlock(&root->fs_info->trans_lock);
338 : : }
339 : 0 : }
340 : :
341 : : static int may_wait_transaction(struct btrfs_root *root, int type)
342 : : {
343 [ # # ][ # # ]: 0 : if (root->fs_info->log_root_recovering)
344 : : return 0;
345 : :
346 [ # # ][ # # ]: 0 : if (type == TRANS_USERSPACE)
347 : : return 1;
348 : :
349 [ # # ][ # # ]: 0 : if (type == TRANS_START &&
[ # # ][ # # ]
350 : 0 : !atomic_read(&root->fs_info->open_ioctl_trans))
351 : : return 1;
352 : :
353 : : return 0;
354 : : }
355 : :
356 : : static inline bool need_reserve_reloc_root(struct btrfs_root *root)
357 : : {
358 [ # # ][ # # ]: 0 : if (!root->fs_info->reloc_ctl ||
359 [ # # ]: 0 : !root->ref_cows ||
360 [ # # ]: 0 : root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
361 : 0 : root->reloc_root)
362 : : return false;
363 : :
364 : : return true;
365 : : }
366 : :
367 : : static struct btrfs_trans_handle *
368 : 0 : start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
369 : : enum btrfs_reserve_flush_enum flush)
370 : : {
371 : : struct btrfs_trans_handle *h;
372 : : struct btrfs_transaction *cur_trans;
373 : : u64 num_bytes = 0;
374 : : u64 qgroup_reserved = 0;
375 : : bool reloc_reserved = false;
376 : : int ret;
377 : :
378 [ # # ]: 0 : if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
379 : : return ERR_PTR(-EROFS);
380 : :
381 [ # # ]: 0 : if (current->journal_info) {
382 [ # # ]: 0 : WARN_ON(type & TRANS_EXTWRITERS);
383 : 0 : h = current->journal_info;
384 : 0 : h->use_count++;
385 [ # # ]: 0 : WARN_ON(h->use_count > 2);
386 : 0 : h->orig_rsv = h->block_rsv;
387 : 0 : h->block_rsv = NULL;
388 : 0 : goto got_it;
389 : : }
390 : :
391 : : /*
392 : : * Do the reservation before we join the transaction so we can do all
393 : : * the appropriate flushing if need be.
394 : : */
395 [ # # ][ # # ]: 0 : if (num_items > 0 && root != root->fs_info->chunk_root) {
396 [ # # ][ # # ]: 0 : if (root->fs_info->quota_enabled &&
397 : 0 : is_fstree(root->root_key.objectid)) {
398 : 0 : qgroup_reserved = num_items * root->leafsize;
399 : 0 : ret = btrfs_qgroup_reserve(root, qgroup_reserved);
400 [ # # ]: 0 : if (ret)
401 : 0 : return ERR_PTR(ret);
402 : : }
403 : :
404 : 0 : num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
405 : : /*
406 : : * Do the reservation for the relocation root creation
407 : : */
408 [ # # ]: 0 : if (unlikely(need_reserve_reloc_root(root))) {
409 : 0 : num_bytes += root->nodesize;
410 : : reloc_reserved = true;
411 : : }
412 : :
413 : 0 : ret = btrfs_block_rsv_add(root,
414 : : &root->fs_info->trans_block_rsv,
415 : : num_bytes, flush);
416 [ # # ]: 0 : if (ret)
417 : : goto reserve_fail;
418 : : }
419 : : again:
420 : 0 : h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
421 [ # # ]: 0 : if (!h) {
422 : : ret = -ENOMEM;
423 : : goto alloc_fail;
424 : : }
425 : :
426 : : /*
427 : : * If we are JOIN_NOLOCK we're already committing a transaction and
428 : : * waiting on this guy, so we don't need to do the sb_start_intwrite
429 : : * because we're already holding a ref. We need this because we could
430 : : * have raced in and did an fsync() on a file which can kick a commit
431 : : * and then we deadlock with somebody doing a freeze.
432 : : *
433 : : * If we are ATTACH, it means we just want to catch the current
434 : : * transaction and commit it, so we needn't do sb_start_intwrite().
435 : : */
436 [ # # ]: 0 : if (type & __TRANS_FREEZABLE)
437 : 0 : sb_start_intwrite(root->fs_info->sb);
438 : :
439 [ # # ]: 0 : if (may_wait_transaction(root, type))
440 : 0 : wait_current_trans(root);
441 : :
442 : : do {
443 : 0 : ret = join_transaction(root, type);
444 [ # # ]: 0 : if (ret == -EBUSY) {
445 : 0 : wait_current_trans(root);
446 [ # # ]: 0 : if (unlikely(type == TRANS_ATTACH))
447 : : ret = -ENOENT;
448 : : }
449 [ # # ]: 0 : } while (ret == -EBUSY);
450 : :
451 [ # # ]: 0 : if (ret < 0) {
452 : : /* We must get the transaction if we are JOIN_NOLOCK. */
453 [ # # ]: 0 : BUG_ON(type == TRANS_JOIN_NOLOCK);
454 : : goto join_fail;
455 : : }
456 : :
457 : 0 : cur_trans = root->fs_info->running_transaction;
458 : :
459 : 0 : h->transid = cur_trans->transid;
460 : 0 : h->transaction = cur_trans;
461 : 0 : h->blocks_used = 0;
462 : 0 : h->bytes_reserved = 0;
463 : 0 : h->root = root;
464 : 0 : h->delayed_ref_updates = 0;
465 : 0 : h->use_count = 1;
466 : 0 : h->adding_csums = 0;
467 : 0 : h->block_rsv = NULL;
468 : 0 : h->orig_rsv = NULL;
469 : 0 : h->aborted = 0;
470 : 0 : h->qgroup_reserved = 0;
471 : 0 : h->delayed_ref_elem.seq = 0;
472 : 0 : h->type = type;
473 : 0 : h->allocating_chunk = false;
474 : 0 : h->reloc_reserved = false;
475 : 0 : INIT_LIST_HEAD(&h->qgroup_ref_list);
476 : 0 : INIT_LIST_HEAD(&h->new_bgs);
477 : :
478 : 0 : smp_mb();
479 [ # # ][ # # ]: 0 : if (cur_trans->state >= TRANS_STATE_BLOCKED &&
480 : : may_wait_transaction(root, type)) {
481 : 0 : btrfs_commit_transaction(h, root);
482 : 0 : goto again;
483 : : }
484 : :
485 [ # # ]: 0 : if (num_bytes) {
486 : 0 : trace_btrfs_space_reservation(root->fs_info, "transaction",
487 : : h->transid, num_bytes, 1);
488 : 0 : h->block_rsv = &root->fs_info->trans_block_rsv;
489 : 0 : h->bytes_reserved = num_bytes;
490 : 0 : h->reloc_reserved = reloc_reserved;
491 : : }
492 : 0 : h->qgroup_reserved = qgroup_reserved;
493 : :
494 : : got_it:
495 : 0 : btrfs_record_root_in_trans(h, root);
496 : :
497 [ # # ][ # # ]: 0 : if (!current->journal_info && type != TRANS_USERSPACE)
498 : 0 : current->journal_info = h;
499 : 0 : return h;
500 : :
501 : : join_fail:
502 [ # # ]: 0 : if (type & __TRANS_FREEZABLE)
503 : 0 : sb_end_intwrite(root->fs_info->sb);
504 : 0 : kmem_cache_free(btrfs_trans_handle_cachep, h);
505 : : alloc_fail:
506 [ # # ]: 0 : if (num_bytes)
507 : 0 : btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
508 : : num_bytes);
509 : : reserve_fail:
510 [ # # ]: 0 : if (qgroup_reserved)
511 : 0 : btrfs_qgroup_free(root, qgroup_reserved);
512 : 0 : return ERR_PTR(ret);
513 : : }
514 : :
515 : 0 : struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
516 : : int num_items)
517 : : {
518 : 0 : return start_transaction(root, num_items, TRANS_START,
519 : : BTRFS_RESERVE_FLUSH_ALL);
520 : : }
521 : :
522 : 0 : struct btrfs_trans_handle *btrfs_start_transaction_lflush(
523 : : struct btrfs_root *root, int num_items)
524 : : {
525 : 0 : return start_transaction(root, num_items, TRANS_START,
526 : : BTRFS_RESERVE_FLUSH_LIMIT);
527 : : }
528 : :
529 : 0 : struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
530 : : {
531 : 0 : return start_transaction(root, 0, TRANS_JOIN, 0);
532 : : }
533 : :
534 : 0 : struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
535 : : {
536 : 0 : return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
537 : : }
538 : :
539 : 0 : struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
540 : : {
541 : 0 : return start_transaction(root, 0, TRANS_USERSPACE, 0);
542 : : }
543 : :
544 : : /*
545 : : * btrfs_attach_transaction() - catch the running transaction
546 : : *
547 : : * It is used when we want to commit the current the transaction, but
548 : : * don't want to start a new one.
549 : : *
550 : : * Note: If this function return -ENOENT, it just means there is no
551 : : * running transaction. But it is possible that the inactive transaction
552 : : * is still in the memory, not fully on disk. If you hope there is no
553 : : * inactive transaction in the fs when -ENOENT is returned, you should
554 : : * invoke
555 : : * btrfs_attach_transaction_barrier()
556 : : */
557 : 0 : struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
558 : : {
559 : 0 : return start_transaction(root, 0, TRANS_ATTACH, 0);
560 : : }
561 : :
562 : : /*
563 : : * btrfs_attach_transaction_barrier() - catch the running transaction
564 : : *
565 : : * It is similar to the above function, the differentia is this one
566 : : * will wait for all the inactive transactions until they fully
567 : : * complete.
568 : : */
569 : : struct btrfs_trans_handle *
570 : 0 : btrfs_attach_transaction_barrier(struct btrfs_root *root)
571 : : {
572 : : struct btrfs_trans_handle *trans;
573 : :
574 : 0 : trans = start_transaction(root, 0, TRANS_ATTACH, 0);
575 [ # # ][ # # ]: 0 : if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
576 : 0 : btrfs_wait_for_commit(root, 0);
577 : :
578 : 0 : return trans;
579 : : }
580 : :
581 : : /* wait for a transaction commit to be fully complete */
582 : 0 : static noinline void wait_for_commit(struct btrfs_root *root,
583 : : struct btrfs_transaction *commit)
584 : : {
585 [ # # ][ # # ]: 0 : wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
586 : 0 : }
587 : :
588 : 0 : int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
589 : : {
590 : : struct btrfs_transaction *cur_trans = NULL, *t;
591 : : int ret = 0;
592 : :
593 [ # # ]: 0 : if (transid) {
594 [ # # ]: 0 : if (transid <= root->fs_info->last_trans_committed)
595 : : goto out;
596 : :
597 : : ret = -EINVAL;
598 : : /* find specified transaction */
599 : : spin_lock(&root->fs_info->trans_lock);
600 [ # # ]: 0 : list_for_each_entry(t, &root->fs_info->trans_list, list) {
601 [ # # ]: 0 : if (t->transid == transid) {
602 : : cur_trans = t;
603 : 0 : atomic_inc(&cur_trans->use_count);
604 : : ret = 0;
605 : 0 : break;
606 : : }
607 [ # # ]: 0 : if (t->transid > transid) {
608 : : ret = 0;
609 : : break;
610 : : }
611 : : }
612 : 0 : spin_unlock(&root->fs_info->trans_lock);
613 : : /* The specified transaction doesn't exist */
614 [ # # ]: 0 : if (!cur_trans)
615 : : goto out;
616 : : } else {
617 : : /* find newest transaction that is committing | committed */
618 : 0 : spin_lock(&root->fs_info->trans_lock);
619 [ # # ]: 0 : list_for_each_entry_reverse(t, &root->fs_info->trans_list,
620 : : list) {
621 [ # # ]: 0 : if (t->state >= TRANS_STATE_COMMIT_START) {
622 [ # # ]: 0 : if (t->state == TRANS_STATE_COMPLETED)
623 : : break;
624 : : cur_trans = t;
625 : 0 : atomic_inc(&cur_trans->use_count);
626 : : break;
627 : : }
628 : : }
629 : 0 : spin_unlock(&root->fs_info->trans_lock);
630 [ # # ]: 0 : if (!cur_trans)
631 : : goto out; /* nothing committing|committed */
632 : : }
633 : :
634 : 0 : wait_for_commit(root, cur_trans);
635 : 0 : btrfs_put_transaction(cur_trans);
636 : : out:
637 : 0 : return ret;
638 : : }
639 : :
640 : 0 : void btrfs_throttle(struct btrfs_root *root)
641 : : {
642 [ # # ]: 0 : if (!atomic_read(&root->fs_info->open_ioctl_trans))
643 : 0 : wait_current_trans(root);
644 : 0 : }
645 : :
646 : 0 : static int should_end_transaction(struct btrfs_trans_handle *trans,
647 : : struct btrfs_root *root)
648 : : {
649 [ # # # # ]: 0 : if (root->fs_info->global_block_rsv.space_info->full &&
650 : 0 : btrfs_should_throttle_delayed_refs(trans, root))
651 : : return 1;
652 : :
653 : 0 : return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
654 : : }
655 : :
656 : 0 : int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
657 : : struct btrfs_root *root)
658 : : {
659 : 0 : struct btrfs_transaction *cur_trans = trans->transaction;
660 : : int updates;
661 : : int err;
662 : :
663 : 0 : smp_mb();
664 [ # # ][ # # ]: 0 : if (cur_trans->state >= TRANS_STATE_BLOCKED ||
665 : 0 : cur_trans->delayed_refs.flushing)
666 : : return 1;
667 : :
668 : 0 : updates = trans->delayed_ref_updates;
669 : 0 : trans->delayed_ref_updates = 0;
670 [ # # ]: 0 : if (updates) {
671 : 0 : err = btrfs_run_delayed_refs(trans, root, updates);
672 [ # # ]: 0 : if (err) /* Error code will also eval true */
673 : : return err;
674 : : }
675 : :
676 : 0 : return should_end_transaction(trans, root);
677 : : }
678 : :
679 : 0 : static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
680 : : struct btrfs_root *root, int throttle)
681 : : {
682 : 0 : struct btrfs_transaction *cur_trans = trans->transaction;
683 : 0 : struct btrfs_fs_info *info = root->fs_info;
684 : 0 : unsigned long cur = trans->delayed_ref_updates;
685 : 0 : int lock = (trans->type != TRANS_JOIN_NOLOCK);
686 : : int err = 0;
687 : :
688 [ # # ]: 0 : if (--trans->use_count) {
689 : 0 : trans->block_rsv = trans->orig_rsv;
690 : 0 : return 0;
691 : : }
692 : :
693 : : /*
694 : : * do the qgroup accounting as early as possible
695 : : */
696 : 0 : err = btrfs_delayed_refs_qgroup_accounting(trans, info);
697 : :
698 : 0 : btrfs_trans_release_metadata(trans, root);
699 : 0 : trans->block_rsv = NULL;
700 : :
701 [ # # ]: 0 : if (trans->qgroup_reserved) {
702 : : /*
703 : : * the same root has to be passed here between start_transaction
704 : : * and end_transaction. Subvolume quota depends on this.
705 : : */
706 : 0 : btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
707 : 0 : trans->qgroup_reserved = 0;
708 : : }
709 : :
710 [ # # ]: 0 : if (!list_empty(&trans->new_bgs))
711 : 0 : btrfs_create_pending_block_groups(trans, root);
712 : :
713 : 0 : trans->delayed_ref_updates = 0;
714 [ # # ]: 0 : if (btrfs_should_throttle_delayed_refs(trans, root)) {
715 : 0 : cur = max_t(unsigned long, cur, 1);
716 : 0 : trans->delayed_ref_updates = 0;
717 : 0 : btrfs_run_delayed_refs(trans, root, cur);
718 : : }
719 : :
720 : 0 : btrfs_trans_release_metadata(trans, root);
721 : 0 : trans->block_rsv = NULL;
722 : :
723 [ # # ]: 0 : if (!list_empty(&trans->new_bgs))
724 : 0 : btrfs_create_pending_block_groups(trans, root);
725 : :
726 [ # # ]: 0 : if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
[ # # # # ]
727 [ # # ]: 0 : should_end_transaction(trans, root) &&
728 : 0 : ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
729 : : spin_lock(&info->trans_lock);
730 [ # # ]: 0 : if (cur_trans->state == TRANS_STATE_RUNNING)
731 : 0 : cur_trans->state = TRANS_STATE_BLOCKED;
732 : : spin_unlock(&info->trans_lock);
733 : : }
734 : :
735 [ # # ][ # # ]: 0 : if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
736 [ # # ]: 0 : if (throttle) {
737 : : /*
738 : : * We may race with somebody else here so end up having
739 : : * to call end_transaction on ourselves again, so inc
740 : : * our use_count.
741 : : */
742 : 0 : trans->use_count++;
743 : 0 : return btrfs_commit_transaction(trans, root);
744 : : } else {
745 : 0 : wake_up_process(info->transaction_kthread);
746 : : }
747 : : }
748 : :
749 [ # # ]: 0 : if (trans->type & __TRANS_FREEZABLE)
750 : 0 : sb_end_intwrite(root->fs_info->sb);
751 : :
752 [ # # ]: 0 : WARN_ON(cur_trans != info->running_transaction);
753 [ # # ]: 0 : WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
754 : 0 : atomic_dec(&cur_trans->num_writers);
755 : 0 : extwriter_counter_dec(cur_trans, trans->type);
756 : :
757 : 0 : smp_mb();
758 [ # # ]: 0 : if (waitqueue_active(&cur_trans->writer_wait))
759 : 0 : wake_up(&cur_trans->writer_wait);
760 : 0 : btrfs_put_transaction(cur_trans);
761 : :
762 [ # # ]: 0 : if (current->journal_info == trans)
763 : 0 : current->journal_info = NULL;
764 : :
765 [ # # ]: 0 : if (throttle)
766 : 0 : btrfs_run_delayed_iputs(root);
767 : :
768 [ # # ][ # # ]: 0 : if (trans->aborted ||
769 : 0 : test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
770 : 0 : wake_up_process(info->transaction_kthread);
771 : : err = -EIO;
772 : : }
773 : 0 : assert_qgroups_uptodate(trans);
774 : :
775 : 0 : kmem_cache_free(btrfs_trans_handle_cachep, trans);
776 : 0 : return err;
777 : : }
778 : :
779 : 0 : int btrfs_end_transaction(struct btrfs_trans_handle *trans,
780 : : struct btrfs_root *root)
781 : : {
782 : 0 : return __btrfs_end_transaction(trans, root, 0);
783 : : }
784 : :
785 : 0 : int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
786 : : struct btrfs_root *root)
787 : : {
788 : 0 : return __btrfs_end_transaction(trans, root, 1);
789 : : }
790 : :
791 : 0 : int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
792 : : struct btrfs_root *root)
793 : : {
794 : 0 : return __btrfs_end_transaction(trans, root, 1);
795 : : }
796 : :
797 : : /*
798 : : * when btree blocks are allocated, they have some corresponding bits set for
799 : : * them in one of two extent_io trees. This is used to make sure all of
800 : : * those extents are sent to disk but does not wait on them
801 : : */
802 : 0 : int btrfs_write_marked_extents(struct btrfs_root *root,
803 : : struct extent_io_tree *dirty_pages, int mark)
804 : : {
805 : : int err = 0;
806 : : int werr = 0;
807 : 0 : struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
808 : 0 : struct extent_state *cached_state = NULL;
809 : 0 : u64 start = 0;
810 : : u64 end;
811 : :
812 [ # # ]: 0 : while (!find_first_extent_bit(dirty_pages, start, &start, &end,
813 : : mark, &cached_state)) {
814 : 0 : convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
815 : : mark, &cached_state, GFP_NOFS);
816 : 0 : cached_state = NULL;
817 : 0 : err = filemap_fdatawrite_range(mapping, start, end);
818 [ # # ]: 0 : if (err)
819 : : werr = err;
820 : 0 : cond_resched();
821 : 0 : start = end + 1;
822 : : }
823 [ # # ]: 0 : if (err)
824 : : werr = err;
825 : 0 : return werr;
826 : : }
827 : :
828 : : /*
829 : : * when btree blocks are allocated, they have some corresponding bits set for
830 : : * them in one of two extent_io trees. This is used to make sure all of
831 : : * those extents are on disk for transaction or log commit. We wait
832 : : * on all the pages and clear them from the dirty pages state tree
833 : : */
834 : 0 : int btrfs_wait_marked_extents(struct btrfs_root *root,
835 : : struct extent_io_tree *dirty_pages, int mark)
836 : : {
837 : : int err = 0;
838 : : int werr = 0;
839 : 0 : struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
840 : 0 : struct extent_state *cached_state = NULL;
841 : 0 : u64 start = 0;
842 : : u64 end;
843 : :
844 [ # # ]: 0 : while (!find_first_extent_bit(dirty_pages, start, &start, &end,
845 : : EXTENT_NEED_WAIT, &cached_state)) {
846 : 0 : clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
847 : : 0, 0, &cached_state, GFP_NOFS);
848 : 0 : err = filemap_fdatawait_range(mapping, start, end);
849 [ # # ]: 0 : if (err)
850 : : werr = err;
851 : 0 : cond_resched();
852 : 0 : start = end + 1;
853 : : }
854 [ # # ]: 0 : if (err)
855 : : werr = err;
856 : 0 : return werr;
857 : : }
858 : :
859 : : /*
860 : : * when btree blocks are allocated, they have some corresponding bits set for
861 : : * them in one of two extent_io trees. This is used to make sure all of
862 : : * those extents are on disk for transaction or log commit
863 : : */
864 : 0 : static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
865 : : struct extent_io_tree *dirty_pages, int mark)
866 : : {
867 : : int ret;
868 : : int ret2;
869 : : struct blk_plug plug;
870 : :
871 : 0 : blk_start_plug(&plug);
872 : 0 : ret = btrfs_write_marked_extents(root, dirty_pages, mark);
873 : 0 : blk_finish_plug(&plug);
874 : 0 : ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
875 : :
876 [ # # ]: 0 : if (ret)
877 : : return ret;
878 [ # # ]: 0 : if (ret2)
879 : 0 : return ret2;
880 : : return 0;
881 : : }
882 : :
883 : 0 : int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
884 : : struct btrfs_root *root)
885 : : {
886 [ # # ][ # # ]: 0 : if (!trans || !trans->transaction) {
887 : : struct inode *btree_inode;
888 : 0 : btree_inode = root->fs_info->btree_inode;
889 : 0 : return filemap_write_and_wait(btree_inode->i_mapping);
890 : : }
891 : 0 : return btrfs_write_and_wait_marked_extents(root,
892 : : &trans->transaction->dirty_pages,
893 : : EXTENT_DIRTY);
894 : : }
895 : :
896 : : /*
897 : : * this is used to update the root pointer in the tree of tree roots.
898 : : *
899 : : * But, in the case of the extent allocation tree, updating the root
900 : : * pointer may allocate blocks which may change the root of the extent
901 : : * allocation tree.
902 : : *
903 : : * So, this loops and repeats and makes sure the cowonly root didn't
904 : : * change while the root pointer was being updated in the metadata.
905 : : */
906 : 0 : static int update_cowonly_root(struct btrfs_trans_handle *trans,
907 : : struct btrfs_root *root)
908 : : {
909 : : int ret;
910 : : u64 old_root_bytenr;
911 : : u64 old_root_used;
912 : 0 : struct btrfs_root *tree_root = root->fs_info->tree_root;
913 : :
914 : : old_root_used = btrfs_root_used(&root->root_item);
915 : 0 : btrfs_write_dirty_block_groups(trans, root);
916 : :
917 : : while (1) {
918 : : old_root_bytenr = btrfs_root_bytenr(&root->root_item);
919 [ # # ][ # # ]: 0 : if (old_root_bytenr == root->node->start &&
920 : : old_root_used == btrfs_root_used(&root->root_item))
921 : : break;
922 : :
923 : 0 : btrfs_set_root_node(&root->root_item, root->node);
924 : 0 : ret = btrfs_update_root(trans, tree_root,
925 : : &root->root_key,
926 : : &root->root_item);
927 [ # # ]: 0 : if (ret)
928 : : return ret;
929 : :
930 : : old_root_used = btrfs_root_used(&root->root_item);
931 : 0 : ret = btrfs_write_dirty_block_groups(trans, root);
932 [ # # ]: 0 : if (ret)
933 : : return ret;
934 : : }
935 : :
936 [ # # ]: 0 : if (root != root->fs_info->extent_root)
937 : 0 : switch_commit_root(root);
938 : :
939 : : return 0;
940 : : }
941 : :
942 : : /*
943 : : * update all the cowonly tree roots on disk
944 : : *
945 : : * The error handling in this function may not be obvious. Any of the
946 : : * failures will cause the file system to go offline. We still need
947 : : * to clean up the delayed refs.
948 : : */
949 : 0 : static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
950 : : struct btrfs_root *root)
951 : : {
952 : 0 : struct btrfs_fs_info *fs_info = root->fs_info;
953 : : struct list_head *next;
954 : : struct extent_buffer *eb;
955 : : int ret;
956 : :
957 : 0 : ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
958 [ # # ]: 0 : if (ret)
959 : : return ret;
960 : :
961 : 0 : eb = btrfs_lock_root_node(fs_info->tree_root);
962 : 0 : ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
963 : : 0, &eb);
964 : 0 : btrfs_tree_unlock(eb);
965 : 0 : free_extent_buffer(eb);
966 : :
967 [ # # ]: 0 : if (ret)
968 : : return ret;
969 : :
970 : 0 : ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
971 [ # # ]: 0 : if (ret)
972 : : return ret;
973 : :
974 : 0 : ret = btrfs_run_dev_stats(trans, root->fs_info);
975 [ # # ]: 0 : if (ret)
976 : : return ret;
977 : 0 : ret = btrfs_run_dev_replace(trans, root->fs_info);
978 [ # # ]: 0 : if (ret)
979 : : return ret;
980 : 0 : ret = btrfs_run_qgroups(trans, root->fs_info);
981 [ # # ]: 0 : if (ret)
982 : : return ret;
983 : :
984 : : /* run_qgroups might have added some more refs */
985 : 0 : ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
986 [ # # ]: 0 : if (ret)
987 : : return ret;
988 : :
989 [ # # ]: 0 : while (!list_empty(&fs_info->dirty_cowonly_roots)) {
990 : : next = fs_info->dirty_cowonly_roots.next;
991 : : list_del_init(next);
992 : 0 : root = list_entry(next, struct btrfs_root, dirty_list);
993 : :
994 : 0 : ret = update_cowonly_root(trans, root);
995 [ # # ]: 0 : if (ret)
996 : : return ret;
997 : : }
998 : :
999 : 0 : down_write(&fs_info->extent_commit_sem);
1000 : 0 : switch_commit_root(fs_info->extent_root);
1001 : 0 : up_write(&fs_info->extent_commit_sem);
1002 : :
1003 : 0 : btrfs_after_dev_replace_commit(fs_info);
1004 : :
1005 : 0 : return 0;
1006 : : }
1007 : :
1008 : : /*
1009 : : * dead roots are old snapshots that need to be deleted. This allocates
1010 : : * a dirty root struct and adds it into the list of dead roots that need to
1011 : : * be deleted
1012 : : */
1013 : 0 : void btrfs_add_dead_root(struct btrfs_root *root)
1014 : : {
1015 : 0 : spin_lock(&root->fs_info->trans_lock);
1016 [ # # ]: 0 : if (list_empty(&root->root_list))
1017 : 0 : list_add_tail(&root->root_list, &root->fs_info->dead_roots);
1018 : 0 : spin_unlock(&root->fs_info->trans_lock);
1019 : 0 : }
1020 : :
1021 : : /*
1022 : : * update all the cowonly tree roots on disk
1023 : : */
1024 : 0 : static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
1025 : : struct btrfs_root *root)
1026 : : {
1027 : : struct btrfs_root *gang[8];
1028 : 0 : struct btrfs_fs_info *fs_info = root->fs_info;
1029 : : int i;
1030 : : int ret;
1031 : : int err = 0;
1032 : :
1033 : : spin_lock(&fs_info->fs_roots_radix_lock);
1034 : : while (1) {
1035 : 0 : ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
1036 : : (void **)gang, 0,
1037 : : ARRAY_SIZE(gang),
1038 : : BTRFS_ROOT_TRANS_TAG);
1039 [ # # ]: 0 : if (ret == 0)
1040 : : break;
1041 [ # # ]: 0 : for (i = 0; i < ret; i++) {
1042 : 0 : root = gang[i];
1043 : 0 : radix_tree_tag_clear(&fs_info->fs_roots_radix,
1044 : 0 : (unsigned long)root->root_key.objectid,
1045 : : BTRFS_ROOT_TRANS_TAG);
1046 : : spin_unlock(&fs_info->fs_roots_radix_lock);
1047 : :
1048 : 0 : btrfs_free_log(trans, root);
1049 : 0 : btrfs_update_reloc_root(trans, root);
1050 : 0 : btrfs_orphan_commit_root(trans, root);
1051 : :
1052 : 0 : btrfs_save_ino_cache(root, trans);
1053 : :
1054 : : /* see comments in should_cow_block() */
1055 : 0 : root->force_cow = 0;
1056 : 0 : smp_wmb();
1057 : :
1058 [ # # ]: 0 : if (root->commit_root != root->node) {
1059 : 0 : mutex_lock(&root->fs_commit_mutex);
1060 : 0 : switch_commit_root(root);
1061 : 0 : btrfs_unpin_free_ino(root);
1062 : 0 : mutex_unlock(&root->fs_commit_mutex);
1063 : :
1064 : 0 : btrfs_set_root_node(&root->root_item,
1065 : : root->node);
1066 : : }
1067 : :
1068 : 0 : err = btrfs_update_root(trans, fs_info->tree_root,
1069 : : &root->root_key,
1070 : : &root->root_item);
1071 : : spin_lock(&fs_info->fs_roots_radix_lock);
1072 [ # # ]: 0 : if (err)
1073 : : break;
1074 : : }
1075 : : }
1076 : : spin_unlock(&fs_info->fs_roots_radix_lock);
1077 : 0 : return err;
1078 : : }
1079 : :
1080 : : /*
1081 : : * defrag a given btree.
1082 : : * Every leaf in the btree is read and defragged.
1083 : : */
1084 : 0 : int btrfs_defrag_root(struct btrfs_root *root)
1085 : : {
1086 : 0 : struct btrfs_fs_info *info = root->fs_info;
1087 : : struct btrfs_trans_handle *trans;
1088 : : int ret;
1089 : :
1090 [ # # ]: 0 : if (xchg(&root->defrag_running, 1))
1091 : : return 0;
1092 : :
1093 : : while (1) {
1094 : : trans = btrfs_start_transaction(root, 0);
1095 [ # # ]: 0 : if (IS_ERR(trans))
1096 : 0 : return PTR_ERR(trans);
1097 : :
1098 : 0 : ret = btrfs_defrag_leaves(trans, root);
1099 : :
1100 : : btrfs_end_transaction(trans, root);
1101 : 0 : btrfs_btree_balance_dirty(info->tree_root);
1102 : 0 : cond_resched();
1103 : :
1104 [ # # ][ # # ]: 0 : if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1105 : : break;
1106 : :
1107 [ # # ]: 0 : if (btrfs_defrag_cancelled(root->fs_info)) {
1108 : 0 : printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1109 : : ret = -EAGAIN;
1110 : 0 : break;
1111 : : }
1112 : : }
1113 : 0 : root->defrag_running = 0;
1114 : 0 : return ret;
1115 : : }
1116 : :
1117 : : /*
1118 : : * new snapshots need to be created at a very specific time in the
1119 : : * transaction commit. This does the actual creation.
1120 : : *
1121 : : * Note:
1122 : : * If the error which may affect the commitment of the current transaction
1123 : : * happens, we should return the error number. If the error which just affect
1124 : : * the creation of the pending snapshots, just return 0.
1125 : : */
1126 : 0 : static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1127 : : struct btrfs_fs_info *fs_info,
1128 : : struct btrfs_pending_snapshot *pending)
1129 : : {
1130 : : struct btrfs_key key;
1131 : : struct btrfs_root_item *new_root_item;
1132 : 0 : struct btrfs_root *tree_root = fs_info->tree_root;
1133 : 0 : struct btrfs_root *root = pending->root;
1134 : : struct btrfs_root *parent_root;
1135 : : struct btrfs_block_rsv *rsv;
1136 : : struct inode *parent_inode;
1137 : : struct btrfs_path *path;
1138 : : struct btrfs_dir_item *dir_item;
1139 : : struct dentry *dentry;
1140 : : struct extent_buffer *tmp;
1141 : : struct extent_buffer *old;
1142 : 0 : struct timespec cur_time = CURRENT_TIME;
1143 : : int ret = 0;
1144 : 0 : u64 to_reserve = 0;
1145 : 0 : u64 index = 0;
1146 : : u64 objectid;
1147 : : u64 root_flags;
1148 : : uuid_le new_uuid;
1149 : :
1150 : 0 : path = btrfs_alloc_path();
1151 [ # # ]: 0 : if (!path) {
1152 : 0 : pending->error = -ENOMEM;
1153 : 0 : return 0;
1154 : : }
1155 : :
1156 : : new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
1157 [ # # ]: 0 : if (!new_root_item) {
1158 : 0 : pending->error = -ENOMEM;
1159 : 0 : goto root_item_alloc_fail;
1160 : : }
1161 : :
1162 : 0 : pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1163 [ # # ]: 0 : if (pending->error)
1164 : : goto no_free_objectid;
1165 : :
1166 : 0 : btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1167 : :
1168 [ # # ]: 0 : if (to_reserve > 0) {
1169 : 0 : pending->error = btrfs_block_rsv_add(root,
1170 : : &pending->block_rsv,
1171 : : to_reserve,
1172 : : BTRFS_RESERVE_NO_FLUSH);
1173 [ # # ]: 0 : if (pending->error)
1174 : : goto no_free_objectid;
1175 : : }
1176 : :
1177 : 0 : pending->error = btrfs_qgroup_inherit(trans, fs_info,
1178 : : root->root_key.objectid,
1179 : : objectid, pending->inherit);
1180 [ # # ]: 0 : if (pending->error)
1181 : : goto no_free_objectid;
1182 : :
1183 : 0 : key.objectid = objectid;
1184 : 0 : key.offset = (u64)-1;
1185 : 0 : key.type = BTRFS_ROOT_ITEM_KEY;
1186 : :
1187 : 0 : rsv = trans->block_rsv;
1188 : 0 : trans->block_rsv = &pending->block_rsv;
1189 : 0 : trans->bytes_reserved = trans->block_rsv->reserved;
1190 : :
1191 : 0 : dentry = pending->dentry;
1192 : 0 : parent_inode = pending->dir;
1193 : 0 : parent_root = BTRFS_I(parent_inode)->root;
1194 : 0 : record_root_in_trans(trans, parent_root);
1195 : :
1196 : : /*
1197 : : * insert the directory item
1198 : : */
1199 : 0 : ret = btrfs_set_inode_index(parent_inode, &index);
1200 [ # # ]: 0 : BUG_ON(ret); /* -ENOMEM */
1201 : :
1202 : : /* check if there is a file/dir which has the same name. */
1203 : 0 : dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1204 : : btrfs_ino(parent_inode),
1205 : 0 : dentry->d_name.name,
1206 : 0 : dentry->d_name.len, 0);
1207 [ # # ][ # # ]: 0 : if (dir_item != NULL && !IS_ERR(dir_item)) {
1208 : 0 : pending->error = -EEXIST;
1209 : 0 : goto dir_item_existed;
1210 [ # # ]: 0 : } else if (IS_ERR(dir_item)) {
1211 : : ret = PTR_ERR(dir_item);
1212 : 0 : btrfs_abort_transaction(trans, root, ret);
1213 : 0 : goto fail;
1214 : : }
1215 : 0 : btrfs_release_path(path);
1216 : :
1217 : : /*
1218 : : * pull in the delayed directory update
1219 : : * and the delayed inode item
1220 : : * otherwise we corrupt the FS during
1221 : : * snapshot
1222 : : */
1223 : 0 : ret = btrfs_run_delayed_items(trans, root);
1224 [ # # ]: 0 : if (ret) { /* Transaction aborted */
1225 : 0 : btrfs_abort_transaction(trans, root, ret);
1226 : 0 : goto fail;
1227 : : }
1228 : :
1229 : 0 : record_root_in_trans(trans, root);
1230 : 0 : btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1231 : 0 : memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1232 : 0 : btrfs_check_and_init_root_item(new_root_item);
1233 : :
1234 : : root_flags = btrfs_root_flags(new_root_item);
1235 [ # # ]: 0 : if (pending->readonly)
1236 : 0 : root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1237 : : else
1238 : 0 : root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1239 : : btrfs_set_root_flags(new_root_item, root_flags);
1240 : :
1241 : 0 : btrfs_set_root_generation_v2(new_root_item,
1242 : : trans->transid);
1243 : 0 : uuid_le_gen(&new_uuid);
1244 : 0 : memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1245 : 0 : memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1246 : : BTRFS_UUID_SIZE);
1247 [ # # ]: 0 : if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1248 : 0 : memset(new_root_item->received_uuid, 0,
1249 : : sizeof(new_root_item->received_uuid));
1250 : 0 : memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1251 : 0 : memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1252 : : btrfs_set_root_stransid(new_root_item, 0);
1253 : : btrfs_set_root_rtransid(new_root_item, 0);
1254 : : }
1255 : 0 : btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
1256 : 0 : btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
1257 : 0 : btrfs_set_root_otransid(new_root_item, trans->transid);
1258 : :
1259 : 0 : old = btrfs_lock_root_node(root);
1260 : 0 : ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
1261 [ # # ]: 0 : if (ret) {
1262 : 0 : btrfs_tree_unlock(old);
1263 : 0 : free_extent_buffer(old);
1264 : 0 : btrfs_abort_transaction(trans, root, ret);
1265 : 0 : goto fail;
1266 : : }
1267 : :
1268 : 0 : btrfs_set_lock_blocking(old);
1269 : :
1270 : 0 : ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1271 : : /* clean up in any case */
1272 : 0 : btrfs_tree_unlock(old);
1273 : 0 : free_extent_buffer(old);
1274 [ # # ]: 0 : if (ret) {
1275 : 0 : btrfs_abort_transaction(trans, root, ret);
1276 : 0 : goto fail;
1277 : : }
1278 : :
1279 : : /* see comments in should_cow_block() */
1280 : 0 : root->force_cow = 1;
1281 : 0 : smp_wmb();
1282 : :
1283 : 0 : btrfs_set_root_node(new_root_item, tmp);
1284 : : /* record when the snapshot was created in key.offset */
1285 : 0 : key.offset = trans->transid;
1286 : 0 : ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1287 : 0 : btrfs_tree_unlock(tmp);
1288 : 0 : free_extent_buffer(tmp);
1289 [ # # ]: 0 : if (ret) {
1290 : 0 : btrfs_abort_transaction(trans, root, ret);
1291 : 0 : goto fail;
1292 : : }
1293 : :
1294 : : /*
1295 : : * insert root back/forward references
1296 : : */
1297 : 0 : ret = btrfs_add_root_ref(trans, tree_root, objectid,
1298 : : parent_root->root_key.objectid,
1299 : : btrfs_ino(parent_inode), index,
1300 : 0 : dentry->d_name.name, dentry->d_name.len);
1301 [ # # ]: 0 : if (ret) {
1302 : 0 : btrfs_abort_transaction(trans, root, ret);
1303 : 0 : goto fail;
1304 : : }
1305 : :
1306 : 0 : key.offset = (u64)-1;
1307 : 0 : pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1308 [ # # ]: 0 : if (IS_ERR(pending->snap)) {
1309 : : ret = PTR_ERR(pending->snap);
1310 : 0 : btrfs_abort_transaction(trans, root, ret);
1311 : 0 : goto fail;
1312 : : }
1313 : :
1314 : 0 : ret = btrfs_reloc_post_snapshot(trans, pending);
1315 [ # # ]: 0 : if (ret) {
1316 : 0 : btrfs_abort_transaction(trans, root, ret);
1317 : 0 : goto fail;
1318 : : }
1319 : :
1320 : 0 : ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1321 [ # # ]: 0 : if (ret) {
1322 : 0 : btrfs_abort_transaction(trans, root, ret);
1323 : 0 : goto fail;
1324 : : }
1325 : :
1326 : 0 : ret = btrfs_insert_dir_item(trans, parent_root,
1327 : 0 : dentry->d_name.name, dentry->d_name.len,
1328 : : parent_inode, &key,
1329 : : BTRFS_FT_DIR, index);
1330 : : /* We have check then name at the beginning, so it is impossible. */
1331 [ # # ]: 0 : BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
1332 [ # # ]: 0 : if (ret) {
1333 : 0 : btrfs_abort_transaction(trans, root, ret);
1334 : 0 : goto fail;
1335 : : }
1336 : :
1337 : 0 : btrfs_i_size_write(parent_inode, parent_inode->i_size +
1338 : 0 : dentry->d_name.len * 2);
1339 : 0 : parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1340 : 0 : ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1341 [ # # ]: 0 : if (ret) {
1342 : 0 : btrfs_abort_transaction(trans, root, ret);
1343 : 0 : goto fail;
1344 : : }
1345 : 0 : ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b,
1346 : : BTRFS_UUID_KEY_SUBVOL, objectid);
1347 [ # # ]: 0 : if (ret) {
1348 : 0 : btrfs_abort_transaction(trans, root, ret);
1349 : 0 : goto fail;
1350 : : }
1351 [ # # ]: 0 : if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1352 : 0 : ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
1353 : : new_root_item->received_uuid,
1354 : : BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1355 : : objectid);
1356 [ # # ]: 0 : if (ret && ret != -EEXIST) {
1357 : 0 : btrfs_abort_transaction(trans, root, ret);
1358 : 0 : goto fail;
1359 : : }
1360 : : }
1361 : : fail:
1362 : 0 : pending->error = ret;
1363 : : dir_item_existed:
1364 : 0 : trans->block_rsv = rsv;
1365 : 0 : trans->bytes_reserved = 0;
1366 : : no_free_objectid:
1367 : 0 : kfree(new_root_item);
1368 : : root_item_alloc_fail:
1369 : 0 : btrfs_free_path(path);
1370 : 0 : return ret;
1371 : : }
1372 : :
1373 : : /*
1374 : : * create all the snapshots we've scheduled for creation
1375 : : */
1376 : 0 : static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1377 : : struct btrfs_fs_info *fs_info)
1378 : : {
1379 : : struct btrfs_pending_snapshot *pending, *next;
1380 : 0 : struct list_head *head = &trans->transaction->pending_snapshots;
1381 : : int ret = 0;
1382 : :
1383 [ # # ]: 0 : list_for_each_entry_safe(pending, next, head, list) {
1384 : : list_del(&pending->list);
1385 : 0 : ret = create_pending_snapshot(trans, fs_info, pending);
1386 [ # # ]: 0 : if (ret)
1387 : : break;
1388 : : }
1389 : 0 : return ret;
1390 : : }
1391 : :
1392 : 0 : static void update_super_roots(struct btrfs_root *root)
1393 : : {
1394 : : struct btrfs_root_item *root_item;
1395 : : struct btrfs_super_block *super;
1396 : :
1397 : 0 : super = root->fs_info->super_copy;
1398 : :
1399 : 0 : root_item = &root->fs_info->chunk_root->root_item;
1400 : 0 : super->chunk_root = root_item->bytenr;
1401 : 0 : super->chunk_root_generation = root_item->generation;
1402 : 0 : super->chunk_root_level = root_item->level;
1403 : :
1404 : 0 : root_item = &root->fs_info->tree_root->root_item;
1405 : 0 : super->root = root_item->bytenr;
1406 : 0 : super->generation = root_item->generation;
1407 : 0 : super->root_level = root_item->level;
1408 [ # # ]: 0 : if (btrfs_test_opt(root, SPACE_CACHE))
1409 : 0 : super->cache_generation = root_item->generation;
1410 [ # # ]: 0 : if (root->fs_info->update_uuid_tree_gen)
1411 : 0 : super->uuid_tree_generation = root_item->generation;
1412 : 0 : }
1413 : :
1414 : 0 : int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1415 : : {
1416 : : struct btrfs_transaction *trans;
1417 : : int ret = 0;
1418 : :
1419 : : spin_lock(&info->trans_lock);
1420 : 0 : trans = info->running_transaction;
1421 [ # # ]: 0 : if (trans)
1422 : 0 : ret = (trans->state >= TRANS_STATE_COMMIT_START);
1423 : : spin_unlock(&info->trans_lock);
1424 : 0 : return ret;
1425 : : }
1426 : :
1427 : 0 : int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1428 : : {
1429 : 0 : struct btrfs_transaction *trans;
1430 : : int ret = 0;
1431 : :
1432 : : spin_lock(&info->trans_lock);
1433 : 0 : trans = info->running_transaction;
1434 [ # # ]: 0 : if (trans)
1435 : : ret = is_transaction_blocked(trans);
1436 : : spin_unlock(&info->trans_lock);
1437 : 0 : return ret;
1438 : : }
1439 : :
1440 : : /*
1441 : : * wait for the current transaction commit to start and block subsequent
1442 : : * transaction joins
1443 : : */
1444 : 0 : static void wait_current_trans_commit_start(struct btrfs_root *root,
1445 : : struct btrfs_transaction *trans)
1446 : : {
1447 [ # # ][ # # ]: 0 : wait_event(root->fs_info->transaction_blocked_wait,
[ # # ][ # # ]
1448 : : trans->state >= TRANS_STATE_COMMIT_START ||
1449 : : trans->aborted);
1450 : 0 : }
1451 : :
1452 : : /*
1453 : : * wait for the current transaction to start and then become unblocked.
1454 : : * caller holds ref.
1455 : : */
1456 : 0 : static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1457 : : struct btrfs_transaction *trans)
1458 : : {
1459 [ # # ][ # # ]: 0 : wait_event(root->fs_info->transaction_wait,
[ # # ][ # # ]
1460 : : trans->state >= TRANS_STATE_UNBLOCKED ||
1461 : : trans->aborted);
1462 : 0 : }
1463 : :
1464 : : /*
1465 : : * commit transactions asynchronously. once btrfs_commit_transaction_async
1466 : : * returns, any subsequent transaction will not be allowed to join.
1467 : : */
1468 : : struct btrfs_async_commit {
1469 : : struct btrfs_trans_handle *newtrans;
1470 : : struct btrfs_root *root;
1471 : : struct work_struct work;
1472 : : };
1473 : :
1474 : 0 : static void do_async_commit(struct work_struct *work)
1475 : : {
1476 : 0 : struct btrfs_async_commit *ac =
1477 : : container_of(work, struct btrfs_async_commit, work);
1478 : :
1479 : : /*
1480 : : * We've got freeze protection passed with the transaction.
1481 : : * Tell lockdep about it.
1482 : : */
1483 : : if (ac->newtrans->type & __TRANS_FREEZABLE)
1484 : : rwsem_acquire_read(
1485 : : &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1486 : : 0, 1, _THIS_IP_);
1487 : :
1488 : 0 : current->journal_info = ac->newtrans;
1489 : :
1490 : 0 : btrfs_commit_transaction(ac->newtrans, ac->root);
1491 : 0 : kfree(ac);
1492 : 0 : }
1493 : :
1494 : 0 : int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1495 : : struct btrfs_root *root,
1496 : : int wait_for_unblock)
1497 : : {
1498 : : struct btrfs_async_commit *ac;
1499 : : struct btrfs_transaction *cur_trans;
1500 : :
1501 : : ac = kmalloc(sizeof(*ac), GFP_NOFS);
1502 [ # # ]: 0 : if (!ac)
1503 : : return -ENOMEM;
1504 : :
1505 : 0 : INIT_WORK(&ac->work, do_async_commit);
1506 : 0 : ac->root = root;
1507 : 0 : ac->newtrans = btrfs_join_transaction(root);
1508 [ # # ]: 0 : if (IS_ERR(ac->newtrans)) {
1509 : : int err = PTR_ERR(ac->newtrans);
1510 : 0 : kfree(ac);
1511 : 0 : return err;
1512 : : }
1513 : :
1514 : : /* take transaction reference */
1515 : 0 : cur_trans = trans->transaction;
1516 : 0 : atomic_inc(&cur_trans->use_count);
1517 : :
1518 : : btrfs_end_transaction(trans, root);
1519 : :
1520 : : /*
1521 : : * Tell lockdep we've released the freeze rwsem, since the
1522 : : * async commit thread will be the one to unlock it.
1523 : : */
1524 : : if (ac->newtrans->type & __TRANS_FREEZABLE)
1525 : : rwsem_release(
1526 : : &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1527 : : 1, _THIS_IP_);
1528 : :
1529 : 0 : schedule_work(&ac->work);
1530 : :
1531 : : /* wait for transaction to start and unblock */
1532 [ # # ]: 0 : if (wait_for_unblock)
1533 : 0 : wait_current_trans_commit_start_and_unblock(root, cur_trans);
1534 : : else
1535 : 0 : wait_current_trans_commit_start(root, cur_trans);
1536 : :
1537 [ # # ]: 0 : if (current->journal_info == trans)
1538 : 0 : current->journal_info = NULL;
1539 : :
1540 : 0 : btrfs_put_transaction(cur_trans);
1541 : 0 : return 0;
1542 : : }
1543 : :
1544 : :
1545 : 0 : static void cleanup_transaction(struct btrfs_trans_handle *trans,
1546 : : struct btrfs_root *root, int err)
1547 : : {
1548 : 0 : struct btrfs_transaction *cur_trans = trans->transaction;
1549 : 0 : DEFINE_WAIT(wait);
1550 : :
1551 [ # # ]: 0 : WARN_ON(trans->use_count > 1);
1552 : :
1553 : 0 : btrfs_abort_transaction(trans, root, err);
1554 : :
1555 : 0 : spin_lock(&root->fs_info->trans_lock);
1556 : :
1557 : : /*
1558 : : * If the transaction is removed from the list, it means this
1559 : : * transaction has been committed successfully, so it is impossible
1560 : : * to call the cleanup function.
1561 : : */
1562 [ # # ]: 0 : BUG_ON(list_empty(&cur_trans->list));
1563 : :
1564 : : list_del_init(&cur_trans->list);
1565 [ # # ]: 0 : if (cur_trans == root->fs_info->running_transaction) {
1566 : 0 : cur_trans->state = TRANS_STATE_COMMIT_DOING;
1567 : 0 : spin_unlock(&root->fs_info->trans_lock);
1568 [ # # ][ # # ]: 0 : wait_event(cur_trans->writer_wait,
1569 : : atomic_read(&cur_trans->num_writers) == 1);
1570 : :
1571 : 0 : spin_lock(&root->fs_info->trans_lock);
1572 : : }
1573 : 0 : spin_unlock(&root->fs_info->trans_lock);
1574 : :
1575 : 0 : btrfs_cleanup_one_transaction(trans->transaction, root);
1576 : :
1577 : 0 : spin_lock(&root->fs_info->trans_lock);
1578 [ # # ]: 0 : if (cur_trans == root->fs_info->running_transaction)
1579 : 0 : root->fs_info->running_transaction = NULL;
1580 : 0 : spin_unlock(&root->fs_info->trans_lock);
1581 : :
1582 [ # # ]: 0 : if (trans->type & __TRANS_FREEZABLE)
1583 : 0 : sb_end_intwrite(root->fs_info->sb);
1584 : 0 : btrfs_put_transaction(cur_trans);
1585 : 0 : btrfs_put_transaction(cur_trans);
1586 : :
1587 : : trace_btrfs_transaction_commit(root);
1588 : :
1589 : 0 : btrfs_scrub_continue(root);
1590 : :
1591 [ # # ]: 0 : if (current->journal_info == trans)
1592 : 0 : current->journal_info = NULL;
1593 : :
1594 : 0 : kmem_cache_free(btrfs_trans_handle_cachep, trans);
1595 : 0 : }
1596 : :
1597 : 0 : static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1598 : : struct btrfs_root *root)
1599 : : {
1600 : : int ret;
1601 : :
1602 : 0 : ret = btrfs_run_delayed_items(trans, root);
1603 : : /*
1604 : : * running the delayed items may have added new refs. account
1605 : : * them now so that they hinder processing of more delayed refs
1606 : : * as little as possible.
1607 : : */
1608 [ # # ]: 0 : if (ret) {
1609 : 0 : btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1610 : 0 : return ret;
1611 : : }
1612 : :
1613 : 0 : ret = btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1614 [ # # ]: 0 : if (ret)
1615 : : return ret;
1616 : :
1617 : : /*
1618 : : * rename don't use btrfs_join_transaction, so, once we
1619 : : * set the transaction to blocked above, we aren't going
1620 : : * to get any new ordered operations. We can safely run
1621 : : * it here and no for sure that nothing new will be added
1622 : : * to the list
1623 : : */
1624 : 0 : ret = btrfs_run_ordered_operations(trans, root, 1);
1625 : :
1626 : 0 : return ret;
1627 : : }
1628 : :
1629 : : static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
1630 : : {
1631 [ # # ]: 0 : if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1632 : 0 : return btrfs_start_delalloc_roots(fs_info, 1);
1633 : : return 0;
1634 : : }
1635 : :
1636 : : static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
1637 : : {
1638 [ # # ]: 0 : if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1639 : 0 : btrfs_wait_ordered_roots(fs_info, -1);
1640 : : }
1641 : :
1642 : 0 : int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1643 : 0 : struct btrfs_root *root)
1644 : : {
1645 : 0 : struct btrfs_transaction *cur_trans = trans->transaction;
1646 : : struct btrfs_transaction *prev_trans = NULL;
1647 : : int ret;
1648 : :
1649 : 0 : ret = btrfs_run_ordered_operations(trans, root, 0);
1650 [ # # ]: 0 : if (ret) {
1651 : 0 : btrfs_abort_transaction(trans, root, ret);
1652 : 0 : btrfs_end_transaction(trans, root);
1653 : 0 : return ret;
1654 : : }
1655 : :
1656 : : /* Stop the commit early if ->aborted is set */
1657 [ # # ]: 0 : if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1658 : 0 : ret = cur_trans->aborted;
1659 : 0 : btrfs_end_transaction(trans, root);
1660 : 0 : return ret;
1661 : : }
1662 : :
1663 : : /* make a pass through all the delayed refs we have so far
1664 : : * any runnings procs may add more while we are here
1665 : : */
1666 : 0 : ret = btrfs_run_delayed_refs(trans, root, 0);
1667 [ # # ]: 0 : if (ret) {
1668 : 0 : btrfs_end_transaction(trans, root);
1669 : 0 : return ret;
1670 : : }
1671 : :
1672 : 0 : btrfs_trans_release_metadata(trans, root);
1673 : 0 : trans->block_rsv = NULL;
1674 [ # # ]: 0 : if (trans->qgroup_reserved) {
1675 : 0 : btrfs_qgroup_free(root, trans->qgroup_reserved);
1676 : 0 : trans->qgroup_reserved = 0;
1677 : : }
1678 : :
1679 : 0 : cur_trans = trans->transaction;
1680 : :
1681 : : /*
1682 : : * set the flushing flag so procs in this transaction have to
1683 : : * start sending their work down.
1684 : : */
1685 : 0 : cur_trans->delayed_refs.flushing = 1;
1686 : 0 : smp_wmb();
1687 : :
1688 [ # # ]: 0 : if (!list_empty(&trans->new_bgs))
1689 : 0 : btrfs_create_pending_block_groups(trans, root);
1690 : :
1691 : 0 : ret = btrfs_run_delayed_refs(trans, root, 0);
1692 [ # # ]: 0 : if (ret) {
1693 : 0 : btrfs_end_transaction(trans, root);
1694 : 0 : return ret;
1695 : : }
1696 : :
1697 : 0 : spin_lock(&root->fs_info->trans_lock);
1698 [ # # ]: 0 : if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
1699 : 0 : spin_unlock(&root->fs_info->trans_lock);
1700 : 0 : atomic_inc(&cur_trans->use_count);
1701 : 0 : ret = btrfs_end_transaction(trans, root);
1702 : :
1703 : 0 : wait_for_commit(root, cur_trans);
1704 : :
1705 : 0 : btrfs_put_transaction(cur_trans);
1706 : :
1707 : 0 : return ret;
1708 : : }
1709 : :
1710 : 0 : cur_trans->state = TRANS_STATE_COMMIT_START;
1711 : 0 : wake_up(&root->fs_info->transaction_blocked_wait);
1712 : :
1713 [ # # ]: 0 : if (cur_trans->list.prev != &root->fs_info->trans_list) {
1714 : 0 : prev_trans = list_entry(cur_trans->list.prev,
1715 : : struct btrfs_transaction, list);
1716 [ # # ]: 0 : if (prev_trans->state != TRANS_STATE_COMPLETED) {
1717 : 0 : atomic_inc(&prev_trans->use_count);
1718 : 0 : spin_unlock(&root->fs_info->trans_lock);
1719 : :
1720 : 0 : wait_for_commit(root, prev_trans);
1721 : :
1722 : 0 : btrfs_put_transaction(prev_trans);
1723 : : } else {
1724 : : spin_unlock(&root->fs_info->trans_lock);
1725 : : }
1726 : : } else {
1727 : : spin_unlock(&root->fs_info->trans_lock);
1728 : : }
1729 : :
1730 : 0 : extwriter_counter_dec(cur_trans, trans->type);
1731 : :
1732 : 0 : ret = btrfs_start_delalloc_flush(root->fs_info);
1733 [ # # ]: 0 : if (ret)
1734 : : goto cleanup_transaction;
1735 : :
1736 : 0 : ret = btrfs_flush_all_pending_stuffs(trans, root);
1737 [ # # ]: 0 : if (ret)
1738 : : goto cleanup_transaction;
1739 : :
1740 [ # # ][ # # ]: 0 : wait_event(cur_trans->writer_wait,
1741 : : extwriter_counter_read(cur_trans) == 0);
1742 : :
1743 : : /* some pending stuffs might be added after the previous flush. */
1744 : 0 : ret = btrfs_flush_all_pending_stuffs(trans, root);
1745 [ # # ]: 0 : if (ret)
1746 : : goto cleanup_transaction;
1747 : :
1748 : 0 : btrfs_wait_delalloc_flush(root->fs_info);
1749 : : /*
1750 : : * Ok now we need to make sure to block out any other joins while we
1751 : : * commit the transaction. We could have started a join before setting
1752 : : * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1753 : : */
1754 : 0 : spin_lock(&root->fs_info->trans_lock);
1755 : 0 : cur_trans->state = TRANS_STATE_COMMIT_DOING;
1756 : 0 : spin_unlock(&root->fs_info->trans_lock);
1757 [ # # ][ # # ]: 0 : wait_event(cur_trans->writer_wait,
1758 : : atomic_read(&cur_trans->num_writers) == 1);
1759 : :
1760 : : /* ->aborted might be set after the previous check, so check it */
1761 [ # # ]: 0 : if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1762 : 0 : ret = cur_trans->aborted;
1763 : 0 : goto cleanup_transaction;
1764 : : }
1765 : : /*
1766 : : * the reloc mutex makes sure that we stop
1767 : : * the balancing code from coming in and moving
1768 : : * extents around in the middle of the commit
1769 : : */
1770 : 0 : mutex_lock(&root->fs_info->reloc_mutex);
1771 : :
1772 : : /*
1773 : : * We needn't worry about the delayed items because we will
1774 : : * deal with them in create_pending_snapshot(), which is the
1775 : : * core function of the snapshot creation.
1776 : : */
1777 : 0 : ret = create_pending_snapshots(trans, root->fs_info);
1778 [ # # ]: 0 : if (ret) {
1779 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1780 : 0 : goto cleanup_transaction;
1781 : : }
1782 : :
1783 : : /*
1784 : : * We insert the dir indexes of the snapshots and update the inode
1785 : : * of the snapshots' parents after the snapshot creation, so there
1786 : : * are some delayed items which are not dealt with. Now deal with
1787 : : * them.
1788 : : *
1789 : : * We needn't worry that this operation will corrupt the snapshots,
1790 : : * because all the tree which are snapshoted will be forced to COW
1791 : : * the nodes and leaves.
1792 : : */
1793 : 0 : ret = btrfs_run_delayed_items(trans, root);
1794 [ # # ]: 0 : if (ret) {
1795 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1796 : 0 : goto cleanup_transaction;
1797 : : }
1798 : :
1799 : 0 : ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1800 [ # # ]: 0 : if (ret) {
1801 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1802 : 0 : goto cleanup_transaction;
1803 : : }
1804 : :
1805 : : /*
1806 : : * make sure none of the code above managed to slip in a
1807 : : * delayed item
1808 : : */
1809 : 0 : btrfs_assert_delayed_root_empty(root);
1810 : :
1811 [ # # ]: 0 : WARN_ON(cur_trans != trans->transaction);
1812 : :
1813 : 0 : btrfs_scrub_pause(root);
1814 : : /* btrfs_commit_tree_roots is responsible for getting the
1815 : : * various roots consistent with each other. Every pointer
1816 : : * in the tree of tree roots has to point to the most up to date
1817 : : * root for every subvolume and other tree. So, we have to keep
1818 : : * the tree logging code from jumping in and changing any
1819 : : * of the trees.
1820 : : *
1821 : : * At this point in the commit, there can't be any tree-log
1822 : : * writers, but a little lower down we drop the trans mutex
1823 : : * and let new people in. By holding the tree_log_mutex
1824 : : * from now until after the super is written, we avoid races
1825 : : * with the tree-log code.
1826 : : */
1827 : 0 : mutex_lock(&root->fs_info->tree_log_mutex);
1828 : :
1829 : 0 : ret = commit_fs_roots(trans, root);
1830 [ # # ]: 0 : if (ret) {
1831 : 0 : mutex_unlock(&root->fs_info->tree_log_mutex);
1832 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1833 : 0 : goto cleanup_transaction;
1834 : : }
1835 : :
1836 : : /* commit_fs_roots gets rid of all the tree log roots, it is now
1837 : : * safe to free the root of tree log roots
1838 : : */
1839 : 0 : btrfs_free_log_root_tree(trans, root->fs_info);
1840 : :
1841 : 0 : ret = commit_cowonly_roots(trans, root);
1842 [ # # ]: 0 : if (ret) {
1843 : 0 : mutex_unlock(&root->fs_info->tree_log_mutex);
1844 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1845 : 0 : goto cleanup_transaction;
1846 : : }
1847 : :
1848 : : /*
1849 : : * The tasks which save the space cache and inode cache may also
1850 : : * update ->aborted, check it.
1851 : : */
1852 [ # # ]: 0 : if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1853 : 0 : ret = cur_trans->aborted;
1854 : 0 : mutex_unlock(&root->fs_info->tree_log_mutex);
1855 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1856 : 0 : goto cleanup_transaction;
1857 : : }
1858 : :
1859 : 0 : btrfs_prepare_extent_commit(trans, root);
1860 : :
1861 : 0 : cur_trans = root->fs_info->running_transaction;
1862 : :
1863 : 0 : btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1864 : : root->fs_info->tree_root->node);
1865 : 0 : switch_commit_root(root->fs_info->tree_root);
1866 : :
1867 : 0 : btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1868 : : root->fs_info->chunk_root->node);
1869 : 0 : switch_commit_root(root->fs_info->chunk_root);
1870 : :
1871 : 0 : assert_qgroups_uptodate(trans);
1872 : 0 : update_super_roots(root);
1873 : :
1874 : 0 : btrfs_set_super_log_root(root->fs_info->super_copy, 0);
1875 : 0 : btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1876 : 0 : memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
1877 : : sizeof(*root->fs_info->super_copy));
1878 : :
1879 : 0 : spin_lock(&root->fs_info->trans_lock);
1880 : 0 : cur_trans->state = TRANS_STATE_UNBLOCKED;
1881 : 0 : root->fs_info->running_transaction = NULL;
1882 : 0 : spin_unlock(&root->fs_info->trans_lock);
1883 : 0 : mutex_unlock(&root->fs_info->reloc_mutex);
1884 : :
1885 : 0 : wake_up(&root->fs_info->transaction_wait);
1886 : :
1887 : 0 : ret = btrfs_write_and_wait_transaction(trans, root);
1888 [ # # ]: 0 : if (ret) {
1889 : 0 : btrfs_error(root->fs_info, ret,
1890 : : "Error while writing out transaction");
1891 : 0 : mutex_unlock(&root->fs_info->tree_log_mutex);
1892 : 0 : goto cleanup_transaction;
1893 : : }
1894 : :
1895 : 0 : ret = write_ctree_super(trans, root, 0);
1896 [ # # ]: 0 : if (ret) {
1897 : 0 : mutex_unlock(&root->fs_info->tree_log_mutex);
1898 : 0 : goto cleanup_transaction;
1899 : : }
1900 : :
1901 : : /*
1902 : : * the super is written, we can safely allow the tree-loggers
1903 : : * to go about their business
1904 : : */
1905 : 0 : mutex_unlock(&root->fs_info->tree_log_mutex);
1906 : :
1907 : 0 : btrfs_finish_extent_commit(trans, root);
1908 : :
1909 : 0 : root->fs_info->last_trans_committed = cur_trans->transid;
1910 : : /*
1911 : : * We needn't acquire the lock here because there is no other task
1912 : : * which can change it.
1913 : : */
1914 : 0 : cur_trans->state = TRANS_STATE_COMPLETED;
1915 : 0 : wake_up(&cur_trans->commit_wait);
1916 : :
1917 : 0 : spin_lock(&root->fs_info->trans_lock);
1918 : 0 : list_del_init(&cur_trans->list);
1919 : 0 : spin_unlock(&root->fs_info->trans_lock);
1920 : :
1921 : 0 : btrfs_put_transaction(cur_trans);
1922 : 0 : btrfs_put_transaction(cur_trans);
1923 : :
1924 [ # # ]: 0 : if (trans->type & __TRANS_FREEZABLE)
1925 : 0 : sb_end_intwrite(root->fs_info->sb);
1926 : :
1927 : : trace_btrfs_transaction_commit(root);
1928 : :
1929 : 0 : btrfs_scrub_continue(root);
1930 : :
1931 [ # # ]: 0 : if (current->journal_info == trans)
1932 : 0 : current->journal_info = NULL;
1933 : :
1934 : 0 : kmem_cache_free(btrfs_trans_handle_cachep, trans);
1935 : :
1936 [ # # ]: 0 : if (current != root->fs_info->transaction_kthread)
1937 : 0 : btrfs_run_delayed_iputs(root);
1938 : :
1939 : 0 : return ret;
1940 : :
1941 : : cleanup_transaction:
1942 : 0 : btrfs_trans_release_metadata(trans, root);
1943 : 0 : trans->block_rsv = NULL;
1944 [ # # ]: 0 : if (trans->qgroup_reserved) {
1945 : 0 : btrfs_qgroup_free(root, trans->qgroup_reserved);
1946 : 0 : trans->qgroup_reserved = 0;
1947 : : }
1948 : 0 : btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
1949 [ # # ]: 0 : if (current->journal_info == trans)
1950 : 0 : current->journal_info = NULL;
1951 : 0 : cleanup_transaction(trans, root, ret);
1952 : :
1953 : 0 : return ret;
1954 : : }
1955 : :
1956 : : /*
1957 : : * return < 0 if error
1958 : : * 0 if there are no more dead_roots at the time of call
1959 : : * 1 there are more to be processed, call me again
1960 : : *
1961 : : * The return value indicates there are certainly more snapshots to delete, but
1962 : : * if there comes a new one during processing, it may return 0. We don't mind,
1963 : : * because btrfs_commit_super will poke cleaner thread and it will process it a
1964 : : * few seconds later.
1965 : : */
1966 : 0 : int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
1967 : : {
1968 : : int ret;
1969 : 0 : struct btrfs_fs_info *fs_info = root->fs_info;
1970 : :
1971 : : spin_lock(&fs_info->trans_lock);
1972 [ # # ]: 0 : if (list_empty(&fs_info->dead_roots)) {
1973 : : spin_unlock(&fs_info->trans_lock);
1974 : 0 : return 0;
1975 : : }
1976 : 0 : root = list_first_entry(&fs_info->dead_roots,
1977 : : struct btrfs_root, root_list);
1978 : 0 : list_del_init(&root->root_list);
1979 : : spin_unlock(&fs_info->trans_lock);
1980 : :
1981 : : pr_debug("btrfs: cleaner removing %llu\n", root->objectid);
1982 : :
1983 : 0 : btrfs_kill_all_delayed_nodes(root);
1984 : :
1985 [ # # ]: 0 : if (btrfs_header_backref_rev(root->node) <
1986 : : BTRFS_MIXED_BACKREF_REV)
1987 : 0 : ret = btrfs_drop_snapshot(root, NULL, 0, 0);
1988 : : else
1989 : 0 : ret = btrfs_drop_snapshot(root, NULL, 1, 0);
1990 : : /*
1991 : : * If we encounter a transaction abort during snapshot cleaning, we
1992 : : * don't want to crash here
1993 : : */
1994 : 0 : return (ret < 0) ? 0 : 1;
1995 : : }
|