blob: b3de823eb628d31735eae51f5a418aae36d54cad [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Mason9f5fae22007-03-20 14:38:32 -04002#include "ctree.h"
3#include "disk-io.h"
4#include "transaction.h"
5
6/*
7 * walks the btree of allocated inodes and find a hole.
8 */
9int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
10 struct btrfs_root *fs_root,
11 u64 dirid, u64 *objectid)
12{
Chris Mason7cfcc172007-04-02 14:53:59 -040013 struct btrfs_path *path;
Chris Mason9f5fae22007-03-20 14:38:32 -040014 struct btrfs_key key;
15 int ret;
16 u64 hole_size = 0;
17 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -040018 u64 last_ino = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040019 int start_found;
20 struct btrfs_leaf *l;
21 struct btrfs_root *root = fs_root->fs_info->inode_root;
22 struct btrfs_key search_key;
23 u64 search_start = dirid;
24
25 if (fs_root->fs_info->last_inode_alloc_dirid == dirid)
26 search_start = fs_root->fs_info->last_inode_alloc;
27
Chris Mason6407bf62007-03-27 06:33:00 -040028 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
Chris Mason9f5fae22007-03-20 14:38:32 -040029 search_key.objectid = search_start;
30 search_key.flags = 0;
31 btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
32 search_key.offset = 0;
33
Chris Mason7cfcc172007-04-02 14:53:59 -040034 path = btrfs_alloc_path();
35 BUG_ON(!path);
36 btrfs_init_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040037 start_found = 0;
Chris Mason7cfcc172007-04-02 14:53:59 -040038 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
Chris Mason9f5fae22007-03-20 14:38:32 -040039 if (ret < 0)
40 goto error;
41
Chris Mason7cfcc172007-04-02 14:53:59 -040042 if (path->slots[0] > 0)
43 path->slots[0]--;
Chris Mason9f5fae22007-03-20 14:38:32 -040044
45 while (1) {
Chris Mason7cfcc172007-04-02 14:53:59 -040046 l = btrfs_buffer_leaf(path->nodes[0]);
47 slot = path->slots[0];
Chris Mason9f5fae22007-03-20 14:38:32 -040048 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason7cfcc172007-04-02 14:53:59 -040049 ret = btrfs_next_leaf(root, path);
Chris Mason9f5fae22007-03-20 14:38:32 -040050 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;
Chris Mason7cfcc172007-04-02 14:53:59 -040077 path->slots[0]++;
Chris Mason9f5fae22007-03-20 14:38:32 -040078 }
79 // FIXME -ENOSPC
80found:
81 root->fs_info->last_inode_alloc = *objectid;
82 root->fs_info->last_inode_alloc_dirid = dirid;
Chris Mason7cfcc172007-04-02 14:53:59 -040083 btrfs_release_path(root, path);
84 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040085 BUG_ON(*objectid < search_start);
86 return 0;
87error:
Chris Mason7cfcc172007-04-02 14:53:59 -040088 btrfs_release_path(root, path);
89 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040090 return ret;
91}
92
93int 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;
Chris Mason7cfcc172007-04-02 14:53:59 -040098 struct btrfs_path *path;
Chris Mason9f5fae22007-03-20 14:38:32 -040099 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;
Chris Mason7cfcc172007-04-02 14:53:59 -0400107 path = btrfs_alloc_path();
108 BUG_ON(!path);
109 btrfs_init_path(path);
110 ret = btrfs_insert_empty_item(trans, inode_root, path, &key,
Chris Mason9f5fae22007-03-20 14:38:32 -0400111 sizeof(struct btrfs_inode_map_item));
112 if (ret)
113 goto out;
114
Chris Mason7cfcc172007-04-02 14:53:59 -0400115 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
116 path->slots[0], struct btrfs_inode_map_item);
Chris Mason9f5fae22007-03-20 14:38:32 -0400117 btrfs_cpu_key_to_disk(&inode_item->key, location);
Chris Mason7cfcc172007-04-02 14:53:59 -0400118 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason9f5fae22007-03-20 14:38:32 -0400119out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400120 btrfs_release_path(inode_root, path);
121 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400122 return ret;
123}
124
125int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
126 struct btrfs_root *fs_root, struct btrfs_path *path,
127 u64 objectid, int mod)
128{
129 int ret;
130 struct btrfs_key key;
131 int ins_len = mod < 0 ? -1 : 0;
132 int cow = mod != 0;
133 struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
134
135 key.objectid = objectid;
136 key.flags = 0;
137 key.offset = 0;
138 btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
139 ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);
140 return ret;
141}
142