1 #define _XOPEN_SOURCE 500
8 #include "kerncompat.h"
9 #include "radix-tree.h"
13 static int allocated_blocks
= 0;
15 int cache_max
= 10000;
17 static int check_tree_block(struct ctree_root
*root
, struct tree_buffer
*buf
)
19 if (buf
->blocknr
!= buf
->node
.header
.blocknr
)
21 if (root
->node
&& buf
->node
.header
.parentid
!= root
->node
->node
.header
.parentid
)
26 static int free_some_buffers(struct ctree_root
*root
)
28 struct list_head
*node
, *next
;
29 struct tree_buffer
*b
;
30 if (root
->cache_size
< cache_max
)
32 list_for_each_safe(node
, next
, &root
->cache
) {
33 b
= list_entry(node
, struct tree_buffer
, cache
);
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
)
45 struct tree_buffer
*alloc_tree_block(struct ctree_root
*root
, u64 blocknr
)
47 struct tree_buffer
*buf
;
49 buf
= malloc(sizeof(struct tree_buffer
));
53 buf
->blocknr
= blocknr
;
55 INIT_LIST_HEAD(&buf
->dirty
);
56 free_some_buffers(root
);
57 radix_tree_preload(GFP_KERNEL
);
58 ret
= radix_tree_insert(&root
->cache_radix
, blocknr
, buf
);
59 radix_tree_preload_end();
60 list_add_tail(&buf
->cache
, &root
->cache
);
69 struct tree_buffer
*find_tree_block(struct ctree_root
*root
, u64 blocknr
)
71 struct tree_buffer
*buf
;
72 buf
= radix_tree_lookup(&root
->cache_radix
, blocknr
);
76 buf
= alloc_tree_block(root
, blocknr
);
85 struct tree_buffer
*read_tree_block(struct ctree_root
*root
, u64 blocknr
)
87 loff_t offset
= blocknr
* CTREE_BLOCKSIZE
;
88 struct tree_buffer
*buf
;
91 buf
= radix_tree_lookup(&root
->cache_radix
, blocknr
);
95 buf
= alloc_tree_block(root
, blocknr
);
98 ret
= pread(root
->fp
, &buf
->node
, CTREE_BLOCKSIZE
, offset
);
99 if (ret
!= CTREE_BLOCKSIZE
) {
104 if (check_tree_block(root
, buf
))
109 int dirty_tree_block(struct ctree_root
*root
, struct tree_buffer
*buf
)
111 if (!list_empty(&buf
->dirty
))
113 list_add_tail(&buf
->dirty
, &root
->trans
);
118 int clean_tree_block(struct ctree_root
*root
, struct tree_buffer
*buf
)
120 if (!list_empty(&buf
->dirty
)) {
121 list_del_init(&buf
->dirty
);
122 tree_block_release(root
, buf
);
127 int write_tree_block(struct ctree_root
*root
, struct tree_buffer
*buf
)
129 u64 blocknr
= buf
->blocknr
;
130 loff_t offset
= blocknr
* CTREE_BLOCKSIZE
;
133 if (buf
->blocknr
!= buf
->node
.header
.blocknr
)
135 ret
= pwrite(root
->fp
, &buf
->node
, CTREE_BLOCKSIZE
, offset
);
136 if (ret
!= CTREE_BLOCKSIZE
)
141 static int __commit_transaction(struct ctree_root
*root
)
143 struct tree_buffer
*b
;
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
);
152 tree_block_release(root
, b
);
157 int commit_transaction(struct ctree_root
*root
)
160 ret
= __commit_transaction(root
);
161 if (!ret
&& root
!= root
->extent_root
)
162 ret
= __commit_transaction(root
->extent_root
);
167 static int __setup_root(struct ctree_root
*root
, struct ctree_root
*extent_root
,
168 struct ctree_root_info
*info
, int fp
)
170 INIT_LIST_HEAD(&root
->trans
);
171 INIT_LIST_HEAD(&root
->cache
);
174 root
->node
= read_tree_block(root
, info
->tree_root
);
175 root
->extent_root
= extent_root
;
179 struct ctree_root
*open_ctree(char *filename
, struct ctree_super_block
*super
)
181 struct ctree_root
*root
= malloc(sizeof(struct ctree_root
));
182 struct ctree_root
*extent_root
= malloc(sizeof(struct ctree_root
));
186 fp
= open(filename
, O_CREAT
| O_RDWR
, 0600);
191 INIT_RADIX_TREE(&root
->cache_radix
, GFP_KERNEL
);
192 INIT_RADIX_TREE(&extent_root
->cache_radix
, GFP_KERNEL
);
193 ret
= pread(fp
, super
, sizeof(struct ctree_super_block
),
194 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE
));
195 if (ret
== 0 || super
->root_info
.tree_root
== 0) {
196 printf("making new FS!\n");
200 ret
= pread(fp
, super
, sizeof(struct ctree_super_block
),
201 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE
));
202 if (ret
!= sizeof(struct ctree_super_block
))
206 __setup_root(root
, extent_root
, &super
->root_info
, fp
);
207 __setup_root(extent_root
, extent_root
, &super
->extent_info
, fp
);
211 static int __update_root(struct ctree_root
*root
, struct ctree_root_info
*info
)
213 info
->tree_root
= root
->node
->blocknr
;
217 int write_ctree_super(struct ctree_root
*root
, struct ctree_super_block
*s
)
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
);
230 static int drop_cache(struct ctree_root
*root
)
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
);
240 int close_ctree(struct ctree_root
*root
)
242 commit_transaction(root
);
243 drop_cache(root
->extent_root
);
245 BUG_ON(!list_empty(&root
->trans
));
246 BUG_ON(!list_empty(&root
->extent_root
->trans
));
250 tree_block_release(root
, root
->node
);
251 if (root
->extent_root
->node
)
252 tree_block_release(root
->extent_root
, root
->extent_root
->node
);
254 printf("on close %d blocks are allocated\n", allocated_blocks
);
258 void tree_block_release(struct ctree_root
*root
, struct tree_buffer
*buf
)
263 if (buf
->count
== 0) {
264 if (!radix_tree_lookup(&root
->cache_radix
, buf
->blocknr
))
266 radix_tree_delete(&root
->cache_radix
, buf
->blocknr
);
267 memset(buf
, 0, sizeof(*buf
));
269 BUG_ON(allocated_blocks
== 0);
271 BUG_ON(root
->cache_size
== 0);
This page took 0.063222 seconds and 5 git commands to generate.