Add backing store, memory management
[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
13 static int allocated_blocks = 0;
14
15 struct ctree_header {
16         u64 root_block;
17 } __attribute__ ((__packed__));
18
19 static int get_free_block(struct ctree_root *root, u64 *block)
20 {
21         struct stat st;
22         int ret;
23
24         st.st_size = 0;
25         ret = fstat(root->fp, &st);
26         if (st.st_size > sizeof(struct ctree_header)) {
27                 *block = (st.st_size -
28                         sizeof(struct ctree_header)) / CTREE_BLOCKSIZE;
29         } else {
30                 *block = 0;
31         }
32         ret = ftruncate(root->fp, sizeof(struct ctree_header) + (*block + 1) *
33                         CTREE_BLOCKSIZE);
34         return ret;
35 }
36
37 struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
38 {
39         struct tree_buffer *buf;
40         int ret;
41         buf = malloc(sizeof(struct tree_buffer));
42         if (!buf)
43                 return buf;
44         allocated_blocks++;
45         buf->blocknr = blocknr;
46         buf->count = 1;
47         radix_tree_preload(GFP_KERNEL);
48         ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
49         radix_tree_preload_end();
50         if (ret) {
51                 free(buf);
52                 return NULL;
53         }
54         return buf;
55 }
56
57 struct tree_buffer *alloc_free_block(struct ctree_root *root)
58 {
59         u64 free_block;
60         int ret;
61         struct tree_buffer * buf;
62         ret = get_free_block(root, &free_block);
63         if (ret) {
64                 BUG();
65                 return NULL;
66         }
67         buf = alloc_tree_block(root, free_block);
68         if (!buf)
69                 BUG();
70         return buf;
71 }
72
73 struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
74 {
75         loff_t offset = blocknr * CTREE_BLOCKSIZE + sizeof(struct ctree_header);
76         struct tree_buffer *buf;
77         int ret;
78
79         buf = radix_tree_lookup(&root->cache_radix, blocknr);
80         if (buf) {
81                 buf->count++;
82                 if (buf->blocknr != blocknr)
83                         BUG();
84                 if (buf->blocknr != buf->node.header.blocknr)
85                         BUG();
86                 return buf;
87         }
88         buf = alloc_tree_block(root, blocknr);
89         if (!buf)
90                 return NULL;
91         ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
92         if (ret != CTREE_BLOCKSIZE) {
93                 free(buf);
94                 return NULL;
95         }
96         if (buf->blocknr != buf->node.header.blocknr)
97                 BUG();
98         return buf;
99 }
100
101 int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
102 {
103         u64 blocknr = buf->blocknr;
104         loff_t offset = blocknr * CTREE_BLOCKSIZE + sizeof(struct ctree_header);
105         int ret;
106
107         if (buf->blocknr != buf->node.header.blocknr)
108                 BUG();
109         ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
110         if (ret != CTREE_BLOCKSIZE)
111                 return ret;
112         if (buf == root->node)
113                 return update_root_block(root);
114         return 0;
115 }
116
117 struct ctree_root *open_ctree(char *filename)
118 {
119         struct ctree_root *root = malloc(sizeof(struct ctree_root));
120         int fp;
121         u64 root_block;
122         int ret;
123
124         fp = open(filename, O_CREAT | O_RDWR);
125         if (fp < 0) {
126                 free(root);
127                 return NULL;
128         }
129         root->fp = fp;
130         INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
131         ret = pread(fp, &root_block, sizeof(u64), 0);
132         if (ret == sizeof(u64)) {
133                 printf("reading root node at block %lu\n", root_block);
134                 root->node = read_tree_block(root, root_block);
135         } else
136                 root->node = NULL;
137         return root;
138 }
139
140 int close_ctree(struct ctree_root *root)
141 {
142         close(root->fp);
143         if (root->node)
144                 tree_block_release(root, root->node);
145         free(root);
146         printf("on close %d blocks are allocated\n", allocated_blocks);
147         return 0;
148 }
149
150 int update_root_block(struct ctree_root *root)
151 {
152         int ret;
153         u64 root_block = root->node->blocknr;
154
155         ret = pwrite(root->fp, &root_block, sizeof(u64), 0);
156         if (ret != sizeof(u64))
157                 return ret;
158         return 0;
159 }
160
161 void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
162 {
163         buf->count--;
164         if (buf->count == 0) {
165                 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
166                         BUG();
167                 radix_tree_delete(&root->cache_radix, buf->blocknr);
168                 memset(buf, 0, sizeof(*buf));
169                 free(buf);
170                 BUG_ON(allocated_blocks == 0);
171                 allocated_blocks--;
172         }
173 }
174