1 #define _XOPEN_SOURCE 500
8 #include "kerncompat.h"
9 #include "radix-tree.h"
13 static int allocated_blocks
= 0;
15 static int get_free_block(struct ctree_root
*root
, u64
*block
)
20 if (root
->alloc_extent
->num_used
>= root
->alloc_extent
->num_blocks
)
23 *block
= root
->alloc_extent
->blocknr
+ root
->alloc_extent
->num_used
;
24 root
->alloc_extent
->num_used
+= 1;
25 if (root
->alloc_extent
->num_used
>= root
->alloc_extent
->num_blocks
) {
26 struct alloc_extent
*ae
= root
->alloc_extent
;
27 root
->alloc_extent
= root
->reserve_extent
;
28 root
->reserve_extent
= ae
;
32 ret
= fstat(root
->fp
, &st
);
33 if (st
.st_size
< (*block
+ 1) * CTREE_BLOCKSIZE
)
34 ret
= ftruncate(root
->fp
,
35 (*block
+ 1) * CTREE_BLOCKSIZE
);
39 struct tree_buffer
*alloc_tree_block(struct ctree_root
*root
, u64 blocknr
)
41 struct tree_buffer
*buf
;
43 buf
= malloc(sizeof(struct tree_buffer
));
47 buf
->blocknr
= blocknr
;
49 radix_tree_preload(GFP_KERNEL
);
50 ret
= radix_tree_insert(&root
->cache_radix
, blocknr
, buf
);
51 radix_tree_preload_end();
59 struct tree_buffer
*alloc_free_block(struct ctree_root
*root
)
63 struct tree_buffer
* buf
;
64 ret
= get_free_block(root
, &free_block
);
69 buf
= alloc_tree_block(root
, free_block
);
75 struct tree_buffer
*read_tree_block(struct ctree_root
*root
, u64 blocknr
)
77 loff_t offset
= blocknr
* CTREE_BLOCKSIZE
;
78 struct tree_buffer
*buf
;
81 buf
= radix_tree_lookup(&root
->cache_radix
, blocknr
);
84 if (buf
->blocknr
!= blocknr
)
86 if (buf
->blocknr
!= buf
->node
.header
.blocknr
)
90 buf
= alloc_tree_block(root
, blocknr
);
93 ret
= pread(root
->fp
, &buf
->node
, CTREE_BLOCKSIZE
, offset
);
94 if (ret
!= CTREE_BLOCKSIZE
) {
98 if (buf
->blocknr
!= buf
->node
.header
.blocknr
)
103 int write_tree_block(struct ctree_root
*root
, struct tree_buffer
*buf
)
105 u64 blocknr
= buf
->blocknr
;
106 loff_t offset
= blocknr
* CTREE_BLOCKSIZE
;
109 if (buf
->blocknr
!= buf
->node
.header
.blocknr
)
111 ret
= pwrite(root
->fp
, &buf
->node
, CTREE_BLOCKSIZE
, offset
);
112 if (ret
!= CTREE_BLOCKSIZE
)
114 if (buf
== root
->node
)
115 return update_root_block(root
);
119 struct ctree_super_block
{
120 struct ctree_root_info root_info
;
121 struct ctree_root_info extent_info
;
122 } __attribute__ ((__packed__
));
124 static int __setup_root(struct ctree_root
*root
, struct ctree_root
*extent_root
,
125 struct ctree_root_info
*info
, int fp
)
128 root
->node
= read_tree_block(root
, info
->tree_root
);
129 root
->extent_root
= extent_root
;
130 memcpy(&root
->ai1
, &info
->alloc_extent
, sizeof(info
->alloc_extent
));
131 memcpy(&root
->ai2
, &info
->reserve_extent
, sizeof(info
->reserve_extent
));
132 root
->alloc_extent
= &root
->ai1
;
133 root
->reserve_extent
= &root
->ai2
;
134 INIT_RADIX_TREE(&root
->cache_radix
, GFP_KERNEL
);
135 printf("setup done reading root %p, used %lu\n", root
, root
->alloc_extent
->num_used
);
139 struct ctree_root
*open_ctree(char *filename
)
141 struct ctree_root
*root
= malloc(sizeof(struct ctree_root
));
142 struct ctree_root
*extent_root
= malloc(sizeof(struct ctree_root
));
143 struct ctree_super_block super
;
147 fp
= open(filename
, O_CREAT
| O_RDWR
);
152 ret
= pread(fp
, &super
, sizeof(struct ctree_super_block
),
153 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE
));
158 ret
= pread(fp
, &super
, sizeof(struct ctree_super_block
),
159 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE
));
160 if (ret
!= sizeof(struct ctree_super_block
))
164 __setup_root(root
, extent_root
, &super
.root_info
, fp
);
165 __setup_root(extent_root
, extent_root
, &super
.extent_info
, fp
);
169 int close_ctree(struct ctree_root
*root
)
173 tree_block_release(root
, root
->node
);
175 printf("on close %d blocks are allocated\n", allocated_blocks
);
179 int update_root_block(struct ctree_root
*root
)
182 u64 root_block
= root
->node
->blocknr
;
184 ret
= pwrite(root
->fp
, &root_block
, sizeof(u64
), 0);
185 if (ret
!= sizeof(u64
))
190 void tree_block_release(struct ctree_root
*root
, struct tree_buffer
*buf
)
194 if (buf
->count
== 0) {
195 if (!radix_tree_lookup(&root
->cache_radix
, buf
->blocknr
))
197 radix_tree_delete(&root
->cache_radix
, buf
->blocknr
);
198 memset(buf
, 0, sizeof(*buf
));
200 BUG_ON(allocated_blocks
== 0);