Btrfs: Break up ctree.c a little
[safe/jmp/linux-2.6] / fs / btrfs / disk-io.c
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
15 static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
16 {
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;
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
44 struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
45 {
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                 }
56         }
57         return buf;
58 }
59
60
61 struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
62 {
63         loff_t offset = blocknr * CTREE_BLOCKSIZE;
64         struct tree_buffer *buf;
65         int ret;
66
67         buf = radix_tree_lookup(&root->cache_radix, blocknr);
68         if (buf) {
69                 buf->count++;
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                 }
79         }
80         if (check_tree_block(root, buf))
81                 BUG();
82         return buf;
83 }
84
85 int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
86 {
87         u64 blocknr = buf->blocknr;
88         loff_t offset = blocknr * CTREE_BLOCKSIZE;
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;
96         return 0;
97 }
98
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;
103         root->node = NULL;
104         root->node = read_tree_block(root, info->tree_root);
105         root->extent_root = extent_root;
106         return 0;
107 }
108
109 struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
110 {
111         struct ctree_root *root = malloc(sizeof(struct ctree_root));
112         struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
113         int fp;
114         int ret;
115
116         fp = open(filename, O_CREAT | O_RDWR);
117         if (fp < 0) {
118                 free(root);
119                 return NULL;
120         }
121         INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
122         INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
123         ret = pread(fp, super, sizeof(struct ctree_super_block),
124                      CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
125         if (ret == 0 || super->root_info.tree_root == 0) {
126                 printf("making new FS!\n");
127                 ret = mkfs(fp);
128                 if (ret)
129                         return NULL;
130                 ret = pread(fp, super, sizeof(struct ctree_super_block),
131                              CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
132                 if (ret != sizeof(struct ctree_super_block))
133                         return NULL;
134         }
135         BUG_ON(ret < 0);
136         __setup_root(root, extent_root, &super->root_info, fp);
137         __setup_root(extent_root, extent_root, &super->extent_info, fp);
138         return root;
139 }
140
141 static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
142 {
143         info->tree_root = root->node->blocknr;
144         return 0;
145 }
146
147 int 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
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);
169         return 0;
170 }
171
172 void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
173 {
174         buf->count--;
175         if (buf->count < 0)
176                 BUG();
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