blob: f7ca5362291ee4c50319f24bcfccd6dc4c3d8cc5 [file] [log] [blame]
Chris Masoneb60cea2007-02-02 09:18:22 -05001#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
13static int allocated_blocks = 0;
Chris Masoned2ff2c2007-03-01 18:59:40 -050014int cache_size = 0;
15int cache_max = 10000;
Chris Masoneb60cea2007-02-02 09:18:22 -050016
Chris Mason9a8dd152007-02-23 08:38:36 -050017static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050018{
Chris Mason9a8dd152007-02-23 08:38:36 -050019 if (buf->blocknr != buf->node.header.blocknr)
20 BUG();
21 if (root->node && buf->node.header.parentid != root->node->node.header.parentid)
22 BUG();
23 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050024}
25
Chris Masoned2ff2c2007-03-01 18:59:40 -050026static 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)
39 return 0;
40 }
41 }
42 return 0;
43}
44
Chris Masoneb60cea2007-02-02 09:18:22 -050045struct 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 Masoned2ff2c2007-03-01 18:59:40 -050054 buf->count = 2;
55 INIT_LIST_HEAD(&buf->dirty);
56 free_some_buffers(root);
Chris Masoneb60cea2007-02-02 09:18:22 -050057 radix_tree_preload(GFP_KERNEL);
58 ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
59 radix_tree_preload_end();
Chris Masoned2ff2c2007-03-01 18:59:40 -050060 list_add_tail(&buf->cache, &root->cache);
61 root->cache_size++;
Chris Masoneb60cea2007-02-02 09:18:22 -050062 if (ret) {
63 free(buf);
64 return NULL;
65 }
66 return buf;
67}
68
Chris Mason9a8dd152007-02-23 08:38:36 -050069struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050070{
Chris Mason9a8dd152007-02-23 08:38:36 -050071 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 Masoneb60cea2007-02-02 09:18:22 -050081 }
Chris Masoneb60cea2007-02-02 09:18:22 -050082 return buf;
83}
84
85struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
86{
Chris Masond97e63b2007-02-20 16:40:44 -050087 loff_t offset = blocknr * CTREE_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -050088 struct tree_buffer *buf;
89 int ret;
90
91 buf = radix_tree_lookup(&root->cache_radix, blocknr);
92 if (buf) {
93 buf->count++;
Chris Mason9a8dd152007-02-23 08:38:36 -050094 } 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 Masoneb60cea2007-02-02 09:18:22 -0500103 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500104 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -0500105 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500106 return buf;
107}
108
Chris Masoned2ff2c2007-03-01 18:59:40 -0500109int 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
118int 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 Masoneb60cea2007-02-02 09:18:22 -0500127int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
128{
129 u64 blocknr = buf->blocknr;
Chris Masond97e63b2007-02-20 16:40:44 -0500130 loff_t offset = blocknr * CTREE_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500131 int ret;
132
133 if (buf->blocknr != buf->node.header.blocknr)
134 BUG();
135 ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
136 if (ret != CTREE_BLOCKSIZE)
137 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 return 0;
139}
140
Chris Masoned2ff2c2007-03-01 18:59:40 -0500141static 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
157int commit_transaction(struct ctree_root *root)
158{
159 int ret;
160 ret = __commit_transaction(root);
161 if (!ret && root != root->extent_root)
162 ret = __commit_transaction(root->extent_root);
163 BUG_ON(ret);
164 return ret;
165}
166
Chris Masond97e63b2007-02-20 16:40:44 -0500167static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
168 struct ctree_root_info *info, int fp)
169{
Chris Masoned2ff2c2007-03-01 18:59:40 -0500170 INIT_LIST_HEAD(&root->trans);
171 INIT_LIST_HEAD(&root->cache);
Chris Masond97e63b2007-02-20 16:40:44 -0500172 root->fp = fp;
Chris Masoncfaa7292007-02-21 17:04:57 -0500173 root->node = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -0500174 root->node = read_tree_block(root, info->tree_root);
175 root->extent_root = extent_root;
Chris Masond97e63b2007-02-20 16:40:44 -0500176 return 0;
177}
178
Chris Masoncfaa7292007-02-21 17:04:57 -0500179struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500180{
181 struct ctree_root *root = malloc(sizeof(struct ctree_root));
Chris Masond97e63b2007-02-20 16:40:44 -0500182 struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
Chris Masoneb60cea2007-02-02 09:18:22 -0500183 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500184 int ret;
185
Chris Masonc6730242007-02-26 10:46:55 -0500186 fp = open(filename, O_CREAT | O_RDWR, 0600);
Chris Masoneb60cea2007-02-02 09:18:22 -0500187 if (fp < 0) {
188 free(root);
189 return NULL;
190 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500191 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
192 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
Chris Masoncfaa7292007-02-21 17:04:57 -0500193 ret = pread(fp, super, sizeof(struct ctree_super_block),
Chris Masond97e63b2007-02-20 16:40:44 -0500194 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
Chris Mason5c680ed2007-02-22 11:39:13 -0500195 if (ret == 0 || super->root_info.tree_root == 0) {
196 printf("making new FS!\n");
Chris Masond97e63b2007-02-20 16:40:44 -0500197 ret = mkfs(fp);
198 if (ret)
199 return NULL;
Chris Masoncfaa7292007-02-21 17:04:57 -0500200 ret = pread(fp, super, sizeof(struct ctree_super_block),
Chris Masond97e63b2007-02-20 16:40:44 -0500201 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
202 if (ret != sizeof(struct ctree_super_block))
203 return NULL;
204 }
205 BUG_ON(ret < 0);
Chris Masoncfaa7292007-02-21 17:04:57 -0500206 __setup_root(root, extent_root, &super->root_info, fp);
207 __setup_root(extent_root, extent_root, &super->extent_info, fp);
Chris Masoneb60cea2007-02-02 09:18:22 -0500208 return root;
209}
210
Chris Masoncfaa7292007-02-21 17:04:57 -0500211static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
212{
213 info->tree_root = root->node->blocknr;
Chris Masoncfaa7292007-02-21 17:04:57 -0500214 return 0;
215}
216
217int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
218{
219 int ret;
220 __update_root(root, &s->root_info);
221 __update_root(root->extent_root, &s->extent_info);
222 ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
223 if (ret != sizeof(*s)) {
224 fprintf(stderr, "failed to write new super block err %d\n", ret);
225 return ret;
226 }
227 return 0;
228}
229
Chris Masoned2ff2c2007-03-01 18:59:40 -0500230static int drop_cache(struct ctree_root *root)
231{
232 while(!list_empty(&root->cache)) {
233 struct tree_buffer *b = list_entry(root->cache.next,
234 struct tree_buffer, cache);
235 list_del_init(&b->cache);
236 tree_block_release(root, b);
237 }
238 return 0;
239}
Chris Masoneb60cea2007-02-02 09:18:22 -0500240int close_ctree(struct ctree_root *root)
241{
Chris Masonf0930a32007-03-02 09:47:58 -0500242 commit_transaction(root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500243 drop_cache(root->extent_root);
244 drop_cache(root);
245 BUG_ON(!list_empty(&root->trans));
246 BUG_ON(!list_empty(&root->extent_root->trans));
247
Chris Masoneb60cea2007-02-02 09:18:22 -0500248 close(root->fp);
249 if (root->node)
250 tree_block_release(root, root->node);
Chris Masoncfaa7292007-02-21 17:04:57 -0500251 if (root->extent_root->node)
252 tree_block_release(root->extent_root, root->extent_root->node);
Chris Masoneb60cea2007-02-02 09:18:22 -0500253 free(root);
254 printf("on close %d blocks are allocated\n", allocated_blocks);
255 return 0;
256}
257
Chris Masoneb60cea2007-02-02 09:18:22 -0500258void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
259{
Chris Masoneb60cea2007-02-02 09:18:22 -0500260 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500261 if (buf->count < 0)
262 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500263 if (buf->count == 0) {
264 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
265 BUG();
266 radix_tree_delete(&root->cache_radix, buf->blocknr);
267 memset(buf, 0, sizeof(*buf));
268 free(buf);
269 BUG_ON(allocated_blocks == 0);
270 allocated_blocks--;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500271 BUG_ON(root->cache_size == 0);
272 root->cache_size--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500273 }
274}
275