blob: c346c46020e5ede08975797c61424855a6d0345e [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"
Qu Wenruoc57dd1f2020-07-31 19:29:11 +080036#include "rcu-string.h"
Chris Masonfec577f2007-02-26 10:40:21 -050037
Arne Jansen709c0482011-09-12 12:22:57 +020038#undef SCRAMBLE_DELAYED_REFS
39
Qu Wenruo9f9b8e82018-10-24 20:24:01 +080040
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030042 struct btrfs_delayed_ref_node *node, u64 parent,
43 u64 root_objectid, u64 owner_objectid,
44 u64 owner_offset, int refs_to_drop,
45 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040046static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
47 struct extent_buffer *leaf,
48 struct btrfs_extent_item *ei);
49static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040050 u64 parent, u64 root_objectid,
51 u64 flags, u64 owner, u64 offset,
52 struct btrfs_key *ins, int ref_mod);
53static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030054 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030055 struct btrfs_delayed_extent_op *extent_op);
Yan Zheng11833d62009-09-11 16:11:19 -040056static int find_next_key(struct btrfs_path *path, int level,
57 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050058
David Sterba32da53862019-10-29 19:20:18 +010059static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
Josef Bacik0f9dd462008-09-23 13:14:11 -040060{
61 return (cache->flags & bits) == bits;
62}
63
Josef Bacik6f410d12019-06-20 15:37:49 -040064int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
65 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -040066{
Yan Zheng11833d62009-09-11 16:11:19 -040067 u64 end = start + num_bytes - 1;
Nikolay Borisovfe119a62020-01-20 16:09:18 +020068 set_extent_bits(&fs_info->excluded_extents, start, end,
69 EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040070 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -040071}
72
David Sterba32da53862019-10-29 19:20:18 +010073void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -040074{
Nikolay Borisov9e715da2018-06-20 15:49:08 +030075 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -040076 u64 start, end;
77
David Sterbab3470b52019-10-23 18:48:22 +020078 start = cache->start;
79 end = start + cache->length - 1;
Yan Zheng11833d62009-09-11 16:11:19 -040080
Nikolay Borisovfe119a62020-01-20 16:09:18 +020081 clear_extent_bits(&fs_info->excluded_extents, start, end,
82 EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040083}
84
Filipe Manana1a4ed8f2014-10-27 10:44:24 +000085/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040086int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -040087{
88 int ret;
89 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -040090 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -040091
Zheng Yan31840ae2008-09-23 13:14:14 -040092 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070093 if (!path)
94 return -ENOMEM;
95
Chris Masone02119d2008-09-05 16:13:11 -040096 key.objectid = start;
97 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -050098 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040099 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400100 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500101 return ret;
102}
103
Chris Masond8d5f3e2007-12-11 12:42:00 -0500104/*
Josef Bacik3173a182013-03-07 14:22:04 -0500105 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400106 *
107 * the head node for delayed ref is used to store the sum of all the
108 * reference count modifications queued up in the rbtree. the head
109 * node may also store the extent flags to set. This way you can check
110 * to see what the reference count and extent flags would be if all of
111 * the delayed refs are not processed.
112 */
113int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400114 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500115 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400116{
117 struct btrfs_delayed_ref_head *head;
118 struct btrfs_delayed_ref_root *delayed_refs;
119 struct btrfs_path *path;
120 struct btrfs_extent_item *ei;
121 struct extent_buffer *leaf;
122 struct btrfs_key key;
123 u32 item_size;
124 u64 num_refs;
125 u64 extent_flags;
126 int ret;
127
Josef Bacik3173a182013-03-07 14:22:04 -0500128 /*
129 * If we don't have skinny metadata, don't bother doing anything
130 * different
131 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400132 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
133 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500134 metadata = 0;
135 }
136
Yan, Zhenga22285a2010-05-16 10:48:46 -0400137 path = btrfs_alloc_path();
138 if (!path)
139 return -ENOMEM;
140
Yan, Zhenga22285a2010-05-16 10:48:46 -0400141 if (!trans) {
142 path->skip_locking = 1;
143 path->search_commit_root = 1;
144 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000145
146search_again:
147 key.objectid = bytenr;
148 key.offset = offset;
149 if (metadata)
150 key.type = BTRFS_METADATA_ITEM_KEY;
151 else
152 key.type = BTRFS_EXTENT_ITEM_KEY;
153
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400154 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400155 if (ret < 0)
156 goto out_free;
157
Josef Bacik3173a182013-03-07 14:22:04 -0500158 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100159 if (path->slots[0]) {
160 path->slots[0]--;
161 btrfs_item_key_to_cpu(path->nodes[0], &key,
162 path->slots[0]);
163 if (key.objectid == bytenr &&
164 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400165 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100166 ret = 0;
167 }
Josef Bacik3173a182013-03-07 14:22:04 -0500168 }
169
Yan, Zhenga22285a2010-05-16 10:48:46 -0400170 if (ret == 0) {
171 leaf = path->nodes[0];
172 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
173 if (item_size >= sizeof(*ei)) {
174 ei = btrfs_item_ptr(leaf, path->slots[0],
175 struct btrfs_extent_item);
176 num_refs = btrfs_extent_refs(leaf, ei);
177 extent_flags = btrfs_extent_flags(leaf, ei);
178 } else {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300179 ret = -EINVAL;
180 btrfs_print_v0_err(fs_info);
181 if (trans)
182 btrfs_abort_transaction(trans, ret);
183 else
184 btrfs_handle_fs_error(fs_info, ret, NULL);
185
186 goto out_free;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400187 }
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300188
Yan, Zhenga22285a2010-05-16 10:48:46 -0400189 BUG_ON(num_refs == 0);
190 } else {
191 num_refs = 0;
192 extent_flags = 0;
193 ret = 0;
194 }
195
196 if (!trans)
197 goto out;
198
199 delayed_refs = &trans->transaction->delayed_refs;
200 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800201 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400202 if (head) {
203 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400204 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400205 spin_unlock(&delayed_refs->lock);
206
David Sterbab3b4aa72011-04-21 01:20:15 +0200207 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400208
David Sterba8cc33e52011-05-02 15:29:25 +0200209 /*
210 * Mutex was contended, block until it's released and try
211 * again
212 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400213 mutex_lock(&head->mutex);
214 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400215 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000216 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400217 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500218 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400219 if (head->extent_op && head->extent_op->update_flags)
220 extent_flags |= head->extent_op->flags_to_set;
221 else
222 BUG_ON(num_refs == 0);
223
Josef Bacikd2788502017-09-29 15:43:57 -0400224 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500225 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400226 mutex_unlock(&head->mutex);
227 }
228 spin_unlock(&delayed_refs->lock);
229out:
230 WARN_ON(num_refs == 0);
231 if (refs)
232 *refs = num_refs;
233 if (flags)
234 *flags = extent_flags;
235out_free:
236 btrfs_free_path(path);
237 return ret;
238}
239
240/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500241 * Back reference rules. Back refs have three main goals:
242 *
243 * 1) differentiate between all holders of references to an extent so that
244 * when a reference is dropped we can make sure it was a valid reference
245 * before freeing the extent.
246 *
247 * 2) Provide enough information to quickly find the holders of an extent
248 * if we notice a given block is corrupted or bad.
249 *
250 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
251 * maintenance. This is actually the same as #2, but with a slightly
252 * different use case.
253 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 * There are two kinds of back refs. The implicit back refs is optimized
255 * for pointers in non-shared tree blocks. For a given pointer in a block,
256 * back refs of this kind provide information about the block's owner tree
257 * and the pointer's key. These information allow us to find the block by
258 * b-tree searching. The full back refs is for pointers in tree blocks not
259 * referenced by their owner trees. The location of tree block is recorded
260 * in the back refs. Actually the full back refs is generic, and can be
261 * used in all cases the implicit back refs is used. The major shortcoming
262 * of the full back refs is its overhead. Every time a tree block gets
263 * COWed, we have to update back refs entry for all pointers in it.
264 *
265 * For a newly allocated tree block, we use implicit back refs for
266 * pointers in it. This means most tree related operations only involve
267 * implicit back refs. For a tree block created in old transaction, the
268 * only way to drop a reference to it is COW it. So we can detect the
269 * event that tree block loses its owner tree's reference and do the
270 * back refs conversion.
271 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400272 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400273 *
274 * The reference count of the block is one and the tree is the block's
275 * owner tree. Nothing to do in this case.
276 *
277 * The reference count of the block is one and the tree is not the
278 * block's owner tree. In this case, full back refs is used for pointers
279 * in the block. Remove these full back refs, add implicit back refs for
280 * every pointers in the new block.
281 *
282 * The reference count of the block is greater than one and the tree is
283 * the block's owner tree. In this case, implicit back refs is used for
284 * pointers in the block. Add full back refs for every pointers in the
285 * block, increase lower level extents' reference counts. The original
286 * implicit back refs are entailed to the new block.
287 *
288 * The reference count of the block is greater than one and the tree is
289 * not the block's owner tree. Add implicit back refs for every pointer in
290 * the new block, increase lower level extents' reference count.
291 *
292 * Back Reference Key composing:
293 *
294 * The key objectid corresponds to the first byte in the extent,
295 * The key type is used to differentiate between types of back refs.
296 * There are different meanings of the key offset for different types
297 * of back refs.
298 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500299 * File extents can be referenced by:
300 *
301 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400302 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500303 * - different offsets inside a file (bookend extents in file.c)
304 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400305 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500306 *
307 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500308 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400309 * - original offset in the file
310 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400311 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400312 * The key offset for the implicit back refs is hash of the first
313 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500314 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400315 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500316 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400317 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500318 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400319 * The key offset for the implicit back refs is the first byte of
320 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500321 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400322 * When a file extent is allocated, The implicit back refs is used.
323 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500324 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400325 * (root_key.objectid, inode objectid, offset in file, 1)
326 *
327 * When a file extent is removed file truncation, we find the
328 * corresponding implicit back refs and check the following fields:
329 *
330 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500331 *
332 * Btree extents can be referenced by:
333 *
334 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500335 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400336 * Both the implicit back refs and the full back refs for tree blocks
337 * only consist of key. The key offset for the implicit back refs is
338 * objectid of block's owner tree. The key offset for the full back refs
339 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500340 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400341 * When implicit back refs is used, information about the lowest key and
342 * level of the tree block are required. These information are stored in
343 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500344 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400345
Liu Bo167ce952017-08-18 15:15:18 -0600346/*
347 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100348 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
Liu Bo167ce952017-08-18 15:15:18 -0600349 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
350 */
351int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
352 struct btrfs_extent_inline_ref *iref,
353 enum btrfs_inline_ref_type is_data)
354{
355 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -0600356 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -0600357
358 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
359 type == BTRFS_SHARED_BLOCK_REF_KEY ||
360 type == BTRFS_SHARED_DATA_REF_KEY ||
361 type == BTRFS_EXTENT_DATA_REF_KEY) {
362 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600363 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600364 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600365 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
366 ASSERT(eb->fs_info);
367 /*
Qu Wenruoea577882020-08-26 17:26:43 +0800368 * Every shared one has parent tree block,
369 * which must be aligned to sector size.
Liu Bo64ecdb62017-08-18 15:15:24 -0600370 */
371 if (offset &&
Qu Wenruoea577882020-08-26 17:26:43 +0800372 IS_ALIGNED(offset, eb->fs_info->sectorsize))
Liu Bo64ecdb62017-08-18 15:15:24 -0600373 return type;
374 }
Liu Bo167ce952017-08-18 15:15:18 -0600375 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600376 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600377 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600378 if (type == BTRFS_SHARED_DATA_REF_KEY) {
379 ASSERT(eb->fs_info);
380 /*
Qu Wenruoea577882020-08-26 17:26:43 +0800381 * Every shared one has parent tree block,
382 * which must be aligned to sector size.
Liu Bo64ecdb62017-08-18 15:15:24 -0600383 */
384 if (offset &&
Qu Wenruoea577882020-08-26 17:26:43 +0800385 IS_ALIGNED(offset, eb->fs_info->sectorsize))
Liu Bo64ecdb62017-08-18 15:15:24 -0600386 return type;
387 }
Liu Bo167ce952017-08-18 15:15:18 -0600388 } else {
389 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
390 return type;
391 }
392 }
393
394 btrfs_print_leaf((struct extent_buffer *)eb);
Qu Wenruoea577882020-08-26 17:26:43 +0800395 btrfs_err(eb->fs_info,
396 "eb %llu iref 0x%lx invalid extent inline ref type %d",
397 eb->start, (unsigned long)iref, type);
Liu Bo167ce952017-08-18 15:15:18 -0600398 WARN_ON(1);
399
400 return BTRFS_REF_TYPE_INVALID;
401}
402
Qu Wenruo0785a9a2019-08-09 09:24:24 +0800403u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400404{
405 u32 high_crc = ~(u32)0;
406 u32 low_crc = ~(u32)0;
407 __le64 lenum;
408
409 lenum = cpu_to_le64(root_objectid);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200410 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400411 lenum = cpu_to_le64(owner);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200412 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400413 lenum = cpu_to_le64(offset);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200414 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400415
416 return ((u64)high_crc << 31) ^ (u64)low_crc;
417}
418
419static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
420 struct btrfs_extent_data_ref *ref)
421{
422 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
423 btrfs_extent_data_ref_objectid(leaf, ref),
424 btrfs_extent_data_ref_offset(leaf, ref));
425}
426
427static int match_extent_data_ref(struct extent_buffer *leaf,
428 struct btrfs_extent_data_ref *ref,
429 u64 root_objectid, u64 owner, u64 offset)
430{
431 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
432 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
433 btrfs_extent_data_ref_offset(leaf, ref) != offset)
434 return 0;
435 return 1;
436}
437
438static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400439 struct btrfs_path *path,
440 u64 bytenr, u64 parent,
441 u64 root_objectid,
442 u64 owner, u64 offset)
443{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +0300444 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400445 struct btrfs_key key;
446 struct btrfs_extent_data_ref *ref;
447 struct extent_buffer *leaf;
448 u32 nritems;
449 int ret;
450 int recow;
451 int err = -ENOENT;
452
453 key.objectid = bytenr;
454 if (parent) {
455 key.type = BTRFS_SHARED_DATA_REF_KEY;
456 key.offset = parent;
457 } else {
458 key.type = BTRFS_EXTENT_DATA_REF_KEY;
459 key.offset = hash_extent_data_ref(root_objectid,
460 owner, offset);
461 }
462again:
463 recow = 0;
464 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
465 if (ret < 0) {
466 err = ret;
467 goto fail;
468 }
469
470 if (parent) {
471 if (!ret)
472 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400473 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400474 }
475
476 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 nritems = btrfs_header_nritems(leaf);
478 while (1) {
479 if (path->slots[0] >= nritems) {
480 ret = btrfs_next_leaf(root, path);
481 if (ret < 0)
482 err = ret;
483 if (ret)
484 goto fail;
485
486 leaf = path->nodes[0];
487 nritems = btrfs_header_nritems(leaf);
488 recow = 1;
489 }
490
491 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
492 if (key.objectid != bytenr ||
493 key.type != BTRFS_EXTENT_DATA_REF_KEY)
494 goto fail;
495
496 ref = btrfs_item_ptr(leaf, path->slots[0],
497 struct btrfs_extent_data_ref);
498
499 if (match_extent_data_ref(leaf, ref, root_objectid,
500 owner, offset)) {
501 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200502 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400503 goto again;
504 }
505 err = 0;
506 break;
507 }
508 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400509 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400510fail:
511 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400512}
513
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400514static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400515 struct btrfs_path *path,
516 u64 bytenr, u64 parent,
517 u64 root_objectid, u64 owner,
518 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400519{
Nikolay Borisov62b895a2018-06-20 15:48:44 +0300520 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -0400521 struct btrfs_key key;
522 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400523 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400524 u32 num_refs;
525 int ret;
526
527 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400528 if (parent) {
529 key.type = BTRFS_SHARED_DATA_REF_KEY;
530 key.offset = parent;
531 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400532 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400533 key.type = BTRFS_EXTENT_DATA_REF_KEY;
534 key.offset = hash_extent_data_ref(root_objectid,
535 owner, offset);
536 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400537 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400538
539 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
540 if (ret && ret != -EEXIST)
541 goto fail;
542
543 leaf = path->nodes[0];
544 if (parent) {
545 struct btrfs_shared_data_ref *ref;
546 ref = btrfs_item_ptr(leaf, path->slots[0],
547 struct btrfs_shared_data_ref);
548 if (ret == 0) {
549 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
550 } else {
551 num_refs = btrfs_shared_data_ref_count(leaf, ref);
552 num_refs += refs_to_add;
553 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
554 }
555 } else {
556 struct btrfs_extent_data_ref *ref;
557 while (ret == -EEXIST) {
558 ref = btrfs_item_ptr(leaf, path->slots[0],
559 struct btrfs_extent_data_ref);
560 if (match_extent_data_ref(leaf, ref, root_objectid,
561 owner, offset))
562 break;
David Sterbab3b4aa72011-04-21 01:20:15 +0200563 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400564 key.offset++;
565 ret = btrfs_insert_empty_item(trans, root, path, &key,
566 size);
567 if (ret && ret != -EEXIST)
568 goto fail;
569
570 leaf = path->nodes[0];
571 }
572 ref = btrfs_item_ptr(leaf, path->slots[0],
573 struct btrfs_extent_data_ref);
574 if (ret == 0) {
575 btrfs_set_extent_data_ref_root(leaf, ref,
576 root_objectid);
577 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
578 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
579 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
580 } else {
581 num_refs = btrfs_extent_data_ref_count(leaf, ref);
582 num_refs += refs_to_add;
583 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
584 }
585 }
586 btrfs_mark_buffer_dirty(leaf);
587 ret = 0;
588fail:
David Sterbab3b4aa72011-04-21 01:20:15 +0200589 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500590 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500591}
592
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400594 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -0700595 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -0400596{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400597 struct btrfs_key key;
598 struct btrfs_extent_data_ref *ref1 = NULL;
599 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400600 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400601 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400602 int ret = 0;
603
604 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400605 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
606
607 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
608 ref1 = btrfs_item_ptr(leaf, path->slots[0],
609 struct btrfs_extent_data_ref);
610 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
611 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
612 ref2 = btrfs_item_ptr(leaf, path->slots[0],
613 struct btrfs_shared_data_ref);
614 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200615 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300616 btrfs_print_v0_err(trans->fs_info);
617 btrfs_abort_transaction(trans, -EINVAL);
618 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400619 } else {
620 BUG();
621 }
622
Chris Mason56bec292009-03-13 10:10:06 -0400623 BUG_ON(num_refs < refs_to_drop);
624 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400625
Zheng Yan31840ae2008-09-23 13:14:14 -0400626 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +0300627 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -0700628 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400629 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400630 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
631 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
632 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
633 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
Zheng Yan31840ae2008-09-23 13:14:14 -0400634 btrfs_mark_buffer_dirty(leaf);
635 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400636 return ret;
637}
638
Zhaolei9ed0dea2015-08-06 22:16:24 +0800639static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400640 struct btrfs_extent_inline_ref *iref)
641{
642 struct btrfs_key key;
643 struct extent_buffer *leaf;
644 struct btrfs_extent_data_ref *ref1;
645 struct btrfs_shared_data_ref *ref2;
646 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -0600647 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400648
649 leaf = path->nodes[0];
650 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300651
652 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400653 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -0600654 /*
655 * If type is invalid, we should have bailed out earlier than
656 * this call.
657 */
658 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
659 ASSERT(type != BTRFS_REF_TYPE_INVALID);
660 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
662 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
663 } else {
664 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
665 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
666 }
667 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
668 ref1 = btrfs_item_ptr(leaf, path->slots[0],
669 struct btrfs_extent_data_ref);
670 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
671 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
672 ref2 = btrfs_item_ptr(leaf, path->slots[0],
673 struct btrfs_shared_data_ref);
674 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 } else {
676 WARN_ON(1);
677 }
678 return num_refs;
679}
680
681static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 struct btrfs_path *path,
683 u64 bytenr, u64 parent,
684 u64 root_objectid)
685{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +0300686 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400687 struct btrfs_key key;
688 int ret;
689
690 key.objectid = bytenr;
691 if (parent) {
692 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
693 key.offset = parent;
694 } else {
695 key.type = BTRFS_TREE_BLOCK_REF_KEY;
696 key.offset = root_objectid;
697 }
698
699 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
700 if (ret > 0)
701 ret = -ENOENT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400702 return ret;
703}
704
705static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400706 struct btrfs_path *path,
707 u64 bytenr, u64 parent,
708 u64 root_objectid)
709{
710 struct btrfs_key key;
711 int ret;
712
713 key.objectid = bytenr;
714 if (parent) {
715 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
716 key.offset = parent;
717 } else {
718 key.type = BTRFS_TREE_BLOCK_REF_KEY;
719 key.offset = root_objectid;
720 }
721
Nikolay Borisov10728402018-06-20 15:48:43 +0300722 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500723 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +0200724 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -0400725 return ret;
726}
727
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400728static inline int extent_ref_type(u64 parent, u64 owner)
729{
730 int type;
731 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
732 if (parent > 0)
733 type = BTRFS_SHARED_BLOCK_REF_KEY;
734 else
735 type = BTRFS_TREE_BLOCK_REF_KEY;
736 } else {
737 if (parent > 0)
738 type = BTRFS_SHARED_DATA_REF_KEY;
739 else
740 type = BTRFS_EXTENT_DATA_REF_KEY;
741 }
742 return type;
743}
744
Yan Zheng2c47e6052009-06-27 21:07:35 -0400745static int find_next_key(struct btrfs_path *path, int level,
746 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400747
748{
Yan Zheng2c47e6052009-06-27 21:07:35 -0400749 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400750 if (!path->nodes[level])
751 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400752 if (path->slots[level] + 1 >=
753 btrfs_header_nritems(path->nodes[level]))
754 continue;
755 if (level == 0)
756 btrfs_item_key_to_cpu(path->nodes[level], key,
757 path->slots[level] + 1);
758 else
759 btrfs_node_key_to_cpu(path->nodes[level], key,
760 path->slots[level] + 1);
761 return 0;
762 }
763 return 1;
764}
765
766/*
767 * look for inline back ref. if back ref is found, *ref_ret is set
768 * to the address of inline back ref, and 0 is returned.
769 *
770 * if back ref isn't found, *ref_ret is set to the address where it
771 * should be inserted, and -ENOENT is returned.
772 *
773 * if insert is true and there are too many inline back refs, the path
774 * points to the extent item, and -EAGAIN is returned.
775 *
776 * NOTE: inline back refs are ordered in the same way that back ref
777 * items in the tree are ordered.
778 */
779static noinline_for_stack
780int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400781 struct btrfs_path *path,
782 struct btrfs_extent_inline_ref **ref_ret,
783 u64 bytenr, u64 num_bytes,
784 u64 parent, u64 root_objectid,
785 u64 owner, u64 offset, int insert)
786{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +0300787 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500788 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400789 struct btrfs_key key;
790 struct extent_buffer *leaf;
791 struct btrfs_extent_item *ei;
792 struct btrfs_extent_inline_ref *iref;
793 u64 flags;
794 u64 item_size;
795 unsigned long ptr;
796 unsigned long end;
797 int extra_size;
798 int type;
799 int want;
800 int ret;
801 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400802 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -0600803 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400804
805 key.objectid = bytenr;
806 key.type = BTRFS_EXTENT_ITEM_KEY;
807 key.offset = num_bytes;
808
809 want = extent_ref_type(parent, owner);
810 if (insert) {
811 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -0400812 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813 } else
814 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -0500815
816 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +0300817 * Owner is our level, so we can just add one to get the level for the
818 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -0500819 */
820 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
821 key.type = BTRFS_METADATA_ITEM_KEY;
822 key.offset = owner;
823 }
824
825again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400826 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
827 if (ret < 0) {
828 err = ret;
829 goto out;
830 }
Josef Bacik3173a182013-03-07 14:22:04 -0500831
832 /*
833 * We may be a newly converted file system which still has the old fat
834 * extent entries for metadata, so try and see if we have one of those.
835 */
836 if (ret > 0 && skinny_metadata) {
837 skinny_metadata = false;
838 if (path->slots[0]) {
839 path->slots[0]--;
840 btrfs_item_key_to_cpu(path->nodes[0], &key,
841 path->slots[0]);
842 if (key.objectid == bytenr &&
843 key.type == BTRFS_EXTENT_ITEM_KEY &&
844 key.offset == num_bytes)
845 ret = 0;
846 }
847 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +0100848 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500849 key.type = BTRFS_EXTENT_ITEM_KEY;
850 key.offset = num_bytes;
851 btrfs_release_path(path);
852 goto again;
853 }
854 }
855
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100856 if (ret && !insert) {
857 err = -ENOENT;
858 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530859 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -0500860 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -0500861 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100862 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863
864 leaf = path->nodes[0];
865 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200866 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300867 err = -EINVAL;
868 btrfs_print_v0_err(fs_info);
869 btrfs_abort_transaction(trans, err);
870 goto out;
871 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400872
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
874 flags = btrfs_extent_flags(leaf, ei);
875
876 ptr = (unsigned long)(ei + 1);
877 end = (unsigned long)ei + item_size;
878
Josef Bacik3173a182013-03-07 14:22:04 -0500879 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400880 ptr += sizeof(struct btrfs_tree_block_info);
881 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 }
883
Liu Bo3de28d52017-08-18 15:15:19 -0600884 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
885 needed = BTRFS_REF_TYPE_DATA;
886 else
887 needed = BTRFS_REF_TYPE_BLOCK;
888
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889 err = -ENOENT;
890 while (1) {
891 if (ptr >= end) {
892 WARN_ON(ptr > end);
893 break;
894 }
895 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600896 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
897 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +0800898 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -0600899 goto out;
900 }
901
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 if (want < type)
903 break;
904 if (want > type) {
905 ptr += btrfs_extent_inline_ref_size(type);
906 continue;
907 }
908
909 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
910 struct btrfs_extent_data_ref *dref;
911 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
912 if (match_extent_data_ref(leaf, dref, root_objectid,
913 owner, offset)) {
914 err = 0;
915 break;
916 }
917 if (hash_extent_data_ref_item(leaf, dref) <
918 hash_extent_data_ref(root_objectid, owner, offset))
919 break;
920 } else {
921 u64 ref_offset;
922 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
923 if (parent > 0) {
924 if (parent == ref_offset) {
925 err = 0;
926 break;
927 }
928 if (ref_offset < parent)
929 break;
930 } else {
931 if (root_objectid == ref_offset) {
932 err = 0;
933 break;
934 }
935 if (ref_offset < root_objectid)
936 break;
937 }
938 }
939 ptr += btrfs_extent_inline_ref_size(type);
940 }
941 if (err == -ENOENT && insert) {
942 if (item_size + extra_size >=
943 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
944 err = -EAGAIN;
945 goto out;
946 }
947 /*
948 * To add new inline back ref, we have to make sure
949 * there is no corresponding back ref item.
950 * For simplicity, we just do not add new inline back
951 * ref if there is any kind of item for this block
952 */
Yan Zheng2c47e6052009-06-27 21:07:35 -0400953 if (find_next_key(path, 0, &key) == 0 &&
954 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -0400955 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 err = -EAGAIN;
957 goto out;
958 }
959 }
960 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
961out:
Yan Zheng85d41982009-06-11 08:51:10 -0400962 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 path->keep_locks = 0;
964 btrfs_unlock_up_safe(path, 1);
965 }
966 return err;
967}
968
969/*
970 * helper to add new inline back ref
971 */
972static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500973void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100974 struct btrfs_path *path,
975 struct btrfs_extent_inline_ref *iref,
976 u64 parent, u64 root_objectid,
977 u64 owner, u64 offset, int refs_to_add,
978 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400979{
980 struct extent_buffer *leaf;
981 struct btrfs_extent_item *ei;
982 unsigned long ptr;
983 unsigned long end;
984 unsigned long item_offset;
985 u64 refs;
986 int size;
987 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988
989 leaf = path->nodes[0];
990 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
991 item_offset = (unsigned long)iref - (unsigned long)ei;
992
993 type = extent_ref_type(parent, owner);
994 size = btrfs_extent_inline_ref_size(type);
995
David Sterbac71dd882019-03-20 14:51:10 +0100996 btrfs_extend_item(path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997
998 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
999 refs = btrfs_extent_refs(leaf, ei);
1000 refs += refs_to_add;
1001 btrfs_set_extent_refs(leaf, ei, refs);
1002 if (extent_op)
1003 __run_delayed_extent_op(extent_op, leaf, ei);
1004
1005 ptr = (unsigned long)ei + item_offset;
1006 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1007 if (ptr < end - size)
1008 memmove_extent_buffer(leaf, ptr + size, ptr,
1009 end - size - ptr);
1010
1011 iref = (struct btrfs_extent_inline_ref *)ptr;
1012 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1013 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1014 struct btrfs_extent_data_ref *dref;
1015 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1016 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1017 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1018 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1019 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1020 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1021 struct btrfs_shared_data_ref *sref;
1022 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1023 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1024 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1025 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1026 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1027 } else {
1028 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1029 }
1030 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031}
1032
1033static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001034 struct btrfs_path *path,
1035 struct btrfs_extent_inline_ref **ref_ret,
1036 u64 bytenr, u64 num_bytes, u64 parent,
1037 u64 root_objectid, u64 owner, u64 offset)
1038{
1039 int ret;
1040
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001041 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1042 num_bytes, parent, root_objectid,
1043 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 if (ret != -ENOENT)
1045 return ret;
1046
David Sterbab3b4aa72011-04-21 01:20:15 +02001047 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 *ref_ret = NULL;
1049
1050 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001051 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1052 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001053 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001054 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1055 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056 }
1057 return ret;
1058}
1059
1060/*
1061 * helper to update/remove inline back ref
1062 */
1063static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001064void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001065 struct btrfs_extent_inline_ref *iref,
1066 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001067 struct btrfs_delayed_extent_op *extent_op,
1068 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001070 struct extent_buffer *leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001071 struct btrfs_extent_item *ei;
1072 struct btrfs_extent_data_ref *dref = NULL;
1073 struct btrfs_shared_data_ref *sref = NULL;
1074 unsigned long ptr;
1075 unsigned long end;
1076 u32 item_size;
1077 int size;
1078 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001079 u64 refs;
1080
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1082 refs = btrfs_extent_refs(leaf, ei);
1083 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1084 refs += refs_to_mod;
1085 btrfs_set_extent_refs(leaf, ei, refs);
1086 if (extent_op)
1087 __run_delayed_extent_op(extent_op, leaf, ei);
1088
Liu Bo3de28d52017-08-18 15:15:19 -06001089 /*
1090 * If type is invalid, we should have bailed out after
1091 * lookup_inline_extent_backref().
1092 */
1093 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1094 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095
1096 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1097 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1098 refs = btrfs_extent_data_ref_count(leaf, dref);
1099 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1100 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1101 refs = btrfs_shared_data_ref_count(leaf, sref);
1102 } else {
1103 refs = 1;
1104 BUG_ON(refs_to_mod != -1);
1105 }
1106
1107 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1108 refs += refs_to_mod;
1109
1110 if (refs > 0) {
1111 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1112 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1113 else
1114 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1115 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001116 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001117 size = btrfs_extent_inline_ref_size(type);
1118 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1119 ptr = (unsigned long)iref;
1120 end = (unsigned long)ei + item_size;
1121 if (ptr + size < end)
1122 memmove_extent_buffer(leaf, ptr, ptr + size,
1123 end - ptr - size);
1124 item_size -= size;
David Sterba78ac4f92019-03-20 14:49:12 +01001125 btrfs_truncate_item(path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001126 }
1127 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001128}
1129
1130static noinline_for_stack
1131int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 struct btrfs_path *path,
1133 u64 bytenr, u64 num_bytes, u64 parent,
1134 u64 root_objectid, u64 owner,
1135 u64 offset, int refs_to_add,
1136 struct btrfs_delayed_extent_op *extent_op)
1137{
1138 struct btrfs_extent_inline_ref *iref;
1139 int ret;
1140
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001141 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1142 num_bytes, parent, root_objectid,
1143 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001144 if (ret == 0) {
Qu Wenruo07cce5c2020-08-19 14:35:49 +08001145 /*
1146 * We're adding refs to a tree block we already own, this
1147 * should not happen at all.
1148 */
1149 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1150 btrfs_crit(trans->fs_info,
1151"adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu",
1152 bytenr, num_bytes, root_objectid);
1153 if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {
1154 WARN_ON(1);
1155 btrfs_crit(trans->fs_info,
1156 "path->slots[0]=%d path->nodes[0]:", path->slots[0]);
1157 btrfs_print_leaf(path->nodes[0]);
1158 }
1159 return -EUCLEAN;
1160 }
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001161 update_inline_extent_backref(path, iref, refs_to_add,
1162 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 } else if (ret == -ENOENT) {
Nikolay Borisova639cde2018-06-20 15:49:10 +03001164 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001165 root_objectid, owner, offset,
1166 refs_to_add, extent_op);
1167 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001168 }
1169 return ret;
1170}
1171
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001172static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001173 struct btrfs_path *path,
1174 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001175 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001176{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001177 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178
1179 BUG_ON(!is_data && refs_to_drop != 1);
1180 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001181 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1182 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001184 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001185 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001186 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001187 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001188 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001189 }
1190 return ret;
1191}
1192
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001193static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1194 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001195{
Jeff Mahoney86557862015-06-15 09:41:16 -04001196 int j, ret = 0;
1197 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001198 u64 aligned_start = ALIGN(start, 1 << 9);
1199
1200 if (WARN_ON(start != aligned_start)) {
1201 len -= aligned_start - start;
1202 len = round_down(len, 1 << 9);
1203 start = aligned_start;
1204 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001205
1206 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001207
1208 if (!len)
1209 return 0;
1210
1211 end = start + len;
1212 bytes_left = len;
1213
1214 /* Skip any superblocks on this device. */
1215 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1216 u64 sb_start = btrfs_sb_offset(j);
1217 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1218 u64 size = sb_start - start;
1219
1220 if (!in_range(sb_start, start, bytes_left) &&
1221 !in_range(sb_end, start, bytes_left) &&
1222 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1223 continue;
1224
1225 /*
1226 * Superblock spans beginning of range. Adjust start and
1227 * try again.
1228 */
1229 if (sb_start <= start) {
1230 start += sb_end - start;
1231 if (start > end) {
1232 bytes_left = 0;
1233 break;
1234 }
1235 bytes_left = end - start;
1236 continue;
1237 }
1238
1239 if (size) {
1240 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1241 GFP_NOFS, 0);
1242 if (!ret)
1243 *discarded_bytes += size;
1244 else if (ret != -EOPNOTSUPP)
1245 return ret;
1246 }
1247
1248 start = sb_end;
1249 if (start > end) {
1250 bytes_left = 0;
1251 break;
1252 }
1253 bytes_left = end - start;
1254 }
1255
1256 if (bytes_left) {
1257 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001258 GFP_NOFS, 0);
1259 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001260 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001261 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001262 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001263}
Chris Mason15916de2008-11-19 21:17:22 -05001264
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001265int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001266 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001267{
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001268 int ret = 0;
Li Dongyang5378e602011-03-24 10:24:27 +00001269 u64 discarded_bytes = 0;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001270 u64 end = bytenr + num_bytes;
1271 u64 cur = bytenr;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001272 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001273
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001274
Filipe Manana29992412016-05-27 17:42:05 +01001275 /*
1276 * Avoid races with device replace and make sure our bbio has devices
1277 * associated to its stripes that don't go away while we are discarding.
1278 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001279 btrfs_bio_counter_inc_blocked(fs_info);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001280 while (cur < end) {
1281 struct btrfs_bio_stripe *stripe;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001282 int i;
1283
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001284 num_bytes = end - cur;
1285 /* Tell the block device(s) that the sectors can be discarded */
1286 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1287 &num_bytes, &bbio, 0);
1288 /*
1289 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1290 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1291 * thus we can't continue anyway.
1292 */
1293 if (ret < 0)
1294 goto out;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001295
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001296 stripe = bbio->stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001297 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001298 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001299 struct request_queue *req_q;
Anand Jainfe910d22021-04-30 19:59:51 +08001300 struct btrfs_device *device = stripe->dev;
Anand Jain38b5f682017-11-29 18:53:43 +08001301
Anand Jainfe910d22021-04-30 19:59:51 +08001302 if (!device->bdev) {
Filipe Manana627e0872018-01-30 18:40:22 +00001303 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1304 continue;
1305 }
Anand Jainfe910d22021-04-30 19:59:51 +08001306 req_q = bdev_get_queue(device->bdev);
Anand Jain38b5f682017-11-29 18:53:43 +08001307 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001308 continue;
1309
Anand Jainfe910d22021-04-30 19:59:51 +08001310 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
1311 continue;
1312
1313 ret = btrfs_issue_discard(device->bdev,
Li Dongyang5378e602011-03-24 10:24:27 +00001314 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001315 stripe->length,
1316 &bytes);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001317 if (!ret) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001318 discarded_bytes += bytes;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001319 } else if (ret != -EOPNOTSUPP) {
1320 /*
1321 * Logic errors or -ENOMEM, or -EIO, but
1322 * unlikely to happen.
1323 *
1324 * And since there are two loops, explicitly
1325 * go to out to avoid confusion.
1326 */
1327 btrfs_put_bbio(bbio);
1328 goto out;
1329 }
Josef Bacikd5e20032011-08-04 14:52:27 +00001330
1331 /*
1332 * Just in case we get back EOPNOTSUPP for some reason,
1333 * just ignore the return value so we don't screw up
1334 * people calling discard_extent.
1335 */
1336 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001337 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001338 btrfs_put_bbio(bbio);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001339 cur += num_bytes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001340 }
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001341out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001342 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001343
1344 if (actual_bytes)
1345 *actual_bytes = discarded_bytes;
1346
Liu Hui1f3c79a2009-01-05 15:57:51 -05001347
David Woodhouse53b381b2013-01-29 18:40:14 -05001348 if (ret == -EOPNOTSUPP)
1349 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001350 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001351}
1352
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001353/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001355 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001356{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001357 struct btrfs_fs_info *fs_info = trans->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04001358 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001359
Qu Wenruo82fa1132019-04-04 14:45:35 +08001360 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1361 generic_ref->action);
1362 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1363 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001364
Qu Wenruo82fa1132019-04-04 14:45:35 +08001365 if (generic_ref->type == BTRFS_REF_METADATA)
Josef Bacik7ec15362021-01-15 16:48:55 -05001366 ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001367 else
Josef Bacik7ec15362021-01-15 16:48:55 -05001368 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0);
Omar Sandovald7eae342017-06-06 16:45:31 -07001369
Qu Wenruo82fa1132019-04-04 14:45:35 +08001370 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001371
Zheng Yan31840ae2008-09-23 13:14:14 -04001372 return ret;
1373}
1374
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001375/*
1376 * __btrfs_inc_extent_ref - insert backreference for a given extent
1377 *
Qu Wenruo07cce5c2020-08-19 14:35:49 +08001378 * The counterpart is in __btrfs_free_extent(), with examples and more details
1379 * how it works.
1380 *
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001381 * @trans: Handle of transaction
1382 *
1383 * @node: The delayed ref node used to get the bytenr/length for
1384 * extent whose references are incremented.
1385 *
1386 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1387 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1388 * bytenr of the parent block. Since new extents are always
1389 * created with indirect references, this will only be the case
1390 * when relocating a shared extent. In that case, root_objectid
1391 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1392 * be 0
1393 *
1394 * @root_objectid: The id of the root where this modification has originated,
1395 * this can be either one of the well-known metadata trees or
1396 * the subvolume id which references this extent.
1397 *
1398 * @owner: For data extents it is the inode number of the owning file.
1399 * For metadata extents this parameter holds the level in the
1400 * tree of the extent.
1401 *
1402 * @offset: For metadata extents the offset is ignored and is currently
1403 * always passed as 0. For data extents it is the fileoffset
1404 * this extent belongs to.
1405 *
1406 * @refs_to_add Number of references to add
1407 *
1408 * @extent_op Pointer to a structure, holding information necessary when
1409 * updating a tree block's flags
1410 *
1411 */
Chris Mason925baed2008-06-25 16:01:30 -04001412static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001413 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001414 u64 parent, u64 root_objectid,
1415 u64 owner, u64 offset, int refs_to_add,
1416 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001417{
Chris Mason5caf2a02007-04-02 11:20:42 -04001418 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001419 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001420 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001421 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001422 u64 bytenr = node->bytenr;
1423 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001424 u64 refs;
1425 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001426
Chris Mason5caf2a02007-04-02 11:20:42 -04001427 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001428 if (!path)
1429 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001430
Chris Masonb9473432009-03-13 11:00:37 -04001431 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001432 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001433 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1434 parent, root_objectid, owner,
1435 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001436 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001437 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001438
1439 /*
1440 * Ok we had -EAGAIN which means we didn't have space to insert and
1441 * inline extent ref, so just update the reference count and add a
1442 * normal backref.
1443 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001444 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001445 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001446 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1447 refs = btrfs_extent_refs(leaf, item);
1448 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1449 if (extent_op)
1450 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001452 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001453 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001454
Chris Masonb9473432009-03-13 11:00:37 -04001455 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001456 /* now insert the actual backref */
Nikolay Borisov65cd6d92020-03-04 11:33:53 +02001457 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1458 BUG_ON(refs_to_add != 1);
1459 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1460 root_objectid);
1461 } else {
1462 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1463 root_objectid, owner, offset,
1464 refs_to_add);
1465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001467 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001468out:
Chris Mason74493f72007-12-11 09:25:06 -05001469 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001470 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001471}
1472
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001473static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 struct btrfs_delayed_ref_node *node,
1475 struct btrfs_delayed_extent_op *extent_op,
1476 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001477{
Chris Mason56bec292009-03-13 10:10:06 -04001478 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 struct btrfs_delayed_data_ref *ref;
1480 struct btrfs_key ins;
1481 u64 parent = 0;
1482 u64 ref_root = 0;
1483 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001484
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001485 ins.objectid = node->bytenr;
1486 ins.offset = node->num_bytes;
1487 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001488
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001489 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001490 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001492 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1493 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001494 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001495
1496 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001497 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001498 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001499 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1500 flags, ref->objectid,
1501 ref->offset, &ins,
1502 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001504 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1505 ref->objectid, ref->offset,
1506 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001507 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001508 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509 ref_root, ref->objectid,
1510 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001511 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001512 } else {
1513 BUG();
1514 }
Chris Mason56bec292009-03-13 10:10:06 -04001515 return ret;
1516}
1517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001518static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1519 struct extent_buffer *leaf,
1520 struct btrfs_extent_item *ei)
1521{
1522 u64 flags = btrfs_extent_flags(leaf, ei);
1523 if (extent_op->update_flags) {
1524 flags |= extent_op->flags_to_set;
1525 btrfs_set_extent_flags(leaf, ei, flags);
1526 }
1527
1528 if (extent_op->update_key) {
1529 struct btrfs_tree_block_info *bi;
1530 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1531 bi = (struct btrfs_tree_block_info *)(ei + 1);
1532 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1533 }
1534}
1535
1536static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001537 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001538 struct btrfs_delayed_extent_op *extent_op)
1539{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001540 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541 struct btrfs_key key;
1542 struct btrfs_path *path;
1543 struct btrfs_extent_item *ei;
1544 struct extent_buffer *leaf;
1545 u32 item_size;
1546 int ret;
1547 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001548 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001549
David Sterbabf31f872020-02-05 17:34:34 +01001550 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001551 return 0;
1552
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001553 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001554 metadata = 0;
1555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001556 path = btrfs_alloc_path();
1557 if (!path)
1558 return -ENOMEM;
1559
Josef Bacikd2788502017-09-29 15:43:57 -04001560 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001561
Josef Bacik3173a182013-03-07 14:22:04 -05001562 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001563 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001564 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001565 } else {
1566 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001567 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001568 }
1569
1570again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001571 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001572 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001573 if (ret < 0) {
1574 err = ret;
1575 goto out;
1576 }
1577 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001578 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001579 if (path->slots[0] > 0) {
1580 path->slots[0]--;
1581 btrfs_item_key_to_cpu(path->nodes[0], &key,
1582 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001583 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001584 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001585 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001586 ret = 0;
1587 }
1588 if (ret > 0) {
1589 btrfs_release_path(path);
1590 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001591
Josef Bacikd2788502017-09-29 15:43:57 -04001592 key.objectid = head->bytenr;
1593 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001594 key.type = BTRFS_EXTENT_ITEM_KEY;
1595 goto again;
1596 }
1597 } else {
1598 err = -EIO;
1599 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001600 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001601 }
1602
1603 leaf = path->nodes[0];
1604 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001605
David Sterba6d8ff4e2018-06-26 16:20:59 +02001606 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001607 err = -EINVAL;
1608 btrfs_print_v0_err(fs_info);
1609 btrfs_abort_transaction(trans, err);
1610 goto out;
1611 }
1612
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001613 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1614 __run_delayed_extent_op(extent_op, leaf, ei);
1615
1616 btrfs_mark_buffer_dirty(leaf);
1617out:
1618 btrfs_free_path(path);
1619 return err;
1620}
1621
1622static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001623 struct btrfs_delayed_ref_node *node,
1624 struct btrfs_delayed_extent_op *extent_op,
1625 int insert_reserved)
1626{
1627 int ret = 0;
1628 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001629 u64 parent = 0;
1630 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631
1632 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001633 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1636 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001637 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638
Liu Bo02794222016-09-14 19:19:05 -07001639 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001640 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001641 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1642 node->bytenr, node->ref_mod, node->action, ref_root,
1643 parent);
1644 return -EIO;
1645 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001647 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001648 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001649 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001650 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1651 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001652 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001653 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001654 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001655 } else {
1656 BUG();
1657 }
1658 return ret;
1659}
1660
Chris Mason56bec292009-03-13 10:10:06 -04001661/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663 struct btrfs_delayed_ref_node *node,
1664 struct btrfs_delayed_extent_op *extent_op,
1665 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001666{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001667 int ret = 0;
1668
David Sterbabf31f872020-02-05 17:34:34 +01001669 if (TRANS_ABORTED(trans)) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04001670 if (insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001671 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001672 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001673 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1676 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001677 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678 insert_reserved);
1679 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1680 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001681 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682 insert_reserved);
1683 else
1684 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001685 if (ret && insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001686 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001687 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001688}
1689
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001690static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001691select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001692{
Filipe Mananacffc3372015-07-09 13:13:44 +01001693 struct btrfs_delayed_ref_node *ref;
1694
Liu Boe3d03962018-08-23 03:51:50 +08001695 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001696 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001697
Filipe Mananacffc3372015-07-09 13:13:44 +01001698 /*
1699 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1700 * This is to prevent a ref count from going down to zero, which deletes
1701 * the extent item from the extent tree, when there still are references
1702 * to add, which would fail because they would not find the extent item.
1703 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001704 if (!list_empty(&head->ref_add_list))
1705 return list_first_entry(&head->ref_add_list,
1706 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001707
Liu Boe3d03962018-08-23 03:51:50 +08001708 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001709 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001710 ASSERT(list_empty(&ref->add_list));
1711 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001712}
1713
Josef Bacik2eadaa22017-09-29 15:43:52 -04001714static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1715 struct btrfs_delayed_ref_head *head)
1716{
1717 spin_lock(&delayed_refs->lock);
1718 head->processing = 0;
1719 delayed_refs->num_heads_ready++;
1720 spin_unlock(&delayed_refs->lock);
1721 btrfs_delayed_ref_unlock(head);
1722}
1723
Josef Bacikbedc66172018-12-03 10:20:31 -05001724static struct btrfs_delayed_extent_op *cleanup_extent_op(
1725 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001726{
1727 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001728
1729 if (!extent_op)
1730 return NULL;
1731
1732 if (head->must_insert_reserved) {
1733 head->extent_op = NULL;
1734 btrfs_free_delayed_extent_op(extent_op);
1735 return NULL;
1736 }
1737 return extent_op;
1738}
1739
1740static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1741 struct btrfs_delayed_ref_head *head)
1742{
1743 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001744 int ret;
1745
Josef Bacikbedc66172018-12-03 10:20:31 -05001746 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001747 if (!extent_op)
1748 return 0;
1749 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001750 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001751 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001752 btrfs_free_delayed_extent_op(extent_op);
1753 return ret ? ret : 1;
1754}
1755
Josef Bacik31890da2018-11-21 14:05:41 -05001756void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1757 struct btrfs_delayed_ref_root *delayed_refs,
1758 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001759{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001760 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001761
Josef Bacik9a739912021-01-15 16:48:56 -05001762 /*
1763 * We had csum deletions accounted for in our delayed refs rsv, we need
1764 * to drop the csum leaves for this update from our delayed_refs_rsv.
1765 */
1766 if (head->total_ref_mod < 0 && head->is_data) {
1767 spin_lock(&delayed_refs->lock);
1768 delayed_refs->pending_csums -= head->num_bytes;
1769 spin_unlock(&delayed_refs->lock);
1770 nr_items += btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes);
1771 }
1772
1773 /*
1774 * We were dropping refs, or had a new ref and dropped it, and thus must
1775 * adjust down our total_bytes_pinned, the space may or may not have
1776 * been pinned and so is accounted for properly in the pinned space by
1777 * now.
1778 */
1779 if (head->total_ref_mod < 0 ||
1780 (head->total_ref_mod == 0 && head->must_insert_reserved)) {
Josef Bacik7ec15362021-01-15 16:48:55 -05001781 u64 flags = btrfs_ref_head_to_space_flags(head);
Josef Bacik07c47772018-12-03 10:20:30 -05001782
Josef Bacik7ec15362021-01-15 16:48:55 -05001783 btrfs_mod_total_bytes_pinned(fs_info, flags, -head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001784 }
1785
Josef Bacikba2c4d42018-12-03 10:20:33 -05001786 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001787}
1788
Josef Bacik194ab0b2017-09-29 15:43:54 -04001789static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001790 struct btrfs_delayed_ref_head *head)
1791{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001792
1793 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001794 struct btrfs_delayed_ref_root *delayed_refs;
1795 int ret;
1796
1797 delayed_refs = &trans->transaction->delayed_refs;
1798
Josef Bacikbedc66172018-12-03 10:20:31 -05001799 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001800 if (ret < 0) {
1801 unselect_delayed_ref_head(delayed_refs, head);
1802 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1803 return ret;
1804 } else if (ret) {
1805 return ret;
1806 }
1807
1808 /*
1809 * Need to drop our head ref lock and re-acquire the delayed ref lock
1810 * and then re-check to make sure nobody got added.
1811 */
1812 spin_unlock(&head->lock);
1813 spin_lock(&delayed_refs->lock);
1814 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001815 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001816 spin_unlock(&head->lock);
1817 spin_unlock(&delayed_refs->lock);
1818 return 1;
1819 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001820 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001821 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001822 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001823
Josef Bacikc1103f72017-09-29 15:43:56 -04001824 if (head->must_insert_reserved) {
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001825 btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001826 if (head->is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00001827 ret = btrfs_del_csums(trans, fs_info->csum_root,
1828 head->bytenr, head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001829 }
1830 }
1831
Josef Bacik31890da2018-11-21 14:05:41 -05001832 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001833
1834 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001835 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001836 btrfs_put_delayed_ref_head(head);
Josef Bacik466d83f2021-05-19 10:52:46 -04001837 return ret;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001838}
1839
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001840static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1841 struct btrfs_trans_handle *trans)
1842{
1843 struct btrfs_delayed_ref_root *delayed_refs =
1844 &trans->transaction->delayed_refs;
1845 struct btrfs_delayed_ref_head *head = NULL;
1846 int ret;
1847
1848 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001849 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001850 if (!head) {
1851 spin_unlock(&delayed_refs->lock);
1852 return head;
1853 }
1854
1855 /*
1856 * Grab the lock that says we are going to process all the refs for
1857 * this head
1858 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001859 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001860 spin_unlock(&delayed_refs->lock);
1861
1862 /*
1863 * We may have dropped the spin lock to get the head mutex lock, and
1864 * that might have given someone else time to free the head. If that's
1865 * true, it has been removed from our list and we can move on.
1866 */
1867 if (ret == -EAGAIN)
1868 head = ERR_PTR(-EAGAIN);
1869
1870 return head;
1871}
1872
Nikolay Borisove7261382018-08-15 10:39:55 +03001873static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1874 struct btrfs_delayed_ref_head *locked_ref,
1875 unsigned long *run_refs)
1876{
1877 struct btrfs_fs_info *fs_info = trans->fs_info;
1878 struct btrfs_delayed_ref_root *delayed_refs;
1879 struct btrfs_delayed_extent_op *extent_op;
1880 struct btrfs_delayed_ref_node *ref;
1881 int must_insert_reserved = 0;
1882 int ret;
1883
1884 delayed_refs = &trans->transaction->delayed_refs;
1885
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001886 lockdep_assert_held(&locked_ref->mutex);
1887 lockdep_assert_held(&locked_ref->lock);
1888
Nikolay Borisove7261382018-08-15 10:39:55 +03001889 while ((ref = select_delayed_ref(locked_ref))) {
1890 if (ref->seq &&
1891 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1892 spin_unlock(&locked_ref->lock);
1893 unselect_delayed_ref_head(delayed_refs, locked_ref);
1894 return -EAGAIN;
1895 }
1896
1897 (*run_refs)++;
1898 ref->in_tree = 0;
1899 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1900 RB_CLEAR_NODE(&ref->ref_node);
1901 if (!list_empty(&ref->add_list))
1902 list_del(&ref->add_list);
1903 /*
1904 * When we play the delayed ref, also correct the ref_mod on
1905 * head
1906 */
1907 switch (ref->action) {
1908 case BTRFS_ADD_DELAYED_REF:
1909 case BTRFS_ADD_DELAYED_EXTENT:
1910 locked_ref->ref_mod -= ref->ref_mod;
1911 break;
1912 case BTRFS_DROP_DELAYED_REF:
1913 locked_ref->ref_mod += ref->ref_mod;
1914 break;
1915 default:
1916 WARN_ON(1);
1917 }
1918 atomic_dec(&delayed_refs->num_entries);
1919
1920 /*
1921 * Record the must_insert_reserved flag before we drop the
1922 * spin lock.
1923 */
1924 must_insert_reserved = locked_ref->must_insert_reserved;
1925 locked_ref->must_insert_reserved = 0;
1926
1927 extent_op = locked_ref->extent_op;
1928 locked_ref->extent_op = NULL;
1929 spin_unlock(&locked_ref->lock);
1930
1931 ret = run_one_delayed_ref(trans, ref, extent_op,
1932 must_insert_reserved);
1933
1934 btrfs_free_delayed_extent_op(extent_op);
1935 if (ret) {
1936 unselect_delayed_ref_head(delayed_refs, locked_ref);
1937 btrfs_put_delayed_ref(ref);
1938 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1939 ret);
1940 return ret;
1941 }
1942
1943 btrfs_put_delayed_ref(ref);
1944 cond_resched();
1945
1946 spin_lock(&locked_ref->lock);
1947 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1948 }
1949
1950 return 0;
1951}
1952
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001953/*
1954 * Returns 0 on success or if called with an already aborted transaction.
1955 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
1956 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05001957static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05001958 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04001959{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02001960 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04001961 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001962 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05001963 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04001964 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001965 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05001966 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001967
1968 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001969 do {
Chris Mason56bec292009-03-13 10:10:06 -04001970 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001971 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001972 if (IS_ERR_OR_NULL(locked_ref)) {
1973 if (PTR_ERR(locked_ref) == -EAGAIN) {
1974 continue;
1975 } else {
1976 break;
1977 }
Chris Mason56bec292009-03-13 10:10:06 -04001978 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001979 count++;
Chris Mason56bec292009-03-13 10:10:06 -04001980 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01001981 /*
1982 * We need to try and merge add/drops of the same ref since we
1983 * can run into issues with relocate dropping the implicit ref
1984 * and then it being added back again before the drop can
1985 * finish. If we merged anything we need to re-loop so we can
1986 * get a good ref.
1987 * Or we can get node references of the same type that weren't
1988 * merged when created due to bumps in the tree mod seq, and
1989 * we need to merge them to prevent adding an inline extent
1990 * backref before dropping it (triggering a BUG_ON at
1991 * insert_inline_extent_backref()).
1992 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05001993 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03001994 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04001995
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001996 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
1997 &actual_count);
1998 if (ret < 0 && ret != -EAGAIN) {
1999 /*
2000 * Error, btrfs_run_delayed_refs_for_head already
2001 * unlocked everything so just bail out
2002 */
2003 return ret;
2004 } else if (!ret) {
2005 /*
2006 * Success, perform the usual cleanup of a processed
2007 * head
2008 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002009 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002010 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002011 /* We dropped our lock, we need to loop. */
2012 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002013 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002014 } else if (ret) {
2015 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002016 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002017 }
Chris Mason56bec292009-03-13 10:10:06 -04002018
Josef Bacikb00e6252017-09-29 15:43:53 -04002019 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002020 * Either success case or btrfs_run_delayed_refs_for_head
2021 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002022 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002023
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002024 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002025 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002026 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002027
2028 /*
2029 * We don't want to include ref heads since we can have empty ref heads
2030 * and those will drastically skew our runtime down since we just do
2031 * accounting, no actual extent tree updates.
2032 */
2033 if (actual_count > 0) {
2034 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2035 u64 avg;
2036
2037 /*
2038 * We weigh the current average higher than our current runtime
2039 * to avoid large swings in the average.
2040 */
2041 spin_lock(&delayed_refs->lock);
2042 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002043 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002044 spin_unlock(&delayed_refs->lock);
2045 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002046 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002047}
2048
Arne Jansen709c0482011-09-12 12:22:57 +02002049#ifdef SCRAMBLE_DELAYED_REFS
2050/*
2051 * Normally delayed refs get processed in ascending bytenr order. This
2052 * correlates in most cases to the order added. To expose dependencies on this
2053 * order, we start to process the tree in the middle instead of the beginning
2054 */
2055static u64 find_middle(struct rb_root *root)
2056{
2057 struct rb_node *n = root->rb_node;
2058 struct btrfs_delayed_ref_node *entry;
2059 int alt = 1;
2060 u64 middle;
2061 u64 first = 0, last = 0;
2062
2063 n = rb_first(root);
2064 if (n) {
2065 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2066 first = entry->bytenr;
2067 }
2068 n = rb_last(root);
2069 if (n) {
2070 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2071 last = entry->bytenr;
2072 }
2073 n = root->rb_node;
2074
2075 while (n) {
2076 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2077 WARN_ON(!entry->in_tree);
2078
2079 middle = entry->bytenr;
2080
2081 if (alt)
2082 n = n->rb_left;
2083 else
2084 n = n->rb_right;
2085
2086 alt = 1 - alt;
2087 }
2088 return middle;
2089}
2090#endif
2091
Josef Bacik12621332015-02-03 07:50:16 -08002092/*
2093 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2094 * would require to store the csums for that many bytes.
2095 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002096u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002097{
2098 u64 csum_size;
2099 u64 num_csums_per_leaf;
2100 u64 num_csums;
2101
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002102 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002103 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002104 (u64)btrfs_super_csum_size(fs_info->super_copy));
2105 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002106 num_csums += num_csums_per_leaf - 1;
2107 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2108 return num_csums;
2109}
2110
Chris Masonc3e69d52009-03-13 10:17:05 -04002111/*
2112 * this starts processing the delayed reference count updates and
2113 * extent insertions we have queued up so far. count can be
2114 * 0, which means to process everything in the tree at the start
2115 * of the run (but not newly added entries), or it can be some target
2116 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002117 *
2118 * Returns 0 on success or if called with an aborted transaction
2119 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002120 */
2121int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002122 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002123{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002124 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002125 struct rb_node *node;
2126 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002127 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002128 int ret;
2129 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002130
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002131 /* We'll clean this up in btrfs_cleanup_transaction */
David Sterbabf31f872020-02-05 17:34:34 +01002132 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002133 return 0;
2134
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002135 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002136 return 0;
2137
Chris Masonc3e69d52009-03-13 10:17:05 -04002138 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002139 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002140 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002141
Chris Masonc3e69d52009-03-13 10:17:05 -04002142again:
Arne Jansen709c0482011-09-12 12:22:57 +02002143#ifdef SCRAMBLE_DELAYED_REFS
2144 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2145#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002146 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002147 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002148 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002149 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002150 }
2151
Chris Mason56bec292009-03-13 10:10:06 -04002152 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002153 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002154
Josef Bacikd7df2c72014-01-23 09:21:38 -05002155 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002156 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002157 if (!node) {
2158 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002159 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002160 }
Josef Bacikd2788502017-09-29 15:43:57 -04002161 head = rb_entry(node, struct btrfs_delayed_ref_head,
2162 href_node);
2163 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002164 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002165
2166 /* Mutex was contended, block until it's released and retry. */
2167 mutex_lock(&head->mutex);
2168 mutex_unlock(&head->mutex);
2169
2170 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002171 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002172 goto again;
2173 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002174out:
Chris Masona28ec192007-03-06 20:08:01 -05002175 return 0;
2176}
2177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002178int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
David Sterba42c9d0b2019-03-20 11:54:13 +01002179 struct extent_buffer *eb, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002180 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002181{
2182 struct btrfs_delayed_extent_op *extent_op;
2183 int ret;
2184
Miao Xie78a61842012-11-21 02:21:28 +00002185 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002186 if (!extent_op)
2187 return -ENOMEM;
2188
2189 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002190 extent_op->update_flags = true;
2191 extent_op->update_key = false;
2192 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002193 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194
David Sterba42c9d0b2019-03-20 11:54:13 +01002195 ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002197 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 return ret;
2199}
2200
Liu Boe4c3b2d2017-01-30 12:25:28 -08002201static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 struct btrfs_path *path,
2203 u64 objectid, u64 offset, u64 bytenr)
2204{
2205 struct btrfs_delayed_ref_head *head;
2206 struct btrfs_delayed_ref_node *ref;
2207 struct btrfs_delayed_data_ref *data_ref;
2208 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002209 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002210 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002211 int ret = 0;
2212
ethanwu998ac6d2018-04-29 15:59:42 +08002213 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002214 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002215 if (cur_trans)
2216 refcount_inc(&cur_trans->use_count);
2217 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002218 if (!cur_trans)
2219 return 0;
2220
2221 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002223 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002224 if (!head) {
2225 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002226 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002227 return 0;
2228 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229
2230 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002231 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002232 spin_unlock(&delayed_refs->lock);
2233
David Sterbab3b4aa72011-04-21 01:20:15 +02002234 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002235
David Sterba8cc33e52011-05-02 15:29:25 +02002236 /*
2237 * Mutex was contended, block until it's released and let
2238 * caller try again
2239 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240 mutex_lock(&head->mutex);
2241 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002242 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002243 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002244 return -EAGAIN;
2245 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002247
2248 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002249 /*
2250 * XXX: We should replace this with a proper search function in the
2251 * future.
2252 */
Liu Boe3d03962018-08-23 03:51:50 +08002253 for (node = rb_first_cached(&head->ref_tree); node;
2254 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002255 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002256 /* If it's a shared ref we know a cross reference exists */
2257 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2258 ret = 1;
2259 break;
2260 }
2261
2262 data_ref = btrfs_delayed_node_to_data_ref(ref);
2263
2264 /*
2265 * If our ref doesn't match the one we're currently looking at
2266 * then we have a cross reference.
2267 */
2268 if (data_ref->root != root->root_key.objectid ||
2269 data_ref->objectid != objectid ||
2270 data_ref->offset != offset) {
2271 ret = 1;
2272 break;
2273 }
2274 }
2275 spin_unlock(&head->lock);
2276 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002277 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002278 return ret;
2279}
2280
Liu Boe4c3b2d2017-01-30 12:25:28 -08002281static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002282 struct btrfs_path *path,
Boris Burkova84d5d42020-08-18 11:00:05 -07002283 u64 objectid, u64 offset, u64 bytenr,
2284 bool strict)
Chris Masonbe20aa92007-12-17 20:14:01 -05002285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002286 struct btrfs_fs_info *fs_info = root->fs_info;
2287 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002288 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002289 struct btrfs_extent_data_ref *ref;
2290 struct btrfs_extent_inline_ref *iref;
2291 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002292 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002294 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002295 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002296
Chris Masonbe20aa92007-12-17 20:14:01 -05002297 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002298 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002299 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002300
Chris Masonbe20aa92007-12-17 20:14:01 -05002301 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2302 if (ret < 0)
2303 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002304 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002305
2306 ret = -ENOENT;
2307 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002308 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002309
Zheng Yan31840ae2008-09-23 13:14:14 -04002310 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002311 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002312 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002313
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002314 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002315 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002316
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002317 ret = 1;
2318 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002319 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2320
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002321 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002322 if (item_size != sizeof(*ei) +
2323 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2324 goto out;
2325
Boris Burkova84d5d42020-08-18 11:00:05 -07002326 /*
2327 * If extent created before last snapshot => it's shared unless the
2328 * snapshot has been deleted. Use the heuristic if strict is false.
2329 */
2330 if (!strict &&
2331 (btrfs_extent_generation(leaf, ei) <=
2332 btrfs_root_last_snapshot(&root->root_item)))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002333 goto out;
2334
2335 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002336
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002337 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002338 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2339 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002340 goto out;
2341
2342 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2343 if (btrfs_extent_refs(leaf, ei) !=
2344 btrfs_extent_data_ref_count(leaf, ref) ||
2345 btrfs_extent_data_ref_root(leaf, ref) !=
2346 root->root_key.objectid ||
2347 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2348 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2349 goto out;
2350
Yan Zhengf321e492008-07-30 09:26:11 -04002351 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002352out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002353 return ret;
2354}
2355
Liu Boe4c3b2d2017-01-30 12:25:28 -08002356int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
Boris Burkova84d5d42020-08-18 11:00:05 -07002357 u64 bytenr, bool strict)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002358{
2359 struct btrfs_path *path;
2360 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002361
2362 path = btrfs_alloc_path();
2363 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002364 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365
2366 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002367 ret = check_committed_ref(root, path, objectid,
Boris Burkova84d5d42020-08-18 11:00:05 -07002368 offset, bytenr, strict);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002369 if (ret && ret != -ENOENT)
2370 goto out;
2371
Misono Tomohiro380fd062018-08-30 10:59:16 +09002372 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2373 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375out:
Yan Zhengf321e492008-07-30 09:26:11 -04002376 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002377 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2378 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002379 return ret;
2380}
2381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002383 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002384 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002385 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002386{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002387 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002388 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002389 u64 num_bytes;
2390 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002391 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002392 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002393 struct btrfs_key key;
2394 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002395 struct btrfs_ref generic_ref = { 0 };
2396 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002397 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002398 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002399 int level;
2400 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002402 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002403 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002404
Zheng Yan31840ae2008-09-23 13:14:14 -04002405 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 nritems = btrfs_header_nritems(buf);
2407 level = btrfs_header_level(buf);
2408
Qu Wenruo92a7cc42020-05-15 14:01:40 +08002409 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002411
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002412 if (full_backref)
2413 parent = buf->start;
2414 else
2415 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002416 if (inc)
2417 action = BTRFS_ADD_DELAYED_REF;
2418 else
2419 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002420
Zheng Yan31840ae2008-09-23 13:14:14 -04002421 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002422 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002423 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002424 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002425 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002426 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002427 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002428 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002429 BTRFS_FILE_EXTENT_INLINE)
2430 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002431 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2432 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002434
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002435 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2436 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002437 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2438 num_bytes, parent);
2439 generic_ref.real_root = root->root_key.objectid;
2440 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2441 key.offset);
2442 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002443 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002444 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002445 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002446 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 if (ret)
2448 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002449 } else {
2450 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002451 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002452 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2453 num_bytes, parent);
2454 generic_ref.real_root = root->root_key.objectid;
2455 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2456 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002457 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002458 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002459 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002460 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002461 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002462 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002463 }
2464 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002465 return 0;
2466fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002467 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002468}
2469
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002471 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002472{
Josef Bacike339a6b2014-07-02 10:54:25 -07002473 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474}
Zheng Yan31840ae2008-09-23 13:14:14 -04002475
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002476int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002477 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002478{
Josef Bacike339a6b2014-07-02 10:54:25 -07002479 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002480}
2481
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002482int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002483{
David Sterba32da53862019-10-29 19:20:18 +01002484 struct btrfs_block_group *block_group;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002485 int readonly = 0;
2486
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002487 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002488 if (!block_group || block_group->ro)
2489 readonly = 1;
2490 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002491 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002492 return readonly;
2493}
2494
Jeff Mahoney1b868262017-05-17 11:38:35 -04002495static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002497 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002498 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002499 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002500
Yan, Zhengb742bb822010-05-16 10:46:24 -04002501 if (data)
2502 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002504 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2505 else
2506 flags = BTRFS_BLOCK_GROUP_METADATA;
2507
Josef Bacik878d7b62019-06-20 15:38:05 -04002508 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002509 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002510}
2511
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002512static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002513{
David Sterba32da53862019-10-29 19:20:18 +01002514 struct btrfs_block_group *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002515 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002516
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 spin_lock(&fs_info->block_group_cache_lock);
2518 bytenr = fs_info->first_logical_byte;
2519 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002520
2521 if (bytenr < (u64)-1)
2522 return bytenr;
2523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002524 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002525 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002526 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002527
David Sterbab3470b52019-10-23 18:48:22 +02002528 bytenr = cache->start;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002529 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002530
2531 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002532}
2533
Nikolay Borisov6690d072020-01-20 16:09:14 +02002534static int pin_down_extent(struct btrfs_trans_handle *trans,
2535 struct btrfs_block_group *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002536 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002537{
David Sterbafdf08602019-03-20 12:12:32 +01002538 struct btrfs_fs_info *fs_info = cache->fs_info;
2539
Yan Zheng11833d62009-09-11 16:11:19 -04002540 spin_lock(&cache->space_info->lock);
2541 spin_lock(&cache->lock);
2542 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002543 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2544 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002545 if (reserved) {
2546 cache->reserved -= num_bytes;
2547 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002548 }
Yan Zheng11833d62009-09-11 16:11:19 -04002549 spin_unlock(&cache->lock);
2550 spin_unlock(&cache->space_info->lock);
2551
Josef Bacik7ec15362021-01-15 16:48:55 -05002552 __btrfs_mod_total_bytes_pinned(cache->space_info, num_bytes);
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002553 set_extent_dirty(&trans->transaction->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002554 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002555 return 0;
2556}
Chris Mason9078a3e2007-04-26 16:46:15 -04002557
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002558int btrfs_pin_extent(struct btrfs_trans_handle *trans,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002559 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002560{
David Sterba32da53862019-10-29 19:20:18 +01002561 struct btrfs_block_group *cache;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002562
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002563 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002564 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002565
Nikolay Borisov6690d072020-01-20 16:09:14 +02002566 pin_down_extent(trans, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002567
2568 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002569 return 0;
2570}
Zheng Yane8569812008-09-26 10:05:48 -04002571
Yan, Zhengf0486c62010-05-16 10:46:25 -04002572/*
Chris Masone688b7252011-10-31 20:52:39 -04002573 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002574 */
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002575int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
Chris Masone688b7252011-10-31 20:52:39 -04002576 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002577{
David Sterba32da53862019-10-29 19:20:18 +01002578 struct btrfs_block_group *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002579 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002580
Nikolay Borisovf2fb7292020-01-20 16:09:16 +02002581 btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes);
2582
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002583 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002584 if (!cache)
2585 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002586
2587 /*
2588 * pull in the free space cache (if any) so that our pin
2589 * removes the free space from the cache. We have load_only set
2590 * to one because the slow code to read in the free extents does check
2591 * the pinned extents.
2592 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002593 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002594
Nikolay Borisov6690d072020-01-20 16:09:14 +02002595 pin_down_extent(trans, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002596
2597 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002598 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002599 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002600 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002601}
2602
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002603static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2604 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002605{
2606 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002607 struct btrfs_block_group *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002608 struct btrfs_caching_control *caching_ctl;
2609
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002610 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002611 if (!block_group)
2612 return -EINVAL;
2613
Josef Bacik676f1f72019-06-20 15:37:48 -04002614 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002615 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002616
2617 if (!caching_ctl) {
2618 /* Logic error */
David Sterba32da53862019-10-29 19:20:18 +01002619 BUG_ON(!btrfs_block_group_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002620 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2621 } else {
2622 mutex_lock(&caching_ctl->mutex);
2623
2624 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002625 ret = btrfs_add_excluded_extent(fs_info, start,
2626 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002627 } else if (start + num_bytes <= caching_ctl->progress) {
2628 ret = btrfs_remove_free_space(block_group,
2629 start, num_bytes);
2630 } else {
2631 num_bytes = caching_ctl->progress - start;
2632 ret = btrfs_remove_free_space(block_group,
2633 start, num_bytes);
2634 if (ret)
2635 goto out_lock;
2636
2637 num_bytes = (start + num_bytes) -
2638 caching_ctl->progress;
2639 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002640 ret = btrfs_add_excluded_extent(fs_info, start,
2641 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002642 }
2643out_lock:
2644 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002645 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002646 }
2647 btrfs_put_block_group(block_group);
2648 return ret;
2649}
2650
David Sterbabcdc4282019-03-20 12:14:33 +01002651int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002652{
David Sterbabcdc4282019-03-20 12:14:33 +01002653 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002654 struct btrfs_file_extent_item *item;
2655 struct btrfs_key key;
2656 int found_type;
2657 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002658 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002659
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002660 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002661 return 0;
2662
2663 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2664 btrfs_item_key_to_cpu(eb, &key, i);
2665 if (key.type != BTRFS_EXTENT_DATA_KEY)
2666 continue;
2667 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2668 found_type = btrfs_file_extent_type(eb, item);
2669 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2670 continue;
2671 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2672 continue;
2673 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2674 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002675 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2676 if (ret)
2677 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002678 }
2679
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002680 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002681}
2682
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002683static void
David Sterba32da53862019-10-29 19:20:18 +01002684btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002685{
2686 atomic_inc(&bg->reservations);
2687}
2688
Josef Bacikc759c4e2015-10-02 15:25:10 -04002689/*
2690 * Returns the free cluster for the given space info and sets empty_cluster to
2691 * what it should be based on the mount options.
2692 */
2693static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002694fetch_cluster_info(struct btrfs_fs_info *fs_info,
2695 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002696{
2697 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002698
2699 *empty_cluster = 0;
2700 if (btrfs_mixed_space_info(space_info))
2701 return ret;
2702
Josef Bacikc759c4e2015-10-02 15:25:10 -04002703 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002704 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002705 if (btrfs_test_opt(fs_info, SSD))
2706 *empty_cluster = SZ_2M;
2707 else
Byongho Leeee221842015-12-15 01:42:10 +09002708 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002709 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2710 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2711 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002712 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002713 }
2714
2715 return ret;
2716}
2717
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002718static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2719 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002720 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002721{
David Sterba32da53862019-10-29 19:20:18 +01002722 struct btrfs_block_group *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002723 struct btrfs_space_info *space_info;
2724 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002725 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002726 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002727 u64 total_unpinned = 0;
2728 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002729 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002730
2731 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002732 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002733 if (!cache ||
David Sterbab3470b52019-10-23 18:48:22 +02002734 start >= cache->start + cache->length) {
Yan Zheng11833d62009-09-11 16:11:19 -04002735 if (cache)
2736 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002737 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002738 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002739 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002740
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002741 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002742 cache->space_info,
2743 &empty_cluster);
2744 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002745 }
2746
David Sterbab3470b52019-10-23 18:48:22 +02002747 len = cache->start + cache->length - start;
Yan Zheng11833d62009-09-11 16:11:19 -04002748 len = min(len, end + 1 - start);
2749
Josef Bacik56d16542020-10-23 09:58:04 -04002750 if (start < cache->last_byte_to_unpin && return_free_space) {
2751 u64 add_len = min(len, cache->last_byte_to_unpin - start);
2752
2753 btrfs_add_free_space(cache, start, add_len);
Yan Zheng11833d62009-09-11 16:11:19 -04002754 }
Josef Bacik25179202008-10-29 14:49:05 -04002755
Yan, Zhengf0486c62010-05-16 10:46:25 -04002756 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002757 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002758 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002759
Josef Bacikc759c4e2015-10-02 15:25:10 -04002760 /*
2761 * If this space cluster has been marked as fragmented and we've
2762 * unpinned enough in this block group to potentially allow a
2763 * cluster to be created inside of it go ahead and clear the
2764 * fragmented check.
2765 */
2766 if (cluster && cluster->fragmented &&
2767 total_unpinned > empty_cluster) {
2768 spin_lock(&cluster->lock);
2769 cluster->fragmented = 0;
2770 spin_unlock(&cluster->lock);
2771 }
2772
Josef Bacik7b398f82012-10-22 15:52:28 -04002773 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002774 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002775 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002776 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacik4f4db212015-09-29 11:40:47 -04002777 space_info->max_extent_size = 0;
Josef Bacik7ec15362021-01-15 16:48:55 -05002778 __btrfs_mod_total_bytes_pinned(space_info, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04002779 if (cache->ro) {
2780 space_info->bytes_readonly += len;
2781 readonly = true;
2782 }
Josef Bacik25179202008-10-29 14:49:05 -04002783 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002784 if (!readonly && return_free_space &&
2785 global_rsv->space_info == space_info) {
2786 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002787
Josef Bacik7b398f82012-10-22 15:52:28 -04002788 spin_lock(&global_rsv->lock);
2789 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002790 to_add = min(len, global_rsv->size -
2791 global_rsv->reserved);
2792 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002793 btrfs_space_info_update_bytes_may_use(fs_info,
2794 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002795 if (global_rsv->reserved >= global_rsv->size)
2796 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002797 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002798 }
2799 spin_unlock(&global_rsv->lock);
2800 }
Josef Bacik27327982020-07-21 10:22:18 -04002801 /* Add to any tickets we may have */
2802 if (!readonly && return_free_space && len)
2803 btrfs_try_granting_tickets(fs_info, space_info);
Josef Bacik7b398f82012-10-22 15:52:28 -04002804 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002805 }
2806
2807 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002808 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002809 return 0;
2810}
2811
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002812int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002813{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002814 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002815 struct btrfs_block_group *block_group, *tmp;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002816 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002817 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002818 u64 start;
2819 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002820 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002821
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002822 unpin = &trans->transaction->pinned_extents;
Yan Zheng11833d62009-09-11 16:11:19 -04002823
David Sterbabf31f872020-02-05 17:34:34 +01002824 while (!TRANS_ABORTED(trans)) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002825 struct extent_state *cached_state = NULL;
2826
Filipe Mananad4b450c2015-01-29 19:18:25 +00002827 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002828 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002829 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002830 if (ret) {
2831 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002832 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002833 }
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002834 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
2835 clear_extent_bits(&fs_info->excluded_extents, start,
Nikolay Borisovf2fb7292020-01-20 16:09:16 +02002836 end, EXTENT_UPTODATE);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002837
Dennis Zhou46b27f52019-12-13 16:22:11 -08002838 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002839 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002840 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002841
Filipe Manana0e6ec382018-11-16 13:04:44 +00002842 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002843 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002844 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002845 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002846 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002847 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002848
Dennis Zhoua2309302020-01-02 16:26:35 -05002849 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
2850 btrfs_discard_calc_delay(&fs_info->discard_ctl);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002851 btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -05002852 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002853
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002854 /*
2855 * Transaction is finished. We don't need the lock anymore. We
2856 * do need to clean up the block groups in case of a transaction
2857 * abort.
2858 */
2859 deleted_bgs = &trans->transaction->deleted_bgs;
2860 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2861 u64 trimmed = 0;
2862
2863 ret = -EROFS;
David Sterbabf31f872020-02-05 17:34:34 +01002864 if (!TRANS_ABORTED(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002865 ret = btrfs_discard_extent(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02002866 block_group->start,
2867 block_group->length,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002868 &trimmed);
2869
2870 list_del_init(&block_group->bg_list);
Filipe Manana6b7304a2020-05-08 11:01:47 +01002871 btrfs_unfreeze_block_group(block_group);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002872 btrfs_put_block_group(block_group);
2873
2874 if (ret) {
2875 const char *errstr = btrfs_decode_error(ret);
2876 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002877 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002878 ret, errstr);
2879 }
2880 }
2881
Chris Masone20d96d2007-03-22 12:13:20 -04002882 return 0;
2883}
2884
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08002885/*
2886 * Drop one or more refs of @node.
2887 *
2888 * 1. Locate the extent refs.
2889 * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item.
2890 * Locate it, then reduce the refs number or remove the ref line completely.
2891 *
2892 * 2. Update the refs count in EXTENT/METADATA_ITEM
2893 *
2894 * Inline backref case:
2895 *
2896 * in extent tree we have:
2897 *
2898 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
2899 * refs 2 gen 6 flags DATA
2900 * extent data backref root FS_TREE objectid 258 offset 0 count 1
2901 * extent data backref root FS_TREE objectid 257 offset 0 count 1
2902 *
2903 * This function gets called with:
2904 *
2905 * node->bytenr = 13631488
2906 * node->num_bytes = 1048576
2907 * root_objectid = FS_TREE
2908 * owner_objectid = 257
2909 * owner_offset = 0
2910 * refs_to_drop = 1
2911 *
2912 * Then we should get some like:
2913 *
2914 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
2915 * refs 1 gen 6 flags DATA
2916 * extent data backref root FS_TREE objectid 258 offset 0 count 1
2917 *
2918 * Keyed backref case:
2919 *
2920 * in extent tree we have:
2921 *
2922 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
2923 * refs 754 gen 6 flags DATA
2924 * [...]
2925 * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28
2926 * extent data backref root FS_TREE objectid 866 offset 0 count 1
2927 *
2928 * This function get called with:
2929 *
2930 * node->bytenr = 13631488
2931 * node->num_bytes = 1048576
2932 * root_objectid = FS_TREE
2933 * owner_objectid = 866
2934 * owner_offset = 0
2935 * refs_to_drop = 1
2936 *
2937 * Then we should get some like:
2938 *
2939 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
2940 * refs 753 gen 6 flags DATA
2941 *
2942 * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.
2943 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002944static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002945 struct btrfs_delayed_ref_node *node, u64 parent,
2946 u64 root_objectid, u64 owner_objectid,
2947 u64 owner_offset, int refs_to_drop,
2948 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002949{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002950 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002951 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002952 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002953 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002954 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002955 struct btrfs_extent_item *ei;
2956 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002957 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002958 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002959 int extent_slot = 0;
2960 int found_extent = 0;
2961 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002962 u32 item_size;
2963 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002964 u64 bytenr = node->bytenr;
2965 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002966 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002967 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002968
Chris Mason5caf2a02007-04-02 11:20:42 -04002969 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002970 if (!path)
2971 return -ENOMEM;
2972
Chris Masonb9473432009-03-13 11:00:37 -04002973 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974
2975 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08002976
2977 if (!is_data && refs_to_drop != 1) {
2978 btrfs_crit(info,
2979"invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u",
2980 node->bytenr, refs_to_drop);
2981 ret = -EINVAL;
2982 btrfs_abort_transaction(trans, ret);
2983 goto out;
2984 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985
Josef Bacik3173a182013-03-07 14:22:04 -05002986 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02002987 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05002988
Nikolay Borisovfbe48012018-06-20 15:48:52 +03002989 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
2990 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002991 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05002992 if (ret == 0) {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08002993 /*
2994 * Either the inline backref or the SHARED_DATA_REF/
2995 * SHARED_BLOCK_REF is found
2996 *
2997 * Here is a quick path to locate EXTENT/METADATA_ITEM.
2998 * It's possible the EXTENT/METADATA_ITEM is near current slot.
2999 */
Chris Mason952fcca2008-02-18 16:33:44 -05003000 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003001 while (extent_slot >= 0) {
3002 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003003 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003004 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003005 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3007 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003008 found_extent = 1;
3009 break;
3010 }
Josef Bacik3173a182013-03-07 14:22:04 -05003011 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3012 key.offset == owner_objectid) {
3013 found_extent = 1;
3014 break;
3015 }
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003016
3017 /* Quick path didn't find the EXTEMT/METADATA_ITEM */
Chris Mason952fcca2008-02-18 16:33:44 -05003018 if (path->slots[0] - extent_slot > 5)
3019 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003021 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003022
Zheng Yan31840ae2008-09-23 13:14:14 -04003023 if (!found_extent) {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003024 if (iref) {
3025 btrfs_crit(info,
3026"invalid iref, no EXTENT/METADATA_ITEM found but has inline extent ref");
3027 btrfs_abort_transaction(trans, -EUCLEAN);
3028 goto err_dump;
3029 }
3030 /* Must be SHARED_* item, remove the backref first */
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003031 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003032 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003033 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003034 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003035 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003036 goto out;
3037 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003038 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003039 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003040
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003041 /* Slow path to locate EXTENT/METADATA_ITEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003042 key.objectid = bytenr;
3043 key.type = BTRFS_EXTENT_ITEM_KEY;
3044 key.offset = num_bytes;
3045
Josef Bacik3173a182013-03-07 14:22:04 -05003046 if (!is_data && skinny_metadata) {
3047 key.type = BTRFS_METADATA_ITEM_KEY;
3048 key.offset = owner_objectid;
3049 }
3050
Zheng Yan31840ae2008-09-23 13:14:14 -04003051 ret = btrfs_search_slot(trans, extent_root,
3052 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003053 if (ret > 0 && skinny_metadata && path->slots[0]) {
3054 /*
3055 * Couldn't find our skinny metadata item,
3056 * see if we have ye olde extent item.
3057 */
3058 path->slots[0]--;
3059 btrfs_item_key_to_cpu(path->nodes[0], &key,
3060 path->slots[0]);
3061 if (key.objectid == bytenr &&
3062 key.type == BTRFS_EXTENT_ITEM_KEY &&
3063 key.offset == num_bytes)
3064 ret = 0;
3065 }
3066
3067 if (ret > 0 && skinny_metadata) {
3068 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003069 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003070 key.type = BTRFS_EXTENT_ITEM_KEY;
3071 key.offset = num_bytes;
3072 btrfs_release_path(path);
3073 ret = btrfs_search_slot(trans, extent_root,
3074 &key, path, -1, 1);
3075 }
3076
Josef Bacikf3465ca2008-11-12 14:19:50 -05003077 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003078 btrfs_err(info,
3079 "umm, got %d back from search, was looking for %llu",
3080 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003081 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003082 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003083 }
David Sterba005d6422012-09-18 07:52:32 -06003084 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003085 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003086 goto out;
3087 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003088 extent_slot = path->slots[0];
3089 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303090 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003091 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003092 btrfs_err(info,
3093 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003094 bytenr, parent, root_objectid, owner_objectid,
3095 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003096 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003097 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003098 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003099 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003100 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003101 }
Chris Mason5f39d392007-10-15 16:14:19 -04003102
3103 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003104 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003105 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003106 ret = -EINVAL;
3107 btrfs_print_v0_err(info);
3108 btrfs_abort_transaction(trans, ret);
3109 goto out;
3110 }
Chris Mason952fcca2008-02-18 16:33:44 -05003111 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003112 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003113 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3114 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 struct btrfs_tree_block_info *bi;
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003116 if (item_size < sizeof(*ei) + sizeof(*bi)) {
3117 btrfs_crit(info,
Pujin Shicad69d12020-10-14 11:24:19 +08003118"invalid extent item size for key (%llu, %u, %llu) owner %llu, has %u expect >= %zu",
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003119 key.objectid, key.type, key.offset,
3120 owner_objectid, item_size,
3121 sizeof(*ei) + sizeof(*bi));
3122 btrfs_abort_transaction(trans, -EUCLEAN);
3123 goto err_dump;
3124 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003125 bi = (struct btrfs_tree_block_info *)(ei + 1);
3126 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003127 }
3128
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003129 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003130 if (refs < refs_to_drop) {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003131 btrfs_crit(info,
3132 "trying to drop %d refs but we only have %llu for bytenr %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003133 refs_to_drop, refs, bytenr);
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003134 btrfs_abort_transaction(trans, -EUCLEAN);
3135 goto err_dump;
Josef Bacik32b02532013-04-24 16:38:50 -04003136 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003137 refs -= refs_to_drop;
3138
3139 if (refs > 0) {
3140 if (extent_op)
3141 __run_delayed_extent_op(extent_op, leaf, ei);
3142 /*
3143 * In the case of inline back ref, reference count will
3144 * be updated by remove_extent_backref
3145 */
3146 if (iref) {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003147 if (!found_extent) {
3148 btrfs_crit(info,
3149"invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found");
3150 btrfs_abort_transaction(trans, -EUCLEAN);
3151 goto err_dump;
3152 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003153 } else {
3154 btrfs_set_extent_refs(leaf, ei, refs);
3155 btrfs_mark_buffer_dirty(leaf);
3156 }
3157 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003158 ret = remove_extent_backref(trans, path, iref,
3159 refs_to_drop, is_data,
3160 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003161 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003162 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003163 goto out;
3164 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 }
3166 } else {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003167 /* In this branch refs == 1 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003168 if (found_extent) {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003169 if (is_data && refs_to_drop !=
3170 extent_data_ref_count(path, iref)) {
3171 btrfs_crit(info,
3172 "invalid refs_to_drop, current refs %u refs_to_drop %u",
3173 extent_data_ref_count(path, iref),
3174 refs_to_drop);
3175 btrfs_abort_transaction(trans, -EUCLEAN);
3176 goto err_dump;
3177 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003178 if (iref) {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003179 if (path->slots[0] != extent_slot) {
3180 btrfs_crit(info,
3181"invalid iref, extent item key (%llu %u %llu) doesn't have wanted iref",
3182 key.objectid, key.type,
3183 key.offset);
3184 btrfs_abort_transaction(trans, -EUCLEAN);
3185 goto err_dump;
3186 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003187 } else {
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003188 /*
3189 * No inline ref, we must be at SHARED_* item,
3190 * And it's single ref, it must be:
3191 * | extent_slot ||extent_slot + 1|
3192 * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ]
3193 */
3194 if (path->slots[0] != extent_slot + 1) {
3195 btrfs_crit(info,
3196 "invalid SHARED_* item, previous item is not EXTENT/METADATA_ITEM");
3197 btrfs_abort_transaction(trans, -EUCLEAN);
3198 goto err_dump;
3199 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003200 path->slots[0] = extent_slot;
3201 num_to_del = 2;
3202 }
Chris Mason78fae272007-03-25 11:35:08 -04003203 }
Chris Masonb9473432009-03-13 11:00:37 -04003204
Josef Bacikfcebe452014-05-13 17:30:47 -07003205 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003206 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3207 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003208 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003209 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003210 goto out;
3211 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003213
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003214 if (is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00003215 ret = btrfs_del_csums(trans, info->csum_root, bytenr,
3216 num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003217 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003218 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003219 goto out;
3220 }
Chris Mason459931e2008-12-10 09:10:46 -05003221 }
3222
Nikolay Borisove7355e52018-05-10 15:44:55 +03003223 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003224 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003225 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003226 goto out;
3227 }
3228
Josef Bacikade4b512019-06-20 15:38:01 -04003229 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003230 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003231 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003232 goto out;
3233 }
Chris Masona28ec192007-03-06 20:08:01 -05003234 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003235 btrfs_release_path(path);
3236
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003237out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003238 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003239 return ret;
Qu Wenruo1c2a07f52020-08-19 14:35:48 +08003240err_dump:
3241 /*
3242 * Leaf dump can take up a lot of log buffer, so we only do full leaf
3243 * dump for debug build.
3244 */
3245 if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {
3246 btrfs_crit(info, "path->slots[0]=%d extent_slot=%d",
3247 path->slots[0], extent_slot);
3248 btrfs_print_leaf(path->nodes[0]);
3249 }
3250
3251 btrfs_free_path(path);
3252 return -EUCLEAN;
Chris Masona28ec192007-03-06 20:08:01 -05003253}
3254
3255/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003256 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003257 * delayed ref for that extent as well. This searches the delayed ref tree for
3258 * a given extent, and if there are no other delayed refs to be processed, it
3259 * removes it from the tree.
3260 */
3261static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003262 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003263{
3264 struct btrfs_delayed_ref_head *head;
3265 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003266 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003267
3268 delayed_refs = &trans->transaction->delayed_refs;
3269 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003270 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003271 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003272 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003273
Josef Bacikd7df2c72014-01-23 09:21:38 -05003274 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003275 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003276 goto out;
3277
Josef Bacikbedc66172018-12-03 10:20:31 -05003278 if (cleanup_extent_op(head) != NULL)
3279 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003280
Chris Mason1887be62009-03-13 10:11:24 -04003281 /*
3282 * waiting for the lock here would deadlock. If someone else has it
3283 * locked they are already in the process of dropping it anyway
3284 */
3285 if (!mutex_trylock(&head->mutex))
3286 goto out;
3287
Josef Bacikd7baffd2018-12-03 10:20:29 -05003288 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003289 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003290
Josef Bacikd7df2c72014-01-23 09:21:38 -05003291 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003292 spin_unlock(&delayed_refs->lock);
3293
Yan, Zhengf0486c62010-05-16 10:46:25 -04003294 BUG_ON(head->extent_op);
3295 if (head->must_insert_reserved)
3296 ret = 1;
3297
Josef Bacik31890da2018-11-21 14:05:41 -05003298 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003299 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003300 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003301 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003302out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003303 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003304
3305out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003306 spin_unlock(&delayed_refs->lock);
3307 return 0;
3308}
3309
Yan, Zhengf0486c62010-05-16 10:46:25 -04003310void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3311 struct btrfs_root *root,
3312 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003313 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003315 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003316 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04003317 int ret;
3318
Qu Wenruoed4f2552019-04-04 14:45:31 +08003319 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3320 buf->start, buf->len, parent);
3321 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3322 root->root_key.objectid);
3323
Yan, Zhengf0486c62010-05-16 10:46:25 -04003324 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003325 btrfs_ref_tree_mod(fs_info, &generic_ref);
Josef Bacik7ec15362021-01-15 16:48:55 -05003326 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003327 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04003328 }
3329
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003330 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
David Sterba32da53862019-10-29 19:20:18 +01003331 struct btrfs_block_group *cache;
Filipe Manana62198722015-01-06 20:18:45 +00003332
Yan, Zhengf0486c62010-05-16 10:46:25 -04003333 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003334 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003335 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003336 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003337 }
3338
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003339 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003340
Yan, Zhengf0486c62010-05-16 10:46:25 -04003341 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Nikolay Borisov6690d072020-01-20 16:09:14 +02003342 pin_down_extent(trans, cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003343 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003344 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003345 }
3346
3347 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3348
3349 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003350 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003351 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003352 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003353 }
3354out:
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003355 if (last_ref) {
3356 /*
3357 * Deleting the buffer, clear the corrupt flag since it doesn't
3358 * matter anymore.
3359 */
3360 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3361 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003362}
3363
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003364/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003365int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003366{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003367 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003368 int ret;
3369
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003370 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003371 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003372
Chris Mason56bec292009-03-13 10:10:06 -04003373 /*
3374 * tree log blocks never actually go into the extent allocation
3375 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003376 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003377 if ((ref->type == BTRFS_REF_METADATA &&
3378 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3379 (ref->type == BTRFS_REF_DATA &&
3380 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003381 /* unlocks the pinned mutex */
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02003382 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003383 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003384 } else if (ref->type == BTRFS_REF_METADATA) {
Josef Bacik7ec15362021-01-15 16:48:55 -05003385 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 } else {
Josef Bacik7ec15362021-01-15 16:48:55 -05003387 ret = btrfs_add_delayed_data_ref(trans, ref, 0);
Chris Mason56bec292009-03-13 10:10:06 -04003388 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003389
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003390 if (!((ref->type == BTRFS_REF_METADATA &&
3391 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3392 (ref->type == BTRFS_REF_DATA &&
3393 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3394 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003395
Chris Mason925baed2008-06-25 16:01:30 -04003396 return ret;
3397}
3398
Josef Bacik817d52f2009-07-13 21:29:25 -04003399enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003400 LOOP_CACHING_NOWAIT,
3401 LOOP_CACHING_WAIT,
3402 LOOP_ALLOC_CHUNK,
3403 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003404};
3405
Miao Xiee570fd22014-06-19 10:42:50 +08003406static inline void
David Sterba32da53862019-10-29 19:20:18 +01003407btrfs_lock_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003408 int delalloc)
3409{
3410 if (delalloc)
3411 down_read(&cache->data_rwsem);
3412}
3413
David Sterba32da53862019-10-29 19:20:18 +01003414static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003415 int delalloc)
3416{
3417 btrfs_get_block_group(cache);
3418 if (delalloc)
3419 down_read(&cache->data_rwsem);
3420}
3421
David Sterba32da53862019-10-29 19:20:18 +01003422static struct btrfs_block_group *btrfs_lock_cluster(
3423 struct btrfs_block_group *block_group,
Miao Xiee570fd22014-06-19 10:42:50 +08003424 struct btrfs_free_cluster *cluster,
3425 int delalloc)
Jules Irengec142c6a2020-03-31 21:46:41 +01003426 __acquires(&cluster->refill_lock)
Miao Xiee570fd22014-06-19 10:42:50 +08003427{
David Sterba32da53862019-10-29 19:20:18 +01003428 struct btrfs_block_group *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003429
Miao Xiee570fd22014-06-19 10:42:50 +08003430 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003431 while (1) {
3432 used_bg = cluster->block_group;
3433 if (!used_bg)
3434 return NULL;
3435
3436 if (used_bg == block_group)
3437 return used_bg;
3438
3439 btrfs_get_block_group(used_bg);
3440
3441 if (!delalloc)
3442 return used_bg;
3443
3444 if (down_read_trylock(&used_bg->data_rwsem))
3445 return used_bg;
3446
3447 spin_unlock(&cluster->refill_lock);
3448
Liu Boe321f8a2016-11-30 16:11:04 -08003449 /* We should only have one-level nested. */
3450 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003451
3452 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003453 if (used_bg == cluster->block_group)
3454 return used_bg;
3455
3456 up_read(&used_bg->data_rwsem);
3457 btrfs_put_block_group(used_bg);
3458 }
Miao Xiee570fd22014-06-19 10:42:50 +08003459}
3460
3461static inline void
David Sterba32da53862019-10-29 19:20:18 +01003462btrfs_release_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003463 int delalloc)
3464{
3465 if (delalloc)
3466 up_read(&cache->data_rwsem);
3467 btrfs_put_block_group(cache);
3468}
3469
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003470enum btrfs_extent_allocation_policy {
3471 BTRFS_EXTENT_ALLOC_CLUSTERED,
3472};
3473
Josef Bacik817d52f2009-07-13 21:29:25 -04003474/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08003475 * Structure used internally for find_free_extent() function. Wraps needed
3476 * parameters.
3477 */
3478struct find_free_extent_ctl {
3479 /* Basic allocation info */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003480 u64 num_bytes;
3481 u64 empty_size;
3482 u64 flags;
3483 int delalloc;
3484
3485 /* Where to start the search inside the bg */
3486 u64 search_start;
3487
3488 /* For clustered allocation */
3489 u64 empty_cluster;
Naohiro Aotac10859b2020-02-25 12:56:18 +09003490 struct btrfs_free_cluster *last_ptr;
3491 bool use_cluster;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003492
3493 bool have_caching_bg;
3494 bool orig_have_caching_bg;
3495
3496 /* RAID index, converted from flags */
3497 int index;
3498
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003499 /*
3500 * Current loop number, check find_free_extent_update_loop() for details
3501 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003502 int loop;
3503
3504 /*
3505 * Whether we're refilling a cluster, if true we need to re-search
3506 * current block group but don't try to refill the cluster again.
3507 */
3508 bool retry_clustered;
3509
3510 /*
3511 * Whether we're updating free space cache, if true we need to re-search
3512 * current block group but don't try updating free space cache again.
3513 */
3514 bool retry_unclustered;
3515
3516 /* If current block group is cached */
3517 int cached;
3518
3519 /* Max contiguous hole found */
3520 u64 max_extent_size;
3521
3522 /* Total free space from free space cache, not always contiguous */
3523 u64 total_free_space;
3524
3525 /* Found result */
3526 u64 found_offset;
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003527
Naohiro Aotaea544142020-02-25 12:56:17 +09003528 /* Hint where to start looking for an empty space */
3529 u64 hint_byte;
3530
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003531 /* Allocation policy */
3532 enum btrfs_extent_allocation_policy policy;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003533};
3534
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003535
3536/*
3537 * Helper function for find_free_extent().
3538 *
3539 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3540 * Return -EAGAIN to inform caller that we need to re-search this block group
3541 * Return >0 to inform caller that we find nothing
3542 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3543 */
David Sterba32da53862019-10-29 19:20:18 +01003544static int find_free_extent_clustered(struct btrfs_block_group *bg,
Naohiro Aota897cae72020-02-25 12:56:20 +09003545 struct find_free_extent_ctl *ffe_ctl,
3546 struct btrfs_block_group **cluster_bg_ret)
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003547{
David Sterba32da53862019-10-29 19:20:18 +01003548 struct btrfs_block_group *cluster_bg;
Naohiro Aota897cae72020-02-25 12:56:20 +09003549 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003550 u64 aligned_cluster;
3551 u64 offset;
3552 int ret;
3553
3554 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3555 if (!cluster_bg)
3556 goto refill_cluster;
3557 if (cluster_bg != bg && (cluster_bg->ro ||
3558 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3559 goto release_cluster;
3560
3561 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
David Sterbab3470b52019-10-23 18:48:22 +02003562 ffe_ctl->num_bytes, cluster_bg->start,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003563 &ffe_ctl->max_extent_size);
3564 if (offset) {
3565 /* We have a block, we're done */
3566 spin_unlock(&last_ptr->refill_lock);
3567 trace_btrfs_reserve_extent_cluster(cluster_bg,
3568 ffe_ctl->search_start, ffe_ctl->num_bytes);
3569 *cluster_bg_ret = cluster_bg;
3570 ffe_ctl->found_offset = offset;
3571 return 0;
3572 }
3573 WARN_ON(last_ptr->block_group != cluster_bg);
3574
3575release_cluster:
3576 /*
3577 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3578 * lets just skip it and let the allocator find whatever block it can
3579 * find. If we reach this point, we will have tried the cluster
3580 * allocator plenty of times and not have found anything, so we are
3581 * likely way too fragmented for the clustering stuff to find anything.
3582 *
3583 * However, if the cluster is taken from the current block group,
3584 * release the cluster first, so that we stand a better chance of
3585 * succeeding in the unclustered allocation.
3586 */
3587 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3588 spin_unlock(&last_ptr->refill_lock);
3589 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3590 return -ENOENT;
3591 }
3592
3593 /* This cluster didn't work out, free it and start over */
3594 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3595
3596 if (cluster_bg != bg)
3597 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3598
3599refill_cluster:
3600 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3601 spin_unlock(&last_ptr->refill_lock);
3602 return -ENOENT;
3603 }
3604
3605 aligned_cluster = max_t(u64,
3606 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3607 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003608 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3609 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003610 if (ret == 0) {
3611 /* Now pull our allocation out of this cluster */
3612 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3613 ffe_ctl->num_bytes, ffe_ctl->search_start,
3614 &ffe_ctl->max_extent_size);
3615 if (offset) {
3616 /* We found one, proceed */
3617 spin_unlock(&last_ptr->refill_lock);
3618 trace_btrfs_reserve_extent_cluster(bg,
3619 ffe_ctl->search_start,
3620 ffe_ctl->num_bytes);
3621 ffe_ctl->found_offset = offset;
3622 return 0;
3623 }
3624 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3625 !ffe_ctl->retry_clustered) {
3626 spin_unlock(&last_ptr->refill_lock);
3627
3628 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003629 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003630 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3631 return -EAGAIN;
3632 }
3633 /*
3634 * At this point we either didn't find a cluster or we weren't able to
3635 * allocate a block from our cluster. Free the cluster we've been
3636 * trying to use, and go to the next block group.
3637 */
3638 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3639 spin_unlock(&last_ptr->refill_lock);
3640 return 1;
3641}
3642
Qu Wenruob4bd7452018-11-02 09:39:47 +08003643/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003644 * Return >0 to inform caller that we find nothing
3645 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3646 * Return -EAGAIN to inform caller that we need to re-search this block group
3647 */
David Sterba32da53862019-10-29 19:20:18 +01003648static int find_free_extent_unclustered(struct btrfs_block_group *bg,
Naohiro Aota897cae72020-02-25 12:56:20 +09003649 struct find_free_extent_ctl *ffe_ctl)
Qu Wenruoe1a41842018-11-02 09:39:49 +08003650{
Naohiro Aota897cae72020-02-25 12:56:20 +09003651 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003652 u64 offset;
3653
3654 /*
3655 * We are doing an unclustered allocation, set the fragmented flag so
3656 * we don't bother trying to setup a cluster again until we get more
3657 * space.
3658 */
3659 if (unlikely(last_ptr)) {
3660 spin_lock(&last_ptr->lock);
3661 last_ptr->fragmented = 1;
3662 spin_unlock(&last_ptr->lock);
3663 }
3664 if (ffe_ctl->cached) {
3665 struct btrfs_free_space_ctl *free_space_ctl;
3666
3667 free_space_ctl = bg->free_space_ctl;
3668 spin_lock(&free_space_ctl->tree_lock);
3669 if (free_space_ctl->free_space <
3670 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3671 ffe_ctl->empty_size) {
3672 ffe_ctl->total_free_space = max_t(u64,
3673 ffe_ctl->total_free_space,
3674 free_space_ctl->free_space);
3675 spin_unlock(&free_space_ctl->tree_lock);
3676 return 1;
3677 }
3678 spin_unlock(&free_space_ctl->tree_lock);
3679 }
3680
3681 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3682 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3683 &ffe_ctl->max_extent_size);
3684
3685 /*
3686 * If we didn't find a chunk, and we haven't failed on this block group
3687 * before, and this block group is in the middle of caching and we are
3688 * ok with waiting, then go ahead and wait for progress to be made, and
3689 * set @retry_unclustered to true.
3690 *
3691 * If @retry_unclustered is true then we've already waited on this
3692 * block group once and should move on to the next block group.
3693 */
3694 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3695 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003696 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3697 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003698 ffe_ctl->retry_unclustered = true;
3699 return -EAGAIN;
3700 } else if (!offset) {
3701 return 1;
3702 }
3703 ffe_ctl->found_offset = offset;
3704 return 0;
3705}
3706
Naohiro Aotac6686902020-02-25 12:56:19 +09003707static int do_allocation_clustered(struct btrfs_block_group *block_group,
3708 struct find_free_extent_ctl *ffe_ctl,
3709 struct btrfs_block_group **bg_ret)
3710{
3711 int ret;
3712
3713 /* We want to try and use the cluster allocator, so lets look there */
3714 if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) {
Naohiro Aota897cae72020-02-25 12:56:20 +09003715 ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret);
Naohiro Aotac6686902020-02-25 12:56:19 +09003716 if (ret >= 0 || ret == -EAGAIN)
3717 return ret;
3718 /* ret == -ENOENT case falls through */
3719 }
3720
Naohiro Aota897cae72020-02-25 12:56:20 +09003721 return find_free_extent_unclustered(block_group, ffe_ctl);
Naohiro Aotac6686902020-02-25 12:56:19 +09003722}
3723
3724static int do_allocation(struct btrfs_block_group *block_group,
3725 struct find_free_extent_ctl *ffe_ctl,
3726 struct btrfs_block_group **bg_ret)
3727{
3728 switch (ffe_ctl->policy) {
3729 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3730 return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
3731 default:
3732 BUG();
3733 }
3734}
3735
Naohiro Aotababa5062020-02-25 12:56:21 +09003736static void release_block_group(struct btrfs_block_group *block_group,
3737 struct find_free_extent_ctl *ffe_ctl,
3738 int delalloc)
3739{
3740 switch (ffe_ctl->policy) {
3741 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3742 ffe_ctl->retry_clustered = false;
3743 ffe_ctl->retry_unclustered = false;
3744 break;
3745 default:
3746 BUG();
3747 }
3748
3749 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
3750 ffe_ctl->index);
3751 btrfs_release_block_group(block_group, delalloc);
3752}
3753
Naohiro Aota0ab97242020-02-25 12:56:22 +09003754static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl,
3755 struct btrfs_key *ins)
3756{
3757 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3758
3759 if (!ffe_ctl->use_cluster && last_ptr) {
3760 spin_lock(&last_ptr->lock);
3761 last_ptr->window_start = ins->objectid;
3762 spin_unlock(&last_ptr->lock);
3763 }
3764}
3765
3766static void found_extent(struct find_free_extent_ctl *ffe_ctl,
3767 struct btrfs_key *ins)
3768{
3769 switch (ffe_ctl->policy) {
3770 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3771 found_extent_clustered(ffe_ctl, ins);
3772 break;
3773 default:
3774 BUG();
3775 }
3776}
3777
Naohiro Aotac70e2132020-02-25 12:56:24 +09003778static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl)
3779{
3780 switch (ffe_ctl->policy) {
3781 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3782 /*
3783 * If we can't allocate a new chunk we've already looped through
3784 * at least once, move on to the NO_EMPTY_SIZE case.
3785 */
3786 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3787 return 0;
3788 default:
3789 BUG();
3790 }
3791}
3792
Qu Wenruoe1a41842018-11-02 09:39:49 +08003793/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003794 * Return >0 means caller needs to re-search for free extent
3795 * Return 0 means we have the needed free extent.
3796 * Return <0 means we failed to locate any free extent.
3797 */
3798static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003799 struct btrfs_key *ins,
3800 struct find_free_extent_ctl *ffe_ctl,
Naohiro Aota15b7ee62020-02-25 12:56:23 +09003801 bool full_search)
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003802{
3803 struct btrfs_root *root = fs_info->extent_root;
3804 int ret;
3805
3806 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3807 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3808 ffe_ctl->orig_have_caching_bg = true;
3809
3810 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3811 ffe_ctl->have_caching_bg)
3812 return 1;
3813
3814 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3815 return 1;
3816
3817 if (ins->objectid) {
Naohiro Aota0ab97242020-02-25 12:56:22 +09003818 found_extent(ffe_ctl, ins);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003819 return 0;
3820 }
3821
3822 /*
3823 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3824 * caching kthreads as we move along
3825 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3826 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3827 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3828 * again
3829 */
3830 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3831 ffe_ctl->index = 0;
3832 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3833 /*
3834 * We want to skip the LOOP_CACHING_WAIT step if we
3835 * don't have any uncached bgs and we've already done a
3836 * full search through.
3837 */
3838 if (ffe_ctl->orig_have_caching_bg || !full_search)
3839 ffe_ctl->loop = LOOP_CACHING_WAIT;
3840 else
3841 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3842 } else {
3843 ffe_ctl->loop++;
3844 }
3845
3846 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3847 struct btrfs_trans_handle *trans;
3848 int exist = 0;
3849
3850 trans = current->journal_info;
3851 if (trans)
3852 exist = 1;
3853 else
3854 trans = btrfs_join_transaction(root);
3855
3856 if (IS_ERR(trans)) {
3857 ret = PTR_ERR(trans);
3858 return ret;
3859 }
3860
Josef Bacikfc471cb2019-06-18 16:09:17 -04003861 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3862 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003863
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003864 /* Do not bail out on ENOSPC since we can do more. */
Naohiro Aotac70e2132020-02-25 12:56:24 +09003865 if (ret == -ENOSPC)
3866 ret = chunk_allocation_failed(ffe_ctl);
3867 else if (ret < 0)
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003868 btrfs_abort_transaction(trans, ret);
3869 else
3870 ret = 0;
3871 if (!exist)
3872 btrfs_end_transaction(trans);
3873 if (ret)
3874 return ret;
3875 }
3876
3877 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
Naohiro Aota45d8e032020-02-25 12:56:25 +09003878 if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED)
3879 return -ENOSPC;
3880
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003881 /*
3882 * Don't loop again if we already have no empty_size and
3883 * no empty_cluster.
3884 */
3885 if (ffe_ctl->empty_size == 0 &&
3886 ffe_ctl->empty_cluster == 0)
3887 return -ENOSPC;
3888 ffe_ctl->empty_size = 0;
3889 ffe_ctl->empty_cluster = 0;
3890 }
3891 return 1;
3892 }
3893 return -ENOSPC;
3894}
3895
Naohiro Aota7e895402020-02-25 12:56:26 +09003896static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info,
3897 struct find_free_extent_ctl *ffe_ctl,
3898 struct btrfs_space_info *space_info,
3899 struct btrfs_key *ins)
3900{
3901 /*
3902 * If our free space is heavily fragmented we may not be able to make
3903 * big contiguous allocations, so instead of doing the expensive search
3904 * for free space, simply return ENOSPC with our max_extent_size so we
3905 * can go ahead and search for a more manageable chunk.
3906 *
3907 * If our max_extent_size is large enough for our allocation simply
3908 * disable clustering since we will likely not be able to find enough
3909 * space to create a cluster and induce latency trying.
3910 */
3911 if (space_info->max_extent_size) {
3912 spin_lock(&space_info->lock);
3913 if (space_info->max_extent_size &&
3914 ffe_ctl->num_bytes > space_info->max_extent_size) {
3915 ins->offset = space_info->max_extent_size;
3916 spin_unlock(&space_info->lock);
3917 return -ENOSPC;
3918 } else if (space_info->max_extent_size) {
3919 ffe_ctl->use_cluster = false;
3920 }
3921 spin_unlock(&space_info->lock);
3922 }
3923
3924 ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info,
3925 &ffe_ctl->empty_cluster);
3926 if (ffe_ctl->last_ptr) {
3927 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3928
3929 spin_lock(&last_ptr->lock);
3930 if (last_ptr->block_group)
3931 ffe_ctl->hint_byte = last_ptr->window_start;
3932 if (last_ptr->fragmented) {
3933 /*
3934 * We still set window_start so we can keep track of the
3935 * last place we found an allocation to try and save
3936 * some time.
3937 */
3938 ffe_ctl->hint_byte = last_ptr->window_start;
3939 ffe_ctl->use_cluster = false;
3940 }
3941 spin_unlock(&last_ptr->lock);
3942 }
3943
3944 return 0;
3945}
3946
3947static int prepare_allocation(struct btrfs_fs_info *fs_info,
3948 struct find_free_extent_ctl *ffe_ctl,
3949 struct btrfs_space_info *space_info,
3950 struct btrfs_key *ins)
3951{
3952 switch (ffe_ctl->policy) {
3953 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3954 return prepare_allocation_clustered(fs_info, ffe_ctl,
3955 space_info, ins);
3956 default:
3957 BUG();
3958 }
3959}
3960
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003961/*
Chris Masonfec577f2007-02-26 10:40:21 -05003962 * walks the btree of allocated extents and find a hole of a given size.
3963 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003964 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003965 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003966 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003967 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003968 *
3969 * If there is no suitable free space, we will record the max size of
3970 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003971 *
3972 * The overall logic and call chain:
3973 *
3974 * find_free_extent()
3975 * |- Iterate through all block groups
3976 * | |- Get a valid block group
3977 * | |- Try to do clustered allocation in that block group
3978 * | |- Try to do unclustered allocation in that block group
3979 * | |- Check if the result is valid
3980 * | | |- If valid, then exit
3981 * | |- Jump to next block group
3982 * |
3983 * |- Push harder to find free extents
3984 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003985 */
Qu Wenruo437490f2020-07-28 09:42:49 +08003986static noinline int find_free_extent(struct btrfs_root *root,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003987 u64 ram_bytes, u64 num_bytes, u64 empty_size,
Naohiro Aotaea544142020-02-25 12:56:17 +09003988 u64 hint_byte_orig, struct btrfs_key *ins,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003989 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003990{
Qu Wenruo437490f2020-07-28 09:42:49 +08003991 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04003992 int ret = 0;
Josef Bacikdb8fe642019-11-19 13:59:00 -05003993 int cache_block_group_error = 0;
David Sterba32da53862019-10-29 19:20:18 +01003994 struct btrfs_block_group *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003995 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003996 struct btrfs_space_info *space_info;
Josef Bacika5e681d2015-10-01 14:54:10 -04003997 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003998
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003999 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004000
Qu Wenruob4bd7452018-11-02 09:39:47 +08004001 ffe_ctl.num_bytes = num_bytes;
4002 ffe_ctl.empty_size = empty_size;
4003 ffe_ctl.flags = flags;
4004 ffe_ctl.search_start = 0;
Qu Wenruob4bd7452018-11-02 09:39:47 +08004005 ffe_ctl.delalloc = delalloc;
4006 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
4007 ffe_ctl.have_caching_bg = false;
4008 ffe_ctl.orig_have_caching_bg = false;
4009 ffe_ctl.found_offset = 0;
Naohiro Aotaea544142020-02-25 12:56:17 +09004010 ffe_ctl.hint_byte = hint_byte_orig;
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09004011 ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
Qu Wenruob4bd7452018-11-02 09:39:47 +08004012
Naohiro Aotac10859b2020-02-25 12:56:18 +09004013 /* For clustered allocation */
4014 ffe_ctl.retry_clustered = false;
4015 ffe_ctl.retry_unclustered = false;
4016 ffe_ctl.last_ptr = NULL;
4017 ffe_ctl.use_cluster = true;
4018
David Sterba962a2982014-06-04 18:41:45 +02004019 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04004020 ins->objectid = 0;
4021 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004022
Qu Wenruo437490f2020-07-28 09:42:49 +08004023 trace_find_free_extent(root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05004024
Josef Bacik280c29082019-06-18 16:09:19 -04004025 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004026 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004027 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004028 return -ENOSPC;
4029 }
Josef Bacik2552d172009-04-03 10:14:19 -04004030
Naohiro Aota7e895402020-02-25 12:56:26 +09004031 ret = prepare_allocation(fs_info, &ffe_ctl, space_info, ins);
4032 if (ret < 0)
4033 return ret;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004034
Qu Wenruob4bd7452018-11-02 09:39:47 +08004035 ffe_ctl.search_start = max(ffe_ctl.search_start,
4036 first_logical_byte(fs_info, 0));
Naohiro Aotaea544142020-02-25 12:56:17 +09004037 ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte);
4038 if (ffe_ctl.search_start == ffe_ctl.hint_byte) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004039 block_group = btrfs_lookup_block_group(fs_info,
4040 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004041 /*
4042 * we don't want to use the block group if it doesn't match our
4043 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004044 *
4045 * However if we are re-searching with an ideal block group
4046 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004047 */
David Sterbab6919a52013-04-29 13:39:40 +00004048 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05004049 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04004050 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004051 if (list_empty(&block_group->list) ||
4052 block_group->ro) {
4053 /*
4054 * someone is removing this block group,
4055 * we can't jump into the have_block_group
4056 * target because our list pointers are not
4057 * valid
4058 */
4059 btrfs_put_block_group(block_group);
4060 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004061 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004062 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004063 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08004064 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04004065 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004066 }
Josef Bacik2552d172009-04-03 10:14:19 -04004067 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004068 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004069 }
Chris Mason42e70e72008-11-07 18:17:11 -05004070 }
Josef Bacik2552d172009-04-03 10:14:19 -04004071search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004072 ffe_ctl.have_caching_bg = false;
4073 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
4074 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04004075 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04004076 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004077 list_for_each_entry(block_group,
4078 &space_info->block_groups[ffe_ctl.index], list) {
Naohiro Aotac6686902020-02-25 12:56:19 +09004079 struct btrfs_block_group *bg_ret;
4080
Jeff Mahoney14443932017-07-19 23:25:51 -04004081 /* If the block group is read-only, we can skip it entirely. */
4082 if (unlikely(block_group->ro))
4083 continue;
4084
Miao Xiee570fd22014-06-19 10:42:50 +08004085 btrfs_grab_block_group(block_group, delalloc);
David Sterbab3470b52019-10-23 18:48:22 +02004086 ffe_ctl.search_start = block_group->start;
Chris Mason42e70e72008-11-07 18:17:11 -05004087
Chris Mason83a50de2010-12-13 15:06:46 -05004088 /*
4089 * this can happen if we end up cycling through all the
4090 * raid types, but we want to make sure we only allocate
4091 * for the proper type.
4092 */
David Sterbab6919a52013-04-29 13:39:40 +00004093 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07004094 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02004095 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02004096 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05004097 BTRFS_BLOCK_GROUP_RAID10;
4098
4099 /*
4100 * if they asked for extra copies and this block group
4101 * doesn't provide them, bail. This does allow us to
4102 * fill raid0 from raid1.
4103 */
David Sterbab6919a52013-04-29 13:39:40 +00004104 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05004105 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08004106
4107 /*
4108 * This block group has different flags than we want.
4109 * It's possible that we have MIXED_GROUP flag but no
4110 * block group is mixed. Just skip such block group.
4111 */
4112 btrfs_release_block_group(block_group, delalloc);
4113 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05004114 }
4115
Josef Bacik2552d172009-04-03 10:14:19 -04004116have_block_group:
David Sterba32da53862019-10-29 19:20:18 +01004117 ffe_ctl.cached = btrfs_block_group_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004118 if (unlikely(!ffe_ctl.cached)) {
4119 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04004120 ret = btrfs_cache_block_group(block_group, 0);
Josef Bacikdb8fe642019-11-19 13:59:00 -05004121
4122 /*
4123 * If we get ENOMEM here or something else we want to
4124 * try other block groups, because it may not be fatal.
4125 * However if we can't find anything else we need to
4126 * save our return here so that we return the actual
4127 * error that caused problems, not ENOSPC.
4128 */
4129 if (ret < 0) {
4130 if (!cache_block_group_error)
4131 cache_block_group_error = ret;
4132 ret = 0;
4133 goto loop;
4134 }
Chris Mason1d4284b2012-03-28 20:31:37 -04004135 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05004136 }
4137
Josef Bacik36cce922013-08-05 11:15:21 -04004138 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
4139 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004140
Naohiro Aotac6686902020-02-25 12:56:19 +09004141 bg_ret = NULL;
4142 ret = do_allocation(block_group, &ffe_ctl, &bg_ret);
4143 if (ret == 0) {
4144 if (bg_ret && bg_ret != block_group) {
4145 btrfs_release_block_group(block_group, delalloc);
4146 block_group = bg_ret;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004147 }
Naohiro Aotac6686902020-02-25 12:56:19 +09004148 } else if (ret == -EAGAIN) {
4149 goto have_block_group;
4150 } else if (ret > 0) {
4151 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004152 }
4153
Naohiro Aotac6686902020-02-25 12:56:19 +09004154 /* Checks */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004155 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4156 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04004157
Josef Bacik2552d172009-04-03 10:14:19 -04004158 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004159 if (ffe_ctl.search_start + num_bytes >
David Sterbab3470b52019-10-23 18:48:22 +02004160 block_group->start + block_group->length) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004161 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4162 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004163 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004164 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004165
Qu Wenruob4bd7452018-11-02 09:39:47 +08004166 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4167 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4168 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004169
Wang Xiaoguang18513092016-07-25 15:51:40 +08004170 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4171 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004172 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004173 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4174 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004175 goto loop;
4176 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004177 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004178
Josef Bacik2552d172009-04-03 10:14:19 -04004179 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004180 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004181 ins->offset = num_bytes;
4182
Qu Wenruob4bd7452018-11-02 09:39:47 +08004183 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4184 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004185 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004186 break;
4187loop:
Naohiro Aotababa5062020-02-25 12:56:21 +09004188 release_block_group(block_group, &ffe_ctl, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004189 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004190 }
4191 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004192
Naohiro Aota15b7ee62020-02-25 12:56:23 +09004193 ret = find_free_extent_update_loop(fs_info, ins, &ffe_ctl, full_search);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004194 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004195 goto search;
4196
Josef Bacikdb8fe642019-11-19 13:59:00 -05004197 if (ret == -ENOSPC && !cache_block_group_error) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004198 /*
4199 * Use ffe_ctl->total_free_space as fallback if we can't find
4200 * any contiguous hole.
4201 */
4202 if (!ffe_ctl.max_extent_size)
4203 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004204 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004205 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004206 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004207 ins->offset = ffe_ctl.max_extent_size;
Josef Bacikdb8fe642019-11-19 13:59:00 -05004208 } else if (ret == -ENOSPC) {
4209 ret = cache_block_group_error;
Josef Bacik4f4db212015-09-29 11:40:47 -04004210 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004211 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004212}
Chris Masonec44a352008-04-28 15:29:52 -04004213
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004214/*
4215 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4216 * hole that is at least as big as @num_bytes.
4217 *
4218 * @root - The root that will contain this extent
4219 *
4220 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4221 * is used for accounting purposes. This value differs
4222 * from @num_bytes only in the case of compressed extents.
4223 *
4224 * @num_bytes - Number of bytes to allocate on-disk.
4225 *
4226 * @min_alloc_size - Indicates the minimum amount of space that the
4227 * allocator should try to satisfy. In some cases
4228 * @num_bytes may be larger than what is required and if
4229 * the filesystem is fragmented then allocation fails.
4230 * However, the presence of @min_alloc_size gives a
4231 * chance to try and satisfy the smaller allocation.
4232 *
4233 * @empty_size - A hint that you plan on doing more COW. This is the
4234 * size in bytes the allocator should try to find free
4235 * next to the block it returns. This is just a hint and
4236 * may be ignored by the allocator.
4237 *
4238 * @hint_byte - Hint to the allocator to start searching above the byte
4239 * address passed. It might be ignored.
4240 *
4241 * @ins - This key is modified to record the found hole. It will
4242 * have the following values:
4243 * ins->objectid == start position
4244 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4245 * ins->offset == the size of the hole.
4246 *
4247 * @is_data - Boolean flag indicating whether an extent is
4248 * allocated for data (true) or metadata (false)
4249 *
4250 * @delalloc - Boolean flag indicating whether this allocation is for
4251 * delalloc or not. If 'true' data_rwsem of block groups
4252 * is going to be acquired.
4253 *
4254 *
4255 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4256 * case -ENOSPC is returned then @ins->offset will contain the size of the
4257 * largest available hole the allocator managed to find.
4258 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004259int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004260 u64 num_bytes, u64 min_alloc_size,
4261 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004262 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004263{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004264 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004265 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004266 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004267 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004268
Jeff Mahoney1b868262017-05-17 11:38:35 -04004269 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004270again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004271 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruo437490f2020-07-28 09:42:49 +08004272 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004273 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004274 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004275 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004276 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004277 if (!final_tried && ins->offset) {
4278 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004279 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004280 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004281 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004282 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004283 if (num_bytes == min_alloc_size)
4284 final_tried = true;
4285 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004286 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004287 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004288
Josef Bacik280c29082019-06-18 16:09:19 -04004289 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004290 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004291 "allocation failed flags %llu, wanted %llu",
4292 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004293 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004294 btrfs_dump_space_info(fs_info, sinfo,
4295 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004296 }
Chris Mason925baed2008-06-25 16:01:30 -04004297 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004298
4299 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004300}
4301
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004302int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004303 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004304{
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004305 struct btrfs_block_group *cache;
4306
4307 cache = btrfs_lookup_block_group(fs_info, start);
4308 if (!cache) {
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004309 btrfs_err(fs_info, "Unable to find block group for %llu",
4310 start);
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004311 return -ENOSPC;
4312 }
4313
4314 btrfs_add_free_space(cache, start, len);
4315 btrfs_free_reserved_bytes(cache, len, delalloc);
4316 trace_btrfs_reserved_extent_free(fs_info, start, len);
4317
4318 btrfs_put_block_group(cache);
4319 return 0;
Chris Masone688b7252011-10-31 20:52:39 -04004320}
4321
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004322int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
4323 u64 len)
Chris Masone688b7252011-10-31 20:52:39 -04004324{
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004325 struct btrfs_block_group *cache;
4326 int ret = 0;
4327
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004328 cache = btrfs_lookup_block_group(trans->fs_info, start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004329 if (!cache) {
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004330 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4331 start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004332 return -ENOSPC;
4333 }
4334
Nikolay Borisov6690d072020-01-20 16:09:14 +02004335 ret = pin_down_extent(trans, cache, start, len, 1);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004336 btrfs_put_block_group(cache);
4337 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04004338}
4339
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004340static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341 u64 parent, u64 root_objectid,
4342 u64 flags, u64 owner, u64 offset,
4343 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004345 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004347 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004348 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004350 struct extent_buffer *leaf;
4351 int type;
4352 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004354 if (parent > 0)
4355 type = BTRFS_SHARED_DATA_REF_KEY;
4356 else
4357 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004358
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004359 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004360
4361 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004362 if (!path)
4363 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004364
Chris Masonb9473432009-03-13 11:00:37 -04004365 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004366 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4367 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004368 if (ret) {
4369 btrfs_free_path(path);
4370 return ret;
4371 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004372
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004373 leaf = path->nodes[0];
4374 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004375 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004376 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4377 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4378 btrfs_set_extent_flags(leaf, extent_item,
4379 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004380
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004381 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4382 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4383 if (parent > 0) {
4384 struct btrfs_shared_data_ref *ref;
4385 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4386 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4387 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4388 } else {
4389 struct btrfs_extent_data_ref *ref;
4390 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4391 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4392 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4393 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4394 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4395 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004396
4397 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004398 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004399
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004400 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004401 if (ret)
4402 return ret;
4403
Josef Bacikade4b512019-06-20 15:38:01 -04004404 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004405 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004406 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004407 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004408 BUG();
4409 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004410 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004411 return ret;
4412}
4413
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004414static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004415 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004416 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004417{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004418 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004419 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004420 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004421 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004422 struct btrfs_tree_block_info *block_info;
4423 struct btrfs_extent_inline_ref *iref;
4424 struct btrfs_path *path;
4425 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004426 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004427 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004428 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004429 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004431
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004432 ref = btrfs_delayed_node_to_tree_ref(node);
4433
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004434 extent_key.objectid = node->bytenr;
4435 if (skinny_metadata) {
4436 extent_key.offset = ref->level;
4437 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4438 num_bytes = fs_info->nodesize;
4439 } else {
4440 extent_key.offset = node->num_bytes;
4441 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004442 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004443 num_bytes = node->num_bytes;
4444 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004445
4446 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004447 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004448 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004449
4450 path->leave_spinning = 1;
4451 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004452 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004453 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004454 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004455 return ret;
4456 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004457
4458 leaf = path->nodes[0];
4459 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4460 struct btrfs_extent_item);
4461 btrfs_set_extent_refs(leaf, extent_item, 1);
4462 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4463 btrfs_set_extent_flags(leaf, extent_item,
4464 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004465
Josef Bacik3173a182013-03-07 14:22:04 -05004466 if (skinny_metadata) {
4467 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4468 } else {
4469 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004470 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004471 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004472 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4473 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004474
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004475 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004476 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4477 btrfs_set_extent_inline_ref_type(leaf, iref,
4478 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004479 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004480 } else {
4481 btrfs_set_extent_inline_ref_type(leaf, iref,
4482 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004483 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004484 }
4485
4486 btrfs_mark_buffer_dirty(leaf);
4487 btrfs_free_path(path);
4488
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004489 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4490 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004491 if (ret)
4492 return ret;
4493
Josef Bacikade4b512019-06-20 15:38:01 -04004494 ret = btrfs_update_block_group(trans, extent_key.objectid,
4495 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004496 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004497 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004498 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004499 BUG();
4500 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004501
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004502 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004503 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004504 return ret;
4505}
4506
4507int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004508 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004509 u64 offset, u64 ram_bytes,
4510 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004511{
Qu Wenruo76675592019-04-04 14:45:32 +08004512 struct btrfs_ref generic_ref = { 0 };
Chris Mason1c2308f82008-09-23 13:14:13 -04004513
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004514 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004515
Qu Wenruo76675592019-04-04 14:45:32 +08004516 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4517 ins->objectid, ins->offset, 0);
4518 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004519 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Josef Bacik7ec15362021-01-15 16:48:55 -05004520
4521 return btrfs_add_delayed_data_ref(trans, &generic_ref, ram_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004522}
Chris Masone02119d2008-09-05 16:13:11 -04004523
4524/*
4525 * this is used by the tree logging recovery code. It records that
4526 * an extent has been allocated and makes sure to clear the free
4527 * space cache bits as well
4528 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004529int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004530 u64 root_objectid, u64 owner, u64 offset,
4531 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004532{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004533 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004534 int ret;
David Sterba32da53862019-10-29 19:20:18 +01004535 struct btrfs_block_group *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004536 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004537
4538 /*
4539 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004540 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004541 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004542 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004543 ret = __exclude_logged_extent(fs_info, ins->objectid,
4544 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004545 if (ret)
4546 return ret;
4547 }
Chris Masone02119d2008-09-05 16:13:11 -04004548
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004549 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004550 if (!block_group)
4551 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004552
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004553 space_info = block_group->space_info;
4554 spin_lock(&space_info->lock);
4555 spin_lock(&block_group->lock);
4556 space_info->bytes_reserved += ins->offset;
4557 block_group->reserved += ins->offset;
4558 spin_unlock(&block_group->lock);
4559 spin_unlock(&space_info->lock);
4560
Nikolay Borisovef89b822018-06-20 15:48:58 +03004561 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4562 offset, ins, 1);
Josef Bacikbd727172020-02-13 10:47:30 -05004563 if (ret)
Josef Bacikab9b2c72020-02-13 10:47:30 -05004564 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004565 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004566 return ret;
4567}
4568
Eric Sandeen48a3b632013-04-25 20:41:01 +00004569static struct extent_buffer *
4570btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacik9631e4c2020-08-20 11:46:03 -04004571 u64 bytenr, int level, u64 owner,
4572 enum btrfs_lock_nesting nest)
Chris Mason65b51a02008-08-01 15:11:20 -04004573{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004574 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004575 struct extent_buffer *buf;
4576
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004577 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004578 if (IS_ERR(buf))
4579 return buf;
4580
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004581 /*
4582 * Extra safety check in case the extent tree is corrupted and extent
4583 * allocator chooses to use a tree block which is already used and
4584 * locked.
4585 */
4586 if (buf->lock_owner == current->pid) {
4587 btrfs_err_rl(fs_info,
4588"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4589 buf->start, btrfs_header_owner(buf), current->pid);
4590 free_extent_buffer(buf);
4591 return ERR_PTR(-EUCLEAN);
4592 }
4593
Josef Bacikad244662020-08-10 11:42:30 -04004594 btrfs_set_buffer_lockdep_class(owner, buf, level);
Josef Bacik9631e4c2020-08-20 11:46:03 -04004595 __btrfs_tree_lock(buf, nest);
David Sterba6a884d7d2019-03-20 14:30:02 +01004596 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004597 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004598
David Sterba8bead252018-04-04 02:03:48 +02004599 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004600 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004601
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004602 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4603 btrfs_set_header_level(buf, level);
4604 btrfs_set_header_bytenr(buf, buf->start);
4605 btrfs_set_header_generation(buf, trans->transid);
4606 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4607 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004608 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004609 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004610 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004611 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004612 /*
4613 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004614 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004615 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004616 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004617 set_extent_dirty(&root->dirty_log_pages, buf->start,
4618 buf->start + buf->len - 1, GFP_NOFS);
4619 else
4620 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004621 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004622 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004623 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004624 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4625 buf->start + buf->len - 1, GFP_NOFS);
4626 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004627 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004628 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004629 return buf;
4630}
4631
Chris Masonfec577f2007-02-26 10:40:21 -05004632/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004633 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004634 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004635 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004636struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004637 struct btrfs_root *root,
4638 u64 parent, u64 root_objectid,
4639 const struct btrfs_disk_key *key,
4640 int level, u64 hint,
Josef Bacik9631e4c2020-08-20 11:46:03 -04004641 u64 empty_size,
4642 enum btrfs_lock_nesting nest)
Chris Masonfec577f2007-02-26 10:40:21 -05004643{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004645 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004646 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004647 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004648 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004649 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004650 u64 flags = 0;
4651 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004652 u32 blocksize = fs_info->nodesize;
4653 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004654
David Sterba05653ef2016-07-15 15:23:37 +02004655#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004656 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004657 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Josef Bacik9631e4c2020-08-20 11:46:03 -04004658 level, root_objectid, nest);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004659 if (!IS_ERR(buf))
4660 root->alloc_bytenr += blocksize;
4661 return buf;
4662 }
David Sterba05653ef2016-07-15 15:23:37 +02004663#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004664
Josef Bacik67f9c222019-06-19 13:47:23 -04004665 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004666 if (IS_ERR(block_rsv))
4667 return ERR_CAST(block_rsv);
4668
Wang Xiaoguang18513092016-07-25 15:51:40 +08004669 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004670 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004671 if (ret)
4672 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004673
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004674 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
Josef Bacik9631e4c2020-08-20 11:46:03 -04004675 root_objectid, nest);
Omar Sandoval67b78592015-02-24 02:47:04 -08004676 if (IS_ERR(buf)) {
4677 ret = PTR_ERR(buf);
4678 goto out_free_reserved;
4679 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004680
4681 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4682 if (parent == 0)
4683 parent = ins.objectid;
4684 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4685 } else
4686 BUG_ON(parent > 0);
4687
4688 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004689 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004690 if (!extent_op) {
4691 ret = -ENOMEM;
4692 goto out_free_buf;
4693 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004694 if (key)
4695 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4696 else
4697 memset(&extent_op->key, 0, sizeof(extent_op->key));
4698 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004699 extent_op->update_key = skinny_metadata ? false : true;
4700 extent_op->update_flags = true;
4701 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004702 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004703
Qu Wenruoed4f2552019-04-04 14:45:31 +08004704 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4705 ins.objectid, ins.offset, parent);
4706 generic_ref.real_root = root->root_key.objectid;
4707 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004708 btrfs_ref_tree_mod(fs_info, &generic_ref);
Josef Bacik7ec15362021-01-15 16:48:55 -05004709 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, extent_op);
Omar Sandoval67b78592015-02-24 02:47:04 -08004710 if (ret)
4711 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004712 }
Chris Masonfec577f2007-02-26 10:40:21 -05004713 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004714
4715out_free_delayed:
4716 btrfs_free_delayed_extent_op(extent_op);
4717out_free_buf:
4718 free_extent_buffer(buf);
4719out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004720 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004721out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004722 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004723 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004724}
Chris Masona28ec192007-03-06 20:08:01 -05004725
Yan Zheng2c47e6052009-06-27 21:07:35 -04004726struct walk_control {
4727 u64 refs[BTRFS_MAX_LEVEL];
4728 u64 flags[BTRFS_MAX_LEVEL];
4729 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004730 struct btrfs_key drop_progress;
4731 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004732 int stage;
4733 int level;
4734 int shared_level;
4735 int update_ref;
4736 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004737 int reada_slot;
4738 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004739 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004740};
4741
4742#define DROP_REFERENCE 1
4743#define UPDATE_BACKREF 2
4744
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004745static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4746 struct btrfs_root *root,
4747 struct walk_control *wc,
4748 struct btrfs_path *path)
4749{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004750 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004751 u64 bytenr;
4752 u64 generation;
4753 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004754 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004755 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004756 struct btrfs_key key;
4757 struct extent_buffer *eb;
4758 int ret;
4759 int slot;
4760 int nread = 0;
4761
4762 if (path->slots[wc->level] < wc->reada_slot) {
4763 wc->reada_count = wc->reada_count * 2 / 3;
4764 wc->reada_count = max(wc->reada_count, 2);
4765 } else {
4766 wc->reada_count = wc->reada_count * 3 / 2;
4767 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004768 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004769 }
4770
4771 eb = path->nodes[wc->level];
4772 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004773
4774 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4775 if (nread >= wc->reada_count)
4776 break;
4777
4778 cond_resched();
4779 bytenr = btrfs_node_blockptr(eb, slot);
4780 generation = btrfs_node_ptr_generation(eb, slot);
4781
4782 if (slot == path->slots[wc->level])
4783 goto reada;
4784
4785 if (wc->stage == UPDATE_BACKREF &&
4786 generation <= root->root_key.offset)
4787 continue;
4788
Yan, Zheng94fcca92009-10-09 09:25:16 -04004789 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004790 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004791 wc->level - 1, 1, &refs,
4792 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004793 /* We don't care about errors in readahead. */
4794 if (ret < 0)
4795 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004796 BUG_ON(refs == 0);
4797
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004798 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004799 if (refs == 1)
4800 goto reada;
4801
Yan, Zheng94fcca92009-10-09 09:25:16 -04004802 if (wc->level == 1 &&
4803 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4804 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004805 if (!wc->update_ref ||
4806 generation <= root->root_key.offset)
4807 continue;
4808 btrfs_node_key_to_cpu(eb, &key, slot);
4809 ret = btrfs_comp_cpu_keys(&key,
4810 &wc->update_progress);
4811 if (ret < 0)
4812 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004813 } else {
4814 if (wc->level == 1 &&
4815 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4816 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004817 }
4818reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004819 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004820 nread++;
4821 }
4822 wc->reada_slot = slot;
4823}
4824
Chris Mason9aca1d52007-03-13 11:09:37 -04004825/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004826 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004827 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004828 * when wc->stage == UPDATE_BACKREF, this function updates
4829 * back refs for pointers in the block.
4830 *
4831 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004832 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004833static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4834 struct btrfs_root *root,
4835 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004836 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004837{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004838 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004839 int level = wc->level;
4840 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004841 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4842 int ret;
4843
4844 if (wc->stage == UPDATE_BACKREF &&
4845 btrfs_header_owner(eb) != root->root_key.objectid)
4846 return 1;
4847
4848 /*
4849 * when reference count of tree block is 1, it won't increase
4850 * again. once full backref flag is set, we never clear it.
4851 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004852 if (lookup_info &&
4853 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4854 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004855 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004856 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004857 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004858 &wc->refs[level],
4859 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004860 BUG_ON(ret == -ENOMEM);
4861 if (ret)
4862 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004863 BUG_ON(wc->refs[level] == 0);
4864 }
4865
Yan Zheng2c47e6052009-06-27 21:07:35 -04004866 if (wc->stage == DROP_REFERENCE) {
4867 if (wc->refs[level] > 1)
4868 return 1;
4869
4870 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004871 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004872 path->locks[level] = 0;
4873 }
4874 return 0;
4875 }
4876
4877 /* wc->stage == UPDATE_BACKREF */
4878 if (!(wc->flags[level] & flag)) {
4879 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004880 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004881 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004882 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004883 BUG_ON(ret); /* -ENOMEM */
David Sterba42c9d0b2019-03-20 11:54:13 +01004884 ret = btrfs_set_disk_extent_flags(trans, eb, flag,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004885 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004886 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004887 wc->flags[level] |= flag;
4888 }
4889
4890 /*
4891 * the block is shared by multiple trees, so it's not good to
4892 * keep the tree lock
4893 */
4894 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04004895 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004896 path->locks[level] = 0;
4897 }
4898 return 0;
4899}
4900
4901/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004902 * This is used to verify a ref exists for this root to deal with a bug where we
4903 * would have a drop_progress key that hadn't been updated properly.
4904 */
4905static int check_ref_exists(struct btrfs_trans_handle *trans,
4906 struct btrfs_root *root, u64 bytenr, u64 parent,
4907 int level)
4908{
4909 struct btrfs_path *path;
4910 struct btrfs_extent_inline_ref *iref;
4911 int ret;
4912
4913 path = btrfs_alloc_path();
4914 if (!path)
4915 return -ENOMEM;
4916
4917 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4918 root->fs_info->nodesize, parent,
4919 root->root_key.objectid, level, 0);
4920 btrfs_free_path(path);
4921 if (ret == -ENOENT)
4922 return 0;
4923 if (ret < 0)
4924 return ret;
4925 return 1;
4926}
4927
4928/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004929 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004930 *
4931 * when wc->stage == DROP_REFERENCE, this function checks
4932 * reference count of the block pointed to. if the block
4933 * is shared and we need update back refs for the subtree
4934 * rooted at the block, this function changes wc->stage to
4935 * UPDATE_BACKREF. if the block is shared and there is no
4936 * need to update back, this function drops the reference
4937 * to the block.
4938 *
4939 * NOTE: return value 1 means we should stop walking down.
4940 */
4941static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4942 struct btrfs_root *root,
4943 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004944 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004945{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004946 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004947 u64 bytenr;
4948 u64 generation;
4949 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004950 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004951 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004952 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004953 struct extent_buffer *next;
4954 int level = wc->level;
4955 int reada = 0;
4956 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004957 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004958
4959 generation = btrfs_node_ptr_generation(path->nodes[level],
4960 path->slots[level]);
4961 /*
4962 * if the lower level block was created before the snapshot
4963 * was created, we know there is no need to update back refs
4964 * for the subtree
4965 */
4966 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004967 generation <= root->root_key.offset) {
4968 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004969 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004970 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004971
4972 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004973 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4974 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004975
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004976 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004977 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004978 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004979 if (IS_ERR(next))
4980 return PTR_ERR(next);
4981
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004982 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4983 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004984 reada = 1;
4985 }
4986 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004987 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004988
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004989 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004990 &wc->refs[level - 1],
4991 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004992 if (ret < 0)
4993 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004994
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004995 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004996 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004997 ret = -EIO;
4998 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004999 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005000 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005001
Yan, Zheng94fcca92009-10-09 09:25:16 -04005002 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005003 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07005004 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005005 if (level == 1 &&
5006 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5007 goto skip;
5008
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005009 if (!wc->update_ref ||
5010 generation <= root->root_key.offset)
5011 goto skip;
5012
5013 btrfs_node_key_to_cpu(path->nodes[level], &key,
5014 path->slots[level]);
5015 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5016 if (ret < 0)
5017 goto skip;
5018
5019 wc->stage = UPDATE_BACKREF;
5020 wc->shared_level = level - 1;
5021 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005022 } else {
5023 if (level == 1 &&
5024 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5025 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005026 }
5027
Chris Masonb9fab912012-05-06 07:23:47 -04005028 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005029 btrfs_tree_unlock(next);
5030 free_extent_buffer(next);
5031 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005032 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005033 }
5034
5035 if (!next) {
5036 if (reada && level == 1)
5037 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08005038 next = read_tree_block(fs_info, bytenr, generation, level - 1,
5039 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08005040 if (IS_ERR(next)) {
5041 return PTR_ERR(next);
5042 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04005043 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00005044 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04005045 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005046 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02005047 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005048 }
5049
5050 level--;
Josef Bacik48672682016-09-23 13:23:28 +02005051 ASSERT(level == btrfs_header_level(next));
5052 if (level != btrfs_header_level(next)) {
5053 btrfs_err(root->fs_info, "mismatched level");
5054 ret = -EIO;
5055 goto out_unlock;
5056 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005057 path->nodes[level] = next;
5058 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005059 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005060 wc->level = level;
5061 if (wc->level == 1)
5062 wc->reada_slot = 0;
5063 return 0;
5064skip:
5065 wc->refs[level - 1] = 0;
5066 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005067 if (wc->stage == DROP_REFERENCE) {
5068 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5069 parent = path->nodes[level]->start;
5070 } else {
Josef Bacik48672682016-09-23 13:23:28 +02005071 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04005072 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02005073 if (root->root_key.objectid !=
5074 btrfs_header_owner(path->nodes[level])) {
5075 btrfs_err(root->fs_info,
5076 "mismatched block owner");
5077 ret = -EIO;
5078 goto out_unlock;
5079 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005080 parent = 0;
5081 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005082
Qu Wenruo2cd86d32018-09-27 14:42:33 +08005083 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05005084 * If we had a drop_progress we need to verify the refs are set
5085 * as expected. If we find our ref then we know that from here
5086 * on out everything should be correct, and we can clear the
5087 * ->restarted flag.
5088 */
5089 if (wc->restarted) {
5090 ret = check_ref_exists(trans, root, bytenr, parent,
5091 level - 1);
5092 if (ret < 0)
5093 goto out_unlock;
5094 if (ret == 0)
5095 goto no_delete;
5096 ret = 0;
5097 wc->restarted = 0;
5098 }
5099
5100 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08005101 * Reloc tree doesn't contribute to qgroup numbers, and we have
5102 * already accounted them at merge time (replace_path),
5103 * thus we could skip expensive subtree trace here.
5104 */
5105 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
5106 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08005107 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08005108 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07005109 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005110 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005111 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
5112 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07005113 }
5114 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005115
5116 /*
5117 * We need to update the next key in our walk control so we can
5118 * update the drop_progress key accordingly. We don't care if
5119 * find_next_key doesn't find a key because that means we're at
5120 * the end and are going to clean up now.
5121 */
5122 wc->drop_level = level;
5123 find_next_key(path, level, &wc->drop_progress);
5124
Qu Wenruoffd4bb22019-04-04 14:45:36 +08005125 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
5126 fs_info->nodesize, parent);
5127 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
5128 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02005129 if (ret)
5130 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005131 }
Josef Bacik78c52d92019-02-06 15:46:14 -05005132no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02005133 *lookup_info = 1;
5134 ret = 1;
5135
5136out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005137 btrfs_tree_unlock(next);
5138 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02005139
5140 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005141}
5142
5143/*
Liu Bo2c016dc2012-12-26 15:32:17 +08005144 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04005145 *
5146 * when wc->stage == DROP_REFERENCE, this function drops
5147 * reference count on the block.
5148 *
5149 * when wc->stage == UPDATE_BACKREF, this function changes
5150 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5151 * to UPDATE_BACKREF previously while processing the block.
5152 *
5153 * NOTE: return value 1 means we should stop walking up.
5154 */
5155static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5156 struct btrfs_root *root,
5157 struct btrfs_path *path,
5158 struct walk_control *wc)
5159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005160 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005161 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005162 int level = wc->level;
5163 struct extent_buffer *eb = path->nodes[level];
5164 u64 parent = 0;
5165
5166 if (wc->stage == UPDATE_BACKREF) {
5167 BUG_ON(wc->shared_level < level);
5168 if (level < wc->shared_level)
5169 goto out;
5170
Yan Zheng2c47e6052009-06-27 21:07:35 -04005171 ret = find_next_key(path, level + 1, &wc->update_progress);
5172 if (ret > 0)
5173 wc->update_ref = 0;
5174
5175 wc->stage = DROP_REFERENCE;
5176 wc->shared_level = -1;
5177 path->slots[level] = 0;
5178
5179 /*
5180 * check reference count again if the block isn't locked.
5181 * we should start walking down the tree again if reference
5182 * count is one.
5183 */
5184 if (!path->locks[level]) {
5185 BUG_ON(level == 0);
5186 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005187 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005188 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005189
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005190 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005191 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005192 &wc->refs[level],
5193 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005194 if (ret < 0) {
5195 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005196 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005197 return ret;
5198 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005199 BUG_ON(wc->refs[level] == 0);
5200 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005201 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005202 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005203 return 1;
5204 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005205 }
5206 }
5207
5208 /* wc->stage == DROP_REFERENCE */
5209 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5210
5211 if (wc->refs[level] == 1) {
5212 if (level == 0) {
5213 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005214 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005215 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005216 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005217 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005218 if (is_fstree(root->root_key.objectid)) {
5219 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5220 if (ret) {
5221 btrfs_err_rl(fs_info,
5222 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005223 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005224 }
Mark Fasheh11526512014-07-17 12:39:01 -07005225 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005226 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005227 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005228 if (!path->locks[level] &&
5229 btrfs_header_generation(eb) == trans->transid) {
5230 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005231 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005232 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005233 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005234 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005235 }
5236
5237 if (eb == root->node) {
5238 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5239 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005240 else if (root->root_key.objectid != btrfs_header_owner(eb))
5241 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005242 } else {
5243 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5244 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005245 else if (root->root_key.objectid !=
5246 btrfs_header_owner(path->nodes[level + 1]))
5247 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005248 }
5249
Jan Schmidt5581a512012-05-16 17:04:52 +02005250 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005251out:
5252 wc->refs[level] = 0;
5253 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005254 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005255
5256owner_mismatch:
5257 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5258 btrfs_header_owner(eb), root->root_key.objectid);
5259 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005260}
5261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005262static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5263 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005264 struct btrfs_path *path,
5265 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005266{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005267 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005268 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005269 int ret;
5270
Yan Zheng2c47e6052009-06-27 21:07:35 -04005271 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005272 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005273 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005274 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005275
Yan Zheng2c47e6052009-06-27 21:07:35 -04005276 if (level == 0)
5277 break;
5278
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005279 if (path->slots[level] >=
5280 btrfs_header_nritems(path->nodes[level]))
5281 break;
5282
Yan, Zheng94fcca92009-10-09 09:25:16 -04005283 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005284 if (ret > 0) {
5285 path->slots[level]++;
5286 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005287 } else if (ret < 0)
5288 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005289 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005290 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005291 return 0;
5292}
5293
Chris Masond3977122009-01-05 21:25:51 -05005294static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005295 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005296 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005297 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005298{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005299 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005300 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005301
Yan Zheng2c47e6052009-06-27 21:07:35 -04005302 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5303 while (level < max_level && path->nodes[level]) {
5304 wc->level = level;
5305 if (path->slots[level] + 1 <
5306 btrfs_header_nritems(path->nodes[level])) {
5307 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005308 return 0;
5309 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005310 ret = walk_up_proc(trans, root, path, wc);
5311 if (ret > 0)
5312 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005313 if (ret < 0)
5314 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005315
Yan Zheng2c47e6052009-06-27 21:07:35 -04005316 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005317 btrfs_tree_unlock_rw(path->nodes[level],
5318 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005319 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005320 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005321 free_extent_buffer(path->nodes[level]);
5322 path->nodes[level] = NULL;
5323 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005324 }
5325 }
5326 return 1;
5327}
5328
Chris Mason9aca1d52007-03-13 11:09:37 -04005329/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005330 * drop a subvolume tree.
5331 *
5332 * this function traverses the tree freeing any blocks that only
5333 * referenced by the tree.
5334 *
5335 * when a shared tree block is found. this function decreases its
5336 * reference count by one. if update_ref is true, this function
5337 * also make sure backrefs for the shared block and all lower level
5338 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005339 *
5340 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005341 */
Nikolay Borisov0078a9f2020-03-10 11:43:51 +02005342int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005343{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005344 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005345 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005346 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005347 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005348 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005349 struct walk_control *wc;
5350 struct btrfs_key key;
5351 int err = 0;
5352 int ret;
5353 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005354 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005355
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005356 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005357
Chris Mason5caf2a02007-04-02 11:20:42 -04005358 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005359 if (!path) {
5360 err = -ENOMEM;
5361 goto out;
5362 }
Chris Mason20524f02007-03-10 06:35:47 -05005363
Yan Zheng2c47e6052009-06-27 21:07:35 -04005364 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005365 if (!wc) {
5366 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005367 err = -ENOMEM;
5368 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005369 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005370
Qu Wenruof3e3d9c2020-07-13 09:03:20 +08005371 /*
5372 * Use join to avoid potential EINTR from transaction start. See
5373 * wait_reserve_ticket and the whole reservation callchain.
5374 */
5375 if (for_reloc)
5376 trans = btrfs_join_transaction(tree_root);
5377 else
5378 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005379 if (IS_ERR(trans)) {
5380 err = PTR_ERR(trans);
5381 goto out_free;
5382 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005383
Josef Bacik0568e822018-11-30 11:52:14 -05005384 err = btrfs_run_delayed_items(trans);
5385 if (err)
5386 goto out_end_trans;
5387
Josef Bacik83354f02018-11-30 11:52:13 -05005388 /*
5389 * This will help us catch people modifying the fs tree while we're
5390 * dropping it. It is unsafe to mess with the fs tree while it's being
5391 * dropped as we unlock the root node and parent nodes as we walk down
5392 * the tree, assuming nothing will change. If something does change
5393 * then we'll have stale information and drop references to blocks we've
5394 * already dropped.
5395 */
5396 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005397 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005398 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005399 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005400 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005401 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005402 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005403 memset(&wc->update_progress, 0,
5404 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005405 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005406 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005407 memcpy(&wc->update_progress, &key,
5408 sizeof(wc->update_progress));
5409
Chris Mason6702ed42007-08-07 16:15:09 -04005410 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005411 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005412 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5414 path->lowest_level = 0;
5415 if (ret < 0) {
5416 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005417 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005418 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005419 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005420
Chris Mason7d9eb122008-07-08 14:19:17 -04005421 /*
5422 * unlock our path, this is safe because only this
5423 * function is allowed to delete this snapshot
5424 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005425 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005426
Yan Zheng2c47e6052009-06-27 21:07:35 -04005427 level = btrfs_header_level(root->node);
5428 while (1) {
5429 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005430 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005431 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005432
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005433 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005434 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005435 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005436 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005437 if (ret < 0) {
5438 err = ret;
5439 goto out_end_trans;
5440 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005441 BUG_ON(wc->refs[level] == 0);
5442
5443 if (level == root_item->drop_level)
5444 break;
5445
5446 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005447 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005448 WARN_ON(wc->refs[level] != 1);
5449 level--;
5450 }
5451 }
5452
Josef Bacik78c52d92019-02-06 15:46:14 -05005453 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005454 wc->level = level;
5455 wc->shared_level = -1;
5456 wc->stage = DROP_REFERENCE;
5457 wc->update_ref = update_ref;
5458 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005459 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005460
5461 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005462
Yan Zheng2c47e6052009-06-27 21:07:35 -04005463 ret = walk_down_tree(trans, root, path, wc);
5464 if (ret < 0) {
5465 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005466 break;
5467 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005468
5469 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5470 if (ret < 0) {
5471 err = ret;
5472 break;
5473 }
5474
5475 if (ret > 0) {
5476 BUG_ON(wc->stage != DROP_REFERENCE);
5477 break;
5478 }
5479
5480 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005481 wc->drop_level = wc->level;
5482 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5483 &wc->drop_progress,
5484 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005485 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005486 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5487 &wc->drop_progress);
5488 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005489
5490 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005491 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005492 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005493 ret = btrfs_update_root(trans, tree_root,
5494 &root->root_key,
5495 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005496 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005497 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005498 err = ret;
5499 goto out_end_trans;
5500 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005501
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005502 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005503 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005504 btrfs_debug(fs_info,
5505 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005506 err = -EAGAIN;
5507 goto out_free;
5508 }
5509
Josef Bacik9e2fc8f2020-12-16 11:22:05 -05005510 /*
5511 * Use join to avoid potential EINTR from transaction
5512 * start. See wait_reserve_ticket and the whole
5513 * reservation callchain.
5514 */
5515 if (for_reloc)
5516 trans = btrfs_join_transaction(tree_root);
5517 else
5518 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005519 if (IS_ERR(trans)) {
5520 err = PTR_ERR(trans);
5521 goto out_free;
5522 }
Chris Masonc3e69d52009-03-13 10:17:05 -04005523 }
Chris Mason20524f02007-03-10 06:35:47 -05005524 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005525 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005526 if (err)
5527 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005528
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005529 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005530 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005531 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005532 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005533 goto out_end_trans;
5534 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005535
Yan, Zheng76dda932009-09-21 16:00:26 -04005536 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005537 ret = btrfs_find_root(tree_root, &root->root_key, path,
5538 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005539 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005540 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005541 err = ret;
5542 goto out_end_trans;
5543 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005544 /* if we fail to delete the orphan item this time
5545 * around, it'll get picked up the next time.
5546 *
5547 * The most common failure here is just -ENOENT.
5548 */
5549 btrfs_del_orphan_item(trans, tree_root,
5550 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005551 }
5552 }
5553
Qu Wenruoa3cf0e42020-07-14 09:12:20 +08005554 /*
5555 * This subvolume is going to be completely dropped, and won't be
5556 * recorded as dirty roots, thus pertrans meta rsv will not be freed at
5557 * commit transaction time. So free it here manually.
5558 */
5559 btrfs_qgroup_convert_reserved_meta(root, INT_MAX);
5560 btrfs_qgroup_free_meta_all_pertrans(root);
5561
Josef Bacik8c389382020-02-14 16:11:42 -05005562 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state))
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005563 btrfs_add_dropped_root(trans, root);
Josef Bacik8c389382020-02-14 16:11:42 -05005564 else
Josef Bacik00246522020-01-24 09:33:01 -05005565 btrfs_put_root(root);
Josef Bacikd29a9f62013-07-17 19:30:20 -04005566 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005567out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005568 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005569out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005570 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005571 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005572out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005573 /*
5574 * So if we need to stop dropping the snapshot for whatever reason we
5575 * need to make sure to add it back to the dead root list so that we
5576 * keep trying to do the work later. This also cleans up roots if we
5577 * don't have it in the radix (like when we recover after a power fail
5578 * or unmount) so we don't leak memory.
5579 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005580 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005581 btrfs_add_dead_root(root);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005582 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005583}
Chris Mason9078a3e2007-04-26 16:46:15 -04005584
Yan Zheng2c47e6052009-06-27 21:07:35 -04005585/*
5586 * drop subtree rooted at tree block 'node'.
5587 *
5588 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005589 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005590 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005591int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5592 struct btrfs_root *root,
5593 struct extent_buffer *node,
5594 struct extent_buffer *parent)
5595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005596 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005597 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005598 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005599 int level;
5600 int parent_level;
5601 int ret = 0;
5602 int wret;
5603
Yan Zheng2c47e6052009-06-27 21:07:35 -04005604 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5605
Yan Zhengf82d02d2008-10-29 14:49:05 -04005606 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005607 if (!path)
5608 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005609
Yan Zheng2c47e6052009-06-27 21:07:35 -04005610 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005611 if (!wc) {
5612 btrfs_free_path(path);
5613 return -ENOMEM;
5614 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005615
Chris Masonb9447ef82009-03-09 11:45:38 -04005616 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005617 parent_level = btrfs_header_level(parent);
David Sterba67439da2019-10-08 13:28:47 +02005618 atomic_inc(&parent->refs);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005619 path->nodes[parent_level] = parent;
5620 path->slots[parent_level] = btrfs_header_nritems(parent);
5621
Chris Masonb9447ef82009-03-09 11:45:38 -04005622 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005623 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005624 path->nodes[level] = node;
5625 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005626 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005627
5628 wc->refs[parent_level] = 1;
5629 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5630 wc->level = level;
5631 wc->shared_level = -1;
5632 wc->stage = DROP_REFERENCE;
5633 wc->update_ref = 0;
5634 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005635 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005636
5637 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005638 wret = walk_down_tree(trans, root, path, wc);
5639 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005640 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005641 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005642 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005643
Yan Zheng2c47e6052009-06-27 21:07:35 -04005644 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005645 if (wret < 0)
5646 ret = wret;
5647 if (wret != 0)
5648 break;
5649 }
5650
Yan Zheng2c47e6052009-06-27 21:07:35 -04005651 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005652 btrfs_free_path(path);
5653 return ret;
5654}
5655
Miao Xie6d07bce2011-01-05 10:07:31 +00005656/*
5657 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005658 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005659 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005660u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005661{
David Sterba32da53862019-10-29 19:20:18 +01005662 struct btrfs_block_group *block_group;
Miao Xie6d07bce2011-01-05 10:07:31 +00005663 u64 free_bytes = 0;
5664 int factor;
5665
Nicholas D Steeves01327612016-05-19 21:18:45 -04005666 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005667 if (list_empty(&sinfo->ro_bgs))
5668 return 0;
5669
5670 spin_lock(&sinfo->lock);
5671 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005672 spin_lock(&block_group->lock);
5673
5674 if (!block_group->ro) {
5675 spin_unlock(&block_group->lock);
5676 continue;
5677 }
5678
David Sterba46df06b2018-07-13 20:46:30 +02005679 factor = btrfs_bg_type_to_factor(block_group->flags);
David Sterbab3470b52019-10-23 18:48:22 +02005680 free_bytes += (block_group->length -
David Sterbabf38be62019-10-23 18:48:11 +02005681 block_group->used) * factor;
Miao Xie6d07bce2011-01-05 10:07:31 +00005682
5683 spin_unlock(&block_group->lock);
5684 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005685 spin_unlock(&sinfo->lock);
5686
5687 return free_bytes;
5688}
5689
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005690int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5691 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005692{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005693 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005694}
5695
Jeff Mahoney499f3772015-06-15 09:41:17 -04005696/*
5697 * It used to be that old block groups would be left around forever.
5698 * Iterating over them would be enough to trim unused space. Since we
5699 * now automatically remove them, we also need to iterate over unallocated
5700 * space.
5701 *
5702 * We don't want a transaction for this since the discard may take a
5703 * substantial amount of time. We don't require that a transaction be
5704 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005705 * to ensure that we're not discarding chunks that were released or
5706 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005707 *
5708 * Holding the chunks lock will prevent other threads from allocating
5709 * or releasing chunks, but it won't prevent a running transaction
5710 * from committing and releasing the memory that the pending chunks
5711 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005712 * transaction and hold the commit root sem. We only need to hold
5713 * it while performing the free space search since we have already
5714 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005715 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005716static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005717{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005718 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005719 int ret;
5720
5721 *trimmed = 0;
5722
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005723 /* Discard not supported = nothing to do. */
5724 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5725 return 0;
5726
Andrea Gelmini52042d82018-11-28 12:05:13 +01005727 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005728 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005729 return 0;
5730
5731 /* No free space = nothing to do. */
5732 if (device->total_bytes <= device->bytes_used)
5733 return 0;
5734
5735 ret = 0;
5736
5737 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005738 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005739 u64 bytes;
5740
5741 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5742 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005743 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005744
Nikolay Borisov929be172019-03-27 14:24:18 +02005745 find_first_clear_extent_bit(&device->alloc_state, start,
5746 &start, &end,
5747 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005748
Qu Wenruoc57dd1f2020-07-31 19:29:11 +08005749 /* Check if there are any CHUNK_* bits left */
5750 if (start > device->total_bytes) {
5751 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
5752 btrfs_warn_in_rcu(fs_info,
5753"ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu",
5754 start, end - start + 1,
5755 rcu_str_deref(device->name),
5756 device->total_bytes);
5757 mutex_unlock(&fs_info->chunk_mutex);
5758 ret = 0;
5759 break;
5760 }
5761
Nikolay Borisov53460a42019-06-03 13:06:01 +03005762 /* Ensure we skip the reserved area in the first 1M */
5763 start = max_t(u64, start, SZ_1M);
5764
Nikolay Borisov929be172019-03-27 14:24:18 +02005765 /*
5766 * If find_first_clear_extent_bit find a range that spans the
5767 * end of the device it will set end to -1, in this case it's up
5768 * to the caller to trim the value to the size of the device.
5769 */
5770 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005771
Nikolay Borisov929be172019-03-27 14:24:18 +02005772 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005773
Nikolay Borisov929be172019-03-27 14:24:18 +02005774 /* We didn't find any extents */
5775 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005776 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005777 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005778 break;
5779 }
5780
Nikolay Borisov929be172019-03-27 14:24:18 +02005781 ret = btrfs_issue_discard(device->bdev, start, len,
5782 &bytes);
5783 if (!ret)
5784 set_extent_bits(&device->alloc_state, start,
5785 start + bytes - 1,
5786 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005787 mutex_unlock(&fs_info->chunk_mutex);
5788
5789 if (ret)
5790 break;
5791
5792 start += len;
5793 *trimmed += bytes;
5794
5795 if (fatal_signal_pending(current)) {
5796 ret = -ERESTARTSYS;
5797 break;
5798 }
5799
5800 cond_resched();
5801 }
5802
5803 return ret;
5804}
5805
Qu Wenruo93bba242018-09-07 14:16:23 +08005806/*
5807 * Trim the whole filesystem by:
5808 * 1) trimming the free space in each block group
5809 * 2) trimming the unallocated space on each device
5810 *
5811 * This will also continue trimming even if a block group or device encounters
5812 * an error. The return value will be the last error, or 0 if nothing bad
5813 * happens.
5814 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005815int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005816{
David Sterba32da53862019-10-29 19:20:18 +01005817 struct btrfs_block_group *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005818 struct btrfs_device *device;
5819 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005820 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005821 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005822 u64 start;
5823 u64 end;
5824 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005825 u64 bg_failed = 0;
5826 u64 dev_failed = 0;
5827 int bg_ret = 0;
5828 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005829 int ret = 0;
5830
Qu Wenruo07301df2019-05-28 16:21:54 +08005831 /*
5832 * Check range overflow if range->len is set.
5833 * The default range->len is U64_MAX.
5834 */
5835 if (range->len != U64_MAX &&
5836 check_add_overflow(range->start, range->len, &range_end))
5837 return -EINVAL;
5838
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005839 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005840 for (; cache; cache = btrfs_next_block_group(cache)) {
David Sterbab3470b52019-10-23 18:48:22 +02005841 if (cache->start >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005842 btrfs_put_block_group(cache);
5843 break;
5844 }
5845
David Sterbab3470b52019-10-23 18:48:22 +02005846 start = max(range->start, cache->start);
5847 end = min(range_end, cache->start + cache->length);
Li Dongyangf7039b12011-03-24 10:24:28 +00005848
5849 if (end - start >= range->minlen) {
David Sterba32da53862019-10-29 19:20:18 +01005850 if (!btrfs_block_group_done(cache)) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005851 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005852 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005853 bg_failed++;
5854 bg_ret = ret;
5855 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005856 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005857 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005858 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005859 bg_failed++;
5860 bg_ret = ret;
5861 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005862 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005863 }
5864 ret = btrfs_trim_block_group(cache,
5865 &group_trimmed,
5866 start,
5867 end,
5868 range->minlen);
5869
5870 trimmed += group_trimmed;
5871 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005872 bg_failed++;
5873 bg_ret = ret;
5874 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005875 }
5876 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005877 }
5878
Qu Wenruo93bba242018-09-07 14:16:23 +08005879 if (bg_failed)
5880 btrfs_warn(fs_info,
5881 "failed to trim %llu block group(s), last error %d",
5882 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005883 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005884 devices = &fs_info->fs_devices->devices;
5885 list_for_each_entry(device, devices, dev_list) {
Anand Jain755971d2021-07-04 19:14:39 +08005886 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
5887 continue;
5888
Nikolay Borisov8103d102019-06-03 13:06:00 +03005889 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005890 if (ret) {
5891 dev_failed++;
5892 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005893 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005894 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005895
5896 trimmed += group_trimmed;
5897 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005898 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005899
Qu Wenruo93bba242018-09-07 14:16:23 +08005900 if (dev_failed)
5901 btrfs_warn(fs_info,
5902 "failed to trim %llu device(s), last error %d",
5903 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005904 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005905 if (bg_ret)
5906 return bg_ret;
5907 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005908}