~ubuntu-branches/ubuntu/quantal/linux-linaro-mx51/quantal

« back to all changes in this revision

Viewing changes to fs/btrfs/transaction.c

  • Committer: Package Import Robot
  • Author(s): John Rigby, John Rigby
  • Date: 2011-09-26 10:44:23 UTC
  • Revision ID: package-import@ubuntu.com-20110926104423-3o58a3c1bj7x00rs
Tags: 3.0.0-1007.9
[ John Rigby ]

Enable crypto modules and remove crypto-modules from
exclude-module files
LP: #826021

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "transaction.h"
28
28
#include "locking.h"
29
29
#include "tree-log.h"
 
30
#include "inode-map.h"
30
31
 
31
32
#define BTRFS_ROOT_TRANS_TAG 0
32
33
 
34
35
{
35
36
        WARN_ON(atomic_read(&transaction->use_count) == 0);
36
37
        if (atomic_dec_and_test(&transaction->use_count)) {
 
38
                BUG_ON(!list_empty(&transaction->list));
37
39
                memset(transaction, 0, sizeof(*transaction));
38
40
                kmem_cache_free(btrfs_transaction_cachep, transaction);
39
41
        }
48
50
/*
49
51
 * either allocate a new transaction or hop into the existing one
50
52
 */
51
 
static noinline int join_transaction(struct btrfs_root *root)
 
53
static noinline int join_transaction(struct btrfs_root *root, int nofail)
52
54
{
53
55
        struct btrfs_transaction *cur_trans;
 
56
 
 
57
        spin_lock(&root->fs_info->trans_lock);
 
58
        if (root->fs_info->trans_no_join) {
 
59
                if (!nofail) {
 
60
                        spin_unlock(&root->fs_info->trans_lock);
 
61
                        return -EBUSY;
 
62
                }
 
63
        }
 
64
 
54
65
        cur_trans = root->fs_info->running_transaction;
55
 
        if (!cur_trans) {
56
 
                cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
57
 
                                             GFP_NOFS);
58
 
                if (!cur_trans)
59
 
                        return -ENOMEM;
60
 
                root->fs_info->generation++;
61
 
                atomic_set(&cur_trans->num_writers, 1);
62
 
                cur_trans->num_joined = 0;
63
 
                cur_trans->transid = root->fs_info->generation;
64
 
                init_waitqueue_head(&cur_trans->writer_wait);
65
 
                init_waitqueue_head(&cur_trans->commit_wait);
66
 
                cur_trans->in_commit = 0;
67
 
                cur_trans->blocked = 0;
68
 
                atomic_set(&cur_trans->use_count, 1);
69
 
                cur_trans->commit_done = 0;
70
 
                cur_trans->start_time = get_seconds();
71
 
 
72
 
                cur_trans->delayed_refs.root = RB_ROOT;
73
 
                cur_trans->delayed_refs.num_entries = 0;
74
 
                cur_trans->delayed_refs.num_heads_ready = 0;
75
 
                cur_trans->delayed_refs.num_heads = 0;
76
 
                cur_trans->delayed_refs.flushing = 0;
77
 
                cur_trans->delayed_refs.run_delayed_start = 0;
78
 
                spin_lock_init(&cur_trans->delayed_refs.lock);
79
 
 
80
 
                INIT_LIST_HEAD(&cur_trans->pending_snapshots);
81
 
                list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
82
 
                extent_io_tree_init(&cur_trans->dirty_pages,
83
 
                                     root->fs_info->btree_inode->i_mapping,
84
 
                                     GFP_NOFS);
85
 
                spin_lock(&root->fs_info->new_trans_lock);
86
 
                root->fs_info->running_transaction = cur_trans;
87
 
                spin_unlock(&root->fs_info->new_trans_lock);
88
 
        } else {
89
 
                atomic_inc(&cur_trans->num_writers);
90
 
                cur_trans->num_joined++;
91
 
        }
 
66
        if (cur_trans) {
 
67
                atomic_inc(&cur_trans->use_count);
 
68
                atomic_inc(&cur_trans->num_writers);
 
69
                cur_trans->num_joined++;
 
70
                spin_unlock(&root->fs_info->trans_lock);
 
71
                return 0;
 
72
        }
 
73
        spin_unlock(&root->fs_info->trans_lock);
 
74
 
 
75
        cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
 
76
        if (!cur_trans)
 
77
                return -ENOMEM;
 
78
        spin_lock(&root->fs_info->trans_lock);
 
79
        if (root->fs_info->running_transaction) {
 
80
                kmem_cache_free(btrfs_transaction_cachep, cur_trans);
 
81
                cur_trans = root->fs_info->running_transaction;
 
82
                atomic_inc(&cur_trans->use_count);
 
83
                atomic_inc(&cur_trans->num_writers);
 
84
                cur_trans->num_joined++;
 
85
                spin_unlock(&root->fs_info->trans_lock);
 
86
                return 0;
 
87
        }
 
88
        atomic_set(&cur_trans->num_writers, 1);
 
89
        cur_trans->num_joined = 0;
 
90
        init_waitqueue_head(&cur_trans->writer_wait);
 
91
        init_waitqueue_head(&cur_trans->commit_wait);
 
92
        cur_trans->in_commit = 0;
 
93
        cur_trans->blocked = 0;
 
94
        /*
 
95
         * One for this trans handle, one so it will live on until we
 
96
         * commit the transaction.
 
97
         */
 
98
        atomic_set(&cur_trans->use_count, 2);
 
99
        cur_trans->commit_done = 0;
 
100
        cur_trans->start_time = get_seconds();
 
101
 
 
102
        cur_trans->delayed_refs.root = RB_ROOT;
 
103
        cur_trans->delayed_refs.num_entries = 0;
 
104
        cur_trans->delayed_refs.num_heads_ready = 0;
 
105
        cur_trans->delayed_refs.num_heads = 0;
 
106
        cur_trans->delayed_refs.flushing = 0;
 
107
        cur_trans->delayed_refs.run_delayed_start = 0;
 
108
        spin_lock_init(&cur_trans->commit_lock);
 
109
        spin_lock_init(&cur_trans->delayed_refs.lock);
 
110
 
 
111
        INIT_LIST_HEAD(&cur_trans->pending_snapshots);
 
112
        list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
 
113
        extent_io_tree_init(&cur_trans->dirty_pages,
 
114
                             root->fs_info->btree_inode->i_mapping);
 
115
        root->fs_info->generation++;
 
116
        cur_trans->transid = root->fs_info->generation;
 
117
        root->fs_info->running_transaction = cur_trans;
 
118
        spin_unlock(&root->fs_info->trans_lock);
92
119
 
93
120
        return 0;
94
121
}
99
126
 * to make sure the old root from before we joined the transaction is deleted
100
127
 * when the transaction commits
101
128
 */
102
 
static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
103
 
                                         struct btrfs_root *root)
 
129
static int record_root_in_trans(struct btrfs_trans_handle *trans,
 
130
                               struct btrfs_root *root)
104
131
{
105
132
        if (root->ref_cows && root->last_trans < trans->transid) {
106
133
                WARN_ON(root == root->fs_info->extent_root);
107
134
                WARN_ON(root->commit_root != root->node);
108
135
 
 
136
                /*
 
137
                 * see below for in_trans_setup usage rules
 
138
                 * we have the reloc mutex held now, so there
 
139
                 * is only one writer in this function
 
140
                 */
 
141
                root->in_trans_setup = 1;
 
142
 
 
143
                /* make sure readers find in_trans_setup before
 
144
                 * they find our root->last_trans update
 
145
                 */
 
146
                smp_wmb();
 
147
 
 
148
                spin_lock(&root->fs_info->fs_roots_radix_lock);
 
149
                if (root->last_trans == trans->transid) {
 
150
                        spin_unlock(&root->fs_info->fs_roots_radix_lock);
 
151
                        return 0;
 
152
                }
109
153
                radix_tree_tag_set(&root->fs_info->fs_roots_radix,
110
154
                           (unsigned long)root->root_key.objectid,
111
155
                           BTRFS_ROOT_TRANS_TAG);
 
156
                spin_unlock(&root->fs_info->fs_roots_radix_lock);
112
157
                root->last_trans = trans->transid;
 
158
 
 
159
                /* this is pretty tricky.  We don't want to
 
160
                 * take the relocation lock in btrfs_record_root_in_trans
 
161
                 * unless we're really doing the first setup for this root in
 
162
                 * this transaction.
 
163
                 *
 
164
                 * Normally we'd use root->last_trans as a flag to decide
 
165
                 * if we want to take the expensive mutex.
 
166
                 *
 
167
                 * But, we have to set root->last_trans before we
 
168
                 * init the relocation root, otherwise, we trip over warnings
 
169
                 * in ctree.c.  The solution used here is to flag ourselves
 
170
                 * with root->in_trans_setup.  When this is 1, we're still
 
171
                 * fixing up the reloc trees and everyone must wait.
 
172
                 *
 
173
                 * When this is zero, they can trust root->last_trans and fly
 
174
                 * through btrfs_record_root_in_trans without having to take the
 
175
                 * lock.  smp_wmb() makes sure that all the writes above are
 
176
                 * done before we pop in the zero below
 
177
                 */
113
178
                btrfs_init_reloc_root(trans, root);
 
179
                smp_wmb();
 
180
                root->in_trans_setup = 0;
114
181
        }
115
182
        return 0;
116
183
}
117
184
 
 
185
 
118
186
int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
119
187
                               struct btrfs_root *root)
120
188
{
121
189
        if (!root->ref_cows)
122
190
                return 0;
123
191
 
124
 
        mutex_lock(&root->fs_info->trans_mutex);
125
 
        if (root->last_trans == trans->transid) {
126
 
                mutex_unlock(&root->fs_info->trans_mutex);
 
192
        /*
 
193
         * see record_root_in_trans for comments about in_trans_setup usage
 
194
         * and barriers
 
195
         */
 
196
        smp_rmb();
 
197
        if (root->last_trans == trans->transid &&
 
198
            !root->in_trans_setup)
127
199
                return 0;
128
 
        }
129
200
 
 
201
        mutex_lock(&root->fs_info->reloc_mutex);
130
202
        record_root_in_trans(trans, root);
131
 
        mutex_unlock(&root->fs_info->trans_mutex);
 
203
        mutex_unlock(&root->fs_info->reloc_mutex);
 
204
 
132
205
        return 0;
133
206
}
134
207
 
140
213
{
141
214
        struct btrfs_transaction *cur_trans;
142
215
 
 
216
        spin_lock(&root->fs_info->trans_lock);
143
217
        cur_trans = root->fs_info->running_transaction;
144
218
        if (cur_trans && cur_trans->blocked) {
145
219
                DEFINE_WAIT(wait);
146
220
                atomic_inc(&cur_trans->use_count);
 
221
                spin_unlock(&root->fs_info->trans_lock);
147
222
                while (1) {
148
223
                        prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149
224
                                        TASK_UNINTERRUPTIBLE);
150
225
                        if (!cur_trans->blocked)
151
226
                                break;
152
 
                        mutex_unlock(&root->fs_info->trans_mutex);
153
227
                        schedule();
154
 
                        mutex_lock(&root->fs_info->trans_mutex);
155
228
                }
156
229
                finish_wait(&root->fs_info->transaction_wait, &wait);
157
230
                put_transaction(cur_trans);
 
231
        } else {
 
232
                spin_unlock(&root->fs_info->trans_lock);
158
233
        }
