Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1 | #define _XOPEN_SOURCE 500 |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | #include "kerncompat.h" |
| 9 | #include "radix-tree.h" |
| 10 | #include "ctree.h" |
| 11 | #include "disk-io.h" |
| 12 | |
| 13 | static int allocated_blocks = 0; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 14 | int cache_max = 10000; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 15 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 16 | static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 17 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 18 | if (buf->blocknr != btrfs_header_blocknr(&buf->node.header)) |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 19 | BUG(); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 20 | if (root->node && btrfs_header_parentid(&buf->node.header) != |
| 21 | btrfs_header_parentid(&root->node->node.header)) |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 22 | BUG(); |
| 23 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 24 | } |
| 25 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 26 | static int free_some_buffers(struct btrfs_root *root) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 27 | { |
| 28 | struct list_head *node, *next; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 29 | struct btrfs_buffer *b; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 30 | if (root->cache_size < cache_max) |
| 31 | return 0; |
| 32 | list_for_each_safe(node, next, &root->cache) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 33 | b = list_entry(node, struct btrfs_buffer, cache); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 34 | if (b->count == 1) { |
| 35 | BUG_ON(!list_empty(&b->dirty)); |
| 36 | list_del_init(&b->cache); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 37 | btrfs_block_release(root, b); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 38 | if (root->cache_size < cache_max) |
Chris Mason | 77ce684 | 2007-03-02 10:06:43 -0500 | [diff] [blame] | 39 | break; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 45 | struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 46 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 47 | struct btrfs_buffer *buf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 48 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 49 | buf = malloc(sizeof(struct btrfs_buffer)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 50 | if (!buf) |
| 51 | return buf; |
| 52 | allocated_blocks++; |
| 53 | buf->blocknr = blocknr; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 54 | buf->count = 2; |
| 55 | INIT_LIST_HEAD(&buf->dirty); |
| 56 | free_some_buffers(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 57 | radix_tree_preload(GFP_KERNEL); |
| 58 | ret = radix_tree_insert(&root->cache_radix, blocknr, buf); |
| 59 | radix_tree_preload_end(); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 60 | list_add_tail(&buf->cache, &root->cache); |
| 61 | root->cache_size++; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 62 | if (ret) { |
| 63 | free(buf); |
| 64 | return NULL; |
| 65 | } |
| 66 | return buf; |
| 67 | } |
| 68 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 69 | struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 70 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 71 | struct btrfs_buffer *buf; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 72 | buf = radix_tree_lookup(&root->cache_radix, blocknr); |
| 73 | if (buf) { |
| 74 | buf->count++; |
| 75 | } else { |
| 76 | buf = alloc_tree_block(root, blocknr); |
| 77 | if (!buf) { |
| 78 | BUG(); |
| 79 | return NULL; |
| 80 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 81 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 82 | return buf; |
| 83 | } |
| 84 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 85 | struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 86 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 87 | loff_t offset = blocknr * BTRFS_BLOCKSIZE; |
| 88 | struct btrfs_buffer *buf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 89 | int ret; |
| 90 | |
| 91 | buf = radix_tree_lookup(&root->cache_radix, blocknr); |
| 92 | if (buf) { |
| 93 | buf->count++; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 94 | } else { |
| 95 | buf = alloc_tree_block(root, blocknr); |
| 96 | if (!buf) |
| 97 | return NULL; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 98 | ret = pread(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset); |
| 99 | if (ret != BTRFS_BLOCKSIZE) { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 100 | free(buf); |
| 101 | return NULL; |
| 102 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 103 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 104 | if (check_tree_block(root, buf)) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 105 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 106 | return buf; |
| 107 | } |
| 108 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 109 | int dirty_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 110 | { |
| 111 | if (!list_empty(&buf->dirty)) |
| 112 | return 0; |
| 113 | list_add_tail(&buf->dirty, &root->trans); |
| 114 | buf->count++; |
| 115 | return 0; |
| 116 | } |
| 117 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 118 | int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 119 | { |
| 120 | if (!list_empty(&buf->dirty)) { |
| 121 | list_del_init(&buf->dirty); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 122 | btrfs_block_release(root, buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 127 | int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 128 | { |
| 129 | u64 blocknr = buf->blocknr; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 130 | loff_t offset = blocknr * BTRFS_BLOCKSIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 131 | int ret; |
| 132 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 133 | if (buf->blocknr != btrfs_header_blocknr(&buf->node.header)) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 134 | BUG(); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 135 | ret = pwrite(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset); |
| 136 | if (ret != BTRFS_BLOCKSIZE) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 137 | return ret; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 141 | static int __commit_transaction(struct btrfs_root *root) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 142 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 143 | struct btrfs_buffer *b; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 144 | int ret = 0; |
| 145 | int wret; |
| 146 | while(!list_empty(&root->trans)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 147 | b = list_entry(root->trans.next, struct btrfs_buffer, dirty); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 148 | list_del_init(&b->dirty); |
| 149 | wret = write_tree_block(root, b); |
| 150 | if (wret) |
| 151 | ret = wret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 152 | btrfs_block_release(root, b); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 153 | } |
| 154 | return ret; |
| 155 | } |
| 156 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 157 | static int commit_extent_and_tree_roots(struct btrfs_root *tree_root, |
| 158 | struct btrfs_root *extent_root) |
| 159 | { |
| 160 | int ret; |
| 161 | u64 old_extent_block; |
| 162 | |
| 163 | while(1) { |
| 164 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); |
| 165 | if (old_extent_block == extent_root->node->blocknr) |
| 166 | break; |
| 167 | btrfs_set_root_blocknr(&extent_root->root_item, |
| 168 | extent_root->node->blocknr); |
| 169 | ret = btrfs_update_root(tree_root, |
| 170 | &extent_root->root_key, |
| 171 | &extent_root->root_item); |
| 172 | BUG_ON(ret); |
| 173 | } |
| 174 | __commit_transaction(extent_root); |
| 175 | __commit_transaction(tree_root); |
| 176 | return 0; |
| 177 | } |
| 178 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 179 | int btrfs_commit_transaction(struct btrfs_root *root, |
| 180 | struct btrfs_super_block *s) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 181 | { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 182 | int ret = 0; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 183 | struct btrfs_buffer *snap = root->commit_root; |
| 184 | struct btrfs_key snap_key; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 185 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 186 | ret = __commit_transaction(root); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 187 | BUG_ON(ret); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 188 | |
| 189 | if (root->commit_root == root->node) |
| 190 | return 0; |
| 191 | |
| 192 | memcpy(&snap_key, &root->root_key, sizeof(snap_key)); |
| 193 | root->root_key.offset++; |
| 194 | |
| 195 | btrfs_set_root_blocknr(&root->root_item, root->node->blocknr); |
| 196 | ret = btrfs_insert_root(root->tree_root, &root->root_key, |
| 197 | &root->root_item); |
| 198 | BUG_ON(ret); |
| 199 | |
| 200 | ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root); |
| 201 | BUG_ON(ret); |
| 202 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 203 | write_ctree_super(root, s); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 204 | btrfs_finish_extent_commit(root->extent_root); |
| 205 | btrfs_finish_extent_commit(root->tree_root); |
| 206 | |
| 207 | root->commit_root = root->node; |
| 208 | root->node->count++; |
| 209 | ret = btrfs_drop_snapshot(root, snap); |
| 210 | BUG_ON(ret); |
| 211 | |
| 212 | ret = btrfs_del_root(root->tree_root, &snap_key); |
| 213 | BUG_ON(ret); |
| 214 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 215 | return ret; |
| 216 | } |
| 217 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 218 | static int __setup_root(struct btrfs_root *root, u64 objectid, int fp) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 219 | { |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 220 | INIT_LIST_HEAD(&root->trans); |
| 221 | INIT_LIST_HEAD(&root->cache); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 222 | root->cache_size = 0; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 223 | root->fp = fp; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 224 | root->node = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 225 | root->commit_root = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 226 | memset(&root->current_insert, 0, sizeof(root->current_insert)); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 227 | memset(&root->last_insert, 0, sizeof(root->last_insert)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 228 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 229 | memset(&root->root_item, 0, sizeof(root->root_item)); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int find_and_setup_root(struct btrfs_root *tree_root, u64 objectid, |
| 234 | struct btrfs_root *root, int fp) |
| 235 | { |
| 236 | int ret; |
| 237 | |
| 238 | __setup_root(root, objectid, fp); |
| 239 | ret = btrfs_find_last_root(tree_root, objectid, |
| 240 | &root->root_item, &root->root_key); |
| 241 | BUG_ON(ret); |
| 242 | |
| 243 | root->node = read_tree_block(root, |
| 244 | btrfs_root_blocknr(&root->root_item)); |
| 245 | root->ref_cows = 0; |
| 246 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 250 | struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 251 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 252 | struct btrfs_root *root = malloc(sizeof(struct btrfs_root)); |
| 253 | struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 254 | struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 255 | int fp; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 256 | int ret; |
| 257 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 258 | root->extent_root = extent_root; |
| 259 | root->tree_root = tree_root; |
| 260 | |
| 261 | extent_root->extent_root = extent_root; |
| 262 | extent_root->tree_root = tree_root; |
| 263 | |
| 264 | tree_root->extent_root = extent_root; |
| 265 | tree_root->tree_root = tree_root; |
| 266 | |
Chris Mason | c673024 | 2007-02-26 10:46:55 -0500 | [diff] [blame] | 267 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 268 | if (fp < 0) { |
| 269 | free(root); |
| 270 | return NULL; |
| 271 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 272 | INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 273 | INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL); |
| 274 | INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 275 | INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 276 | INIT_RADIX_TREE(&tree_root->pinned_radix, GFP_KERNEL); |
| 277 | INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL); |
| 278 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 279 | ret = pread(fp, super, sizeof(struct btrfs_super_block), |
| 280 | BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 281 | if (ret == 0 || btrfs_super_root(super) == 0) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 282 | printf("making new FS!\n"); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 283 | ret = mkfs(fp, 0, BTRFS_BLOCKSIZE); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 284 | if (ret) |
| 285 | return NULL; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 286 | ret = pread(fp, super, sizeof(struct btrfs_super_block), |
| 287 | BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE)); |
| 288 | if (ret != sizeof(struct btrfs_super_block)) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 289 | return NULL; |
| 290 | } |
| 291 | BUG_ON(ret < 0); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 292 | |
| 293 | __setup_root(tree_root, BTRFS_ROOT_TREE_OBJECTID, fp); |
| 294 | tree_root->node = read_tree_block(tree_root, btrfs_super_root(super)); |
| 295 | BUG_ON(!tree_root->node); |
| 296 | |
| 297 | ret = find_and_setup_root(tree_root, BTRFS_EXTENT_TREE_OBJECTID, |
| 298 | extent_root, fp); |
| 299 | BUG_ON(ret); |
| 300 | |
| 301 | ret = find_and_setup_root(tree_root, BTRFS_FS_TREE_OBJECTID, |
| 302 | root, fp); |
| 303 | BUG_ON(ret); |
| 304 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 305 | root->commit_root = root->node; |
| 306 | root->node->count++; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 307 | root->ref_cows = 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 308 | return root; |
| 309 | } |
| 310 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 311 | int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 312 | { |
| 313 | int ret; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 314 | btrfs_set_super_root(s, root->tree_root->node->blocknr); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 315 | ret = pwrite(root->fp, s, sizeof(*s), |
| 316 | BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE)); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 317 | if (ret != sizeof(*s)) { |
| 318 | fprintf(stderr, "failed to write new super block err %d\n", ret); |
| 319 | return ret; |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 324 | static int drop_cache(struct btrfs_root *root) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 325 | { |
| 326 | while(!list_empty(&root->cache)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 327 | struct btrfs_buffer *b = list_entry(root->cache.next, |
| 328 | struct btrfs_buffer, cache); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 329 | list_del_init(&b->cache); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 330 | btrfs_block_release(root, b); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 331 | } |
| 332 | return 0; |
| 333 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 334 | int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 335 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 336 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 337 | btrfs_commit_transaction(root, s); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 338 | ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root); |
| 339 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 340 | write_ctree_super(root, s); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 341 | drop_cache(root->extent_root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 342 | drop_cache(root->tree_root); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 343 | drop_cache(root); |
| 344 | BUG_ON(!list_empty(&root->trans)); |
| 345 | BUG_ON(!list_empty(&root->extent_root->trans)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 346 | BUG_ON(!list_empty(&root->tree_root->trans)); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 347 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 348 | close(root->fp); |
| 349 | if (root->node) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 350 | btrfs_block_release(root, root->node); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 351 | if (root->extent_root->node) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 352 | btrfs_block_release(root->extent_root, root->extent_root->node); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame^] | 353 | if (root->tree_root->node) |
| 354 | btrfs_block_release(root->tree_root, root->tree_root->node); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 355 | btrfs_block_release(root, root->commit_root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 356 | free(root); |
| 357 | printf("on close %d blocks are allocated\n", allocated_blocks); |
| 358 | return 0; |
| 359 | } |
| 360 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 361 | void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 362 | { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 363 | buf->count--; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 364 | if (buf->count < 0) |
| 365 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 366 | if (buf->count == 0) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 367 | BUG_ON(!list_empty(&buf->cache)); |
| 368 | BUG_ON(!list_empty(&buf->dirty)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 369 | if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) |
| 370 | BUG(); |
| 371 | radix_tree_delete(&root->cache_radix, buf->blocknr); |
| 372 | memset(buf, 0, sizeof(*buf)); |
| 373 | free(buf); |
| 374 | BUG_ON(allocated_blocks == 0); |
| 375 | allocated_blocks--; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 376 | BUG_ON(root->cache_size == 0); |
| 377 | root->cache_size--; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |