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