extent-tree.c 155 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Chris Mason's avatar
Chris Mason committed
2
3
4
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 */
5

Zach Brown's avatar
Zach Brown committed
6
#include <linux/sched.h>
7
#include <linux/sched/signal.h>
8
#include <linux/pagemap.h>
9
#include <linux/writeback.h>
10
#include <linux/blkdev.h>
11
#include <linux/sort.h>
12
#include <linux/rcupdate.h>
Josef Bacik's avatar
Josef Bacik committed
13
#include <linux/kthread.h>
14
#include <linux/slab.h>
15
#include <linux/ratelimit.h>
16
#include <linux/percpu_counter.h>
17
#include <linux/lockdep.h>
18
#include <linux/crc32c.h>
19
#include "misc.h"
20
#include "tree-log.h"
21
22
#include "disk-io.h"
#include "print-tree.h"
23
#include "volumes.h"
David Woodhouse's avatar
David Woodhouse committed
24
#include "raid56.h"
25
#include "locking.h"
26
#include "free-space-cache.h"
27
#include "free-space-tree.h"
28
#include "sysfs.h"
Josef Bacik's avatar
Josef Bacik committed
29
#include "qgroup.h"
30
#include "ref-verify.h"
31
#include "space-info.h"
32
#include "block-rsv.h"
33
#include "delalloc-space.h"
34
#include "block-group.h"
35

36
37
#undef SCRAMBLE_DELAYED_REFS

38

39
static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
40
41
42
43
			       struct btrfs_delayed_ref_node *node, u64 parent,
			       u64 root_objectid, u64 owner_objectid,
			       u64 owner_offset, int refs_to_drop,
			       struct btrfs_delayed_extent_op *extra_op);
44
45
46
47
48
49
50
51
static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
				    struct extent_buffer *leaf,
				    struct btrfs_extent_item *ei);
static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
				      u64 parent, u64 root_objectid,
				      u64 flags, u64 owner, u64 offset,
				      struct btrfs_key *ins, int ref_mod);
static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
52
				     struct btrfs_delayed_ref_node *node,
53
				     struct btrfs_delayed_extent_op *extent_op);
54
55
static int find_next_key(struct btrfs_path *path, int level,
			 struct btrfs_key *key);
56

57
static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
58
59
60
61
{
	return (cache->flags & bits) == bits;
}

62
63
int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
			      u64 start, u64 num_bytes)
Josef Bacik's avatar
Josef Bacik committed
64
{
65
	u64 end = start + num_bytes - 1;
66
	set_extent_bits(&fs_info->freed_extents[0],
67
			start, end, EXTENT_UPTODATE);
68
	set_extent_bits(&fs_info->freed_extents[1],
69
			start, end, EXTENT_UPTODATE);
70
71
	return 0;
}
Josef Bacik's avatar
Josef Bacik committed
72

73
void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
74
{
75
	struct btrfs_fs_info *fs_info = cache->fs_info;
76
	u64 start, end;
Josef Bacik's avatar
Josef Bacik committed
77

78
79
	start = cache->start;
	end = start + cache->length - 1;
80

81
	clear_extent_bits(&fs_info->freed_extents[0],
82
			  start, end, EXTENT_UPTODATE);
83
	clear_extent_bits(&fs_info->freed_extents[1],
84
			  start, end, EXTENT_UPTODATE);
Josef Bacik's avatar
Josef Bacik committed
85
86
}

87
static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
88
{
89
90
	if (ref->type == BTRFS_REF_METADATA) {
		if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
91
			return BTRFS_BLOCK_GROUP_SYSTEM;
92
		else
93
			return BTRFS_BLOCK_GROUP_METADATA;
94
	}
95
96
97
98
99
100
101
102
103
	return BTRFS_BLOCK_GROUP_DATA;
}

static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
			     struct btrfs_ref *ref)
{
	struct btrfs_space_info *space_info;
	u64 flags = generic_ref_to_space_flags(ref);

104
	space_info = btrfs_find_space_info(fs_info, flags);
105
106
107
108
109
110
111
112
113
114
	ASSERT(space_info);
	percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
		    BTRFS_TOTAL_BYTES_PINNED_BATCH);
}

static void sub_pinned_bytes(struct btrfs_fs_info *fs_info,
			     struct btrfs_ref *ref)
{
	struct btrfs_space_info *space_info;
	u64 flags = generic_ref_to_space_flags(ref);
115

116
	space_info = btrfs_find_space_info(fs_info, flags);
117
	ASSERT(space_info);
118
	percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
119
		    BTRFS_TOTAL_BYTES_PINNED_BATCH);
120
121
}

122
/* simple helper to search for an existing data extent at a given offset */
123
int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
124
125
126
{
	int ret;
	struct btrfs_key key;
Zheng Yan's avatar
Zheng Yan committed
127
	struct btrfs_path *path;
128

Zheng Yan's avatar
Zheng Yan committed
129
	path = btrfs_alloc_path();
130
131
132
	if (!path)
		return -ENOMEM;

133
134
	key.objectid = start;
	key.offset = len;
135
	key.type = BTRFS_EXTENT_ITEM_KEY;
136
	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan's avatar
Zheng Yan committed
137
	btrfs_free_path(path);
138
139
140
	return ret;
}

141
/*
142
 * helper function to lookup reference count and flags of a tree block.
143
144
145
146
147
148
149
150
 *
 * the head node for delayed ref is used to store the sum of all the
 * reference count modifications queued up in the rbtree. the head
 * node may also store the extent flags to set. This way you can check
 * to see what the reference count and extent flags would be if all of
 * the delayed refs are not processed.
 */
int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
151
			     struct btrfs_fs_info *fs_info, u64 bytenr,
152
			     u64 offset, int metadata, u64 *refs, u64 *flags)
153
154
155
156
157
158
159
160
161
162
163
164
{
	struct btrfs_delayed_ref_head *head;
	struct btrfs_delayed_ref_root *delayed_refs;
	struct btrfs_path *path;
	struct btrfs_extent_item *ei;
	struct extent_buffer *leaf;
	struct btrfs_key key;
	u32 item_size;
	u64 num_refs;
	u64 extent_flags;
	int ret;

165
166
167
168
	/*
	 * If we don't have skinny metadata, don't bother doing anything
	 * different
	 */
169
170
	if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
		offset = fs_info->nodesize;
171
172
173
		metadata = 0;
	}

174
175
176
177
178
179
180
181
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	if (!trans) {
		path->skip_locking = 1;
		path->search_commit_root = 1;
	}
182
183
184
185
186
187
188
189
190

search_again:
	key.objectid = bytenr;
	key.offset = offset;
	if (metadata)
		key.type = BTRFS_METADATA_ITEM_KEY;
	else
		key.type = BTRFS_EXTENT_ITEM_KEY;

191
	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
192
193
194
	if (ret < 0)
		goto out_free;

195
	if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
196
197
198
199
200
201
		if (path->slots[0]) {
			path->slots[0]--;
			btrfs_item_key_to_cpu(path->nodes[0], &key,
					      path->slots[0]);
			if (key.objectid == bytenr &&
			    key.type == BTRFS_EXTENT_ITEM_KEY &&
202
			    key.offset == fs_info->nodesize)
203
204
				ret = 0;
		}
205
206
	}

207
208
209
210
211
212
213
214
215
	if (ret == 0) {
		leaf = path->nodes[0];
		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
		if (item_size >= sizeof(*ei)) {
			ei = btrfs_item_ptr(leaf, path->slots[0],
					    struct btrfs_extent_item);
			num_refs = btrfs_extent_refs(leaf, ei);
			extent_flags = btrfs_extent_flags(leaf, ei);
		} else {
216
217
218
219
220
221
222
223
			ret = -EINVAL;
			btrfs_print_v0_err(fs_info);
			if (trans)
				btrfs_abort_transaction(trans, ret);
			else
				btrfs_handle_fs_error(fs_info, ret, NULL);

			goto out_free;
224
		}
225

226
227
228
229
230
231
232
233
234
235
236
237
		BUG_ON(num_refs == 0);
	} else {
		num_refs = 0;
		extent_flags = 0;
		ret = 0;
	}

	if (!trans)
		goto out;

	delayed_refs = &trans->transaction->delayed_refs;
	spin_lock(&delayed_refs->lock);
238
	head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
239
240
	if (head) {
		if (!mutex_trylock(&head->mutex)) {
241
			refcount_inc(&head->refs);
242
243
			spin_unlock(&delayed_refs->lock);

244
			btrfs_release_path(path);
245

246
247
248
249
			/*
			 * Mutex was contended, block until it's released and try
			 * again
			 */
250
251
			mutex_lock(&head->mutex);
			mutex_unlock(&head->mutex);
252
			btrfs_put_delayed_ref_head(head);
253
			goto search_again;
254
		}
255
		spin_lock(&head->lock);
256
257
258
259
260
		if (head->extent_op && head->extent_op->update_flags)
			extent_flags |= head->extent_op->flags_to_set;
		else
			BUG_ON(num_refs == 0);

261
		num_refs += head->ref_mod;
262
		spin_unlock(&head->lock);
263
264
265
266
267
268
269
270
271
272
273
274
275
276
		mutex_unlock(&head->mutex);
	}
	spin_unlock(&delayed_refs->lock);
out:
	WARN_ON(num_refs == 0);
	if (refs)
		*refs = num_refs;
	if (flags)
		*flags = extent_flags;
out_free:
	btrfs_free_path(path);
	return ret;
}

277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Back reference rules.  Back refs have three main goals:
 *
 * 1) differentiate between all holders of references to an extent so that
 *    when a reference is dropped we can make sure it was a valid reference
 *    before freeing the extent.
 *
 * 2) Provide enough information to quickly find the holders of an extent
 *    if we notice a given block is corrupted or bad.
 *
 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
 *    maintenance.  This is actually the same as #2, but with a slightly
 *    different use case.
 *
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
 * There are two kinds of back refs. The implicit back refs is optimized
 * for pointers in non-shared tree blocks. For a given pointer in a block,
 * back refs of this kind provide information about the block's owner tree
 * and the pointer's key. These information allow us to find the block by
 * b-tree searching. The full back refs is for pointers in tree blocks not
 * referenced by their owner trees. The location of tree block is recorded
 * in the back refs. Actually the full back refs is generic, and can be
 * used in all cases the implicit back refs is used. The major shortcoming
 * of the full back refs is its overhead. Every time a tree block gets
 * COWed, we have to update back refs entry for all pointers in it.
 *
 * For a newly allocated tree block, we use implicit back refs for
 * pointers in it. This means most tree related operations only involve
 * implicit back refs. For a tree block created in old transaction, the
 * only way to drop a reference to it is COW it. So we can detect the
 * event that tree block loses its owner tree's reference and do the
 * back refs conversion.
 *
