blob: f412b33921356d6f12b7288cb379425a84144ce3 [file] [log] [blame]
Chris Mason9f5fae22007-03-20 14:38:32 -04001#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 */
12int 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 search_key.objectid = search_start;
32 search_key.flags = 0;
33 btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
34 search_key.offset = 0;
35
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;
41
42 if (path.slots[0] > 0)
43 path.slots[0]--;
44
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;
58 }
59 *objectid = last_ino > search_start ?
60 last_ino : search_start;
61 goto found;
62 }
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;
72 }
73 }
74 }
75 start_found = 1;
76 last_ino = key.objectid + 1;
77 path.slots[0]++;
78 }
79 // FIXME -ENOSPC
80found:
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;
86error:
87 btrfs_release_path(root, &path);
88 return ret;
89}
90
91int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
92 struct btrfs_root *fs_root,
93 u64 objectid, struct btrfs_key *location)
94{
95 int ret = 0;
96 struct btrfs_path path;
97 struct btrfs_inode_map_item *inode_item;
98 struct btrfs_key key;
99 struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
100
101 key.objectid = objectid;
102 key.flags = 0;
103 btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
104 key.offset = 0;
105 btrfs_init_path(&path);
106 ret = btrfs_insert_empty_item(trans, inode_root, &path, &key,
107 sizeof(struct btrfs_inode_map_item));
108 if (ret)
109 goto out;
110
111 inode_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
112 struct btrfs_inode_map_item);
113 btrfs_cpu_key_to_disk(&inode_item->key, location);
114out:
115 btrfs_release_path(inode_root, &path);
116 return ret;
117}
118
119int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
120 struct btrfs_root *fs_root, struct btrfs_path *path,
121 u64 objectid, int mod)
122{
123 int ret;
124 struct btrfs_key key;
125 int ins_len = mod < 0 ? -1 : 0;
126 int cow = mod != 0;
127 struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
128
129 key.objectid = objectid;
130 key.flags = 0;
131 key.offset = 0;
132 btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
133 ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);
134 return ret;
135}
136