inode-map.c 2.1 KB
Newer Older
C
Chris Mason 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

19 20 21 22
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"

C
Chris Mason 已提交
23
int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
C
Chris Mason 已提交
24 25 26
{
	struct btrfs_path *path;
	int ret;
27
	struct extent_buffer *l;
C
Chris Mason 已提交
28
	struct btrfs_key search_key;
29
	struct btrfs_key found_key;
C
Chris Mason 已提交
30 31 32 33 34
	int slot;

	path = btrfs_alloc_path();
	BUG_ON(!path);

35 36
	search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
	search_key.type = -1;
C
Chris Mason 已提交
37 38 39 40 41 42 43
	search_key.offset = (u64)-1;
	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
	if (ret < 0)
		goto error;
	BUG_ON(ret == 0);
	if (path->slots[0] > 0) {
		slot = path->slots[0] - 1;
44 45
		l = path->nodes[0];
		btrfs_item_key_to_cpu(l, &found_key, slot);
46 47
		*objectid = max_t(u64, found_key.objectid,
				  BTRFS_FIRST_FREE_OBJECTID - 1);
C
Chris Mason 已提交
48
	} else {
49
		*objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
C
Chris Mason 已提交
50 51 52 53 54 55 56
	}
	ret = 0;
error:
	btrfs_free_path(path);
	return ret;
}

57
int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
C
Chris Mason 已提交
58
			     struct btrfs_root *root,
59 60 61
			     u64 dirid, u64 *objectid)
{
	int ret;
62
	mutex_lock(&root->objectid_mutex);
63

64 65 66 67 68
	if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
		ret = btrfs_find_highest_inode(root, &root->highest_objectid);
		if (ret)
			goto out;
	}
69

70 71 72
	if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
		ret = -ENOSPC;
		goto out;
73
	}
74 75 76 77

	*objectid = ++root->highest_objectid;
	ret = 0;
out:
78
	mutex_unlock(&root->objectid_mutex);
79 80
	return ret;
}