309
 * When a tree block is COWed through a tree, there are four cases:
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
 *
 * The reference count of the block is one and the tree is the block's
 * owner tree. Nothing to do in this case.
 *
 * The reference count of the block is one and the tree is not the
 * block's owner tree. In this case, full back refs is used for pointers
 * in the block. Remove these full back refs, add implicit back refs for
 * every pointers in the new block.
 *
 * The reference count of the block is greater than one and the tree is
 * the block's owner tree. In this case, implicit back refs is used for
 * pointers in the block. Add full back refs for every pointers in the
 * block, increase lower level extents' reference counts. The original
 * implicit back refs are entailed to the new block.
 *
 * The reference count of the block is greater than one and the tree is
 * not the block's owner tree. Add implicit back refs for every pointer in
 * the new block, increase lower level extents' reference count.
 *
 * Back Reference Key composing:
 *
 * The key objectid corresponds to the first byte in the extent,
 * The key type is used to differentiate between types of back refs.
 * There are different meanings of the key offset for different types
 * of back refs.
 *
336
337
338
 * File extents can be referenced by:
 *
 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan's avatar
Zheng Yan committed
339
 * - different files inside a single subvolume
340
341
 * - different offsets inside a file (bookend extents in file.c)
 *
342
 * The extent ref structure for the implicit back refs has fields for:
343
344
345
 *
 * - Objectid of the subvolume root
 * - objectid of the file holding the reference
346
347
 * - original offset in the file
 * - how many bookend extents
348
 *
349
350
 * The key offset for the implicit back refs is hash of the first
 * three fields.
351
 *
352
 * The extent ref structure for the full back refs has field for:
353
 *
354
 * - number of pointers in the tree leaf
355
 *
356
357
 * The key offset for the implicit back refs is the first byte of
 * the tree leaf
358
 *
359
360
 * When a file extent is allocated, The implicit back refs is used.
 * the fields are filled in:
361
 *
362
 *     (root_key.objectid, inode objectid, offset in file, 1)
363
 *
364
365
 * When a file extent is removed file truncation, we find the
 * corresponding implicit back refs and check the following fields:
366
 *
367
 *     (btrfs_header_owner(leaf), inode objectid, offset in file)
368
 *
369
 * Btree extents can be referenced by:
370
 *
371
 * - Different subvolumes
372
 *
373
374
375
376
 * Both the implicit back refs and the full back refs for tree blocks
 * only consist of key. The key offset for the implicit back refs is
 * objectid of block's owner tree. The key offset for the full back refs
 * is the first byte of parent block.
377
 *
378
379
380
 * When implicit back refs is used, information about the lowest key and
 * level of the tree block are required. These information are stored in
 * tree block info structure.
381
 */
Zheng Yan's avatar
Zheng Yan committed
382

383
384
/*
 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
385
 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
386
387
388
389
390
391
392
 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
 */
int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
				     struct btrfs_extent_inline_ref *iref,
				     enum btrfs_inline_ref_type is_data)
{
	int type = btrfs_extent_inline_ref_type(eb, iref);
393
	u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
394
395
396
397
398
399

	if (type == BTRFS_TREE_BLOCK_REF_KEY ||
	    type == BTRFS_SHARED_BLOCK_REF_KEY ||
	    type == BTRFS_SHARED_DATA_REF_KEY ||
	    type == BTRFS_EXTENT_DATA_REF_KEY) {
		if (is_data == BTRFS_REF_TYPE_BLOCK) {
400
			if (type == BTRFS_TREE_BLOCK_REF_KEY)
401
				return type;
402
403
404
405
406
407
408
409
410
411
412
			if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
				ASSERT(eb->fs_info);
				/*
				 * Every shared one has parent tree
				 * block, which must be aligned to
				 * nodesize.
				 */
				if (offset &&
				    IS_ALIGNED(offset, eb->fs_info->nodesize))
					return type;
			}
413
		} else if (is_data == BTRFS_REF_TYPE_DATA) {
414
			if (type == BTRFS_EXTENT_DATA_REF_KEY)
415
				return type;
416
417
418
419
420
421
422
423
424
425
426
			if (type == BTRFS_SHARED_DATA_REF_KEY) {
				ASSERT(eb->fs_info);
				/*
				 * Every shared one has parent tree
				 * block, which must be aligned to
				 * nodesize.
				 */
				if (offset &&
				    IS_ALIGNED(offset, eb->fs_info->nodesize))
					return type;
			}
427
428
429
430
431
432
433
434
435
436
437
438
439
440
		} else {
			ASSERT(is_data == BTRFS_REF_TYPE_ANY);
			return type;
		}
	}

	btrfs_print_leaf((struct extent_buffer *)eb);
	btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
		  eb->start, type);
	WARN_ON(1);

	return BTRFS_REF_TYPE_INVALID;
}

