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 | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 18 | if (buf->blocknr != buf->node.header.blocknr) |
| 19 | BUG(); |
| 20 | if (root->node && buf->node.header.parentid != root->node->node.header.parentid) |
| 21 | BUG(); |
| 22 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 23 | } |
| 24 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 25 | static int free_some_buffers(struct ctree_root *root) |
| 26 | { |
| 27 | struct list_head *node, *next; |
| 28 | struct tree_buffer *b; |
| 29 | if (root->cache_size < cache_max) |
| 30 | return 0; |
| 31 | list_for_each_safe(node, next, &root->cache) { |
| 32 | b = list_entry(node, struct tree_buffer, cache); |
| 33 | if (b->count == 1) { |
| 34 | BUG_ON(!list_empty(&b->dirty)); |
| 35 | list_del_init(&b->cache); |
| 36 | tree_block_release(root, b); |
| 37 | if (root->cache_size < cache_max) |
Chris Mason | 77ce684 | 2007-03-02 10:06:43 -0500 | [diff] [blame^] | 38 | break; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 44 | struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr) |
| 45 | { |
| 46 | struct tree_buffer *buf; |
| 47 | int ret; |
| 48 | buf = malloc(sizeof(struct tree_buffer)); |
| 49 | if (!buf) |
| 50 | return buf; |
| 51 | allocated_blocks++; |
| 52 | buf->blocknr = blocknr; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 53 | buf->count = 2; |
| 54 | INIT_LIST_HEAD(&buf->dirty); |
| 55 | free_some_buffers(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 56 | radix_tree_preload(GFP_KERNEL); |
| 57 | ret = radix_tree_insert(&root->cache_radix, blocknr, buf); |
| 58 | radix_tree_preload_end(); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 59 | list_add_tail(&buf->cache, &root->cache); |
| 60 | root->cache_size++; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 61 | if (ret) { |
| 62 | free(buf); |
| 63 | return NULL; |
| 64 | } |
| 65 | return buf; |
| 66 | } |
| 67 | |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 68 | struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 69 | { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 70 | struct tree_buffer *buf; |
| 71 | buf = radix_tree_lookup(&root->cache_radix, blocknr); |
| 72 | if (buf) { |
| 73 | buf->count++; |
| 74 | } else { |
| 75 | buf = alloc_tree_block(root, blocknr); |
| 76 | if (!buf) { |
| 77 | BUG(); |
| 78 | return NULL; |
| 79 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 80 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 81 | return buf; |
| 82 | } |
| 83 | |
| 84 | struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) |
| 85 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 86 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 87 | struct tree_buffer *buf; |
| 88 | int ret; |
| 89 | |
| 90 | buf = radix_tree_lookup(&root->cache_radix, blocknr); |
| 91 | if (buf) { |
| 92 | buf->count++; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 93 | } else { |
| 94 | buf = alloc_tree_block(root, blocknr); |
| 95 | if (!buf) |
| 96 | return NULL; |
| 97 | ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); |
| 98 | if (ret != CTREE_BLOCKSIZE) { |
| 99 | free(buf); |
| 100 | return NULL; |
| 101 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 102 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 103 | if (check_tree_block(root, buf)) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 104 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 105 | return buf; |
| 106 | } |
| 107 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 108 | int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 109 | { |
| 110 | if (!list_empty(&buf->dirty)) |
| 111 | return 0; |
| 112 | list_add_tail(&buf->dirty, &root->trans); |
| 113 | buf->count++; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 118 | { |
| 119 | if (!list_empty(&buf->dirty)) { |
| 120 | list_del_init(&buf->dirty); |
| 121 | tree_block_release(root, buf); |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 126 | int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 127 | { |
| 128 | u64 blocknr = buf->blocknr; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 129 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 130 | int ret; |
| 131 | |
| 132 | if (buf->blocknr != buf->node.header.blocknr) |
| 133 | BUG(); |
| 134 | ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); |
| 135 | if (ret != CTREE_BLOCKSIZE) |
| 136 | return ret; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 140 | static int __commit_transaction(struct ctree_root *root) |
| 141 | { |
| 142 | struct tree_buffer *b; |
| 143 | int ret = 0; |
| 144 | int wret; |
| 145 | while(!list_empty(&root->trans)) { |
| 146 | b = list_entry(root->trans.next, struct tree_buffer, dirty); |
| 147 | list_del_init(&b->dirty); |
| 148 | wret = write_tree_block(root, b); |
| 149 | if (wret) |
| 150 | ret = wret; |
| 151 | tree_block_release(root, b); |
| 152 | } |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | int commit_transaction(struct ctree_root *root) |
| 157 | { |
| 158 | int ret; |
| 159 | ret = __commit_transaction(root); |
| 160 | if (!ret && root != root->extent_root) |
| 161 | ret = __commit_transaction(root->extent_root); |
| 162 | BUG_ON(ret); |
| 163 | return ret; |
| 164 | } |
| 165 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 166 | static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root, |
| 167 | struct ctree_root_info *info, int fp) |
| 168 | { |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 169 | INIT_LIST_HEAD(&root->trans); |
| 170 | INIT_LIST_HEAD(&root->cache); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 171 | root->fp = fp; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 172 | root->node = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 173 | root->node = read_tree_block(root, info->tree_root); |
| 174 | root->extent_root = extent_root; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 178 | struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 179 | { |
| 180 | struct ctree_root *root = malloc(sizeof(struct ctree_root)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 181 | struct ctree_root *extent_root = malloc(sizeof(struct ctree_root)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 182 | int fp; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 183 | int ret; |
| 184 | |
Chris Mason | c673024 | 2007-02-26 10:46:55 -0500 | [diff] [blame] | 185 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 186 | if (fp < 0) { |
| 187 | free(root); |
| 188 | return NULL; |
| 189 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 190 | INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); |
| 191 | INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 192 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 193 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 194 | if (ret == 0 || super->root_info.tree_root == 0) { |
| 195 | printf("making new FS!\n"); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 196 | ret = mkfs(fp); |
| 197 | if (ret) |
| 198 | return NULL; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 199 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 200 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 201 | if (ret != sizeof(struct ctree_super_block)) |
| 202 | return NULL; |
| 203 | } |
| 204 | BUG_ON(ret < 0); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 205 | __setup_root(root, extent_root, &super->root_info, fp); |
| 206 | __setup_root(extent_root, extent_root, &super->extent_info, fp); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 207 | return root; |
| 208 | } |
| 209 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 210 | static int __update_root(struct ctree_root *root, struct ctree_root_info *info) |
| 211 | { |
| 212 | info->tree_root = root->node->blocknr; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s) |
| 217 | { |
| 218 | int ret; |
| 219 | __update_root(root, &s->root_info); |
| 220 | __update_root(root->extent_root, &s->extent_info); |
| 221 | ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 222 | if (ret != sizeof(*s)) { |
| 223 | fprintf(stderr, "failed to write new super block err %d\n", ret); |
| 224 | return ret; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 229 | static int drop_cache(struct ctree_root *root) |
| 230 | { |
| 231 | while(!list_empty(&root->cache)) { |
| 232 | struct tree_buffer *b = list_entry(root->cache.next, |
| 233 | struct tree_buffer, cache); |
| 234 | list_del_init(&b->cache); |
| 235 | tree_block_release(root, b); |
| 236 | } |
| 237 | return 0; |
| 238 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 239 | int close_ctree(struct ctree_root *root) |
| 240 | { |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 241 | commit_transaction(root); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 242 | drop_cache(root->extent_root); |
| 243 | drop_cache(root); |
| 244 | BUG_ON(!list_empty(&root->trans)); |
| 245 | BUG_ON(!list_empty(&root->extent_root->trans)); |
| 246 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 247 | close(root->fp); |
| 248 | if (root->node) |
| 249 | tree_block_release(root, root->node); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 250 | if (root->extent_root->node) |
| 251 | tree_block_release(root->extent_root, root->extent_root->node); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 252 | free(root); |
| 253 | printf("on close %d blocks are allocated\n", allocated_blocks); |
| 254 | return 0; |
| 255 | } |
| 256 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 257 | void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) |
| 258 | { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 259 | buf->count--; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 260 | if (buf->count < 0) |
| 261 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 262 | if (buf->count == 0) { |
| 263 | if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) |
| 264 | BUG(); |
| 265 | radix_tree_delete(&root->cache_radix, buf->blocknr); |
| 266 | memset(buf, 0, sizeof(*buf)); |
| 267 | free(buf); |
| 268 | BUG_ON(allocated_blocks == 0); |
| 269 | allocated_blocks--; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 270 | BUG_ON(root->cache_size == 0); |
| 271 | root->cache_size--; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |