Update btrfs-progs to better match the kernel
[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 "transaction.h"
32 #include "crc32c.h"
33
34 static u64 allocated_bytes = 0;
35 int cache_max = 10000;
36
37 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh,
38                              u64 logical)
39 {
40         bh->fd = root->fs_info->fp;
41         bh->dev_bytenr = logical;
42         return 0;
43 }
44
45 static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
46 {
47         if (buf->bytenr != btrfs_header_bytenr(&buf->node.header))
48                 BUG();
49         if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid,
50                    sizeof(buf->node.header.fsid)))
51                 BUG();
52         return 0;
53 }
54
55 static int free_some_buffers(struct btrfs_root *root)
56 {
57         struct list_head *node, *next;
58         struct btrfs_buffer *b;
59         if (root->fs_info->cache_size < cache_max)
60                 return 0;
61         list_for_each_safe(node, next, &root->fs_info->cache) {
62                 b = list_entry(node, struct btrfs_buffer, cache);
63                 if (b->count == 1) {
64                         BUG_ON(!list_empty(&b->dirty));
65                         list_del_init(&b->cache);
66                         btrfs_block_release(root, b);
67                         if (root->fs_info->cache_size < cache_max)
68                                 break;
69                 }
70         }
71         return 0;
72 }
73
74 struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 bytenr,
75                                       u32 blocksize)
76 {
77         struct btrfs_buffer *buf;
78         int ret;
79
80         buf = malloc(sizeof(struct btrfs_buffer) + blocksize);
81         if (!buf)
82                 return buf;
83         allocated_bytes += blocksize;
84
85         buf->bytenr = bytenr;
86         buf->count = 2;
87         buf->size = blocksize;
88         buf->cache_node.start = bytenr;
89         buf->cache_node.size = blocksize;
90
91         INIT_LIST_HEAD(&buf->dirty);
92         free_some_buffers(root);
93
94         ret = insert_existing_cache_extent(&root->fs_info->extent_cache,
95                                            &buf->cache_node);
96
97         list_add_tail(&buf->cache, &root->fs_info->cache);
98         root->fs_info->cache_size += blocksize;
99         if (ret) {
100                 free(buf);
101                 return NULL;
102         }
103         return buf;
104 }
105
106 struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 bytenr,
107                                      u32 blocksize)
108 {
109         struct btrfs_buffer *buf;
110         struct cache_extent *cache;
111
112         cache = find_cache_extent(&root->fs_info->extent_cache,
113                                   bytenr, blocksize);
114         if (cache) {
115                 buf = container_of(cache, struct btrfs_buffer, cache_node);
116                 buf->count++;
117         } else {
118                 buf = alloc_tree_block(root, bytenr, blocksize);
119                 if (!buf) {
120                         BUG();
121                         return NULL;
122                 }
123         }
124         return buf;
125 }
126
127 struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
128                                      u32 blocksize)
129 {
130         struct btrfs_buffer *buf;
131         int ret;
132         struct cache_extent *cache;
133
134         cache = find_cache_extent(&root->fs_info->extent_cache,
135                                   bytenr, blocksize);
136         if (cache) {
137                 buf = container_of(cache, struct btrfs_buffer, cache_node);
138                 buf->count++;
139                 if (check_tree_block(root, buf))
140                         BUG();
141         } else {
142                 buf = alloc_tree_block(root, bytenr, blocksize);
143                 if (!buf)
144                         return NULL;
145                 btrfs_map_bh_to_logical(root, buf, bytenr);
146                 ret = pread(buf->fd, &buf->node, blocksize,
147                             buf->dev_bytenr);
148                 if (ret != blocksize) {
149                         free(buf);
150                         return NULL;
151                 }
152                 if (check_tree_block(root, buf))
153                         BUG();
154         }
155         return buf;
156 }
157
158 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
159                      struct btrfs_buffer *buf)
160 {
161         if (!list_empty(&buf->dirty))
162                 return 0;
163         list_add_tail(&buf->dirty, &root->fs_info->trans);
164         buf->count++;
165         if (check_tree_block(root, buf))
166                 BUG();
167         return 0;
168 }
169
170 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
171                      struct btrfs_buffer *buf)
172 {
173         if (!list_empty(&buf->dirty)) {
174                 list_del_init(&buf->dirty);
175                 btrfs_block_release(root, buf);
176         }
177         return 0;
178 }
179
180 int btrfs_csum_node(struct btrfs_root *root, struct btrfs_node *node)
181 {
182         u32 crc = ~(u32)0;
183         size_t len = btrfs_level_size(root, btrfs_header_level(&node->header)) -
184                                       BTRFS_CSUM_SIZE;
185
186         crc = crc32c(crc, (char *)(node) + BTRFS_CSUM_SIZE, len);
187         crc = ~cpu_to_le32(crc);
188         memcpy(node->header.csum, &crc, BTRFS_CRC32_SIZE);
189         return 0;
190 }
191
192 int btrfs_csum_super(struct btrfs_root *root, struct btrfs_super_block *super)
193 {
194         u32 crc = ~(u32)0;
195         char block[512];
196         size_t len = 512 - BTRFS_CSUM_SIZE;
197
198         memset(block, 0, 512);
199         memcpy(block, super, sizeof(*super));
200
201         crc = crc32c(crc, block + BTRFS_CSUM_SIZE, len);
202         crc = ~cpu_to_le32(crc);
203         memcpy(super->csum, &crc, BTRFS_CRC32_SIZE);
204         return 0;
205 }
206
207 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
208                      struct btrfs_buffer *buf)
209 {
210         int ret;
211
212         if (buf->bytenr != btrfs_header_bytenr(&buf->node.header))
213                 BUG();
214         btrfs_map_bh_to_logical(root, buf, buf->bytenr);
215         if (check_tree_block(root, buf))
216                 BUG();
217
218         btrfs_csum_node(root, &buf->node);
219
220         ret = pwrite(buf->fd, &buf->node, buf->size,
221                      buf->dev_bytenr);
222         if (ret != buf->size)
223                 return ret;
224         return 0;
225 }
226
227 static int __commit_transaction(struct btrfs_trans_handle *trans, struct
228                                 btrfs_root *root)
229 {
230         struct btrfs_buffer *b;
231         int ret = 0;
232         int wret;
233         while(!list_empty(&root->fs_info->trans)) {
234                 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
235                                dirty);
236                 list_del_init(&b->dirty);
237                 wret = write_tree_block(trans, root, b);
238                 if (wret)
239                         ret = wret;
240                 btrfs_block_release(root, b);
241         }
242         return ret;
243 }
244
245 static int commit_tree_roots(struct btrfs_trans_handle *trans,
246                              struct btrfs_fs_info *fs_info)
247 {
248         int ret;
249         u64 old_extent_bytenr;
250         struct btrfs_root *tree_root = fs_info->tree_root;
251         struct btrfs_root *extent_root = fs_info->extent_root;
252
253         btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
254         while(1) {
255                 old_extent_bytenr = btrfs_root_bytenr(&extent_root->root_item);
256                 if (old_extent_bytenr == extent_root->node->bytenr)
257                         break;
258                 btrfs_set_root_bytenr(&extent_root->root_item,
259                                        extent_root->node->bytenr);
260                 extent_root->root_item.level =
261                         btrfs_header_level(&extent_root->node->node.header);
262                 ret = btrfs_update_root(trans, tree_root,
263                                         &extent_root->root_key,
264                                         &extent_root->root_item);
265                 BUG_ON(ret);
266                 btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
267         }
268         return 0;
269 }
270
271 int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
272                              btrfs_root *root, struct btrfs_super_block *s)
273 {
274         int ret = 0;
275         struct btrfs_buffer *snap = root->commit_root;
276         struct btrfs_key snap_key;
277
278         if (root->commit_root == root->node)
279                 return 0;
280
281         memcpy(&snap_key, &root->root_key, sizeof(snap_key));
282         root->root_key.offset++;
283
284         btrfs_set_root_bytenr(&root->root_item, root->node->bytenr);
285         root->root_item.level =
286                         btrfs_header_level(&root->node->node.header);
287         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
288                                 &root->root_key, &root->root_item);
289         BUG_ON(ret);
290
291         ret = commit_tree_roots(trans, root->fs_info);
292         BUG_ON(ret);
293
294         ret = __commit_transaction(trans, root);
295         BUG_ON(ret);
296
297         write_ctree_super(trans, root, s);
298         btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
299         btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
300
301         root->commit_root = root->node;
302         root->node->count++;
303         ret = btrfs_drop_snapshot(trans, root, snap);
304         BUG_ON(ret);
305         ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
306         BUG_ON(ret);
307         btrfs_free_transaction(root, trans);
308         return ret;
309 }
310
311 static int __setup_root(struct btrfs_super_block *super,
312                         struct btrfs_root *root,
313                         struct btrfs_fs_info *fs_info,
314                         u64 objectid, int fp)
315 {
316         root->node = NULL;
317         root->commit_root = NULL;
318         root->sectorsize = btrfs_super_sectorsize(super);
319         root->nodesize = btrfs_super_nodesize(super);
320         root->leafsize = btrfs_super_leafsize(super);
321         root->stripesize = btrfs_super_stripesize(super);
322         root->ref_cows = 0;
323         root->fs_info = fs_info;
324         memset(&root->root_key, 0, sizeof(root->root_key));
325         memset(&root->root_item, 0, sizeof(root->root_item));
326         root->root_key.objectid = objectid;
327         return 0;
328 }
329
330 struct btrfs_buffer *read_root_block(struct btrfs_root *root, u64 bytenr,
331                                             u8 level)
332 {
333         struct btrfs_buffer *node;
334         u32 size = btrfs_level_size(root, level);
335
336         node = read_tree_block(root, bytenr, size);
337         BUG_ON(!node);
338         return node;
339 }
340
341 static int find_and_setup_root(struct btrfs_super_block *super,
342                                struct btrfs_root *tree_root,
343                                struct btrfs_fs_info *fs_info,
344                                u64 objectid,
345                                struct btrfs_root *root, int fp)
346 {
347         int ret;
348
349         __setup_root(super, root, fs_info, objectid, fp);
350         ret = btrfs_find_last_root(tree_root, objectid,
351                                    &root->root_item, &root->root_key);
352         BUG_ON(ret);
353         root->node = read_root_block(root,
354                                      btrfs_root_bytenr(&root->root_item),
355                                      root->root_item.level);
356         BUG_ON(!root->node);
357         return 0;
358 }
359
360 struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
361 {
362         int fp;
363
364         fp = open(filename, O_CREAT | O_RDWR, 0600);
365         if (fp < 0) {
366                 return NULL;
367         }
368         return open_ctree_fd(fp, super);
369 }
370
371 struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
372 {
373         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
374         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
375         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
376         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
377         int ret;
378
379         INIT_LIST_HEAD(&fs_info->trans);
380         INIT_LIST_HEAD(&fs_info->cache);
381         cache_tree_init(&fs_info->extent_cache);
382         cache_tree_init(&fs_info->pending_tree);
383         cache_tree_init(&fs_info->pinned_tree);
384         cache_tree_init(&fs_info->del_pending);
385         cache_tree_init(&fs_info->block_group_cache);
386         fs_info->cache_size = 0;
387         fs_info->fp = fp;
388         fs_info->running_transaction = NULL;
389         fs_info->fs_root = root;
390         fs_info->tree_root = tree_root;
391         fs_info->extent_root = extent_root;
392         fs_info->last_inode_alloc = 0;
393         fs_info->last_inode_alloc_dirid = 0;
394         fs_info->disk_super = super;
395         memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
396
397         ret = pread(fp, super, sizeof(struct btrfs_super_block),
398                      BTRFS_SUPER_INFO_OFFSET);
399         if (ret == 0 || btrfs_super_root(super) == 0) {
400                 BUG();
401                 return NULL;
402         }
403         BUG_ON(ret < 0);
404
405         __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
406         tree_root->node = read_root_block(tree_root, btrfs_super_root(super),
407                                           btrfs_super_root_level(super));
408         BUG_ON(!tree_root->node);
409
410         ret = find_and_setup_root(super, tree_root, fs_info,
411                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
412         BUG_ON(ret);
413
414         ret = find_and_setup_root(super, tree_root, fs_info,
415                                   BTRFS_FS_TREE_OBJECTID, root, fp);
416         BUG_ON(ret);
417
418         root->commit_root = root->node;
419         root->node->count++;
420         root->ref_cows = 1;
421         root->fs_info->generation = btrfs_super_generation(super) + 1;
422         btrfs_read_block_groups(root);
423         return root;
424 }
425
426 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
427                       *root, struct btrfs_super_block *s)
428 {
429         int ret;
430         btrfs_set_super_root(s, root->fs_info->tree_root->node->bytenr);
431         btrfs_set_super_generation(s, trans->transid);
432         btrfs_set_super_root_level(s,
433               btrfs_header_level(&root->fs_info->tree_root->node->node.header));
434         btrfs_csum_super(root, s);
435
436         ret = pwrite(root->fs_info->fp, s, sizeof(*s),
437                      BTRFS_SUPER_INFO_OFFSET);
438         if (ret != sizeof(*s)) {
439                 fprintf(stderr, "failed to write new super block err %d\n", ret);
440                 return ret;
441         }
442         return 0;
443 }
444
445 static int drop_cache(struct btrfs_root *root)
446 {
447         while(!list_empty(&root->fs_info->cache)) {
448                 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
449                                                     struct btrfs_buffer,
450                                                     cache);
451                 list_del_init(&b->cache);
452                 btrfs_block_release(root, b);
453         }
454         return 0;
455 }
456
457 int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
458 {
459         int ret;
460         struct btrfs_trans_handle *trans;
461         trans = btrfs_start_transaction(root, 1);
462         btrfs_commit_transaction(trans, root, s);
463         trans = btrfs_start_transaction(root, 1);
464         ret = commit_tree_roots(trans, root->fs_info);
465         BUG_ON(ret);
466         ret = __commit_transaction(trans, root);
467         BUG_ON(ret);
468         write_ctree_super(trans, root, s);
469         btrfs_free_transaction(root, trans);
470         drop_cache(root);
471         BUG_ON(!list_empty(&root->fs_info->trans));
472         btrfs_free_block_groups(root->fs_info);
473         close(root->fs_info->fp);
474         if (root->node)
475                 btrfs_block_release(root, root->node);
476         if (root->fs_info->extent_root->node)
477                 btrfs_block_release(root->fs_info->extent_root,
478                                     root->fs_info->extent_root->node);
479         if (root->fs_info->tree_root->node)
480                 btrfs_block_release(root->fs_info->tree_root,
481                                     root->fs_info->tree_root->node);
482         btrfs_block_release(root, root->commit_root);
483         free(root);
484         printf("on close %llu blocks are allocated\n",
485                (unsigned long long)allocated_bytes);
486         return 0;
487 }
488
489 void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
490 {
491         buf->count--;
492         if (buf->count < 0)
493                 BUG();
494         if (buf->count == 0) {
495                 BUG_ON(!list_empty(&buf->cache));
496                 BUG_ON(!list_empty(&buf->dirty));
497
498                 remove_cache_extent(&root->fs_info->extent_cache,
499                                     &buf->cache_node);
500                 BUG_ON(allocated_bytes == 0);
501                 allocated_bytes -= buf->size;
502                 BUG_ON(root->fs_info->cache_size == 0);
503                 root->fs_info->cache_size -= buf->size;
504
505                 memset(buf, 0, sizeof(*buf));
506                 free(buf);
507         }
508 }
509