441
u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
442
443
444
445
446
447
{
	u32 high_crc = ~(u32)0;
	u32 low_crc = ~(u32)0;
	__le64 lenum;

	lenum = cpu_to_le64(root_objectid);
448
	high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
449
	lenum = cpu_to_le64(owner);
450
	low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
451
	lenum = cpu_to_le64(offset);
452
	low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

	return ((u64)high_crc << 31) ^ (u64)low_crc;
}

static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
				     struct btrfs_extent_data_ref *ref)
{
	return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
				    btrfs_extent_data_ref_objectid(leaf, ref),
				    btrfs_extent_data_ref_offset(leaf, ref));
}

static int match_extent_data_ref(struct extent_buffer *leaf,
				 struct btrfs_extent_data_ref *ref,
				 u64 root_objectid, u64 owner, u64 offset)
{
	if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
	    btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
	    btrfs_extent_data_ref_offset(leaf, ref) != offset)
		return 0;
	return 1;
}

static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
					   struct btrfs_path *path,
					   u64 bytenr, u64 parent,
					   u64 root_objectid,
					   u64 owner, u64 offset)
{
482
	struct btrfs_root *root = trans->fs_info->extent_root;
483
484
	struct btrfs_key key;
	struct btrfs_extent_data_ref *ref;
Zheng Yan's avatar
Zheng Yan committed
485
	struct extent_buffer *leaf;
486
	u32 nritems;
487
	int ret;
488
489
	int recow;
	int err = -ENOENT;
490

Zheng Yan's avatar
Zheng Yan committed
491
	key.objectid = bytenr;
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
	if (parent) {
		key.type = BTRFS_SHARED_DATA_REF_KEY;
		key.offset = parent;
	} else {
		key.type = BTRFS_EXTENT_DATA_REF_KEY;
		key.offset = hash_extent_data_ref(root_objectid,
						  owner, offset);
	}
again:
	recow = 0;
	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
	if (ret < 0) {
		err = ret;
		goto fail;
	}
Zheng Yan's avatar
Zheng Yan committed
507

508
509
510
511
	if (parent) {
		if (!ret)
			return 0;
		goto fail;
Zheng Yan's avatar
Zheng Yan committed
512
513
514
	}

	leaf = path->nodes[0];
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	nritems = btrfs_header_nritems(leaf);
	while (1) {
		if (path->slots[0] >= nritems) {
			ret = btrfs_next_leaf(root, path);
			if (ret < 0)
				err = ret;
			if (ret)
				goto fail;

			leaf = path->nodes[0];
			nritems = btrfs_header_nritems(leaf);
			recow = 1;
		}

		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
		if (key.objectid != bytenr ||
		    key.type != BTRFS_EXTENT_DATA_REF_KEY)
			goto fail;

		ref = btrfs_item_ptr(leaf, path->slots[0],
				     struct btrfs_extent_data_ref);

		if (match_extent_data_ref(leaf, ref, root_objectid,
					  owner, offset)) {
			if (recow) {
540
				btrfs_release_path(path);
541
542
543
544
545
546
				goto again;
			}
			err = 0;
			break;
		}
		path->slots[0]++;
Zheng Yan's avatar
Zheng Yan committed
547
	}
548
549
fail:
	return err;
Zheng Yan's avatar
Zheng Yan committed
550
551
}

552
553
554
555
556
static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
					   struct btrfs_path *path,
					   u64 bytenr, u64 parent,
					   u64 root_objectid, u64 owner,
					   u64 offset, int refs_to_add)
Zheng Yan's avatar
Zheng Yan committed
557
{
558
	struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan's avatar
Zheng Yan committed
559
560
	struct btrfs_key key;
	struct extent_buffer *leaf;
561
	u32 size;
Zheng Yan's avatar
Zheng Yan committed
562
563
	u32 num_refs;
	int ret;
564
565

	key.objectid = bytenr;
566
567
568
569
570
571
572
573
574
575
	if (parent) {
		key.type = BTRFS_SHARED_DATA_REF_KEY;
		key.offset = parent;
		size = sizeof(struct btrfs_shared_data_ref);
	} else {
		key.type = BTRFS_EXTENT_DATA_REF_KEY;
		key.offset = hash_extent_data_ref(root_objectid,
						  owner, offset);
		size = sizeof(struct btrfs_extent_data_ref);
	}
576

577
578
579
580
581
582
583
	ret = btrfs_insert_empty_item(trans, root, path, &key, size);
	if (ret && ret != -EEXIST)
		goto fail;

	leaf = path->nodes[0];
	if (parent) {
		struct btrfs_shared_data_ref *ref;
Zheng Yan's avatar
Zheng Yan committed
584
		ref = btrfs_item_ptr(leaf, path->slots[0],
585
586
587
588
589
590
591
				     struct btrfs_shared_data_ref);
		if (ret == 0) {
			btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
		} else {
			num_refs = btrfs_shared_data_ref_count(leaf, ref);
			num_refs += refs_to_add;
			btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
Zheng Yan's avatar
Zheng Yan committed
592
		}
593
594
595
596
597
598
599
600
	} else {
		struct btrfs_extent_data_ref *ref;
		while (ret == -EEXIST) {
			ref = btrfs_item_ptr(leaf, path->slots[0],
					     struct btrfs_extent_data_ref);
			if (match_extent_data_ref(leaf, ref, root_objectid,
						  owner, offset))
				break;
601
			btrfs_release_path(path);
602
603
604
605
606
			key.offset++;
			ret = btrfs_insert_empty_item(trans, root, path, &key,
						      size);
			if (ret && ret != -EEXIST)
				goto fail;
Zheng Yan's avatar
Zheng Yan committed
607

608
609
610
611
612
613
614
615
616
617
618
619
620
621
			leaf = path->nodes[0];
		}
		ref = btrfs_item_ptr(leaf, path->slots[0],
				     struct btrfs_extent_data_ref);
		if (ret == 0) {
			btrfs_set_extent_data_ref_root(leaf, ref,
						       root_objectid);
			btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
			btrfs_set_extent_data_ref_offset(leaf, ref, offset);
			btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
		} else {
			num_refs = btrfs_extent_data_ref_count(leaf, ref);
			num_refs += refs_to_add;
			btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
Zheng Yan's avatar
Zheng Yan committed
622
623
		}
	}
624
625
626
	btrfs_mark_buffer_dirty(leaf);
	ret = 0;
fail:
627
	btrfs_release_path(path);
628
	return ret;
629
630
}

