aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/disk-io.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r--fs/btrfs/disk-io.c176
1 files changed, 96 insertions, 80 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 25ce07908ee3..1849a99690c8 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -28,15 +28,15 @@ static int free_some_buffers(struct btrfs_root *root)
28{ 28{
29 struct list_head *node, *next; 29 struct list_head *node, *next;
30 struct btrfs_buffer *b; 30 struct btrfs_buffer *b;
31 if (root->cache_size < cache_max) 31 if (root->fs_info->cache_size < cache_max)
32 return 0; 32 return 0;
33 list_for_each_safe(node, next, &root->cache) { 33 list_for_each_safe(node, next, &root->fs_info->cache) {
34 b = list_entry(node, struct btrfs_buffer, cache); 34 b = list_entry(node, struct btrfs_buffer, cache);
35 if (b->count == 1) { 35 if (b->count == 1) {
36 BUG_ON(!list_empty(&b->dirty)); 36 BUG_ON(!list_empty(&b->dirty));
37 list_del_init(&b->cache); 37 list_del_init(&b->cache);
38 btrfs_block_release(root, b); 38 btrfs_block_release(root, b);
39 if (root->cache_size < cache_max) 39 if (root->fs_info->cache_size < cache_max)
40 break; 40 break;
41 } 41 }
42 } 42 }
@@ -57,10 +57,10 @@ struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
57 INIT_LIST_HEAD(&buf->dirty); 57 INIT_LIST_HEAD(&buf->dirty);
58 free_some_buffers(root); 58 free_some_buffers(root);
59 radix_tree_preload(GFP_KERNEL); 59 radix_tree_preload(GFP_KERNEL);
60 ret = radix_tree_insert(&root->cache_radix, blocknr, buf); 60 ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
61 radix_tree_preload_end(); 61 radix_tree_preload_end();
62 list_add_tail(&buf->cache, &root->cache); 62 list_add_tail(&buf->cache, &root->fs_info->cache);
63 root->cache_size++; 63 root->fs_info->cache_size++;
64 if (ret) { 64 if (ret) {
65 free(buf); 65 free(buf);
66 return NULL; 66 return NULL;
@@ -71,7 +71,7 @@ struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
71struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr) 71struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
72{ 72{
73 struct btrfs_buffer *buf; 73 struct btrfs_buffer *buf;
74 buf = radix_tree_lookup(&root->cache_radix, blocknr); 74 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
75 if (buf) { 75 if (buf) {
76 buf->count++; 76 buf->count++;
77 } else { 77 } else {
@@ -90,14 +90,15 @@ struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
90 struct btrfs_buffer *buf; 90 struct btrfs_buffer *buf;
91 int ret; 91 int ret;
92 92
93 buf = radix_tree_lookup(&root->cache_radix, blocknr); 93 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
94 if (buf) { 94 if (buf) {
95 buf->count++; 95 buf->count++;
96 } else { 96 } else {
97 buf = alloc_tree_block(root, blocknr); 97 buf = alloc_tree_block(root, blocknr);
98 if (!buf) 98 if (!buf)
99 return NULL; 99 return NULL;
100 ret = pread(root->fp, &buf->node, root->blocksize, offset); 100 ret = pread(root->fs_info->fp, &buf->node, root->blocksize,
101 offset);
101 if (ret != root->blocksize) { 102 if (ret != root->blocksize) {
102 free(buf); 103 free(buf);
103 return NULL; 104 return NULL;
@@ -113,7 +114,7 @@ int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
113{ 114{
114 if (!list_empty(&buf->dirty)) 115 if (!list_empty(&buf->dirty))
115 return 0; 116 return 0;
116 list_add_tail(&buf->dirty, &root->trans); 117 list_add_tail(&buf->dirty, &root->fs_info->trans);
117 buf->count++; 118 buf->count++;
118 return 0; 119 return 0;
119} 120}
@@ -137,7 +138,7 @@ int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
137 138
138 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header)) 139 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
139 BUG(); 140 BUG();
140 ret = pwrite(root->fp, &buf->node, root->blocksize, offset); 141 ret = pwrite(root->fs_info->fp, &buf->node, root->blocksize, offset);
141 if (ret != root->blocksize) 142 if (ret != root->blocksize)
142 return ret; 143 return ret;
143 return 0; 144 return 0;
@@ -149,8 +150,9 @@ static int __commit_transaction(struct btrfs_trans_handle *trans, struct
149 struct btrfs_buffer *b; 150 struct btrfs_buffer *b;
150 int ret = 0; 151 int ret = 0;
151 int wret; 152 int wret;
152 while(!list_empty(&root->trans)) { 153 while(!list_empty(&root->fs_info->trans)) {
153 b = list_entry(root->trans.next, struct btrfs_buffer, dirty); 154 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
155 dirty);
154 list_del_init(&b->dirty); 156 list_del_init(&b->dirty);
155 wret = write_tree_block(trans, root, b); 157 wret = write_tree_block(trans, root, b);
156 if (wret) 158 if (wret)
@@ -160,13 +162,21 @@ static int __commit_transaction(struct btrfs_trans_handle *trans, struct
160 return ret; 162 return ret;
161} 163}
162 164
163static int commit_extent_and_tree_roots(struct btrfs_trans_handle *trans, 165static int commit_tree_roots(struct btrfs_trans_handle *trans,
164 struct btrfs_root *tree_root, struct 166 struct btrfs_fs_info *fs_info)
165 btrfs_root *extent_root)
166{ 167{
167 int ret; 168 int ret;
168 u64 old_extent_block; 169 u64 old_extent_block;
169 170 struct btrfs_root *tree_root = fs_info->tree_root;
171 struct btrfs_root *extent_root = fs_info->extent_root;
172 struct btrfs_root *inode_root = fs_info->inode_root;
173
174 btrfs_set_root_blocknr(&inode_root->root_item,
175 inode_root->node->blocknr);
176 ret = btrfs_update_root(trans, tree_root,
177 &inode_root->root_key,
178 &inode_root->root_item);
179 BUG_ON(ret);
170 while(1) { 180 while(1) {
171 old_extent_block = btrfs_root_blocknr(&extent_root->root_item); 181 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
172 if (old_extent_block == extent_root->node->blocknr) 182 if (old_extent_block == extent_root->node->blocknr)
@@ -178,8 +188,6 @@ static int commit_extent_and_tree_roots(struct btrfs_trans_handle *trans,
178 &extent_root->root_item); 188 &extent_root->root_item);
179 BUG_ON(ret); 189 BUG_ON(ret);
180 } 190 }
181 __commit_transaction(trans, extent_root);
182 __commit_transaction(trans, tree_root);
183 return 0; 191 return 0;
184} 192}
185 193
@@ -190,9 +198,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
190 struct btrfs_buffer *snap = root->commit_root; 198 struct btrfs_buffer *snap = root->commit_root;
191 struct btrfs_key snap_key; 199 struct btrfs_key snap_key;
192 200
193 ret = __commit_transaction(trans, root);
194 BUG_ON(ret);
195
196 if (root->commit_root == root->node) 201 if (root->commit_root == root->node)
197 return 0; 202 return 0;
198 203
@@ -200,54 +205,55 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
200 root->root_key.offset++; 205 root->root_key.offset++;
201 206
202 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr); 207 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
203 ret = btrfs_insert_root(trans, root->tree_root, &root->root_key, 208 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
204 &root->root_item); 209 &root->root_key, &root->root_item);
210 BUG_ON(ret);
211
212 ret = commit_tree_roots(trans, root->fs_info);
205 BUG_ON(ret); 213 BUG_ON(ret);
206 214
207 ret = commit_extent_and_tree_roots(trans, root->tree_root, 215 ret = __commit_transaction(trans, root);
208 root->extent_root);
209 BUG_ON(ret); 216 BUG_ON(ret);
210 217
211 write_ctree_super(trans, root, s); 218 write_ctree_super(trans, root, s);
212 btrfs_finish_extent_commit(trans, root->extent_root); 219 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
213 btrfs_finish_extent_commit(trans, root->tree_root); 220 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
214 221
215 root->commit_root = root->node; 222 root->commit_root = root->node;
216 root->node->count++; 223 root->node->count++;
217 ret = btrfs_drop_snapshot(trans, root, snap); 224 ret = btrfs_drop_snapshot(trans, root, snap);
218 BUG_ON(ret); 225 BUG_ON(ret);
219 226
220 ret = btrfs_del_root(trans, root->tree_root, &snap_key); 227 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
221 BUG_ON(ret); 228 BUG_ON(ret);
222 229
223 return ret; 230 return ret;
224} 231}
225 232
226static int __setup_root(struct btrfs_super_block *super, 233static int __setup_root(struct btrfs_super_block *super,
227 struct btrfs_root *root, u64 objectid, int fp) 234 struct btrfs_root *root,
235 struct btrfs_fs_info *fs_info,
236 u64 objectid, int fp)
228{ 237{
229 INIT_LIST_HEAD(&root->trans);
230 INIT_LIST_HEAD(&root->cache);
231 root->cache_size = 0;
232 root->fp = fp;
233 root->node = NULL; 238 root->node = NULL;
234 root->commit_root = NULL; 239 root->commit_root = NULL;
235 root->blocksize = btrfs_super_blocksize(super); 240 root->blocksize = btrfs_super_blocksize(super);
236 root->ref_cows = 0; 241 root->ref_cows = 0;
237 memset(&root->current_insert, 0, sizeof(root->current_insert)); 242 root->fs_info = fs_info;
238 memset(&root->last_insert, 0, sizeof(root->last_insert));
239 memset(&root->root_key, 0, sizeof(root->root_key)); 243 memset(&root->root_key, 0, sizeof(root->root_key));
240 memset(&root->root_item, 0, sizeof(root->root_item)); 244 memset(&root->root_item, 0, sizeof(root->root_item));
241 return 0; 245 return 0;
242} 246}
243 247
244static int find_and_setup_root(struct btrfs_super_block *super, 248static int find_and_setup_root(struct btrfs_super_block *super,
245 struct btrfs_root *tree_root, u64 objectid, 249 struct btrfs_root *tree_root,
250 struct btrfs_fs_info *fs_info,
251 u64 objectid,
246 struct btrfs_root *root, int fp) 252 struct btrfs_root *root, int fp)
247{ 253{
248 int ret; 254 int ret;
249 255
250 __setup_root(super, root, objectid, fp); 256 __setup_root(super, root, fs_info, objectid, fp);
251 ret = btrfs_find_last_root(tree_root, objectid, 257 ret = btrfs_find_last_root(tree_root, objectid,
252 &root->root_item, &root->root_key); 258 &root->root_item, &root->root_key);
253 BUG_ON(ret); 259 BUG_ON(ret);
@@ -263,29 +269,31 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
263 struct btrfs_root *root = malloc(sizeof(struct btrfs_root)); 269 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
264 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root)); 270 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
265 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root)); 271 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
272 struct btrfs_root *inode_root = malloc(sizeof(struct btrfs_root));
273 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
266 int fp; 274 int fp;
267 int ret; 275 int ret;
268 276
269 root->extent_root = extent_root;
270 root->tree_root = tree_root;
271
272 extent_root->extent_root = extent_root;
273 extent_root->tree_root = tree_root;
274
275 tree_root->extent_root = extent_root;
276 tree_root->tree_root = tree_root;
277
278 fp = open(filename, O_CREAT | O_RDWR, 0600); 277 fp = open(filename, O_CREAT | O_RDWR, 0600);
279 if (fp < 0) { 278 if (fp < 0) {
280 free(root); 279 free(root);
281 return NULL; 280 return NULL;
282 } 281 }
283 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); 282 INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
284 INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL); 283 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
285 INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL); 284 INIT_LIST_HEAD(&fs_info->trans);
286 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); 285 INIT_LIST_HEAD(&fs_info->cache);
287 INIT_RADIX_TREE(&tree_root->pinned_radix, GFP_KERNEL); 286 fs_info->cache_size = 0;
288 INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL); 287 fs_info->fp = fp;
288 fs_info->running_transaction = NULL;
289 fs_info->fs_root = root;
290 fs_info->tree_root = tree_root;
291 fs_info->extent_root = extent_root;
292 fs_info->inode_root = inode_root;
293 fs_info->last_inode_alloc = 0;
294 fs_info->last_inode_alloc_dirid = 0;
295 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
296 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
289 297
290 ret = pread(fp, super, sizeof(struct btrfs_super_block), 298 ret = pread(fp, super, sizeof(struct btrfs_super_block),
291 BTRFS_SUPER_INFO_OFFSET); 299 BTRFS_SUPER_INFO_OFFSET);
@@ -301,16 +309,20 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
301 } 309 }
302 BUG_ON(ret < 0); 310 BUG_ON(ret < 0);
303 311
304 __setup_root(super, tree_root, BTRFS_ROOT_TREE_OBJECTID, fp); 312 __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
305 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super)); 313 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
306 BUG_ON(!tree_root->node); 314 BUG_ON(!tree_root->node);
307 315
308 ret = find_and_setup_root(super, tree_root, BTRFS_EXTENT_TREE_OBJECTID, 316 ret = find_and_setup_root(super, tree_root, fs_info,
309 extent_root, fp); 317 BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
310 BUG_ON(ret); 318 BUG_ON(ret);
311 319
312 ret = find_and_setup_root(super, tree_root, BTRFS_FS_TREE_OBJECTID, 320 ret = find_and_setup_root(super, tree_root, fs_info,
313 root, fp); 321 BTRFS_INODE_MAP_OBJECTID, inode_root, fp);
322 BUG_ON(ret);
323
324 ret = find_and_setup_root(super, tree_root, fs_info,
325 BTRFS_FS_TREE_OBJECTID, root, fp);
314 BUG_ON(ret); 326 BUG_ON(ret);
315 327
316 root->commit_root = root->node; 328 root->commit_root = root->node;
@@ -323,8 +335,8 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
323 *root, struct btrfs_super_block *s) 335 *root, struct btrfs_super_block *s)
324{ 336{
325 int ret; 337 int ret;
326 btrfs_set_super_root(s, root->tree_root->node->blocknr); 338 btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
327 ret = pwrite(root->fp, s, sizeof(*s), 339 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
328 BTRFS_SUPER_INFO_OFFSET); 340 BTRFS_SUPER_INFO_OFFSET);
329 if (ret != sizeof(*s)) { 341 if (ret != sizeof(*s)) {
330 fprintf(stderr, "failed to write new super block err %d\n", ret); 342 fprintf(stderr, "failed to write new super block err %d\n", ret);
@@ -335,9 +347,10 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
335 347
336static int drop_cache(struct btrfs_root *root) 348static int drop_cache(struct btrfs_root *root)
337{ 349{
338 while(!list_empty(&root->cache)) { 350 while(!list_empty(&root->fs_info->cache)) {
339 struct btrfs_buffer *b = list_entry(root->cache.next, 351 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
340 struct btrfs_buffer, cache); 352 struct btrfs_buffer,
353 cache);
341 list_del_init(&b->cache); 354 list_del_init(&b->cache);
342 btrfs_block_release(root, b); 355 btrfs_block_release(root, b);
343 } 356 }
@@ -348,26 +361,28 @@ int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
348 int ret; 361 int ret;
349 struct btrfs_trans_handle *trans; 362 struct btrfs_trans_handle *trans;
350 363
351 trans = root->running_transaction; 364 trans = root->fs_info->running_transaction;
352 btrfs_commit_transaction(trans, root, s); 365 btrfs_commit_transaction(trans, root, s);
353 ret = commit_extent_and_tree_roots(trans, root->tree_root, 366 ret = commit_tree_roots(trans, root->fs_info);
354 root->extent_root); 367 BUG_ON(ret);
368 ret = __commit_transaction(trans, root);
355 BUG_ON(ret); 369 BUG_ON(ret);
356 write_ctree_super(trans, root, s); 370 write_ctree_super(trans, root, s);
357 drop_cache(root->extent_root);
358 drop_cache(root->tree_root);
359 drop_cache(root); 371 drop_cache(root);
360 BUG_ON(!list_empty(&root->trans)); 372 BUG_ON(!list_empty(&root->fs_info->trans));
361 BUG_ON(!list_empty(&root->extent_root->trans));
362 BUG_ON(!list_empty(&root->tree_root->trans));
363 373
364 close(root->fp); 374 close(root->fs_info->fp);
365 if (root->node) 375 if (root->node)
366 btrfs_block_release(root, root->node); 376 btrfs_block_release(root, root->node);
367 if (root->extent_root->node) 377 if (root->fs_info->extent_root->node)
368 btrfs_block_release(root->extent_root, root->extent_root->node); 378 btrfs_block_release(root->fs_info->extent_root,
369 if (root->tree_root->node) 379 root->fs_info->extent_root->node);
370 btrfs_block_release(root->tree_root, root->tree_root->node); 380 if (root->fs_info->inode_root->node)
381 btrfs_block_release(root->fs_info->inode_root,
382 root->fs_info->inode_root->node);
383 if (root->fs_info->tree_root->node)
384 btrfs_block_release(root->fs_info->tree_root,
385 root->fs_info->tree_root->node);
371 btrfs_block_release(root, root->commit_root); 386 btrfs_block_release(root, root->commit_root);
372 free(root); 387 free(root);
373 printf("on close %d blocks are allocated\n", allocated_blocks); 388 printf("on close %d blocks are allocated\n", allocated_blocks);
@@ -382,15 +397,16 @@ void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
382 if (buf->count == 0) { 397 if (buf->count == 0) {
383 BUG_ON(!list_empty(&buf->cache)); 398 BUG_ON(!list_empty(&buf->cache));
384 BUG_ON(!list_empty(&buf->dirty)); 399 BUG_ON(!list_empty(&buf->dirty));
385 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) 400 if (!radix_tree_lookup(&root->fs_info->cache_radix,
401 buf->blocknr))
386 BUG(); 402 BUG();
387 radix_tree_delete(&root->cache_radix, buf->blocknr); 403 radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
388 memset(buf, 0, sizeof(*buf)); 404 memset(buf, 0, sizeof(*buf));
389 free(buf); 405 free(buf);
390 BUG_ON(allocated_blocks == 0); 406 BUG_ON(allocated_blocks == 0);
391 allocated_blocks--; 407 allocated_blocks--;
392 BUG_ON(root->cache_size == 0); 408 BUG_ON(root->fs_info->cache_size == 0);
393 root->cache_size--; 409 root->fs_info->cache_size--;
394 } 410 }
395} 411}
396 412