aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/disk-io.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-02-02 09:18:22 -0500
committerDavid Woodhouse <dwmw2@hera.kernel.org>2007-02-02 09:18:22 -0500
commiteb60ceac0ebd11e9896ed1ca84fcad71a4b23601 (patch)
tree70d426737630b713bc18dc5ae8c342651f0f1f44 /fs/btrfs/disk-io.c
parent4920c9ac9a4bbc6bf9acd8c614987ee6b378e78f (diff)
Btrfs: Add backing store, memory management
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r--fs/btrfs/disk-io.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
new file mode 100644
index 000000000000..8d51a07051d9
--- /dev/null
+++ b/fs/btrfs/disk-io.c
@@ -0,0 +1,174 @@
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
13static int allocated_blocks = 0;
14
15struct ctree_header {
16 u64 root_block;
17} __attribute__ ((__packed__));
18
19static 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
37struct 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
57struct 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
73struct 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
101int 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
117struct 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
140int 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
150int 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
161void 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