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