159
234
}
160
235
 
167
242
 
168
243
static int may_wait_transaction(struct btrfs_root *root, int type)
169
244
{
170
 
        if (!root->fs_info->log_root_recovering &&
171
 
            ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
172
 
             type == TRANS_USERSPACE))
173
 
                return 1;
 
245
        if (root->fs_info->log_root_recovering)
 
246
                return 0;
 
247
 
 
248
        if (type == TRANS_USERSPACE)
 
249
                return 1;
 
250
 
 
251
        if (type == TRANS_START &&
 
252
            !atomic_read(&root->fs_info->open_ioctl_trans))
 
253
                return 1;
 
254
 
174
255
        return 0;
175
256
}
176
257
 
184
265
 
185
266
        if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
186
267
                return ERR_PTR(-EROFS);
 
268
 
 
269
        if (current->journal_info) {
 
270
                WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
 
271
                h = current->journal_info;
 
272
                h->use_count++;
 
273
                h->orig_rsv = h->block_rsv;
 
274
                h->block_rsv = NULL;
 
275
                goto got_it;
 
276
        }
187
277
again:
188
278
        h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
189
279
        if (!h)
190
280
                return ERR_PTR(-ENOMEM);
191
281
 
192
 
        if (type != TRANS_JOIN_NOLOCK)
193
 
                mutex_lock(&root->fs_info->trans_mutex);
194
282
        if (may_wait_transaction(root, type))
195
283
                wait_current_trans(root);
196
284
 
197
 
        ret = join_transaction(root);
 
285
        do {
 
286
                ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
 
287
                if (ret == -EBUSY)
 
288
                        wait_current_trans(root);
 
289
        } while (ret == -EBUSY);
 
290
 
198
291
        if (ret < 0) {
199
292
                kmem_cache_free(btrfs_trans_handle_cachep, h);
200
 
                if (type != TRANS_JOIN_NOLOCK)
201
 
                        mutex_unlock(&root->fs_info->trans_mutex);
202
293
                return ERR_PTR(ret);
203
294
        }
204
295
 
205
296
        cur_trans = root->fs_info->running_transaction;
206
 
        atomic_inc(&cur_trans->use_count);
207
 
        if (type != TRANS_JOIN_NOLOCK)
208
 
                mutex_unlock(&root->fs_info->trans_mutex);
209
297
 
210
298
        h->transid = cur_trans->transid;
211
299
        h->transaction = cur_trans;
212
300
        h->blocks_used = 0;
213
 
        h->block_group = 0;
214
301
        h->bytes_reserved = 0;
215
302
        h->delayed_ref_updates = 0;
 
303
        h->use_count = 1;
216
304
        h->block_rsv = NULL;
 
305
        h->orig_rsv = NULL;
217
306
 
218
307
        smp_mb();
219
308
        if (cur_trans->blocked && may_wait_transaction(root, type)) {
241
330
                }
242
331
        }
243
332
 
244
 
        if (type != TRANS_JOIN_NOLOCK)
245
 
                mutex_lock(&root->fs_info->trans_mutex);
246
 
        record_root_in_trans(h, root);
247
 
        if (type != TRANS_JOIN_NOLOCK)
248
 
                mutex_unlock(&root->fs_info->trans_mutex);
 
333
got_it:
 
334
        btrfs_record_root_in_trans(h, root);
249
335
 
250
336
        if (!current->journal_info && type != TRANS_USERSPACE)
251
337
                current->journal_info = h;
257
343
{
258
344
        return start_transaction(root, num_items, TRANS_START);
259
345
}
260
 
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
261
 
                                                   int num_blocks)
 
346
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
262
347
{
263
348
        return start_transaction(root, 0, TRANS_JOIN);
264
349
}
265
350
 
266
 
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
267
 
                                                          int num_blocks)
 
351
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
268
352
{
269
353
        return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
270
354
}
271
355
 
272
 
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
273
 
                                                         int num_blocks)
 
356
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
274
357
{
275
 
        return start_transaction(r, 0, TRANS_USERSPACE);
 
358
        return start_transaction(root, 0, TRANS_USERSPACE);
276
359
}
277
360
 
278
361
/* wait for a transaction commit to be fully complete */
280
363
                                    struct btrfs_transaction *commit)
281
364
{
282
365
        DEFINE_WAIT(wait);
283
 
        mutex_lock(&root->fs_info->trans_mutex);
284
366
        while (!commit->commit_done) {
285
367
                prepare_to_wait(&commit->commit_wait, &wait,
286
368
                                TASK_UNINTERRUPTIBLE);
287
369
                if (commit->commit_done)
288
370
                        break;
289
 
                mutex_unlock(&root->fs_info->trans_mutex);
290
371
                schedule();
291
 
                mutex_lock(&root->fs_info->trans_mutex);
292
372
        }
293
 
        mutex_unlock(&root->fs_info->trans_mutex);
294
373
        finish_wait(&commit->commit_wait, &wait);
295
374
        return 0;
296
375
}
300
379
        struct btrfs_transaction *cur_trans = NULL, *t;