631
632
static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
					   struct btrfs_path *path,
Josef Bacik's avatar
Josef Bacik committed
633
					   int refs_to_drop, int *last_ref)
Zheng Yan's avatar
Zheng Yan committed
634
{
635
636
637
	struct btrfs_key key;
	struct btrfs_extent_data_ref *ref1 = NULL;
	struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan's avatar
Zheng Yan committed
638
	struct extent_buffer *leaf;
639
	u32 num_refs = 0;
Zheng Yan's avatar
Zheng Yan committed
640
641
642
	int ret = 0;

	leaf = path->nodes[0];
643
644
645
646
647
648
649
650
651
652
	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);

	if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
		ref1 = btrfs_item_ptr(leaf, path->slots[0],
				      struct btrfs_extent_data_ref);
		num_refs = btrfs_extent_data_ref_count(leaf, ref1);
	} else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
		ref2 = btrfs_item_ptr(leaf, path->slots[0],
				      struct btrfs_shared_data_ref);
		num_refs = btrfs_shared_data_ref_count(leaf, ref2);
653
	} else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
654
655
656
		btrfs_print_v0_err(trans->fs_info);
		btrfs_abort_transaction(trans, -EINVAL);
		return -EINVAL;
657
658
659
660
	} else {
		BUG();
	}

661
662
	BUG_ON(num_refs < refs_to_drop);
	num_refs -= refs_to_drop;
663

Zheng Yan's avatar
Zheng Yan committed
664
	if (num_refs == 0) {
665
		ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacik's avatar
Josef Bacik committed
666
		*last_ref = 1;
Zheng Yan's avatar
Zheng Yan committed
667
	} else {
668
669
670
671
		if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
			btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
		else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
			btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
Zheng Yan's avatar
Zheng Yan committed
672
673
674
675
676
		btrfs_mark_buffer_dirty(leaf);
	}
	return ret;
}

677
static noinline u32 extent_data_ref_count(struct btrfs_path *path,
678
					  struct btrfs_extent_inline_ref *iref)
679
{
680
681
682
683
684
	struct btrfs_key key;
	struct extent_buffer *leaf;
	struct btrfs_extent_data_ref *ref1;
	struct btrfs_shared_data_ref *ref2;
	u32 num_refs = 0;
685
	int type;
686
687
688

	leaf = path->nodes[0];
	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
689
690

	BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
691
	if (iref) {
692
693
694
695
696
697
698
		/*
		 * If type is invalid, we should have bailed out earlier than
		 * this call.
		 */
		type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
		ASSERT(type != BTRFS_REF_TYPE_INVALID);
		if (type == BTRFS_EXTENT_DATA_REF_KEY) {
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
			ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
			num_refs = btrfs_extent_data_ref_count(leaf, ref1);
		} else {
			ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
			num_refs = btrfs_shared_data_ref_count(leaf, ref2);
		}
	} else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
		ref1 = btrfs_item_ptr(leaf, path->slots[0],
				      struct btrfs_extent_data_ref);
		num_refs = btrfs_extent_data_ref_count(leaf, ref1);
	} else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
		ref2 = btrfs_item_ptr(leaf, path->slots[0],
				      struct btrfs_shared_data_ref);
		num_refs = btrfs_shared_data_ref_count(leaf, ref2);
	} else {
		WARN_ON(1);
	}
	return num_refs;
}
718

719
720
721
722
static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
					  struct btrfs_path *path,
					  u64 bytenr, u64 parent,
					  u64 root_objectid)
723
{
724
	struct btrfs_root *root = trans->fs_info->extent_root;
725
	struct btrfs_key key;
726
727
	int ret;

728
729
730
731
732
733
734
	key.objectid = bytenr;
	if (parent) {
		key.type = BTRFS_SHARED_BLOCK_REF_KEY;
		key.offset = parent;
	} else {
		key.type = BTRFS_TREE_BLOCK_REF_KEY;
		key.offset = root_objectid;
735
736
	}

737
738
739
740
	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
	if (ret > 0)
		ret = -ENOENT;
	return ret;
741
742
}

743
744
745
746
static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
					  struct btrfs_path *path,
					  u64 bytenr, u64 parent,
					  u64 root_objectid)
Zheng Yan's avatar
Zheng Yan committed
747
{
748
	struct btrfs_key key;
Zheng Yan's avatar
Zheng Yan committed
749
750
	int ret;

751
752
753
754
755
756
757
758
759
	key.objectid = bytenr;
	if (parent) {
		key.type = BTRFS_SHARED_BLOCK_REF_KEY;
		key.offset = parent;
	} else {
		key.type = BTRFS_TREE_BLOCK_REF_KEY;
		key.offset = root_objectid;
	}

760
	ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
761
				      path, &key, 0);
762
	btrfs_release_path(path);
Zheng Yan's avatar
Zheng Yan committed
763
764
765
	return ret;
}

766
static inline int extent_ref_type(u64 parent, u64 owner)
Zheng Yan's avatar
Zheng Yan committed
767
{
768
769
770
771
772
773
774
775
776
777
778
779
780
	int type;
	if (owner < BTRFS_FIRST_FREE_OBJECTID) {
		if (parent > 0)
			type = BTRFS_SHARED_BLOCK_REF_KEY;
		else
			type = BTRFS_TREE_BLOCK_REF_KEY;
	} else {
		if (parent > 0)
			type = BTRFS_SHARED_DATA_REF_KEY;
		else
			type = BTRFS_EXTENT_DATA_REF_KEY;
	}
	return type;
Zheng Yan's avatar
Zheng Yan committed
781
}
782

783
784
static int find_next_key(struct btrfs_path *path, int level,
			 struct btrfs_key *key)
785

Chris Mason's avatar
Chris Mason committed
786
{
787
	for (; level < BTRFS_MAX_LEVEL; level++) {
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
		if (!path->nodes[level])
			break;
		if (path->slots[level] + 1 >=
		    btrfs_header_nritems(path->nodes[level]))
			continue;
		if (level == 0)
			btrfs_item_key_to_cpu(path->nodes[level], key,
					      path->slots[level] + 1);
		else
			btrfs_node_key_to_cpu(path->nodes[level], key,
					      path->slots[level] + 1);
		return 0;
	}
	return 1;
}
Chris Mason's avatar
Chris Mason committed
803

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
 * look for inline back ref. if back ref is found, *ref_ret is set
 * to the address of inline back ref, and 0 is returned.
 *
 * if back ref isn't found, *ref_ret is set to the address where it
 * should be inserted, and -ENOENT is returned.
 *
 * if insert is true and there are too many inline back refs, the path
 * points to the extent item, and -EAGAIN is returned.
 *
 * NOTE: inline back refs are ordered in the same way that back ref
 *	 items in the tree are ordered.
 */
static noinline_for_stack
int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
				 struct btrfs_path *path,
				 struct btrfs_extent_inline_ref **ref_ret,
				 u64 bytenr, u64 num_bytes,
				 u64 parent, u64 root_objectid,
				 u64 owner, u64 offset, int insert)
{
825
	struct btrfs_fs_info *fs_info = trans->fs_info;
826
	struct btrfs_root *root = fs_info->extent_root;
827
828
829
830
831
832
833
834
835
836
837
838
839
	struct btrfs_key key;
	struct extent_buffer *leaf;
	struct btrfs_extent_item *ei;
	struct btrfs_extent_inline_ref *iref;
	u64 flags;
	u64 item_size;
	unsigned long ptr;
	unsigned long end;
	int extra_size;
	int type;
	int want;
	int ret;
	int err = 0;
840
	bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
841
	int needed;
842

843
	key.objectid = bytenr;
Zheng Yan's avatar
Zheng Yan committed
844
	key.type = BTRFS_EXTENT_ITEM_KEY;
845
	key.offset = num_bytes;
Zheng Yan's avatar
Zheng Yan committed
846

847
848
849
	want = extent_ref_type(parent, owner);
	if (insert) {
		extra_size = btrfs_extent_inline_ref_size(want);
850
		path->keep_locks = 1;
851
852
	} else
		extra_size = -1;
853
854

	/*
855
856
	 * Owner is our level, so we can just add one to get the level for the
	 * block we are interested in.
857
858
859
860
861
862
863
	 */
	if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
		key.type = BTRFS_METADATA_ITEM_KEY;
		key.offset = owner;
	}

again:
864
	ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
865
	if (ret < 0) {
866
867
868
		err = ret;
		goto out;
	}
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885

	/*
	 * We may be a newly converted file system which still has the old fat
	 * extent entries for metadata, so try and see if we have one of those.
	 */
	if (ret > 0 && skinny_metadata) {
		skinny_metadata = false;
		if (path->slots[0]) {
			path->slots[0]--;
			btrfs_item_key_to_cpu(path->nodes[0], &key,
					      path->slots[0]);
			if (key.objectid == bytenr &&
			    key.type == BTRFS_EXTENT_ITEM_KEY &&
			    key.offset == num_bytes)
				ret = 0;
		}
		if (ret) {
886
			key.objectid = bytenr;
887
888
889
890
891
892
893
			key.type = BTRFS_EXTENT_ITEM_KEY;
			key.offset = num_bytes;
			btrfs_release_path(path);
			goto again;
		}
	}

894
895
896
	if (ret && !insert) {
		err = -ENOENT;
		goto out;
897
	} else if (WARN_ON(ret)) {
898
899
		err = -EIO;
		goto out;
900
	}
901
902
903

	leaf = path->nodes[0];
	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
904
	if (unlikely(item_size < sizeof(*ei))) {
905
906
907
908
909
		err = -EINVAL;
		btrfs_print_v0_err(fs_info);
		btrfs_abort_transaction(trans, err);
		goto out;
	}
910
911
912
913
914
915
916

	ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
	flags = btrfs_extent_flags(leaf, ei);

	ptr = (unsigned long)(ei + 1);
	end = (unsigned long)ei + item_size;

917
	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
918
919
920
921
		ptr += sizeof(struct btrfs_tree_block_info);
		BUG_ON(ptr > end);
	}

922
923
924
925
926
	if (owner >= BTRFS_FIRST_FREE_OBJECTID)
		needed = BTRFS_REF_TYPE_DATA;
	else
		needed = BTRFS_REF_TYPE_BLOCK;

927
928
929
930
931
932
933
	err = -ENOENT;
	while (1) {
		if (ptr >= end) {
			WARN_ON(ptr > end);
			break;
		}
		iref = (struct btrfs_extent_inline_ref *)ptr;
934
935
		type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
		if (type == BTRFS_REF_TYPE_INVALID) {
936
			err = -EUCLEAN;
937
938
939
			goto out;
		}

940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
		if (want < type)
			break;
		if (want > type) {
			ptr += btrfs_extent_inline_ref_size(type);
			continue;
		}

		if (type == BTRFS_EXTENT_DATA_REF_KEY) {
			struct btrfs_extent_data_ref *dref;
			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
			if (match_extent_data_ref(leaf, dref, root_objectid,
						  owner, offset)) {
				err = 0;
				break;
			}
			if (hash_extent_data_ref_item(leaf, dref) <
			    hash_extent_data_ref(root_objectid, owner, offset))
				break;
		} else {
			u64 ref_offset;
			ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
			if (parent > 0) {
				if (parent == ref_offset) {
					err = 0;
					break;
				}
				if (ref_offset < parent)
					break;
			} else {
				if (root_objectid == ref_offset) {
					err = 0;
					break;
				}
				if (ref_offset < root_objectid)
					break;
			}
		}
		ptr += btrfs_extent_inline_ref_size(type);
	}
	if (err == -ENOENT && insert) {
		if (item_size + extra_size >=
		    BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
			err = -EAGAIN;
			goto out;
		}
		/*
		 * To add new inline back ref, we have to make sure
		 * there is no corresponding back ref item.
		 * For simplicity, we just do not add new inline back
		 * ref if there is any kind of item for this block
		 */
991
992
		if (find_next_key(path, 0, &key) == 0 &&
		    key.objectid == bytenr &&
993
		    key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
994
995
996
997
998
999
			err = -EAGAIN;
			goto out;
		}
	}
	*ref_ret = (struct btrfs_extent_inline_ref *)ptr;
out:
1000
	if (insert) {
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
		path->keep_locks = 0;
		btrfs_unlock_up_safe(path, 1);
	}
	return err;
}

/*
 * helper to add new inline back ref
 */
static noinline_for_stack
1011
void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
1012
1013
1014
1015
1016
				 struct btrfs_path *path,
				 struct btrfs_extent_inline_ref *iref,
				 u64 parent, u64 root_objectid,
				 u64 owner, u64 offset, int refs_to_add,
				 struct btrfs_delayed_extent_op *extent_op)
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
{
	struct extent_buffer *leaf;
	struct btrfs_extent_item *ei;
	unsigned long ptr;
	unsigned long end;
	unsigned long item_offset;
	u64 refs;
	int size;
	int type;

	leaf = path->nodes[0];
	ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
	item_offset = (unsigned long)iref - (unsigned long)ei;

	type = extent_ref_type(parent, owner);
	size = btrfs_extent_inline_ref_size(type);

1034
	btrfs_extend_item(path, size);
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078

	ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
	refs = btrfs_extent_refs(leaf, ei);
	refs += refs_to_add;
	btrfs_set_extent_refs(leaf, ei, refs);
	if (extent_op)
		__run_delayed_extent_op(extent_op, leaf, ei);

	ptr = (unsigned long)ei + item_offset;
	end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
	if (ptr < end - size)
		memmove_extent_buffer(leaf, ptr + size, ptr,
				      end - size - ptr);

	iref = (struct btrfs_extent_inline_ref *)ptr;
	btrfs_set_extent_inline_ref_type(leaf, iref, type);
	if (type == BTRFS_EXTENT_DATA_REF_KEY) {
		struct btrfs_extent_data_ref *dref;
		dref = (struct btrfs_extent_data_ref *)(&iref->offset);
		btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
		btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
		btrfs_set_extent_data_ref_offset(leaf, dref, offset);
		btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
	} else if (type == BTRFS_SHARED_DATA_REF_KEY) {
		struct btrfs_shared_data_ref *sref;
		sref = (struct btrfs_shared_data_ref *)(iref + 1);
		btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
		btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
	} else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
		btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
	} else {
		btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
	}
	btrfs_mark_buffer_dirty(leaf);
}

