blob: 318e27a6378b7b61a0b5a92f6db710a6b34d8b52 [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
Chris Mason1b05da22007-04-10 12:13:09 -04006int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
Chris Mason5be6f7f2007-04-05 13:35:25 -04007{
8 struct btrfs_path *path;
9 int ret;
10 struct btrfs_leaf *l;
Chris Mason5be6f7f2007-04-05 13:35:25 -040011 struct btrfs_key search_key;
12 int slot;
13
14 path = btrfs_alloc_path();
15 BUG_ON(!path);
16
17 search_key.objectid = (u64)-1;
18 search_key.offset = (u64)-1;
19 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
20 if (ret < 0)
21 goto error;
22 BUG_ON(ret == 0);
23 if (path->slots[0] > 0) {
24 slot = path->slots[0] - 1;
25 l = btrfs_buffer_leaf(path->nodes[0]);
26 *objectid = btrfs_disk_key_objectid(&l->items[slot].key);
27 } else {
28 *objectid = BTRFS_FIRST_FREE_OBJECTID;
29 }
30 ret = 0;
31error:
32 btrfs_free_path(path);
33 return ret;
34}
35
Chris Mason9f5fae22007-03-20 14:38:32 -040036/*
37 * walks the btree of allocated inodes and find a hole.
38 */
39int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
Chris Mason1b05da22007-04-10 12:13:09 -040040 struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -040041 u64 dirid, u64 *objectid)
42{
Chris Mason7cfcc172007-04-02 14:53:59 -040043 struct btrfs_path *path;
Chris Mason9f5fae22007-03-20 14:38:32 -040044 struct btrfs_key key;
45 int ret;
46 u64 hole_size = 0;
47 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -040048 u64 last_ino = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040049 int start_found;
50 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -040051 struct btrfs_key search_key;
52 u64 search_start = dirid;
53
Chris Mason7cfcc172007-04-02 14:53:59 -040054 path = btrfs_alloc_path();
55 BUG_ON(!path);
Chris Masonb1a4d962007-04-04 15:27:52 -040056 search_key.flags = 0;
Chris Mason1b05da22007-04-10 12:13:09 -040057 search_start = root->last_inode_alloc;
Chris Masonb1a4d962007-04-04 15:27:52 -040058 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
59 search_key.objectid = search_start;
60 search_key.offset = 0;
61
Chris Mason7cfcc172007-04-02 14:53:59 -040062 btrfs_init_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040063 start_found = 0;
Chris Mason7cfcc172007-04-02 14:53:59 -040064 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
Chris Mason9f5fae22007-03-20 14:38:32 -040065 if (ret < 0)
66 goto error;
67
Chris Mason7cfcc172007-04-02 14:53:59 -040068 if (path->slots[0] > 0)
69 path->slots[0]--;
Chris Mason9f5fae22007-03-20 14:38:32 -040070
71 while (1) {
Chris Mason7cfcc172007-04-02 14:53:59 -040072 l = btrfs_buffer_leaf(path->nodes[0]);
73 slot = path->slots[0];
Chris Mason9f5fae22007-03-20 14:38:32 -040074 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason7cfcc172007-04-02 14:53:59 -040075 ret = btrfs_next_leaf(root, path);
Chris Mason9f5fae22007-03-20 14:38:32 -040076 if (ret == 0)
77 continue;
78 if (ret < 0)
79 goto error;
80 if (!start_found) {
81 *objectid = search_start;
82 start_found = 1;
83 goto found;
84 }
85 *objectid = last_ino > search_start ?
86 last_ino : search_start;
87 goto found;
88 }
89 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
90 if (key.objectid >= search_start) {
91 if (start_found) {
92 if (last_ino < search_start)
93 last_ino = search_start;
94 hole_size = key.objectid - last_ino;
95 if (hole_size > 0) {
96 *objectid = last_ino;
97 goto found;
98 }
99 }
100 }
101 start_found = 1;
102 last_ino = key.objectid + 1;
Chris Mason7cfcc172007-04-02 14:53:59 -0400103 path->slots[0]++;
Chris Mason9f5fae22007-03-20 14:38:32 -0400104 }
105 // FIXME -ENOSPC
106found:
Chris Mason1b05da22007-04-10 12:13:09 -0400107 root->last_inode_alloc = *objectid;
Chris Mason7cfcc172007-04-02 14:53:59 -0400108 btrfs_release_path(root, path);
109 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400110 BUG_ON(*objectid < search_start);
111 return 0;
112error:
Chris Mason7cfcc172007-04-02 14:53:59 -0400113 btrfs_release_path(root, path);
114 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400115 return ret;
116}