301
380
        int ret;
302
381
 
303
 
        mutex_lock(&root->fs_info->trans_mutex);
304
 
 
305
382
        ret = 0;
306
383
        if (transid) {
307
384
                if (transid <= root->fs_info->last_trans_committed)
308
 
                        goto out_unlock;
 
385
                        goto out;
309
386
 
310
387
                /* find specified transaction */
 
388
                spin_lock(&root->fs_info->trans_lock);
311
389
                list_for_each_entry(t, &root->fs_info->trans_list, list) {
312
390
                        if (t->transid == transid) {
313
391
                                cur_trans = t;
 
392
                                atomic_inc(&cur_trans->use_count);
314
393
                                break;
315
394
                        }
316
395
                        if (t->transid > transid)
317
396
                                break;
318
397
                }
 
398
                spin_unlock(&root->fs_info->trans_lock);
319
399
                ret = -EINVAL;
320
400
                if (!cur_trans)
321
 
                        goto out_unlock;  /* bad transid */
 
401
                        goto out;  /* bad transid */
322
402
        } else {
323
403
                /* find newest transaction that is committing | committed */
 
404
                spin_lock(&root->fs_info->trans_lock);
324
405
                list_for_each_entry_reverse(t, &root->fs_info->trans_list,
325
406
                                            list) {
326
407
                        if (t->in_commit) {
327
408
                                if (t->commit_done)
328
 
                                        goto out_unlock;
 
409
                                        break;
329
410
                                cur_trans = t;
 
411
                                atomic_inc(&cur_trans->use_count);
330
412
                                break;
331
413
                        }
332
414
                }
 
415
                spin_unlock(&root->fs_info->trans_lock);
333
416
                if (!cur_trans)
334
 
                        goto out_unlock;  /* nothing committing|committed */
 
417
                        goto out;  /* nothing committing|committed */
335
418
        }
336
419
 
337
 
        atomic_inc(&cur_trans->use_count);
338
 
        mutex_unlock(&root->fs_info->trans_mutex);
339
 
 
340
420
        wait_for_commit(root, cur_trans);
341
421
 
342
 
        mutex_lock(&root->fs_info->trans_mutex);
343
422
        put_transaction(cur_trans);
344
423
        ret = 0;
345
 
out_unlock:
346
 
        mutex_unlock(&root->fs_info->trans_mutex);
 
424
out:
347
425
        return ret;
348
426
}
349
427
 
350
 
#if 0
351
 
/*
352
 
 * rate limit against the drop_snapshot code.  This helps to slow down new
353
 
 * operations if the drop_snapshot code isn't able to keep up.
354
 
 */
355
 
static void throttle_on_drops(struct btrfs_root *root)
356
 
{
357
 
        struct btrfs_fs_info *info = root->fs_info;
358
 
        int harder_count = 0;
359
 
 
360
 
harder:
361
 
        if (atomic_read(&info->throttles)) {
362
 
                DEFINE_WAIT(wait);
363
 
                int thr;
364
 
                thr = atomic_read(&info->throttle_gen);
365
 
 
366
 
                do {
367
 
                        prepare_to_wait(&info->transaction_throttle,
368
 
                                        &wait, TASK_UNINTERRUPTIBLE);
369
 
                        if (!atomic_read(&info->throttles)) {
370
 
                                finish_wait(&info->transaction_throttle, &wait);
371
 
                                break;
372
 
                        }
373
 
                        schedule();
374
 
                        finish_wait(&info->transaction_throttle, &wait);
375
 
                } while (thr == atomic_read(&info->throttle_gen));
376
 
                harder_count++;
377
 
 
378
 
                if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
379
 
                    harder_count < 2)
380
 
                        goto harder;
381
 
 
382
 
                if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
383
 
                    harder_count < 10)
384
 
                        goto harder;
385
 
 
386
 
                if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
387
 
                    harder_count < 20)
388
 
                        goto harder;
389
 
        }
390
 
}
391
 
#endif
392
 
 
393
428
void btrfs_throttle(struct btrfs_root *root)
394
429
{
395
 
        mutex_lock(&root->fs_info->trans_mutex);
396
 
        if (!root->fs_info->open_ioctl_trans)
 
430
        if (!atomic_read(&root->fs_info->open_ioctl_trans))
397
431
                wait_current_trans(root);
398
 
        mutex_unlock(&root->fs_info->trans_mutex);
399
432
}
400
433
 
401
434
static int should_end_transaction(struct btrfs_trans_handle *trans,
413
446
        struct btrfs_transaction *cur_trans = trans->transaction;
414
447
        int updates;
415
448
 
 
449
        smp_mb();
416
450
        if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
417
451
                return 1;
418
452
 
431
465
        struct btrfs_fs_info *info = root->fs_info;
432
466
        int count = 0;
433
467
 
 
468
        if (--trans->use_count) {
 
469
                trans->block_rsv = trans->orig_rsv;
 
470
                return 0;
 
471
        }
 
472
 
434
473
        while (count < 4) {
435
474
                unsigned long cur = trans->delayed_ref_updates;
436
475
                trans->delayed_ref_updates = 0;
453
492
 
454
493
        btrfs_trans_release_metadata(trans, root);
455
494
 
456
 
        if (lock && !root->fs_info->open_ioctl_trans &&
457
 
            should_end_transaction(trans, root))
 
495
        if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
 
496
            should_end_transaction(trans, root)) {
458
497
                trans->transaction->blocked = 1;
 
498
                smp_wmb();
 
499
        }
459
500
 
460
501
        if (lock && cur_trans->blocked && !cur_trans->in_commit) {
461
502
                if (throttle)
487
528
int btrfs_end_transaction(struct btrfs_trans_handle *trans,
488
529
                          struct btrfs_root *root)
489
530
{
490
 
        return __btrfs_end_transaction(trans, root, 0, 1);
 
531
        int ret;
 
532
 
 
533
        ret = __btrfs_end_transaction(trans, root, 0, 1);
 
534
        if (ret)
 
535
                return ret;
 
536
        return 0;
491
537
}
492
538
 
493
539
int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
494
540
                                   struct btrfs_root *root)
495
541
{
 
542
        int ret;
 
543
 
 
544
        ret = __btrfs_end_transaction(trans, root, 1, 1);
 
545
        if (ret)
 
546
                return ret;
 
547
        return 0;
 
548
}
 
549
 
 
550
int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
 
551
                                 struct btrfs_root *root)
 
