Dynamic chunk allocation
[platform/upstream/btrfs-progs.git] / disk-io.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 600
20 #define __USE_XOPEN2K
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include "kerncompat.h"
28 #include "radix-tree.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "volumes.h"
32 #include "transaction.h"
33 #include "crc32c.h"
34
35 int btrfs_open_device(struct btrfs_device *dev)
36 {
37         dev->fd = open(dev->name, O_RDWR, 0600);
38         BUG_ON(dev->fd < 0);
39         return 0;
40 }
41
42 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
43                             u64 logical)
44 {
45         u64 physical;
46         u64 length;
47         struct btrfs_device *device;
48         int ret;
49
50         ret = btrfs_map_block(&root->fs_info->mapping_tree, logical, &physical,
51                               &length, &device);
52         BUG_ON(ret);
53         buf->fd = device->fd;
54         buf->dev_bytenr = physical;
55         return 0;
56 }
57
58 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
59 {
60         if (buf->start != btrfs_header_bytenr(buf))
61                 BUG();
62
63         if (memcmp_extent_buffer(buf, root->fs_info->fsid,
64                                  (unsigned long)btrfs_header_fsid(buf),
65                                  BTRFS_FSID_SIZE))
66                 BUG();
67         return 0;
68 }
69
70 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
71 {
72         return crc32c(seed, data, len);
73 }
74
75 void btrfs_csum_final(u32 crc, char *result)
76 {
77         *(__le32 *)result = ~cpu_to_le32(crc);
78 }
79
80 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
81                            int verify)
82 {
83         char result[BTRFS_CRC32_SIZE];
84         u32 len;
85         u32 crc = ~(u32)0;
86
87         len = buf->len - BTRFS_CSUM_SIZE;
88         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
89         btrfs_csum_final(crc, result);
90
91         if (verify) {
92                 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
93                         printk("checksum verify failed on %llu\n", buf->start);
94                         return 1;
95                 }
96         } else {
97                 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
98         }
99         return 0;
100 }
101
102 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
103                                             u64 bytenr, u32 blocksize)
104 {
105         return find_extent_buffer(&root->fs_info->extent_cache,
106                                   bytenr, blocksize);
107 }
108
109 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
110                                                  u64 bytenr, u32 blocksize)
111 {
112         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
113                                    blocksize);
114 }
115
116 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
117 {
118         return 0;
119 }
120
121 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
122                                      u32 blocksize)
123 {
124         int ret;
125         struct extent_buffer *eb;
126
127         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
128         if (!eb)
129                 return NULL;
130         if (!btrfs_buffer_uptodate(eb)) {
131                 btrfs_map_bh_to_logical(root, eb, eb->start);
132                 ret = read_extent_from_disk(eb);
133                 if (ret) {
134                         free_extent_buffer(eb);
135                         return NULL;
136                 }
137                 btrfs_set_buffer_uptodate(eb);
138         }
139         return eb;
140 }
141
142 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
143                      struct extent_buffer *eb)
144 {
145         if (check_tree_block(root, eb))
146                 BUG();
147         if (!btrfs_buffer_uptodate(eb))
148                 BUG();
149         btrfs_map_bh_to_logical(root, eb, eb->start);
150         csum_tree_block(root, eb, 0);
151         return write_extent_to_disk(eb);
152 }
153
154 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
155                         u32 stripesize, struct btrfs_root *root,
156                         struct btrfs_fs_info *fs_info, u64 objectid)
157 {
158         root->node = NULL;
159         root->commit_root = NULL;
160         root->sectorsize = sectorsize;
161         root->nodesize = nodesize;
162         root->leafsize = leafsize;
163         root->stripesize = stripesize;
164         root->ref_cows = 0;
165         root->track_dirty = 0;
166
167         root->fs_info = fs_info;
168         root->objectid = objectid;
169         root->last_trans = 0;
170         root->highest_inode = 0;
171         root->last_inode_alloc = 0;
172
173         INIT_LIST_HEAD(&root->dirty_list);
174         memset(&root->root_key, 0, sizeof(root->root_key));
175         memset(&root->root_item, 0, sizeof(root->root_item));
176         root->root_key.objectid = objectid;
177         return 0;
178 }
179
180 static int update_cowonly_root(struct btrfs_trans_handle *trans,
181                                struct btrfs_root *root)
182 {
183         int ret;
184         u64 old_root_bytenr;
185         struct btrfs_root *tree_root = root->fs_info->tree_root;
186
187         btrfs_write_dirty_block_groups(trans, root);
188         while(1) {
189                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
190                 if (old_root_bytenr == root->node->start)
191                         break;
192                 btrfs_set_root_bytenr(&root->root_item,
193                                        root->node->start);
194                 root->root_item.level = btrfs_header_level(root->node);
195                 ret = btrfs_update_root(trans, tree_root,
196                                         &root->root_key,
197                                         &root->root_item);
198                 BUG_ON(ret);
199                 btrfs_write_dirty_block_groups(trans, root);
200         }
201         return 0;
202 }
203
204 static int commit_tree_roots(struct btrfs_trans_handle *trans,
205                              struct btrfs_fs_info *fs_info)
206 {
207         struct btrfs_root *root;
208         struct list_head *next;
209
210         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
211                 next = fs_info->dirty_cowonly_roots.next;
212                 list_del_init(next);
213                 root = list_entry(next, struct btrfs_root, dirty_list);
214                 update_cowonly_root(trans, root);
215         }
216         return 0;
217 }
218
219 static int __commit_transaction(struct btrfs_trans_handle *trans,
220                                 struct btrfs_root *root)
221 {
222         u64 start;
223         u64 end;
224         struct extent_buffer *eb;
225         struct extent_io_tree *tree = &root->fs_info->extent_cache;
226         int ret;
227
228         while(1) {
229                 ret = find_first_extent_bit(tree, 0, &start, &end,
230                                             EXTENT_DIRTY);
231                 if (ret)
232                         break;
233                 while(start <= end) {
234                         eb = find_first_extent_buffer(tree, start);
235                         BUG_ON(!eb || eb->start != start);
236                         ret = write_tree_block(trans, root, eb);
237                         BUG_ON(ret);
238                         start += eb->len;
239                         clear_extent_buffer_dirty(eb);
240                         free_extent_buffer(eb);
241                 }
242         }
243         return 0;
244 }
245
246 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
247                              struct btrfs_root *root)
248 {
249         int ret = 0;
250         struct btrfs_root *new_root = NULL;
251         struct btrfs_fs_info *fs_info = root->fs_info;
252
253         if (root->commit_root == root->node)
254                 goto commit_tree;
255
256         new_root = malloc(sizeof(*new_root));
257         if (!new_root)
258                 return -ENOMEM;
259         memcpy(new_root, root, sizeof(*new_root));
260         new_root->node = root->commit_root;
261         root->commit_root = NULL;
262
263         root->root_key.offset = trans->transid;
264         btrfs_set_root_bytenr(&root->root_item, root->node->start);
265         root->root_item.level = btrfs_header_level(root->node);
266         ret = btrfs_insert_root(trans, fs_info->tree_root,
267                                 &root->root_key, &root->root_item);
268         BUG_ON(ret);
269
270         btrfs_set_root_refs(&new_root->root_item, 0);
271         ret = btrfs_update_root(trans, root->fs_info->tree_root,
272                                 &new_root->root_key, &new_root->root_item);
273         BUG_ON(ret);
274
275         ret = commit_tree_roots(trans, fs_info);
276         BUG_ON(ret);
277         ret = __commit_transaction(trans, root);
278         BUG_ON(ret);
279         write_ctree_super(trans, root);
280         btrfs_finish_extent_commit(trans, fs_info->extent_root,
281                                    &fs_info->pinned_extents);
282         btrfs_free_transaction(root, trans);
283         fs_info->running_transaction = NULL;
284
285         trans = btrfs_start_transaction(root, 1);
286         ret = btrfs_drop_snapshot(trans, new_root);
287         BUG_ON(ret);
288         ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
289         BUG_ON(ret);
290 commit_tree:
291         ret = commit_tree_roots(trans, fs_info);
292         BUG_ON(ret);
293         ret = __commit_transaction(trans, root);
294         BUG_ON(ret);
295         write_ctree_super(trans, root);
296         btrfs_finish_extent_commit(trans, fs_info->extent_root,
297                                    &fs_info->pinned_extents);
298         btrfs_free_transaction(root, trans);
299         free_extent_buffer(root->commit_root);
300         root->commit_root = NULL;
301         fs_info->running_transaction = NULL;
302         if (new_root) {
303                 free_extent_buffer(new_root->node);
304                 free(new_root);
305         }
306         return 0;
307 }
308
309 static int find_and_setup_root(struct btrfs_root *tree_root,
310                                struct btrfs_fs_info *fs_info,
311                                u64 objectid, struct btrfs_root *root)
312 {
313         int ret;
314         u32 blocksize;
315
316         __setup_root(tree_root->nodesize, tree_root->leafsize,
317                      tree_root->sectorsize, tree_root->stripesize,
318                      root, fs_info, objectid);
319         ret = btrfs_find_last_root(tree_root, objectid,
320                                    &root->root_item, &root->root_key);
321         BUG_ON(ret);
322
323         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
324         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
325                                      blocksize);
326         BUG_ON(!root->node);
327         return 0;
328 }
329
330 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
331 {
332         if (root->node)
333                 free_extent_buffer(root->node);
334         if (root->commit_root)
335                 free_extent_buffer(root->commit_root);
336
337         free(root);
338         return 0;
339 }
340
341 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
342                                       struct btrfs_key *location)
343 {
344         struct btrfs_root *root;
345         struct btrfs_root *tree_root = fs_info->tree_root;
346         struct btrfs_path *path;
347         struct extent_buffer *l;
348         u32 blocksize;
349         int ret = 0;
350
351         root = malloc(sizeof(*root));
352         if (!root)
353                 return ERR_PTR(-ENOMEM);
354         memset(root, 0, sizeof(*root));
355         if (location->offset == (u64)-1) {
356                 ret = find_and_setup_root(tree_root, fs_info,
357                                           location->objectid, root);
358                 if (ret) {
359                         free(root);
360                         return ERR_PTR(ret);
361                 }
362                 goto insert;
363         }
364
365         __setup_root(tree_root->nodesize, tree_root->leafsize,
366                      tree_root->sectorsize, tree_root->stripesize,
367                      root, fs_info, location->objectid);
368
369         path = btrfs_alloc_path();
370         BUG_ON(!path);
371         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
372         if (ret != 0) {
373                 if (ret > 0)
374                         ret = -ENOENT;
375                 goto out;
376         }
377         l = path->nodes[0];
378         read_extent_buffer(l, &root->root_item,
379                btrfs_item_ptr_offset(l, path->slots[0]),
380                sizeof(root->root_item));
381         memcpy(&root->root_key, location, sizeof(*location));
382         ret = 0;
383 out:
384         btrfs_release_path(root, path);
385         btrfs_free_path(path);
386         if (ret) {
387                 free(root);
388                 return ERR_PTR(ret);
389         }
390         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
391         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
392                                      blocksize);
393         BUG_ON(!root->node);
394 insert:
395         root->ref_cows = 1;
396         return root;
397 }
398
399 struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr)
400 {
401         int fp;
402
403         fp = open(filename, O_CREAT | O_RDWR, 0600);
404         if (fp < 0) {
405                 return NULL;
406         }
407         return open_ctree_fd(fp, sb_bytenr);
408 }
409
410 struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr)
411 {
412         u32 sectorsize;
413         u32 nodesize;
414         u32 leafsize;
415         u32 blocksize;
416         u32 stripesize;
417         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
418         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
419         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
420         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
421         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
422         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
423         int ret;
424         struct btrfs_super_block *disk_super;
425
426         if (sb_bytenr == 0)
427                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
428
429         fs_info->fp = fp;
430         fs_info->running_transaction = NULL;
431         fs_info->fs_root = root;
432         fs_info->tree_root = tree_root;
433         fs_info->extent_root = extent_root;
434         fs_info->extent_ops = NULL;
435         fs_info->priv_data = NULL;
436         fs_info->chunk_root = chunk_root;
437         fs_info->dev_root = dev_root;
438         fs_info->force_system_allocs = 0;
439
440         extent_io_tree_init(&fs_info->extent_cache);
441         extent_io_tree_init(&fs_info->free_space_cache);
442         extent_io_tree_init(&fs_info->block_group_cache);
443         extent_io_tree_init(&fs_info->pinned_extents);
444         extent_io_tree_init(&fs_info->pending_del);
445         extent_io_tree_init(&fs_info->extent_ins);
446
447         cache_tree_init(&fs_info->mapping_tree.cache_tree);
448
449         mutex_init(&fs_info->fs_mutex);
450         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
451         INIT_LIST_HEAD(&fs_info->devices);
452         INIT_LIST_HEAD(&fs_info->space_info);
453
454         __setup_root(4096, 4096, 4096, 4096, tree_root,
455                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
456
457         fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
458                                                           4096);
459         BUG_ON(!fs_info->sb_buffer);
460         fs_info->sb_buffer->fd = fp;
461         fs_info->sb_buffer->dev_bytenr = sb_bytenr;
462         ret = read_extent_from_disk(fs_info->sb_buffer);
463         BUG_ON(ret);
464         btrfs_set_buffer_uptodate(fs_info->sb_buffer);
465
466         read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
467                            sizeof(fs_info->super_copy));
468         read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
469                            (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
470                            BTRFS_FSID_SIZE);
471
472         disk_super = &fs_info->super_copy;
473         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
474                     sizeof(disk_super->magic))) {
475                 printk("No valid btrfs found\n");
476                 BUG_ON(1);
477         }
478         nodesize = btrfs_super_nodesize(disk_super);
479         leafsize = btrfs_super_leafsize(disk_super);
480         sectorsize = btrfs_super_sectorsize(disk_super);
481         stripesize = btrfs_super_stripesize(disk_super);
482         tree_root->nodesize = nodesize;
483         tree_root->leafsize = leafsize;
484         tree_root->sectorsize = sectorsize;
485         tree_root->stripesize = stripesize;
486
487         ret = btrfs_read_sys_array(tree_root);
488         BUG_ON(ret);
489         blocksize = btrfs_level_size(tree_root,
490                                      btrfs_super_chunk_root_level(disk_super));
491
492         __setup_root(nodesize, leafsize, sectorsize, stripesize,
493                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
494         chunk_root->node = read_tree_block(chunk_root,
495                                            btrfs_super_chunk_root(disk_super),
496                                            blocksize);
497
498         BUG_ON(!chunk_root->node);
499         ret = btrfs_read_chunk_tree(chunk_root);
500         BUG_ON(ret);
501
502         blocksize = btrfs_level_size(tree_root,
503                                      btrfs_super_root_level(disk_super));
504
505         tree_root->node = read_tree_block(tree_root,
506                                           btrfs_super_root(disk_super),
507                                           blocksize);
508         BUG_ON(!tree_root->node);
509         ret = find_and_setup_root(tree_root, fs_info,
510                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
511         BUG_ON(ret);
512         extent_root->track_dirty = 1;
513
514         ret = find_and_setup_root(tree_root, fs_info,
515                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
516         BUG_ON(ret);
517         dev_root->track_dirty = 1;
518
519         ret = find_and_setup_root(tree_root, fs_info,
520                                   BTRFS_FS_TREE_OBJECTID, root);
521         BUG_ON(ret);
522         root->ref_cows = 1;
523         fs_info->generation = btrfs_super_generation(disk_super) + 1;
524         btrfs_read_block_groups(root);
525         return root;
526 }
527
528 int write_ctree_super(struct btrfs_trans_handle *trans,
529                       struct btrfs_root *root)
530 {
531         int ret;
532         struct btrfs_root *tree_root = root->fs_info->tree_root;
533         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
534         btrfs_set_super_generation(&root->fs_info->super_copy,
535                                    trans->transid);
536         btrfs_set_super_root(&root->fs_info->super_copy,
537                              tree_root->node->start);
538         btrfs_set_super_root_level(&root->fs_info->super_copy,
539                                    btrfs_header_level(tree_root->node));
540         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
541                                    chunk_root->node->start);
542         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
543                                          btrfs_header_level(chunk_root->node));
544         write_extent_buffer(root->fs_info->sb_buffer,
545                             &root->fs_info->super_copy, 0,
546                             sizeof(root->fs_info->super_copy));
547         ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
548         if (ret)
549                 fprintf(stderr, "failed to write new super block err %d\n", ret);
550         return ret;
551 }
552
553 static int close_all_devices(struct btrfs_fs_info *fs_info)
554 {
555         struct list_head *list;
556         struct list_head *next;
557         struct btrfs_device *device;
558
559         list = &fs_info->devices;
560         while(!list_empty(list)) {
561                 next = list->next;
562                 list_del(next);
563                 device = list_entry(next, struct btrfs_device, dev_list);
564                 kfree(device->name);
565                 close(device->fd);
566                 kfree(device);
567         }
568         return 0;
569 }
570
571 int close_ctree(struct btrfs_root *root)
572 {
573         int ret;
574         struct btrfs_trans_handle *trans;
575         struct btrfs_fs_info *fs_info = root->fs_info;
576
577         trans = btrfs_start_transaction(root, 1);
578         btrfs_commit_transaction(trans, root);
579         trans = btrfs_start_transaction(root, 1);
580         ret = commit_tree_roots(trans, root->fs_info);
581         BUG_ON(ret);
582         ret = __commit_transaction(trans, root);
583         BUG_ON(ret);
584         write_ctree_super(trans, root);
585         btrfs_free_transaction(root, trans);
586         btrfs_free_block_groups(root->fs_info);
587         close(root->fs_info->fp);
588         if (root->node)
589                 free_extent_buffer(root->node);
590         if (root->fs_info->extent_root->node)
591                 free_extent_buffer(root->fs_info->extent_root->node);
592         if (root->fs_info->tree_root->node)
593                 free_extent_buffer(root->fs_info->tree_root->node);
594         free_extent_buffer(root->commit_root);
595         free_extent_buffer(root->fs_info->sb_buffer);
596
597         if (root->fs_info->chunk_root->node);
598                 free_extent_buffer(root->fs_info->chunk_root->node);
599
600         if (root->fs_info->dev_root->node);
601                 free_extent_buffer(root->fs_info->dev_root->node);
602
603         close_all_devices(root->fs_info);
604         extent_io_tree_cleanup(&fs_info->extent_cache);
605         extent_io_tree_cleanup(&fs_info->free_space_cache);
606         extent_io_tree_cleanup(&fs_info->block_group_cache);
607         extent_io_tree_cleanup(&fs_info->pinned_extents);
608         extent_io_tree_cleanup(&fs_info->pending_del);
609         extent_io_tree_cleanup(&fs_info->extent_ins);
610
611         free(fs_info->tree_root);
612         free(fs_info->extent_root);
613         free(fs_info->fs_root);
614         free(fs_info->chunk_root);
615         free(fs_info->dev_root);
616         free(fs_info);
617
618         return 0;
619 }
620
621 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
622                      struct extent_buffer *eb)
623 {
624         return clear_extent_buffer_dirty(eb);
625 }
626
627 int wait_on_tree_block_writeback(struct btrfs_root *root,
628                                  struct extent_buffer *eb)
629 {
630         return 0;
631 }
632
633 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
634 {
635         set_extent_buffer_dirty(eb);
636 }
637
638 int btrfs_buffer_uptodate(struct extent_buffer *eb)
639 {
640         return extent_buffer_uptodate(eb);
641 }
642
643 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
644 {
645         return set_extent_buffer_uptodate(eb);
646 }