Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 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 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 21 | #include <linux/writeback.h> |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "disk-io.h" |
| 25 | #include "transaction.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame^] | 26 | #include "locking.h" |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 27 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 28 | static int total_trans = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 29 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
| 30 | extern struct kmem_cache *btrfs_transaction_cachep; |
| 31 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 32 | static struct workqueue_struct *trans_wq; |
| 33 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 34 | #define BTRFS_ROOT_TRANS_TAG 0 |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 35 | #define BTRFS_ROOT_DEFRAG_TAG 1 |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 36 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 37 | static noinline void put_transaction(struct btrfs_transaction *transaction) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 38 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 39 | WARN_ON(transaction->use_count == 0); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 40 | transaction->use_count--; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 41 | if (transaction->use_count == 0) { |
| 42 | WARN_ON(total_trans == 0); |
| 43 | total_trans--; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 44 | list_del_init(&transaction->list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 45 | memset(transaction, 0, sizeof(*transaction)); |
| 46 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 47 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 50 | static noinline int join_transaction(struct btrfs_root *root) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 51 | { |
| 52 | struct btrfs_transaction *cur_trans; |
| 53 | cur_trans = root->fs_info->running_transaction; |
| 54 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 55 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 56 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 57 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 58 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 59 | root->fs_info->generation++; |
Chris Mason | e18e480 | 2008-01-18 10:54:22 -0500 | [diff] [blame] | 60 | root->fs_info->last_alloc = 0; |
Chris Mason | 4529ba4 | 2008-01-31 16:45:07 -0500 | [diff] [blame] | 61 | root->fs_info->last_data_alloc = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 62 | cur_trans->num_writers = 1; |
| 63 | cur_trans->num_joined = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 64 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 65 | init_waitqueue_head(&cur_trans->writer_wait); |
| 66 | init_waitqueue_head(&cur_trans->commit_wait); |
| 67 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 68 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 69 | cur_trans->commit_done = 0; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 70 | cur_trans->start_time = get_seconds(); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 71 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 72 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 73 | btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 74 | extent_io_tree_init(&cur_trans->dirty_pages, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 75 | root->fs_info->btree_inode->i_mapping, |
| 76 | GFP_NOFS); |
Chris Mason | 48ec2cf | 2008-06-09 09:35:50 -0400 | [diff] [blame] | 77 | spin_lock(&root->fs_info->new_trans_lock); |
| 78 | root->fs_info->running_transaction = cur_trans; |
| 79 | spin_unlock(&root->fs_info->new_trans_lock); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 80 | } else { |
| 81 | cur_trans->num_writers++; |
| 82 | cur_trans->num_joined++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 83 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 84 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 88 | static noinline int record_root_in_trans(struct btrfs_root *root) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 89 | { |
| 90 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
| 91 | if (root->ref_cows && root->last_trans < running_trans_id) { |
| 92 | WARN_ON(root == root->fs_info->extent_root); |
| 93 | if (root->root_item.refs != 0) { |
| 94 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 95 | (unsigned long)root->root_key.objectid, |
| 96 | BTRFS_ROOT_TRANS_TAG); |
| 97 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 98 | (unsigned long)root->root_key.objectid, |
| 99 | BTRFS_ROOT_DEFRAG_TAG); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame^] | 100 | root->commit_root = btrfs_root_node(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 101 | } else { |
| 102 | WARN_ON(1); |
| 103 | } |
| 104 | root->last_trans = running_trans_id; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 109 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 110 | int num_blocks) |
| 111 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 112 | struct btrfs_trans_handle *h = |
| 113 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 114 | int ret; |
| 115 | |
| 116 | mutex_lock(&root->fs_info->trans_mutex); |
| 117 | ret = join_transaction(root); |
| 118 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 119 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 120 | record_root_in_trans(root); |
| 121 | h->transid = root->fs_info->running_transaction->transid; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 122 | h->transaction = root->fs_info->running_transaction; |
| 123 | h->blocks_reserved = num_blocks; |
| 124 | h->blocks_used = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 125 | h->block_group = NULL; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 126 | h->alloc_exclude_nr = 0; |
| 127 | h->alloc_exclude_start = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 128 | root->fs_info->running_transaction->use_count++; |
| 129 | mutex_unlock(&root->fs_info->trans_mutex); |
| 130 | return h; |
| 131 | } |
| 132 | |
| 133 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 134 | struct btrfs_root *root) |
| 135 | { |
| 136 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 137 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 138 | mutex_lock(&root->fs_info->trans_mutex); |
| 139 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 140 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 141 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 142 | cur_trans->num_writers--; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 143 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 144 | wake_up(&cur_trans->writer_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 145 | put_transaction(cur_trans); |
| 146 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 147 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 148 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 154 | struct btrfs_root *root) |
| 155 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 156 | int ret; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 157 | int err; |
| 158 | int werr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 159 | struct extent_io_tree *dirty_pages; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 160 | struct page *page; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 161 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 162 | u64 start; |
| 163 | u64 end; |
| 164 | unsigned long index; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 165 | |
| 166 | if (!trans || !trans->transaction) { |
| 167 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 168 | } |
| 169 | dirty_pages = &trans->transaction->dirty_pages; |
| 170 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 171 | ret = find_first_extent_bit(dirty_pages, 0, &start, &end, |
| 172 | EXTENT_DIRTY); |
| 173 | if (ret) |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 174 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 175 | clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); |
| 176 | while(start <= end) { |
| 177 | index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 178 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 179 | page = find_lock_page(btree_inode->i_mapping, index); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 180 | if (!page) |
| 181 | continue; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 182 | if (PageWriteback(page)) { |
| 183 | if (PageDirty(page)) |
| 184 | wait_on_page_writeback(page); |
| 185 | else { |
| 186 | unlock_page(page); |
| 187 | page_cache_release(page); |
| 188 | continue; |
| 189 | } |
| 190 | } |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 191 | err = write_one_page(page, 0); |
| 192 | if (err) |
| 193 | werr = err; |
| 194 | page_cache_release(page); |
| 195 | } |
| 196 | } |
| 197 | err = filemap_fdatawait(btree_inode->i_mapping); |
| 198 | if (err) |
| 199 | werr = err; |
| 200 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 203 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
| 204 | struct btrfs_root *root) |
| 205 | { |
| 206 | int ret; |
| 207 | u64 old_root_bytenr; |
| 208 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
| 209 | |
| 210 | btrfs_write_dirty_block_groups(trans, root); |
| 211 | while(1) { |
| 212 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
| 213 | if (old_root_bytenr == root->node->start) |
| 214 | break; |
| 215 | btrfs_set_root_bytenr(&root->root_item, |
| 216 | root->node->start); |
| 217 | btrfs_set_root_level(&root->root_item, |
| 218 | btrfs_header_level(root->node)); |
| 219 | ret = btrfs_update_root(trans, tree_root, |
| 220 | &root->root_key, |
| 221 | &root->root_item); |
| 222 | BUG_ON(ret); |
| 223 | btrfs_write_dirty_block_groups(trans, root); |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 228 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 229 | struct btrfs_root *root) |
| 230 | { |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 231 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 232 | struct list_head *next; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 233 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 234 | while(!list_empty(&fs_info->dirty_cowonly_roots)) { |
| 235 | next = fs_info->dirty_cowonly_roots.next; |
| 236 | list_del_init(next); |
| 237 | root = list_entry(next, struct btrfs_root, dirty_list); |
| 238 | update_cowonly_root(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 243 | static noinline int wait_for_commit(struct btrfs_root *root, |
| 244 | struct btrfs_transaction *commit) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 245 | { |
| 246 | DEFINE_WAIT(wait); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 247 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 248 | while(!commit->commit_done) { |
| 249 | prepare_to_wait(&commit->commit_wait, &wait, |
| 250 | TASK_UNINTERRUPTIBLE); |
| 251 | if (commit->commit_done) |
| 252 | break; |
| 253 | mutex_unlock(&root->fs_info->trans_mutex); |
| 254 | schedule(); |
| 255 | mutex_lock(&root->fs_info->trans_mutex); |
| 256 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 257 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 258 | finish_wait(&commit->commit_wait, &wait); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 262 | struct dirty_root { |
| 263 | struct list_head list; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 264 | struct btrfs_root *root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 265 | struct btrfs_root *latest_root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 266 | }; |
| 267 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 268 | int btrfs_add_dead_root(struct btrfs_root *root, |
| 269 | struct btrfs_root *latest, |
| 270 | struct list_head *dead_list) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 271 | { |
| 272 | struct dirty_root *dirty; |
| 273 | |
| 274 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 275 | if (!dirty) |
| 276 | return -ENOMEM; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 277 | dirty->root = root; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 278 | dirty->latest_root = latest; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 279 | list_add(&dirty->list, dead_list); |
| 280 | return 0; |
| 281 | } |
| 282 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 283 | static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 284 | struct radix_tree_root *radix, |
| 285 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 286 | { |
| 287 | struct dirty_root *dirty; |
| 288 | struct btrfs_root *gang[8]; |
| 289 | struct btrfs_root *root; |
| 290 | int i; |
| 291 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 292 | int err = 0; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 293 | u32 refs; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 294 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 295 | while(1) { |
| 296 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 297 | ARRAY_SIZE(gang), |
| 298 | BTRFS_ROOT_TRANS_TAG); |
| 299 | if (ret == 0) |
| 300 | break; |
| 301 | for (i = 0; i < ret; i++) { |
| 302 | root = gang[i]; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 303 | radix_tree_tag_clear(radix, |
| 304 | (unsigned long)root->root_key.objectid, |
| 305 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 306 | if (root->commit_root == root->node) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 307 | WARN_ON(root->node->start != |
| 308 | btrfs_root_bytenr(&root->root_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 309 | free_extent_buffer(root->commit_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 310 | root->commit_root = NULL; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 311 | |
| 312 | /* make sure to update the root on disk |
| 313 | * so we get any updates to the block used |
| 314 | * counts |
| 315 | */ |
| 316 | err = btrfs_update_root(trans, |
| 317 | root->fs_info->tree_root, |
| 318 | &root->root_key, |
| 319 | &root->root_item); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 320 | continue; |
| 321 | } |
| 322 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 323 | BUG_ON(!dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 324 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); |
| 325 | BUG_ON(!dirty->root); |
| 326 | |
| 327 | memset(&root->root_item.drop_progress, 0, |
| 328 | sizeof(struct btrfs_disk_key)); |
| 329 | root->root_item.drop_level = 0; |
| 330 | |
| 331 | memcpy(dirty->root, root, sizeof(*root)); |
| 332 | dirty->root->node = root->commit_root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 333 | dirty->latest_root = root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 334 | root->commit_root = NULL; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 335 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 336 | root->root_key.offset = root->fs_info->generation; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 337 | btrfs_set_root_bytenr(&root->root_item, |
| 338 | root->node->start); |
| 339 | btrfs_set_root_level(&root->root_item, |
| 340 | btrfs_header_level(root->node)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 341 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 342 | &root->root_key, |
| 343 | &root->root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 344 | if (err) |
| 345 | break; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 346 | |
| 347 | refs = btrfs_root_refs(&dirty->root->root_item); |
| 348 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 349 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 350 | &dirty->root->root_key, |
| 351 | &dirty->root->root_item); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 352 | |
| 353 | BUG_ON(err); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 354 | if (refs == 1) { |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 355 | list_add(&dirty->list, list); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 356 | } else { |
| 357 | WARN_ON(1); |
| 358 | kfree(dirty->root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 359 | kfree(dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 360 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 361 | } |
| 362 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 363 | return err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 366 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
| 367 | { |
| 368 | struct btrfs_fs_info *info = root->fs_info; |
| 369 | int ret; |
| 370 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 371 | unsigned long nr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 372 | |
| 373 | if (root->defrag_running) |
| 374 | return 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 375 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 376 | while (1) { |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 377 | root->defrag_running = 1; |
| 378 | ret = btrfs_defrag_leaves(trans, root, cacheonly); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 379 | nr = trans->blocks_used; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 380 | btrfs_end_transaction(trans, root); |
| 381 | mutex_unlock(&info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 382 | btrfs_btree_balance_dirty(info->tree_root, nr); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 383 | cond_resched(); |
| 384 | |
| 385 | mutex_lock(&info->fs_mutex); |
| 386 | trans = btrfs_start_transaction(root, 1); |
| 387 | if (ret != -EAGAIN) |
| 388 | break; |
| 389 | } |
| 390 | root->defrag_running = 0; |
| 391 | radix_tree_tag_clear(&info->fs_roots_radix, |
| 392 | (unsigned long)root->root_key.objectid, |
| 393 | BTRFS_ROOT_DEFRAG_TAG); |
| 394 | btrfs_end_transaction(trans, root); |
| 395 | return 0; |
| 396 | } |
| 397 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 398 | int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) |
| 399 | { |
| 400 | struct btrfs_root *gang[1]; |
| 401 | struct btrfs_root *root; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 402 | int i; |
| 403 | int ret; |
| 404 | int err = 0; |
| 405 | u64 last = 0; |
| 406 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 407 | while(1) { |
| 408 | ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, |
| 409 | (void **)gang, last, |
| 410 | ARRAY_SIZE(gang), |
| 411 | BTRFS_ROOT_DEFRAG_TAG); |
| 412 | if (ret == 0) |
| 413 | break; |
| 414 | for (i = 0; i < ret; i++) { |
| 415 | root = gang[i]; |
| 416 | last = root->root_key.objectid + 1; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 417 | btrfs_defrag_root(root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 420 | btrfs_defrag_root(info->extent_root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 421 | return err; |
| 422 | } |
| 423 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 424 | static noinline int drop_dirty_roots(struct btrfs_root *tree_root, |
| 425 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 426 | { |
| 427 | struct dirty_root *dirty; |
| 428 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 429 | unsigned long nr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 430 | u64 num_bytes; |
| 431 | u64 bytes_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 432 | int ret = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 433 | int err; |
| 434 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 435 | while(!list_empty(list)) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 436 | struct btrfs_root *root; |
| 437 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 438 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 439 | dirty = list_entry(list->next, struct dirty_root, list); |
| 440 | list_del_init(&dirty->list); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 441 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 442 | num_bytes = btrfs_root_used(&dirty->root->root_item); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 443 | root = dirty->latest_root; |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 444 | root->fs_info->throttles++; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 445 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 446 | while(1) { |
| 447 | trans = btrfs_start_transaction(tree_root, 1); |
| 448 | ret = btrfs_drop_snapshot(trans, dirty->root); |
| 449 | if (ret != -EAGAIN) { |
| 450 | break; |
| 451 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 452 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 453 | err = btrfs_update_root(trans, |
| 454 | tree_root, |
| 455 | &dirty->root->root_key, |
| 456 | &dirty->root->root_item); |
| 457 | if (err) |
| 458 | ret = err; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 459 | nr = trans->blocks_used; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 460 | ret = btrfs_end_transaction(trans, tree_root); |
| 461 | BUG_ON(ret); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 462 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 463 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 464 | cond_resched(); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 465 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 466 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 467 | BUG_ON(ret); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 468 | root->fs_info->throttles--; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 469 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 470 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
| 471 | bytes_used = btrfs_root_used(&root->root_item); |
| 472 | if (num_bytes) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 473 | record_root_in_trans(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 474 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 475 | bytes_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 476 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 477 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 478 | if (ret) { |
| 479 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 480 | break; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 481 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 482 | nr = trans->blocks_used; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 483 | ret = btrfs_end_transaction(trans, tree_root); |
| 484 | BUG_ON(ret); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 485 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 486 | free_extent_buffer(dirty->root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 487 | kfree(dirty->root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 488 | kfree(dirty); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 489 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 490 | |
| 491 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 492 | cond_resched(); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 493 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 494 | return ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 497 | int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans, |
| 498 | struct btrfs_root *root) |
| 499 | { |
| 500 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 501 | struct inode *inode; |
| 502 | u64 root_objectid = 0; |
| 503 | u64 objectid = 0; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 504 | int ret; |
| 505 | |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 506 | root->fs_info->throttles++; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 507 | while(1) { |
| 508 | ret = btrfs_find_first_ordered_inode( |
| 509 | &cur_trans->ordered_inode_tree, |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 510 | &root_objectid, &objectid, &inode); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 511 | if (!ret) |
| 512 | break; |
| 513 | |
| 514 | mutex_unlock(&root->fs_info->trans_mutex); |
| 515 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 516 | |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 517 | if (S_ISREG(inode->i_mode)) { |
| 518 | atomic_inc(&BTRFS_I(inode)->ordered_writeback); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 519 | filemap_fdatawrite(inode->i_mapping); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 520 | atomic_dec(&BTRFS_I(inode)->ordered_writeback); |
| 521 | } |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 522 | iput(inode); |
| 523 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 524 | mutex_lock(&root->fs_info->fs_mutex); |
| 525 | mutex_lock(&root->fs_info->trans_mutex); |
| 526 | } |
| 527 | while(1) { |
| 528 | root_objectid = 0; |
| 529 | objectid = 0; |
| 530 | ret = btrfs_find_del_first_ordered_inode( |
| 531 | &cur_trans->ordered_inode_tree, |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 532 | &root_objectid, &objectid, &inode); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 533 | if (!ret) |
| 534 | break; |
| 535 | mutex_unlock(&root->fs_info->trans_mutex); |
| 536 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 537 | |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 538 | if (S_ISREG(inode->i_mode)) { |
| 539 | atomic_inc(&BTRFS_I(inode)->ordered_writeback); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 540 | filemap_write_and_wait(inode->i_mapping); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 541 | atomic_dec(&BTRFS_I(inode)->ordered_writeback); |
| 542 | } |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 543 | atomic_dec(&inode->i_count); |
| 544 | iput(inode); |
| 545 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 546 | mutex_lock(&root->fs_info->fs_mutex); |
| 547 | mutex_lock(&root->fs_info->trans_mutex); |
| 548 | } |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 549 | root->fs_info->throttles--; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 553 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 554 | struct btrfs_fs_info *fs_info, |
| 555 | struct btrfs_pending_snapshot *pending) |
| 556 | { |
| 557 | struct btrfs_key key; |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 558 | struct btrfs_root_item *new_root_item; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 559 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 560 | struct btrfs_root *root = pending->root; |
| 561 | struct extent_buffer *tmp; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame^] | 562 | struct extent_buffer *old; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 563 | int ret; |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 564 | int namelen; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 565 | u64 objectid; |
| 566 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 567 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
| 568 | if (!new_root_item) { |
| 569 | ret = -ENOMEM; |
| 570 | goto fail; |
| 571 | } |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 572 | ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); |
| 573 | if (ret) |
| 574 | goto fail; |
| 575 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 576 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 577 | |
| 578 | key.objectid = objectid; |
| 579 | key.offset = 1; |
| 580 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 581 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame^] | 582 | old = btrfs_lock_root_node(root); |
| 583 | btrfs_cow_block(trans, root, old, NULL, 0, &old); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 584 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame^] | 585 | btrfs_copy_root(trans, root, old, &tmp, objectid); |
| 586 | btrfs_tree_unlock(old); |
| 587 | free_extent_buffer(old); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 588 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 589 | btrfs_set_root_bytenr(new_root_item, tmp->start); |
| 590 | btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 591 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 592 | new_root_item); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame^] | 593 | btrfs_tree_unlock(tmp); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 594 | free_extent_buffer(tmp); |
| 595 | if (ret) |
| 596 | goto fail; |
| 597 | |
| 598 | /* |
| 599 | * insert the directory item |
| 600 | */ |
| 601 | key.offset = (u64)-1; |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 602 | namelen = strlen(pending->name); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 603 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 604 | pending->name, namelen, |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 605 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 606 | &key, BTRFS_FT_DIR); |
| 607 | |
| 608 | if (ret) |
| 609 | goto fail; |
| 610 | |
| 611 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
| 612 | pending->name, strlen(pending->name), objectid, |
| 613 | root->fs_info->sb->s_root->d_inode->i_ino); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 614 | |
| 615 | /* Invalidate existing dcache entry for new snapshot. */ |
| 616 | btrfs_invalidate_dcache_root(root, pending->name, namelen); |
| 617 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 618 | fail: |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 619 | kfree(new_root_item); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 620 | return ret; |
| 621 | } |
| 622 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 623 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
| 624 | struct btrfs_fs_info *fs_info) |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 625 | { |
| 626 | struct btrfs_pending_snapshot *pending; |
| 627 | struct list_head *head = &trans->transaction->pending_snapshots; |
| 628 | int ret; |
| 629 | |
| 630 | while(!list_empty(head)) { |
| 631 | pending = list_entry(head->next, |
| 632 | struct btrfs_pending_snapshot, list); |
| 633 | ret = create_pending_snapshot(trans, fs_info, pending); |
| 634 | BUG_ON(ret); |
| 635 | list_del(&pending->list); |
| 636 | kfree(pending->name); |
| 637 | kfree(pending); |
| 638 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 642 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 643 | struct btrfs_root *root) |
| 644 | { |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 645 | unsigned long joined = 0; |
| 646 | unsigned long timeout = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 647 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 648 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 649 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 650 | struct list_head dirty_fs_roots; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 651 | struct extent_io_tree *pinned_copy; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 652 | DEFINE_WAIT(wait); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 653 | int ret; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 654 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 655 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 656 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 657 | mutex_lock(&root->fs_info->trans_mutex); |
| 658 | if (trans->transaction->in_commit) { |
| 659 | cur_trans = trans->transaction; |
| 660 | trans->transaction->use_count++; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 661 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 662 | btrfs_end_transaction(trans, root); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 663 | |
| 664 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 665 | ret = wait_for_commit(root, cur_trans); |
| 666 | BUG_ON(ret); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 667 | |
| 668 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 669 | put_transaction(cur_trans); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 670 | mutex_unlock(&root->fs_info->trans_mutex); |
| 671 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 672 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 673 | return 0; |
| 674 | } |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 675 | |
| 676 | pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); |
| 677 | if (!pinned_copy) |
| 678 | return -ENOMEM; |
| 679 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 680 | extent_io_tree_init(pinned_copy, |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 681 | root->fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 682 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 683 | trans->transaction->in_commit = 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 684 | cur_trans = trans->transaction; |
| 685 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 686 | prev_trans = list_entry(cur_trans->list.prev, |
| 687 | struct btrfs_transaction, list); |
| 688 | if (!prev_trans->commit_done) { |
| 689 | prev_trans->use_count++; |
| 690 | mutex_unlock(&root->fs_info->fs_mutex); |
| 691 | mutex_unlock(&root->fs_info->trans_mutex); |
| 692 | |
| 693 | wait_for_commit(root, prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 694 | |
| 695 | mutex_lock(&root->fs_info->fs_mutex); |
| 696 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 697 | put_transaction(prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 698 | } |
| 699 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 700 | |
| 701 | do { |
| 702 | joined = cur_trans->num_joined; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 703 | WARN_ON(cur_trans != trans->transaction); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 704 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 705 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 706 | |
| 707 | if (cur_trans->num_writers > 1) |
| 708 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 709 | else |
| 710 | timeout = 1; |
| 711 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 712 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 713 | mutex_unlock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 714 | |
| 715 | schedule_timeout(timeout); |
| 716 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 717 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 718 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 719 | finish_wait(&cur_trans->writer_wait, &wait); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 720 | ret = btrfs_write_ordered_inodes(trans, root); |
| 721 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 722 | } while (cur_trans->num_writers > 1 || |
| 723 | (cur_trans->num_joined != joined)); |
| 724 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 725 | ret = create_pending_snapshots(trans, root->fs_info); |
| 726 | BUG_ON(ret); |
| 727 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 728 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 729 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 730 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
| 731 | &dirty_fs_roots); |
| 732 | BUG_ON(ret); |
| 733 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 734 | ret = btrfs_commit_tree_roots(trans, root); |
| 735 | BUG_ON(ret); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 736 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 737 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 738 | spin_lock(&root->fs_info->new_trans_lock); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 739 | root->fs_info->running_transaction = NULL; |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 740 | spin_unlock(&root->fs_info->new_trans_lock); |
Chris Mason | 4b52dff | 2007-06-26 10:06:50 -0400 | [diff] [blame] | 741 | btrfs_set_super_generation(&root->fs_info->super_copy, |
| 742 | cur_trans->transid); |
| 743 | btrfs_set_super_root(&root->fs_info->super_copy, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 744 | root->fs_info->tree_root->node->start); |
| 745 | btrfs_set_super_root_level(&root->fs_info->super_copy, |
| 746 | btrfs_header_level(root->fs_info->tree_root->node)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 747 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 748 | btrfs_set_super_chunk_root(&root->fs_info->super_copy, |
| 749 | chunk_root->node->start); |
| 750 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, |
| 751 | btrfs_header_level(chunk_root->node)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 752 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
| 753 | sizeof(root->fs_info->super_copy)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 754 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 755 | btrfs_copy_pinned(root, pinned_copy); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 756 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 757 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 758 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 759 | ret = btrfs_write_and_wait_transaction(trans, root); |
| 760 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 761 | write_ctree_super(trans, root); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 762 | |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 763 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 764 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 765 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 766 | |
| 767 | kfree(pinned_copy); |
| 768 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 769 | cur_trans->commit_done = 1; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 770 | root->fs_info->last_trans_committed = cur_trans->transid; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 771 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 772 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 773 | put_transaction(cur_trans); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 774 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 775 | if (root->fs_info->closing) |
| 776 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); |
| 777 | else |
| 778 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 779 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 780 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 781 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 782 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 783 | if (root->fs_info->closing) { |
| 784 | mutex_unlock(&root->fs_info->fs_mutex); |
| 785 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
| 786 | mutex_lock(&root->fs_info->fs_mutex); |
| 787 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 788 | return ret; |
| 789 | } |
| 790 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 791 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
| 792 | { |
| 793 | struct list_head dirty_roots; |
| 794 | INIT_LIST_HEAD(&dirty_roots); |
| 795 | |
| 796 | mutex_lock(&root->fs_info->trans_mutex); |
| 797 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); |
| 798 | mutex_unlock(&root->fs_info->trans_mutex); |
| 799 | |
| 800 | if (!list_empty(&dirty_roots)) { |
| 801 | drop_dirty_roots(root, &dirty_roots); |
| 802 | } |
| 803 | return 0; |
| 804 | } |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 805 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 806 | void btrfs_transaction_cleaner(void *p) |
| 807 | #else |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 808 | void btrfs_transaction_cleaner(struct work_struct *work) |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 809 | #endif |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 810 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 811 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 812 | struct btrfs_fs_info *fs_info = p; |
| 813 | #else |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 814 | struct btrfs_fs_info *fs_info = container_of(work, |
| 815 | struct btrfs_fs_info, |
| 816 | trans_work.work); |
| 817 | |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 818 | #endif |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 819 | struct btrfs_root *root = fs_info->tree_root; |
| 820 | struct btrfs_transaction *cur; |
| 821 | struct btrfs_trans_handle *trans; |
| 822 | unsigned long now; |
| 823 | unsigned long delay = HZ * 30; |
| 824 | int ret; |
| 825 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 826 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 827 | if (root->fs_info->closing) |
| 828 | goto out; |
| 829 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 830 | mutex_lock(&root->fs_info->trans_mutex); |
| 831 | cur = root->fs_info->running_transaction; |
| 832 | if (!cur) { |
| 833 | mutex_unlock(&root->fs_info->trans_mutex); |
| 834 | goto out; |
| 835 | } |
| 836 | now = get_seconds(); |
| 837 | if (now < cur->start_time || now - cur->start_time < 30) { |
| 838 | mutex_unlock(&root->fs_info->trans_mutex); |
| 839 | delay = HZ * 5; |
| 840 | goto out; |
| 841 | } |
| 842 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 843 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 844 | trans = btrfs_start_transaction(root, 1); |
| 845 | ret = btrfs_commit_transaction(trans, root); |
| 846 | out: |
| 847 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 848 | btrfs_clean_old_snapshots(root); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 849 | btrfs_transaction_queue_work(root, delay); |
| 850 | } |
| 851 | |
| 852 | void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) |
| 853 | { |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 854 | if (!root->fs_info->closing) |
| 855 | queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | void btrfs_transaction_flush_work(struct btrfs_root *root) |
| 859 | { |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 860 | cancel_delayed_work(&root->fs_info->trans_work); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 861 | flush_workqueue(trans_wq); |
| 862 | } |
| 863 | |
| 864 | void __init btrfs_init_transaction_sys(void) |
| 865 | { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 866 | trans_wq = create_workqueue("btrfs-transaction"); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 867 | } |
| 868 | |
Christian Hesse | 17636e0 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 869 | void btrfs_exit_transaction_sys(void) |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 870 | { |
| 871 | destroy_workqueue(trans_wq); |
| 872 | } |
| 873 | |