552
{
 
553
        int ret;
 
554
 
 
555
        ret = __btrfs_end_transaction(trans, root, 0, 0);
 
556
        if (ret)
 
557
                return ret;
 
558
        return 0;
 
559
}
 
560
 
 
561
int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
 
562
                                struct btrfs_root *root)
 
563
{
496
564
        return __btrfs_end_transaction(trans, root, 1, 1);
497
565
}
498
566
 
499
 
int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
500
 
                                 struct btrfs_root *root)
501
 
{
502
 
        return __btrfs_end_transaction(trans, root, 0, 0);
503
 
}
504
 
 
505
567
/*
506
568
 * when btree blocks are allocated, they have some corresponding bits set for
507
569
 * them in one of two extent_io trees.  This is used to make sure all of
725
787
 */
726
788
int btrfs_add_dead_root(struct btrfs_root *root)
727
789
{
728
 
        mutex_lock(&root->fs_info->trans_mutex);
 
790
        spin_lock(&root->fs_info->trans_lock);
729
791
        list_add(&root->root_list, &root->fs_info->dead_roots);
730
 
        mutex_unlock(&root->fs_info->trans_mutex);
 
792
        spin_unlock(&root->fs_info->trans_lock);
731
793
        return 0;
732
794
}
733
795
 
743
805
        int ret;
744
806
        int err = 0;
745
807
 
 
808
        spin_lock(&fs_info->fs_roots_radix_lock);
746
809
        while (1) {
747
810
                ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
748
811
                                                 (void **)gang, 0,
755
818
                        radix_tree_tag_clear(&fs_info->fs_roots_radix,
756
819
                                        (unsigned long)root->root_key.objectid,
757
820
                                        BTRFS_ROOT_TRANS_TAG);
 
821
                        spin_unlock(&fs_info->fs_roots_radix_lock);
758
822
 
759
823
                        btrfs_free_log(trans, root);
760
824
                        btrfs_update_reloc_root(trans, root);
761
825
                        btrfs_orphan_commit_root(trans, root);
762
826
 
 
827
                        btrfs_save_ino_cache(root, trans);
 
828
 
763
829
                        if (root->commit_root != root->node) {
 
830
                                mutex_lock(&root->fs_commit_mutex);
764
831
                                switch_commit_root(root);
 
832
                                btrfs_unpin_free_ino(root);
 
833
                                mutex_unlock(&root->fs_commit_mutex);
 
834
 
765
835
                                btrfs_set_root_node(&root->root_item,
766
836
                                                    root->node);
767
837
                        }
769
839
                        err = btrfs_update_root(trans, fs_info->tree_root,
770
840
                                                &root->root_key,
771
841
                                                &root->root_item);
 
842
                        spin_lock(&fs_info->fs_roots_radix_lock);
772
843
                        if (err)
773
844
                                break;
774
845
                }
775
846
        }
 
847
        spin_unlock(&fs_info->fs_roots_radix_lock);
776
848
        return err;
777
849
}
778
850
 
802
874
                btrfs_btree_balance_dirty(info->tree_root, nr);
803
875
                cond_resched();
804
876
 
805
 
                if (root->fs_info->closing || ret != -EAGAIN)
 
877
                if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
806
878
                        break;
807
879
        }
808
880
        root->defrag_running = 0;
809
881
        return ret;
810
882
}
811
883
 
812
 
#if 0
813
 
/*
814
 
 * when dropping snapshots, we generate a ton of delayed refs, and it makes
815
 
 * sense not to join the transaction while it is trying to flush the current
816
 
 * queue of delayed refs out.
817
 
 *
818
 
 * This is used by the drop snapshot code only
819
 
 */
820
 
static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
821
 
{
822
 
        DEFINE_WAIT(wait);
823
 
 
824
 
        mutex_lock(&info->trans_mutex);
825
 
        while (info->running_transaction &&
826
 
               info->running_transaction->delayed_refs.flushing) {
827
 
                prepare_to_wait(&info->transaction_wait, &wait,
828
 
                                TASK_UNINTERRUPTIBLE);
829
 
                mutex_unlock(&info->trans_mutex);
830
 
 
831
 
                schedule();
832
 
 
833
 
                mutex_lock(&info->trans_mutex);
834
 
                finish_wait(&info->transaction_wait, &wait);
835
 
        }
836
 
        mutex_unlock(&info->trans_mutex);
837
 
        return 0;
838
 
}
839
 
 
840
 
/*
841
 
 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
842
 
 * all of them
843
 
 */
844
 
int btrfs_drop_dead_root(struct btrfs_root *root)
845
 
{
846
 
        struct btrfs_trans_handle *trans;
847
 
        struct btrfs_root *tree_root = root->fs_info->tree_root;
848
 
        unsigned long nr;
849
 
        int ret;
850
 
 
851
 
        while (1) {
852
 
                /*
853
 
                 * we don't want to jump in and create a bunch of
854
 
                 * delayed refs if the transaction is starting to close
855
 
                 */
856
 
                wait_transaction_pre_flush(tree_root->fs_info);
857
 
                trans = btrfs_start_transaction(tree_root, 1);
858
 
 
859
 
                /*
860
 
                 * we've joined a transaction, make sure it isn't
861
 
                 * closing right now
862
 
                 */
863
 
                if (trans->transaction->delayed_refs.flushing) {
864
 
                        btrfs_end_transaction(trans, tree_root);
865
 
                        continue;
866
 
                }
867
 
 
868
 
                ret = btrfs_drop_snapshot(trans, root);
869
 
                if (ret != -EAGAIN)
870
 
                        break;
871
 
 
872
 
                ret = btrfs_update_root(trans, tree_root,
873
 
                                        &root->root_key,
874
 
                                        &root->root_item);
875
 
                if (ret)
876
 
                        break;
877
 
 
878
 
                nr = trans->blocks_used;
879
 
                ret = btrfs_end_transaction(trans, tree_root);
880
 
                BUG_ON(ret);
881
 
 
882
 
                btrfs_btree_balance_dirty(tree_root, nr);
883
 
                cond_resched();
884
 
        }
885
 
        BUG_ON(ret);
886
 
 
887
 
        ret = btrfs_del_root(trans, tree_root, &root->root_key);
888
 
        BUG_ON(ret);
889
 
 
890
 
        nr = trans->blocks_used;
891
 
        ret = btrfs_end_transaction(trans, tree_root);
892
 
        BUG_ON(ret);
893
 
 
894
 
        free_extent_buffer(root->node);
895
 
        free_extent_buffer(root->commit_root);
896
 
        kfree(root);
897
 
 
898
 
        btrfs_btree_balance_dirty(tree_root, nr);
899
 
        return ret;
900
 
}
901
 
#endif
902
 
 
903
884
/*
904
885
 * new snapshots need to be created at a very specific time in the
905
886
 * transaction commit.  This does the actual creation
930
911
                goto fail;
931
912
        }
932
913
 
933
 
        ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
 
914
        ret = btrfs_find_free_objectid(tree_root, &objectid);
934
915
        if (ret) {
935
916
                pending->error = ret;
936
917
                goto fail;
967
948
        BUG_ON(ret);
968
949
        ret = btrfs_insert_dir_item(trans, parent_root,
969
950
                                dentry->d_name.name, dentry->d_name.len,
970
 
                                parent_inode->i_ino, &key,
 
951
                                parent_inode, &key,
971
952
                                BTRFS_FT_DIR, index);
972
953
        BUG_ON(ret);
973
954
 
976
957
        ret = btrfs_update_inode(trans, parent_root, parent_inode);
977
958
        BUG_ON(ret);
978
959
 
 
960
        /*
 
961
         * pull in the delayed directory update
 
962
         * and the delayed inode item
 
963
         * otherwise we corrupt the FS during
 
964
         * snapshot
 
965
         */
 
966
        ret = btrfs_run_delayed_items(trans, root);
 
967
        BUG_ON(ret);
 
968
 
979
969
        record_root_in_trans(trans, root);
980
970
        btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
981
971
        memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1009
999
         */
1010
1000
        ret = btrfs_add_root_ref(trans, tree_root, objectid,
1011
1001
                                 parent_root->root_key.objectid,
1012
 
                                 parent_inode->i_ino, index,
 
1002
                                 btrfs_ino(parent_inode), index,
1013
1003
                                 dentry->d_name.name, dentry->d_name.len);
1014
1004
        BUG_ON(ret);
1015
1005
        dput(parent);
1066
1056
int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1067
1057
{
1068
1058
        int ret = 0;
1069
 
        spin_lock(&info->new_trans_lock);
 
1059
        spin_lock(&info->trans_lock);
1070
1060
        if (info->running_transaction)
1071
1061
                ret = info->running_transaction->in_commit;
1072
 
        spin_unlock(&info->new_trans_lock);
 
1062
        spin_unlock(&info->trans_lock);
1073
1063
        return ret;
1074
1064
}
1075
1065
 
1076
1066
int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1077
1067
{
1078
1068
        int ret = 0;
1079
 
        spin_lock(&info->new_trans_lock);
 
1069
        spin_lock(&info->trans_lock);
1080
1070
        if (info->running_transaction)
1081
1071
                ret = info->running_transaction->blocked;
1082
 
        spin_unlock(&info->new_trans_lock);
 
1072
        spin_unlock(&info->trans_lock);
1083
1073
        return ret;
1084
1074
}
1085
1075
 
1103
1093
                                    &wait);
1104
1094
                        break;
1105
1095
                }
1106
 
                mutex_unlock(&root->fs_info->trans_mutex);
1107
1096
                schedule();
1108
 
                mutex_lock(&root->fs_info->trans_mutex);
1109
1097
                finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1110
1098
        }
1111
1099
}
1131
1119
                                    &wait);
1132
1120
                        break;
1133
1121
                }
1134
 
                mutex_unlock(&root->fs_info->trans_mutex);
1135
1122
                schedule();
1136
 
                mutex_lock(&root->fs_info->trans_mutex);
1137
1123
                finish_wait(&root->fs_info->transaction_wait,
1138
1124
                            &wait);
1139
1125
        }
1171
1157
 
1172
1158
        INIT_DELAYED_WORK(&ac->work, do_async_commit);
1173
1159
        ac->root = root;
1174
 
        ac->newtrans = btrfs_join_transaction(root, 0);
 
1160
        ac->newtrans = btrfs_join_transaction(root);
1175
1161
        if (IS_ERR(ac->newtrans)) {
1176
1162
                int err = PTR_ERR(ac->newtrans);
1177
1163
                kfree(ac);
1179
1165
        }
1180
1166
 
1181
1167
        /* take transaction reference */
1182
 
        mutex_lock(&root->fs_info->trans_mutex);
1183
1168
        cur_trans = trans->transaction;
1184
1169
        atomic_inc(&cur_trans->use_count);
1185
 
        mutex_unlock(&root->fs_info->trans_mutex);
1186
1170
 
1187
1171
        btrfs_end_transaction(trans, root);
1188
1172
        schedule_delayed_work(&ac->work, 0);
1189
1173
 
1190
1174
        /* wait for transaction to start and unblock */
1191
 
        mutex_lock(&root->fs_info->trans_mutex);
1192
1175
        if (wait_for_unblock)
1193
1176
                wait_current_trans_commit_start_and_unblock(root, cur_trans);
1194
1177
        else
1195
1178
                wait_current_trans_commit_start(root, cur_trans);
 
1179
 
 
1180
        if (current->journal_info == trans)
 
1181
                current->journal_info = NULL;
 
1182
 
1196
1183
        put_transaction(cur_trans);
1197
 
        mutex_unlock(&root->fs_info->trans_mutex);
1198
 
 
1199
1184
        return 0;
1200
1185
}
1201
1186
 
1238
1223
        ret = btrfs_run_delayed_refs(trans, root, 0);
1239
1224
        BUG_ON(ret);
1240
1225
 
1241
 
        mutex_lock(&root->fs_info->trans_mutex);
 
1226
        spin_lock(&cur_trans->commit_lock);
1242
1227
        if (cur_trans->in_commit) {
 
1228
                spin_unlock(&cur_trans->commit_lock);
1243
1229
                atomic_inc(&cur_trans->use_count);
1244
 
                mutex_unlock(&root->fs_info->trans_mutex);
1245
1230
                btrfs_end_transaction(trans, root);
1246
1231
 
1247
1232
                ret = wait_for_commit(root, cur_trans);
1248
1233
                BUG_ON(ret);
1249
1234
 
1250
 
                mutex_lock(&root->fs_info->trans_mutex);
1251
1235
                put_transaction(cur_trans);
1252
 
                mutex_unlock(&root->fs_info->trans_mutex);
1253
1236
 
1254
1237
                return 0;
1255
1238
        }
1256
1239
 
1257
1240
        trans->transaction->in_commit = 1;
1258
1241
        trans->transaction->blocked = 1;
 
1242
        spin_unlock(&cur_trans->commit_lock);
1259
1243
        wake_up(&root->fs_info->transaction_blocked_wait);
1260
1244
 
 
1245
        spin_lock(&root->fs_info->trans_lock);
1261
1246
        if (cur_trans->list.prev != &root->fs_info->trans_list) {
1262
1247
                prev_trans = list_entry(cur_trans->list.prev,
1263
1248
                                        struct btrfs_transaction, list);
1264
1249
                if (!prev_trans->commit_done) {
1265
1250
                        atomic_inc(&prev_trans->use_count);
1266
 
                        mutex_unlock(&root->fs_info->trans_mutex);
 
1251
                        spin_unlock(&root->fs_info->trans_lock);
1267
1252
 
1268
1253
                        wait_for_commit(root, prev_trans);
1269
1254
 
1270
 
                        mutex_lock(&root->fs_info->trans_mutex);
1271
1255
                        put_transaction(prev_trans);
 
1256
                } else {
 
1257
                        spin_unlock(&root->fs_info->trans_lock);
1272
1258
                }
 
1259
        } else {
 
1260
                spin_unlock(&root->fs_info->trans_lock);
1273
1261
        }
1274
1262
 
1275
1263
        if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1277
1265
 
1278
1266
        do {
1279
1267
                int snap_pending = 0;
 
1268
 
1280
1269
                joined = cur_trans->num_joined;
1281
1270
                if (!list_empty(&trans->transaction->pending_snapshots))
1282
1271
                        snap_pending = 1;
1283
1272
 
1284
1273
                WARN_ON(cur_trans != trans->transaction);
1285
 
                mutex_unlock(&root->fs_info->trans_mutex);
1286
1274
 
1287
1275
                if (flush_on_commit || snap_pending) {
1288
1276
                        btrfs_start_delalloc_inodes(root, 1);
1290
1278
                        BUG_ON(ret);
1291
1279
                }
1292
1280
 
 
1281
                ret = btrfs_run_delayed_items(trans, root);
 
1282
                BUG_ON(ret);
 
1283
 
1293
1284
                /*
1294
1285
                 * rename don't use btrfs_join_transaction, so, once we
1295
1286
                 * set the transaction to blocked above, we aren't going
1302
1293
                prepare_to_wait(&cur_trans->writer_wait, &wait,
1303
1294
                                TASK_UNINTERRUPTIBLE);
1304
1295
 
1305
 
                smp_mb();
1306
1296
                if (atomic_read(&cur_trans->num_writers) > 1)
1307
1297
                        schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1308
1298
                else if (should_grow)
1309
1299
                        schedule_timeout(1);
1310
1300
 
1311
 
                mutex_lock(&root->fs_info->trans_mutex);
1312
1301
                finish_wait(&cur_trans->writer_wait, &wait);
1313
1302
        } while (atomic_read(&cur_trans->num_writers) > 1 ||
1314
1303
                 (should_grow && cur_trans->num_joined != joined));
1315
1304
 
 
1305
        /*
 
1306
         * Ok now we need to make sure to block out any other joins while we
 
1307
         * commit the transaction.  We could have started a join before setting
 
1308
         * no_join so make sure to wait for num_writers to == 1 again.
 
1309
         */
 
1310
        spin_lock(&root->fs_info->trans_lock);
 
1311
        root->fs_info->trans_no_join = 1;
 
1312
        spin_unlock(&root->fs_info->trans_lock);
 
1313
        wait_event(cur_trans->writer_wait,
 
1314
                   atomic_read(&cur_trans->num_writers) == 1);
 
1315
 
 
1316
        /*
 
1317
         * the reloc mutex makes sure that we stop
 
1318
         * the balancing code from coming in and moving
 
1319
         * extents around in the middle of the commit
 
1320
         */
 
1321
        mutex_lock(&root->fs_info->reloc_mutex);
 
1322
 
 
1323
        ret = btrfs_run_delayed_items(trans, root);
 
1324
        BUG_ON(ret);
 
1325
 
1316
1326
        ret = create_pending_snapshots(trans, root->fs_info);
1317
1327
        BUG_ON(ret);
1318
1328
 
1319
1329
        ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1320
1330
        BUG_ON(ret);
1321
1331
 
 
1332
        /*
 
1333
         * make sure none of the code above managed to slip in a
 
1334
         * delayed item
 
1335
         */
 
1336
        btrfs_assert_delayed_root_empty(root);
 
1337
 
1322
1338
        WARN_ON(cur_trans != trans->transaction);
1323
1339
 
 
1340
        btrfs_scrub_pause(root);
1324
1341
        /* btrfs_commit_tree_roots is responsible for getting the
1325
1342
         * various roots consistent with each other.  Every pointer
1326
1343
         * in the tree of tree roots has to point to the most up to date
1350
1367
        btrfs_prepare_extent_commit(trans, root);
1351
1368
 
1352
1369
        cur_trans = root->fs_info->running_transaction;
1353
 
        spin_lock(&root->fs_info->new_trans_lock);
1354
 
        root->fs_info->running_transaction = NULL;
1355
 
        spin_unlock(&root->fs_info->new_trans_lock);
1356
1370
 
1357
1371
        btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1358
1372
                            root->fs_info->tree_root->node);
1373
1387
               sizeof(root->fs_info->super_copy));
1374
1388
 
1375
1389
        trans->transaction->blocked = 0;
 
1390
        spin_lock(&root->fs_info->trans_lock);
 
1391
        root->fs_info->running_transaction = NULL;
 
1392
        root->fs_info->trans_no_join = 0;
 
1393
        spin_unlock(&root->fs_info->trans_lock);
 
1394
        mutex_unlock(&root->fs_info->reloc_mutex);
1376
1395
 
1377
1396
        wake_up(&root->fs_info->transaction_wait);
1378
1397
 
1379
 
        mutex_unlock(&root->fs_info->trans_mutex);
1380
1398
        ret = btrfs_write_and_wait_transaction(trans, root);
1381
1399
        BUG_ON(ret);
1382
1400
        write_ctree_super(trans, root, 0);
1389
1407
 
1390
1408
        btrfs_finish_extent_commit(trans, root);
1391
1409
 
1392
 
        mutex_lock(&root->fs_info->trans_mutex);
1393
 
 
1394
1410
        cur_trans->commit_done = 1;
1395
1411
 
1396
1412
        root->fs_info->last_trans_committed = cur_trans->transid;
1397
1413
 
1398
1414
        wake_up(&cur_trans->commit_wait);
1399
1415
 
 
1416
        spin_lock(&root->fs_info->trans_lock);
1400
1417
        list_del_init(&cur_trans->list);
 
1418
        spin_unlock(&root->fs_info->trans_lock);
 
1419
 
1401
1420
        put_transaction(cur_trans);
1402
1421
        put_transaction(cur_trans);
1403
1422
 
1404
1423
        trace_btrfs_transaction_commit(root);
1405
1424
 
1406
 
        mutex_unlock(&root->fs_info->trans_mutex);
 
1425
        btrfs_scrub_continue(root);
1407
1426
 
1408
1427
        if (current->journal_info == trans)
1409
1428
                current->journal_info = NULL;
1424
1443
        LIST_HEAD(list);
1425
1444
        struct btrfs_fs_info *fs_info = root->fs_info;
1426
1445
 
1427
 
        mutex_lock(&fs_info->trans_mutex);
 
1446
        spin_lock(&fs_info->trans_lock);
1428
1447
        list_splice_init(&fs_info->dead_roots, &list);
1429
 
        mutex_unlock(&fs_info->trans_mutex);
 
1448
        spin_unlock(&fs_info->trans_lock);
1430
1449
 
1431
1450
        while (!list_empty(&list)) {
1432
1451
                root = list_entry(list.next, struct btrfs_root, root_list);
1433
1452
                list_del(&root->root_list);
1434
1453
 
 
1454
                btrfs_kill_all_delayed_nodes(root);
 
1455
 
1435
1456
                if (btrfs_header_backref_rev(root->node) <
1436
1457
                    BTRFS_MIXED_BACKREF_REV)
1437
1458
                        btrfs_drop_snapshot(root, NULL, 0);