| Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* | 
|  | 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 Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 19 | #include "ctree.h" | 
|  | 20 | #include "disk-io.h" | 
|  | 21 | #include "transaction.h" | 
|  | 22 |  | 
| Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 23 | int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) | 
| Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 24 | { | 
|  | 25 | struct btrfs_path *path; | 
|  | 26 | int ret; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 27 | struct extent_buffer *l; | 
| Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 28 | struct btrfs_key search_key; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 29 | struct btrfs_key found_key; | 
| Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 30 | int slot; | 
|  | 31 |  | 
|  | 32 | path = btrfs_alloc_path(); | 
|  | 33 | BUG_ON(!path); | 
|  | 34 |  | 
| Zheng Yan | 6527cdb | 2008-09-05 16:43:53 -0400 | [diff] [blame] | 35 | search_key.objectid = BTRFS_LAST_FREE_OBJECTID; | 
|  | 36 | search_key.type = -1; | 
| Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 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; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 44 | l = path->nodes[0]; | 
|  | 45 | btrfs_item_key_to_cpu(l, &found_key, slot); | 
|  | 46 | *objectid = found_key.objectid; | 
| Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 47 | } else { | 
|  | 48 | *objectid = BTRFS_FIRST_FREE_OBJECTID; | 
|  | 49 | } | 
|  | 50 | ret = 0; | 
|  | 51 | error: | 
|  | 52 | btrfs_free_path(path); | 
|  | 53 | return ret; | 
|  | 54 | } | 
|  | 55 |  | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 56 | /* | 
|  | 57 | * walks the btree of allocated inodes and find a hole. | 
|  | 58 | */ | 
|  | 59 | int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, | 
| Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 60 | struct btrfs_root *root, | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 61 | u64 dirid, u64 *objectid) | 
|  | 62 | { | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 63 | struct btrfs_path *path; | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 64 | struct btrfs_key key; | 
|  | 65 | int ret; | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 66 | int slot = 0; | 
| Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 67 | u64 last_ino = 0; | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 68 | int start_found; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 69 | struct extent_buffer *l; | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 70 | struct btrfs_key search_key; | 
|  | 71 | u64 search_start = dirid; | 
|  | 72 |  | 
| Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 73 | mutex_lock(&root->objectid_mutex); | 
| Zheng Yan | 6527cdb | 2008-09-05 16:43:53 -0400 | [diff] [blame] | 74 | if (root->last_inode_alloc >= BTRFS_FIRST_FREE_OBJECTID && | 
|  | 75 | root->last_inode_alloc < BTRFS_LAST_FREE_OBJECTID) { | 
| Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 76 | *objectid = ++root->last_inode_alloc; | 
|  | 77 | mutex_unlock(&root->objectid_mutex); | 
|  | 78 | return 0; | 
|  | 79 | } | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 80 | path = btrfs_alloc_path(); | 
|  | 81 | BUG_ON(!path); | 
| Joel Becker | e63b6a6c | 2009-04-21 12:38:30 -0700 | [diff] [blame] | 82 | search_start = max(search_start, (u64)BTRFS_FIRST_FREE_OBJECTID); | 
| Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 83 | search_key.objectid = search_start; | 
| Zheng Yan | 6527cdb | 2008-09-05 16:43:53 -0400 | [diff] [blame] | 84 | search_key.type = 0; | 
| Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 85 | search_key.offset = 0; | 
|  | 86 |  | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 87 | start_found = 0; | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 88 | ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 89 | if (ret < 0) | 
|  | 90 | goto error; | 
|  | 91 |  | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 92 | while (1) { | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 93 | l = path->nodes[0]; | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 94 | slot = path->slots[0]; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 95 | if (slot >= btrfs_header_nritems(l)) { | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 96 | ret = btrfs_next_leaf(root, path); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 97 | if (ret == 0) | 
|  | 98 | continue; | 
|  | 99 | if (ret < 0) | 
|  | 100 | goto error; | 
|  | 101 | if (!start_found) { | 
|  | 102 | *objectid = search_start; | 
|  | 103 | start_found = 1; | 
|  | 104 | goto found; | 
|  | 105 | } | 
|  | 106 | *objectid = last_ino > search_start ? | 
|  | 107 | last_ino : search_start; | 
|  | 108 | goto found; | 
|  | 109 | } | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 110 | btrfs_item_key_to_cpu(l, &key, slot); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 111 | if (key.objectid >= search_start) { | 
|  | 112 | if (start_found) { | 
|  | 113 | if (last_ino < search_start) | 
|  | 114 | last_ino = search_start; | 
| Yan | b178542 | 2008-01-22 12:46:56 -0500 | [diff] [blame] | 115 | if (key.objectid > last_ino) { | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 116 | *objectid = last_ino; | 
|  | 117 | goto found; | 
|  | 118 | } | 
| Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 119 | } else if (key.objectid > search_start) { | 
|  | 120 | *objectid = search_start; | 
|  | 121 | goto found; | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 122 | } | 
|  | 123 | } | 
| Zheng Yan | 6527cdb | 2008-09-05 16:43:53 -0400 | [diff] [blame] | 124 | if (key.objectid >= BTRFS_LAST_FREE_OBJECTID) | 
|  | 125 | break; | 
| Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 126 |  | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 127 | start_found = 1; | 
|  | 128 | last_ino = key.objectid + 1; | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 129 | path->slots[0]++; | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 130 | } | 
| Zheng Yan | 6527cdb | 2008-09-05 16:43:53 -0400 | [diff] [blame] | 131 | BUG_ON(1); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 132 | found: | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 133 | btrfs_release_path(root, path); | 
|  | 134 | btrfs_free_path(path); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 135 | BUG_ON(*objectid < search_start); | 
| Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 136 | mutex_unlock(&root->objectid_mutex); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 137 | return 0; | 
|  | 138 | error: | 
| Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 139 | btrfs_release_path(root, path); | 
|  | 140 | btrfs_free_path(path); | 
| Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 141 | mutex_unlock(&root->objectid_mutex); | 
| Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 142 | return ret; | 
|  | 143 | } |