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 | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 16 | static int check_tree_block(struct ctree_root *root, struct tree_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 | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 26 | static int free_some_buffers(struct ctree_root *root) |
| 27 | { |
| 28 | struct list_head *node, *next; |
| 29 | struct tree_buffer *b; |
| 30 | if (root->cache_size < cache_max) |
| 31 | return 0; |
| 32 | list_for_each_safe(node, next, &root->cache) { |
| 33 | b = list_entry(node, struct tree_buffer, cache); |
| 34 | if (b->count == 1) { |
| 35 | BUG_ON(!list_empty(&b->dirty)); |
| 36 | list_del_init(&b->cache); |
| 37 | tree_block_release(root, b); |
| 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 | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 45 | struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr) |
| 46 | { |
| 47 | struct tree_buffer *buf; |
| 48 | int ret; |
| 49 | buf = malloc(sizeof(struct tree_buffer)); |
| 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 | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 69 | struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 70 | { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 71 | struct tree_buffer *buf; |
| 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 | |
| 85 | struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) |
| 86 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 87 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 88 | struct tree_buffer *buf; |
| 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; |
| 98 | ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); |
| 99 | if (ret != CTREE_BLOCKSIZE) { |
| 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 | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 109 | int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 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 | |
| 118 | int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 119 | { |
| 120 | if (!list_empty(&buf->dirty)) { |
| 121 | list_del_init(&buf->dirty); |
| 122 | tree_block_release(root, buf); |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 127 | int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 128 | { |
| 129 | u64 blocknr = buf->blocknr; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 130 | loff_t offset = blocknr * CTREE_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(); |
| 135 | ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); |
| 136 | if (ret != CTREE_BLOCKSIZE) |
| 137 | return ret; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 141 | static int __commit_transaction(struct ctree_root *root) |
| 142 | { |
| 143 | struct tree_buffer *b; |
| 144 | int ret = 0; |
| 145 | int wret; |
| 146 | while(!list_empty(&root->trans)) { |
| 147 | b = list_entry(root->trans.next, struct tree_buffer, dirty); |
| 148 | list_del_init(&b->dirty); |
| 149 | wret = write_tree_block(root, b); |
| 150 | if (wret) |
| 151 | ret = wret; |
| 152 | tree_block_release(root, b); |
| 153 | } |
| 154 | return ret; |
| 155 | } |
| 156 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 157 | int commit_transaction(struct ctree_root *root, struct ctree_super_block *s) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 158 | { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 159 | int ret = 0; |
| 160 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 161 | ret = __commit_transaction(root); |
| 162 | if (!ret && root != root->extent_root) |
| 163 | ret = __commit_transaction(root->extent_root); |
| 164 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 165 | if (root->commit_root != root->node) { |
| 166 | struct tree_buffer *snap = root->commit_root; |
| 167 | root->commit_root = root->node; |
| 168 | root->node->count++; |
| 169 | ret = btrfs_drop_snapshot(root, snap); |
| 170 | BUG_ON(ret); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 171 | // tree_block_release(root, snap); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 172 | } |
| 173 | write_ctree_super(root, s); |
| 174 | btrfs_finish_extent_commit(root); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 175 | return ret; |
| 176 | } |
| 177 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 178 | static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root, |
| 179 | struct ctree_root_info *info, int fp) |
| 180 | { |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 181 | INIT_LIST_HEAD(&root->trans); |
| 182 | INIT_LIST_HEAD(&root->cache); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 183 | root->cache_size = 0; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 184 | root->fp = fp; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 185 | root->node = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 186 | root->extent_root = extent_root; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 187 | root->commit_root = NULL; |
| 188 | root->node = read_tree_block(root, info->tree_root); |
| 189 | memset(&root->current_insert, 0, sizeof(root->current_insert)); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 190 | memset(&root->last_insert, 0, sizeof(root->last_insert)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 194 | struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 195 | { |
| 196 | struct ctree_root *root = malloc(sizeof(struct ctree_root)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 197 | struct ctree_root *extent_root = malloc(sizeof(struct ctree_root)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 198 | int fp; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 199 | int ret; |
| 200 | |
Chris Mason | c673024 | 2007-02-26 10:46:55 -0500 | [diff] [blame] | 201 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 202 | if (fp < 0) { |
| 203 | free(root); |
| 204 | return NULL; |
| 205 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 206 | INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 207 | INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL); |
| 208 | INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 209 | INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 210 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 211 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 212 | if (ret == 0 || super->root_info.tree_root == 0) { |
| 213 | printf("making new FS!\n"); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 214 | ret = mkfs(fp); |
| 215 | if (ret) |
| 216 | return NULL; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 217 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 218 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 219 | if (ret != sizeof(struct ctree_super_block)) |
| 220 | return NULL; |
| 221 | } |
| 222 | BUG_ON(ret < 0); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 223 | __setup_root(root, extent_root, &super->root_info, fp); |
| 224 | __setup_root(extent_root, extent_root, &super->extent_info, fp); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 225 | root->commit_root = root->node; |
| 226 | root->node->count++; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 227 | return root; |
| 228 | } |
| 229 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 230 | static int __update_root(struct ctree_root *root, struct ctree_root_info *info) |
| 231 | { |
| 232 | info->tree_root = root->node->blocknr; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s) |
| 237 | { |
| 238 | int ret; |
| 239 | __update_root(root, &s->root_info); |
| 240 | __update_root(root->extent_root, &s->extent_info); |
| 241 | ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 242 | if (ret != sizeof(*s)) { |
| 243 | fprintf(stderr, "failed to write new super block err %d\n", ret); |
| 244 | return ret; |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 249 | static int drop_cache(struct ctree_root *root) |
| 250 | { |
| 251 | while(!list_empty(&root->cache)) { |
| 252 | struct tree_buffer *b = list_entry(root->cache.next, |
| 253 | struct tree_buffer, cache); |
| 254 | list_del_init(&b->cache); |
| 255 | tree_block_release(root, b); |
| 256 | } |
| 257 | return 0; |
| 258 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 259 | int close_ctree(struct ctree_root *root, struct ctree_super_block *s) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 260 | { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 261 | commit_transaction(root, s); |
| 262 | __commit_transaction(root->extent_root); |
| 263 | write_ctree_super(root, s); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 264 | drop_cache(root->extent_root); |
| 265 | drop_cache(root); |
| 266 | BUG_ON(!list_empty(&root->trans)); |
| 267 | BUG_ON(!list_empty(&root->extent_root->trans)); |
| 268 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 269 | close(root->fp); |
| 270 | if (root->node) |
| 271 | tree_block_release(root, root->node); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 272 | if (root->extent_root->node) |
| 273 | tree_block_release(root->extent_root, root->extent_root->node); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 274 | tree_block_release(root, root->commit_root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 275 | free(root); |
| 276 | printf("on close %d blocks are allocated\n", allocated_blocks); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 280 | void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) |
| 281 | { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 282 | buf->count--; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 283 | if (buf->count < 0) |
| 284 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 285 | if (buf->count == 0) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 286 | BUG_ON(!list_empty(&buf->cache)); |
| 287 | BUG_ON(!list_empty(&buf->dirty)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 288 | if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) |
| 289 | BUG(); |
| 290 | radix_tree_delete(&root->cache_radix, buf->blocknr); |
| 291 | memset(buf, 0, sizeof(*buf)); |
| 292 | free(buf); |
| 293 | BUG_ON(allocated_blocks == 0); |
| 294 | allocated_blocks--; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 295 | BUG_ON(root->cache_size == 0); |
| 296 | root->cache_size--; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |