1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "ctree.h"
20#include "disk-io.h"
21#include "transaction.h"
22
23int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
24{
25 struct btrfs_path *path;
26 int ret;
27 struct extent_buffer *l;
28 struct btrfs_key search_key;
29 struct btrfs_key found_key;
30 int slot;
31
32 path = btrfs_alloc_path();
33 BUG_ON(!path);
34
35 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
36 search_key.type = -1;
37 search_key.offset = (u64)-1;
38 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
39 if (ret < 0)
40 goto error;
41 BUG_ON(ret == 0);
42 if (path->slots[0] > 0) {
43 slot = path->slots[0] - 1;
44 l = path->nodes[0];
45 btrfs_item_key_to_cpu(l, &found_key, slot);
46 *objectid = max_t(u64, found_key.objectid,
47 BTRFS_FIRST_FREE_OBJECTID - 1);
48 } else {
49 *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
50 }
51 ret = 0;
52error:
53 btrfs_free_path(path);
54 return ret;
55}
56
57int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
58 struct btrfs_root *root,
59 u64 dirid, u64 *objectid)
60{
61 int ret;
62 mutex_lock(&root->objectid_mutex);
63
64 if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
65 ret = btrfs_find_highest_inode(root, &root->highest_objectid);
66 if (ret)
67 goto out;
68 }
69
70 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
71 ret = -ENOSPC;
72 goto out;
73 }
74
75 *objectid = ++root->highest_objectid;
76 ret = 0;
77out:
78 mutex_unlock(&root->objectid_mutex);
79 return ret;
80}
81