Allow large blocks
[platform/upstream/btrfs-progs.git] / inode-map.c
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26
27 /*
28  * walks the btree of allocated inodes and find a hole.
29  */
30 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
31                              struct btrfs_root *root,
32                              u64 dirid, u64 *objectid)
33 {
34         struct btrfs_path path;
35         struct btrfs_key key;
36         int ret;
37         u64 hole_size = 0;
38         int slot = 0;
39         u64 last_ino = 0;
40         int start_found;
41         struct btrfs_leaf *l;
42         struct btrfs_key search_key;
43         u64 search_start = dirid;
44
45         if (root->fs_info->last_inode_alloc_dirid == dirid)
46                 search_start = root->fs_info->last_inode_alloc;
47
48         if (search_start < BTRFS_FIRST_FREE_OBJECTID)
49                 search_start = BTRFS_FIRST_FREE_OBJECTID;
50         search_key.objectid = search_start;
51         search_key.type = 0;
52         search_key.offset = 0;
53
54         btrfs_init_path(&path);
55         start_found = 0;
56         ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0);
57         if (ret < 0)
58                 goto error;
59
60         if (path.slots[0] > 0)
61                 path.slots[0]--;
62
63         while (1) {
64                 l = &path.nodes[0]->leaf;
65                 slot = path.slots[0];
66                 if (slot >= btrfs_header_nritems(&l->header)) {
67                         ret = btrfs_next_leaf(root, &path);
68                         if (ret == 0)
69                                 continue;
70                         if (ret < 0)
71                                 goto error;
72                         if (!start_found) {
73                                 *objectid = search_start;
74                                 start_found = 1;
75                                 goto found;
76                         }
77                         *objectid = last_ino > search_start ?
78                                 last_ino : search_start;
79                         goto found;
80                 }
81                 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
82                 if (key.objectid >= search_start) {
83                         if (start_found) {
84                                 if (last_ino < search_start)
85                                         last_ino = search_start;
86                                 hole_size = key.objectid - last_ino;
87                                 if (hole_size > 0) {
88                                         *objectid = last_ino;
89                                         goto found;
90                                 }
91                         }
92                 }
93                 start_found = 1;
94                 last_ino = key.objectid + 1;
95                 path.slots[0]++;
96         }
97         // FIXME -ENOSPC
98 found:
99         root->fs_info->last_inode_alloc = *objectid;
100         root->fs_info->last_inode_alloc_dirid = dirid;
101         btrfs_release_path(root, &path);
102         BUG_ON(*objectid < search_start);
103         return 0;
104 error:
105         btrfs_release_path(root, &path);
106         return ret;
107 }