static int lookup_extent_backref(struct btrfs_trans_handle *trans,
				 struct btrfs_path *path,
				 struct btrfs_extent_inline_ref **ref_ret,
				 u64 bytenr, u64 num_bytes, u64 parent,
				 u64 root_objectid, u64 owner, u64 offset)
{
	int ret;

1079
1080
1081
	ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
					   num_bytes, parent, root_objectid,
					   owner, offset, 0);
1082
	if (ret != -ENOENT)
1083
		return ret;
1084

1085
	btrfs_release_path(path);
1086
1087
1088
	*ref_ret = NULL;

	if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1089
1090
		ret = lookup_tree_block_ref(trans, path, bytenr, parent,
					    root_objectid);
1091
	} else {
1092
1093
		ret = lookup_extent_data_ref(trans, path, bytenr, parent,
					     root_objectid, owner, offset);
1094
	}
1095
1096
	return ret;
}
Zheng Yan's avatar
Zheng Yan committed
1097

1098
1099
1100
1101
/*
 * helper to update/remove inline back ref
 */
static noinline_for_stack
1102
void update_inline_extent_backref(struct btrfs_path *path,
1103
1104
				  struct btrfs_extent_inline_ref *iref,
				  int refs_to_mod,
Josef Bacik's avatar
Josef Bacik committed
1105
1106
				  struct btrfs_delayed_extent_op *extent_op,
				  int *last_ref)
1107
{
1108
	struct extent_buffer *leaf = path->nodes[0];
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
	struct btrfs_extent_item *ei;
	struct btrfs_extent_data_ref *dref = NULL;
	struct btrfs_shared_data_ref *sref = NULL;
	unsigned long ptr;
	unsigned long end;
	u32 item_size;
	int size;
	int type;
	u64 refs;

	ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
	refs = btrfs_extent_refs(leaf, ei);
	WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
	refs += refs_to_mod;
	btrfs_set_extent_refs(leaf, ei, refs);
	if (extent_op)
		__run_delayed_extent_op(extent_op, leaf, ei);

1127
1128
1129
1130
1131
1132
	/*
	 * If type is invalid, we should have bailed out after
	 * lookup_inline_extent_backref().
	 */
	type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
	ASSERT(type != BTRFS_REF_TYPE_INVALID);
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142

	if (type == BTRFS_EXTENT_DATA_REF_KEY) {
		dref = (struct btrfs_extent_data_ref *)(&iref->offset);
		refs = btrfs_extent_data_ref_count(leaf, dref);
	} else if (type == BTRFS_SHARED_DATA_REF_KEY) {
		sref = (struct btrfs_shared_data_ref *)(iref + 1);
		refs = btrfs_shared_data_ref_count(leaf, sref);
	} else {
		refs = 1;
		BUG_ON(refs_to_mod != -1);
1143
	}
Zheng Yan's avatar
Zheng Yan committed
1144

1145
1146
1147
1148
1149
1150
1151
1152
1153
	BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
	refs += refs_to_mod;

	if (refs > 0) {
		if (type == BTRFS_EXTENT_DATA_REF_KEY)
			btrfs_set_extent_data_ref_count(leaf, dref, refs);
		else
			btrfs_set_shared_data_ref_count(leaf, sref, refs);
	} else {
Josef Bacik's avatar
Josef Bacik committed
1154
		*last_ref = 1;
1155
1156
1157
1158
1159
1160
1161
1162
		size =  btrfs_extent_inline_ref_size(type);
		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
		ptr = (unsigned long)iref;
		end = (unsigned long)ei + item_size;
		if (ptr + size < end)
			memmove_extent_buffer(leaf, ptr, ptr + size,
					      end - ptr - size);
		item_size -= size;
1163
		btrfs_truncate_item(path, item_size, 1);
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
	}
	btrfs_mark_buffer_dirty(leaf);
}

static noinline_for_stack
int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
				 struct btrfs_path *path,
				 u64 bytenr, u64 num_bytes, u64 parent,
				 u64 root_objectid, u64 owner,
				 u64 offset, int refs_to_add,
				 struct btrfs_delayed_extent_op *extent_op)
{
	struct btrfs_extent_inline_ref *iref;
	int ret;

1179
1180
1181
	ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
					   num_bytes, parent, root_objectid,
					   owner, offset, 1);
1182
1183
	if (ret == 0) {
		BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1184
1185
		update_inline_extent_backref(path, iref, refs_to_add,
					     extent_op, NULL);
1186
	} else if (ret == -ENOENT) {
1187
		setup_inline_extent_backref(trans->fs_info, path, iref, parent,
1188
1189
1190
					    root_objectid, owner, offset,
					    refs_to_add, extent_op);
		ret = 0;
1191
	}
1192
1193
	return ret;
}
Zheng Yan's avatar
Zheng Yan committed
1194

1195
1196
1197
1198
1199
1200
1201
1202
static int insert_extent_backref(struct btrfs_trans_handle *trans,
				 struct btrfs_path *path,
				 u64 bytenr, u64 parent, u64 root_objectid,
				 u64 owner, u64 offset, int refs_to_add)
{
	int ret;
	if (owner < BTRFS_FIRST_FREE_OBJECTID) {
		BUG_ON(refs_to_add != 1);
1203
1204
		ret = insert_tree_block_ref(trans, path, bytenr, parent,
					    root_objectid);
1205
	} else {
1206
1207
1208
		ret = insert_extent_data_ref(trans, path, bytenr, parent,
					     root_objectid