blob: c42dc72706bf08d226893601c0d9206ac9f846e5 [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;
14
Chris Mason9a8dd152007-02-23 08:38:36 -050015static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050016{
Chris Mason9a8dd152007-02-23 08:38:36 -050017 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 Masoneb60cea2007-02-02 09:18:22 -050022}
23
24struct 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 Mason9a8dd152007-02-23 08:38:36 -050044struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050045{
Chris Mason9a8dd152007-02-23 08:38:36 -050046 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 Masoneb60cea2007-02-02 09:18:22 -050056 }
Chris Masoneb60cea2007-02-02 09:18:22 -050057 return buf;
58}
59
Chris Mason9a8dd152007-02-23 08:38:36 -050060
Chris Masoneb60cea2007-02-02 09:18:22 -050061struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
62{
Chris Masond97e63b2007-02-20 16:40:44 -050063 loff_t offset = blocknr * CTREE_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -050064 struct tree_buffer *buf;
65 int ret;
66
67 buf = radix_tree_lookup(&root->cache_radix, blocknr);
68 if (buf) {
69 buf->count++;
Chris Mason9a8dd152007-02-23 08:38:36 -050070 } 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 Masoneb60cea2007-02-02 09:18:22 -050079 }
Chris Mason9a8dd152007-02-23 08:38:36 -050080 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -050081 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -050082 return buf;
83}
84
85int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
86{
87 u64 blocknr = buf->blocknr;
Chris Masond97e63b2007-02-20 16:40:44 -050088 loff_t offset = blocknr * CTREE_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -050089 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 Masoneb60cea2007-02-02 09:18:22 -050096 return 0;
97}
98
Chris Masond97e63b2007-02-20 16:40:44 -050099static 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 Masoncfaa7292007-02-21 17:04:57 -0500103 root->node = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -0500104 root->node = read_tree_block(root, info->tree_root);
105 root->extent_root = extent_root;
Chris Masond97e63b2007-02-20 16:40:44 -0500106 return 0;
107}
108
Chris Masoncfaa7292007-02-21 17:04:57 -0500109struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500110{
111 struct ctree_root *root = malloc(sizeof(struct ctree_root));
Chris Masond97e63b2007-02-20 16:40:44 -0500112 struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
Chris Masoneb60cea2007-02-02 09:18:22 -0500113 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500114 int ret;
115
Chris Masonc6730242007-02-26 10:46:55 -0500116 fp = open(filename, O_CREAT | O_RDWR, 0600);
Chris Masoneb60cea2007-02-02 09:18:22 -0500117 if (fp < 0) {
118 free(root);
119 return NULL;
120 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500121 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
122 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
Chris Masoncfaa7292007-02-21 17:04:57 -0500123 ret = pread(fp, super, sizeof(struct ctree_super_block),
Chris Masond97e63b2007-02-20 16:40:44 -0500124 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
Chris Mason5c680ed2007-02-22 11:39:13 -0500125 if (ret == 0 || super->root_info.tree_root == 0) {
126 printf("making new FS!\n");
Chris Masond97e63b2007-02-20 16:40:44 -0500127 ret = mkfs(fp);
128 if (ret)
129 return NULL;
Chris Masoncfaa7292007-02-21 17:04:57 -0500130 ret = pread(fp, super, sizeof(struct ctree_super_block),
Chris Masond97e63b2007-02-20 16:40:44 -0500131 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
132 if (ret != sizeof(struct ctree_super_block))
133 return NULL;
134 }
135 BUG_ON(ret < 0);
Chris Masoncfaa7292007-02-21 17:04:57 -0500136 __setup_root(root, extent_root, &super->root_info, fp);
137 __setup_root(extent_root, extent_root, &super->extent_info, fp);
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 return root;
139}
140
Chris Masoncfaa7292007-02-21 17:04:57 -0500141static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
142{
143 info->tree_root = root->node->blocknr;
Chris Masoncfaa7292007-02-21 17:04:57 -0500144 return 0;
145}
146
147int 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 Masoneb60cea2007-02-02 09:18:22 -0500160int close_ctree(struct ctree_root *root)
161{
162 close(root->fp);
163 if (root->node)
164 tree_block_release(root, root->node);
Chris Masoncfaa7292007-02-21 17:04:57 -0500165 if (root->extent_root->node)
166 tree_block_release(root->extent_root, root->extent_root->node);
Chris Masoneb60cea2007-02-02 09:18:22 -0500167 free(root);
168 printf("on close %d blocks are allocated\n", allocated_blocks);
169 return 0;
170}
171
Chris Masoneb60cea2007-02-02 09:18:22 -0500172void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
173{
Chris Masoneb60cea2007-02-02 09:18:22 -0500174 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500175 if (buf->count < 0)
176 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500177 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