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 | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 20 | #include "ctree.h" |
| 21 | #include "disk-io.h" |
| 22 | #include "transaction.h" |
| 23 | |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 24 | int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 25 | { |
| 26 | struct btrfs_path *path; |
| 27 | int ret; |
| 28 | struct btrfs_leaf *l; |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 29 | struct btrfs_key search_key; |
| 30 | int slot; |
| 31 | |
| 32 | path = btrfs_alloc_path(); |
| 33 | BUG_ON(!path); |
| 34 | |
| 35 | search_key.objectid = (u64)-1; |
| 36 | search_key.offset = (u64)-1; |
| 37 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 38 | if (ret < 0) |
| 39 | goto error; |
| 40 | BUG_ON(ret == 0); |
| 41 | if (path->slots[0] > 0) { |
| 42 | slot = path->slots[0] - 1; |
| 43 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 44 | *objectid = btrfs_disk_key_objectid(&l->items[slot].key); |
| 45 | } else { |
| 46 | *objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 47 | } |
| 48 | ret = 0; |
| 49 | error: |
| 50 | btrfs_free_path(path); |
| 51 | return ret; |
| 52 | } |
| 53 | |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 54 | /* |
| 55 | * walks the btree of allocated inodes and find a hole. |
| 56 | */ |
| 57 | int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 58 | struct btrfs_root *root, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 59 | u64 dirid, u64 *objectid) |
| 60 | { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 61 | struct btrfs_path *path; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 62 | struct btrfs_key key; |
| 63 | int ret; |
| 64 | u64 hole_size = 0; |
| 65 | int slot = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 66 | u64 last_ino = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 67 | int start_found; |
| 68 | struct btrfs_leaf *l; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 69 | struct btrfs_key search_key; |
| 70 | u64 search_start = dirid; |
| 71 | |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 72 | path = btrfs_alloc_path(); |
| 73 | BUG_ON(!path); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 74 | search_key.flags = 0; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 75 | search_start = root->last_inode_alloc; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 76 | search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID); |
| 77 | search_key.objectid = search_start; |
| 78 | search_key.offset = 0; |
| 79 | |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 80 | btrfs_init_path(path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 81 | start_found = 0; |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 82 | ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 83 | if (ret < 0) |
| 84 | goto error; |
| 85 | |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 86 | if (path->slots[0] > 0) |
| 87 | path->slots[0]--; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 88 | |
| 89 | while (1) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 90 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 91 | slot = path->slots[0]; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 92 | if (slot >= btrfs_header_nritems(&l->header)) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 93 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 94 | if (ret == 0) |
| 95 | continue; |
| 96 | if (ret < 0) |
| 97 | goto error; |
| 98 | if (!start_found) { |
| 99 | *objectid = search_start; |
| 100 | start_found = 1; |
| 101 | goto found; |
| 102 | } |
| 103 | *objectid = last_ino > search_start ? |
| 104 | last_ino : search_start; |
| 105 | goto found; |
| 106 | } |
| 107 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
| 108 | if (key.objectid >= search_start) { |
| 109 | if (start_found) { |
| 110 | if (last_ino < search_start) |
| 111 | last_ino = search_start; |
| 112 | hole_size = key.objectid - last_ino; |
| 113 | if (hole_size > 0) { |
| 114 | *objectid = last_ino; |
| 115 | goto found; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | start_found = 1; |
| 120 | last_ino = key.objectid + 1; |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 121 | path->slots[0]++; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 122 | } |
| 123 | // FIXME -ENOSPC |
| 124 | found: |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 125 | root->last_inode_alloc = *objectid; |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 126 | btrfs_release_path(root, path); |
| 127 | btrfs_free_path(path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 128 | BUG_ON(*objectid < search_start); |
| 129 | return 0; |
| 130 | error: |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 131 | btrfs_release_path(root, path); |
| 132 | btrfs_free_path(path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 133 | return ret; |
| 134 | } |