32 bit compile fixes
[btrfs-progs-unstable.git] / inode-map.c
blob4622303887d078531798deaf3259d1f4b55cf0e3
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"
9 /*
10 * walks the btree of allocated inodes and find a hole.
12 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
13 struct btrfs_root *root,
14 u64 dirid, u64 *objectid)
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 = 0;
22 int start_found;
23 struct btrfs_leaf *l;
24 struct btrfs_key search_key;
25 u64 search_start = dirid;
27 if (root->fs_info->last_inode_alloc_dirid == dirid)
28 search_start = root->fs_info->last_inode_alloc;
30 if (search_start < BTRFS_FIRST_FREE_OBJECTID)
31 search_start = BTRFS_FIRST_FREE_OBJECTID;
32 search_key.objectid = search_start;
33 search_key.flags = 0;
34 search_key.offset = 0;
36 btrfs_init_path(&path);
37 start_found = 0;
38 ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0);
39 if (ret < 0)
40 goto error;
42 if (path.slots[0] > 0)
43 path.slots[0]--;
45 while (1) {
46 l = &path.nodes[0]->leaf;
47 slot = path.slots[0];
48 if (slot >= btrfs_header_nritems(&l->header)) {
49 ret = btrfs_next_leaf(root, &path);
50 if (ret == 0)
51 continue;
52 if (ret < 0)
53 goto error;
54 if (!start_found) {
55 *objectid = search_start;
56 start_found = 1;
57 goto found;
59 *objectid = last_ino > search_start ?
60 last_ino : search_start;
61 goto found;
63 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
64 if (key.objectid >= search_start) {
65 if (start_found) {
66 if (last_ino < search_start)
67 last_ino = search_start;
68 hole_size = key.objectid - last_ino;
69 if (hole_size > 0) {
70 *objectid = last_ino;
71 goto found;
75 start_found = 1;
76 last_ino = key.objectid + 1;
77 path.slots[0]++;
79 // FIXME -ENOSPC
80 found:
81 root->fs_info->last_inode_alloc = *objectid;
82 root->fs_info->last_inode_alloc_dirid = dirid;
83 btrfs_release_path(root, &path);
84 BUG_ON(*objectid < search_start);
85 return 0;
86 error:
87 btrfs_release_path(root, &path);
88 return ret;