early support for multiple devices
[platform/upstream/btrfs-progs.git] / 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 #include "transaction.h"
13
14 static int allocated_blocks = 0;
15 int cache_max = 10000;
16
17 static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
18 {
19         if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
20                 BUG();
21         if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid,
22                    sizeof(buf->node.header.fsid)))
23                 BUG();
24         return 0;
25 }
26
27 static int free_some_buffers(struct btrfs_root *root)
28 {
29         struct list_head *node, *next;
30         struct btrfs_buffer *b;
31         if (root->fs_info->cache_size < cache_max)
32                 return 0;
33         list_for_each_safe(node, next, &root->fs_info->cache) {
34                 b = list_entry(node, struct btrfs_buffer, cache);
35                 if (b->count == 1) {
36                         BUG_ON(!list_empty(&b->dirty));
37                         list_del_init(&b->cache);
38                         btrfs_block_release(root, b);
39                         if (root->fs_info->cache_size < cache_max)
40                                 break;
41                 }
42         }
43         return 0;
44 }
45
46 struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
47 {
48         struct btrfs_buffer *buf;
49         int ret;
50
51         buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
52         if (!buf)
53                 return buf;
54         allocated_blocks++;
55         buf->blocknr = blocknr;
56         buf->count = 2;
57         INIT_LIST_HEAD(&buf->dirty);
58         free_some_buffers(root);
59         radix_tree_preload(GFP_KERNEL);
60         ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
61         radix_tree_preload_end();
62         list_add_tail(&buf->cache, &root->fs_info->cache);
63         root->fs_info->cache_size++;
64         if (ret) {
65                 free(buf);
66                 return NULL;
67         }
68         return buf;
69 }
70
71 struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
72 {
73         struct btrfs_buffer *buf;
74         buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
75         if (buf) {
76                 buf->count++;
77         } else {
78                 buf = alloc_tree_block(root, blocknr);
79                 if (!buf) {
80                         BUG();
81                         return NULL;
82                 }
83         }
84         return buf;
85 }
86
87 struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
88 {
89         loff_t offset = blocknr * root->blocksize;
90         struct btrfs_buffer *buf;
91         int ret;
92         buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
93         if (buf) {
94                 buf->count++;
95         } else {
96                 buf = alloc_tree_block(root, blocknr);
97                 if (!buf)
98                         return NULL;
99                 ret = pread(root->fs_info->fp, &buf->node, root->blocksize,
100                             offset);
101                 if (ret != root->blocksize) {
102                         free(buf);
103                         return NULL;
104                 }
105         }
106         if (check_tree_block(root, buf))
107                 BUG();
108         return buf;
109 }
110
111 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
112                      struct btrfs_buffer *buf)
113 {
114         if (!list_empty(&buf->dirty))
115                 return 0;
116         list_add_tail(&buf->dirty, &root->fs_info->trans);
117         buf->count++;
118         return 0;
119 }
120
121 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
122                      struct btrfs_buffer *buf)
123 {
124         if (!list_empty(&buf->dirty)) {
125                 list_del_init(&buf->dirty);
126                 btrfs_block_release(root, buf);
127         }
128         return 0;
129 }
130
131 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
132                      struct btrfs_buffer *buf)
133 {
134         u64 blocknr = buf->blocknr;
135         loff_t offset = blocknr * root->blocksize;
136         int ret;
137
138         if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
139                 BUG();
140         ret = pwrite(root->fs_info->fp, &buf->node, root->blocksize, offset);
141         if (ret != root->blocksize)
142                 return ret;
143         return 0;
144 }
145
146 static int __commit_transaction(struct btrfs_trans_handle *trans, struct
147                                 btrfs_root *root)
148 {
149         struct btrfs_buffer *b;
150         int ret = 0;
151         int wret;
152         while(!list_empty(&root->fs_info->trans)) {
153                 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
154                                dirty);
155                 list_del_init(&b->dirty);
156                 wret = write_tree_block(trans, root, b);
157                 if (wret)
158                         ret = wret;
159                 btrfs_block_release(root, b);
160         }
161         return ret;
162 }
163
164 static int commit_tree_roots(struct btrfs_trans_handle *trans,
165                              struct btrfs_fs_info *fs_info)
166 {
167         int ret;
168         u64 old_extent_block;
169         struct btrfs_root *tree_root = fs_info->tree_root;
170         struct btrfs_root *extent_root = fs_info->extent_root;
171
172         while(1) {
173                 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
174                 if (old_extent_block == extent_root->node->blocknr)
175                         break;
176                 btrfs_set_root_blocknr(&extent_root->root_item,
177                                        extent_root->node->blocknr);
178                 ret = btrfs_update_root(trans, tree_root,
179                                         &extent_root->root_key,
180                                         &extent_root->root_item);
181                 BUG_ON(ret);
182         }
183         return 0;
184 }
185
186 int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
187                              btrfs_root *root, struct btrfs_super_block *s)
188 {
189         int ret = 0;
190         struct btrfs_buffer *snap = root->commit_root;
191         struct btrfs_key snap_key;
192
193         if (root->commit_root == root->node)
194                 return 0;
195
196         memcpy(&snap_key, &root->root_key, sizeof(snap_key));
197         root->root_key.offset++;
198
199         btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
200         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
201                                 &root->root_key, &root->root_item);
202         BUG_ON(ret);
203
204         ret = commit_tree_roots(trans, root->fs_info);
205         BUG_ON(ret);
206
207         ret = __commit_transaction(trans, root);
208         BUG_ON(ret);
209
210         write_ctree_super(trans, root, s);
211         btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
212         btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
213
214         root->commit_root = root->node;
215         root->node->count++;
216         ret = btrfs_drop_snapshot(trans, root, snap);
217         BUG_ON(ret);
218
219         ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
220         BUG_ON(ret);
221         root->fs_info->generation = root->root_key.offset + 1;
222
223         return ret;
224 }
225
226 static int __setup_root(struct btrfs_super_block *super,
227                         struct btrfs_root *root,
228                         struct btrfs_fs_info *fs_info,
229                         u64 objectid, int fp)
230 {
231         root->node = NULL;
232         root->commit_root = NULL;
233         root->blocksize = btrfs_super_blocksize(super);
234         root->ref_cows = 0;
235         root->fs_info = fs_info;
236         memset(&root->root_key, 0, sizeof(root->root_key));
237         memset(&root->root_item, 0, sizeof(root->root_item));
238         return 0;
239 }
240
241 static int find_and_setup_root(struct btrfs_super_block *super,
242                                struct btrfs_root *tree_root,
243                                struct btrfs_fs_info *fs_info,
244                                u64 objectid,
245                                struct btrfs_root *root, int fp)
246 {
247         int ret;
248
249         __setup_root(super, root, fs_info, objectid, fp);
250         ret = btrfs_find_last_root(tree_root, objectid,
251                                    &root->root_item, &root->root_key);
252         BUG_ON(ret);
253
254         root->node = read_tree_block(root,
255                                      btrfs_root_blocknr(&root->root_item));
256         BUG_ON(!root->node);
257         return 0;
258 }
259
260 struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
261 {
262         int fp;
263
264         fp = open(filename, O_CREAT | O_RDWR, 0600);
265         if (fp < 0) {
266                 return NULL;
267         }
268         return open_ctree_fd(fp, super);
269 }
270
271 struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
272 {
273         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
274         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
275         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
276         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
277         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
278         int ret;
279
280         INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
281         INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
282         INIT_LIST_HEAD(&fs_info->trans);
283         INIT_LIST_HEAD(&fs_info->cache);
284         fs_info->cache_size = 0;
285         fs_info->fp = fp;
286         fs_info->running_transaction = NULL;
287         fs_info->fs_root = root;
288         fs_info->tree_root = tree_root;
289         fs_info->extent_root = extent_root;
290         fs_info->dev_root = dev_root;
291         fs_info->last_inode_alloc = 0;
292         fs_info->last_inode_alloc_dirid = 0;
293         fs_info->disk_super = super;
294         memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
295         memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
296
297         ret = pread(fp, super, sizeof(struct btrfs_super_block),
298                      BTRFS_SUPER_INFO_OFFSET);
299         if (ret == 0 || btrfs_super_root(super) == 0) {
300                 BUG();
301                 return NULL;
302         }
303         BUG_ON(ret < 0);
304         __setup_root(super, dev_root, fs_info, BTRFS_DEV_TREE_OBJECTID, fp);
305         dev_root->node = read_tree_block(dev_root,
306                                          btrfs_super_device_root(super));
307
308         __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
309         tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
310         BUG_ON(!tree_root->node);
311
312         ret = find_and_setup_root(super, tree_root, fs_info,
313                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
314         BUG_ON(ret);
315
316         ret = find_and_setup_root(super, tree_root, fs_info,
317                                   BTRFS_FS_TREE_OBJECTID, root, fp);
318         BUG_ON(ret);
319
320         root->commit_root = root->node;
321         root->node->count++;
322         root->ref_cows = 1;
323         root->fs_info->generation = root->root_key.offset + 1;
324         return root;
325 }
326
327 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
328                       *root, struct btrfs_super_block *s)
329 {
330         int ret;
331         btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
332         ret = pwrite(root->fs_info->fp, s, sizeof(*s),
333                      BTRFS_SUPER_INFO_OFFSET);
334         if (ret != sizeof(*s)) {
335                 fprintf(stderr, "failed to write new super block err %d\n", ret);
336                 return ret;
337         }
338         return 0;
339 }
340
341 static int drop_cache(struct btrfs_root *root)
342 {
343         while(!list_empty(&root->fs_info->cache)) {
344                 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
345                                                     struct btrfs_buffer,
346                                                     cache);
347                 list_del_init(&b->cache);
348                 btrfs_block_release(root, b);
349         }
350         return 0;
351 }
352 int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
353 {
354         int ret;
355         struct btrfs_trans_handle *trans;
356
357         trans = root->fs_info->running_transaction;
358         btrfs_commit_transaction(trans, root, s);
359         ret = commit_tree_roots(trans, root->fs_info);
360         BUG_ON(ret);
361         ret = __commit_transaction(trans, root);
362         BUG_ON(ret);
363         write_ctree_super(trans, root, s);
364         drop_cache(root);
365         BUG_ON(!list_empty(&root->fs_info->trans));
366
367         close(root->fs_info->fp);
368         if (root->node)
369                 btrfs_block_release(root, root->node);
370         if (root->fs_info->extent_root->node)
371                 btrfs_block_release(root->fs_info->extent_root,
372                                     root->fs_info->extent_root->node);
373         if (root->fs_info->tree_root->node)
374                 btrfs_block_release(root->fs_info->tree_root,
375                                     root->fs_info->tree_root->node);
376         btrfs_block_release(root, root->commit_root);
377         free(root);
378         printf("on close %d blocks are allocated\n", allocated_blocks);
379         return 0;
380 }
381
382 void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
383 {
384         buf->count--;
385         if (buf->count < 0)
386                 BUG();
387         if (buf->count == 0) {
388                 BUG_ON(!list_empty(&buf->cache));
389                 BUG_ON(!list_empty(&buf->dirty));
390                 if (!radix_tree_lookup(&root->fs_info->cache_radix,
391                                        buf->blocknr))
392                         BUG();
393                 radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
394                 memset(buf, 0, sizeof(*buf));
395                 free(buf);
396                 BUG_ON(allocated_blocks == 0);
397                 allocated_blocks--;
398                 BUG_ON(root->fs_info->cache_size == 0);
399                 root->fs_info->cache_size--;
400         }
401 }
402