Commit | Line | Data |
---|---|---|
eb60ceac CM |
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 | ||
9a8dd150 | 15 | static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
eb60ceac | 16 | { |
9a8dd150 CM |
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; | |
eb60ceac CM |
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 | ||
9a8dd150 | 44 | struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr) |
eb60ceac | 45 | { |
9a8dd150 CM |
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 | } | |
eb60ceac | 56 | } |
eb60ceac CM |
57 | return buf; |
58 | } | |
59 | ||
9a8dd150 | 60 | |
eb60ceac CM |
61 | struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) |
62 | { | |
d97e63b6 | 63 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
eb60ceac CM |
64 | struct tree_buffer *buf; |
65 | int ret; | |
66 | ||
67 | buf = radix_tree_lookup(&root->cache_radix, blocknr); | |
68 | if (buf) { | |
69 | buf->count++; | |
9a8dd150 CM |
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 | } | |
eb60ceac | 79 | } |
9a8dd150 | 80 | if (check_tree_block(root, buf)) |
cfaa7295 | 81 | BUG(); |
eb60ceac CM |
82 | return buf; |
83 | } | |
84 | ||
85 | int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) | |
86 | { | |
87 | u64 blocknr = buf->blocknr; | |
d97e63b6 | 88 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
eb60ceac CM |
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; | |
eb60ceac CM |
96 | return 0; |
97 | } | |
98 | ||
d97e63b6 CM |
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; | |
cfaa7295 | 103 | root->node = NULL; |
d97e63b6 CM |
104 | root->node = read_tree_block(root, info->tree_root); |
105 | root->extent_root = extent_root; | |
d97e63b6 CM |
106 | return 0; |
107 | } | |
108 | ||
cfaa7295 | 109 | struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super) |
eb60ceac CM |
110 | { |
111 | struct ctree_root *root = malloc(sizeof(struct ctree_root)); | |
d97e63b6 | 112 | struct ctree_root *extent_root = malloc(sizeof(struct ctree_root)); |
eb60ceac | 113 | int fp; |
eb60ceac CM |
114 | int ret; |
115 | ||
c673024a | 116 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
eb60ceac CM |
117 | if (fp < 0) { |
118 | free(root); | |
119 | return NULL; | |
120 | } | |
9a8dd150 CM |
121 | INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); |
122 | INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); | |
cfaa7295 | 123 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
d97e63b6 | 124 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
5c680ed6 CM |
125 | if (ret == 0 || super->root_info.tree_root == 0) { |
126 | printf("making new FS!\n"); | |
d97e63b6 CM |
127 | ret = mkfs(fp); |
128 | if (ret) | |
129 | return NULL; | |
cfaa7295 | 130 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
d97e63b6 CM |
131 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
132 | if (ret != sizeof(struct ctree_super_block)) | |
133 | return NULL; | |
134 | } | |
135 | BUG_ON(ret < 0); | |
cfaa7295 CM |
136 | __setup_root(root, extent_root, &super->root_info, fp); |
137 | __setup_root(extent_root, extent_root, &super->extent_info, fp); | |
eb60ceac CM |
138 | return root; |
139 | } | |
140 | ||
cfaa7295 | 141 | static int __update_root(struct ctree_root *root, struct ctree_root_info *info) |
eb60ceac | 142 | { |
cfaa7295 | 143 | info->tree_root = root->node->blocknr; |
eb60ceac CM |
144 | return 0; |
145 | } | |
146 | ||
cfaa7295 | 147 | int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s) |
eb60ceac CM |
148 | { |
149 | int ret; | |
cfaa7295 CM |
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); | |
eb60ceac | 155 | return ret; |
cfaa7295 CM |
156 | } |
157 | return 0; | |
158 | } | |
159 | ||
160 | int close_ctree(struct ctree_root *root) | |
161 | { | |
162 | close(root->fp); | |
163 | if (root->node) | |
164 | tree_block_release(root, root->node); | |
165 | if (root->extent_root->node) | |
166 | tree_block_release(root->extent_root, root->extent_root->node); | |
167 | free(root); | |
168 | printf("on close %d blocks are allocated\n", allocated_blocks); | |
eb60ceac CM |
169 | return 0; |
170 | } | |
171 | ||
172 | void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) | |
173 | { | |
174 | buf->count--; | |
cfaa7295 CM |
175 | if (buf->count < 0) |
176 | BUG(); | |
eb60ceac CM |
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 |