Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 STRATO. 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 "ctree.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "backref.h" |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 22 | #include "ulist.h" |
| 23 | #include "transaction.h" |
| 24 | #include "delayed-ref.h" |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 25 | #include "locking.h" |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 26 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 27 | struct extent_inode_elem { |
| 28 | u64 inum; |
| 29 | u64 offset; |
| 30 | struct extent_inode_elem *next; |
| 31 | }; |
| 32 | |
| 33 | static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb, |
| 34 | struct btrfs_file_extent_item *fi, |
| 35 | u64 extent_item_pos, |
| 36 | struct extent_inode_elem **eie) |
| 37 | { |
| 38 | u64 data_offset; |
| 39 | u64 data_len; |
| 40 | struct extent_inode_elem *e; |
| 41 | |
| 42 | data_offset = btrfs_file_extent_offset(eb, fi); |
| 43 | data_len = btrfs_file_extent_num_bytes(eb, fi); |
| 44 | |
| 45 | if (extent_item_pos < data_offset || |
| 46 | extent_item_pos >= data_offset + data_len) |
| 47 | return 1; |
| 48 | |
| 49 | e = kmalloc(sizeof(*e), GFP_NOFS); |
| 50 | if (!e) |
| 51 | return -ENOMEM; |
| 52 | |
| 53 | e->next = *eie; |
| 54 | e->inum = key->objectid; |
| 55 | e->offset = key->offset + (extent_item_pos - data_offset); |
| 56 | *eie = e; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte, |
| 62 | u64 extent_item_pos, |
| 63 | struct extent_inode_elem **eie) |
| 64 | { |
| 65 | u64 disk_byte; |
| 66 | struct btrfs_key key; |
| 67 | struct btrfs_file_extent_item *fi; |
| 68 | int slot; |
| 69 | int nritems; |
| 70 | int extent_type; |
| 71 | int ret; |
| 72 | |
| 73 | /* |
| 74 | * from the shared data ref, we only have the leaf but we need |
| 75 | * the key. thus, we must look into all items and see that we |
| 76 | * find one (some) with a reference to our extent item. |
| 77 | */ |
| 78 | nritems = btrfs_header_nritems(eb); |
| 79 | for (slot = 0; slot < nritems; ++slot) { |
| 80 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 81 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 82 | continue; |
| 83 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 84 | extent_type = btrfs_file_extent_type(eb, fi); |
| 85 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) |
| 86 | continue; |
| 87 | /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ |
| 88 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 89 | if (disk_byte != wanted_disk_byte) |
| 90 | continue; |
| 91 | |
| 92 | ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); |
| 93 | if (ret < 0) |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 100 | /* |
| 101 | * this structure records all encountered refs on the way up to the root |
| 102 | */ |
| 103 | struct __prelim_ref { |
| 104 | struct list_head list; |
| 105 | u64 root_id; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 106 | struct btrfs_key key_for_search; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 107 | int level; |
| 108 | int count; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 109 | struct extent_inode_elem *inode_list; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 110 | u64 parent; |
| 111 | u64 wanted_disk_byte; |
| 112 | }; |
| 113 | |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 114 | /* |
| 115 | * the rules for all callers of this function are: |
| 116 | * - obtaining the parent is the goal |
| 117 | * - if you add a key, you must know that it is a correct key |
| 118 | * - if you cannot add the parent or a correct key, then we will look into the |
| 119 | * block later to set a correct key |
| 120 | * |
| 121 | * delayed refs |
| 122 | * ============ |
| 123 | * backref type | shared | indirect | shared | indirect |
| 124 | * information | tree | tree | data | data |
| 125 | * --------------------+--------+----------+--------+---------- |
| 126 | * parent logical | y | - | - | - |
| 127 | * key to resolve | - | y | y | y |
| 128 | * tree block logical | - | - | - | - |
| 129 | * root for resolving | y | y | y | y |
| 130 | * |
| 131 | * - column 1: we've the parent -> done |
| 132 | * - column 2, 3, 4: we use the key to find the parent |
| 133 | * |
| 134 | * on disk refs (inline or keyed) |
| 135 | * ============================== |
| 136 | * backref type | shared | indirect | shared | indirect |
| 137 | * information | tree | tree | data | data |
| 138 | * --------------------+--------+----------+--------+---------- |
| 139 | * parent logical | y | - | y | - |
| 140 | * key to resolve | - | - | - | y |
| 141 | * tree block logical | y | y | y | y |
| 142 | * root for resolving | - | y | y | y |
| 143 | * |
| 144 | * - column 1, 3: we've the parent -> done |
| 145 | * - column 2: we take the first key from the block to find the parent |
| 146 | * (see __add_missing_keys) |
| 147 | * - column 4: we use the key to find the parent |
| 148 | * |
| 149 | * additional information that's available but not required to find the parent |
| 150 | * block might help in merging entries to gain some speed. |
| 151 | */ |
| 152 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 153 | static int __add_prelim_ref(struct list_head *head, u64 root_id, |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 154 | struct btrfs_key *key, int level, |
| 155 | u64 parent, u64 wanted_disk_byte, int count) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 156 | { |
| 157 | struct __prelim_ref *ref; |
| 158 | |
| 159 | /* in case we're adding delayed refs, we're holding the refs spinlock */ |
| 160 | ref = kmalloc(sizeof(*ref), GFP_ATOMIC); |
| 161 | if (!ref) |
| 162 | return -ENOMEM; |
| 163 | |
| 164 | ref->root_id = root_id; |
| 165 | if (key) |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 166 | ref->key_for_search = *key; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 167 | else |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 168 | memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 169 | |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 170 | ref->inode_list = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 171 | ref->level = level; |
| 172 | ref->count = count; |
| 173 | ref->parent = parent; |
| 174 | ref->wanted_disk_byte = wanted_disk_byte; |
| 175 | list_add_tail(&ref->list, head); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 181 | struct ulist *parents, int level, |
| 182 | struct btrfs_key *key, u64 wanted_disk_byte, |
| 183 | const u64 *extent_item_pos) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 184 | { |
| 185 | int ret; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 186 | int slot = path->slots[level]; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 187 | struct extent_buffer *eb = path->nodes[level]; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 188 | struct btrfs_file_extent_item *fi; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 189 | struct extent_inode_elem *eie = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 190 | u64 disk_byte; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 191 | u64 wanted_objectid = key->objectid; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 192 | |
| 193 | add_parent: |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 194 | if (level == 0 && extent_item_pos) { |
| 195 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 196 | ret = check_extent_in_eb(key, eb, fi, *extent_item_pos, &eie); |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | } |
| 200 | ret = ulist_add(parents, eb->start, (unsigned long)eie, GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 201 | if (ret < 0) |
| 202 | return ret; |
| 203 | |
| 204 | if (level != 0) |
| 205 | return 0; |
| 206 | |
| 207 | /* |
| 208 | * if the current leaf is full with EXTENT_DATA items, we must |
| 209 | * check the next one if that holds a reference as well. |
| 210 | * ref->count cannot be used to skip this check. |
| 211 | * repeat this until we don't find any additional EXTENT_DATA items. |
| 212 | */ |
| 213 | while (1) { |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 214 | eie = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 215 | ret = btrfs_next_leaf(root, path); |
| 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | if (ret) |
| 219 | return 0; |
| 220 | |
| 221 | eb = path->nodes[0]; |
| 222 | for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 223 | btrfs_item_key_to_cpu(eb, key, slot); |
| 224 | if (key->objectid != wanted_objectid || |
| 225 | key->type != BTRFS_EXTENT_DATA_KEY) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 226 | return 0; |
| 227 | fi = btrfs_item_ptr(eb, slot, |
| 228 | struct btrfs_file_extent_item); |
| 229 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 230 | if (disk_byte == wanted_disk_byte) |
| 231 | goto add_parent; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * resolve an indirect backref in the form (root_id, key, level) |
| 240 | * to a logical address |
| 241 | */ |
| 242 | static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 243 | int search_commit_root, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 244 | u64 time_seq, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 245 | struct __prelim_ref *ref, |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 246 | struct ulist *parents, |
| 247 | const u64 *extent_item_pos) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 248 | { |
| 249 | struct btrfs_path *path; |
| 250 | struct btrfs_root *root; |
| 251 | struct btrfs_key root_key; |
| 252 | struct btrfs_key key = {0}; |
| 253 | struct extent_buffer *eb; |
| 254 | int ret = 0; |
| 255 | int root_level; |
| 256 | int level = ref->level; |
| 257 | |
| 258 | path = btrfs_alloc_path(); |
| 259 | if (!path) |
| 260 | return -ENOMEM; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 261 | path->search_commit_root = !!search_commit_root; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 262 | |
| 263 | root_key.objectid = ref->root_id; |
| 264 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 265 | root_key.offset = (u64)-1; |
| 266 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 267 | if (IS_ERR(root)) { |
| 268 | ret = PTR_ERR(root); |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | rcu_read_lock(); |
| 273 | root_level = btrfs_header_level(root->node); |
| 274 | rcu_read_unlock(); |
| 275 | |
| 276 | if (root_level + 1 == level) |
| 277 | goto out; |
| 278 | |
| 279 | path->lowest_level = level; |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 280 | ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 281 | pr_debug("search slot in root %llu (level %d, ref count %d) returned " |
| 282 | "%d for key (%llu %u %llu)\n", |
| 283 | (unsigned long long)ref->root_id, level, ref->count, ret, |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 284 | (unsigned long long)ref->key_for_search.objectid, |
| 285 | ref->key_for_search.type, |
| 286 | (unsigned long long)ref->key_for_search.offset); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 287 | if (ret < 0) |
| 288 | goto out; |
| 289 | |
| 290 | eb = path->nodes[level]; |
| 291 | if (!eb) { |
| 292 | WARN_ON(1); |
| 293 | ret = 1; |
| 294 | goto out; |
| 295 | } |
| 296 | |
Jan Schmidt | f617e2fd | 2012-06-14 16:10:13 +0200 | [diff] [blame^] | 297 | if (level == 0) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 298 | btrfs_item_key_to_cpu(eb, &key, path->slots[0]); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 299 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 300 | ret = add_all_parents(root, path, parents, level, &key, |
| 301 | ref->wanted_disk_byte, extent_item_pos); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 302 | out: |
| 303 | btrfs_free_path(path); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * resolve all indirect backrefs from the list |
| 309 | */ |
| 310 | static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 311 | int search_commit_root, u64 time_seq, |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 312 | struct list_head *head, |
| 313 | const u64 *extent_item_pos) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 314 | { |
| 315 | int err; |
| 316 | int ret = 0; |
| 317 | struct __prelim_ref *ref; |
| 318 | struct __prelim_ref *ref_safe; |
| 319 | struct __prelim_ref *new_ref; |
| 320 | struct ulist *parents; |
| 321 | struct ulist_node *node; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 322 | struct ulist_iterator uiter; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 323 | |
| 324 | parents = ulist_alloc(GFP_NOFS); |
| 325 | if (!parents) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | /* |
| 329 | * _safe allows us to insert directly after the current item without |
| 330 | * iterating over the newly inserted items. |
| 331 | * we're also allowed to re-assign ref during iteration. |
| 332 | */ |
| 333 | list_for_each_entry_safe(ref, ref_safe, head, list) { |
| 334 | if (ref->parent) /* already direct */ |
| 335 | continue; |
| 336 | if (ref->count == 0) |
| 337 | continue; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 338 | err = __resolve_indirect_ref(fs_info, search_commit_root, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 339 | time_seq, ref, parents, |
| 340 | extent_item_pos); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 341 | if (err) { |
| 342 | if (ret == 0) |
| 343 | ret = err; |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | /* we put the first parent into the ref at hand */ |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 348 | ULIST_ITER_INIT(&uiter); |
| 349 | node = ulist_next(parents, &uiter); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 350 | ref->parent = node ? node->val : 0; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 351 | ref->inode_list = |
| 352 | node ? (struct extent_inode_elem *)node->aux : 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 353 | |
| 354 | /* additional parents require new refs being added here */ |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 355 | while ((node = ulist_next(parents, &uiter))) { |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 356 | new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS); |
| 357 | if (!new_ref) { |
| 358 | ret = -ENOMEM; |
| 359 | break; |
| 360 | } |
| 361 | memcpy(new_ref, ref, sizeof(*ref)); |
| 362 | new_ref->parent = node->val; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 363 | new_ref->inode_list = |
| 364 | (struct extent_inode_elem *)node->aux; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 365 | list_add(&new_ref->list, &ref->list); |
| 366 | } |
| 367 | ulist_reinit(parents); |
| 368 | } |
| 369 | |
| 370 | ulist_free(parents); |
| 371 | return ret; |
| 372 | } |
| 373 | |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 374 | static inline int ref_for_same_block(struct __prelim_ref *ref1, |
| 375 | struct __prelim_ref *ref2) |
| 376 | { |
| 377 | if (ref1->level != ref2->level) |
| 378 | return 0; |
| 379 | if (ref1->root_id != ref2->root_id) |
| 380 | return 0; |
| 381 | if (ref1->key_for_search.type != ref2->key_for_search.type) |
| 382 | return 0; |
| 383 | if (ref1->key_for_search.objectid != ref2->key_for_search.objectid) |
| 384 | return 0; |
| 385 | if (ref1->key_for_search.offset != ref2->key_for_search.offset) |
| 386 | return 0; |
| 387 | if (ref1->parent != ref2->parent) |
| 388 | return 0; |
| 389 | |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * read tree blocks and add keys where required. |
| 395 | */ |
| 396 | static int __add_missing_keys(struct btrfs_fs_info *fs_info, |
| 397 | struct list_head *head) |
| 398 | { |
| 399 | struct list_head *pos; |
| 400 | struct extent_buffer *eb; |
| 401 | |
| 402 | list_for_each(pos, head) { |
| 403 | struct __prelim_ref *ref; |
| 404 | ref = list_entry(pos, struct __prelim_ref, list); |
| 405 | |
| 406 | if (ref->parent) |
| 407 | continue; |
| 408 | if (ref->key_for_search.type) |
| 409 | continue; |
| 410 | BUG_ON(!ref->wanted_disk_byte); |
| 411 | eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte, |
| 412 | fs_info->tree_root->leafsize, 0); |
| 413 | BUG_ON(!eb); |
| 414 | btrfs_tree_read_lock(eb); |
| 415 | if (btrfs_header_level(eb) == 0) |
| 416 | btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); |
| 417 | else |
| 418 | btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); |
| 419 | btrfs_tree_read_unlock(eb); |
| 420 | free_extent_buffer(eb); |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 425 | /* |
| 426 | * merge two lists of backrefs and adjust counts accordingly |
| 427 | * |
| 428 | * mode = 1: merge identical keys, if key is set |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 429 | * FIXME: if we add more keys in __add_prelim_ref, we can merge more here. |
| 430 | * additionally, we could even add a key range for the blocks we |
| 431 | * looked into to merge even more (-> replace unresolved refs by those |
| 432 | * having a parent). |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 433 | * mode = 2: merge identical parents |
| 434 | */ |
| 435 | static int __merge_refs(struct list_head *head, int mode) |
| 436 | { |
| 437 | struct list_head *pos1; |
| 438 | |
| 439 | list_for_each(pos1, head) { |
| 440 | struct list_head *n2; |
| 441 | struct list_head *pos2; |
| 442 | struct __prelim_ref *ref1; |
| 443 | |
| 444 | ref1 = list_entry(pos1, struct __prelim_ref, list); |
| 445 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 446 | for (pos2 = pos1->next, n2 = pos2->next; pos2 != head; |
| 447 | pos2 = n2, n2 = pos2->next) { |
| 448 | struct __prelim_ref *ref2; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 449 | struct __prelim_ref *xchg; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 450 | |
| 451 | ref2 = list_entry(pos2, struct __prelim_ref, list); |
| 452 | |
| 453 | if (mode == 1) { |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 454 | if (!ref_for_same_block(ref1, ref2)) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 455 | continue; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 456 | if (!ref1->parent && ref2->parent) { |
| 457 | xchg = ref1; |
| 458 | ref1 = ref2; |
| 459 | ref2 = xchg; |
| 460 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 461 | ref1->count += ref2->count; |
| 462 | } else { |
| 463 | if (ref1->parent != ref2->parent) |
| 464 | continue; |
| 465 | ref1->count += ref2->count; |
| 466 | } |
| 467 | list_del(&ref2->list); |
| 468 | kfree(ref2); |
| 469 | } |
| 470 | |
| 471 | } |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * add all currently queued delayed refs from this head whose seq nr is |
| 477 | * smaller or equal that seq to the list |
| 478 | */ |
| 479 | static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 480 | struct list_head *prefs) |
| 481 | { |
| 482 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
| 483 | struct rb_node *n = &head->node.rb_node; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 484 | struct btrfs_key key; |
| 485 | struct btrfs_key op_key = {0}; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 486 | int sgn; |
Jan Schmidt | b1375d6 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 487 | int ret = 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 488 | |
| 489 | if (extent_op && extent_op->update_key) |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 490 | btrfs_disk_key_to_cpu(&op_key, &extent_op->key); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 491 | |
| 492 | while ((n = rb_prev(n))) { |
| 493 | struct btrfs_delayed_ref_node *node; |
| 494 | node = rb_entry(n, struct btrfs_delayed_ref_node, |
| 495 | rb_node); |
| 496 | if (node->bytenr != head->node.bytenr) |
| 497 | break; |
| 498 | WARN_ON(node->is_head); |
| 499 | |
| 500 | if (node->seq > seq) |
| 501 | continue; |
| 502 | |
| 503 | switch (node->action) { |
| 504 | case BTRFS_ADD_DELAYED_EXTENT: |
| 505 | case BTRFS_UPDATE_DELAYED_HEAD: |
| 506 | WARN_ON(1); |
| 507 | continue; |
| 508 | case BTRFS_ADD_DELAYED_REF: |
| 509 | sgn = 1; |
| 510 | break; |
| 511 | case BTRFS_DROP_DELAYED_REF: |
| 512 | sgn = -1; |
| 513 | break; |
| 514 | default: |
| 515 | BUG_ON(1); |
| 516 | } |
| 517 | switch (node->type) { |
| 518 | case BTRFS_TREE_BLOCK_REF_KEY: { |
| 519 | struct btrfs_delayed_tree_ref *ref; |
| 520 | |
| 521 | ref = btrfs_delayed_node_to_tree_ref(node); |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 522 | ret = __add_prelim_ref(prefs, ref->root, &op_key, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 523 | ref->level + 1, 0, node->bytenr, |
| 524 | node->ref_mod * sgn); |
| 525 | break; |
| 526 | } |
| 527 | case BTRFS_SHARED_BLOCK_REF_KEY: { |
| 528 | struct btrfs_delayed_tree_ref *ref; |
| 529 | |
| 530 | ref = btrfs_delayed_node_to_tree_ref(node); |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 531 | ret = __add_prelim_ref(prefs, ref->root, NULL, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 532 | ref->level + 1, ref->parent, |
| 533 | node->bytenr, |
| 534 | node->ref_mod * sgn); |
| 535 | break; |
| 536 | } |
| 537 | case BTRFS_EXTENT_DATA_REF_KEY: { |
| 538 | struct btrfs_delayed_data_ref *ref; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 539 | ref = btrfs_delayed_node_to_data_ref(node); |
| 540 | |
| 541 | key.objectid = ref->objectid; |
| 542 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 543 | key.offset = ref->offset; |
| 544 | ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, |
| 545 | node->bytenr, |
| 546 | node->ref_mod * sgn); |
| 547 | break; |
| 548 | } |
| 549 | case BTRFS_SHARED_DATA_REF_KEY: { |
| 550 | struct btrfs_delayed_data_ref *ref; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 551 | |
| 552 | ref = btrfs_delayed_node_to_data_ref(node); |
| 553 | |
| 554 | key.objectid = ref->objectid; |
| 555 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 556 | key.offset = ref->offset; |
| 557 | ret = __add_prelim_ref(prefs, ref->root, &key, 0, |
| 558 | ref->parent, node->bytenr, |
| 559 | node->ref_mod * sgn); |
| 560 | break; |
| 561 | } |
| 562 | default: |
| 563 | WARN_ON(1); |
| 564 | } |
| 565 | BUG_ON(ret); |
| 566 | } |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * add all inline backrefs for bytenr to the list |
| 573 | */ |
| 574 | static int __add_inline_refs(struct btrfs_fs_info *fs_info, |
| 575 | struct btrfs_path *path, u64 bytenr, |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 576 | int *info_level, struct list_head *prefs) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 577 | { |
Jan Schmidt | b1375d6 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 578 | int ret = 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 579 | int slot; |
| 580 | struct extent_buffer *leaf; |
| 581 | struct btrfs_key key; |
| 582 | unsigned long ptr; |
| 583 | unsigned long end; |
| 584 | struct btrfs_extent_item *ei; |
| 585 | u64 flags; |
| 586 | u64 item_size; |
| 587 | |
| 588 | /* |
| 589 | * enumerate all inline refs |
| 590 | */ |
| 591 | leaf = path->nodes[0]; |
Jan Schmidt | dadcaf7 | 2012-05-22 13:43:25 +0200 | [diff] [blame] | 592 | slot = path->slots[0]; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 593 | |
| 594 | item_size = btrfs_item_size_nr(leaf, slot); |
| 595 | BUG_ON(item_size < sizeof(*ei)); |
| 596 | |
| 597 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); |
| 598 | flags = btrfs_extent_flags(leaf, ei); |
| 599 | |
| 600 | ptr = (unsigned long)(ei + 1); |
| 601 | end = (unsigned long)ei + item_size; |
| 602 | |
| 603 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 604 | struct btrfs_tree_block_info *info; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 605 | |
| 606 | info = (struct btrfs_tree_block_info *)ptr; |
| 607 | *info_level = btrfs_tree_block_level(leaf, info); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 608 | ptr += sizeof(struct btrfs_tree_block_info); |
| 609 | BUG_ON(ptr > end); |
| 610 | } else { |
| 611 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 612 | } |
| 613 | |
| 614 | while (ptr < end) { |
| 615 | struct btrfs_extent_inline_ref *iref; |
| 616 | u64 offset; |
| 617 | int type; |
| 618 | |
| 619 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 620 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 621 | offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 622 | |
| 623 | switch (type) { |
| 624 | case BTRFS_SHARED_BLOCK_REF_KEY: |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 625 | ret = __add_prelim_ref(prefs, 0, NULL, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 626 | *info_level + 1, offset, |
| 627 | bytenr, 1); |
| 628 | break; |
| 629 | case BTRFS_SHARED_DATA_REF_KEY: { |
| 630 | struct btrfs_shared_data_ref *sdref; |
| 631 | int count; |
| 632 | |
| 633 | sdref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 634 | count = btrfs_shared_data_ref_count(leaf, sdref); |
| 635 | ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, |
| 636 | bytenr, count); |
| 637 | break; |
| 638 | } |
| 639 | case BTRFS_TREE_BLOCK_REF_KEY: |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 640 | ret = __add_prelim_ref(prefs, offset, NULL, |
| 641 | *info_level + 1, 0, |
| 642 | bytenr, 1); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 643 | break; |
| 644 | case BTRFS_EXTENT_DATA_REF_KEY: { |
| 645 | struct btrfs_extent_data_ref *dref; |
| 646 | int count; |
| 647 | u64 root; |
| 648 | |
| 649 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 650 | count = btrfs_extent_data_ref_count(leaf, dref); |
| 651 | key.objectid = btrfs_extent_data_ref_objectid(leaf, |
| 652 | dref); |
| 653 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 654 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); |
| 655 | root = btrfs_extent_data_ref_root(leaf, dref); |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 656 | ret = __add_prelim_ref(prefs, root, &key, 0, 0, |
| 657 | bytenr, count); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 658 | break; |
| 659 | } |
| 660 | default: |
| 661 | WARN_ON(1); |
| 662 | } |
| 663 | BUG_ON(ret); |
| 664 | ptr += btrfs_extent_inline_ref_size(type); |
| 665 | } |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * add all non-inline backrefs for bytenr to the list |
| 672 | */ |
| 673 | static int __add_keyed_refs(struct btrfs_fs_info *fs_info, |
| 674 | struct btrfs_path *path, u64 bytenr, |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 675 | int info_level, struct list_head *prefs) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 676 | { |
| 677 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 678 | int ret; |
| 679 | int slot; |
| 680 | struct extent_buffer *leaf; |
| 681 | struct btrfs_key key; |
| 682 | |
| 683 | while (1) { |
| 684 | ret = btrfs_next_item(extent_root, path); |
| 685 | if (ret < 0) |
| 686 | break; |
| 687 | if (ret) { |
| 688 | ret = 0; |
| 689 | break; |
| 690 | } |
| 691 | |
| 692 | slot = path->slots[0]; |
| 693 | leaf = path->nodes[0]; |
| 694 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 695 | |
| 696 | if (key.objectid != bytenr) |
| 697 | break; |
| 698 | if (key.type < BTRFS_TREE_BLOCK_REF_KEY) |
| 699 | continue; |
| 700 | if (key.type > BTRFS_SHARED_DATA_REF_KEY) |
| 701 | break; |
| 702 | |
| 703 | switch (key.type) { |
| 704 | case BTRFS_SHARED_BLOCK_REF_KEY: |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 705 | ret = __add_prelim_ref(prefs, 0, NULL, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 706 | info_level + 1, key.offset, |
| 707 | bytenr, 1); |
| 708 | break; |
| 709 | case BTRFS_SHARED_DATA_REF_KEY: { |
| 710 | struct btrfs_shared_data_ref *sdref; |
| 711 | int count; |
| 712 | |
| 713 | sdref = btrfs_item_ptr(leaf, slot, |
| 714 | struct btrfs_shared_data_ref); |
| 715 | count = btrfs_shared_data_ref_count(leaf, sdref); |
| 716 | ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, |
| 717 | bytenr, count); |
| 718 | break; |
| 719 | } |
| 720 | case BTRFS_TREE_BLOCK_REF_KEY: |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 721 | ret = __add_prelim_ref(prefs, key.offset, NULL, |
| 722 | info_level + 1, 0, |
| 723 | bytenr, 1); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 724 | break; |
| 725 | case BTRFS_EXTENT_DATA_REF_KEY: { |
| 726 | struct btrfs_extent_data_ref *dref; |
| 727 | int count; |
| 728 | u64 root; |
| 729 | |
| 730 | dref = btrfs_item_ptr(leaf, slot, |
| 731 | struct btrfs_extent_data_ref); |
| 732 | count = btrfs_extent_data_ref_count(leaf, dref); |
| 733 | key.objectid = btrfs_extent_data_ref_objectid(leaf, |
| 734 | dref); |
| 735 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 736 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); |
| 737 | root = btrfs_extent_data_ref_root(leaf, dref); |
| 738 | ret = __add_prelim_ref(prefs, root, &key, 0, 0, |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 739 | bytenr, count); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | default: |
| 743 | WARN_ON(1); |
| 744 | } |
| 745 | BUG_ON(ret); |
| 746 | } |
| 747 | |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * this adds all existing backrefs (inline backrefs, backrefs and delayed |
| 753 | * refs) for the given bytenr to the refs list, merges duplicates and resolves |
| 754 | * indirect refs to their parent bytenr. |
| 755 | * When roots are found, they're added to the roots list |
| 756 | * |
| 757 | * FIXME some caching might speed things up |
| 758 | */ |
| 759 | static int find_parent_nodes(struct btrfs_trans_handle *trans, |
| 760 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 761 | u64 delayed_ref_seq, u64 time_seq, |
| 762 | struct ulist *refs, struct ulist *roots, |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 763 | const u64 *extent_item_pos) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 764 | { |
| 765 | struct btrfs_key key; |
| 766 | struct btrfs_path *path; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 767 | struct btrfs_delayed_ref_root *delayed_refs = NULL; |
Li Zefan | d3b0106 | 2012-03-03 07:41:15 -0500 | [diff] [blame] | 768 | struct btrfs_delayed_ref_head *head; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 769 | int info_level = 0; |
| 770 | int ret; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 771 | int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 772 | struct list_head prefs_delayed; |
| 773 | struct list_head prefs; |
| 774 | struct __prelim_ref *ref; |
| 775 | |
| 776 | INIT_LIST_HEAD(&prefs); |
| 777 | INIT_LIST_HEAD(&prefs_delayed); |
| 778 | |
| 779 | key.objectid = bytenr; |
| 780 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 781 | key.offset = (u64)-1; |
| 782 | |
| 783 | path = btrfs_alloc_path(); |
| 784 | if (!path) |
| 785 | return -ENOMEM; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 786 | path->search_commit_root = !!search_commit_root; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 787 | |
| 788 | /* |
| 789 | * grab both a lock on the path and a lock on the delayed ref head. |
| 790 | * We need both to get a consistent picture of how the refs look |
| 791 | * at a specified point in time |
| 792 | */ |
| 793 | again: |
Li Zefan | d3b0106 | 2012-03-03 07:41:15 -0500 | [diff] [blame] | 794 | head = NULL; |
| 795 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 796 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); |
| 797 | if (ret < 0) |
| 798 | goto out; |
| 799 | BUG_ON(ret == 0); |
| 800 | |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 801 | if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) { |
| 802 | /* |
| 803 | * look if there are updates for this ref queued and lock the |
| 804 | * head |
| 805 | */ |
| 806 | delayed_refs = &trans->transaction->delayed_refs; |
| 807 | spin_lock(&delayed_refs->lock); |
| 808 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 809 | if (head) { |
| 810 | if (!mutex_trylock(&head->mutex)) { |
| 811 | atomic_inc(&head->node.refs); |
| 812 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 813 | |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 814 | btrfs_release_path(path); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 815 | |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 816 | /* |
| 817 | * Mutex was contended, block until it's |
| 818 | * released and try again |
| 819 | */ |
| 820 | mutex_lock(&head->mutex); |
| 821 | mutex_unlock(&head->mutex); |
| 822 | btrfs_put_delayed_ref(&head->node); |
| 823 | goto again; |
| 824 | } |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 825 | ret = __add_delayed_refs(head, delayed_ref_seq, |
| 826 | &prefs_delayed); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 827 | if (ret) { |
| 828 | spin_unlock(&delayed_refs->lock); |
| 829 | goto out; |
| 830 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 831 | } |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 832 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 833 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 834 | |
| 835 | if (path->slots[0]) { |
| 836 | struct extent_buffer *leaf; |
| 837 | int slot; |
| 838 | |
Jan Schmidt | dadcaf7 | 2012-05-22 13:43:25 +0200 | [diff] [blame] | 839 | path->slots[0]--; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 840 | leaf = path->nodes[0]; |
Jan Schmidt | dadcaf7 | 2012-05-22 13:43:25 +0200 | [diff] [blame] | 841 | slot = path->slots[0]; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 842 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 843 | if (key.objectid == bytenr && |
| 844 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
| 845 | ret = __add_inline_refs(fs_info, path, bytenr, |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 846 | &info_level, &prefs); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 847 | if (ret) |
| 848 | goto out; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 849 | ret = __add_keyed_refs(fs_info, path, bytenr, |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 850 | info_level, &prefs); |
| 851 | if (ret) |
| 852 | goto out; |
| 853 | } |
| 854 | } |
| 855 | btrfs_release_path(path); |
| 856 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 857 | list_splice_init(&prefs_delayed, &prefs); |
| 858 | |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 859 | ret = __add_missing_keys(fs_info, &prefs); |
| 860 | if (ret) |
| 861 | goto out; |
| 862 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 863 | ret = __merge_refs(&prefs, 1); |
| 864 | if (ret) |
| 865 | goto out; |
| 866 | |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 867 | ret = __resolve_indirect_refs(fs_info, search_commit_root, time_seq, |
| 868 | &prefs, extent_item_pos); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 869 | if (ret) |
| 870 | goto out; |
| 871 | |
| 872 | ret = __merge_refs(&prefs, 2); |
| 873 | if (ret) |
| 874 | goto out; |
| 875 | |
| 876 | while (!list_empty(&prefs)) { |
| 877 | ref = list_first_entry(&prefs, struct __prelim_ref, list); |
| 878 | list_del(&ref->list); |
| 879 | if (ref->count < 0) |
| 880 | WARN_ON(1); |
| 881 | if (ref->count && ref->root_id && ref->parent == 0) { |
| 882 | /* no parent == root of tree */ |
| 883 | ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); |
| 884 | BUG_ON(ret < 0); |
| 885 | } |
| 886 | if (ref->count && ref->parent) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 887 | struct extent_inode_elem *eie = NULL; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 888 | if (extent_item_pos && !ref->inode_list) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 889 | u32 bsz; |
| 890 | struct extent_buffer *eb; |
| 891 | bsz = btrfs_level_size(fs_info->extent_root, |
| 892 | info_level); |
| 893 | eb = read_tree_block(fs_info->extent_root, |
| 894 | ref->parent, bsz, 0); |
| 895 | BUG_ON(!eb); |
| 896 | ret = find_extent_in_eb(eb, bytenr, |
| 897 | *extent_item_pos, &eie); |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 898 | ref->inode_list = eie; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 899 | free_extent_buffer(eb); |
| 900 | } |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 901 | ret = ulist_add_merge(refs, ref->parent, |
| 902 | (unsigned long)ref->inode_list, |
| 903 | (unsigned long *)&eie, GFP_NOFS); |
| 904 | if (!ret && extent_item_pos) { |
| 905 | /* |
| 906 | * we've recorded that parent, so we must extend |
| 907 | * its inode list here |
| 908 | */ |
| 909 | BUG_ON(!eie); |
| 910 | while (eie->next) |
| 911 | eie = eie->next; |
| 912 | eie->next = ref->inode_list; |
| 913 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 914 | BUG_ON(ret < 0); |
| 915 | } |
| 916 | kfree(ref); |
| 917 | } |
| 918 | |
| 919 | out: |
| 920 | if (head) |
| 921 | mutex_unlock(&head->mutex); |
| 922 | btrfs_free_path(path); |
| 923 | while (!list_empty(&prefs)) { |
| 924 | ref = list_first_entry(&prefs, struct __prelim_ref, list); |
| 925 | list_del(&ref->list); |
| 926 | kfree(ref); |
| 927 | } |
| 928 | while (!list_empty(&prefs_delayed)) { |
| 929 | ref = list_first_entry(&prefs_delayed, struct __prelim_ref, |
| 930 | list); |
| 931 | list_del(&ref->list); |
| 932 | kfree(ref); |
| 933 | } |
| 934 | |
| 935 | return ret; |
| 936 | } |
| 937 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 938 | static void free_leaf_list(struct ulist *blocks) |
| 939 | { |
| 940 | struct ulist_node *node = NULL; |
| 941 | struct extent_inode_elem *eie; |
| 942 | struct extent_inode_elem *eie_next; |
| 943 | struct ulist_iterator uiter; |
| 944 | |
| 945 | ULIST_ITER_INIT(&uiter); |
| 946 | while ((node = ulist_next(blocks, &uiter))) { |
| 947 | if (!node->aux) |
| 948 | continue; |
| 949 | eie = (struct extent_inode_elem *)node->aux; |
| 950 | for (; eie; eie = eie_next) { |
| 951 | eie_next = eie->next; |
| 952 | kfree(eie); |
| 953 | } |
| 954 | node->aux = 0; |
| 955 | } |
| 956 | |
| 957 | ulist_free(blocks); |
| 958 | } |
| 959 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 960 | /* |
| 961 | * Finds all leafs with a reference to the specified combination of bytenr and |
| 962 | * offset. key_list_head will point to a list of corresponding keys (caller must |
| 963 | * free each list element). The leafs will be stored in the leafs ulist, which |
| 964 | * must be freed with ulist_free. |
| 965 | * |
| 966 | * returns 0 on success, <0 on error |
| 967 | */ |
| 968 | static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, |
| 969 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 970 | u64 delayed_ref_seq, u64 time_seq, |
| 971 | struct ulist **leafs, |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 972 | const u64 *extent_item_pos) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 973 | { |
| 974 | struct ulist *tmp; |
| 975 | int ret; |
| 976 | |
| 977 | tmp = ulist_alloc(GFP_NOFS); |
| 978 | if (!tmp) |
| 979 | return -ENOMEM; |
| 980 | *leafs = ulist_alloc(GFP_NOFS); |
| 981 | if (!*leafs) { |
| 982 | ulist_free(tmp); |
| 983 | return -ENOMEM; |
| 984 | } |
| 985 | |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 986 | ret = find_parent_nodes(trans, fs_info, bytenr, delayed_ref_seq, |
| 987 | time_seq, *leafs, tmp, extent_item_pos); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 988 | ulist_free(tmp); |
| 989 | |
| 990 | if (ret < 0 && ret != -ENOENT) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 991 | free_leaf_list(*leafs); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * walk all backrefs for a given extent to find all roots that reference this |
| 1000 | * extent. Walking a backref means finding all extents that reference this |
| 1001 | * extent and in turn walk the backrefs of those, too. Naturally this is a |
| 1002 | * recursive process, but here it is implemented in an iterative fashion: We |
| 1003 | * find all referencing extents for the extent in question and put them on a |
| 1004 | * list. In turn, we find all referencing extents for those, further appending |
| 1005 | * to the list. The way we iterate the list allows adding more elements after |
| 1006 | * the current while iterating. The process stops when we reach the end of the |
| 1007 | * list. Found roots are added to the roots list. |
| 1008 | * |
| 1009 | * returns 0 on success, < 0 on error. |
| 1010 | */ |
| 1011 | int btrfs_find_all_roots(struct btrfs_trans_handle *trans, |
| 1012 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1013 | u64 delayed_ref_seq, u64 time_seq, |
| 1014 | struct ulist **roots) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1015 | { |
| 1016 | struct ulist *tmp; |
| 1017 | struct ulist_node *node = NULL; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1018 | struct ulist_iterator uiter; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1019 | int ret; |
| 1020 | |
| 1021 | tmp = ulist_alloc(GFP_NOFS); |
| 1022 | if (!tmp) |
| 1023 | return -ENOMEM; |
| 1024 | *roots = ulist_alloc(GFP_NOFS); |
| 1025 | if (!*roots) { |
| 1026 | ulist_free(tmp); |
| 1027 | return -ENOMEM; |
| 1028 | } |
| 1029 | |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1030 | ULIST_ITER_INIT(&uiter); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1031 | while (1) { |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1032 | ret = find_parent_nodes(trans, fs_info, bytenr, delayed_ref_seq, |
| 1033 | time_seq, tmp, *roots, NULL); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1034 | if (ret < 0 && ret != -ENOENT) { |
| 1035 | ulist_free(tmp); |
| 1036 | ulist_free(*roots); |
| 1037 | return ret; |
| 1038 | } |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1039 | node = ulist_next(tmp, &uiter); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1040 | if (!node) |
| 1041 | break; |
| 1042 | bytenr = node->val; |
| 1043 | } |
| 1044 | |
| 1045 | ulist_free(tmp); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1050 | static int __inode_info(u64 inum, u64 ioff, u8 key_type, |
| 1051 | struct btrfs_root *fs_root, struct btrfs_path *path, |
| 1052 | struct btrfs_key *found_key) |
| 1053 | { |
| 1054 | int ret; |
| 1055 | struct btrfs_key key; |
| 1056 | struct extent_buffer *eb; |
| 1057 | |
| 1058 | key.type = key_type; |
| 1059 | key.objectid = inum; |
| 1060 | key.offset = ioff; |
| 1061 | |
| 1062 | ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); |
| 1063 | if (ret < 0) |
| 1064 | return ret; |
| 1065 | |
| 1066 | eb = path->nodes[0]; |
| 1067 | if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { |
| 1068 | ret = btrfs_next_leaf(fs_root, path); |
| 1069 | if (ret) |
| 1070 | return ret; |
| 1071 | eb = path->nodes[0]; |
| 1072 | } |
| 1073 | |
| 1074 | btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); |
| 1075 | if (found_key->type != key.type || found_key->objectid != key.objectid) |
| 1076 | return 1; |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * this makes the path point to (inum INODE_ITEM ioff) |
| 1083 | */ |
| 1084 | int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, |
| 1085 | struct btrfs_path *path) |
| 1086 | { |
| 1087 | struct btrfs_key key; |
| 1088 | return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path, |
| 1089 | &key); |
| 1090 | } |
| 1091 | |
| 1092 | static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, |
| 1093 | struct btrfs_path *path, |
| 1094 | struct btrfs_key *found_key) |
| 1095 | { |
| 1096 | return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path, |
| 1097 | found_key); |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements |
| 1102 | * of the path are separated by '/' and the path is guaranteed to be |
| 1103 | * 0-terminated. the path is only given within the current file system. |
| 1104 | * Therefore, it never starts with a '/'. the caller is responsible to provide |
| 1105 | * "size" bytes in "dest". the dest buffer will be filled backwards. finally, |
| 1106 | * the start point of the resulting string is returned. this pointer is within |
| 1107 | * dest, normally. |
| 1108 | * in case the path buffer would overflow, the pointer is decremented further |
| 1109 | * as if output was written to the buffer, though no more output is actually |
| 1110 | * generated. that way, the caller can determine how much space would be |
| 1111 | * required for the path to fit into the buffer. in that case, the returned |
| 1112 | * value will be smaller than dest. callers must check this! |
| 1113 | */ |
| 1114 | static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, |
| 1115 | struct btrfs_inode_ref *iref, |
| 1116 | struct extent_buffer *eb_in, u64 parent, |
| 1117 | char *dest, u32 size) |
| 1118 | { |
| 1119 | u32 len; |
| 1120 | int slot; |
| 1121 | u64 next_inum; |
| 1122 | int ret; |
| 1123 | s64 bytes_left = size - 1; |
| 1124 | struct extent_buffer *eb = eb_in; |
| 1125 | struct btrfs_key found_key; |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1126 | int leave_spinning = path->leave_spinning; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1127 | |
| 1128 | if (bytes_left >= 0) |
| 1129 | dest[bytes_left] = '\0'; |
| 1130 | |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1131 | path->leave_spinning = 1; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1132 | while (1) { |
| 1133 | len = btrfs_inode_ref_name_len(eb, iref); |
| 1134 | bytes_left -= len; |
| 1135 | if (bytes_left >= 0) |
| 1136 | read_extent_buffer(eb, dest + bytes_left, |
| 1137 | (unsigned long)(iref + 1), len); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1138 | if (eb != eb_in) { |
| 1139 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1140 | free_extent_buffer(eb); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1141 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1142 | ret = inode_ref_info(parent, 0, fs_root, path, &found_key); |
Jan Schmidt | 8f24b49 | 2012-02-08 16:01:01 +0100 | [diff] [blame] | 1143 | if (ret > 0) |
| 1144 | ret = -ENOENT; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1145 | if (ret) |
| 1146 | break; |
| 1147 | next_inum = found_key.offset; |
| 1148 | |
| 1149 | /* regular exit ahead */ |
| 1150 | if (parent == next_inum) |
| 1151 | break; |
| 1152 | |
| 1153 | slot = path->slots[0]; |
| 1154 | eb = path->nodes[0]; |
| 1155 | /* make sure we can use eb after releasing the path */ |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1156 | if (eb != eb_in) { |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1157 | atomic_inc(&eb->refs); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1158 | btrfs_tree_read_lock(eb); |
| 1159 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1160 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1161 | btrfs_release_path(path); |
| 1162 | |
| 1163 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
| 1164 | parent = next_inum; |
| 1165 | --bytes_left; |
| 1166 | if (bytes_left >= 0) |
| 1167 | dest[bytes_left] = '/'; |
| 1168 | } |
| 1169 | |
| 1170 | btrfs_release_path(path); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1171 | path->leave_spinning = leave_spinning; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1172 | |
| 1173 | if (ret) |
| 1174 | return ERR_PTR(ret); |
| 1175 | |
| 1176 | return dest + bytes_left; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * this makes the path point to (logical EXTENT_ITEM *) |
| 1181 | * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for |
| 1182 | * tree blocks and <0 on error. |
| 1183 | */ |
| 1184 | int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, |
| 1185 | struct btrfs_path *path, struct btrfs_key *found_key) |
| 1186 | { |
| 1187 | int ret; |
| 1188 | u64 flags; |
| 1189 | u32 item_size; |
| 1190 | struct extent_buffer *eb; |
| 1191 | struct btrfs_extent_item *ei; |
| 1192 | struct btrfs_key key; |
| 1193 | |
| 1194 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1195 | key.objectid = logical; |
| 1196 | key.offset = (u64)-1; |
| 1197 | |
| 1198 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
| 1199 | if (ret < 0) |
| 1200 | return ret; |
| 1201 | ret = btrfs_previous_item(fs_info->extent_root, path, |
| 1202 | 0, BTRFS_EXTENT_ITEM_KEY); |
| 1203 | if (ret < 0) |
| 1204 | return ret; |
| 1205 | |
| 1206 | btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); |
| 1207 | if (found_key->type != BTRFS_EXTENT_ITEM_KEY || |
| 1208 | found_key->objectid > logical || |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1209 | found_key->objectid + found_key->offset <= logical) { |
| 1210 | pr_debug("logical %llu is not within any extent\n", |
| 1211 | (unsigned long long)logical); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1212 | return -ENOENT; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1213 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1214 | |
| 1215 | eb = path->nodes[0]; |
| 1216 | item_size = btrfs_item_size_nr(eb, path->slots[0]); |
| 1217 | BUG_ON(item_size < sizeof(*ei)); |
| 1218 | |
| 1219 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); |
| 1220 | flags = btrfs_extent_flags(eb, ei); |
| 1221 | |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1222 | pr_debug("logical %llu is at position %llu within the extent (%llu " |
| 1223 | "EXTENT_ITEM %llu) flags %#llx size %u\n", |
| 1224 | (unsigned long long)logical, |
| 1225 | (unsigned long long)(logical - found_key->objectid), |
| 1226 | (unsigned long long)found_key->objectid, |
| 1227 | (unsigned long long)found_key->offset, |
| 1228 | (unsigned long long)flags, item_size); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1229 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
| 1230 | return BTRFS_EXTENT_FLAG_TREE_BLOCK; |
| 1231 | if (flags & BTRFS_EXTENT_FLAG_DATA) |
| 1232 | return BTRFS_EXTENT_FLAG_DATA; |
| 1233 | |
| 1234 | return -EIO; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * helper function to iterate extent inline refs. ptr must point to a 0 value |
| 1239 | * for the first call and may be modified. it is used to track state. |
| 1240 | * if more refs exist, 0 is returned and the next call to |
| 1241 | * __get_extent_inline_ref must pass the modified ptr parameter to get the |
| 1242 | * next ref. after the last ref was processed, 1 is returned. |
| 1243 | * returns <0 on error |
| 1244 | */ |
| 1245 | static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb, |
| 1246 | struct btrfs_extent_item *ei, u32 item_size, |
| 1247 | struct btrfs_extent_inline_ref **out_eiref, |
| 1248 | int *out_type) |
| 1249 | { |
| 1250 | unsigned long end; |
| 1251 | u64 flags; |
| 1252 | struct btrfs_tree_block_info *info; |
| 1253 | |
| 1254 | if (!*ptr) { |
| 1255 | /* first call */ |
| 1256 | flags = btrfs_extent_flags(eb, ei); |
| 1257 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1258 | info = (struct btrfs_tree_block_info *)(ei + 1); |
| 1259 | *out_eiref = |
| 1260 | (struct btrfs_extent_inline_ref *)(info + 1); |
| 1261 | } else { |
| 1262 | *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 1263 | } |
| 1264 | *ptr = (unsigned long)*out_eiref; |
| 1265 | if ((void *)*ptr >= (void *)ei + item_size) |
| 1266 | return -ENOENT; |
| 1267 | } |
| 1268 | |
| 1269 | end = (unsigned long)ei + item_size; |
| 1270 | *out_eiref = (struct btrfs_extent_inline_ref *)*ptr; |
| 1271 | *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); |
| 1272 | |
| 1273 | *ptr += btrfs_extent_inline_ref_size(*out_type); |
| 1274 | WARN_ON(*ptr > end); |
| 1275 | if (*ptr == end) |
| 1276 | return 1; /* last */ |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * reads the tree block backref for an extent. tree level and root are returned |
| 1283 | * through out_level and out_root. ptr must point to a 0 value for the first |
| 1284 | * call and may be modified (see __get_extent_inline_ref comment). |
| 1285 | * returns 0 if data was provided, 1 if there was no more data to provide or |
| 1286 | * <0 on error. |
| 1287 | */ |
| 1288 | int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, |
| 1289 | struct btrfs_extent_item *ei, u32 item_size, |
| 1290 | u64 *out_root, u8 *out_level) |
| 1291 | { |
| 1292 | int ret; |
| 1293 | int type; |
| 1294 | struct btrfs_tree_block_info *info; |
| 1295 | struct btrfs_extent_inline_ref *eiref; |
| 1296 | |
| 1297 | if (*ptr == (unsigned long)-1) |
| 1298 | return 1; |
| 1299 | |
| 1300 | while (1) { |
| 1301 | ret = __get_extent_inline_ref(ptr, eb, ei, item_size, |
| 1302 | &eiref, &type); |
| 1303 | if (ret < 0) |
| 1304 | return ret; |
| 1305 | |
| 1306 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1307 | type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1308 | break; |
| 1309 | |
| 1310 | if (ret == 1) |
| 1311 | return 1; |
| 1312 | } |
| 1313 | |
| 1314 | /* we can treat both ref types equally here */ |
| 1315 | info = (struct btrfs_tree_block_info *)(ei + 1); |
| 1316 | *out_root = btrfs_extent_inline_ref_offset(eb, eiref); |
| 1317 | *out_level = btrfs_tree_block_level(eb, info); |
| 1318 | |
| 1319 | if (ret == 1) |
| 1320 | *ptr = (unsigned long)-1; |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1325 | static int iterate_leaf_refs(struct extent_inode_elem *inode_list, |
| 1326 | u64 root, u64 extent_item_objectid, |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1327 | iterate_extent_inodes_t *iterate, void *ctx) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1328 | { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1329 | struct extent_inode_elem *eie; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1330 | int ret = 0; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1331 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1332 | for (eie = inode_list; eie; eie = eie->next) { |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1333 | pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), " |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1334 | "root %llu\n", extent_item_objectid, |
| 1335 | eie->inum, eie->offset, root); |
| 1336 | ret = iterate(eie->inum, eie->offset, root, ctx); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1337 | if (ret) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1338 | pr_debug("stopping iteration for %llu due to ret=%d\n", |
| 1339 | extent_item_objectid, ret); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1340 | break; |
| 1341 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1344 | return ret; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * calls iterate() for every inode that references the extent identified by |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1349 | * the given parameters. |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1350 | * when the iterator function returns a non-zero value, iteration stops. |
| 1351 | */ |
| 1352 | int iterate_extent_inodes(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1353 | u64 extent_item_objectid, u64 extent_item_pos, |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1354 | int search_commit_root, |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1355 | iterate_extent_inodes_t *iterate, void *ctx) |
| 1356 | { |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1357 | int ret; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1358 | struct list_head data_refs = LIST_HEAD_INIT(data_refs); |
| 1359 | struct list_head shared_refs = LIST_HEAD_INIT(shared_refs); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1360 | struct btrfs_trans_handle *trans; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1361 | struct ulist *refs = NULL; |
| 1362 | struct ulist *roots = NULL; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1363 | struct ulist_node *ref_node = NULL; |
| 1364 | struct ulist_node *root_node = NULL; |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1365 | struct seq_list seq_elem = {}; |
| 1366 | struct seq_list tree_mod_seq_elem = {}; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1367 | struct ulist_iterator ref_uiter; |
| 1368 | struct ulist_iterator root_uiter; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1369 | struct btrfs_delayed_ref_root *delayed_refs = NULL; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1370 | |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1371 | pr_debug("resolving all inodes for extent %llu\n", |
| 1372 | extent_item_objectid); |
| 1373 | |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1374 | if (search_commit_root) { |
| 1375 | trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT; |
| 1376 | } else { |
| 1377 | trans = btrfs_join_transaction(fs_info->extent_root); |
| 1378 | if (IS_ERR(trans)) |
| 1379 | return PTR_ERR(trans); |
| 1380 | |
| 1381 | delayed_refs = &trans->transaction->delayed_refs; |
| 1382 | spin_lock(&delayed_refs->lock); |
| 1383 | btrfs_get_delayed_seq(delayed_refs, &seq_elem); |
| 1384 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1385 | btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1386 | } |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1387 | |
| 1388 | ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1389 | seq_elem.seq, tree_mod_seq_elem.seq, &refs, |
| 1390 | &extent_item_pos); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1391 | if (ret) |
| 1392 | goto out; |
| 1393 | |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1394 | ULIST_ITER_INIT(&ref_uiter); |
| 1395 | while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1396 | ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1397 | seq_elem.seq, |
| 1398 | tree_mod_seq_elem.seq, &roots); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1399 | if (ret) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1400 | break; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1401 | ULIST_ITER_INIT(&root_uiter); |
| 1402 | while (!ret && (root_node = ulist_next(roots, &root_uiter))) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1403 | pr_debug("root %llu references leaf %llu, data list " |
| 1404 | "%#lx\n", root_node->val, ref_node->val, |
| 1405 | ref_node->aux); |
| 1406 | ret = iterate_leaf_refs( |
| 1407 | (struct extent_inode_elem *)ref_node->aux, |
| 1408 | root_node->val, extent_item_objectid, |
| 1409 | iterate, ctx); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1410 | } |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1411 | ulist_free(roots); |
| 1412 | roots = NULL; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1415 | free_leaf_list(refs); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1416 | ulist_free(roots); |
| 1417 | out: |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1418 | if (!search_commit_root) { |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1419 | btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1420 | btrfs_put_delayed_seq(delayed_refs, &seq_elem); |
| 1421 | btrfs_end_transaction(trans, fs_info->extent_root); |
| 1422 | } |
| 1423 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, |
| 1428 | struct btrfs_path *path, |
| 1429 | iterate_extent_inodes_t *iterate, void *ctx) |
| 1430 | { |
| 1431 | int ret; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1432 | u64 extent_item_pos; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1433 | struct btrfs_key found_key; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1434 | int search_commit_root = path->search_commit_root; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1435 | |
| 1436 | ret = extent_from_logical(fs_info, logical, path, |
| 1437 | &found_key); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1438 | btrfs_release_path(path); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1439 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
| 1440 | ret = -EINVAL; |
| 1441 | if (ret < 0) |
| 1442 | return ret; |
| 1443 | |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1444 | extent_item_pos = logical - found_key.objectid; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1445 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
| 1446 | extent_item_pos, search_commit_root, |
| 1447 | iterate, ctx); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1448 | |
| 1449 | return ret; |
| 1450 | } |
| 1451 | |
| 1452 | static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, |
| 1453 | struct btrfs_path *path, |
| 1454 | iterate_irefs_t *iterate, void *ctx) |
| 1455 | { |
Jan Schmidt | aefc1eb | 2012-04-13 12:28:00 +0200 | [diff] [blame] | 1456 | int ret = 0; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1457 | int slot; |
| 1458 | u32 cur; |
| 1459 | u32 len; |
| 1460 | u32 name_len; |
| 1461 | u64 parent = 0; |
| 1462 | int found = 0; |
| 1463 | struct extent_buffer *eb; |
| 1464 | struct btrfs_item *item; |
| 1465 | struct btrfs_inode_ref *iref; |
| 1466 | struct btrfs_key found_key; |
| 1467 | |
Jan Schmidt | aefc1eb | 2012-04-13 12:28:00 +0200 | [diff] [blame] | 1468 | while (!ret) { |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1469 | path->leave_spinning = 1; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1470 | ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path, |
| 1471 | &found_key); |
| 1472 | if (ret < 0) |
| 1473 | break; |
| 1474 | if (ret) { |
| 1475 | ret = found ? 0 : -ENOENT; |
| 1476 | break; |
| 1477 | } |
| 1478 | ++found; |
| 1479 | |
| 1480 | parent = found_key.offset; |
| 1481 | slot = path->slots[0]; |
| 1482 | eb = path->nodes[0]; |
| 1483 | /* make sure we can use eb after releasing the path */ |
| 1484 | atomic_inc(&eb->refs); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1485 | btrfs_tree_read_lock(eb); |
| 1486 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1487 | btrfs_release_path(path); |
| 1488 | |
| 1489 | item = btrfs_item_nr(eb, slot); |
| 1490 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
| 1491 | |
| 1492 | for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { |
| 1493 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 1494 | /* path must be released before calling iterate()! */ |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1495 | pr_debug("following ref at offset %u for inode %llu in " |
| 1496 | "tree %llu\n", cur, |
| 1497 | (unsigned long long)found_key.objectid, |
| 1498 | (unsigned long long)fs_root->objectid); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1499 | ret = iterate(parent, iref, eb, ctx); |
Jan Schmidt | aefc1eb | 2012-04-13 12:28:00 +0200 | [diff] [blame] | 1500 | if (ret) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1501 | break; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1502 | len = sizeof(*iref) + name_len; |
| 1503 | iref = (struct btrfs_inode_ref *)((char *)iref + len); |
| 1504 | } |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1505 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1506 | free_extent_buffer(eb); |
| 1507 | } |
| 1508 | |
| 1509 | btrfs_release_path(path); |
| 1510 | |
| 1511 | return ret; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * returns 0 if the path could be dumped (probably truncated) |
| 1516 | * returns <0 in case of an error |
| 1517 | */ |
| 1518 | static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref, |
| 1519 | struct extent_buffer *eb, void *ctx) |
| 1520 | { |
| 1521 | struct inode_fs_paths *ipath = ctx; |
| 1522 | char *fspath; |
| 1523 | char *fspath_min; |
| 1524 | int i = ipath->fspath->elem_cnt; |
| 1525 | const int s_ptr = sizeof(char *); |
| 1526 | u32 bytes_left; |
| 1527 | |
| 1528 | bytes_left = ipath->fspath->bytes_left > s_ptr ? |
| 1529 | ipath->fspath->bytes_left - s_ptr : 0; |
| 1530 | |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 1531 | fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1532 | fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb, |
| 1533 | inum, fspath_min, bytes_left); |
| 1534 | if (IS_ERR(fspath)) |
| 1535 | return PTR_ERR(fspath); |
| 1536 | |
| 1537 | if (fspath > fspath_min) { |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1538 | pr_debug("path resolved: %s\n", fspath); |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 1539 | ipath->fspath->val[i] = (u64)(unsigned long)fspath; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1540 | ++ipath->fspath->elem_cnt; |
| 1541 | ipath->fspath->bytes_left = fspath - fspath_min; |
| 1542 | } else { |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1543 | pr_debug("missed path, not enough space. missing bytes: %lu, " |
| 1544 | "constructed so far: %s\n", |
| 1545 | (unsigned long)(fspath_min - fspath), fspath_min); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1546 | ++ipath->fspath->elem_missed; |
| 1547 | ipath->fspath->bytes_missing += fspath_min - fspath; |
| 1548 | ipath->fspath->bytes_left = 0; |
| 1549 | } |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * this dumps all file system paths to the inode into the ipath struct, provided |
| 1556 | * is has been created large enough. each path is zero-terminated and accessed |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 1557 | * from ipath->fspath->val[i]. |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1558 | * when it returns, there are ipath->fspath->elem_cnt number of paths available |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 1559 | * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1560 | * number of missed paths in recored in ipath->fspath->elem_missed, otherwise, |
| 1561 | * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would |
| 1562 | * have been needed to return all paths. |
| 1563 | */ |
| 1564 | int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) |
| 1565 | { |
| 1566 | return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, |
| 1567 | inode_to_path, ipath); |
| 1568 | } |
| 1569 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1570 | struct btrfs_data_container *init_data_container(u32 total_bytes) |
| 1571 | { |
| 1572 | struct btrfs_data_container *data; |
| 1573 | size_t alloc_bytes; |
| 1574 | |
| 1575 | alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); |
| 1576 | data = kmalloc(alloc_bytes, GFP_NOFS); |
| 1577 | if (!data) |
| 1578 | return ERR_PTR(-ENOMEM); |
| 1579 | |
| 1580 | if (total_bytes >= sizeof(*data)) { |
| 1581 | data->bytes_left = total_bytes - sizeof(*data); |
| 1582 | data->bytes_missing = 0; |
| 1583 | } else { |
| 1584 | data->bytes_missing = sizeof(*data) - total_bytes; |
| 1585 | data->bytes_left = 0; |
| 1586 | } |
| 1587 | |
| 1588 | data->elem_cnt = 0; |
| 1589 | data->elem_missed = 0; |
| 1590 | |
| 1591 | return data; |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * allocates space to return multiple file system paths for an inode. |
| 1596 | * total_bytes to allocate are passed, note that space usable for actual path |
| 1597 | * information will be total_bytes - sizeof(struct inode_fs_paths). |
| 1598 | * the returned pointer must be freed with free_ipath() in the end. |
| 1599 | */ |
| 1600 | struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, |
| 1601 | struct btrfs_path *path) |
| 1602 | { |
| 1603 | struct inode_fs_paths *ifp; |
| 1604 | struct btrfs_data_container *fspath; |
| 1605 | |
| 1606 | fspath = init_data_container(total_bytes); |
| 1607 | if (IS_ERR(fspath)) |
| 1608 | return (void *)fspath; |
| 1609 | |
| 1610 | ifp = kmalloc(sizeof(*ifp), GFP_NOFS); |
| 1611 | if (!ifp) { |
| 1612 | kfree(fspath); |
| 1613 | return ERR_PTR(-ENOMEM); |
| 1614 | } |
| 1615 | |
| 1616 | ifp->btrfs_path = path; |
| 1617 | ifp->fspath = fspath; |
| 1618 | ifp->fs_root = fs_root; |
| 1619 | |
| 1620 | return ifp; |
| 1621 | } |
| 1622 | |
| 1623 | void free_ipath(struct inode_fs_paths *ipath) |
| 1624 | { |
Jesper Juhl | 4735fb2 | 2012-04-12 22:47:52 +0200 | [diff] [blame] | 1625 | if (!ipath) |
| 1626 | return; |
Ilya Dryomov | 5eb56d2 | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 1627 | kfree(ipath->fspath); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1628 | kfree(ipath); |
| 1629 | } |