Btrfs: corruption hunt continues
[safe/jmp/linux-2.6] / fs / btrfs / inode-map.c
1 #include <linux/module.h>
2 #include "ctree.h"
3 #include "disk-io.h"
4 #include "transaction.h"
5
6 /*
7  * walks the btree of allocated inodes and find a hole.
8  */
9 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
10                              struct btrfs_root *fs_root,
11                              u64 dirid, u64 *objectid)
12 {
13         struct btrfs_path path;
14         struct btrfs_key key;
15         int ret;
16         u64 hole_size = 0;
17         int slot = 0;
18         u64 last_ino = 0;
19         int start_found;
20         struct btrfs_leaf *l;
21         struct btrfs_root *root = fs_root->fs_info->inode_root;
22         struct btrfs_key search_key;
23         u64 search_start = dirid;
24
25         if (fs_root->fs_info->last_inode_alloc_dirid == dirid)
26                 search_start = fs_root->fs_info->last_inode_alloc;
27
28         search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
29         search_key.objectid = search_start;
30         search_key.flags = 0;
31         btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
32         search_key.offset = 0;
33
34         btrfs_init_path(&path);
35         start_found = 0;
36         ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0);
37         if (ret < 0)
38                 goto error;
39
40         if (path.slots[0] > 0)
41                 path.slots[0]--;
42
43         while (1) {
44                 l = btrfs_buffer_leaf(path.nodes[0]);
45                 slot = path.slots[0];
46                 if (slot >= btrfs_header_nritems(&l->header)) {
47                         ret = btrfs_next_leaf(root, &path);
48                         if (ret == 0)
49                                 continue;
50                         if (ret < 0)
51                                 goto error;
52                         if (!start_found) {
53                                 *objectid = search_start;
54                                 start_found = 1;
55                                 goto found;
56                         }
57                         *objectid = last_ino > search_start ?
58                                 last_ino : search_start;
59                         goto found;
60                 }
61                 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
62                 if (key.objectid >= search_start) {
63                         if (start_found) {
64                                 if (last_ino < search_start)
65                                         last_ino = search_start;
66                                 hole_size = key.objectid - last_ino;
67                                 if (hole_size > 0) {
68                                         *objectid = last_ino;
69                                         goto found;
70                                 }
71                         }
72                 }
73                 start_found = 1;
74                 last_ino = key.objectid + 1;
75                 path.slots[0]++;
76         }
77         // FIXME -ENOSPC
78 found:
79         root->fs_info->last_inode_alloc = *objectid;
80         root->fs_info->last_inode_alloc_dirid = dirid;
81         btrfs_release_path(root, &path);
82         BUG_ON(*objectid < search_start);
83         return 0;
84 error:
85         btrfs_release_path(root, &path);
86         return ret;
87 }
88
89 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
90                            struct btrfs_root *fs_root,
91                            u64 objectid, struct btrfs_key *location)
92 {
93         int ret = 0;
94         struct btrfs_path path;
95         struct btrfs_inode_map_item *inode_item;
96         struct btrfs_key key;
97         struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
98
99         key.objectid = objectid;
100         key.flags = 0;
101         btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
102         key.offset = 0;
103         btrfs_init_path(&path);
104         ret = btrfs_insert_empty_item(trans, inode_root, &path, &key,
105                                       sizeof(struct btrfs_inode_map_item));
106         if (ret)
107                 goto out;
108
109         inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
110                                     path.slots[0], struct btrfs_inode_map_item);
111         btrfs_cpu_key_to_disk(&inode_item->key, location);
112         btrfs_mark_buffer_dirty(path.nodes[0]);
113 out:
114         btrfs_release_path(inode_root, &path);
115         return ret;
116 }
117
118 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
119                            struct btrfs_root *fs_root, struct btrfs_path *path,
120                            u64 objectid, int mod)
121 {
122         int ret;
123         struct btrfs_key key;
124         int ins_len = mod < 0 ? -1 : 0;
125         int cow = mod != 0;
126         struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
127
128         key.objectid = objectid;
129         key.flags = 0;
130         key.offset = 0;
131         btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
132         ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);
133         return ret;
134 }
135