blob: e6ea01d7665958ec08382be1d2e78f1aaedac24d [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Zach Brownec6b9102007-07-11 10:00:37 -04006#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01007#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -05008#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -04009#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010010#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050011#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040012#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040013#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020015#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040016#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040017#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020018#include <linux/crc32c.h>
David Sterba602cbe92019-08-21 18:48:25 +020019#include "misc.h"
Miao Xie995946d2014-04-02 19:51:06 +080020#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050021#include "disk-io.h"
22#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040023#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050024#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040026#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070027#include "free-space-tree.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040028#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070029#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040030#include "ref-verify.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040031#include "space-info.h"
Josef Bacikd12ffdd2019-06-19 13:47:17 -040032#include "block-rsv.h"
Josef Bacik86736342019-06-19 15:12:00 -040033#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040034#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080035#include "discard.h"
Chris Masonfec577f2007-02-26 10:40:21 -050036
Arne Jansen709c0482011-09-12 12:22:57 +020037#undef SCRAMBLE_DELAYED_REFS
38
Qu Wenruo9f9b8e82018-10-24 20:24:01 +080039
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030041 struct btrfs_delayed_ref_node *node, u64 parent,
42 u64 root_objectid, u64 owner_objectid,
43 u64 owner_offset, int refs_to_drop,
44 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040045static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
46 struct extent_buffer *leaf,
47 struct btrfs_extent_item *ei);
48static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040049 u64 parent, u64 root_objectid,
50 u64 flags, u64 owner, u64 offset,
51 struct btrfs_key *ins, int ref_mod);
52static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030053 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030054 struct btrfs_delayed_extent_op *extent_op);
Yan Zheng11833d62009-09-11 16:11:19 -040055static int find_next_key(struct btrfs_path *path, int level,
56 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050057
David Sterba32da53862019-10-29 19:20:18 +010058static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
Josef Bacik0f9dd462008-09-23 13:14:11 -040059{
60 return (cache->flags & bits) == bits;
61}
62
Josef Bacik6f410d12019-06-20 15:37:49 -040063int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
64 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -040065{
Yan Zheng11833d62009-09-11 16:11:19 -040066 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040067 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +020068 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040069 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +020070 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040071 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -040072}
73
David Sterba32da53862019-10-29 19:20:18 +010074void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -040075{
Nikolay Borisov9e715da2018-06-20 15:49:08 +030076 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -040077 u64 start, end;
78
David Sterbab3470b52019-10-23 18:48:22 +020079 start = cache->start;
80 end = start + cache->length - 1;
Yan Zheng11833d62009-09-11 16:11:19 -040081
Jeff Mahoney0b246af2016-06-22 18:54:23 -040082 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +020083 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040084 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +020085 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040086}
87
Qu Wenruo78192442019-05-15 07:33:48 +080088static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
Omar Sandoval0d9f8242017-06-06 16:45:26 -070089{
Qu Wenruoddf30cf2019-04-04 14:45:34 +080090 if (ref->type == BTRFS_REF_METADATA) {
91 if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
Qu Wenruo78192442019-05-15 07:33:48 +080092 return BTRFS_BLOCK_GROUP_SYSTEM;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070093 else
Qu Wenruo78192442019-05-15 07:33:48 +080094 return BTRFS_BLOCK_GROUP_METADATA;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070095 }
Qu Wenruo78192442019-05-15 07:33:48 +080096 return BTRFS_BLOCK_GROUP_DATA;
97}
98
99static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
100 struct btrfs_ref *ref)
101{
102 struct btrfs_space_info *space_info;
103 u64 flags = generic_ref_to_space_flags(ref);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700104
Josef Bacik280c29082019-06-18 16:09:19 -0400105 space_info = btrfs_find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700106 ASSERT(space_info);
Qu Wenruo78192442019-05-15 07:33:48 +0800107 percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
108 BTRFS_TOTAL_BYTES_PINNED_BATCH);
109}
110
111static void sub_pinned_bytes(struct btrfs_fs_info *fs_info,
112 struct btrfs_ref *ref)
113{
114 struct btrfs_space_info *space_info;
115 u64 flags = generic_ref_to_space_flags(ref);
116
Josef Bacik280c29082019-06-18 16:09:19 -0400117 space_info = btrfs_find_space_info(fs_info, flags);
Qu Wenruo78192442019-05-15 07:33:48 +0800118 ASSERT(space_info);
119 percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
Ethan Liendec59fa2018-07-13 16:50:42 +0800120 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700121}
122
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000123/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400124int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400125{
126 int ret;
127 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400128 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400129
Zheng Yan31840ae2008-09-23 13:14:14 -0400130 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700131 if (!path)
132 return -ENOMEM;
133
Chris Masone02119d2008-09-05 16:13:11 -0400134 key.objectid = start;
135 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500136 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400137 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400138 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500139 return ret;
140}
141
Chris Masond8d5f3e2007-12-11 12:42:00 -0500142/*
Josef Bacik3173a182013-03-07 14:22:04 -0500143 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400144 *
145 * the head node for delayed ref is used to store the sum of all the
146 * reference count modifications queued up in the rbtree. the head
147 * node may also store the extent flags to set. This way you can check
148 * to see what the reference count and extent flags would be if all of
149 * the delayed refs are not processed.
150 */
151int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400152 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500153 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400154{
155 struct btrfs_delayed_ref_head *head;
156 struct btrfs_delayed_ref_root *delayed_refs;
157 struct btrfs_path *path;
158 struct btrfs_extent_item *ei;
159 struct extent_buffer *leaf;
160 struct btrfs_key key;
161 u32 item_size;
162 u64 num_refs;
163 u64 extent_flags;
164 int ret;
165
Josef Bacik3173a182013-03-07 14:22:04 -0500166 /*
167 * If we don't have skinny metadata, don't bother doing anything
168 * different
169 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400170 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
171 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500172 metadata = 0;
173 }
174
Yan, Zhenga22285a2010-05-16 10:48:46 -0400175 path = btrfs_alloc_path();
176 if (!path)
177 return -ENOMEM;
178
Yan, Zhenga22285a2010-05-16 10:48:46 -0400179 if (!trans) {
180 path->skip_locking = 1;
181 path->search_commit_root = 1;
182 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000183
184search_again:
185 key.objectid = bytenr;
186 key.offset = offset;
187 if (metadata)
188 key.type = BTRFS_METADATA_ITEM_KEY;
189 else
190 key.type = BTRFS_EXTENT_ITEM_KEY;
191
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400192 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400193 if (ret < 0)
194 goto out_free;
195
Josef Bacik3173a182013-03-07 14:22:04 -0500196 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100197 if (path->slots[0]) {
198 path->slots[0]--;
199 btrfs_item_key_to_cpu(path->nodes[0], &key,
200 path->slots[0]);
201 if (key.objectid == bytenr &&
202 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400203 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100204 ret = 0;
205 }
Josef Bacik3173a182013-03-07 14:22:04 -0500206 }
207
Yan, Zhenga22285a2010-05-16 10:48:46 -0400208 if (ret == 0) {
209 leaf = path->nodes[0];
210 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
211 if (item_size >= sizeof(*ei)) {
212 ei = btrfs_item_ptr(leaf, path->slots[0],
213 struct btrfs_extent_item);
214 num_refs = btrfs_extent_refs(leaf, ei);
215 extent_flags = btrfs_extent_flags(leaf, ei);
216 } else {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300217 ret = -EINVAL;
218 btrfs_print_v0_err(fs_info);
219 if (trans)
220 btrfs_abort_transaction(trans, ret);
221 else
222 btrfs_handle_fs_error(fs_info, ret, NULL);
223
224 goto out_free;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400225 }
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300226
Yan, Zhenga22285a2010-05-16 10:48:46 -0400227 BUG_ON(num_refs == 0);
228 } else {
229 num_refs = 0;
230 extent_flags = 0;
231 ret = 0;
232 }
233
234 if (!trans)
235 goto out;
236
237 delayed_refs = &trans->transaction->delayed_refs;
238 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800239 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400240 if (head) {
241 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400242 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400243 spin_unlock(&delayed_refs->lock);
244
David Sterbab3b4aa72011-04-21 01:20:15 +0200245 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400246
David Sterba8cc33e52011-05-02 15:29:25 +0200247 /*
248 * Mutex was contended, block until it's released and try
249 * again
250 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400251 mutex_lock(&head->mutex);
252 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400253 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000254 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400255 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500256 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400257 if (head->extent_op && head->extent_op->update_flags)
258 extent_flags |= head->extent_op->flags_to_set;
259 else
260 BUG_ON(num_refs == 0);
261
Josef Bacikd2788502017-09-29 15:43:57 -0400262 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500263 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400264 mutex_unlock(&head->mutex);
265 }
266 spin_unlock(&delayed_refs->lock);
267out:
268 WARN_ON(num_refs == 0);
269 if (refs)
270 *refs = num_refs;
271 if (flags)
272 *flags = extent_flags;
273out_free:
274 btrfs_free_path(path);
275 return ret;
276}
277
278/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500279 * Back reference rules. Back refs have three main goals:
280 *
281 * 1) differentiate between all holders of references to an extent so that
282 * when a reference is dropped we can make sure it was a valid reference
283 * before freeing the extent.
284 *
285 * 2) Provide enough information to quickly find the holders of an extent
286 * if we notice a given block is corrupted or bad.
287 *
288 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
289 * maintenance. This is actually the same as #2, but with a slightly
290 * different use case.
291 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400292 * There are two kinds of back refs. The implicit back refs is optimized
293 * for pointers in non-shared tree blocks. For a given pointer in a block,
294 * back refs of this kind provide information about the block's owner tree
295 * and the pointer's key. These information allow us to find the block by
296 * b-tree searching. The full back refs is for pointers in tree blocks not
297 * referenced by their owner trees. The location of tree block is recorded
298 * in the back refs. Actually the full back refs is generic, and can be
299 * used in all cases the implicit back refs is used. The major shortcoming
300 * of the full back refs is its overhead. Every time a tree block gets
301 * COWed, we have to update back refs entry for all pointers in it.
302 *
303 * For a newly allocated tree block, we use implicit back refs for
304 * pointers in it. This means most tree related operations only involve
305 * implicit back refs. For a tree block created in old transaction, the
306 * only way to drop a reference to it is COW it. So we can detect the
307 * event that tree block loses its owner tree's reference and do the
308 * back refs conversion.
309 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400310 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400311 *
312 * The reference count of the block is one and the tree is the block's
313 * owner tree. Nothing to do in this case.
314 *
315 * The reference count of the block is one and the tree is not the
316 * block's owner tree. In this case, full back refs is used for pointers
317 * in the block. Remove these full back refs, add implicit back refs for
318 * every pointers in the new block.
319 *
320 * The reference count of the block is greater than one and the tree is
321 * the block's owner tree. In this case, implicit back refs is used for
322 * pointers in the block. Add full back refs for every pointers in the
323 * block, increase lower level extents' reference counts. The original
324 * implicit back refs are entailed to the new block.
325 *
326 * The reference count of the block is greater than one and the tree is
327 * not the block's owner tree. Add implicit back refs for every pointer in
328 * the new block, increase lower level extents' reference count.
329 *
330 * Back Reference Key composing:
331 *
332 * The key objectid corresponds to the first byte in the extent,
333 * The key type is used to differentiate between types of back refs.
334 * There are different meanings of the key offset for different types
335 * of back refs.
336 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500337 * File extents can be referenced by:
338 *
339 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400340 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500341 * - different offsets inside a file (bookend extents in file.c)
342 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400343 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500344 *
345 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500346 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400347 * - original offset in the file
348 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400349 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400350 * The key offset for the implicit back refs is hash of the first
351 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500352 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400353 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500354 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400355 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500356 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400357 * The key offset for the implicit back refs is the first byte of
358 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500359 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 * When a file extent is allocated, The implicit back refs is used.
361 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500362 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400363 * (root_key.objectid, inode objectid, offset in file, 1)
364 *
365 * When a file extent is removed file truncation, we find the
366 * corresponding implicit back refs and check the following fields:
367 *
368 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500369 *
370 * Btree extents can be referenced by:
371 *
372 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500373 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400374 * Both the implicit back refs and the full back refs for tree blocks
375 * only consist of key. The key offset for the implicit back refs is
376 * objectid of block's owner tree. The key offset for the full back refs
377 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500378 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400379 * When implicit back refs is used, information about the lowest key and
380 * level of the tree block are required. These information are stored in
381 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500382 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400383
Liu Bo167ce952017-08-18 15:15:18 -0600384/*
385 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100386 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
Liu Bo167ce952017-08-18 15:15:18 -0600387 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
388 */
389int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
390 struct btrfs_extent_inline_ref *iref,
391 enum btrfs_inline_ref_type is_data)
392{
393 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -0600394 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -0600395
396 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
397 type == BTRFS_SHARED_BLOCK_REF_KEY ||
398 type == BTRFS_SHARED_DATA_REF_KEY ||
399 type == BTRFS_EXTENT_DATA_REF_KEY) {
400 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600401 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600402 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600403 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
404 ASSERT(eb->fs_info);
405 /*
406 * Every shared one has parent tree
407 * block, which must be aligned to
408 * nodesize.
409 */
410 if (offset &&
411 IS_ALIGNED(offset, eb->fs_info->nodesize))
412 return type;
413 }
Liu Bo167ce952017-08-18 15:15:18 -0600414 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600415 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600416 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600417 if (type == BTRFS_SHARED_DATA_REF_KEY) {
418 ASSERT(eb->fs_info);
419 /*
420 * Every shared one has parent tree
421 * block, which must be aligned to
422 * nodesize.
423 */
424 if (offset &&
425 IS_ALIGNED(offset, eb->fs_info->nodesize))
426 return type;
427 }
Liu Bo167ce952017-08-18 15:15:18 -0600428 } else {
429 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
430 return type;
431 }
432 }
433
434 btrfs_print_leaf((struct extent_buffer *)eb);
435 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
436 eb->start, type);
437 WARN_ON(1);
438
439 return BTRFS_REF_TYPE_INVALID;
440}
441
Qu Wenruo0785a9a2019-08-09 09:24:24 +0800442u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443{
444 u32 high_crc = ~(u32)0;
445 u32 low_crc = ~(u32)0;
446 __le64 lenum;
447
448 lenum = cpu_to_le64(root_objectid);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200449 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 lenum = cpu_to_le64(owner);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200451 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400452 lenum = cpu_to_le64(offset);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200453 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454
455 return ((u64)high_crc << 31) ^ (u64)low_crc;
456}
457
458static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
459 struct btrfs_extent_data_ref *ref)
460{
461 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
462 btrfs_extent_data_ref_objectid(leaf, ref),
463 btrfs_extent_data_ref_offset(leaf, ref));
464}
465
466static int match_extent_data_ref(struct extent_buffer *leaf,
467 struct btrfs_extent_data_ref *ref,
468 u64 root_objectid, u64 owner, u64 offset)
469{
470 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
471 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
472 btrfs_extent_data_ref_offset(leaf, ref) != offset)
473 return 0;
474 return 1;
475}
476
477static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400478 struct btrfs_path *path,
479 u64 bytenr, u64 parent,
480 u64 root_objectid,
481 u64 owner, u64 offset)
482{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +0300483 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400484 struct btrfs_key key;
485 struct btrfs_extent_data_ref *ref;
486 struct extent_buffer *leaf;
487 u32 nritems;
488 int ret;
489 int recow;
490 int err = -ENOENT;
491
492 key.objectid = bytenr;
493 if (parent) {
494 key.type = BTRFS_SHARED_DATA_REF_KEY;
495 key.offset = parent;
496 } else {
497 key.type = BTRFS_EXTENT_DATA_REF_KEY;
498 key.offset = hash_extent_data_ref(root_objectid,
499 owner, offset);
500 }
501again:
502 recow = 0;
503 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
504 if (ret < 0) {
505 err = ret;
506 goto fail;
507 }
508
509 if (parent) {
510 if (!ret)
511 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400512 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400513 }
514
515 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 nritems = btrfs_header_nritems(leaf);
517 while (1) {
518 if (path->slots[0] >= nritems) {
519 ret = btrfs_next_leaf(root, path);
520 if (ret < 0)
521 err = ret;
522 if (ret)
523 goto fail;
524
525 leaf = path->nodes[0];
526 nritems = btrfs_header_nritems(leaf);
527 recow = 1;
528 }
529
530 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
531 if (key.objectid != bytenr ||
532 key.type != BTRFS_EXTENT_DATA_REF_KEY)
533 goto fail;
534
535 ref = btrfs_item_ptr(leaf, path->slots[0],
536 struct btrfs_extent_data_ref);
537
538 if (match_extent_data_ref(leaf, ref, root_objectid,
539 owner, offset)) {
540 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200541 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400542 goto again;
543 }
544 err = 0;
545 break;
546 }
547 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400548 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549fail:
550 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400551}
552
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400553static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 struct btrfs_path *path,
555 u64 bytenr, u64 parent,
556 u64 root_objectid, u64 owner,
557 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400558{
Nikolay Borisov62b895a2018-06-20 15:48:44 +0300559 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -0400560 struct btrfs_key key;
561 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400562 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 u32 num_refs;
564 int ret;
565
566 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400567 if (parent) {
568 key.type = BTRFS_SHARED_DATA_REF_KEY;
569 key.offset = parent;
570 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400571 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400572 key.type = BTRFS_EXTENT_DATA_REF_KEY;
573 key.offset = hash_extent_data_ref(root_objectid,
574 owner, offset);
575 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400576 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400577
578 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
579 if (ret && ret != -EEXIST)
580 goto fail;
581
582 leaf = path->nodes[0];
583 if (parent) {
584 struct btrfs_shared_data_ref *ref;
585 ref = btrfs_item_ptr(leaf, path->slots[0],
586 struct btrfs_shared_data_ref);
587 if (ret == 0) {
588 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
589 } else {
590 num_refs = btrfs_shared_data_ref_count(leaf, ref);
591 num_refs += refs_to_add;
592 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
593 }
594 } else {
595 struct btrfs_extent_data_ref *ref;
596 while (ret == -EEXIST) {
597 ref = btrfs_item_ptr(leaf, path->slots[0],
598 struct btrfs_extent_data_ref);
599 if (match_extent_data_ref(leaf, ref, root_objectid,
600 owner, offset))
601 break;
David Sterbab3b4aa72011-04-21 01:20:15 +0200602 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400603 key.offset++;
604 ret = btrfs_insert_empty_item(trans, root, path, &key,
605 size);
606 if (ret && ret != -EEXIST)
607 goto fail;
608
609 leaf = path->nodes[0];
610 }
611 ref = btrfs_item_ptr(leaf, path->slots[0],
612 struct btrfs_extent_data_ref);
613 if (ret == 0) {
614 btrfs_set_extent_data_ref_root(leaf, ref,
615 root_objectid);
616 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
617 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
618 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
619 } else {
620 num_refs = btrfs_extent_data_ref_count(leaf, ref);
621 num_refs += refs_to_add;
622 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
623 }
624 }
625 btrfs_mark_buffer_dirty(leaf);
626 ret = 0;
627fail:
David Sterbab3b4aa72011-04-21 01:20:15 +0200628 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500629 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500630}
631
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400633 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -0700634 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -0400635{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400636 struct btrfs_key key;
637 struct btrfs_extent_data_ref *ref1 = NULL;
638 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400639 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400640 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400641 int ret = 0;
642
643 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400644 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
645
646 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
647 ref1 = btrfs_item_ptr(leaf, path->slots[0],
648 struct btrfs_extent_data_ref);
649 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
650 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
651 ref2 = btrfs_item_ptr(leaf, path->slots[0],
652 struct btrfs_shared_data_ref);
653 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200654 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300655 btrfs_print_v0_err(trans->fs_info);
656 btrfs_abort_transaction(trans, -EINVAL);
657 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 } else {
659 BUG();
660 }
661
Chris Mason56bec292009-03-13 10:10:06 -0400662 BUG_ON(num_refs < refs_to_drop);
663 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +0300666 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -0700667 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400668 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
670 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
671 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
672 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
Zheng Yan31840ae2008-09-23 13:14:14 -0400673 btrfs_mark_buffer_dirty(leaf);
674 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 return ret;
676}
677
Zhaolei9ed0dea2015-08-06 22:16:24 +0800678static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400679 struct btrfs_extent_inline_ref *iref)
680{
681 struct btrfs_key key;
682 struct extent_buffer *leaf;
683 struct btrfs_extent_data_ref *ref1;
684 struct btrfs_shared_data_ref *ref2;
685 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -0600686 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400687
688 leaf = path->nodes[0];
689 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300690
691 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400692 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -0600693 /*
694 * If type is invalid, we should have bailed out earlier than
695 * this call.
696 */
697 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
698 ASSERT(type != BTRFS_REF_TYPE_INVALID);
699 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
701 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
702 } else {
703 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
704 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
705 }
706 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
707 ref1 = btrfs_item_ptr(leaf, path->slots[0],
708 struct btrfs_extent_data_ref);
709 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
710 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
711 ref2 = btrfs_item_ptr(leaf, path->slots[0],
712 struct btrfs_shared_data_ref);
713 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400714 } else {
715 WARN_ON(1);
716 }
717 return num_refs;
718}
719
720static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400721 struct btrfs_path *path,
722 u64 bytenr, u64 parent,
723 u64 root_objectid)
724{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +0300725 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400726 struct btrfs_key key;
727 int ret;
728
729 key.objectid = bytenr;
730 if (parent) {
731 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
732 key.offset = parent;
733 } else {
734 key.type = BTRFS_TREE_BLOCK_REF_KEY;
735 key.offset = root_objectid;
736 }
737
738 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
739 if (ret > 0)
740 ret = -ENOENT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400741 return ret;
742}
743
744static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400745 struct btrfs_path *path,
746 u64 bytenr, u64 parent,
747 u64 root_objectid)
748{
749 struct btrfs_key key;
750 int ret;
751
752 key.objectid = bytenr;
753 if (parent) {
754 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
755 key.offset = parent;
756 } else {
757 key.type = BTRFS_TREE_BLOCK_REF_KEY;
758 key.offset = root_objectid;
759 }
760
Nikolay Borisov10728402018-06-20 15:48:43 +0300761 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500762 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +0200763 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -0400764 return ret;
765}
766
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400767static inline int extent_ref_type(u64 parent, u64 owner)
768{
769 int type;
770 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
771 if (parent > 0)
772 type = BTRFS_SHARED_BLOCK_REF_KEY;
773 else
774 type = BTRFS_TREE_BLOCK_REF_KEY;
775 } else {
776 if (parent > 0)
777 type = BTRFS_SHARED_DATA_REF_KEY;
778 else
779 type = BTRFS_EXTENT_DATA_REF_KEY;
780 }
781 return type;
782}
783
Yan Zheng2c47e6052009-06-27 21:07:35 -0400784static int find_next_key(struct btrfs_path *path, int level,
785 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400786
787{
Yan Zheng2c47e6052009-06-27 21:07:35 -0400788 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400789 if (!path->nodes[level])
790 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 if (path->slots[level] + 1 >=
792 btrfs_header_nritems(path->nodes[level]))
793 continue;
794 if (level == 0)
795 btrfs_item_key_to_cpu(path->nodes[level], key,
796 path->slots[level] + 1);
797 else
798 btrfs_node_key_to_cpu(path->nodes[level], key,
799 path->slots[level] + 1);
800 return 0;
801 }
802 return 1;
803}
804
805/*
806 * look for inline back ref. if back ref is found, *ref_ret is set
807 * to the address of inline back ref, and 0 is returned.
808 *
809 * if back ref isn't found, *ref_ret is set to the address where it
810 * should be inserted, and -ENOENT is returned.
811 *
812 * if insert is true and there are too many inline back refs, the path
813 * points to the extent item, and -EAGAIN is returned.
814 *
815 * NOTE: inline back refs are ordered in the same way that back ref
816 * items in the tree are ordered.
817 */
818static noinline_for_stack
819int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 struct btrfs_path *path,
821 struct btrfs_extent_inline_ref **ref_ret,
822 u64 bytenr, u64 num_bytes,
823 u64 parent, u64 root_objectid,
824 u64 owner, u64 offset, int insert)
825{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +0300826 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500827 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828 struct btrfs_key key;
829 struct extent_buffer *leaf;
830 struct btrfs_extent_item *ei;
831 struct btrfs_extent_inline_ref *iref;
832 u64 flags;
833 u64 item_size;
834 unsigned long ptr;
835 unsigned long end;
836 int extra_size;
837 int type;
838 int want;
839 int ret;
840 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400841 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -0600842 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400843
844 key.objectid = bytenr;
845 key.type = BTRFS_EXTENT_ITEM_KEY;
846 key.offset = num_bytes;
847
848 want = extent_ref_type(parent, owner);
849 if (insert) {
850 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -0400851 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 } else
853 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -0500854
855 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +0300856 * Owner is our level, so we can just add one to get the level for the
857 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -0500858 */
859 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
860 key.type = BTRFS_METADATA_ITEM_KEY;
861 key.offset = owner;
862 }
863
864again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
866 if (ret < 0) {
867 err = ret;
868 goto out;
869 }
Josef Bacik3173a182013-03-07 14:22:04 -0500870
871 /*
872 * We may be a newly converted file system which still has the old fat
873 * extent entries for metadata, so try and see if we have one of those.
874 */
875 if (ret > 0 && skinny_metadata) {
876 skinny_metadata = false;
877 if (path->slots[0]) {
878 path->slots[0]--;
879 btrfs_item_key_to_cpu(path->nodes[0], &key,
880 path->slots[0]);
881 if (key.objectid == bytenr &&
882 key.type == BTRFS_EXTENT_ITEM_KEY &&
883 key.offset == num_bytes)
884 ret = 0;
885 }
886 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +0100887 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500888 key.type = BTRFS_EXTENT_ITEM_KEY;
889 key.offset = num_bytes;
890 btrfs_release_path(path);
891 goto again;
892 }
893 }
894
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 if (ret && !insert) {
896 err = -ENOENT;
897 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530898 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -0500899 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -0500900 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902
903 leaf = path->nodes[0];
904 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200905 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300906 err = -EINVAL;
907 btrfs_print_v0_err(fs_info);
908 btrfs_abort_transaction(trans, err);
909 goto out;
910 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
913 flags = btrfs_extent_flags(leaf, ei);
914
915 ptr = (unsigned long)(ei + 1);
916 end = (unsigned long)ei + item_size;
917
Josef Bacik3173a182013-03-07 14:22:04 -0500918 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 ptr += sizeof(struct btrfs_tree_block_info);
920 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 }
922
Liu Bo3de28d52017-08-18 15:15:19 -0600923 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
924 needed = BTRFS_REF_TYPE_DATA;
925 else
926 needed = BTRFS_REF_TYPE_BLOCK;
927
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928 err = -ENOENT;
929 while (1) {
930 if (ptr >= end) {
931 WARN_ON(ptr > end);
932 break;
933 }
934 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600935 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
936 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +0800937 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -0600938 goto out;
939 }
940
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 if (want < type)
942 break;
943 if (want > type) {
944 ptr += btrfs_extent_inline_ref_size(type);
945 continue;
946 }
947
948 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
949 struct btrfs_extent_data_ref *dref;
950 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
951 if (match_extent_data_ref(leaf, dref, root_objectid,
952 owner, offset)) {
953 err = 0;
954 break;
955 }
956 if (hash_extent_data_ref_item(leaf, dref) <
957 hash_extent_data_ref(root_objectid, owner, offset))
958 break;
959 } else {
960 u64 ref_offset;
961 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
962 if (parent > 0) {
963 if (parent == ref_offset) {
964 err = 0;
965 break;
966 }
967 if (ref_offset < parent)
968 break;
969 } else {
970 if (root_objectid == ref_offset) {
971 err = 0;
972 break;
973 }
974 if (ref_offset < root_objectid)
975 break;
976 }
977 }
978 ptr += btrfs_extent_inline_ref_size(type);
979 }
980 if (err == -ENOENT && insert) {
981 if (item_size + extra_size >=
982 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
983 err = -EAGAIN;
984 goto out;
985 }
986 /*
987 * To add new inline back ref, we have to make sure
988 * there is no corresponding back ref item.
989 * For simplicity, we just do not add new inline back
990 * ref if there is any kind of item for this block
991 */
Yan Zheng2c47e6052009-06-27 21:07:35 -0400992 if (find_next_key(path, 0, &key) == 0 &&
993 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -0400994 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 err = -EAGAIN;
996 goto out;
997 }
998 }
999 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1000out:
Yan Zheng85d41982009-06-11 08:51:10 -04001001 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 path->keep_locks = 0;
1003 btrfs_unlock_up_safe(path, 1);
1004 }
1005 return err;
1006}
1007
1008/*
1009 * helper to add new inline back ref
1010 */
1011static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001012void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001013 struct btrfs_path *path,
1014 struct btrfs_extent_inline_ref *iref,
1015 u64 parent, u64 root_objectid,
1016 u64 owner, u64 offset, int refs_to_add,
1017 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018{
1019 struct extent_buffer *leaf;
1020 struct btrfs_extent_item *ei;
1021 unsigned long ptr;
1022 unsigned long end;
1023 unsigned long item_offset;
1024 u64 refs;
1025 int size;
1026 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027
1028 leaf = path->nodes[0];
1029 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1030 item_offset = (unsigned long)iref - (unsigned long)ei;
1031
1032 type = extent_ref_type(parent, owner);
1033 size = btrfs_extent_inline_ref_size(type);
1034
David Sterbac71dd882019-03-20 14:51:10 +01001035 btrfs_extend_item(path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036
1037 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1038 refs = btrfs_extent_refs(leaf, ei);
1039 refs += refs_to_add;
1040 btrfs_set_extent_refs(leaf, ei, refs);
1041 if (extent_op)
1042 __run_delayed_extent_op(extent_op, leaf, ei);
1043
1044 ptr = (unsigned long)ei + item_offset;
1045 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1046 if (ptr < end - size)
1047 memmove_extent_buffer(leaf, ptr + size, ptr,
1048 end - size - ptr);
1049
1050 iref = (struct btrfs_extent_inline_ref *)ptr;
1051 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1052 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1053 struct btrfs_extent_data_ref *dref;
1054 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1055 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1056 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1057 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1058 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1059 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1060 struct btrfs_shared_data_ref *sref;
1061 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1062 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1063 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1064 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1065 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1066 } else {
1067 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1068 }
1069 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070}
1071
1072static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001073 struct btrfs_path *path,
1074 struct btrfs_extent_inline_ref **ref_ret,
1075 u64 bytenr, u64 num_bytes, u64 parent,
1076 u64 root_objectid, u64 owner, u64 offset)
1077{
1078 int ret;
1079
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001080 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1081 num_bytes, parent, root_objectid,
1082 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 if (ret != -ENOENT)
1084 return ret;
1085
David Sterbab3b4aa72011-04-21 01:20:15 +02001086 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001087 *ref_ret = NULL;
1088
1089 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001090 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1091 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001093 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1094 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 }
1096 return ret;
1097}
1098
1099/*
1100 * helper to update/remove inline back ref
1101 */
1102static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001103void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001104 struct btrfs_extent_inline_ref *iref,
1105 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001106 struct btrfs_delayed_extent_op *extent_op,
1107 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001109 struct extent_buffer *leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001110 struct btrfs_extent_item *ei;
1111 struct btrfs_extent_data_ref *dref = NULL;
1112 struct btrfs_shared_data_ref *sref = NULL;
1113 unsigned long ptr;
1114 unsigned long end;
1115 u32 item_size;
1116 int size;
1117 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 u64 refs;
1119
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1121 refs = btrfs_extent_refs(leaf, ei);
1122 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1123 refs += refs_to_mod;
1124 btrfs_set_extent_refs(leaf, ei, refs);
1125 if (extent_op)
1126 __run_delayed_extent_op(extent_op, leaf, ei);
1127
Liu Bo3de28d52017-08-18 15:15:19 -06001128 /*
1129 * If type is invalid, we should have bailed out after
1130 * lookup_inline_extent_backref().
1131 */
1132 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1133 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134
1135 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1136 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1137 refs = btrfs_extent_data_ref_count(leaf, dref);
1138 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1139 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1140 refs = btrfs_shared_data_ref_count(leaf, sref);
1141 } else {
1142 refs = 1;
1143 BUG_ON(refs_to_mod != -1);
1144 }
1145
1146 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1147 refs += refs_to_mod;
1148
1149 if (refs > 0) {
1150 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1151 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1152 else
1153 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1154 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001155 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001156 size = btrfs_extent_inline_ref_size(type);
1157 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1158 ptr = (unsigned long)iref;
1159 end = (unsigned long)ei + item_size;
1160 if (ptr + size < end)
1161 memmove_extent_buffer(leaf, ptr, ptr + size,
1162 end - ptr - size);
1163 item_size -= size;
David Sterba78ac4f92019-03-20 14:49:12 +01001164 btrfs_truncate_item(path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001165 }
1166 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001167}
1168
1169static noinline_for_stack
1170int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001171 struct btrfs_path *path,
1172 u64 bytenr, u64 num_bytes, u64 parent,
1173 u64 root_objectid, u64 owner,
1174 u64 offset, int refs_to_add,
1175 struct btrfs_delayed_extent_op *extent_op)
1176{
1177 struct btrfs_extent_inline_ref *iref;
1178 int ret;
1179
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001180 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1181 num_bytes, parent, root_objectid,
1182 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 if (ret == 0) {
1184 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001185 update_inline_extent_backref(path, iref, refs_to_add,
1186 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 } else if (ret == -ENOENT) {
Nikolay Borisova639cde2018-06-20 15:49:10 +03001188 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001189 root_objectid, owner, offset,
1190 refs_to_add, extent_op);
1191 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001192 }
1193 return ret;
1194}
1195
1196static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001197 struct btrfs_path *path,
1198 u64 bytenr, u64 parent, u64 root_objectid,
1199 u64 owner, u64 offset, int refs_to_add)
1200{
1201 int ret;
1202 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1203 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001204 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1205 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001207 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1208 root_objectid, owner, offset,
1209 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210 }
1211 return ret;
1212}
1213
1214static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001215 struct btrfs_path *path,
1216 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001217 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001219 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220
1221 BUG_ON(!is_data && refs_to_drop != 1);
1222 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001223 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1224 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001225 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001226 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001227 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001229 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001230 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 }
1232 return ret;
1233}
1234
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001235static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1236 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001237{
Jeff Mahoney86557862015-06-15 09:41:16 -04001238 int j, ret = 0;
1239 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001240 u64 aligned_start = ALIGN(start, 1 << 9);
1241
1242 if (WARN_ON(start != aligned_start)) {
1243 len -= aligned_start - start;
1244 len = round_down(len, 1 << 9);
1245 start = aligned_start;
1246 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001247
1248 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001249
1250 if (!len)
1251 return 0;
1252
1253 end = start + len;
1254 bytes_left = len;
1255
1256 /* Skip any superblocks on this device. */
1257 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1258 u64 sb_start = btrfs_sb_offset(j);
1259 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1260 u64 size = sb_start - start;
1261
1262 if (!in_range(sb_start, start, bytes_left) &&
1263 !in_range(sb_end, start, bytes_left) &&
1264 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1265 continue;
1266
1267 /*
1268 * Superblock spans beginning of range. Adjust start and
1269 * try again.
1270 */
1271 if (sb_start <= start) {
1272 start += sb_end - start;
1273 if (start > end) {
1274 bytes_left = 0;
1275 break;
1276 }
1277 bytes_left = end - start;
1278 continue;
1279 }
1280
1281 if (size) {
1282 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1283 GFP_NOFS, 0);
1284 if (!ret)
1285 *discarded_bytes += size;
1286 else if (ret != -EOPNOTSUPP)
1287 return ret;
1288 }
1289
1290 start = sb_end;
1291 if (start > end) {
1292 bytes_left = 0;
1293 break;
1294 }
1295 bytes_left = end - start;
1296 }
1297
1298 if (bytes_left) {
1299 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001300 GFP_NOFS, 0);
1301 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001302 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001303 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001304 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001305}
Chris Mason15916de2008-11-19 21:17:22 -05001306
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001307int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001308 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001309{
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001310 int ret = 0;
Li Dongyang5378e602011-03-24 10:24:27 +00001311 u64 discarded_bytes = 0;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001312 u64 end = bytenr + num_bytes;
1313 u64 cur = bytenr;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001314 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001315
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001316
Filipe Manana29992412016-05-27 17:42:05 +01001317 /*
1318 * Avoid races with device replace and make sure our bbio has devices
1319 * associated to its stripes that don't go away while we are discarding.
1320 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001321 btrfs_bio_counter_inc_blocked(fs_info);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001322 while (cur < end) {
1323 struct btrfs_bio_stripe *stripe;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001324 int i;
1325
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001326 num_bytes = end - cur;
1327 /* Tell the block device(s) that the sectors can be discarded */
1328 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1329 &num_bytes, &bbio, 0);
1330 /*
1331 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1332 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1333 * thus we can't continue anyway.
1334 */
1335 if (ret < 0)
1336 goto out;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001337
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001338 stripe = bbio->stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001339 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001340 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001341 struct request_queue *req_q;
1342
Filipe Manana627e0872018-01-30 18:40:22 +00001343 if (!stripe->dev->bdev) {
1344 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1345 continue;
1346 }
Anand Jain38b5f682017-11-29 18:53:43 +08001347 req_q = bdev_get_queue(stripe->dev->bdev);
1348 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001349 continue;
1350
Li Dongyang5378e602011-03-24 10:24:27 +00001351 ret = btrfs_issue_discard(stripe->dev->bdev,
1352 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001353 stripe->length,
1354 &bytes);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001355 if (!ret) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001356 discarded_bytes += bytes;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001357 } else if (ret != -EOPNOTSUPP) {
1358 /*
1359 * Logic errors or -ENOMEM, or -EIO, but
1360 * unlikely to happen.
1361 *
1362 * And since there are two loops, explicitly
1363 * go to out to avoid confusion.
1364 */
1365 btrfs_put_bbio(bbio);
1366 goto out;
1367 }
Josef Bacikd5e20032011-08-04 14:52:27 +00001368
1369 /*
1370 * Just in case we get back EOPNOTSUPP for some reason,
1371 * just ignore the return value so we don't screw up
1372 * people calling discard_extent.
1373 */
1374 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001375 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001376 btrfs_put_bbio(bbio);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001377 cur += num_bytes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001378 }
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001379out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001380 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001381
1382 if (actual_bytes)
1383 *actual_bytes = discarded_bytes;
1384
Liu Hui1f3c79a2009-01-05 15:57:51 -05001385
David Woodhouse53b381b2013-01-29 18:40:14 -05001386 if (ret == -EOPNOTSUPP)
1387 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001388 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001389}
1390
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001393 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001394{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001395 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001396 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001397 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001398
Qu Wenruo82fa1132019-04-04 14:45:35 +08001399 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1400 generic_ref->action);
1401 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1402 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001403
Qu Wenruo82fa1132019-04-04 14:45:35 +08001404 if (generic_ref->type == BTRFS_REF_METADATA)
1405 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001406 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001407 else
1408 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001409 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001410
Qu Wenruo82fa1132019-04-04 14:45:35 +08001411 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001412
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001413 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001414 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001415
Zheng Yan31840ae2008-09-23 13:14:14 -04001416 return ret;
1417}
1418
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001419/*
1420 * __btrfs_inc_extent_ref - insert backreference for a given extent
1421 *
1422 * @trans: Handle of transaction
1423 *
1424 * @node: The delayed ref node used to get the bytenr/length for
1425 * extent whose references are incremented.
1426 *
1427 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1428 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1429 * bytenr of the parent block. Since new extents are always
1430 * created with indirect references, this will only be the case
1431 * when relocating a shared extent. In that case, root_objectid
1432 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1433 * be 0
1434 *
1435 * @root_objectid: The id of the root where this modification has originated,
1436 * this can be either one of the well-known metadata trees or
1437 * the subvolume id which references this extent.
1438 *
1439 * @owner: For data extents it is the inode number of the owning file.
1440 * For metadata extents this parameter holds the level in the
1441 * tree of the extent.
1442 *
1443 * @offset: For metadata extents the offset is ignored and is currently
1444 * always passed as 0. For data extents it is the fileoffset
1445 * this extent belongs to.
1446 *
1447 * @refs_to_add Number of references to add
1448 *
1449 * @extent_op Pointer to a structure, holding information necessary when
1450 * updating a tree block's flags
1451 *
1452 */
Chris Mason925baed2008-06-25 16:01:30 -04001453static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001454 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001455 u64 parent, u64 root_objectid,
1456 u64 owner, u64 offset, int refs_to_add,
1457 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001458{
Chris Mason5caf2a02007-04-02 11:20:42 -04001459 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001460 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001461 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001462 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001463 u64 bytenr = node->bytenr;
1464 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001465 u64 refs;
1466 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001467
Chris Mason5caf2a02007-04-02 11:20:42 -04001468 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001469 if (!path)
1470 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001471
David Sterbae4058b52015-11-27 16:31:35 +01001472 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001473 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001475 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1476 parent, root_objectid, owner,
1477 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001478 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001480
1481 /*
1482 * Ok we had -EAGAIN which means we didn't have space to insert and
1483 * inline extent ref, so just update the reference count and add a
1484 * normal backref.
1485 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001486 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001487 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001488 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1489 refs = btrfs_extent_refs(leaf, item);
1490 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1491 if (extent_op)
1492 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001494 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001495 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001496
David Sterbae4058b52015-11-27 16:31:35 +01001497 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001498 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001499 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03001500 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
1501 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001502 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001503 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504out:
Chris Mason74493f72007-12-11 09:25:06 -05001505 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001506 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001507}
1508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001510 struct btrfs_delayed_ref_node *node,
1511 struct btrfs_delayed_extent_op *extent_op,
1512 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001513{
Chris Mason56bec292009-03-13 10:10:06 -04001514 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001515 struct btrfs_delayed_data_ref *ref;
1516 struct btrfs_key ins;
1517 u64 parent = 0;
1518 u64 ref_root = 0;
1519 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 ins.objectid = node->bytenr;
1522 ins.offset = node->num_bytes;
1523 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001525 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001526 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001527
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1529 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001530 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001531
1532 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001533 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001534 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001535 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1536 flags, ref->objectid,
1537 ref->offset, &ins,
1538 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001540 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1541 ref->objectid, ref->offset,
1542 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001543 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001544 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001545 ref_root, ref->objectid,
1546 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001547 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001548 } else {
1549 BUG();
1550 }
Chris Mason56bec292009-03-13 10:10:06 -04001551 return ret;
1552}
1553
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001554static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1555 struct extent_buffer *leaf,
1556 struct btrfs_extent_item *ei)
1557{
1558 u64 flags = btrfs_extent_flags(leaf, ei);
1559 if (extent_op->update_flags) {
1560 flags |= extent_op->flags_to_set;
1561 btrfs_set_extent_flags(leaf, ei, flags);
1562 }
1563
1564 if (extent_op->update_key) {
1565 struct btrfs_tree_block_info *bi;
1566 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1567 bi = (struct btrfs_tree_block_info *)(ei + 1);
1568 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1569 }
1570}
1571
1572static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001573 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001574 struct btrfs_delayed_extent_op *extent_op)
1575{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001576 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 struct btrfs_key key;
1578 struct btrfs_path *path;
1579 struct btrfs_extent_item *ei;
1580 struct extent_buffer *leaf;
1581 u32 item_size;
1582 int ret;
1583 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001584 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001585
David Sterbabf31f872020-02-05 17:34:34 +01001586 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001587 return 0;
1588
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001589 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001590 metadata = 0;
1591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592 path = btrfs_alloc_path();
1593 if (!path)
1594 return -ENOMEM;
1595
Josef Bacikd2788502017-09-29 15:43:57 -04001596 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001597
Josef Bacik3173a182013-03-07 14:22:04 -05001598 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001599 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001600 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001601 } else {
1602 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001603 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001604 }
1605
1606again:
David Sterbae4058b52015-11-27 16:31:35 +01001607 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001608 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001610 if (ret < 0) {
1611 err = ret;
1612 goto out;
1613 }
1614 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001615 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001616 if (path->slots[0] > 0) {
1617 path->slots[0]--;
1618 btrfs_item_key_to_cpu(path->nodes[0], &key,
1619 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001620 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001621 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001622 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001623 ret = 0;
1624 }
1625 if (ret > 0) {
1626 btrfs_release_path(path);
1627 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001628
Josef Bacikd2788502017-09-29 15:43:57 -04001629 key.objectid = head->bytenr;
1630 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001631 key.type = BTRFS_EXTENT_ITEM_KEY;
1632 goto again;
1633 }
1634 } else {
1635 err = -EIO;
1636 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001637 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 }
1639
1640 leaf = path->nodes[0];
1641 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001642
David Sterba6d8ff4e2018-06-26 16:20:59 +02001643 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001644 err = -EINVAL;
1645 btrfs_print_v0_err(fs_info);
1646 btrfs_abort_transaction(trans, err);
1647 goto out;
1648 }
1649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001650 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1651 __run_delayed_extent_op(extent_op, leaf, ei);
1652
1653 btrfs_mark_buffer_dirty(leaf);
1654out:
1655 btrfs_free_path(path);
1656 return err;
1657}
1658
1659static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001660 struct btrfs_delayed_ref_node *node,
1661 struct btrfs_delayed_extent_op *extent_op,
1662 int insert_reserved)
1663{
1664 int ret = 0;
1665 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 u64 parent = 0;
1667 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668
1669 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001670 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001672 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1673 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001674 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675
Liu Bo02794222016-09-14 19:19:05 -07001676 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001677 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001678 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1679 node->bytenr, node->ref_mod, node->action, ref_root,
1680 parent);
1681 return -EIO;
1682 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001684 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001685 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001687 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1688 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001690 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001691 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001692 } else {
1693 BUG();
1694 }
1695 return ret;
1696}
1697
Chris Mason56bec292009-03-13 10:10:06 -04001698/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700 struct btrfs_delayed_ref_node *node,
1701 struct btrfs_delayed_extent_op *extent_op,
1702 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001703{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001704 int ret = 0;
1705
David Sterbabf31f872020-02-05 17:34:34 +01001706 if (TRANS_ABORTED(trans)) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04001707 if (insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001708 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001709 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001710 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001711
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001712 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1713 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001714 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 insert_reserved);
1716 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1717 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001718 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 insert_reserved);
1720 else
1721 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001722 if (ret && insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001723 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001724 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001725}
1726
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001727static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001728select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001729{
Filipe Mananacffc3372015-07-09 13:13:44 +01001730 struct btrfs_delayed_ref_node *ref;
1731
Liu Boe3d03962018-08-23 03:51:50 +08001732 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001733 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001734
Filipe Mananacffc3372015-07-09 13:13:44 +01001735 /*
1736 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1737 * This is to prevent a ref count from going down to zero, which deletes
1738 * the extent item from the extent tree, when there still are references
1739 * to add, which would fail because they would not find the extent item.
1740 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001741 if (!list_empty(&head->ref_add_list))
1742 return list_first_entry(&head->ref_add_list,
1743 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001744
Liu Boe3d03962018-08-23 03:51:50 +08001745 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001746 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001747 ASSERT(list_empty(&ref->add_list));
1748 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001749}
1750
Josef Bacik2eadaa22017-09-29 15:43:52 -04001751static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1752 struct btrfs_delayed_ref_head *head)
1753{
1754 spin_lock(&delayed_refs->lock);
1755 head->processing = 0;
1756 delayed_refs->num_heads_ready++;
1757 spin_unlock(&delayed_refs->lock);
1758 btrfs_delayed_ref_unlock(head);
1759}
1760
Josef Bacikbedc66172018-12-03 10:20:31 -05001761static struct btrfs_delayed_extent_op *cleanup_extent_op(
1762 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001763{
1764 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001765
1766 if (!extent_op)
1767 return NULL;
1768
1769 if (head->must_insert_reserved) {
1770 head->extent_op = NULL;
1771 btrfs_free_delayed_extent_op(extent_op);
1772 return NULL;
1773 }
1774 return extent_op;
1775}
1776
1777static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1778 struct btrfs_delayed_ref_head *head)
1779{
1780 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001781 int ret;
1782
Josef Bacikbedc66172018-12-03 10:20:31 -05001783 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001784 if (!extent_op)
1785 return 0;
1786 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001787 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001788 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001789 btrfs_free_delayed_extent_op(extent_op);
1790 return ret ? ret : 1;
1791}
1792
Josef Bacik31890da2018-11-21 14:05:41 -05001793void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1794 struct btrfs_delayed_ref_root *delayed_refs,
1795 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001796{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001797 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001798
1799 if (head->total_ref_mod < 0) {
1800 struct btrfs_space_info *space_info;
1801 u64 flags;
1802
1803 if (head->is_data)
1804 flags = BTRFS_BLOCK_GROUP_DATA;
1805 else if (head->is_system)
1806 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1807 else
1808 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacik280c29082019-06-18 16:09:19 -04001809 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05001810 ASSERT(space_info);
1811 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1812 -head->num_bytes,
1813 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1814
Josef Bacikba2c4d42018-12-03 10:20:33 -05001815 /*
1816 * We had csum deletions accounted for in our delayed refs rsv,
1817 * we need to drop the csum leaves for this update from our
1818 * delayed_refs_rsv.
1819 */
Josef Bacik07c47772018-12-03 10:20:30 -05001820 if (head->is_data) {
1821 spin_lock(&delayed_refs->lock);
1822 delayed_refs->pending_csums -= head->num_bytes;
1823 spin_unlock(&delayed_refs->lock);
Josef Bacikba2c4d42018-12-03 10:20:33 -05001824 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1825 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001826 }
1827 }
1828
Josef Bacikba2c4d42018-12-03 10:20:33 -05001829 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001830}
1831
Josef Bacik194ab0b2017-09-29 15:43:54 -04001832static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001833 struct btrfs_delayed_ref_head *head)
1834{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001835
1836 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001837 struct btrfs_delayed_ref_root *delayed_refs;
1838 int ret;
1839
1840 delayed_refs = &trans->transaction->delayed_refs;
1841
Josef Bacikbedc66172018-12-03 10:20:31 -05001842 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001843 if (ret < 0) {
1844 unselect_delayed_ref_head(delayed_refs, head);
1845 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1846 return ret;
1847 } else if (ret) {
1848 return ret;
1849 }
1850
1851 /*
1852 * Need to drop our head ref lock and re-acquire the delayed ref lock
1853 * and then re-check to make sure nobody got added.
1854 */
1855 spin_unlock(&head->lock);
1856 spin_lock(&delayed_refs->lock);
1857 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001858 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001859 spin_unlock(&head->lock);
1860 spin_unlock(&delayed_refs->lock);
1861 return 1;
1862 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001863 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001864 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001865 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001866
Josef Bacikc1103f72017-09-29 15:43:56 -04001867 if (head->must_insert_reserved) {
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001868 btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001869 if (head->is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00001870 ret = btrfs_del_csums(trans, fs_info->csum_root,
1871 head->bytenr, head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001872 }
1873 }
1874
Josef Bacik31890da2018-11-21 14:05:41 -05001875 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001876
1877 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001878 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001879 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001880 return 0;
1881}
1882
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001883static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1884 struct btrfs_trans_handle *trans)
1885{
1886 struct btrfs_delayed_ref_root *delayed_refs =
1887 &trans->transaction->delayed_refs;
1888 struct btrfs_delayed_ref_head *head = NULL;
1889 int ret;
1890
1891 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001892 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001893 if (!head) {
1894 spin_unlock(&delayed_refs->lock);
1895 return head;
1896 }
1897
1898 /*
1899 * Grab the lock that says we are going to process all the refs for
1900 * this head
1901 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001902 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001903 spin_unlock(&delayed_refs->lock);
1904
1905 /*
1906 * We may have dropped the spin lock to get the head mutex lock, and
1907 * that might have given someone else time to free the head. If that's
1908 * true, it has been removed from our list and we can move on.
1909 */
1910 if (ret == -EAGAIN)
1911 head = ERR_PTR(-EAGAIN);
1912
1913 return head;
1914}
1915
Nikolay Borisove7261382018-08-15 10:39:55 +03001916static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1917 struct btrfs_delayed_ref_head *locked_ref,
1918 unsigned long *run_refs)
1919{
1920 struct btrfs_fs_info *fs_info = trans->fs_info;
1921 struct btrfs_delayed_ref_root *delayed_refs;
1922 struct btrfs_delayed_extent_op *extent_op;
1923 struct btrfs_delayed_ref_node *ref;
1924 int must_insert_reserved = 0;
1925 int ret;
1926
1927 delayed_refs = &trans->transaction->delayed_refs;
1928
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001929 lockdep_assert_held(&locked_ref->mutex);
1930 lockdep_assert_held(&locked_ref->lock);
1931
Nikolay Borisove7261382018-08-15 10:39:55 +03001932 while ((ref = select_delayed_ref(locked_ref))) {
1933 if (ref->seq &&
1934 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1935 spin_unlock(&locked_ref->lock);
1936 unselect_delayed_ref_head(delayed_refs, locked_ref);
1937 return -EAGAIN;
1938 }
1939
1940 (*run_refs)++;
1941 ref->in_tree = 0;
1942 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1943 RB_CLEAR_NODE(&ref->ref_node);
1944 if (!list_empty(&ref->add_list))
1945 list_del(&ref->add_list);
1946 /*
1947 * When we play the delayed ref, also correct the ref_mod on
1948 * head
1949 */
1950 switch (ref->action) {
1951 case BTRFS_ADD_DELAYED_REF:
1952 case BTRFS_ADD_DELAYED_EXTENT:
1953 locked_ref->ref_mod -= ref->ref_mod;
1954 break;
1955 case BTRFS_DROP_DELAYED_REF:
1956 locked_ref->ref_mod += ref->ref_mod;
1957 break;
1958 default:
1959 WARN_ON(1);
1960 }
1961 atomic_dec(&delayed_refs->num_entries);
1962
1963 /*
1964 * Record the must_insert_reserved flag before we drop the
1965 * spin lock.
1966 */
1967 must_insert_reserved = locked_ref->must_insert_reserved;
1968 locked_ref->must_insert_reserved = 0;
1969
1970 extent_op = locked_ref->extent_op;
1971 locked_ref->extent_op = NULL;
1972 spin_unlock(&locked_ref->lock);
1973
1974 ret = run_one_delayed_ref(trans, ref, extent_op,
1975 must_insert_reserved);
1976
1977 btrfs_free_delayed_extent_op(extent_op);
1978 if (ret) {
1979 unselect_delayed_ref_head(delayed_refs, locked_ref);
1980 btrfs_put_delayed_ref(ref);
1981 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1982 ret);
1983 return ret;
1984 }
1985
1986 btrfs_put_delayed_ref(ref);
1987 cond_resched();
1988
1989 spin_lock(&locked_ref->lock);
1990 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1991 }
1992
1993 return 0;
1994}
1995
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001996/*
1997 * Returns 0 on success or if called with an already aborted transaction.
1998 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
1999 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002000static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002001 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002002{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002003 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002004 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002005 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002006 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002007 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002008 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002009 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002010
2011 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002012 do {
Chris Mason56bec292009-03-13 10:10:06 -04002013 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002014 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002015 if (IS_ERR_OR_NULL(locked_ref)) {
2016 if (PTR_ERR(locked_ref) == -EAGAIN) {
2017 continue;
2018 } else {
2019 break;
2020 }
Chris Mason56bec292009-03-13 10:10:06 -04002021 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002022 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002023 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002024 /*
2025 * We need to try and merge add/drops of the same ref since we
2026 * can run into issues with relocate dropping the implicit ref
2027 * and then it being added back again before the drop can
2028 * finish. If we merged anything we need to re-loop so we can
2029 * get a good ref.
2030 * Or we can get node references of the same type that weren't
2031 * merged when created due to bumps in the tree mod seq, and
2032 * we need to merge them to prevent adding an inline extent
2033 * backref before dropping it (triggering a BUG_ON at
2034 * insert_inline_extent_backref()).
2035 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002036 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002037 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002038
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002039 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2040 &actual_count);
2041 if (ret < 0 && ret != -EAGAIN) {
2042 /*
2043 * Error, btrfs_run_delayed_refs_for_head already
2044 * unlocked everything so just bail out
2045 */
2046 return ret;
2047 } else if (!ret) {
2048 /*
2049 * Success, perform the usual cleanup of a processed
2050 * head
2051 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002052 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002053 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002054 /* We dropped our lock, we need to loop. */
2055 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002056 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002057 } else if (ret) {
2058 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002059 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002060 }
Chris Mason56bec292009-03-13 10:10:06 -04002061
Josef Bacikb00e6252017-09-29 15:43:53 -04002062 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002063 * Either success case or btrfs_run_delayed_refs_for_head
2064 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002065 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002066
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002067 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002068 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002069 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002070
2071 /*
2072 * We don't want to include ref heads since we can have empty ref heads
2073 * and those will drastically skew our runtime down since we just do
2074 * accounting, no actual extent tree updates.
2075 */
2076 if (actual_count > 0) {
2077 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2078 u64 avg;
2079
2080 /*
2081 * We weigh the current average higher than our current runtime
2082 * to avoid large swings in the average.
2083 */
2084 spin_lock(&delayed_refs->lock);
2085 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002086 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002087 spin_unlock(&delayed_refs->lock);
2088 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002089 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002090}
2091
Arne Jansen709c0482011-09-12 12:22:57 +02002092#ifdef SCRAMBLE_DELAYED_REFS
2093/*
2094 * Normally delayed refs get processed in ascending bytenr order. This
2095 * correlates in most cases to the order added. To expose dependencies on this
2096 * order, we start to process the tree in the middle instead of the beginning
2097 */
2098static u64 find_middle(struct rb_root *root)
2099{
2100 struct rb_node *n = root->rb_node;
2101 struct btrfs_delayed_ref_node *entry;
2102 int alt = 1;
2103 u64 middle;
2104 u64 first = 0, last = 0;
2105
2106 n = rb_first(root);
2107 if (n) {
2108 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2109 first = entry->bytenr;
2110 }
2111 n = rb_last(root);
2112 if (n) {
2113 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2114 last = entry->bytenr;
2115 }
2116 n = root->rb_node;
2117
2118 while (n) {
2119 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2120 WARN_ON(!entry->in_tree);
2121
2122 middle = entry->bytenr;
2123
2124 if (alt)
2125 n = n->rb_left;
2126 else
2127 n = n->rb_right;
2128
2129 alt = 1 - alt;
2130 }
2131 return middle;
2132}
2133#endif
2134
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002135static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002136{
2137 u64 num_bytes;
2138
2139 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2140 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002141 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002142 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2143
2144 /*
2145 * We don't ever fill up leaves all the way so multiply by 2 just to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04002146 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002147 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002148 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002149}
2150
Josef Bacik12621332015-02-03 07:50:16 -08002151/*
2152 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2153 * would require to store the csums for that many bytes.
2154 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002155u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002156{
2157 u64 csum_size;
2158 u64 num_csums_per_leaf;
2159 u64 num_csums;
2160
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002161 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002162 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002163 (u64)btrfs_super_csum_size(fs_info->super_copy));
2164 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002165 num_csums += num_csums_per_leaf - 1;
2166 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2167 return num_csums;
2168}
2169
Chris Masonc3e69d52009-03-13 10:17:05 -04002170/*
2171 * this starts processing the delayed reference count updates and
2172 * extent insertions we have queued up so far. count can be
2173 * 0, which means to process everything in the tree at the start
2174 * of the run (but not newly added entries), or it can be some target
2175 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002176 *
2177 * Returns 0 on success or if called with an aborted transaction
2178 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002179 */
2180int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002181 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002182{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002183 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002184 struct rb_node *node;
2185 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002186 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002187 int ret;
2188 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002189
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002190 /* We'll clean this up in btrfs_cleanup_transaction */
David Sterbabf31f872020-02-05 17:34:34 +01002191 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002192 return 0;
2193
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002194 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002195 return 0;
2196
Chris Masonc3e69d52009-03-13 10:17:05 -04002197 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002198 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002199 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002200
Chris Masonc3e69d52009-03-13 10:17:05 -04002201again:
Arne Jansen709c0482011-09-12 12:22:57 +02002202#ifdef SCRAMBLE_DELAYED_REFS
2203 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2204#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002205 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002206 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002207 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002208 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002209 }
2210
Chris Mason56bec292009-03-13 10:10:06 -04002211 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002212 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002213
Josef Bacikd7df2c72014-01-23 09:21:38 -05002214 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002215 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002216 if (!node) {
2217 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002218 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002219 }
Josef Bacikd2788502017-09-29 15:43:57 -04002220 head = rb_entry(node, struct btrfs_delayed_ref_head,
2221 href_node);
2222 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002223 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002224
2225 /* Mutex was contended, block until it's released and retry. */
2226 mutex_lock(&head->mutex);
2227 mutex_unlock(&head->mutex);
2228
2229 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002230 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002231 goto again;
2232 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002233out:
Chris Masona28ec192007-03-06 20:08:01 -05002234 return 0;
2235}
2236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002237int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002239 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240{
2241 struct btrfs_delayed_extent_op *extent_op;
2242 int ret;
2243
Miao Xie78a61842012-11-21 02:21:28 +00002244 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002245 if (!extent_op)
2246 return -ENOMEM;
2247
2248 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002249 extent_op->update_flags = true;
2250 extent_op->update_key = false;
2251 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002252 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002253
David Sterbac6e340b2019-03-20 11:42:34 +01002254 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002256 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002257 return ret;
2258}
2259
Liu Boe4c3b2d2017-01-30 12:25:28 -08002260static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002261 struct btrfs_path *path,
2262 u64 objectid, u64 offset, u64 bytenr)
2263{
2264 struct btrfs_delayed_ref_head *head;
2265 struct btrfs_delayed_ref_node *ref;
2266 struct btrfs_delayed_data_ref *data_ref;
2267 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002268 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002269 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002270 int ret = 0;
2271
ethanwu998ac6d2018-04-29 15:59:42 +08002272 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002273 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002274 if (cur_trans)
2275 refcount_inc(&cur_trans->use_count);
2276 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002277 if (!cur_trans)
2278 return 0;
2279
2280 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002282 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002283 if (!head) {
2284 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002285 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002286 return 0;
2287 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288
2289 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002290 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002291 spin_unlock(&delayed_refs->lock);
2292
David Sterbab3b4aa72011-04-21 01:20:15 +02002293 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002294
David Sterba8cc33e52011-05-02 15:29:25 +02002295 /*
2296 * Mutex was contended, block until it's released and let
2297 * caller try again
2298 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 mutex_lock(&head->mutex);
2300 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002301 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002302 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002303 return -EAGAIN;
2304 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002306
2307 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002308 /*
2309 * XXX: We should replace this with a proper search function in the
2310 * future.
2311 */
Liu Boe3d03962018-08-23 03:51:50 +08002312 for (node = rb_first_cached(&head->ref_tree); node;
2313 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002314 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002315 /* If it's a shared ref we know a cross reference exists */
2316 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2317 ret = 1;
2318 break;
2319 }
2320
2321 data_ref = btrfs_delayed_node_to_data_ref(ref);
2322
2323 /*
2324 * If our ref doesn't match the one we're currently looking at
2325 * then we have a cross reference.
2326 */
2327 if (data_ref->root != root->root_key.objectid ||
2328 data_ref->objectid != objectid ||
2329 data_ref->offset != offset) {
2330 ret = 1;
2331 break;
2332 }
2333 }
2334 spin_unlock(&head->lock);
2335 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002336 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337 return ret;
2338}
2339
Liu Boe4c3b2d2017-01-30 12:25:28 -08002340static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002341 struct btrfs_path *path,
2342 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002344 struct btrfs_fs_info *fs_info = root->fs_info;
2345 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002346 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347 struct btrfs_extent_data_ref *ref;
2348 struct btrfs_extent_inline_ref *iref;
2349 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002350 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002352 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002353 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002354
Chris Masonbe20aa92007-12-17 20:14:01 -05002355 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002356 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002357 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002358
Chris Masonbe20aa92007-12-17 20:14:01 -05002359 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2360 if (ret < 0)
2361 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002362 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002363
2364 ret = -ENOENT;
2365 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002366 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002367
Zheng Yan31840ae2008-09-23 13:14:14 -04002368 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002369 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002371
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002373 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375 ret = 1;
2376 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2378
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002379 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002380 if (item_size != sizeof(*ei) +
2381 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2382 goto out;
2383
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002384 /* If extent created before last snapshot => it's definitely shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002385 if (btrfs_extent_generation(leaf, ei) <=
2386 btrfs_root_last_snapshot(&root->root_item))
2387 goto out;
2388
2389 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002390
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002391 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002392 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2393 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002394 goto out;
2395
2396 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2397 if (btrfs_extent_refs(leaf, ei) !=
2398 btrfs_extent_data_ref_count(leaf, ref) ||
2399 btrfs_extent_data_ref_root(leaf, ref) !=
2400 root->root_key.objectid ||
2401 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2402 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2403 goto out;
2404
Yan Zhengf321e492008-07-30 09:26:11 -04002405 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002406out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407 return ret;
2408}
2409
Liu Boe4c3b2d2017-01-30 12:25:28 -08002410int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2411 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002412{
2413 struct btrfs_path *path;
2414 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415
2416 path = btrfs_alloc_path();
2417 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002418 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002419
2420 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002421 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002422 offset, bytenr);
2423 if (ret && ret != -ENOENT)
2424 goto out;
2425
Misono Tomohiro380fd062018-08-30 10:59:16 +09002426 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2427 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429out:
Yan Zhengf321e492008-07-30 09:26:11 -04002430 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002431 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2432 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002433 return ret;
2434}
2435
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002436static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002437 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002438 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002439 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002442 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002443 u64 num_bytes;
2444 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002446 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002447 struct btrfs_key key;
2448 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002449 struct btrfs_ref generic_ref = { 0 };
2450 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002451 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002452 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002453 int level;
2454 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002457 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002458
Zheng Yan31840ae2008-09-23 13:14:14 -04002459 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 nritems = btrfs_header_nritems(buf);
2461 level = btrfs_header_level(buf);
2462
Miao Xie27cdeb72014-04-02 19:51:05 +08002463 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002464 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002465
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002466 if (full_backref)
2467 parent = buf->start;
2468 else
2469 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002470 if (inc)
2471 action = BTRFS_ADD_DELAYED_REF;
2472 else
2473 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474
Zheng Yan31840ae2008-09-23 13:14:14 -04002475 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002476 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002477 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002478 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002479 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002480 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002481 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002482 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002483 BTRFS_FILE_EXTENT_INLINE)
2484 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002485 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2486 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002487 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002488
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002489 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2490 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002491 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2492 num_bytes, parent);
2493 generic_ref.real_root = root->root_key.objectid;
2494 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2495 key.offset);
2496 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002497 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002498 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002499 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002500 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 if (ret)
2502 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002503 } else {
2504 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002505 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002506 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2507 num_bytes, parent);
2508 generic_ref.real_root = root->root_key.objectid;
2509 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2510 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002511 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002512 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002513 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002514 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002515 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002516 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002517 }
2518 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002519 return 0;
2520fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002521 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002522}
2523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002524int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002525 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002526{
Josef Bacike339a6b2014-07-02 10:54:25 -07002527 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002528}
Zheng Yan31840ae2008-09-23 13:14:14 -04002529
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002531 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532{
Josef Bacike339a6b2014-07-02 10:54:25 -07002533 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002534}
2535
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002536int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002537{
David Sterba32da53862019-10-29 19:20:18 +01002538 struct btrfs_block_group *block_group;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002539 int readonly = 0;
2540
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002541 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002542 if (!block_group || block_group->ro)
2543 readonly = 1;
2544 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002545 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002546 return readonly;
2547}
2548
Jeff Mahoney1b868262017-05-17 11:38:35 -04002549static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002550{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002551 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002552 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002553 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002554
Yan, Zhengb742bb822010-05-16 10:46:24 -04002555 if (data)
2556 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002557 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002558 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2559 else
2560 flags = BTRFS_BLOCK_GROUP_METADATA;
2561
Josef Bacik878d7b62019-06-20 15:38:05 -04002562 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002563 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002564}
2565
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002566static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002567{
David Sterba32da53862019-10-29 19:20:18 +01002568 struct btrfs_block_group *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002569 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002570
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002571 spin_lock(&fs_info->block_group_cache_lock);
2572 bytenr = fs_info->first_logical_byte;
2573 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002574
2575 if (bytenr < (u64)-1)
2576 return bytenr;
2577
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002578 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002579 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002580 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002581
David Sterbab3470b52019-10-23 18:48:22 +02002582 bytenr = cache->start;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002583 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002584
2585 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002586}
2587
David Sterba32da53862019-10-29 19:20:18 +01002588static int pin_down_extent(struct btrfs_block_group *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002589 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002590{
David Sterbafdf08602019-03-20 12:12:32 +01002591 struct btrfs_fs_info *fs_info = cache->fs_info;
2592
Yan Zheng11833d62009-09-11 16:11:19 -04002593 spin_lock(&cache->space_info->lock);
2594 spin_lock(&cache->lock);
2595 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002596 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2597 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002598 if (reserved) {
2599 cache->reserved -= num_bytes;
2600 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002601 }
Yan Zheng11833d62009-09-11 16:11:19 -04002602 spin_unlock(&cache->lock);
2603 spin_unlock(&cache->space_info->lock);
2604
Ethan Liendec59fa2018-07-13 16:50:42 +08002605 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2606 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002607 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002608 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002609 return 0;
2610}
Chris Mason9078a3e2007-04-26 16:46:15 -04002611
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002612int btrfs_pin_extent(struct btrfs_trans_handle *trans,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002613 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002614{
David Sterba32da53862019-10-29 19:20:18 +01002615 struct btrfs_block_group *cache;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002616
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002617 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002618 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002619
David Sterbafdf08602019-03-20 12:12:32 +01002620 pin_down_extent(cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002621
2622 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002623 return 0;
2624}
Zheng Yane8569812008-09-26 10:05:48 -04002625
Yan, Zhengf0486c62010-05-16 10:46:25 -04002626/*
Chris Masone688b7252011-10-31 20:52:39 -04002627 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002628 */
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002629int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
Chris Masone688b7252011-10-31 20:52:39 -04002630 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002631{
David Sterba32da53862019-10-29 19:20:18 +01002632 struct btrfs_block_group *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002633 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002634
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002635 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002636 if (!cache)
2637 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002638
2639 /*
2640 * pull in the free space cache (if any) so that our pin
2641 * removes the free space from the cache. We have load_only set
2642 * to one because the slow code to read in the free extents does check
2643 * the pinned extents.
2644 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002645 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002646
David Sterbafdf08602019-03-20 12:12:32 +01002647 pin_down_extent(cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002648
2649 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002650 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002651 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002652 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002653}
2654
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002655static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2656 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002657{
2658 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002659 struct btrfs_block_group *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002660 struct btrfs_caching_control *caching_ctl;
2661
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002662 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002663 if (!block_group)
2664 return -EINVAL;
2665
Josef Bacik676f1f72019-06-20 15:37:48 -04002666 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002667 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002668
2669 if (!caching_ctl) {
2670 /* Logic error */
David Sterba32da53862019-10-29 19:20:18 +01002671 BUG_ON(!btrfs_block_group_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002672 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2673 } else {
2674 mutex_lock(&caching_ctl->mutex);
2675
2676 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002677 ret = btrfs_add_excluded_extent(fs_info, start,
2678 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002679 } else if (start + num_bytes <= caching_ctl->progress) {
2680 ret = btrfs_remove_free_space(block_group,
2681 start, num_bytes);
2682 } else {
2683 num_bytes = caching_ctl->progress - start;
2684 ret = btrfs_remove_free_space(block_group,
2685 start, num_bytes);
2686 if (ret)
2687 goto out_lock;
2688
2689 num_bytes = (start + num_bytes) -
2690 caching_ctl->progress;
2691 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002692 ret = btrfs_add_excluded_extent(fs_info, start,
2693 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002694 }
2695out_lock:
2696 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002697 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002698 }
2699 btrfs_put_block_group(block_group);
2700 return ret;
2701}
2702
David Sterbabcdc4282019-03-20 12:14:33 +01002703int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002704{
David Sterbabcdc4282019-03-20 12:14:33 +01002705 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002706 struct btrfs_file_extent_item *item;
2707 struct btrfs_key key;
2708 int found_type;
2709 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002710 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002711
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002712 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002713 return 0;
2714
2715 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2716 btrfs_item_key_to_cpu(eb, &key, i);
2717 if (key.type != BTRFS_EXTENT_DATA_KEY)
2718 continue;
2719 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2720 found_type = btrfs_file_extent_type(eb, item);
2721 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2722 continue;
2723 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2724 continue;
2725 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2726 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002727 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2728 if (ret)
2729 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002730 }
2731
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002732 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002733}
2734
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002735static void
David Sterba32da53862019-10-29 19:20:18 +01002736btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002737{
2738 atomic_inc(&bg->reservations);
2739}
2740
David Sterba8b74c032017-02-10 19:20:56 +01002741void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04002742{
Yan Zheng11833d62009-09-11 16:11:19 -04002743 struct btrfs_caching_control *next;
2744 struct btrfs_caching_control *caching_ctl;
David Sterba32da53862019-10-29 19:20:18 +01002745 struct btrfs_block_group *cache;
Yan Zheng11833d62009-09-11 16:11:19 -04002746
Josef Bacik9e351cc2014-03-13 15:42:13 -04002747 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04002748
2749 list_for_each_entry_safe(caching_ctl, next,
2750 &fs_info->caching_block_groups, list) {
2751 cache = caching_ctl->block_group;
David Sterba32da53862019-10-29 19:20:18 +01002752 if (btrfs_block_group_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04002753 cache->last_byte_to_unpin = (u64)-1;
2754 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04002755 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04002756 } else {
2757 cache->last_byte_to_unpin = caching_ctl->progress;
2758 }
2759 }
2760
2761 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2762 fs_info->pinned_extents = &fs_info->freed_extents[1];
2763 else
2764 fs_info->pinned_extents = &fs_info->freed_extents[0];
2765
Josef Bacik9e351cc2014-03-13 15:42:13 -04002766 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002767
Josef Bacik67f9c222019-06-19 13:47:23 -04002768 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04002769}
2770
Josef Bacikc759c4e2015-10-02 15:25:10 -04002771/*
2772 * Returns the free cluster for the given space info and sets empty_cluster to
2773 * what it should be based on the mount options.
2774 */
2775static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002776fetch_cluster_info(struct btrfs_fs_info *fs_info,
2777 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002778{
2779 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002780
2781 *empty_cluster = 0;
2782 if (btrfs_mixed_space_info(space_info))
2783 return ret;
2784
Josef Bacikc759c4e2015-10-02 15:25:10 -04002785 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002786 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002787 if (btrfs_test_opt(fs_info, SSD))
2788 *empty_cluster = SZ_2M;
2789 else
Byongho Leeee221842015-12-15 01:42:10 +09002790 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002791 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2792 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2793 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002795 }
2796
2797 return ret;
2798}
2799
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002800static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2801 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002802 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002803{
David Sterba32da53862019-10-29 19:20:18 +01002804 struct btrfs_block_group *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002805 struct btrfs_space_info *space_info;
2806 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002807 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002808 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002809 u64 total_unpinned = 0;
2810 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002811 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002812
2813 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002814 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002815 if (!cache ||
David Sterbab3470b52019-10-23 18:48:22 +02002816 start >= cache->start + cache->length) {
Yan Zheng11833d62009-09-11 16:11:19 -04002817 if (cache)
2818 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002819 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002820 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002821 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002822
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002823 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002824 cache->space_info,
2825 &empty_cluster);
2826 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002827 }
2828
David Sterbab3470b52019-10-23 18:48:22 +02002829 len = cache->start + cache->length - start;
Yan Zheng11833d62009-09-11 16:11:19 -04002830 len = min(len, end + 1 - start);
2831
2832 if (start < cache->last_byte_to_unpin) {
2833 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00002834 if (return_free_space)
2835 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04002836 }
Josef Bacik25179202008-10-29 14:49:05 -04002837
Yan, Zhengf0486c62010-05-16 10:46:25 -04002838 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002839 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002840 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002841
Josef Bacikc759c4e2015-10-02 15:25:10 -04002842 /*
2843 * If this space cluster has been marked as fragmented and we've
2844 * unpinned enough in this block group to potentially allow a
2845 * cluster to be created inside of it go ahead and clear the
2846 * fragmented check.
2847 */
2848 if (cluster && cluster->fragmented &&
2849 total_unpinned > empty_cluster) {
2850 spin_lock(&cluster->lock);
2851 cluster->fragmented = 0;
2852 spin_unlock(&cluster->lock);
2853 }
2854
Josef Bacik7b398f82012-10-22 15:52:28 -04002855 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002856 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002857 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002858 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacik4f4db212015-09-29 11:40:47 -04002859 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08002860 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2861 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04002862 if (cache->ro) {
2863 space_info->bytes_readonly += len;
2864 readonly = true;
2865 }
Josef Bacik25179202008-10-29 14:49:05 -04002866 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002867 if (!readonly && return_free_space &&
2868 global_rsv->space_info == space_info) {
2869 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002870
Josef Bacik7b398f82012-10-22 15:52:28 -04002871 spin_lock(&global_rsv->lock);
2872 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002873 to_add = min(len, global_rsv->size -
2874 global_rsv->reserved);
2875 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002876 btrfs_space_info_update_bytes_may_use(fs_info,
2877 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002878 if (global_rsv->reserved >= global_rsv->size)
2879 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002880 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002881 }
2882 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002883 /* Add to any tickets we may have */
2884 if (len)
Josef Bacik18fa2282019-08-22 15:10:58 -04002885 btrfs_try_granting_tickets(fs_info,
2886 space_info);
Josef Bacik7b398f82012-10-22 15:52:28 -04002887 }
2888 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002889 }
2890
2891 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002892 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002893 return 0;
2894}
2895
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002896int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002897{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002898 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002899 struct btrfs_block_group *block_group, *tmp;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002900 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002901 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002902 u64 start;
2903 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002904 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002905
Yan Zheng11833d62009-09-11 16:11:19 -04002906 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2907 unpin = &fs_info->freed_extents[1];
2908 else
2909 unpin = &fs_info->freed_extents[0];
2910
David Sterbabf31f872020-02-05 17:34:34 +01002911 while (!TRANS_ABORTED(trans)) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002912 struct extent_state *cached_state = NULL;
2913
Filipe Mananad4b450c2015-01-29 19:18:25 +00002914 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002915 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002916 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002917 if (ret) {
2918 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002919 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002920 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002921
Dennis Zhou46b27f52019-12-13 16:22:11 -08002922 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002923 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002924 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002925
Filipe Manana0e6ec382018-11-16 13:04:44 +00002926 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002927 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002928 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002929 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002930 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002931 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002932
Dennis Zhoua2309302020-01-02 16:26:35 -05002933 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
2934 btrfs_discard_calc_delay(&fs_info->discard_ctl);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002935 btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -05002936 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002937
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002938 /*
2939 * Transaction is finished. We don't need the lock anymore. We
2940 * do need to clean up the block groups in case of a transaction
2941 * abort.
2942 */
2943 deleted_bgs = &trans->transaction->deleted_bgs;
2944 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2945 u64 trimmed = 0;
2946
2947 ret = -EROFS;
David Sterbabf31f872020-02-05 17:34:34 +01002948 if (!TRANS_ABORTED(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002949 ret = btrfs_discard_extent(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02002950 block_group->start,
2951 block_group->length,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002952 &trimmed);
2953
2954 list_del_init(&block_group->bg_list);
2955 btrfs_put_block_group_trimming(block_group);
2956 btrfs_put_block_group(block_group);
2957
2958 if (ret) {
2959 const char *errstr = btrfs_decode_error(ret);
2960 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002961 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002962 ret, errstr);
2963 }
2964 }
2965
Chris Masone20d96d2007-03-22 12:13:20 -04002966 return 0;
2967}
2968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002970 struct btrfs_delayed_ref_node *node, u64 parent,
2971 u64 root_objectid, u64 owner_objectid,
2972 u64 owner_offset, int refs_to_drop,
2973 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002974{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002975 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002976 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002977 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002978 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002979 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980 struct btrfs_extent_item *ei;
2981 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002982 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002984 int extent_slot = 0;
2985 int found_extent = 0;
2986 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 u32 item_size;
2988 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002989 u64 bytenr = node->bytenr;
2990 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002991 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002992 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002993
Chris Mason5caf2a02007-04-02 11:20:42 -04002994 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002995 if (!path)
2996 return -ENOMEM;
2997
David Sterbae4058b52015-11-27 16:31:35 +01002998 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002999 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000
3001 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3002 BUG_ON(!is_data && refs_to_drop != 1);
3003
Josef Bacik3173a182013-03-07 14:22:04 -05003004 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02003005 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05003006
Nikolay Borisovfbe48012018-06-20 15:48:52 +03003007 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
3008 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003009 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003010 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003011 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 while (extent_slot >= 0) {
3013 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003014 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003016 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003017 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3018 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003019 found_extent = 1;
3020 break;
3021 }
Josef Bacik3173a182013-03-07 14:22:04 -05003022 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3023 key.offset == owner_objectid) {
3024 found_extent = 1;
3025 break;
3026 }
Chris Mason952fcca2008-02-18 16:33:44 -05003027 if (path->slots[0] - extent_slot > 5)
3028 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003029 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003030 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003031
Zheng Yan31840ae2008-09-23 13:14:14 -04003032 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003033 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003034 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003035 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003036 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003037 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003038 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003039 goto out;
3040 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003041 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003042 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003043
3044 key.objectid = bytenr;
3045 key.type = BTRFS_EXTENT_ITEM_KEY;
3046 key.offset = num_bytes;
3047
Josef Bacik3173a182013-03-07 14:22:04 -05003048 if (!is_data && skinny_metadata) {
3049 key.type = BTRFS_METADATA_ITEM_KEY;
3050 key.offset = owner_objectid;
3051 }
3052
Zheng Yan31840ae2008-09-23 13:14:14 -04003053 ret = btrfs_search_slot(trans, extent_root,
3054 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003055 if (ret > 0 && skinny_metadata && path->slots[0]) {
3056 /*
3057 * Couldn't find our skinny metadata item,
3058 * see if we have ye olde extent item.
3059 */
3060 path->slots[0]--;
3061 btrfs_item_key_to_cpu(path->nodes[0], &key,
3062 path->slots[0]);
3063 if (key.objectid == bytenr &&
3064 key.type == BTRFS_EXTENT_ITEM_KEY &&
3065 key.offset == num_bytes)
3066 ret = 0;
3067 }
3068
3069 if (ret > 0 && skinny_metadata) {
3070 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003071 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003072 key.type = BTRFS_EXTENT_ITEM_KEY;
3073 key.offset = num_bytes;
3074 btrfs_release_path(path);
3075 ret = btrfs_search_slot(trans, extent_root,
3076 &key, path, -1, 1);
3077 }
3078
Josef Bacikf3465ca2008-11-12 14:19:50 -05003079 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003080 btrfs_err(info,
3081 "umm, got %d back from search, was looking for %llu",
3082 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003083 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003084 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003085 }
David Sterba005d6422012-09-18 07:52:32 -06003086 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003087 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003088 goto out;
3089 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003090 extent_slot = path->slots[0];
3091 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303092 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003093 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003094 btrfs_err(info,
3095 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003096 bytenr, parent, root_objectid, owner_objectid,
3097 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003098 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003099 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003101 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003102 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003103 }
Chris Mason5f39d392007-10-15 16:14:19 -04003104
3105 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003106 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003107 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003108 ret = -EINVAL;
3109 btrfs_print_v0_err(info);
3110 btrfs_abort_transaction(trans, ret);
3111 goto out;
3112 }
Chris Mason952fcca2008-02-18 16:33:44 -05003113 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003114 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003115 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3116 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003117 struct btrfs_tree_block_info *bi;
3118 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3119 bi = (struct btrfs_tree_block_info *)(ei + 1);
3120 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003121 }
3122
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003124 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003125 btrfs_err(info,
3126 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
3127 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04003128 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04003129 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04003130 goto out;
3131 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132 refs -= refs_to_drop;
3133
3134 if (refs > 0) {
3135 if (extent_op)
3136 __run_delayed_extent_op(extent_op, leaf, ei);
3137 /*
3138 * In the case of inline back ref, reference count will
3139 * be updated by remove_extent_backref
3140 */
3141 if (iref) {
3142 BUG_ON(!found_extent);
3143 } else {
3144 btrfs_set_extent_refs(leaf, ei, refs);
3145 btrfs_mark_buffer_dirty(leaf);
3146 }
3147 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003148 ret = remove_extent_backref(trans, path, iref,
3149 refs_to_drop, is_data,
3150 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003151 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003152 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003153 goto out;
3154 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003155 }
3156 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003157 if (found_extent) {
3158 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08003159 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003160 if (iref) {
3161 BUG_ON(path->slots[0] != extent_slot);
3162 } else {
3163 BUG_ON(path->slots[0] != extent_slot + 1);
3164 path->slots[0] = extent_slot;
3165 num_to_del = 2;
3166 }
Chris Mason78fae272007-03-25 11:35:08 -04003167 }
Chris Masonb9473432009-03-13 11:00:37 -04003168
Josef Bacikfcebe452014-05-13 17:30:47 -07003169 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003170 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3171 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003172 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003173 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003174 goto out;
3175 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003176 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003178 if (is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00003179 ret = btrfs_del_csums(trans, info->csum_root, bytenr,
3180 num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003181 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003182 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003183 goto out;
3184 }
Chris Mason459931e2008-12-10 09:10:46 -05003185 }
3186
Nikolay Borisove7355e52018-05-10 15:44:55 +03003187 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003188 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003189 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003190 goto out;
3191 }
3192
Josef Bacikade4b512019-06-20 15:38:01 -04003193 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003194 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003195 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003196 goto out;
3197 }
Chris Masona28ec192007-03-06 20:08:01 -05003198 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003199 btrfs_release_path(path);
3200
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003201out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003202 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003203 return ret;
3204}
3205
3206/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003207 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003208 * delayed ref for that extent as well. This searches the delayed ref tree for
3209 * a given extent, and if there are no other delayed refs to be processed, it
3210 * removes it from the tree.
3211 */
3212static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003213 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003214{
3215 struct btrfs_delayed_ref_head *head;
3216 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003217 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003218
3219 delayed_refs = &trans->transaction->delayed_refs;
3220 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003221 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003222 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003223 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003224
Josef Bacikd7df2c72014-01-23 09:21:38 -05003225 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003226 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003227 goto out;
3228
Josef Bacikbedc66172018-12-03 10:20:31 -05003229 if (cleanup_extent_op(head) != NULL)
3230 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003231
Chris Mason1887be62009-03-13 10:11:24 -04003232 /*
3233 * waiting for the lock here would deadlock. If someone else has it
3234 * locked they are already in the process of dropping it anyway
3235 */
3236 if (!mutex_trylock(&head->mutex))
3237 goto out;
3238
Josef Bacikd7baffd2018-12-03 10:20:29 -05003239 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003240 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003241
Josef Bacikd7df2c72014-01-23 09:21:38 -05003242 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003243 spin_unlock(&delayed_refs->lock);
3244
Yan, Zhengf0486c62010-05-16 10:46:25 -04003245 BUG_ON(head->extent_op);
3246 if (head->must_insert_reserved)
3247 ret = 1;
3248
Josef Bacik31890da2018-11-21 14:05:41 -05003249 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003250 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003251 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003252 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003253out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003254 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003255
3256out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003257 spin_unlock(&delayed_refs->lock);
3258 return 0;
3259}
3260
Yan, Zhengf0486c62010-05-16 10:46:25 -04003261void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3262 struct btrfs_root *root,
3263 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003264 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003265{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003266 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003267 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04003268 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003269 int ret;
3270
Qu Wenruoed4f2552019-04-04 14:45:31 +08003271 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3272 buf->start, buf->len, parent);
3273 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3274 root->root_key.objectid);
3275
Yan, Zhengf0486c62010-05-16 10:46:25 -04003276 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07003277 int old_ref_mod, new_ref_mod;
3278
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003279 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08003280 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003281 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003282 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07003283 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003284 }
3285
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003286 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
David Sterba32da53862019-10-29 19:20:18 +01003287 struct btrfs_block_group *cache;
Filipe Manana62198722015-01-06 20:18:45 +00003288
Yan, Zhengf0486c62010-05-16 10:46:25 -04003289 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003290 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003291 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003292 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003293 }
3294
Omar Sandoval4da8b762017-06-06 16:45:28 -07003295 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003296 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003297
Yan, Zhengf0486c62010-05-16 10:46:25 -04003298 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
David Sterbafdf08602019-03-20 12:12:32 +01003299 pin_down_extent(cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003300 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003301 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003302 }
3303
3304 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3305
3306 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003307 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003308 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003309 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003310 }
3311out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04003312 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08003313 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003314
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003315 if (last_ref) {
3316 /*
3317 * Deleting the buffer, clear the corrupt flag since it doesn't
3318 * matter anymore.
3319 */
3320 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3321 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003322}
3323
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003324/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003325int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003326{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003327 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07003328 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04003329 int ret;
3330
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003331 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003332 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003333
Chris Mason56bec292009-03-13 10:10:06 -04003334 /*
3335 * tree log blocks never actually go into the extent allocation
3336 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003337 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003338 if ((ref->type == BTRFS_REF_METADATA &&
3339 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3340 (ref->type == BTRFS_REF_DATA &&
3341 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003342 /* unlocks the pinned mutex */
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02003343 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07003344 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04003345 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003346 } else if (ref->type == BTRFS_REF_METADATA) {
3347 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003348 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003349 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003350 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07003351 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04003352 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003353
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003354 if (!((ref->type == BTRFS_REF_METADATA &&
3355 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3356 (ref->type == BTRFS_REF_DATA &&
3357 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3358 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003359
Qu Wenruoddf30cf2019-04-04 14:45:34 +08003360 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08003361 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07003362
Chris Mason925baed2008-06-25 16:01:30 -04003363 return ret;
3364}
3365
Josef Bacik817d52f2009-07-13 21:29:25 -04003366enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003367 LOOP_CACHING_NOWAIT,
3368 LOOP_CACHING_WAIT,
3369 LOOP_ALLOC_CHUNK,
3370 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003371};
3372
Miao Xiee570fd22014-06-19 10:42:50 +08003373static inline void
David Sterba32da53862019-10-29 19:20:18 +01003374btrfs_lock_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003375 int delalloc)
3376{
3377 if (delalloc)
3378 down_read(&cache->data_rwsem);
3379}
3380
David Sterba32da53862019-10-29 19:20:18 +01003381static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003382 int delalloc)
3383{
3384 btrfs_get_block_group(cache);
3385 if (delalloc)
3386 down_read(&cache->data_rwsem);
3387}
3388
David Sterba32da53862019-10-29 19:20:18 +01003389static struct btrfs_block_group *btrfs_lock_cluster(
3390 struct btrfs_block_group *block_group,
Miao Xiee570fd22014-06-19 10:42:50 +08003391 struct btrfs_free_cluster *cluster,
3392 int delalloc)
3393{
David Sterba32da53862019-10-29 19:20:18 +01003394 struct btrfs_block_group *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003395
Miao Xiee570fd22014-06-19 10:42:50 +08003396 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003397 while (1) {
3398 used_bg = cluster->block_group;
3399 if (!used_bg)
3400 return NULL;
3401
3402 if (used_bg == block_group)
3403 return used_bg;
3404
3405 btrfs_get_block_group(used_bg);
3406
3407 if (!delalloc)
3408 return used_bg;
3409
3410 if (down_read_trylock(&used_bg->data_rwsem))
3411 return used_bg;
3412
3413 spin_unlock(&cluster->refill_lock);
3414
Liu Boe321f8a2016-11-30 16:11:04 -08003415 /* We should only have one-level nested. */
3416 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003417
3418 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003419 if (used_bg == cluster->block_group)
3420 return used_bg;
3421
3422 up_read(&used_bg->data_rwsem);
3423 btrfs_put_block_group(used_bg);
3424 }
Miao Xiee570fd22014-06-19 10:42:50 +08003425}
3426
3427static inline void
David Sterba32da53862019-10-29 19:20:18 +01003428btrfs_release_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003429 int delalloc)
3430{
3431 if (delalloc)
3432 up_read(&cache->data_rwsem);
3433 btrfs_put_block_group(cache);
3434}
3435
Josef Bacik817d52f2009-07-13 21:29:25 -04003436/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08003437 * Structure used internally for find_free_extent() function. Wraps needed
3438 * parameters.
3439 */
3440struct find_free_extent_ctl {
3441 /* Basic allocation info */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003442 u64 num_bytes;
3443 u64 empty_size;
3444 u64 flags;
3445 int delalloc;
3446
3447 /* Where to start the search inside the bg */
3448 u64 search_start;
3449
3450 /* For clustered allocation */
3451 u64 empty_cluster;
3452
3453 bool have_caching_bg;
3454 bool orig_have_caching_bg;
3455
3456 /* RAID index, converted from flags */
3457 int index;
3458
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003459 /*
3460 * Current loop number, check find_free_extent_update_loop() for details
3461 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003462 int loop;
3463
3464 /*
3465 * Whether we're refilling a cluster, if true we need to re-search
3466 * current block group but don't try to refill the cluster again.
3467 */
3468 bool retry_clustered;
3469
3470 /*
3471 * Whether we're updating free space cache, if true we need to re-search
3472 * current block group but don't try updating free space cache again.
3473 */
3474 bool retry_unclustered;
3475
3476 /* If current block group is cached */
3477 int cached;
3478
3479 /* Max contiguous hole found */
3480 u64 max_extent_size;
3481
3482 /* Total free space from free space cache, not always contiguous */
3483 u64 total_free_space;
3484
3485 /* Found result */
3486 u64 found_offset;
3487};
3488
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003489
3490/*
3491 * Helper function for find_free_extent().
3492 *
3493 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3494 * Return -EAGAIN to inform caller that we need to re-search this block group
3495 * Return >0 to inform caller that we find nothing
3496 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3497 */
David Sterba32da53862019-10-29 19:20:18 +01003498static int find_free_extent_clustered(struct btrfs_block_group *bg,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003499 struct btrfs_free_cluster *last_ptr,
3500 struct find_free_extent_ctl *ffe_ctl,
David Sterba32da53862019-10-29 19:20:18 +01003501 struct btrfs_block_group **cluster_bg_ret)
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003502{
David Sterba32da53862019-10-29 19:20:18 +01003503 struct btrfs_block_group *cluster_bg;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003504 u64 aligned_cluster;
3505 u64 offset;
3506 int ret;
3507
3508 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3509 if (!cluster_bg)
3510 goto refill_cluster;
3511 if (cluster_bg != bg && (cluster_bg->ro ||
3512 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3513 goto release_cluster;
3514
3515 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
David Sterbab3470b52019-10-23 18:48:22 +02003516 ffe_ctl->num_bytes, cluster_bg->start,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003517 &ffe_ctl->max_extent_size);
3518 if (offset) {
3519 /* We have a block, we're done */
3520 spin_unlock(&last_ptr->refill_lock);
3521 trace_btrfs_reserve_extent_cluster(cluster_bg,
3522 ffe_ctl->search_start, ffe_ctl->num_bytes);
3523 *cluster_bg_ret = cluster_bg;
3524 ffe_ctl->found_offset = offset;
3525 return 0;
3526 }
3527 WARN_ON(last_ptr->block_group != cluster_bg);
3528
3529release_cluster:
3530 /*
3531 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3532 * lets just skip it and let the allocator find whatever block it can
3533 * find. If we reach this point, we will have tried the cluster
3534 * allocator plenty of times and not have found anything, so we are
3535 * likely way too fragmented for the clustering stuff to find anything.
3536 *
3537 * However, if the cluster is taken from the current block group,
3538 * release the cluster first, so that we stand a better chance of
3539 * succeeding in the unclustered allocation.
3540 */
3541 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3542 spin_unlock(&last_ptr->refill_lock);
3543 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3544 return -ENOENT;
3545 }
3546
3547 /* This cluster didn't work out, free it and start over */
3548 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3549
3550 if (cluster_bg != bg)
3551 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3552
3553refill_cluster:
3554 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3555 spin_unlock(&last_ptr->refill_lock);
3556 return -ENOENT;
3557 }
3558
3559 aligned_cluster = max_t(u64,
3560 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3561 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003562 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3563 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003564 if (ret == 0) {
3565 /* Now pull our allocation out of this cluster */
3566 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3567 ffe_ctl->num_bytes, ffe_ctl->search_start,
3568 &ffe_ctl->max_extent_size);
3569 if (offset) {
3570 /* We found one, proceed */
3571 spin_unlock(&last_ptr->refill_lock);
3572 trace_btrfs_reserve_extent_cluster(bg,
3573 ffe_ctl->search_start,
3574 ffe_ctl->num_bytes);
3575 ffe_ctl->found_offset = offset;
3576 return 0;
3577 }
3578 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3579 !ffe_ctl->retry_clustered) {
3580 spin_unlock(&last_ptr->refill_lock);
3581
3582 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003583 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003584 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3585 return -EAGAIN;
3586 }
3587 /*
3588 * At this point we either didn't find a cluster or we weren't able to
3589 * allocate a block from our cluster. Free the cluster we've been
3590 * trying to use, and go to the next block group.
3591 */
3592 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3593 spin_unlock(&last_ptr->refill_lock);
3594 return 1;
3595}
3596
Qu Wenruob4bd7452018-11-02 09:39:47 +08003597/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003598 * Return >0 to inform caller that we find nothing
3599 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3600 * Return -EAGAIN to inform caller that we need to re-search this block group
3601 */
David Sterba32da53862019-10-29 19:20:18 +01003602static int find_free_extent_unclustered(struct btrfs_block_group *bg,
Qu Wenruoe1a41842018-11-02 09:39:49 +08003603 struct btrfs_free_cluster *last_ptr,
3604 struct find_free_extent_ctl *ffe_ctl)
3605{
3606 u64 offset;
3607
3608 /*
3609 * We are doing an unclustered allocation, set the fragmented flag so
3610 * we don't bother trying to setup a cluster again until we get more
3611 * space.
3612 */
3613 if (unlikely(last_ptr)) {
3614 spin_lock(&last_ptr->lock);
3615 last_ptr->fragmented = 1;
3616 spin_unlock(&last_ptr->lock);
3617 }
3618 if (ffe_ctl->cached) {
3619 struct btrfs_free_space_ctl *free_space_ctl;
3620
3621 free_space_ctl = bg->free_space_ctl;
3622 spin_lock(&free_space_ctl->tree_lock);
3623 if (free_space_ctl->free_space <
3624 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3625 ffe_ctl->empty_size) {
3626 ffe_ctl->total_free_space = max_t(u64,
3627 ffe_ctl->total_free_space,
3628 free_space_ctl->free_space);
3629 spin_unlock(&free_space_ctl->tree_lock);
3630 return 1;
3631 }
3632 spin_unlock(&free_space_ctl->tree_lock);
3633 }
3634
3635 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3636 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3637 &ffe_ctl->max_extent_size);
3638
3639 /*
3640 * If we didn't find a chunk, and we haven't failed on this block group
3641 * before, and this block group is in the middle of caching and we are
3642 * ok with waiting, then go ahead and wait for progress to be made, and
3643 * set @retry_unclustered to true.
3644 *
3645 * If @retry_unclustered is true then we've already waited on this
3646 * block group once and should move on to the next block group.
3647 */
3648 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3649 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003650 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3651 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003652 ffe_ctl->retry_unclustered = true;
3653 return -EAGAIN;
3654 } else if (!offset) {
3655 return 1;
3656 }
3657 ffe_ctl->found_offset = offset;
3658 return 0;
3659}
3660
3661/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003662 * Return >0 means caller needs to re-search for free extent
3663 * Return 0 means we have the needed free extent.
3664 * Return <0 means we failed to locate any free extent.
3665 */
3666static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
3667 struct btrfs_free_cluster *last_ptr,
3668 struct btrfs_key *ins,
3669 struct find_free_extent_ctl *ffe_ctl,
3670 int full_search, bool use_cluster)
3671{
3672 struct btrfs_root *root = fs_info->extent_root;
3673 int ret;
3674
3675 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3676 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3677 ffe_ctl->orig_have_caching_bg = true;
3678
3679 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3680 ffe_ctl->have_caching_bg)
3681 return 1;
3682
3683 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3684 return 1;
3685
3686 if (ins->objectid) {
3687 if (!use_cluster && last_ptr) {
3688 spin_lock(&last_ptr->lock);
3689 last_ptr->window_start = ins->objectid;
3690 spin_unlock(&last_ptr->lock);
3691 }
3692 return 0;
3693 }
3694
3695 /*
3696 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3697 * caching kthreads as we move along
3698 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3699 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3700 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3701 * again
3702 */
3703 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3704 ffe_ctl->index = 0;
3705 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3706 /*
3707 * We want to skip the LOOP_CACHING_WAIT step if we
3708 * don't have any uncached bgs and we've already done a
3709 * full search through.
3710 */
3711 if (ffe_ctl->orig_have_caching_bg || !full_search)
3712 ffe_ctl->loop = LOOP_CACHING_WAIT;
3713 else
3714 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3715 } else {
3716 ffe_ctl->loop++;
3717 }
3718
3719 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3720 struct btrfs_trans_handle *trans;
3721 int exist = 0;
3722
3723 trans = current->journal_info;
3724 if (trans)
3725 exist = 1;
3726 else
3727 trans = btrfs_join_transaction(root);
3728
3729 if (IS_ERR(trans)) {
3730 ret = PTR_ERR(trans);
3731 return ret;
3732 }
3733
Josef Bacikfc471cb2019-06-18 16:09:17 -04003734 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3735 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003736
3737 /*
3738 * If we can't allocate a new chunk we've already looped
3739 * through at least once, move on to the NO_EMPTY_SIZE
3740 * case.
3741 */
3742 if (ret == -ENOSPC)
3743 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3744
3745 /* Do not bail out on ENOSPC since we can do more. */
3746 if (ret < 0 && ret != -ENOSPC)
3747 btrfs_abort_transaction(trans, ret);
3748 else
3749 ret = 0;
3750 if (!exist)
3751 btrfs_end_transaction(trans);
3752 if (ret)
3753 return ret;
3754 }
3755
3756 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
3757 /*
3758 * Don't loop again if we already have no empty_size and
3759 * no empty_cluster.
3760 */
3761 if (ffe_ctl->empty_size == 0 &&
3762 ffe_ctl->empty_cluster == 0)
3763 return -ENOSPC;
3764 ffe_ctl->empty_size = 0;
3765 ffe_ctl->empty_cluster = 0;
3766 }
3767 return 1;
3768 }
3769 return -ENOSPC;
3770}
3771
3772/*
Chris Masonfec577f2007-02-26 10:40:21 -05003773 * walks the btree of allocated extents and find a hole of a given size.
3774 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003775 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003776 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003777 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003778 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003779 *
3780 * If there is no suitable free space, we will record the max size of
3781 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003782 *
3783 * The overall logic and call chain:
3784 *
3785 * find_free_extent()
3786 * |- Iterate through all block groups
3787 * | |- Get a valid block group
3788 * | |- Try to do clustered allocation in that block group
3789 * | |- Try to do unclustered allocation in that block group
3790 * | |- Check if the result is valid
3791 * | | |- If valid, then exit
3792 * | |- Jump to next block group
3793 * |
3794 * |- Push harder to find free extents
3795 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003796 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003797static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003798 u64 ram_bytes, u64 num_bytes, u64 empty_size,
3799 u64 hint_byte, struct btrfs_key *ins,
3800 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003801{
Josef Bacik80eb2342008-10-29 14:49:05 -04003802 int ret = 0;
Josef Bacikdb8fe642019-11-19 13:59:00 -05003803 int cache_block_group_error = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003804 struct btrfs_free_cluster *last_ptr = NULL;
David Sterba32da53862019-10-29 19:20:18 +01003805 struct btrfs_block_group *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003806 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003807 struct btrfs_space_info *space_info;
Josef Bacik67377732010-09-16 16:19:09 -04003808 bool use_cluster = true;
Josef Bacika5e681d2015-10-01 14:54:10 -04003809 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003810
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003811 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003812
Qu Wenruob4bd7452018-11-02 09:39:47 +08003813 ffe_ctl.num_bytes = num_bytes;
3814 ffe_ctl.empty_size = empty_size;
3815 ffe_ctl.flags = flags;
3816 ffe_ctl.search_start = 0;
3817 ffe_ctl.retry_clustered = false;
3818 ffe_ctl.retry_unclustered = false;
3819 ffe_ctl.delalloc = delalloc;
3820 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
3821 ffe_ctl.have_caching_bg = false;
3822 ffe_ctl.orig_have_caching_bg = false;
3823 ffe_ctl.found_offset = 0;
3824
David Sterba962a2982014-06-04 18:41:45 +02003825 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04003826 ins->objectid = 0;
3827 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003828
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003829 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05003830
Josef Bacik280c29082019-06-18 16:09:19 -04003831 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003832 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003833 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003834 return -ENOSPC;
3835 }
Josef Bacik2552d172009-04-03 10:14:19 -04003836
Josef Bacik67377732010-09-16 16:19:09 -04003837 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04003838 * If our free space is heavily fragmented we may not be able to make
3839 * big contiguous allocations, so instead of doing the expensive search
3840 * for free space, simply return ENOSPC with our max_extent_size so we
3841 * can go ahead and search for a more manageable chunk.
3842 *
3843 * If our max_extent_size is large enough for our allocation simply
3844 * disable clustering since we will likely not be able to find enough
3845 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04003846 */
Josef Bacik4f4db212015-09-29 11:40:47 -04003847 if (unlikely(space_info->max_extent_size)) {
3848 spin_lock(&space_info->lock);
3849 if (space_info->max_extent_size &&
3850 num_bytes > space_info->max_extent_size) {
3851 ins->offset = space_info->max_extent_size;
3852 spin_unlock(&space_info->lock);
3853 return -ENOSPC;
3854 } else if (space_info->max_extent_size) {
3855 use_cluster = false;
3856 }
3857 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003858 }
3859
Qu Wenruob4bd7452018-11-02 09:39:47 +08003860 last_ptr = fetch_cluster_info(fs_info, space_info,
3861 &ffe_ctl.empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04003862 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003863 spin_lock(&last_ptr->lock);
3864 if (last_ptr->block_group)
3865 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003866 if (last_ptr->fragmented) {
3867 /*
3868 * We still set window_start so we can keep track of the
3869 * last place we found an allocation to try and save
3870 * some time.
3871 */
3872 hint_byte = last_ptr->window_start;
3873 use_cluster = false;
3874 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003875 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003876 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003877
Qu Wenruob4bd7452018-11-02 09:39:47 +08003878 ffe_ctl.search_start = max(ffe_ctl.search_start,
3879 first_logical_byte(fs_info, 0));
3880 ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte);
3881 if (ffe_ctl.search_start == hint_byte) {
3882 block_group = btrfs_lookup_block_group(fs_info,
3883 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003884 /*
3885 * we don't want to use the block group if it doesn't match our
3886 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05003887 *
3888 * However if we are re-searching with an ideal block group
3889 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04003890 */
David Sterbab6919a52013-04-29 13:39:40 +00003891 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05003892 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04003893 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003894 if (list_empty(&block_group->list) ||
3895 block_group->ro) {
3896 /*
3897 * someone is removing this block group,
3898 * we can't jump into the have_block_group
3899 * target because our list pointers are not
3900 * valid
3901 */
3902 btrfs_put_block_group(block_group);
3903 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05003904 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003905 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08003906 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08003907 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04003908 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05003909 }
Josef Bacik2552d172009-04-03 10:14:19 -04003910 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003911 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003912 }
Chris Mason42e70e72008-11-07 18:17:11 -05003913 }
Josef Bacik2552d172009-04-03 10:14:19 -04003914search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08003915 ffe_ctl.have_caching_bg = false;
3916 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
3917 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04003918 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04003919 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003920 list_for_each_entry(block_group,
3921 &space_info->block_groups[ffe_ctl.index], list) {
Jeff Mahoney14443932017-07-19 23:25:51 -04003922 /* If the block group is read-only, we can skip it entirely. */
3923 if (unlikely(block_group->ro))
3924 continue;
3925
Miao Xiee570fd22014-06-19 10:42:50 +08003926 btrfs_grab_block_group(block_group, delalloc);
David Sterbab3470b52019-10-23 18:48:22 +02003927 ffe_ctl.search_start = block_group->start;
Chris Mason42e70e72008-11-07 18:17:11 -05003928
Chris Mason83a50de2010-12-13 15:06:46 -05003929 /*
3930 * this can happen if we end up cycling through all the
3931 * raid types, but we want to make sure we only allocate
3932 * for the proper type.
3933 */
David Sterbab6919a52013-04-29 13:39:40 +00003934 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07003935 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02003936 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02003937 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05003938 BTRFS_BLOCK_GROUP_RAID10;
3939
3940 /*
3941 * if they asked for extra copies and this block group
3942 * doesn't provide them, bail. This does allow us to
3943 * fill raid0 from raid1.
3944 */
David Sterbab6919a52013-04-29 13:39:40 +00003945 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05003946 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08003947
3948 /*
3949 * This block group has different flags than we want.
3950 * It's possible that we have MIXED_GROUP flag but no
3951 * block group is mixed. Just skip such block group.
3952 */
3953 btrfs_release_block_group(block_group, delalloc);
3954 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05003955 }
3956
Josef Bacik2552d172009-04-03 10:14:19 -04003957have_block_group:
David Sterba32da53862019-10-29 19:20:18 +01003958 ffe_ctl.cached = btrfs_block_group_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003959 if (unlikely(!ffe_ctl.cached)) {
3960 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003961 ret = btrfs_cache_block_group(block_group, 0);
Josef Bacikdb8fe642019-11-19 13:59:00 -05003962
3963 /*
3964 * If we get ENOMEM here or something else we want to
3965 * try other block groups, because it may not be fatal.
3966 * However if we can't find anything else we need to
3967 * save our return here so that we return the actual
3968 * error that caused problems, not ENOSPC.
3969 */
3970 if (ret < 0) {
3971 if (!cache_block_group_error)
3972 cache_block_group_error = ret;
3973 ret = 0;
3974 goto loop;
3975 }
Chris Mason1d4284b2012-03-28 20:31:37 -04003976 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05003977 }
3978
Josef Bacik36cce922013-08-05 11:15:21 -04003979 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
3980 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003981
Josef Bacik0a243252009-09-11 16:11:20 -04003982 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05003983 * Ok we want to try and use the cluster allocator, so
3984 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04003985 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04003986 if (last_ptr && use_cluster) {
David Sterba32da53862019-10-29 19:20:18 +01003987 struct btrfs_block_group *cluster_bg = NULL;
Chris Mason44fb5512009-06-04 15:34:51 -04003988
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003989 ret = find_free_extent_clustered(block_group, last_ptr,
3990 &ffe_ctl, &cluster_bg);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05003991
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003992 if (ret == 0) {
3993 if (cluster_bg && cluster_bg != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08003994 btrfs_release_block_group(block_group,
3995 delalloc);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003996 block_group = cluster_bg;
Miao Xie215a63d2014-01-15 20:00:56 +08003997 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003998 goto checks;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003999 } else if (ret == -EAGAIN) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004000 goto have_block_group;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08004001 } else if (ret > 0) {
4002 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004003 }
Qu Wenruod06e3bb2018-11-02 09:39:48 +08004004 /* ret == -ENOENT case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004005 }
4006
Qu Wenruoe1a41842018-11-02 09:39:49 +08004007 ret = find_free_extent_unclustered(block_group, last_ptr,
4008 &ffe_ctl);
4009 if (ret == -EAGAIN)
Josef Bacik817d52f2009-07-13 21:29:25 -04004010 goto have_block_group;
Qu Wenruoe1a41842018-11-02 09:39:49 +08004011 else if (ret > 0)
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004012 goto loop;
Qu Wenruoe1a41842018-11-02 09:39:49 +08004013 /* ret == 0 case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004014checks:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004015 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4016 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04004017
Josef Bacik2552d172009-04-03 10:14:19 -04004018 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004019 if (ffe_ctl.search_start + num_bytes >
David Sterbab3470b52019-10-23 18:48:22 +02004020 block_group->start + block_group->length) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004021 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4022 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004023 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004024 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004025
Qu Wenruob4bd7452018-11-02 09:39:47 +08004026 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4027 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4028 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004029
Wang Xiaoguang18513092016-07-25 15:51:40 +08004030 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4031 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004032 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004033 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4034 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004035 goto loop;
4036 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004037 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004038
Josef Bacik2552d172009-04-03 10:14:19 -04004039 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004040 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004041 ins->offset = num_bytes;
4042
Qu Wenruob4bd7452018-11-02 09:39:47 +08004043 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4044 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004045 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004046 break;
4047loop:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004048 ffe_ctl.retry_clustered = false;
4049 ffe_ctl.retry_unclustered = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004050 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
Qu Wenruob4bd7452018-11-02 09:39:47 +08004051 ffe_ctl.index);
Miao Xiee570fd22014-06-19 10:42:50 +08004052 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004053 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004054 }
4055 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004056
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004057 ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl,
4058 full_search, use_cluster);
4059 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004060 goto search;
4061
Josef Bacikdb8fe642019-11-19 13:59:00 -05004062 if (ret == -ENOSPC && !cache_block_group_error) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004063 /*
4064 * Use ffe_ctl->total_free_space as fallback if we can't find
4065 * any contiguous hole.
4066 */
4067 if (!ffe_ctl.max_extent_size)
4068 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004069 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004070 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004071 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004072 ins->offset = ffe_ctl.max_extent_size;
Josef Bacikdb8fe642019-11-19 13:59:00 -05004073 } else if (ret == -ENOSPC) {
4074 ret = cache_block_group_error;
Josef Bacik4f4db212015-09-29 11:40:47 -04004075 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004076 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004077}
Chris Masonec44a352008-04-28 15:29:52 -04004078
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004079/*
4080 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4081 * hole that is at least as big as @num_bytes.
4082 *
4083 * @root - The root that will contain this extent
4084 *
4085 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4086 * is used for accounting purposes. This value differs
4087 * from @num_bytes only in the case of compressed extents.
4088 *
4089 * @num_bytes - Number of bytes to allocate on-disk.
4090 *
4091 * @min_alloc_size - Indicates the minimum amount of space that the
4092 * allocator should try to satisfy. In some cases
4093 * @num_bytes may be larger than what is required and if
4094 * the filesystem is fragmented then allocation fails.
4095 * However, the presence of @min_alloc_size gives a
4096 * chance to try and satisfy the smaller allocation.
4097 *
4098 * @empty_size - A hint that you plan on doing more COW. This is the
4099 * size in bytes the allocator should try to find free
4100 * next to the block it returns. This is just a hint and
4101 * may be ignored by the allocator.
4102 *
4103 * @hint_byte - Hint to the allocator to start searching above the byte
4104 * address passed. It might be ignored.
4105 *
4106 * @ins - This key is modified to record the found hole. It will
4107 * have the following values:
4108 * ins->objectid == start position
4109 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4110 * ins->offset == the size of the hole.
4111 *
4112 * @is_data - Boolean flag indicating whether an extent is
4113 * allocated for data (true) or metadata (false)
4114 *
4115 * @delalloc - Boolean flag indicating whether this allocation is for
4116 * delalloc or not. If 'true' data_rwsem of block groups
4117 * is going to be acquired.
4118 *
4119 *
4120 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4121 * case -ENOSPC is returned then @ins->offset will contain the size of the
4122 * largest available hole the allocator managed to find.
4123 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004124int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004125 u64 num_bytes, u64 min_alloc_size,
4126 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004127 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004128{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004129 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004130 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004131 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004132 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004133
Jeff Mahoney1b868262017-05-17 11:38:35 -04004134 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004135again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004136 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004137 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004138 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004139 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004140 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004141 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004142 if (!final_tried && ins->offset) {
4143 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004144 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004145 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004146 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004147 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004148 if (num_bytes == min_alloc_size)
4149 final_tried = true;
4150 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004151 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004152 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004153
Josef Bacik280c29082019-06-18 16:09:19 -04004154 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004155 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004156 "allocation failed flags %llu, wanted %llu",
4157 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004158 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004159 btrfs_dump_space_info(fs_info, sinfo,
4160 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004161 }
Chris Mason925baed2008-06-25 16:01:30 -04004162 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004163
4164 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004165}
4166
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004167int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004168 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004169{
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004170 struct btrfs_block_group *cache;
4171
4172 cache = btrfs_lookup_block_group(fs_info, start);
4173 if (!cache) {
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004174 btrfs_err(fs_info, "Unable to find block group for %llu",
4175 start);
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004176 return -ENOSPC;
4177 }
4178
4179 btrfs_add_free_space(cache, start, len);
4180 btrfs_free_reserved_bytes(cache, len, delalloc);
4181 trace_btrfs_reserved_extent_free(fs_info, start, len);
4182
4183 btrfs_put_block_group(cache);
4184 return 0;
Chris Masone688b7252011-10-31 20:52:39 -04004185}
4186
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004187int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
4188 u64 len)
Chris Masone688b7252011-10-31 20:52:39 -04004189{
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004190 struct btrfs_block_group *cache;
4191 int ret = 0;
4192
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004193 cache = btrfs_lookup_block_group(trans->fs_info, start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004194 if (!cache) {
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004195 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4196 start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004197 return -ENOSPC;
4198 }
4199
4200 ret = pin_down_extent(cache, start, len, 1);
4201 btrfs_put_block_group(cache);
4202 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04004203}
4204
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004205static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004206 u64 parent, u64 root_objectid,
4207 u64 flags, u64 owner, u64 offset,
4208 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004209{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004210 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004211 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004213 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004214 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004215 struct extent_buffer *leaf;
4216 int type;
4217 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004218
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004219 if (parent > 0)
4220 type = BTRFS_SHARED_DATA_REF_KEY;
4221 else
4222 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004224 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004225
4226 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004227 if (!path)
4228 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004229
Chris Masonb9473432009-03-13 11:00:37 -04004230 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004231 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4232 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 if (ret) {
4234 btrfs_free_path(path);
4235 return ret;
4236 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004237
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004238 leaf = path->nodes[0];
4239 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004240 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004241 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4242 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4243 btrfs_set_extent_flags(leaf, extent_item,
4244 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004246 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4247 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4248 if (parent > 0) {
4249 struct btrfs_shared_data_ref *ref;
4250 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4251 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4252 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4253 } else {
4254 struct btrfs_extent_data_ref *ref;
4255 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4256 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4257 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4258 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4259 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4260 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004261
4262 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004263 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004264
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004265 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004266 if (ret)
4267 return ret;
4268
Josef Bacikade4b512019-06-20 15:38:01 -04004269 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004271 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004272 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004273 BUG();
4274 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004275 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004276 return ret;
4277}
4278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004279static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004280 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004281 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004282{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004283 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004284 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004285 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004286 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004287 struct btrfs_tree_block_info *block_info;
4288 struct btrfs_extent_inline_ref *iref;
4289 struct btrfs_path *path;
4290 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004291 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004292 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004293 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004294 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004295 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004296
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004297 ref = btrfs_delayed_node_to_tree_ref(node);
4298
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004299 extent_key.objectid = node->bytenr;
4300 if (skinny_metadata) {
4301 extent_key.offset = ref->level;
4302 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4303 num_bytes = fs_info->nodesize;
4304 } else {
4305 extent_key.offset = node->num_bytes;
4306 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004307 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004308 num_bytes = node->num_bytes;
4309 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004310
4311 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004312 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004313 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004314
4315 path->leave_spinning = 1;
4316 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004317 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004318 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004319 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004320 return ret;
4321 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004322
4323 leaf = path->nodes[0];
4324 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4325 struct btrfs_extent_item);
4326 btrfs_set_extent_refs(leaf, extent_item, 1);
4327 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4328 btrfs_set_extent_flags(leaf, extent_item,
4329 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004330
Josef Bacik3173a182013-03-07 14:22:04 -05004331 if (skinny_metadata) {
4332 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4333 } else {
4334 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004335 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004336 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004337 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4338 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004339
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004340 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4342 btrfs_set_extent_inline_ref_type(leaf, iref,
4343 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004344 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004345 } else {
4346 btrfs_set_extent_inline_ref_type(leaf, iref,
4347 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004348 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004349 }
4350
4351 btrfs_mark_buffer_dirty(leaf);
4352 btrfs_free_path(path);
4353
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004354 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4355 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004356 if (ret)
4357 return ret;
4358
Josef Bacikade4b512019-06-20 15:38:01 -04004359 ret = btrfs_update_block_group(trans, extent_key.objectid,
4360 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004361 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004362 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004363 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004364 BUG();
4365 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004366
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004367 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004368 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004369 return ret;
4370}
4371
4372int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004373 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004374 u64 offset, u64 ram_bytes,
4375 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376{
Qu Wenruo76675592019-04-04 14:45:32 +08004377 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04004378 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004379
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004380 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004381
Qu Wenruo76675592019-04-04 14:45:32 +08004382 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4383 ins->objectid, ins->offset, 0);
4384 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004385 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08004386 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4387 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004388 return ret;
4389}
Chris Masone02119d2008-09-05 16:13:11 -04004390
4391/*
4392 * this is used by the tree logging recovery code. It records that
4393 * an extent has been allocated and makes sure to clear the free
4394 * space cache bits as well
4395 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004396int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004397 u64 root_objectid, u64 owner, u64 offset,
4398 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004399{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004400 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004401 int ret;
David Sterba32da53862019-10-29 19:20:18 +01004402 struct btrfs_block_group *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004403 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004404
4405 /*
4406 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004407 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004408 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004409 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004410 ret = __exclude_logged_extent(fs_info, ins->objectid,
4411 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004412 if (ret)
4413 return ret;
4414 }
Chris Masone02119d2008-09-05 16:13:11 -04004415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004416 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004417 if (!block_group)
4418 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004419
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004420 space_info = block_group->space_info;
4421 spin_lock(&space_info->lock);
4422 spin_lock(&block_group->lock);
4423 space_info->bytes_reserved += ins->offset;
4424 block_group->reserved += ins->offset;
4425 spin_unlock(&block_group->lock);
4426 spin_unlock(&space_info->lock);
4427
Nikolay Borisovef89b822018-06-20 15:48:58 +03004428 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4429 offset, ins, 1);
Josef Bacikbd727172020-02-13 10:47:30 -05004430 if (ret)
4431 btrfs_pin_extent(fs_info, ins->objectid, ins->offset, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004432 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004433 return ret;
4434}
4435
Eric Sandeen48a3b632013-04-25 20:41:01 +00004436static struct extent_buffer *
4437btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004438 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04004439{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004440 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004441 struct extent_buffer *buf;
4442
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004443 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004444 if (IS_ERR(buf))
4445 return buf;
4446
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004447 /*
4448 * Extra safety check in case the extent tree is corrupted and extent
4449 * allocator chooses to use a tree block which is already used and
4450 * locked.
4451 */
4452 if (buf->lock_owner == current->pid) {
4453 btrfs_err_rl(fs_info,
4454"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4455 buf->start, btrfs_header_owner(buf), current->pid);
4456 free_extent_buffer(buf);
4457 return ERR_PTR(-EUCLEAN);
4458 }
4459
Chris Mason85d4e462011-07-26 16:11:19 -04004460 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004461 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01004462 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004463 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004464
David Sterba8bead252018-04-04 02:03:48 +02004465 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004466 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004467
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004468 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4469 btrfs_set_header_level(buf, level);
4470 btrfs_set_header_bytenr(buf, buf->start);
4471 btrfs_set_header_generation(buf, trans->transid);
4472 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4473 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004474 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004475 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004476 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004477 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004478 /*
4479 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004480 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004481 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004482 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004483 set_extent_dirty(&root->dirty_log_pages, buf->start,
4484 buf->start + buf->len - 1, GFP_NOFS);
4485 else
4486 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004487 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004488 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004489 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004490 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4491 buf->start + buf->len - 1, GFP_NOFS);
4492 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004493 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004494 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004495 return buf;
4496}
4497
Chris Masonfec577f2007-02-26 10:40:21 -05004498/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004499 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004500 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004501 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004502struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004503 struct btrfs_root *root,
4504 u64 parent, u64 root_objectid,
4505 const struct btrfs_disk_key *key,
4506 int level, u64 hint,
4507 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004509 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004510 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004511 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004512 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004513 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004514 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004515 u64 flags = 0;
4516 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004517 u32 blocksize = fs_info->nodesize;
4518 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004519
David Sterba05653ef2016-07-15 15:23:37 +02004520#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004521 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004522 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004523 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004524 if (!IS_ERR(buf))
4525 root->alloc_bytenr += blocksize;
4526 return buf;
4527 }
David Sterba05653ef2016-07-15 15:23:37 +02004528#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004529
Josef Bacik67f9c222019-06-19 13:47:23 -04004530 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004531 if (IS_ERR(block_rsv))
4532 return ERR_CAST(block_rsv);
4533
Wang Xiaoguang18513092016-07-25 15:51:40 +08004534 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004535 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004536 if (ret)
4537 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004538
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004539 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4540 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08004541 if (IS_ERR(buf)) {
4542 ret = PTR_ERR(buf);
4543 goto out_free_reserved;
4544 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004545
4546 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4547 if (parent == 0)
4548 parent = ins.objectid;
4549 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4550 } else
4551 BUG_ON(parent > 0);
4552
4553 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004554 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004555 if (!extent_op) {
4556 ret = -ENOMEM;
4557 goto out_free_buf;
4558 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004559 if (key)
4560 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4561 else
4562 memset(&extent_op->key, 0, sizeof(extent_op->key));
4563 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004564 extent_op->update_key = skinny_metadata ? false : true;
4565 extent_op->update_flags = true;
4566 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004567 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004568
Qu Wenruoed4f2552019-04-04 14:45:31 +08004569 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4570 ins.objectid, ins.offset, parent);
4571 generic_ref.real_root = root->root_key.objectid;
4572 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004573 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004574 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07004575 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08004576 if (ret)
4577 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004578 }
Chris Masonfec577f2007-02-26 10:40:21 -05004579 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004580
4581out_free_delayed:
4582 btrfs_free_delayed_extent_op(extent_op);
4583out_free_buf:
4584 free_extent_buffer(buf);
4585out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004586 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004587out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004588 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004589 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004590}
Chris Masona28ec192007-03-06 20:08:01 -05004591
Yan Zheng2c47e6052009-06-27 21:07:35 -04004592struct walk_control {
4593 u64 refs[BTRFS_MAX_LEVEL];
4594 u64 flags[BTRFS_MAX_LEVEL];
4595 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004596 struct btrfs_key drop_progress;
4597 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004598 int stage;
4599 int level;
4600 int shared_level;
4601 int update_ref;
4602 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004603 int reada_slot;
4604 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004605 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004606};
4607
4608#define DROP_REFERENCE 1
4609#define UPDATE_BACKREF 2
4610
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004611static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4612 struct btrfs_root *root,
4613 struct walk_control *wc,
4614 struct btrfs_path *path)
4615{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004616 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004617 u64 bytenr;
4618 u64 generation;
4619 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004620 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004621 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004622 struct btrfs_key key;
4623 struct extent_buffer *eb;
4624 int ret;
4625 int slot;
4626 int nread = 0;
4627
4628 if (path->slots[wc->level] < wc->reada_slot) {
4629 wc->reada_count = wc->reada_count * 2 / 3;
4630 wc->reada_count = max(wc->reada_count, 2);
4631 } else {
4632 wc->reada_count = wc->reada_count * 3 / 2;
4633 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004635 }
4636
4637 eb = path->nodes[wc->level];
4638 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004639
4640 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4641 if (nread >= wc->reada_count)
4642 break;
4643
4644 cond_resched();
4645 bytenr = btrfs_node_blockptr(eb, slot);
4646 generation = btrfs_node_ptr_generation(eb, slot);
4647
4648 if (slot == path->slots[wc->level])
4649 goto reada;
4650
4651 if (wc->stage == UPDATE_BACKREF &&
4652 generation <= root->root_key.offset)
4653 continue;
4654
Yan, Zheng94fcca92009-10-09 09:25:16 -04004655 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004656 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004657 wc->level - 1, 1, &refs,
4658 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004659 /* We don't care about errors in readahead. */
4660 if (ret < 0)
4661 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004662 BUG_ON(refs == 0);
4663
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004664 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004665 if (refs == 1)
4666 goto reada;
4667
Yan, Zheng94fcca92009-10-09 09:25:16 -04004668 if (wc->level == 1 &&
4669 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4670 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004671 if (!wc->update_ref ||
4672 generation <= root->root_key.offset)
4673 continue;
4674 btrfs_node_key_to_cpu(eb, &key, slot);
4675 ret = btrfs_comp_cpu_keys(&key,
4676 &wc->update_progress);
4677 if (ret < 0)
4678 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004679 } else {
4680 if (wc->level == 1 &&
4681 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4682 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004683 }
4684reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004685 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004686 nread++;
4687 }
4688 wc->reada_slot = slot;
4689}
4690
Chris Mason9aca1d52007-03-13 11:09:37 -04004691/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004692 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004693 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004694 * when wc->stage == UPDATE_BACKREF, this function updates
4695 * back refs for pointers in the block.
4696 *
4697 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004698 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004699static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4700 struct btrfs_root *root,
4701 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004702 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004703{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004704 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004705 int level = wc->level;
4706 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004707 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4708 int ret;
4709
4710 if (wc->stage == UPDATE_BACKREF &&
4711 btrfs_header_owner(eb) != root->root_key.objectid)
4712 return 1;
4713
4714 /*
4715 * when reference count of tree block is 1, it won't increase
4716 * again. once full backref flag is set, we never clear it.
4717 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004718 if (lookup_info &&
4719 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4720 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004721 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004722 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004723 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004724 &wc->refs[level],
4725 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 BUG_ON(ret == -ENOMEM);
4727 if (ret)
4728 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004729 BUG_ON(wc->refs[level] == 0);
4730 }
4731
Yan Zheng2c47e6052009-06-27 21:07:35 -04004732 if (wc->stage == DROP_REFERENCE) {
4733 if (wc->refs[level] > 1)
4734 return 1;
4735
4736 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004737 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004738 path->locks[level] = 0;
4739 }
4740 return 0;
4741 }
4742
4743 /* wc->stage == UPDATE_BACKREF */
4744 if (!(wc->flags[level] & flag)) {
4745 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004746 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004747 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004748 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004749 BUG_ON(ret); /* -ENOMEM */
David Sterbaf5c8daa2019-03-20 11:43:36 +01004750 ret = btrfs_set_disk_extent_flags(trans, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004751 eb->len, flag,
4752 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004753 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004754 wc->flags[level] |= flag;
4755 }
4756
4757 /*
4758 * the block is shared by multiple trees, so it's not good to
4759 * keep the tree lock
4760 */
4761 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04004762 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004763 path->locks[level] = 0;
4764 }
4765 return 0;
4766}
4767
4768/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004769 * This is used to verify a ref exists for this root to deal with a bug where we
4770 * would have a drop_progress key that hadn't been updated properly.
4771 */
4772static int check_ref_exists(struct btrfs_trans_handle *trans,
4773 struct btrfs_root *root, u64 bytenr, u64 parent,
4774 int level)
4775{
4776 struct btrfs_path *path;
4777 struct btrfs_extent_inline_ref *iref;
4778 int ret;
4779
4780 path = btrfs_alloc_path();
4781 if (!path)
4782 return -ENOMEM;
4783
4784 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4785 root->fs_info->nodesize, parent,
4786 root->root_key.objectid, level, 0);
4787 btrfs_free_path(path);
4788 if (ret == -ENOENT)
4789 return 0;
4790 if (ret < 0)
4791 return ret;
4792 return 1;
4793}
4794
4795/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004796 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004797 *
4798 * when wc->stage == DROP_REFERENCE, this function checks
4799 * reference count of the block pointed to. if the block
4800 * is shared and we need update back refs for the subtree
4801 * rooted at the block, this function changes wc->stage to
4802 * UPDATE_BACKREF. if the block is shared and there is no
4803 * need to update back, this function drops the reference
4804 * to the block.
4805 *
4806 * NOTE: return value 1 means we should stop walking down.
4807 */
4808static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4809 struct btrfs_root *root,
4810 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004811 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004812{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004814 u64 bytenr;
4815 u64 generation;
4816 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004817 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004818 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004819 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004820 struct extent_buffer *next;
4821 int level = wc->level;
4822 int reada = 0;
4823 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004824 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004825
4826 generation = btrfs_node_ptr_generation(path->nodes[level],
4827 path->slots[level]);
4828 /*
4829 * if the lower level block was created before the snapshot
4830 * was created, we know there is no need to update back refs
4831 * for the subtree
4832 */
4833 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004834 generation <= root->root_key.offset) {
4835 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004836 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004837 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004838
4839 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004840 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4841 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004842
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004844 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004845 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004846 if (IS_ERR(next))
4847 return PTR_ERR(next);
4848
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004849 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4850 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004851 reada = 1;
4852 }
4853 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004854 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004855
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004856 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004857 &wc->refs[level - 1],
4858 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004859 if (ret < 0)
4860 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004861
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004862 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004863 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004864 ret = -EIO;
4865 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004866 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004867 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004868
Yan, Zheng94fcca92009-10-09 09:25:16 -04004869 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004870 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07004871 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004872 if (level == 1 &&
4873 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4874 goto skip;
4875
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004876 if (!wc->update_ref ||
4877 generation <= root->root_key.offset)
4878 goto skip;
4879
4880 btrfs_node_key_to_cpu(path->nodes[level], &key,
4881 path->slots[level]);
4882 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4883 if (ret < 0)
4884 goto skip;
4885
4886 wc->stage = UPDATE_BACKREF;
4887 wc->shared_level = level - 1;
4888 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004889 } else {
4890 if (level == 1 &&
4891 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4892 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004893 }
4894
Chris Masonb9fab912012-05-06 07:23:47 -04004895 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004896 btrfs_tree_unlock(next);
4897 free_extent_buffer(next);
4898 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004899 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004900 }
4901
4902 if (!next) {
4903 if (reada && level == 1)
4904 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08004905 next = read_tree_block(fs_info, bytenr, generation, level - 1,
4906 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08004907 if (IS_ERR(next)) {
4908 return PTR_ERR(next);
4909 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04004910 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004911 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04004912 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004913 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004914 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004915 }
4916
4917 level--;
Josef Bacik48672682016-09-23 13:23:28 +02004918 ASSERT(level == btrfs_header_level(next));
4919 if (level != btrfs_header_level(next)) {
4920 btrfs_err(root->fs_info, "mismatched level");
4921 ret = -EIO;
4922 goto out_unlock;
4923 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004924 path->nodes[level] = next;
4925 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004926 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004927 wc->level = level;
4928 if (wc->level == 1)
4929 wc->reada_slot = 0;
4930 return 0;
4931skip:
4932 wc->refs[level - 1] = 0;
4933 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004934 if (wc->stage == DROP_REFERENCE) {
4935 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4936 parent = path->nodes[level]->start;
4937 } else {
Josef Bacik48672682016-09-23 13:23:28 +02004938 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04004939 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02004940 if (root->root_key.objectid !=
4941 btrfs_header_owner(path->nodes[level])) {
4942 btrfs_err(root->fs_info,
4943 "mismatched block owner");
4944 ret = -EIO;
4945 goto out_unlock;
4946 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004947 parent = 0;
4948 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004949
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004950 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05004951 * If we had a drop_progress we need to verify the refs are set
4952 * as expected. If we find our ref then we know that from here
4953 * on out everything should be correct, and we can clear the
4954 * ->restarted flag.
4955 */
4956 if (wc->restarted) {
4957 ret = check_ref_exists(trans, root, bytenr, parent,
4958 level - 1);
4959 if (ret < 0)
4960 goto out_unlock;
4961 if (ret == 0)
4962 goto no_delete;
4963 ret = 0;
4964 wc->restarted = 0;
4965 }
4966
4967 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004968 * Reloc tree doesn't contribute to qgroup numbers, and we have
4969 * already accounted them at merge time (replace_path),
4970 * thus we could skip expensive subtree trace here.
4971 */
4972 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
4973 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08004974 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08004975 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07004976 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004977 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004978 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
4979 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07004980 }
4981 }
Josef Bacikaea6f022019-02-06 15:46:15 -05004982
4983 /*
4984 * We need to update the next key in our walk control so we can
4985 * update the drop_progress key accordingly. We don't care if
4986 * find_next_key doesn't find a key because that means we're at
4987 * the end and are going to clean up now.
4988 */
4989 wc->drop_level = level;
4990 find_next_key(path, level, &wc->drop_progress);
4991
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004992 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
4993 fs_info->nodesize, parent);
4994 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
4995 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02004996 if (ret)
4997 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004998 }
Josef Bacik78c52d92019-02-06 15:46:14 -05004999no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02005000 *lookup_info = 1;
5001 ret = 1;
5002
5003out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005004 btrfs_tree_unlock(next);
5005 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02005006
5007 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005008}
5009
5010/*
Liu Bo2c016dc2012-12-26 15:32:17 +08005011 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04005012 *
5013 * when wc->stage == DROP_REFERENCE, this function drops
5014 * reference count on the block.
5015 *
5016 * when wc->stage == UPDATE_BACKREF, this function changes
5017 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5018 * to UPDATE_BACKREF previously while processing the block.
5019 *
5020 * NOTE: return value 1 means we should stop walking up.
5021 */
5022static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5023 struct btrfs_root *root,
5024 struct btrfs_path *path,
5025 struct walk_control *wc)
5026{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005027 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005028 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005029 int level = wc->level;
5030 struct extent_buffer *eb = path->nodes[level];
5031 u64 parent = 0;
5032
5033 if (wc->stage == UPDATE_BACKREF) {
5034 BUG_ON(wc->shared_level < level);
5035 if (level < wc->shared_level)
5036 goto out;
5037
Yan Zheng2c47e6052009-06-27 21:07:35 -04005038 ret = find_next_key(path, level + 1, &wc->update_progress);
5039 if (ret > 0)
5040 wc->update_ref = 0;
5041
5042 wc->stage = DROP_REFERENCE;
5043 wc->shared_level = -1;
5044 path->slots[level] = 0;
5045
5046 /*
5047 * check reference count again if the block isn't locked.
5048 * we should start walking down the tree again if reference
5049 * count is one.
5050 */
5051 if (!path->locks[level]) {
5052 BUG_ON(level == 0);
5053 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005054 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005055 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005056
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005057 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005058 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005059 &wc->refs[level],
5060 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005061 if (ret < 0) {
5062 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005063 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005064 return ret;
5065 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005066 BUG_ON(wc->refs[level] == 0);
5067 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005068 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005069 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005070 return 1;
5071 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005072 }
5073 }
5074
5075 /* wc->stage == DROP_REFERENCE */
5076 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5077
5078 if (wc->refs[level] == 1) {
5079 if (level == 0) {
5080 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005081 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005082 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005083 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005084 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005085 if (is_fstree(root->root_key.objectid)) {
5086 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5087 if (ret) {
5088 btrfs_err_rl(fs_info,
5089 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005090 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005091 }
Mark Fasheh11526512014-07-17 12:39:01 -07005092 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005093 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005094 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005095 if (!path->locks[level] &&
5096 btrfs_header_generation(eb) == trans->transid) {
5097 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005098 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005099 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005100 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005101 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005102 }
5103
5104 if (eb == root->node) {
5105 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5106 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005107 else if (root->root_key.objectid != btrfs_header_owner(eb))
5108 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005109 } else {
5110 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5111 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005112 else if (root->root_key.objectid !=
5113 btrfs_header_owner(path->nodes[level + 1]))
5114 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005115 }
5116
Jan Schmidt5581a512012-05-16 17:04:52 +02005117 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005118out:
5119 wc->refs[level] = 0;
5120 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005121 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005122
5123owner_mismatch:
5124 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5125 btrfs_header_owner(eb), root->root_key.objectid);
5126 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005127}
5128
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005129static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5130 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005131 struct btrfs_path *path,
5132 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005133{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005134 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005135 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005136 int ret;
5137
Yan Zheng2c47e6052009-06-27 21:07:35 -04005138 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005139 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005140 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005141 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005142
Yan Zheng2c47e6052009-06-27 21:07:35 -04005143 if (level == 0)
5144 break;
5145
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005146 if (path->slots[level] >=
5147 btrfs_header_nritems(path->nodes[level]))
5148 break;
5149
Yan, Zheng94fcca92009-10-09 09:25:16 -04005150 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005151 if (ret > 0) {
5152 path->slots[level]++;
5153 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005154 } else if (ret < 0)
5155 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005156 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005157 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005158 return 0;
5159}
5160
Chris Masond3977122009-01-05 21:25:51 -05005161static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005162 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005163 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005164 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005165{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005166 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005167 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005168
Yan Zheng2c47e6052009-06-27 21:07:35 -04005169 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5170 while (level < max_level && path->nodes[level]) {
5171 wc->level = level;
5172 if (path->slots[level] + 1 <
5173 btrfs_header_nritems(path->nodes[level])) {
5174 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005175 return 0;
5176 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005177 ret = walk_up_proc(trans, root, path, wc);
5178 if (ret > 0)
5179 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005180 if (ret < 0)
5181 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005182
Yan Zheng2c47e6052009-06-27 21:07:35 -04005183 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005184 btrfs_tree_unlock_rw(path->nodes[level],
5185 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005186 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005187 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005188 free_extent_buffer(path->nodes[level]);
5189 path->nodes[level] = NULL;
5190 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005191 }
5192 }
5193 return 1;
5194}
5195
Chris Mason9aca1d52007-03-13 11:09:37 -04005196/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005197 * drop a subvolume tree.
5198 *
5199 * this function traverses the tree freeing any blocks that only
5200 * referenced by the tree.
5201 *
5202 * when a shared tree block is found. this function decreases its
5203 * reference count by one. if update_ref is true, this function
5204 * also make sure backrefs for the shared block and all lower level
5205 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005206 *
5207 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005208 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04005209int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005210 struct btrfs_block_rsv *block_rsv, int update_ref,
5211 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005212{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005213 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005214 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005215 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005216 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005217 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005218 struct walk_control *wc;
5219 struct btrfs_key key;
5220 int err = 0;
5221 int ret;
5222 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005223 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005224
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005225 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005226
Chris Mason5caf2a02007-04-02 11:20:42 -04005227 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005228 if (!path) {
5229 err = -ENOMEM;
5230 goto out;
5231 }
Chris Mason20524f02007-03-10 06:35:47 -05005232
Yan Zheng2c47e6052009-06-27 21:07:35 -04005233 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005234 if (!wc) {
5235 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005236 err = -ENOMEM;
5237 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005238 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005239
Yan, Zhenga22285a2010-05-16 10:48:46 -04005240 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005241 if (IS_ERR(trans)) {
5242 err = PTR_ERR(trans);
5243 goto out_free;
5244 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005245
Josef Bacik0568e822018-11-30 11:52:14 -05005246 err = btrfs_run_delayed_items(trans);
5247 if (err)
5248 goto out_end_trans;
5249
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005250 if (block_rsv)
5251 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005252
Josef Bacik83354f02018-11-30 11:52:13 -05005253 /*
5254 * This will help us catch people modifying the fs tree while we're
5255 * dropping it. It is unsafe to mess with the fs tree while it's being
5256 * dropped as we unlock the root node and parent nodes as we walk down
5257 * the tree, assuming nothing will change. If something does change
5258 * then we'll have stale information and drop references to blocks we've
5259 * already dropped.
5260 */
5261 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005262 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005263 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005264 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005265 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005266 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005267 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005268 memset(&wc->update_progress, 0,
5269 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005270 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005271 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005272 memcpy(&wc->update_progress, &key,
5273 sizeof(wc->update_progress));
5274
Chris Mason6702ed42007-08-07 16:15:09 -04005275 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005276 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005277 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005278 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5279 path->lowest_level = 0;
5280 if (ret < 0) {
5281 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005282 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005283 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005284 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005285
Chris Mason7d9eb122008-07-08 14:19:17 -04005286 /*
5287 * unlock our path, this is safe because only this
5288 * function is allowed to delete this snapshot
5289 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005290 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005291
Yan Zheng2c47e6052009-06-27 21:07:35 -04005292 level = btrfs_header_level(root->node);
5293 while (1) {
5294 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005295 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005296 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005297
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005298 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005299 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005300 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005301 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005302 if (ret < 0) {
5303 err = ret;
5304 goto out_end_trans;
5305 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005306 BUG_ON(wc->refs[level] == 0);
5307
5308 if (level == root_item->drop_level)
5309 break;
5310
5311 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005312 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005313 WARN_ON(wc->refs[level] != 1);
5314 level--;
5315 }
5316 }
5317
Josef Bacik78c52d92019-02-06 15:46:14 -05005318 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005319 wc->level = level;
5320 wc->shared_level = -1;
5321 wc->stage = DROP_REFERENCE;
5322 wc->update_ref = update_ref;
5323 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005324 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005325
5326 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005327
Yan Zheng2c47e6052009-06-27 21:07:35 -04005328 ret = walk_down_tree(trans, root, path, wc);
5329 if (ret < 0) {
5330 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005331 break;
5332 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005333
5334 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5335 if (ret < 0) {
5336 err = ret;
5337 break;
5338 }
5339
5340 if (ret > 0) {
5341 BUG_ON(wc->stage != DROP_REFERENCE);
5342 break;
5343 }
5344
5345 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005346 wc->drop_level = wc->level;
5347 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5348 &wc->drop_progress,
5349 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005350 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005351 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5352 &wc->drop_progress);
5353 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005354
5355 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005356 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005357 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005358 ret = btrfs_update_root(trans, tree_root,
5359 &root->root_key,
5360 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005361 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005362 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005363 err = ret;
5364 goto out_end_trans;
5365 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005366
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005367 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005368 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005369 btrfs_debug(fs_info,
5370 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005371 err = -EAGAIN;
5372 goto out_free;
5373 }
5374
Yan, Zhenga22285a2010-05-16 10:48:46 -04005375 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005376 if (IS_ERR(trans)) {
5377 err = PTR_ERR(trans);
5378 goto out_free;
5379 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005380 if (block_rsv)
5381 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04005382 }
Chris Mason20524f02007-03-10 06:35:47 -05005383 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005384 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005385 if (err)
5386 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005387
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005388 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005389 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005390 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005391 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005392 goto out_end_trans;
5393 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005394
Yan, Zheng76dda932009-09-21 16:00:26 -04005395 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005396 ret = btrfs_find_root(tree_root, &root->root_key, path,
5397 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005398 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005399 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005400 err = ret;
5401 goto out_end_trans;
5402 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005403 /* if we fail to delete the orphan item this time
5404 * around, it'll get picked up the next time.
5405 *
5406 * The most common failure here is just -ENOENT.
5407 */
5408 btrfs_del_orphan_item(trans, tree_root,
5409 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005410 }
5411 }
5412
Miao Xie27cdeb72014-04-02 19:51:05 +08005413 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005414 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005415 } else {
5416 free_extent_buffer(root->node);
5417 free_extent_buffer(root->commit_root);
Josef Bacik00246522020-01-24 09:33:01 -05005418 btrfs_put_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005419 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04005420 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005421out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005422 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005423out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005424 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005425 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005426out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005427 /*
5428 * So if we need to stop dropping the snapshot for whatever reason we
5429 * need to make sure to add it back to the dead root list so that we
5430 * keep trying to do the work later. This also cleans up roots if we
5431 * don't have it in the radix (like when we recover after a power fail
5432 * or unmount) so we don't leak memory.
5433 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005434 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005435 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08005436 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005437 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005438 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005439}
Chris Mason9078a3e2007-04-26 16:46:15 -04005440
Yan Zheng2c47e6052009-06-27 21:07:35 -04005441/*
5442 * drop subtree rooted at tree block 'node'.
5443 *
5444 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005445 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005446 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005447int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5448 struct btrfs_root *root,
5449 struct extent_buffer *node,
5450 struct extent_buffer *parent)
5451{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005452 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005453 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005454 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005455 int level;
5456 int parent_level;
5457 int ret = 0;
5458 int wret;
5459
Yan Zheng2c47e6052009-06-27 21:07:35 -04005460 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5461
Yan Zhengf82d02d2008-10-29 14:49:05 -04005462 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005463 if (!path)
5464 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005465
Yan Zheng2c47e6052009-06-27 21:07:35 -04005466 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005467 if (!wc) {
5468 btrfs_free_path(path);
5469 return -ENOMEM;
5470 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005471
Chris Masonb9447ef82009-03-09 11:45:38 -04005472 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005473 parent_level = btrfs_header_level(parent);
David Sterba67439da2019-10-08 13:28:47 +02005474 atomic_inc(&parent->refs);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005475 path->nodes[parent_level] = parent;
5476 path->slots[parent_level] = btrfs_header_nritems(parent);
5477
Chris Masonb9447ef82009-03-09 11:45:38 -04005478 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005479 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005480 path->nodes[level] = node;
5481 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005482 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005483
5484 wc->refs[parent_level] = 1;
5485 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5486 wc->level = level;
5487 wc->shared_level = -1;
5488 wc->stage = DROP_REFERENCE;
5489 wc->update_ref = 0;
5490 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005491 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005492
5493 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005494 wret = walk_down_tree(trans, root, path, wc);
5495 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005496 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005497 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005498 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005499
Yan Zheng2c47e6052009-06-27 21:07:35 -04005500 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005501 if (wret < 0)
5502 ret = wret;
5503 if (wret != 0)
5504 break;
5505 }
5506
Yan Zheng2c47e6052009-06-27 21:07:35 -04005507 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005508 btrfs_free_path(path);
5509 return ret;
5510}
5511
Miao Xie6d07bce2011-01-05 10:07:31 +00005512/*
5513 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005514 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005515 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005516u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005517{
David Sterba32da53862019-10-29 19:20:18 +01005518 struct btrfs_block_group *block_group;
Miao Xie6d07bce2011-01-05 10:07:31 +00005519 u64 free_bytes = 0;
5520 int factor;
5521
Nicholas D Steeves01327612016-05-19 21:18:45 -04005522 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005523 if (list_empty(&sinfo->ro_bgs))
5524 return 0;
5525
5526 spin_lock(&sinfo->lock);
5527 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005528 spin_lock(&block_group->lock);
5529
5530 if (!block_group->ro) {
5531 spin_unlock(&block_group->lock);
5532 continue;
5533 }
5534
David Sterba46df06b2018-07-13 20:46:30 +02005535 factor = btrfs_bg_type_to_factor(block_group->flags);
David Sterbab3470b52019-10-23 18:48:22 +02005536 free_bytes += (block_group->length -
David Sterbabf38be62019-10-23 18:48:11 +02005537 block_group->used) * factor;
Miao Xie6d07bce2011-01-05 10:07:31 +00005538
5539 spin_unlock(&block_group->lock);
5540 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005541 spin_unlock(&sinfo->lock);
5542
5543 return free_bytes;
5544}
5545
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005546int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5547 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005548{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005549 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005550}
5551
Jeff Mahoney499f3772015-06-15 09:41:17 -04005552/*
5553 * It used to be that old block groups would be left around forever.
5554 * Iterating over them would be enough to trim unused space. Since we
5555 * now automatically remove them, we also need to iterate over unallocated
5556 * space.
5557 *
5558 * We don't want a transaction for this since the discard may take a
5559 * substantial amount of time. We don't require that a transaction be
5560 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005561 * to ensure that we're not discarding chunks that were released or
5562 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005563 *
5564 * Holding the chunks lock will prevent other threads from allocating
5565 * or releasing chunks, but it won't prevent a running transaction
5566 * from committing and releasing the memory that the pending chunks
5567 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005568 * transaction and hold the commit root sem. We only need to hold
5569 * it while performing the free space search since we have already
5570 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005571 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005572static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005573{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005574 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005575 int ret;
5576
5577 *trimmed = 0;
5578
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005579 /* Discard not supported = nothing to do. */
5580 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5581 return 0;
5582
Andrea Gelmini52042d82018-11-28 12:05:13 +01005583 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005584 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005585 return 0;
5586
5587 /* No free space = nothing to do. */
5588 if (device->total_bytes <= device->bytes_used)
5589 return 0;
5590
5591 ret = 0;
5592
5593 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005594 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005595 u64 bytes;
5596
5597 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5598 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005599 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005600
Nikolay Borisov929be172019-03-27 14:24:18 +02005601 find_first_clear_extent_bit(&device->alloc_state, start,
5602 &start, &end,
5603 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005604
5605 /* Ensure we skip the reserved area in the first 1M */
5606 start = max_t(u64, start, SZ_1M);
5607
Nikolay Borisov929be172019-03-27 14:24:18 +02005608 /*
5609 * If find_first_clear_extent_bit find a range that spans the
5610 * end of the device it will set end to -1, in this case it's up
5611 * to the caller to trim the value to the size of the device.
5612 */
5613 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005614
Nikolay Borisov929be172019-03-27 14:24:18 +02005615 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005616
Nikolay Borisov929be172019-03-27 14:24:18 +02005617 /* We didn't find any extents */
5618 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005619 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005620 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005621 break;
5622 }
5623
Nikolay Borisov929be172019-03-27 14:24:18 +02005624 ret = btrfs_issue_discard(device->bdev, start, len,
5625 &bytes);
5626 if (!ret)
5627 set_extent_bits(&device->alloc_state, start,
5628 start + bytes - 1,
5629 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005630 mutex_unlock(&fs_info->chunk_mutex);
5631
5632 if (ret)
5633 break;
5634
5635 start += len;
5636 *trimmed += bytes;
5637
5638 if (fatal_signal_pending(current)) {
5639 ret = -ERESTARTSYS;
5640 break;
5641 }
5642
5643 cond_resched();
5644 }
5645
5646 return ret;
5647}
5648
Qu Wenruo93bba242018-09-07 14:16:23 +08005649/*
5650 * Trim the whole filesystem by:
5651 * 1) trimming the free space in each block group
5652 * 2) trimming the unallocated space on each device
5653 *
5654 * This will also continue trimming even if a block group or device encounters
5655 * an error. The return value will be the last error, or 0 if nothing bad
5656 * happens.
5657 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005658int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005659{
David Sterba32da53862019-10-29 19:20:18 +01005660 struct btrfs_block_group *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005661 struct btrfs_device *device;
5662 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005663 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005664 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005665 u64 start;
5666 u64 end;
5667 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005668 u64 bg_failed = 0;
5669 u64 dev_failed = 0;
5670 int bg_ret = 0;
5671 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005672 int ret = 0;
5673
Qu Wenruo07301df2019-05-28 16:21:54 +08005674 /*
5675 * Check range overflow if range->len is set.
5676 * The default range->len is U64_MAX.
5677 */
5678 if (range->len != U64_MAX &&
5679 check_add_overflow(range->start, range->len, &range_end))
5680 return -EINVAL;
5681
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005682 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005683 for (; cache; cache = btrfs_next_block_group(cache)) {
David Sterbab3470b52019-10-23 18:48:22 +02005684 if (cache->start >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005685 btrfs_put_block_group(cache);
5686 break;
5687 }
5688
David Sterbab3470b52019-10-23 18:48:22 +02005689 start = max(range->start, cache->start);
5690 end = min(range_end, cache->start + cache->length);
Li Dongyangf7039b12011-03-24 10:24:28 +00005691
5692 if (end - start >= range->minlen) {
David Sterba32da53862019-10-29 19:20:18 +01005693 if (!btrfs_block_group_done(cache)) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005694 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005695 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005696 bg_failed++;
5697 bg_ret = ret;
5698 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005699 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005700 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005701 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005702 bg_failed++;
5703 bg_ret = ret;
5704 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005705 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005706 }
5707 ret = btrfs_trim_block_group(cache,
5708 &group_trimmed,
5709 start,
5710 end,
5711 range->minlen);
5712
5713 trimmed += group_trimmed;
5714 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005715 bg_failed++;
5716 bg_ret = ret;
5717 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005718 }
5719 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005720 }
5721
Qu Wenruo93bba242018-09-07 14:16:23 +08005722 if (bg_failed)
5723 btrfs_warn(fs_info,
5724 "failed to trim %llu block group(s), last error %d",
5725 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005726 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005727 devices = &fs_info->fs_devices->devices;
5728 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03005729 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005730 if (ret) {
5731 dev_failed++;
5732 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005733 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005734 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005735
5736 trimmed += group_trimmed;
5737 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005738 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005739
Qu Wenruo93bba242018-09-07 14:16:23 +08005740 if (dev_failed)
5741 btrfs_warn(fs_info,
5742 "failed to trim %llu device(s), last error %d",
5743 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005744 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005745 if (bg_ret)
5746 return bg_ret;
5747 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005748}
Miao Xie8257b2d2014-03-06 13:38:19 +08005749
5750/*
David Sterbaea14b57f2017-06-22 02:19:11 +02005751 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005752 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
5753 * data into the page cache through nocow before the subvolume is snapshoted,
5754 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +02005755 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005756 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +08005757 */
David Sterbaea14b57f2017-06-22 02:19:11 +02005758void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005759{
5760 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +01005761 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +08005762}
5763
David Sterbaea14b57f2017-06-22 02:19:11 +02005764int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005765{
David Sterbaea14b57f2017-06-22 02:19:11 +02005766 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +08005767 return 0;
5768
5769 percpu_counter_inc(&root->subv_writers->counter);
5770 /*
5771 * Make sure counter is updated before we check for snapshot creation.
5772 */
5773 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +02005774 if (atomic_read(&root->will_be_snapshotted)) {
5775 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08005776 return 0;
5777 }
5778 return 1;
5779}
Zhao Lei0bc19f902016-01-06 18:56:36 +08005780
Zhao Lei0bc19f902016-01-06 18:56:36 +08005781void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
5782{
5783 while (true) {
5784 int ret;
5785
David Sterbaea14b57f2017-06-22 02:19:11 +02005786 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +08005787 if (ret)
5788 break;
Peter Zijlstra46259562018-03-15 11:43:08 +01005789 wait_var_event(&root->will_be_snapshotted,
5790 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +08005791 }
5792}