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