btrfs-progs: debug-tree: teach -t option about the chunk and root trees
[platform/upstream/btrfs-progs.git] / inode-map.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 #include "ctree.h"
20 #include "disk-io.h"
21 #include "transaction.h"
22
23 /*
24  * walks the btree of allocated inodes and find a hole.
25  */
26 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
27                              struct btrfs_root *root,
28                              u64 dirid, u64 *objectid)
29 {
30         struct btrfs_path *path;
31         struct btrfs_key key;
32         int ret;
33         int slot = 0;
34         u64 last_ino = 0;
35         int start_found;
36         struct extent_buffer *l;
37         struct btrfs_key search_key;
38         u64 search_start = dirid;
39
40         path = btrfs_alloc_path();
41         BUG_ON(!path);
42         search_start = root->last_inode_alloc;
43         search_start = max((unsigned long long)search_start,
44                                 BTRFS_FIRST_FREE_OBJECTID);
45         search_key.objectid = search_start;
46         search_key.offset = 0;
47         search_key.type = 0;
48
49         btrfs_init_path(path);
50         start_found = 0;
51         ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
52         if (ret < 0)
53                 goto error;
54
55         if (path->slots[0] > 0)
56                 path->slots[0]--;
57
58         while (1) {
59                 l = path->nodes[0];
60                 slot = path->slots[0];
61                 if (slot >= btrfs_header_nritems(l)) {
62                         ret = btrfs_next_leaf(root, path);
63                         if (ret == 0)
64                                 continue;
65                         if (ret < 0)
66                                 goto error;
67                         if (!start_found) {
68                                 *objectid = search_start;
69                                 start_found = 1;
70                                 goto found;
71                         }
72                         *objectid = last_ino > search_start ?
73                                 last_ino : search_start;
74                         goto found;
75                 }
76                 btrfs_item_key_to_cpu(l, &key, slot);
77                 if (key.objectid >= search_start) {
78                         if (start_found) {
79                                 if (last_ino < search_start)
80                                         last_ino = search_start;
81                                 if (key.objectid > last_ino) {
82                                         *objectid = last_ino;
83                                         goto found;
84                                 }
85                         }
86                 }
87                 start_found = 1;
88                 last_ino = key.objectid + 1;
89                 path->slots[0]++;
90         }
91         // FIXME -ENOSPC
92 found:
93         root->last_inode_alloc = *objectid;
94         btrfs_free_path(path);
95         BUG_ON(*objectid < search_start);
96         return 0;
97 error:
98         btrfs_free_path(path);
99         return ret;
100 }