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; |
| 14 | |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 15 | 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] | 16 | { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 17 | if (buf->blocknr != buf->node.header.blocknr) |
| 18 | BUG(); |
| 19 | if (root->node && buf->node.header.parentid != root->node->node.header.parentid) |
| 20 | BUG(); |
| 21 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr) |
| 25 | { |
| 26 | struct tree_buffer *buf; |
| 27 | int ret; |
| 28 | buf = malloc(sizeof(struct tree_buffer)); |
| 29 | if (!buf) |
| 30 | return buf; |
| 31 | allocated_blocks++; |
| 32 | buf->blocknr = blocknr; |
| 33 | buf->count = 1; |
| 34 | radix_tree_preload(GFP_KERNEL); |
| 35 | ret = radix_tree_insert(&root->cache_radix, blocknr, buf); |
| 36 | radix_tree_preload_end(); |
| 37 | if (ret) { |
| 38 | free(buf); |
| 39 | return NULL; |
| 40 | } |
| 41 | return buf; |
| 42 | } |
| 43 | |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 44 | struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 45 | { |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 46 | struct tree_buffer *buf; |
| 47 | buf = radix_tree_lookup(&root->cache_radix, blocknr); |
| 48 | if (buf) { |
| 49 | buf->count++; |
| 50 | } else { |
| 51 | buf = alloc_tree_block(root, blocknr); |
| 52 | if (!buf) { |
| 53 | BUG(); |
| 54 | return NULL; |
| 55 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 56 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 57 | return buf; |
| 58 | } |
| 59 | |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 60 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 61 | struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) |
| 62 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 63 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 64 | struct tree_buffer *buf; |
| 65 | int ret; |
| 66 | |
| 67 | buf = radix_tree_lookup(&root->cache_radix, blocknr); |
| 68 | if (buf) { |
| 69 | buf->count++; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 70 | } else { |
| 71 | buf = alloc_tree_block(root, blocknr); |
| 72 | if (!buf) |
| 73 | return NULL; |
| 74 | ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); |
| 75 | if (ret != CTREE_BLOCKSIZE) { |
| 76 | free(buf); |
| 77 | return NULL; |
| 78 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 79 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 80 | if (check_tree_block(root, buf)) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 81 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 82 | return buf; |
| 83 | } |
| 84 | |
| 85 | int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
| 86 | { |
| 87 | u64 blocknr = buf->blocknr; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 88 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 89 | int ret; |
| 90 | |
| 91 | if (buf->blocknr != buf->node.header.blocknr) |
| 92 | BUG(); |
| 93 | ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); |
| 94 | if (ret != CTREE_BLOCKSIZE) |
| 95 | return ret; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 99 | static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root, |
| 100 | struct ctree_root_info *info, int fp) |
| 101 | { |
| 102 | root->fp = fp; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 103 | root->node = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 104 | root->node = read_tree_block(root, info->tree_root); |
| 105 | root->extent_root = extent_root; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 109 | struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 110 | { |
| 111 | struct ctree_root *root = malloc(sizeof(struct ctree_root)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 112 | struct ctree_root *extent_root = malloc(sizeof(struct ctree_root)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 113 | int fp; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 114 | int ret; |
| 115 | |
Chris Mason | c673024 | 2007-02-26 10:46:55 -0500 | [diff] [blame] | 116 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 117 | if (fp < 0) { |
| 118 | free(root); |
| 119 | return NULL; |
| 120 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 121 | INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); |
| 122 | INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 123 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 124 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 125 | if (ret == 0 || super->root_info.tree_root == 0) { |
| 126 | printf("making new FS!\n"); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 127 | ret = mkfs(fp); |
| 128 | if (ret) |
| 129 | return NULL; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 130 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 131 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 132 | if (ret != sizeof(struct ctree_super_block)) |
| 133 | return NULL; |
| 134 | } |
| 135 | BUG_ON(ret < 0); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 136 | __setup_root(root, extent_root, &super->root_info, fp); |
| 137 | __setup_root(extent_root, extent_root, &super->extent_info, fp); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 138 | return root; |
| 139 | } |
| 140 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 141 | static int __update_root(struct ctree_root *root, struct ctree_root_info *info) |
| 142 | { |
| 143 | info->tree_root = root->node->blocknr; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s) |
| 148 | { |
| 149 | int ret; |
| 150 | __update_root(root, &s->root_info); |
| 151 | __update_root(root->extent_root, &s->extent_info); |
| 152 | ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 153 | if (ret != sizeof(*s)) { |
| 154 | fprintf(stderr, "failed to write new super block err %d\n", ret); |
| 155 | return ret; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 160 | int close_ctree(struct ctree_root *root) |
| 161 | { |
| 162 | close(root->fp); |
| 163 | if (root->node) |
| 164 | tree_block_release(root, root->node); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 165 | if (root->extent_root->node) |
| 166 | tree_block_release(root->extent_root, root->extent_root->node); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 167 | free(root); |
| 168 | printf("on close %d blocks are allocated\n", allocated_blocks); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 172 | void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) |
| 173 | { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 174 | buf->count--; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 175 | if (buf->count < 0) |
| 176 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 177 | if (buf->count == 0) { |
| 178 | if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) |
| 179 | BUG(); |
| 180 | radix_tree_delete(&root->cache_radix, buf->blocknr); |
| 181 | memset(buf, 0, sizeof(*buf)); |
| 182 | free(buf); |
| 183 | BUG_ON(allocated_blocks == 0); |
| 184 | allocated_blocks--; |
| 185 | } |
| 186 | } |
| 187 | |