Btrfs: Audit callers and return codes to make sure -ENOSPC gets up the stack
[linux-2.6-block.git] / fs / btrfs / transaction.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
79154b1b
CM
19#include <linux/module.h>
20#include <linux/fs.h>
34088780 21#include <linux/sched.h>
79154b1b
CM
22#include "ctree.h"
23#include "disk-io.h"
24#include "transaction.h"
25
78fae27e 26static int total_trans = 0;
2c90e5d6
CM
27extern struct kmem_cache *btrfs_trans_handle_cachep;
28extern struct kmem_cache *btrfs_transaction_cachep;
29
08607c1b
CM
30static struct workqueue_struct *trans_wq;
31
0f7d52f4
CM
32#define BTRFS_ROOT_TRANS_TAG 0
33
79154b1b
CM
34static void put_transaction(struct btrfs_transaction *transaction)
35{
2c90e5d6 36 WARN_ON(transaction->use_count == 0);
79154b1b 37 transaction->use_count--;
78fae27e
CM
38 if (transaction->use_count == 0) {
39 WARN_ON(total_trans == 0);
40 total_trans--;
8fd17795 41 list_del_init(&transaction->list);
2c90e5d6
CM
42 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 44 }
79154b1b
CM
45}
46
47static int join_transaction(struct btrfs_root *root)
48{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
2c90e5d6
CM
52 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
78fae27e 54 total_trans++;
79154b1b 55 BUG_ON(!cur_trans);
0f7d52f4 56 root->fs_info->generation++;
79154b1b
CM
57 root->fs_info->running_transaction = cur_trans;
58 cur_trans->num_writers = 0;
0f7d52f4 59 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
60 init_waitqueue_head(&cur_trans->writer_wait);
61 init_waitqueue_head(&cur_trans->commit_wait);
62 cur_trans->in_commit = 0;
d5719762 63 cur_trans->use_count = 1;
79154b1b 64 cur_trans->commit_done = 0;
08607c1b 65 cur_trans->start_time = get_seconds();
8fd17795 66 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
7c4452b9 67 init_bit_radix(&cur_trans->dirty_pages);
79154b1b
CM
68 }
69 cur_trans->num_writers++;
70 return 0;
71}
72
73struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
74 int num_blocks)
75{
2c90e5d6
CM
76 struct btrfs_trans_handle *h =
77 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b 78 int ret;
0f7d52f4 79 u64 running_trans_id;
79154b1b
CM
80
81 mutex_lock(&root->fs_info->trans_mutex);
82 ret = join_transaction(root);
83 BUG_ON(ret);
0f7d52f4
CM
84 running_trans_id = root->fs_info->running_transaction->transid;
85
86 if (root != root->fs_info->tree_root && root->last_trans <
87 running_trans_id) {
88 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2619ba1f
CM
89 (unsigned long)root->root_key.objectid,
90 BTRFS_ROOT_TRANS_TAG);
0f7d52f4
CM
91 root->commit_root = root->node;
92 get_bh(root->node);
93 }
94 root->last_trans = running_trans_id;
95 h->transid = running_trans_id;
79154b1b
CM
96 h->transaction = root->fs_info->running_transaction;
97 h->blocks_reserved = num_blocks;
98 h->blocks_used = 0;
31f3c99b 99 h->block_group = NULL;
79154b1b
CM
100 root->fs_info->running_transaction->use_count++;
101 mutex_unlock(&root->fs_info->trans_mutex);
102 return h;
103}
104
105int btrfs_end_transaction(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root)
107{
108 struct btrfs_transaction *cur_trans;
d6e4a428 109
79154b1b
CM
110 mutex_lock(&root->fs_info->trans_mutex);
111 cur_trans = root->fs_info->running_transaction;
d5719762 112 WARN_ON(cur_trans->num_writers < 1);
79154b1b
CM
113 if (waitqueue_active(&cur_trans->writer_wait))
114 wake_up(&cur_trans->writer_wait);
115 cur_trans->num_writers--;
116 put_transaction(cur_trans);
117 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 118 memset(trans, 0, sizeof(*trans));
2c90e5d6 119 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
120 return 0;
121}
122
123
124int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
125 struct btrfs_root *root)
126{
7c4452b9
CM
127 unsigned long gang[16];
128 int ret;
129 int i;
130 int err;
131 int werr = 0;
132 struct page *page;
133 struct radix_tree_root *dirty_pages;
134 struct inode *btree_inode = root->fs_info->btree_inode;
135
136 if (!trans || !trans->transaction) {
137 return filemap_write_and_wait(btree_inode->i_mapping);
138 }
139 dirty_pages = &trans->transaction->dirty_pages;
140 while(1) {
e37c9e69
CM
141 ret = find_first_radix_bit(dirty_pages, gang,
142 0, ARRAY_SIZE(gang));
7c4452b9
CM
143 if (!ret)
144 break;
145 for (i = 0; i < ret; i++) {
146 /* FIXME EIO */
147 clear_radix_bit(dirty_pages, gang[i]);
148 page = find_lock_page(btree_inode->i_mapping,
149 gang[i]);
150 if (!page)
151 continue;
152 err = write_one_page(page, 0);
153 if (err)
154 werr = err;
155 page_cache_release(page);
156 }
157 }
158 err = filemap_fdatawait(btree_inode->i_mapping);
159 if (err)
160 werr = err;
161 return werr;
79154b1b
CM
162}
163
164int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
165 struct btrfs_root *root)
166{
167 int ret;
168 u64 old_extent_block;
169 struct btrfs_fs_info *fs_info = root->fs_info;
170 struct btrfs_root *tree_root = fs_info->tree_root;
171 struct btrfs_root *extent_root = fs_info->extent_root;
79154b1b 172
9078a3e1 173 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
174 while(1) {
175 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
7eccb903 176 if (old_extent_block == bh_blocknr(extent_root->node))
79154b1b
CM
177 break;
178 btrfs_set_root_blocknr(&extent_root->root_item,
7eccb903 179 bh_blocknr(extent_root->node));
79154b1b
CM
180 ret = btrfs_update_root(trans, tree_root,
181 &extent_root->root_key,
182 &extent_root->root_item);
183 BUG_ON(ret);
9078a3e1 184 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
185 }
186 return 0;
187}
188
189static int wait_for_commit(struct btrfs_root *root,
190 struct btrfs_transaction *commit)
191{
192 DEFINE_WAIT(wait);
79154b1b
CM
193 while(!commit->commit_done) {
194 prepare_to_wait(&commit->commit_wait, &wait,
195 TASK_UNINTERRUPTIBLE);
196 if (commit->commit_done)
197 break;
198 mutex_unlock(&root->fs_info->trans_mutex);
199 schedule();
200 mutex_lock(&root->fs_info->trans_mutex);
201 }
202 finish_wait(&commit->commit_wait, &wait);
203 return 0;
204}
205
0f7d52f4
CM
206struct dirty_root {
207 struct list_head list;
208 struct btrfs_key snap_key;
209 struct buffer_head *commit_root;
210 struct btrfs_root *root;
211};
212
35b7e476
CM
213static int add_dirty_roots(struct btrfs_trans_handle *trans,
214 struct radix_tree_root *radix,
215 struct list_head *list)
0f7d52f4
CM
216{
217 struct dirty_root *dirty;
218 struct btrfs_root *gang[8];
219 struct btrfs_root *root;
220 int i;
221 int ret;
54aa1f4d
CM
222 int err = 0;
223
0f7d52f4
CM
224 while(1) {
225 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
226 ARRAY_SIZE(gang),
227 BTRFS_ROOT_TRANS_TAG);
228 if (ret == 0)
229 break;
230 for (i = 0; i < ret; i++) {
231 root = gang[i];
2619ba1f
CM
232 radix_tree_tag_clear(radix,
233 (unsigned long)root->root_key.objectid,
234 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 235 if (root->commit_root == root->node) {
7eccb903 236 WARN_ON(bh_blocknr(root->node) !=
0f7d52f4
CM
237 btrfs_root_blocknr(&root->root_item));
238 brelse(root->commit_root);
239 root->commit_root = NULL;
240 continue;
241 }
242 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
243 BUG_ON(!dirty);
244 memcpy(&dirty->snap_key, &root->root_key,
245 sizeof(root->root_key));
246 dirty->commit_root = root->commit_root;
247 root->commit_root = NULL;
248 dirty->root = root;
0f7d52f4
CM
249 root->root_key.offset = root->fs_info->generation;
250 btrfs_set_root_blocknr(&root->root_item,
7eccb903 251 bh_blocknr(root->node));
0f7d52f4
CM
252 err = btrfs_insert_root(trans, root->fs_info->tree_root,
253 &root->root_key,
254 &root->root_item);
54aa1f4d
CM
255 if (err)
256 break;
0f7d52f4
CM
257 list_add(&dirty->list, list);
258 }
259 }
54aa1f4d 260 return err;
0f7d52f4
CM
261}
262
35b7e476
CM
263static int drop_dirty_roots(struct btrfs_root *tree_root,
264 struct list_head *list)
0f7d52f4
CM
265{
266 struct dirty_root *dirty;
267 struct btrfs_trans_handle *trans;
54aa1f4d 268 int ret = 0;
0f7d52f4 269 while(!list_empty(list)) {
facda1e7 270 mutex_lock(&tree_root->fs_info->fs_mutex);
0f7d52f4
CM
271 dirty = list_entry(list->next, struct dirty_root, list);
272 list_del_init(&dirty->list);
273 trans = btrfs_start_transaction(tree_root, 1);
0f7d52f4
CM
274 ret = btrfs_drop_snapshot(trans, dirty->root,
275 dirty->commit_root);
276 BUG_ON(ret);
277
0f7d52f4 278 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
54aa1f4d
CM
279 if (ret)
280 break;
0f7d52f4
CM
281 ret = btrfs_end_transaction(trans, tree_root);
282 BUG_ON(ret);
283 kfree(dirty);
facda1e7 284 mutex_unlock(&tree_root->fs_info->fs_mutex);
8c2383c3 285 btrfs_btree_balance_dirty(tree_root);
0f7d52f4 286 }
54aa1f4d 287 return ret;
0f7d52f4
CM
288}
289
79154b1b
CM
290int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
291 struct btrfs_root *root)
292{
293 int ret = 0;
79154b1b 294 struct btrfs_transaction *cur_trans;
8fd17795 295 struct btrfs_transaction *prev_trans = NULL;
0f7d52f4 296 struct list_head dirty_fs_roots;
79154b1b
CM
297 DEFINE_WAIT(wait);
298
0f7d52f4 299 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 300
79154b1b
CM
301 mutex_lock(&root->fs_info->trans_mutex);
302 if (trans->transaction->in_commit) {
303 cur_trans = trans->transaction;
304 trans->transaction->use_count++;
305 btrfs_end_transaction(trans, root);
306 ret = wait_for_commit(root, cur_trans);
307 BUG_ON(ret);
308 put_transaction(cur_trans);
309 mutex_unlock(&root->fs_info->trans_mutex);
310 return 0;
311 }
2c90e5d6
CM
312 cur_trans = trans->transaction;
313 trans->transaction->in_commit = 1;
79154b1b 314 while (trans->transaction->num_writers > 1) {
2c90e5d6 315 WARN_ON(cur_trans != trans->transaction);
79154b1b
CM
316 prepare_to_wait(&trans->transaction->writer_wait, &wait,
317 TASK_UNINTERRUPTIBLE);
318 if (trans->transaction->num_writers <= 1)
319 break;
320 mutex_unlock(&root->fs_info->trans_mutex);
321 schedule();
322 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6 323 finish_wait(&trans->transaction->writer_wait, &wait);
79154b1b
CM
324 }
325 finish_wait(&trans->transaction->writer_wait, &wait);
2c90e5d6 326 WARN_ON(cur_trans != trans->transaction);
54aa1f4d
CM
327 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
328 &dirty_fs_roots);
329 BUG_ON(ret);
330
79154b1b
CM
331 ret = btrfs_commit_tree_roots(trans, root);
332 BUG_ON(ret);
54aa1f4d 333
78fae27e
CM
334 cur_trans = root->fs_info->running_transaction;
335 root->fs_info->running_transaction = NULL;
8fd17795
CM
336 if (cur_trans->list.prev != &root->fs_info->trans_list) {
337 prev_trans = list_entry(cur_trans->list.prev,
338 struct btrfs_transaction, list);
339 if (prev_trans->commit_done)
340 prev_trans = NULL;
341 else
342 prev_trans->use_count++;
343 }
78fae27e 344 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 345 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b 346 ret = btrfs_write_and_wait_transaction(trans, root);
8fd17795
CM
347 if (prev_trans) {
348 mutex_lock(&root->fs_info->trans_mutex);
349 wait_for_commit(root, prev_trans);
350 put_transaction(prev_trans);
351 mutex_unlock(&root->fs_info->trans_mutex);
352 }
353 btrfs_set_super_generation(root->fs_info->disk_super,
354 cur_trans->transid);
79154b1b 355 BUG_ON(ret);
79154b1b 356 write_ctree_super(trans, root);
8fd17795
CM
357
358 mutex_lock(&root->fs_info->fs_mutex);
78fae27e
CM
359 btrfs_finish_extent_commit(trans, root);
360 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6
CM
361 cur_trans->commit_done = 1;
362 wake_up(&cur_trans->commit_wait);
78fae27e 363 put_transaction(cur_trans);
79154b1b 364 put_transaction(cur_trans);
facda1e7
CM
365 if (root->fs_info->closing)
366 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
367 else
368 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
78fae27e 369 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 370 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 371
facda1e7
CM
372 if (root->fs_info->closing) {
373 mutex_unlock(&root->fs_info->fs_mutex);
374 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
375 mutex_lock(&root->fs_info->fs_mutex);
376 }
79154b1b
CM
377 return ret;
378}
379
08607c1b
CM
380void btrfs_transaction_cleaner(struct work_struct *work)
381{
382 struct btrfs_fs_info *fs_info = container_of(work,
383 struct btrfs_fs_info,
384 trans_work.work);
385
386 struct btrfs_root *root = fs_info->tree_root;
387 struct btrfs_transaction *cur;
388 struct btrfs_trans_handle *trans;
facda1e7 389 struct list_head dirty_roots;
08607c1b
CM
390 unsigned long now;
391 unsigned long delay = HZ * 30;
392 int ret;
393
facda1e7 394 INIT_LIST_HEAD(&dirty_roots);
08607c1b
CM
395 mutex_lock(&root->fs_info->fs_mutex);
396 mutex_lock(&root->fs_info->trans_mutex);
397 cur = root->fs_info->running_transaction;
398 if (!cur) {
399 mutex_unlock(&root->fs_info->trans_mutex);
400 goto out;
401 }
402 now = get_seconds();
403 if (now < cur->start_time || now - cur->start_time < 30) {
404 mutex_unlock(&root->fs_info->trans_mutex);
405 delay = HZ * 5;
406 goto out;
407 }
408 mutex_unlock(&root->fs_info->trans_mutex);
08607c1b
CM
409 trans = btrfs_start_transaction(root, 1);
410 ret = btrfs_commit_transaction(trans, root);
411out:
412 mutex_unlock(&root->fs_info->fs_mutex);
ad693af6
CM
413
414 mutex_lock(&root->fs_info->trans_mutex);
415 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
416 mutex_unlock(&root->fs_info->trans_mutex);
417
418 if (!list_empty(&dirty_roots)) {
419 drop_dirty_roots(root, &dirty_roots);
420 }
08607c1b
CM
421 btrfs_transaction_queue_work(root, delay);
422}
423
424void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
425{
426 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
427}
428
429void btrfs_transaction_flush_work(struct btrfs_root *root)
430{
431 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
432 flush_workqueue(trans_wq);
433}
434
435void __init btrfs_init_transaction_sys(void)
436{
437 trans_wq = create_workqueue("btrfs");
438}
439
440void __exit btrfs_exit_transaction_sys(void)
441{
442 destroy_workqueue(trans_wq);
443}
444