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