blob: c56eb5909172956da6354eef29ea36f7f698a8fc [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason9f5fae22007-03-20 14:38:32 -040019#include "ctree.h"
20#include "disk-io.h"
21#include "transaction.h"
22
Chris Mason1b05da22007-04-10 12:13:09 -040023int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
Chris Mason5be6f7f2007-04-05 13:35:25 -040024{
25 struct btrfs_path *path;
26 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -040027 struct extent_buffer *l;
Chris Mason5be6f7f2007-04-05 13:35:25 -040028 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040029 struct btrfs_key found_key;
Chris Mason5be6f7f2007-04-05 13:35:25 -040030 int slot;
31
32 path = btrfs_alloc_path();
33 BUG_ON(!path);
34
Zheng Yan6527cdb2008-09-05 16:43:53 -040035 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
36 search_key.type = -1;
Chris Mason5be6f7f2007-04-05 13:35:25 -040037 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;
Chris Mason5f39d392007-10-15 16:14:19 -040044 l = path->nodes[0];
45 btrfs_item_key_to_cpu(l, &found_key, slot);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -040046 *objectid = max_t(u64, found_key.objectid,
47 BTRFS_FIRST_FREE_OBJECTID - 1);
Chris Mason5be6f7f2007-04-05 13:35:25 -040048 } else {
Yan, Zheng13a8a7c2009-09-21 15:56:00 -040049 *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -040050 }
51 ret = 0;
52error:
53 btrfs_free_path(path);
54 return ret;
55}
56
Chris Mason9f5fae22007-03-20 14:38:32 -040057int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
Chris Mason1b05da22007-04-10 12:13:09 -040058 struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -040059 u64 dirid, u64 *objectid)
60{
Chris Mason9f5fae22007-03-20 14:38:32 -040061 int ret;
Chris Masona2135012008-06-25 16:01:30 -040062 mutex_lock(&root->objectid_mutex);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -040063
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;
Chris Masona2135012008-06-25 16:01:30 -040068 }
Chris Masonb1a4d962007-04-04 15:27:52 -040069
Yan, Zheng13a8a7c2009-09-21 15:56:00 -040070 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
71 ret = -ENOSPC;
72 goto out;
Chris Mason9f5fae22007-03-20 14:38:32 -040073 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -040074
75 *objectid = ++root->highest_objectid;
76 ret = 0;
77out:
Chris Masona2135012008-06-25 16:01:30 -040078 mutex_unlock(&root->objectid_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -040079 return ret;
80}