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