blob: c34c0c60935f61cb5390a7b9e41726e23f4303b0 [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_max = 10000;
Chris Masoneb60cea2007-02-02 09:18:22 -050015
Chris Mason234b63a2007-03-13 10:46:10 -040016static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050017{
Chris Mason7518a232007-03-12 12:01:18 -040018 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Mason9a8dd152007-02-23 08:38:36 -050019 BUG();
Chris Mason7518a232007-03-12 12:01:18 -040020 if (root->node && btrfs_header_parentid(&buf->node.header) !=
21 btrfs_header_parentid(&root->node->node.header))
Chris Mason9a8dd152007-02-23 08:38:36 -050022 BUG();
23 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050024}
25
Chris Mason234b63a2007-03-13 10:46:10 -040026static int free_some_buffers(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -050027{
28 struct list_head *node, *next;
Chris Mason234b63a2007-03-13 10:46:10 -040029 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -050030 if (root->cache_size < cache_max)
31 return 0;
32 list_for_each_safe(node, next, &root->cache) {
Chris Mason234b63a2007-03-13 10:46:10 -040033 b = list_entry(node, struct btrfs_buffer, cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -050034 if (b->count == 1) {
35 BUG_ON(!list_empty(&b->dirty));
36 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -040037 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -050038 if (root->cache_size < cache_max)
Chris Mason77ce6842007-03-02 10:06:43 -050039 break;
Chris Masoned2ff2c2007-03-01 18:59:40 -050040 }
41 }
42 return 0;
43}
44
Chris Mason234b63a2007-03-13 10:46:10 -040045struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050046{
Chris Mason234b63a2007-03-13 10:46:10 -040047 struct btrfs_buffer *buf;
Chris Masoneb60cea2007-02-02 09:18:22 -050048 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -040049 buf = malloc(sizeof(struct btrfs_buffer));
Chris Masoneb60cea2007-02-02 09:18:22 -050050 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 Mason234b63a2007-03-13 10:46:10 -040069struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050070{
Chris Mason234b63a2007-03-13 10:46:10 -040071 struct btrfs_buffer *buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050072 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
Chris Mason234b63a2007-03-13 10:46:10 -040085struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050086{
Chris Mason234b63a2007-03-13 10:46:10 -040087 loff_t offset = blocknr * BTRFS_BLOCKSIZE;
88 struct btrfs_buffer *buf;
Chris Masoneb60cea2007-02-02 09:18:22 -050089 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;
Chris Mason234b63a2007-03-13 10:46:10 -040098 ret = pread(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
99 if (ret != BTRFS_BLOCKSIZE) {
Chris Mason9a8dd152007-02-23 08:38:36 -0500100 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 Mason234b63a2007-03-13 10:46:10 -0400109int dirty_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500110{
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
Chris Mason234b63a2007-03-13 10:46:10 -0400118int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500119{
120 if (!list_empty(&buf->dirty)) {
121 list_del_init(&buf->dirty);
Chris Mason234b63a2007-03-13 10:46:10 -0400122 btrfs_block_release(root, buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500123 }
124 return 0;
125}
126
Chris Mason234b63a2007-03-13 10:46:10 -0400127int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500128{
129 u64 blocknr = buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400130 loff_t offset = blocknr * BTRFS_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500131 int ret;
132
Chris Mason7518a232007-03-12 12:01:18 -0400133 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 BUG();
Chris Mason234b63a2007-03-13 10:46:10 -0400135 ret = pwrite(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
136 if (ret != BTRFS_BLOCKSIZE)
Chris Masoneb60cea2007-02-02 09:18:22 -0500137 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 return 0;
139}
140
Chris Mason234b63a2007-03-13 10:46:10 -0400141static int __commit_transaction(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500142{
Chris Mason234b63a2007-03-13 10:46:10 -0400143 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500144 int ret = 0;
145 int wret;
146 while(!list_empty(&root->trans)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400147 b = list_entry(root->trans.next, struct btrfs_buffer, dirty);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500148 list_del_init(&b->dirty);
149 wret = write_tree_block(root, b);
150 if (wret)
151 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400152 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500153 }
154 return ret;
155}
156
Chris Mason234b63a2007-03-13 10:46:10 -0400157int btrfs_commit_transaction(struct btrfs_root *root,
158 struct btrfs_super_block *s)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500159{
Chris Masona28ec192007-03-06 20:08:01 -0500160 int ret = 0;
161
Chris Masoned2ff2c2007-03-01 18:59:40 -0500162 ret = __commit_transaction(root);
163 if (!ret && root != root->extent_root)
164 ret = __commit_transaction(root->extent_root);
165 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500166 if (root->commit_root != root->node) {
Chris Mason234b63a2007-03-13 10:46:10 -0400167 struct btrfs_buffer *snap = root->commit_root;
Chris Masona28ec192007-03-06 20:08:01 -0500168 root->commit_root = root->node;
169 root->node->count++;
170 ret = btrfs_drop_snapshot(root, snap);
171 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400172 // btrfs_block_release(root, snap);
Chris Masona28ec192007-03-06 20:08:01 -0500173 }
174 write_ctree_super(root, s);
175 btrfs_finish_extent_commit(root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500176 return ret;
177}
178
Chris Mason234b63a2007-03-13 10:46:10 -0400179static int __setup_root(struct btrfs_root *root, struct btrfs_root *extent_root,
180 struct btrfs_root_info *info, int fp)
Chris Masond97e63b2007-02-20 16:40:44 -0500181{
Chris Masoned2ff2c2007-03-01 18:59:40 -0500182 INIT_LIST_HEAD(&root->trans);
183 INIT_LIST_HEAD(&root->cache);
Chris Masona28ec192007-03-06 20:08:01 -0500184 root->cache_size = 0;
Chris Masond97e63b2007-02-20 16:40:44 -0500185 root->fp = fp;
Chris Masoncfaa7292007-02-21 17:04:57 -0500186 root->node = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -0500187 root->extent_root = extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500188 root->commit_root = NULL;
189 root->node = read_tree_block(root, info->tree_root);
190 memset(&root->current_insert, 0, sizeof(root->current_insert));
Chris Mason0579da42007-03-07 16:15:30 -0500191 memset(&root->last_insert, 0, sizeof(root->last_insert));
Chris Masond97e63b2007-02-20 16:40:44 -0500192 return 0;
193}
194
Chris Mason234b63a2007-03-13 10:46:10 -0400195struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500196{
Chris Mason234b63a2007-03-13 10:46:10 -0400197 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
198 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
Chris Masoneb60cea2007-02-02 09:18:22 -0500199 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500200 int ret;
201
Chris Masonc6730242007-02-26 10:46:55 -0500202 fp = open(filename, O_CREAT | O_RDWR, 0600);
Chris Masoneb60cea2007-02-02 09:18:22 -0500203 if (fp < 0) {
204 free(root);
205 return NULL;
206 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500207 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
Chris Masona28ec192007-03-06 20:08:01 -0500208 INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
209 INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
Chris Mason9a8dd152007-02-23 08:38:36 -0500210 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
Chris Mason234b63a2007-03-13 10:46:10 -0400211 ret = pread(fp, super, sizeof(struct btrfs_super_block),
212 BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
Chris Mason5c680ed2007-02-22 11:39:13 -0500213 if (ret == 0 || super->root_info.tree_root == 0) {
214 printf("making new FS!\n");
Chris Masond97e63b2007-02-20 16:40:44 -0500215 ret = mkfs(fp);
216 if (ret)
217 return NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400218 ret = pread(fp, super, sizeof(struct btrfs_super_block),
219 BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
220 if (ret != sizeof(struct btrfs_super_block))
Chris Masond97e63b2007-02-20 16:40:44 -0500221 return NULL;
222 }
223 BUG_ON(ret < 0);
Chris Masoncfaa7292007-02-21 17:04:57 -0500224 __setup_root(root, extent_root, &super->root_info, fp);
225 __setup_root(extent_root, extent_root, &super->extent_info, fp);
Chris Masona28ec192007-03-06 20:08:01 -0500226 root->commit_root = root->node;
227 root->node->count++;
Chris Masoneb60cea2007-02-02 09:18:22 -0500228 return root;
229}
230
Chris Mason234b63a2007-03-13 10:46:10 -0400231static int __update_root(struct btrfs_root *root, struct btrfs_root_info *info)
Chris Masoncfaa7292007-02-21 17:04:57 -0500232{
233 info->tree_root = root->node->blocknr;
Chris Masoncfaa7292007-02-21 17:04:57 -0500234 return 0;
235}
236
Chris Mason234b63a2007-03-13 10:46:10 -0400237int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s)
Chris Masoncfaa7292007-02-21 17:04:57 -0500238{
239 int ret;
240 __update_root(root, &s->root_info);
241 __update_root(root->extent_root, &s->extent_info);
Chris Mason234b63a2007-03-13 10:46:10 -0400242 ret = pwrite(root->fp, s, sizeof(*s),
243 BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
Chris Masoncfaa7292007-02-21 17:04:57 -0500244 if (ret != sizeof(*s)) {
245 fprintf(stderr, "failed to write new super block err %d\n", ret);
246 return ret;
247 }
248 return 0;
249}
250
Chris Mason234b63a2007-03-13 10:46:10 -0400251static int drop_cache(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500252{
253 while(!list_empty(&root->cache)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400254 struct btrfs_buffer *b = list_entry(root->cache.next,
255 struct btrfs_buffer, cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500256 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -0400257 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500258 }
259 return 0;
260}
Chris Mason234b63a2007-03-13 10:46:10 -0400261int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
Chris Masoneb60cea2007-02-02 09:18:22 -0500262{
Chris Mason234b63a2007-03-13 10:46:10 -0400263 btrfs_commit_transaction(root, s);
Chris Masona28ec192007-03-06 20:08:01 -0500264 __commit_transaction(root->extent_root);
265 write_ctree_super(root, s);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500266 drop_cache(root->extent_root);
267 drop_cache(root);
268 BUG_ON(!list_empty(&root->trans));
269 BUG_ON(!list_empty(&root->extent_root->trans));
270
Chris Masoneb60cea2007-02-02 09:18:22 -0500271 close(root->fp);
272 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400273 btrfs_block_release(root, root->node);
Chris Masoncfaa7292007-02-21 17:04:57 -0500274 if (root->extent_root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400275 btrfs_block_release(root->extent_root, root->extent_root->node);
276 btrfs_block_release(root, root->commit_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500277 free(root);
278 printf("on close %d blocks are allocated\n", allocated_blocks);
279 return 0;
280}
281
Chris Mason234b63a2007-03-13 10:46:10 -0400282void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500283{
Chris Masoneb60cea2007-02-02 09:18:22 -0500284 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500285 if (buf->count < 0)
286 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500287 if (buf->count == 0) {
Chris Mason02217ed2007-03-02 16:08:05 -0500288 BUG_ON(!list_empty(&buf->cache));
289 BUG_ON(!list_empty(&buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500290 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
291 BUG();
292 radix_tree_delete(&root->cache_radix, buf->blocknr);
293 memset(buf, 0, sizeof(*buf));
294 free(buf);
295 BUG_ON(allocated_blocks == 0);
296 allocated_blocks--;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500297 BUG_ON(root->cache_size == 0);
298 root->cache_size--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500299 }
300}
301