csum data struct changes
[platform/upstream/btrfs-progs.git] / inode-map.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "kerncompat.h"
4 #include "radix-tree.h"
5 #include "ctree.h"
6 #include "disk-io.h"
7 #include "transaction.h"
8
9 /*
10  * walks the btree of allocated inodes and find a hole.
11  */
12 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
13                              struct btrfs_root *fs_root,
14                              u64 dirid, u64 *objectid)
15 {
16         struct btrfs_path path;
17         struct btrfs_key key;
18         int ret;
19         u64 hole_size = 0;
20         int slot = 0;
21         u64 last_ino;
22         int start_found;
23         struct btrfs_leaf *l;
24         struct btrfs_root *root = fs_root->fs_info->inode_root;
25         struct btrfs_key search_key;
26         u64 search_start = dirid;
27
28         if (fs_root->fs_info->last_inode_alloc_dirid == dirid)
29                 search_start = fs_root->fs_info->last_inode_alloc;
30
31         if (search_start < BTRFS_FIRST_FREE_OBJECTID)
32                 search_start = BTRFS_FIRST_FREE_OBJECTID;
33         search_key.objectid = search_start;
34         search_key.flags = 0;
35         btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
36         search_key.offset = 0;
37
38         btrfs_init_path(&path);
39         start_found = 0;
40         ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0);
41         if (ret < 0)
42                 goto error;
43
44         if (path.slots[0] > 0)
45                 path.slots[0]--;
46
47         while (1) {
48                 l = &path.nodes[0]->leaf;
49                 slot = path.slots[0];
50                 if (slot >= btrfs_header_nritems(&l->header)) {
51                         ret = btrfs_next_leaf(root, &path);
52                         if (ret == 0)
53                                 continue;
54                         if (ret < 0)
55                                 goto error;
56                         if (!start_found) {
57                                 *objectid = search_start;
58                                 start_found = 1;
59                                 goto found;
60                         }
61                         *objectid = last_ino > search_start ?
62                                 last_ino : search_start;
63                         goto found;
64                 }
65                 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
66                 if (key.objectid >= search_start) {
67                         if (start_found) {
68                                 if (last_ino < search_start)
69                                         last_ino = search_start;
70                                 hole_size = key.objectid - last_ino;
71                                 if (hole_size > 0) {
72                                         *objectid = last_ino;
73                                         goto found;
74                                 }
75                         }
76                 }
77                 start_found = 1;
78                 last_ino = key.objectid + 1;
79                 path.slots[0]++;
80         }
81         // FIXME -ENOSPC
82 found:
83         root->fs_info->last_inode_alloc = *objectid;
84         root->fs_info->last_inode_alloc_dirid = dirid;
85         btrfs_release_path(root, &path);
86         BUG_ON(*objectid < search_start);
87         return 0;
88 error:
89         btrfs_release_path(root, &path);
90         return ret;
91 }
92
93 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
94                            struct btrfs_root *fs_root,
95                            u64 objectid, struct btrfs_key *location)
96 {
97         int ret = 0;
98         struct btrfs_path path;
99         struct btrfs_inode_map_item *inode_item;
100         struct btrfs_key key;
101         struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
102
103         key.objectid = objectid;
104         key.flags = 0;
105         btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
106         key.offset = 0;
107         btrfs_init_path(&path);
108         ret = btrfs_insert_empty_item(trans, inode_root, &path, &key,
109                                       sizeof(struct btrfs_inode_map_item));
110         if (ret)
111                 goto out;
112
113         inode_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
114                                   struct btrfs_inode_map_item);
115         btrfs_cpu_key_to_disk(&inode_item->key, location);
116 out:
117         btrfs_release_path(inode_root, &path);
118         return ret;
119 }
120
121 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
122                            struct btrfs_root *fs_root, struct btrfs_path *path,
123                            u64 objectid, int mod)
124 {
125         int ret;
126         struct btrfs_key key;
127         int ins_len = mod < 0 ? -1 : 0;
128         int cow = mod != 0;
129         struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
130
131         key.objectid = objectid;
132         key.flags = 0;
133         key.offset = 0;
134         btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
135         ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);
136         return ret;
137 }
138