blob: a3778937d7050e5b5d9618a99f2f3173ca1cccce [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Zach Brownec6b9102007-07-11 10:00:37 -04006#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01007#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -05008#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -04009#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010010#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050011#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040012#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040013#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020015#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040016#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040017#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020018#include <linux/crc32c.h>
David Sterba602cbe92019-08-21 18:48:25 +020019#include "misc.h"
Miao Xie995946d2014-04-02 19:51:06 +080020#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050021#include "disk-io.h"
22#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040023#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050024#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040026#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070027#include "free-space-tree.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040028#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070029#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040030#include "ref-verify.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040031#include "space-info.h"
Josef Bacikd12ffdd2019-06-19 13:47:17 -040032#include "block-rsv.h"
Josef Bacik86736342019-06-19 15:12:00 -040033#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040034#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080035#include "discard.h"
Chris Masonfec577f2007-02-26 10:40:21 -050036
Arne Jansen709c0482011-09-12 12:22:57 +020037#undef SCRAMBLE_DELAYED_REFS
38
Qu Wenruo9f9b8e82018-10-24 20:24:01 +080039
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030041 struct btrfs_delayed_ref_node *node, u64 parent,
42 u64 root_objectid, u64 owner_objectid,
43 u64 owner_offset, int refs_to_drop,
44 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040045static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
46 struct extent_buffer *leaf,
47 struct btrfs_extent_item *ei);
48static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040049 u64 parent, u64 root_objectid,
50 u64 flags, u64 owner, u64 offset,
51 struct btrfs_key *ins, int ref_mod);
52static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030053 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030054 struct btrfs_delayed_extent_op *extent_op);
Yan Zheng11833d62009-09-11 16:11:19 -040055static int find_next_key(struct btrfs_path *path, int level,
56 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050057
David Sterba32da53862019-10-29 19:20:18 +010058static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
Josef Bacik0f9dd462008-09-23 13:14:11 -040059{
60 return (cache->flags & bits) == bits;
61}
62
Josef Bacik6f410d12019-06-20 15:37:49 -040063int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
64 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -040065{
Yan Zheng11833d62009-09-11 16:11:19 -040066 u64 end = start + num_bytes - 1;
Nikolay Borisovfe119a62020-01-20 16:09:18 +020067 set_extent_bits(&fs_info->excluded_extents, start, end,
68 EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040069 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -040070}
71
David Sterba32da53862019-10-29 19:20:18 +010072void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -040073{
Nikolay Borisov9e715da2018-06-20 15:49:08 +030074 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -040075 u64 start, end;
76
David Sterbab3470b52019-10-23 18:48:22 +020077 start = cache->start;
78 end = start + cache->length - 1;
Yan Zheng11833d62009-09-11 16:11:19 -040079
Nikolay Borisovfe119a62020-01-20 16:09:18 +020080 clear_extent_bits(&fs_info->excluded_extents, start, end,
81 EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040082}
83
Qu Wenruo78192442019-05-15 07:33:48 +080084static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
Omar Sandoval0d9f8242017-06-06 16:45:26 -070085{
Qu Wenruoddf30cf2019-04-04 14:45:34 +080086 if (ref->type == BTRFS_REF_METADATA) {
87 if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
Qu Wenruo78192442019-05-15 07:33:48 +080088 return BTRFS_BLOCK_GROUP_SYSTEM;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070089 else
Qu Wenruo78192442019-05-15 07:33:48 +080090 return BTRFS_BLOCK_GROUP_METADATA;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070091 }
Qu Wenruo78192442019-05-15 07:33:48 +080092 return BTRFS_BLOCK_GROUP_DATA;
93}
94
95static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
96 struct btrfs_ref *ref)
97{
98 struct btrfs_space_info *space_info;
99 u64 flags = generic_ref_to_space_flags(ref);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700100
Josef Bacik280c29082019-06-18 16:09:19 -0400101 space_info = btrfs_find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700102 ASSERT(space_info);
Qu Wenruo78192442019-05-15 07:33:48 +0800103 percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
104 BTRFS_TOTAL_BYTES_PINNED_BATCH);
105}
106
107static void sub_pinned_bytes(struct btrfs_fs_info *fs_info,
108 struct btrfs_ref *ref)
109{
110 struct btrfs_space_info *space_info;
111 u64 flags = generic_ref_to_space_flags(ref);
112
Josef Bacik280c29082019-06-18 16:09:19 -0400113 space_info = btrfs_find_space_info(fs_info, flags);
Qu Wenruo78192442019-05-15 07:33:48 +0800114 ASSERT(space_info);
115 percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
Ethan Liendec59fa2018-07-13 16:50:42 +0800116 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700117}
118
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000119/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400120int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400121{
122 int ret;
123 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400124 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400125
Zheng Yan31840ae2008-09-23 13:14:14 -0400126 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700127 if (!path)
128 return -ENOMEM;
129
Chris Masone02119d2008-09-05 16:13:11 -0400130 key.objectid = start;
131 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500132 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400133 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400134 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500135 return ret;
136}
137
Chris Masond8d5f3e2007-12-11 12:42:00 -0500138/*
Josef Bacik3173a182013-03-07 14:22:04 -0500139 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400140 *
141 * the head node for delayed ref is used to store the sum of all the
142 * reference count modifications queued up in the rbtree. the head
143 * node may also store the extent flags to set. This way you can check
144 * to see what the reference count and extent flags would be if all of
145 * the delayed refs are not processed.
146 */
147int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400148 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500149 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400150{
151 struct btrfs_delayed_ref_head *head;
152 struct btrfs_delayed_ref_root *delayed_refs;
153 struct btrfs_path *path;
154 struct btrfs_extent_item *ei;
155 struct extent_buffer *leaf;
156 struct btrfs_key key;
157 u32 item_size;
158 u64 num_refs;
159 u64 extent_flags;
160 int ret;
161
Josef Bacik3173a182013-03-07 14:22:04 -0500162 /*
163 * If we don't have skinny metadata, don't bother doing anything
164 * different
165 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400166 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
167 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500168 metadata = 0;
169 }
170
Yan, Zhenga22285a2010-05-16 10:48:46 -0400171 path = btrfs_alloc_path();
172 if (!path)
173 return -ENOMEM;
174
Yan, Zhenga22285a2010-05-16 10:48:46 -0400175 if (!trans) {
176 path->skip_locking = 1;
177 path->search_commit_root = 1;
178 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000179
180search_again:
181 key.objectid = bytenr;
182 key.offset = offset;
183 if (metadata)
184 key.type = BTRFS_METADATA_ITEM_KEY;
185 else
186 key.type = BTRFS_EXTENT_ITEM_KEY;
187
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400188 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400189 if (ret < 0)
190 goto out_free;
191
Josef Bacik3173a182013-03-07 14:22:04 -0500192 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100193 if (path->slots[0]) {
194 path->slots[0]--;
195 btrfs_item_key_to_cpu(path->nodes[0], &key,
196 path->slots[0]);
197 if (key.objectid == bytenr &&
198 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400199 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100200 ret = 0;
201 }
Josef Bacik3173a182013-03-07 14:22:04 -0500202 }
203
Yan, Zhenga22285a2010-05-16 10:48:46 -0400204 if (ret == 0) {
205 leaf = path->nodes[0];
206 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
207 if (item_size >= sizeof(*ei)) {
208 ei = btrfs_item_ptr(leaf, path->slots[0],
209 struct btrfs_extent_item);
210 num_refs = btrfs_extent_refs(leaf, ei);
211 extent_flags = btrfs_extent_flags(leaf, ei);
212 } else {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300213 ret = -EINVAL;
214 btrfs_print_v0_err(fs_info);
215 if (trans)
216 btrfs_abort_transaction(trans, ret);
217 else
218 btrfs_handle_fs_error(fs_info, ret, NULL);
219
220 goto out_free;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400221 }
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300222
Yan, Zhenga22285a2010-05-16 10:48:46 -0400223 BUG_ON(num_refs == 0);
224 } else {
225 num_refs = 0;
226 extent_flags = 0;
227 ret = 0;
228 }
229
230 if (!trans)
231 goto out;
232
233 delayed_refs = &trans->transaction->delayed_refs;
234 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800235 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400236 if (head) {
237 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400238 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400239 spin_unlock(&delayed_refs->lock);
240
David Sterbab3b4aa72011-04-21 01:20:15 +0200241 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400242
David Sterba8cc33e52011-05-02 15:29:25 +0200243 /*
244 * Mutex was contended, block until it's released and try
245 * again
246 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400247 mutex_lock(&head->mutex);
248 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400249 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000250 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400251 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500252 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400253 if (head->extent_op && head->extent_op->update_flags)
254 extent_flags |= head->extent_op->flags_to_set;
255 else
256 BUG_ON(num_refs == 0);
257
Josef Bacikd2788502017-09-29 15:43:57 -0400258 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500259 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400260 mutex_unlock(&head->mutex);
261 }
262 spin_unlock(&delayed_refs->lock);
263out:
264 WARN_ON(num_refs == 0);
265 if (refs)
266 *refs = num_refs;
267 if (flags)
268 *flags = extent_flags;
269out_free:
270 btrfs_free_path(path);
271 return ret;
272}
273
274/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500275 * Back reference rules. Back refs have three main goals:
276 *
277 * 1) differentiate between all holders of references to an extent so that
278 * when a reference is dropped we can make sure it was a valid reference
279 * before freeing the extent.
280 *
281 * 2) Provide enough information to quickly find the holders of an extent
282 * if we notice a given block is corrupted or bad.
283 *
284 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
285 * maintenance. This is actually the same as #2, but with a slightly
286 * different use case.
287 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 * There are two kinds of back refs. The implicit back refs is optimized
289 * for pointers in non-shared tree blocks. For a given pointer in a block,
290 * back refs of this kind provide information about the block's owner tree
291 * and the pointer's key. These information allow us to find the block by
292 * b-tree searching. The full back refs is for pointers in tree blocks not
293 * referenced by their owner trees. The location of tree block is recorded
294 * in the back refs. Actually the full back refs is generic, and can be
295 * used in all cases the implicit back refs is used. The major shortcoming
296 * of the full back refs is its overhead. Every time a tree block gets
297 * COWed, we have to update back refs entry for all pointers in it.
298 *
299 * For a newly allocated tree block, we use implicit back refs for
300 * pointers in it. This means most tree related operations only involve
301 * implicit back refs. For a tree block created in old transaction, the
302 * only way to drop a reference to it is COW it. So we can detect the
303 * event that tree block loses its owner tree's reference and do the
304 * back refs conversion.
305 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400306 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400307 *
308 * The reference count of the block is one and the tree is the block's
309 * owner tree. Nothing to do in this case.
310 *
311 * The reference count of the block is one and the tree is not the
312 * block's owner tree. In this case, full back refs is used for pointers
313 * in the block. Remove these full back refs, add implicit back refs for
314 * every pointers in the new block.
315 *
316 * The reference count of the block is greater than one and the tree is
317 * the block's owner tree. In this case, implicit back refs is used for
318 * pointers in the block. Add full back refs for every pointers in the
319 * block, increase lower level extents' reference counts. The original
320 * implicit back refs are entailed to the new block.
321 *
322 * The reference count of the block is greater than one and the tree is
323 * not the block's owner tree. Add implicit back refs for every pointer in
324 * the new block, increase lower level extents' reference count.
325 *
326 * Back Reference Key composing:
327 *
328 * The key objectid corresponds to the first byte in the extent,
329 * The key type is used to differentiate between types of back refs.
330 * There are different meanings of the key offset for different types
331 * of back refs.
332 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500333 * File extents can be referenced by:
334 *
335 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400336 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500337 * - different offsets inside a file (bookend extents in file.c)
338 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400339 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500340 *
341 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500342 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400343 * - original offset in the file
344 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400345 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400346 * The key offset for the implicit back refs is hash of the first
347 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500348 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400349 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500350 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400351 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500352 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400353 * The key offset for the implicit back refs is the first byte of
354 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500355 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400356 * When a file extent is allocated, The implicit back refs is used.
357 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500358 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 * (root_key.objectid, inode objectid, offset in file, 1)
360 *
361 * When a file extent is removed file truncation, we find the
362 * corresponding implicit back refs and check the following fields:
363 *
364 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500365 *
366 * Btree extents can be referenced by:
367 *
368 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500369 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 * Both the implicit back refs and the full back refs for tree blocks
371 * only consist of key. The key offset for the implicit back refs is
372 * objectid of block's owner tree. The key offset for the full back refs
373 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500374 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400375 * When implicit back refs is used, information about the lowest key and
376 * level of the tree block are required. These information are stored in
377 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500378 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400379
Liu Bo167ce952017-08-18 15:15:18 -0600380/*
381 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100382 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
Liu Bo167ce952017-08-18 15:15:18 -0600383 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
384 */
385int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
386 struct btrfs_extent_inline_ref *iref,
387 enum btrfs_inline_ref_type is_data)
388{
389 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -0600390 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -0600391
392 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
393 type == BTRFS_SHARED_BLOCK_REF_KEY ||
394 type == BTRFS_SHARED_DATA_REF_KEY ||
395 type == BTRFS_EXTENT_DATA_REF_KEY) {
396 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600397 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600398 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600399 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
400 ASSERT(eb->fs_info);
401 /*
402 * Every shared one has parent tree
403 * block, which must be aligned to
404 * nodesize.
405 */
406 if (offset &&
407 IS_ALIGNED(offset, eb->fs_info->nodesize))
408 return type;
409 }
Liu Bo167ce952017-08-18 15:15:18 -0600410 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600411 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600412 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600413 if (type == BTRFS_SHARED_DATA_REF_KEY) {
414 ASSERT(eb->fs_info);
415 /*
416 * Every shared one has parent tree
417 * block, which must be aligned to
418 * nodesize.
419 */
420 if (offset &&
421 IS_ALIGNED(offset, eb->fs_info->nodesize))
422 return type;
423 }
Liu Bo167ce952017-08-18 15:15:18 -0600424 } else {
425 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
426 return type;
427 }
428 }
429
430 btrfs_print_leaf((struct extent_buffer *)eb);
431 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
432 eb->start, type);
433 WARN_ON(1);
434
435 return BTRFS_REF_TYPE_INVALID;
436}
437
Qu Wenruo0785a9a2019-08-09 09:24:24 +0800438u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400439{
440 u32 high_crc = ~(u32)0;
441 u32 low_crc = ~(u32)0;
442 __le64 lenum;
443
444 lenum = cpu_to_le64(root_objectid);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200445 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400446 lenum = cpu_to_le64(owner);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200447 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400448 lenum = cpu_to_le64(offset);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200449 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450
451 return ((u64)high_crc << 31) ^ (u64)low_crc;
452}
453
454static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
455 struct btrfs_extent_data_ref *ref)
456{
457 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
458 btrfs_extent_data_ref_objectid(leaf, ref),
459 btrfs_extent_data_ref_offset(leaf, ref));
460}
461
462static int match_extent_data_ref(struct extent_buffer *leaf,
463 struct btrfs_extent_data_ref *ref,
464 u64 root_objectid, u64 owner, u64 offset)
465{
466 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
467 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
468 btrfs_extent_data_ref_offset(leaf, ref) != offset)
469 return 0;
470 return 1;
471}
472
473static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400474 struct btrfs_path *path,
475 u64 bytenr, u64 parent,
476 u64 root_objectid,
477 u64 owner, u64 offset)
478{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +0300479 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400480 struct btrfs_key key;
481 struct btrfs_extent_data_ref *ref;
482 struct extent_buffer *leaf;
483 u32 nritems;
484 int ret;
485 int recow;
486 int err = -ENOENT;
487
488 key.objectid = bytenr;
489 if (parent) {
490 key.type = BTRFS_SHARED_DATA_REF_KEY;
491 key.offset = parent;
492 } else {
493 key.type = BTRFS_EXTENT_DATA_REF_KEY;
494 key.offset = hash_extent_data_ref(root_objectid,
495 owner, offset);
496 }
497again:
498 recow = 0;
499 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
500 if (ret < 0) {
501 err = ret;
502 goto fail;
503 }
504
505 if (parent) {
506 if (!ret)
507 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400508 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400509 }
510
511 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400512 nritems = btrfs_header_nritems(leaf);
513 while (1) {
514 if (path->slots[0] >= nritems) {
515 ret = btrfs_next_leaf(root, path);
516 if (ret < 0)
517 err = ret;
518 if (ret)
519 goto fail;
520
521 leaf = path->nodes[0];
522 nritems = btrfs_header_nritems(leaf);
523 recow = 1;
524 }
525
526 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
527 if (key.objectid != bytenr ||
528 key.type != BTRFS_EXTENT_DATA_REF_KEY)
529 goto fail;
530
531 ref = btrfs_item_ptr(leaf, path->slots[0],
532 struct btrfs_extent_data_ref);
533
534 if (match_extent_data_ref(leaf, ref, root_objectid,
535 owner, offset)) {
536 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200537 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400538 goto again;
539 }
540 err = 0;
541 break;
542 }
543 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400544 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400545fail:
546 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400547}
548
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400550 struct btrfs_path *path,
551 u64 bytenr, u64 parent,
552 u64 root_objectid, u64 owner,
553 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400554{
Nikolay Borisov62b895a2018-06-20 15:48:44 +0300555 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -0400556 struct btrfs_key key;
557 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400558 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400559 u32 num_refs;
560 int ret;
561
562 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400563 if (parent) {
564 key.type = BTRFS_SHARED_DATA_REF_KEY;
565 key.offset = parent;
566 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400567 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400568 key.type = BTRFS_EXTENT_DATA_REF_KEY;
569 key.offset = hash_extent_data_ref(root_objectid,
570 owner, offset);
571 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400572 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400573
574 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
575 if (ret && ret != -EEXIST)
576 goto fail;
577
578 leaf = path->nodes[0];
579 if (parent) {
580 struct btrfs_shared_data_ref *ref;
581 ref = btrfs_item_ptr(leaf, path->slots[0],
582 struct btrfs_shared_data_ref);
583 if (ret == 0) {
584 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
585 } else {
586 num_refs = btrfs_shared_data_ref_count(leaf, ref);
587 num_refs += refs_to_add;
588 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
589 }
590 } else {
591 struct btrfs_extent_data_ref *ref;
592 while (ret == -EEXIST) {
593 ref = btrfs_item_ptr(leaf, path->slots[0],
594 struct btrfs_extent_data_ref);
595 if (match_extent_data_ref(leaf, ref, root_objectid,
596 owner, offset))
597 break;
David Sterbab3b4aa72011-04-21 01:20:15 +0200598 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400599 key.offset++;
600 ret = btrfs_insert_empty_item(trans, root, path, &key,
601 size);
602 if (ret && ret != -EEXIST)
603 goto fail;
604
605 leaf = path->nodes[0];
606 }
607 ref = btrfs_item_ptr(leaf, path->slots[0],
608 struct btrfs_extent_data_ref);
609 if (ret == 0) {
610 btrfs_set_extent_data_ref_root(leaf, ref,
611 root_objectid);
612 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
613 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
614 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
615 } else {
616 num_refs = btrfs_extent_data_ref_count(leaf, ref);
617 num_refs += refs_to_add;
618 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
619 }
620 }
621 btrfs_mark_buffer_dirty(leaf);
622 ret = 0;
623fail:
David Sterbab3b4aa72011-04-21 01:20:15 +0200624 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500625 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500626}
627
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400629 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -0700630 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -0400631{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 struct btrfs_key key;
633 struct btrfs_extent_data_ref *ref1 = NULL;
634 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400635 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400636 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400637 int ret = 0;
638
639 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400640 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
641
642 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
643 ref1 = btrfs_item_ptr(leaf, path->slots[0],
644 struct btrfs_extent_data_ref);
645 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
646 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
647 ref2 = btrfs_item_ptr(leaf, path->slots[0],
648 struct btrfs_shared_data_ref);
649 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200650 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300651 btrfs_print_v0_err(trans->fs_info);
652 btrfs_abort_transaction(trans, -EINVAL);
653 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400654 } else {
655 BUG();
656 }
657
Chris Mason56bec292009-03-13 10:10:06 -0400658 BUG_ON(num_refs < refs_to_drop);
659 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660
Zheng Yan31840ae2008-09-23 13:14:14 -0400661 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +0300662 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -0700663 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400664 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400665 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
666 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
667 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
668 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
Zheng Yan31840ae2008-09-23 13:14:14 -0400669 btrfs_mark_buffer_dirty(leaf);
670 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400671 return ret;
672}
673
Zhaolei9ed0dea2015-08-06 22:16:24 +0800674static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 struct btrfs_extent_inline_ref *iref)
676{
677 struct btrfs_key key;
678 struct extent_buffer *leaf;
679 struct btrfs_extent_data_ref *ref1;
680 struct btrfs_shared_data_ref *ref2;
681 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -0600682 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683
684 leaf = path->nodes[0];
685 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300686
687 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -0600689 /*
690 * If type is invalid, we should have bailed out earlier than
691 * this call.
692 */
693 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
694 ASSERT(type != BTRFS_REF_TYPE_INVALID);
695 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
697 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
698 } else {
699 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
700 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
701 }
702 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
703 ref1 = btrfs_item_ptr(leaf, path->slots[0],
704 struct btrfs_extent_data_ref);
705 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
706 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
707 ref2 = btrfs_item_ptr(leaf, path->slots[0],
708 struct btrfs_shared_data_ref);
709 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400710 } else {
711 WARN_ON(1);
712 }
713 return num_refs;
714}
715
716static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 struct btrfs_path *path,
718 u64 bytenr, u64 parent,
719 u64 root_objectid)
720{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +0300721 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 struct btrfs_key key;
723 int ret;
724
725 key.objectid = bytenr;
726 if (parent) {
727 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
728 key.offset = parent;
729 } else {
730 key.type = BTRFS_TREE_BLOCK_REF_KEY;
731 key.offset = root_objectid;
732 }
733
734 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
735 if (ret > 0)
736 ret = -ENOENT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400737 return ret;
738}
739
740static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400741 struct btrfs_path *path,
742 u64 bytenr, u64 parent,
743 u64 root_objectid)
744{
745 struct btrfs_key key;
746 int ret;
747
748 key.objectid = bytenr;
749 if (parent) {
750 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
751 key.offset = parent;
752 } else {
753 key.type = BTRFS_TREE_BLOCK_REF_KEY;
754 key.offset = root_objectid;
755 }
756
Nikolay Borisov10728402018-06-20 15:48:43 +0300757 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500758 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +0200759 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -0400760 return ret;
761}
762
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400763static inline int extent_ref_type(u64 parent, u64 owner)
764{
765 int type;
766 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
767 if (parent > 0)
768 type = BTRFS_SHARED_BLOCK_REF_KEY;
769 else
770 type = BTRFS_TREE_BLOCK_REF_KEY;
771 } else {
772 if (parent > 0)
773 type = BTRFS_SHARED_DATA_REF_KEY;
774 else
775 type = BTRFS_EXTENT_DATA_REF_KEY;
776 }
777 return type;
778}
779
Yan Zheng2c47e6052009-06-27 21:07:35 -0400780static int find_next_key(struct btrfs_path *path, int level,
781 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400782
783{
Yan Zheng2c47e6052009-06-27 21:07:35 -0400784 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400785 if (!path->nodes[level])
786 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400787 if (path->slots[level] + 1 >=
788 btrfs_header_nritems(path->nodes[level]))
789 continue;
790 if (level == 0)
791 btrfs_item_key_to_cpu(path->nodes[level], key,
792 path->slots[level] + 1);
793 else
794 btrfs_node_key_to_cpu(path->nodes[level], key,
795 path->slots[level] + 1);
796 return 0;
797 }
798 return 1;
799}
800
801/*
802 * look for inline back ref. if back ref is found, *ref_ret is set
803 * to the address of inline back ref, and 0 is returned.
804 *
805 * if back ref isn't found, *ref_ret is set to the address where it
806 * should be inserted, and -ENOENT is returned.
807 *
808 * if insert is true and there are too many inline back refs, the path
809 * points to the extent item, and -EAGAIN is returned.
810 *
811 * NOTE: inline back refs are ordered in the same way that back ref
812 * items in the tree are ordered.
813 */
814static noinline_for_stack
815int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400816 struct btrfs_path *path,
817 struct btrfs_extent_inline_ref **ref_ret,
818 u64 bytenr, u64 num_bytes,
819 u64 parent, u64 root_objectid,
820 u64 owner, u64 offset, int insert)
821{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +0300822 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500823 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400824 struct btrfs_key key;
825 struct extent_buffer *leaf;
826 struct btrfs_extent_item *ei;
827 struct btrfs_extent_inline_ref *iref;
828 u64 flags;
829 u64 item_size;
830 unsigned long ptr;
831 unsigned long end;
832 int extra_size;
833 int type;
834 int want;
835 int ret;
836 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400837 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -0600838 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839
840 key.objectid = bytenr;
841 key.type = BTRFS_EXTENT_ITEM_KEY;
842 key.offset = num_bytes;
843
844 want = extent_ref_type(parent, owner);
845 if (insert) {
846 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -0400847 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848 } else
849 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -0500850
851 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +0300852 * Owner is our level, so we can just add one to get the level for the
853 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -0500854 */
855 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
856 key.type = BTRFS_METADATA_ITEM_KEY;
857 key.offset = owner;
858 }
859
860again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
862 if (ret < 0) {
863 err = ret;
864 goto out;
865 }
Josef Bacik3173a182013-03-07 14:22:04 -0500866
867 /*
868 * We may be a newly converted file system which still has the old fat
869 * extent entries for metadata, so try and see if we have one of those.
870 */
871 if (ret > 0 && skinny_metadata) {
872 skinny_metadata = false;
873 if (path->slots[0]) {
874 path->slots[0]--;
875 btrfs_item_key_to_cpu(path->nodes[0], &key,
876 path->slots[0]);
877 if (key.objectid == bytenr &&
878 key.type == BTRFS_EXTENT_ITEM_KEY &&
879 key.offset == num_bytes)
880 ret = 0;
881 }
882 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +0100883 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500884 key.type = BTRFS_EXTENT_ITEM_KEY;
885 key.offset = num_bytes;
886 btrfs_release_path(path);
887 goto again;
888 }
889 }
890
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 if (ret && !insert) {
892 err = -ENOENT;
893 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530894 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -0500895 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -0500896 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100897 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898
899 leaf = path->nodes[0];
900 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200901 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300902 err = -EINVAL;
903 btrfs_print_v0_err(fs_info);
904 btrfs_abort_transaction(trans, err);
905 goto out;
906 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
909 flags = btrfs_extent_flags(leaf, ei);
910
911 ptr = (unsigned long)(ei + 1);
912 end = (unsigned long)ei + item_size;
913
Josef Bacik3173a182013-03-07 14:22:04 -0500914 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 ptr += sizeof(struct btrfs_tree_block_info);
916 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 }
918
Liu Bo3de28d52017-08-18 15:15:19 -0600919 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
920 needed = BTRFS_REF_TYPE_DATA;
921 else
922 needed = BTRFS_REF_TYPE_BLOCK;
923
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 err = -ENOENT;
925 while (1) {
926 if (ptr >= end) {
927 WARN_ON(ptr > end);
928 break;
929 }
930 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600931 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
932 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +0800933 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -0600934 goto out;
935 }
936
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937 if (want < type)
938 break;
939 if (want > type) {
940 ptr += btrfs_extent_inline_ref_size(type);
941 continue;
942 }
943
944 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
945 struct btrfs_extent_data_ref *dref;
946 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
947 if (match_extent_data_ref(leaf, dref, root_objectid,
948 owner, offset)) {
949 err = 0;
950 break;
951 }
952 if (hash_extent_data_ref_item(leaf, dref) <
953 hash_extent_data_ref(root_objectid, owner, offset))
954 break;
955 } else {
956 u64 ref_offset;
957 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
958 if (parent > 0) {
959 if (parent == ref_offset) {
960 err = 0;
961 break;
962 }
963 if (ref_offset < parent)
964 break;
965 } else {
966 if (root_objectid == ref_offset) {
967 err = 0;
968 break;
969 }
970 if (ref_offset < root_objectid)
971 break;
972 }
973 }
974 ptr += btrfs_extent_inline_ref_size(type);
975 }
976 if (err == -ENOENT && insert) {
977 if (item_size + extra_size >=
978 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
979 err = -EAGAIN;
980 goto out;
981 }
982 /*
983 * To add new inline back ref, we have to make sure
984 * there is no corresponding back ref item.
985 * For simplicity, we just do not add new inline back
986 * ref if there is any kind of item for this block
987 */
Yan Zheng2c47e6052009-06-27 21:07:35 -0400988 if (find_next_key(path, 0, &key) == 0 &&
989 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -0400990 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 err = -EAGAIN;
992 goto out;
993 }
994 }
995 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
996out:
Yan Zheng85d41982009-06-11 08:51:10 -0400997 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400998 path->keep_locks = 0;
999 btrfs_unlock_up_safe(path, 1);
1000 }
1001 return err;
1002}
1003
1004/*
1005 * helper to add new inline back ref
1006 */
1007static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001008void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001009 struct btrfs_path *path,
1010 struct btrfs_extent_inline_ref *iref,
1011 u64 parent, u64 root_objectid,
1012 u64 owner, u64 offset, int refs_to_add,
1013 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001014{
1015 struct extent_buffer *leaf;
1016 struct btrfs_extent_item *ei;
1017 unsigned long ptr;
1018 unsigned long end;
1019 unsigned long item_offset;
1020 u64 refs;
1021 int size;
1022 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001023
1024 leaf = path->nodes[0];
1025 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1026 item_offset = (unsigned long)iref - (unsigned long)ei;
1027
1028 type = extent_ref_type(parent, owner);
1029 size = btrfs_extent_inline_ref_size(type);
1030
David Sterbac71dd882019-03-20 14:51:10 +01001031 btrfs_extend_item(path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032
1033 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1034 refs = btrfs_extent_refs(leaf, ei);
1035 refs += refs_to_add;
1036 btrfs_set_extent_refs(leaf, ei, refs);
1037 if (extent_op)
1038 __run_delayed_extent_op(extent_op, leaf, ei);
1039
1040 ptr = (unsigned long)ei + item_offset;
1041 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1042 if (ptr < end - size)
1043 memmove_extent_buffer(leaf, ptr + size, ptr,
1044 end - size - ptr);
1045
1046 iref = (struct btrfs_extent_inline_ref *)ptr;
1047 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1048 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1049 struct btrfs_extent_data_ref *dref;
1050 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1051 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1052 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1053 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1054 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1055 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1056 struct btrfs_shared_data_ref *sref;
1057 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1058 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1059 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1060 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1061 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1062 } else {
1063 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1064 }
1065 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001066}
1067
1068static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069 struct btrfs_path *path,
1070 struct btrfs_extent_inline_ref **ref_ret,
1071 u64 bytenr, u64 num_bytes, u64 parent,
1072 u64 root_objectid, u64 owner, u64 offset)
1073{
1074 int ret;
1075
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001076 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1077 num_bytes, parent, root_objectid,
1078 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001079 if (ret != -ENOENT)
1080 return ret;
1081
David Sterbab3b4aa72011-04-21 01:20:15 +02001082 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 *ref_ret = NULL;
1084
1085 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001086 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1087 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001089 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1090 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001091 }
1092 return ret;
1093}
1094
1095/*
1096 * helper to update/remove inline back ref
1097 */
1098static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001099void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001100 struct btrfs_extent_inline_ref *iref,
1101 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001102 struct btrfs_delayed_extent_op *extent_op,
1103 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001105 struct extent_buffer *leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001106 struct btrfs_extent_item *ei;
1107 struct btrfs_extent_data_ref *dref = NULL;
1108 struct btrfs_shared_data_ref *sref = NULL;
1109 unsigned long ptr;
1110 unsigned long end;
1111 u32 item_size;
1112 int size;
1113 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 u64 refs;
1115
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1117 refs = btrfs_extent_refs(leaf, ei);
1118 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1119 refs += refs_to_mod;
1120 btrfs_set_extent_refs(leaf, ei, refs);
1121 if (extent_op)
1122 __run_delayed_extent_op(extent_op, leaf, ei);
1123
Liu Bo3de28d52017-08-18 15:15:19 -06001124 /*
1125 * If type is invalid, we should have bailed out after
1126 * lookup_inline_extent_backref().
1127 */
1128 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1129 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130
1131 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1132 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1133 refs = btrfs_extent_data_ref_count(leaf, dref);
1134 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1135 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1136 refs = btrfs_shared_data_ref_count(leaf, sref);
1137 } else {
1138 refs = 1;
1139 BUG_ON(refs_to_mod != -1);
1140 }
1141
1142 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1143 refs += refs_to_mod;
1144
1145 if (refs > 0) {
1146 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1147 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1148 else
1149 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1150 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001151 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 size = btrfs_extent_inline_ref_size(type);
1153 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1154 ptr = (unsigned long)iref;
1155 end = (unsigned long)ei + item_size;
1156 if (ptr + size < end)
1157 memmove_extent_buffer(leaf, ptr, ptr + size,
1158 end - ptr - size);
1159 item_size -= size;
David Sterba78ac4f92019-03-20 14:49:12 +01001160 btrfs_truncate_item(path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001161 }
1162 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163}
1164
1165static noinline_for_stack
1166int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001167 struct btrfs_path *path,
1168 u64 bytenr, u64 num_bytes, u64 parent,
1169 u64 root_objectid, u64 owner,
1170 u64 offset, int refs_to_add,
1171 struct btrfs_delayed_extent_op *extent_op)
1172{
1173 struct btrfs_extent_inline_ref *iref;
1174 int ret;
1175
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001176 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1177 num_bytes, parent, root_objectid,
1178 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 if (ret == 0) {
1180 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001181 update_inline_extent_backref(path, iref, refs_to_add,
1182 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 } else if (ret == -ENOENT) {
Nikolay Borisova639cde2018-06-20 15:49:10 +03001184 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001185 root_objectid, owner, offset,
1186 refs_to_add, extent_op);
1187 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001188 }
1189 return ret;
1190}
1191
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001192static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 struct btrfs_path *path,
1194 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001195 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001196{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001197 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001198
1199 BUG_ON(!is_data && refs_to_drop != 1);
1200 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001201 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1202 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001204 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001205 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001207 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001208 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 }
1210 return ret;
1211}
1212
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001213static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1214 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001215{
Jeff Mahoney86557862015-06-15 09:41:16 -04001216 int j, ret = 0;
1217 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001218 u64 aligned_start = ALIGN(start, 1 << 9);
1219
1220 if (WARN_ON(start != aligned_start)) {
1221 len -= aligned_start - start;
1222 len = round_down(len, 1 << 9);
1223 start = aligned_start;
1224 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001225
1226 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001227
1228 if (!len)
1229 return 0;
1230
1231 end = start + len;
1232 bytes_left = len;
1233
1234 /* Skip any superblocks on this device. */
1235 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1236 u64 sb_start = btrfs_sb_offset(j);
1237 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1238 u64 size = sb_start - start;
1239
1240 if (!in_range(sb_start, start, bytes_left) &&
1241 !in_range(sb_end, start, bytes_left) &&
1242 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1243 continue;
1244
1245 /*
1246 * Superblock spans beginning of range. Adjust start and
1247 * try again.
1248 */
1249 if (sb_start <= start) {
1250 start += sb_end - start;
1251 if (start > end) {
1252 bytes_left = 0;
1253 break;
1254 }
1255 bytes_left = end - start;
1256 continue;
1257 }
1258
1259 if (size) {
1260 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1261 GFP_NOFS, 0);
1262 if (!ret)
1263 *discarded_bytes += size;
1264 else if (ret != -EOPNOTSUPP)
1265 return ret;
1266 }
1267
1268 start = sb_end;
1269 if (start > end) {
1270 bytes_left = 0;
1271 break;
1272 }
1273 bytes_left = end - start;
1274 }
1275
1276 if (bytes_left) {
1277 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001278 GFP_NOFS, 0);
1279 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001280 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001281 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001282 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001283}
Chris Mason15916de2008-11-19 21:17:22 -05001284
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001285int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001286 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001287{
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001288 int ret = 0;
Li Dongyang5378e602011-03-24 10:24:27 +00001289 u64 discarded_bytes = 0;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001290 u64 end = bytenr + num_bytes;
1291 u64 cur = bytenr;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001292 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001293
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001294
Filipe Manana29992412016-05-27 17:42:05 +01001295 /*
1296 * Avoid races with device replace and make sure our bbio has devices
1297 * associated to its stripes that don't go away while we are discarding.
1298 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001299 btrfs_bio_counter_inc_blocked(fs_info);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001300 while (cur < end) {
1301 struct btrfs_bio_stripe *stripe;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001302 int i;
1303
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001304 num_bytes = end - cur;
1305 /* Tell the block device(s) that the sectors can be discarded */
1306 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1307 &num_bytes, &bbio, 0);
1308 /*
1309 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1310 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1311 * thus we can't continue anyway.
1312 */
1313 if (ret < 0)
1314 goto out;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001315
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001316 stripe = bbio->stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001317 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001318 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001319 struct request_queue *req_q;
1320
Filipe Manana627e0872018-01-30 18:40:22 +00001321 if (!stripe->dev->bdev) {
1322 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1323 continue;
1324 }
Anand Jain38b5f682017-11-29 18:53:43 +08001325 req_q = bdev_get_queue(stripe->dev->bdev);
1326 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001327 continue;
1328
Li Dongyang5378e602011-03-24 10:24:27 +00001329 ret = btrfs_issue_discard(stripe->dev->bdev,
1330 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001331 stripe->length,
1332 &bytes);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001333 if (!ret) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001334 discarded_bytes += bytes;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001335 } else if (ret != -EOPNOTSUPP) {
1336 /*
1337 * Logic errors or -ENOMEM, or -EIO, but
1338 * unlikely to happen.
1339 *
1340 * And since there are two loops, explicitly
1341 * go to out to avoid confusion.
1342 */
1343 btrfs_put_bbio(bbio);
1344 goto out;
1345 }
Josef Bacikd5e20032011-08-04 14:52:27 +00001346
1347 /*
1348 * Just in case we get back EOPNOTSUPP for some reason,
1349 * just ignore the return value so we don't screw up
1350 * people calling discard_extent.
1351 */
1352 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001353 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001354 btrfs_put_bbio(bbio);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001355 cur += num_bytes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001356 }
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001357out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001358 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001359
1360 if (actual_bytes)
1361 *actual_bytes = discarded_bytes;
1362
Liu Hui1f3c79a2009-01-05 15:57:51 -05001363
David Woodhouse53b381b2013-01-29 18:40:14 -05001364 if (ret == -EOPNOTSUPP)
1365 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001366 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001367}
1368
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001370int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001371 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001372{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001373 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001374 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001375 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001376
Qu Wenruo82fa1132019-04-04 14:45:35 +08001377 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1378 generic_ref->action);
1379 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1380 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001381
Qu Wenruo82fa1132019-04-04 14:45:35 +08001382 if (generic_ref->type == BTRFS_REF_METADATA)
1383 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001384 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001385 else
1386 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001387 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001388
Qu Wenruo82fa1132019-04-04 14:45:35 +08001389 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001390
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001391 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001392 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001393
Zheng Yan31840ae2008-09-23 13:14:14 -04001394 return ret;
1395}
1396
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001397/*
1398 * __btrfs_inc_extent_ref - insert backreference for a given extent
1399 *
1400 * @trans: Handle of transaction
1401 *
1402 * @node: The delayed ref node used to get the bytenr/length for
1403 * extent whose references are incremented.
1404 *
1405 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1406 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1407 * bytenr of the parent block. Since new extents are always
1408 * created with indirect references, this will only be the case
1409 * when relocating a shared extent. In that case, root_objectid
1410 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1411 * be 0
1412 *
1413 * @root_objectid: The id of the root where this modification has originated,
1414 * this can be either one of the well-known metadata trees or
1415 * the subvolume id which references this extent.
1416 *
1417 * @owner: For data extents it is the inode number of the owning file.
1418 * For metadata extents this parameter holds the level in the
1419 * tree of the extent.
1420 *
1421 * @offset: For metadata extents the offset is ignored and is currently
1422 * always passed as 0. For data extents it is the fileoffset
1423 * this extent belongs to.
1424 *
1425 * @refs_to_add Number of references to add
1426 *
1427 * @extent_op Pointer to a structure, holding information necessary when
1428 * updating a tree block's flags
1429 *
1430 */
Chris Mason925baed2008-06-25 16:01:30 -04001431static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001432 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001433 u64 parent, u64 root_objectid,
1434 u64 owner, u64 offset, int refs_to_add,
1435 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001436{
Chris Mason5caf2a02007-04-02 11:20:42 -04001437 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001438 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001439 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001440 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001441 u64 bytenr = node->bytenr;
1442 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001443 u64 refs;
1444 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001445
Chris Mason5caf2a02007-04-02 11:20:42 -04001446 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001447 if (!path)
1448 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001449
David Sterbae4058b52015-11-27 16:31:35 +01001450 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001451 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001452 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001453 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1454 parent, root_objectid, owner,
1455 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001456 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001458
1459 /*
1460 * Ok we had -EAGAIN which means we didn't have space to insert and
1461 * inline extent ref, so just update the reference count and add a
1462 * normal backref.
1463 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001464 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001465 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001466 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1467 refs = btrfs_extent_refs(leaf, item);
1468 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1469 if (extent_op)
1470 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001472 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001473 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001474
David Sterbae4058b52015-11-27 16:31:35 +01001475 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001476 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001477 /* now insert the actual backref */
Nikolay Borisov65cd6d92020-03-04 11:33:53 +02001478 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1479 BUG_ON(refs_to_add != 1);
1480 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1481 root_objectid);
1482 } else {
1483 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1484 root_objectid, owner, offset,
1485 refs_to_add);
1486 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001488 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001489out:
Chris Mason74493f72007-12-11 09:25:06 -05001490 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001491 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001492}
1493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001494static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001495 struct btrfs_delayed_ref_node *node,
1496 struct btrfs_delayed_extent_op *extent_op,
1497 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001498{
Chris Mason56bec292009-03-13 10:10:06 -04001499 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001500 struct btrfs_delayed_data_ref *ref;
1501 struct btrfs_key ins;
1502 u64 parent = 0;
1503 u64 ref_root = 0;
1504 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 ins.objectid = node->bytenr;
1507 ins.offset = node->num_bytes;
1508 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001510 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001511 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001512
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001513 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1514 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001515 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001516
1517 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001518 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001519 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001520 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1521 flags, ref->objectid,
1522 ref->offset, &ins,
1523 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001525 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1526 ref->objectid, ref->offset,
1527 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001529 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001530 ref_root, ref->objectid,
1531 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001532 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001533 } else {
1534 BUG();
1535 }
Chris Mason56bec292009-03-13 10:10:06 -04001536 return ret;
1537}
1538
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1540 struct extent_buffer *leaf,
1541 struct btrfs_extent_item *ei)
1542{
1543 u64 flags = btrfs_extent_flags(leaf, ei);
1544 if (extent_op->update_flags) {
1545 flags |= extent_op->flags_to_set;
1546 btrfs_set_extent_flags(leaf, ei, flags);
1547 }
1548
1549 if (extent_op->update_key) {
1550 struct btrfs_tree_block_info *bi;
1551 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1552 bi = (struct btrfs_tree_block_info *)(ei + 1);
1553 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1554 }
1555}
1556
1557static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001558 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001559 struct btrfs_delayed_extent_op *extent_op)
1560{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001561 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001562 struct btrfs_key key;
1563 struct btrfs_path *path;
1564 struct btrfs_extent_item *ei;
1565 struct extent_buffer *leaf;
1566 u32 item_size;
1567 int ret;
1568 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001569 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001570
David Sterbabf31f872020-02-05 17:34:34 +01001571 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001572 return 0;
1573
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001574 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001575 metadata = 0;
1576
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 path = btrfs_alloc_path();
1578 if (!path)
1579 return -ENOMEM;
1580
Josef Bacikd2788502017-09-29 15:43:57 -04001581 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001582
Josef Bacik3173a182013-03-07 14:22:04 -05001583 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001584 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001585 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001586 } else {
1587 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001588 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001589 }
1590
1591again:
David Sterbae4058b52015-11-27 16:31:35 +01001592 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001593 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001594 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001595 if (ret < 0) {
1596 err = ret;
1597 goto out;
1598 }
1599 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001600 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001601 if (path->slots[0] > 0) {
1602 path->slots[0]--;
1603 btrfs_item_key_to_cpu(path->nodes[0], &key,
1604 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001605 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001606 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001607 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001608 ret = 0;
1609 }
1610 if (ret > 0) {
1611 btrfs_release_path(path);
1612 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001613
Josef Bacikd2788502017-09-29 15:43:57 -04001614 key.objectid = head->bytenr;
1615 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001616 key.type = BTRFS_EXTENT_ITEM_KEY;
1617 goto again;
1618 }
1619 } else {
1620 err = -EIO;
1621 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001622 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001623 }
1624
1625 leaf = path->nodes[0];
1626 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001627
David Sterba6d8ff4e2018-06-26 16:20:59 +02001628 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001629 err = -EINVAL;
1630 btrfs_print_v0_err(fs_info);
1631 btrfs_abort_transaction(trans, err);
1632 goto out;
1633 }
1634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1636 __run_delayed_extent_op(extent_op, leaf, ei);
1637
1638 btrfs_mark_buffer_dirty(leaf);
1639out:
1640 btrfs_free_path(path);
1641 return err;
1642}
1643
1644static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001645 struct btrfs_delayed_ref_node *node,
1646 struct btrfs_delayed_extent_op *extent_op,
1647 int insert_reserved)
1648{
1649 int ret = 0;
1650 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001651 u64 parent = 0;
1652 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653
1654 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001655 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1658 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001659 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001660
Liu Bo02794222016-09-14 19:19:05 -07001661 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001662 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001663 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1664 node->bytenr, node->ref_mod, node->action, ref_root,
1665 parent);
1666 return -EIO;
1667 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001669 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001670 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001671 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001672 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1673 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001674 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001675 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001676 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001677 } else {
1678 BUG();
1679 }
1680 return ret;
1681}
1682
Chris Mason56bec292009-03-13 10:10:06 -04001683/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 struct btrfs_delayed_ref_node *node,
1686 struct btrfs_delayed_extent_op *extent_op,
1687 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001688{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001689 int ret = 0;
1690
David Sterbabf31f872020-02-05 17:34:34 +01001691 if (TRANS_ABORTED(trans)) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04001692 if (insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001693 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001694 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001695 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001696
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001697 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1698 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001699 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700 insert_reserved);
1701 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1702 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001703 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704 insert_reserved);
1705 else
1706 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001707 if (ret && insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001708 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001710}
1711
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001712static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001713select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001714{
Filipe Mananacffc3372015-07-09 13:13:44 +01001715 struct btrfs_delayed_ref_node *ref;
1716
Liu Boe3d03962018-08-23 03:51:50 +08001717 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001718 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001719
Filipe Mananacffc3372015-07-09 13:13:44 +01001720 /*
1721 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1722 * This is to prevent a ref count from going down to zero, which deletes
1723 * the extent item from the extent tree, when there still are references
1724 * to add, which would fail because they would not find the extent item.
1725 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001726 if (!list_empty(&head->ref_add_list))
1727 return list_first_entry(&head->ref_add_list,
1728 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001729
Liu Boe3d03962018-08-23 03:51:50 +08001730 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001731 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001732 ASSERT(list_empty(&ref->add_list));
1733 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001734}
1735
Josef Bacik2eadaa22017-09-29 15:43:52 -04001736static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1737 struct btrfs_delayed_ref_head *head)
1738{
1739 spin_lock(&delayed_refs->lock);
1740 head->processing = 0;
1741 delayed_refs->num_heads_ready++;
1742 spin_unlock(&delayed_refs->lock);
1743 btrfs_delayed_ref_unlock(head);
1744}
1745
Josef Bacikbedc66172018-12-03 10:20:31 -05001746static struct btrfs_delayed_extent_op *cleanup_extent_op(
1747 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001748{
1749 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001750
1751 if (!extent_op)
1752 return NULL;
1753
1754 if (head->must_insert_reserved) {
1755 head->extent_op = NULL;
1756 btrfs_free_delayed_extent_op(extent_op);
1757 return NULL;
1758 }
1759 return extent_op;
1760}
1761
1762static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1763 struct btrfs_delayed_ref_head *head)
1764{
1765 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001766 int ret;
1767
Josef Bacikbedc66172018-12-03 10:20:31 -05001768 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001769 if (!extent_op)
1770 return 0;
1771 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001772 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001773 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001774 btrfs_free_delayed_extent_op(extent_op);
1775 return ret ? ret : 1;
1776}
1777
Josef Bacik31890da2018-11-21 14:05:41 -05001778void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1779 struct btrfs_delayed_ref_root *delayed_refs,
1780 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001781{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001782 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001783
1784 if (head->total_ref_mod < 0) {
1785 struct btrfs_space_info *space_info;
1786 u64 flags;
1787
1788 if (head->is_data)
1789 flags = BTRFS_BLOCK_GROUP_DATA;
1790 else if (head->is_system)
1791 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1792 else
1793 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacik280c29082019-06-18 16:09:19 -04001794 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05001795 ASSERT(space_info);
1796 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1797 -head->num_bytes,
1798 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1799
Josef Bacikba2c4d42018-12-03 10:20:33 -05001800 /*
1801 * We had csum deletions accounted for in our delayed refs rsv,
1802 * we need to drop the csum leaves for this update from our
1803 * delayed_refs_rsv.
1804 */
Josef Bacik07c47772018-12-03 10:20:30 -05001805 if (head->is_data) {
1806 spin_lock(&delayed_refs->lock);
1807 delayed_refs->pending_csums -= head->num_bytes;
1808 spin_unlock(&delayed_refs->lock);
Josef Bacikba2c4d42018-12-03 10:20:33 -05001809 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1810 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001811 }
1812 }
1813
Josef Bacikba2c4d42018-12-03 10:20:33 -05001814 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001815}
1816
Josef Bacik194ab0b2017-09-29 15:43:54 -04001817static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001818 struct btrfs_delayed_ref_head *head)
1819{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001820
1821 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001822 struct btrfs_delayed_ref_root *delayed_refs;
1823 int ret;
1824
1825 delayed_refs = &trans->transaction->delayed_refs;
1826
Josef Bacikbedc66172018-12-03 10:20:31 -05001827 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001828 if (ret < 0) {
1829 unselect_delayed_ref_head(delayed_refs, head);
1830 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1831 return ret;
1832 } else if (ret) {
1833 return ret;
1834 }
1835
1836 /*
1837 * Need to drop our head ref lock and re-acquire the delayed ref lock
1838 * and then re-check to make sure nobody got added.
1839 */
1840 spin_unlock(&head->lock);
1841 spin_lock(&delayed_refs->lock);
1842 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001843 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001844 spin_unlock(&head->lock);
1845 spin_unlock(&delayed_refs->lock);
1846 return 1;
1847 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001848 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001849 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001850 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001851
Josef Bacikc1103f72017-09-29 15:43:56 -04001852 if (head->must_insert_reserved) {
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001853 btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001854 if (head->is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00001855 ret = btrfs_del_csums(trans, fs_info->csum_root,
1856 head->bytenr, head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001857 }
1858 }
1859
Josef Bacik31890da2018-11-21 14:05:41 -05001860 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001861
1862 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001863 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001864 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001865 return 0;
1866}
1867
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001868static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1869 struct btrfs_trans_handle *trans)
1870{
1871 struct btrfs_delayed_ref_root *delayed_refs =
1872 &trans->transaction->delayed_refs;
1873 struct btrfs_delayed_ref_head *head = NULL;
1874 int ret;
1875
1876 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001877 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001878 if (!head) {
1879 spin_unlock(&delayed_refs->lock);
1880 return head;
1881 }
1882
1883 /*
1884 * Grab the lock that says we are going to process all the refs for
1885 * this head
1886 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001887 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001888 spin_unlock(&delayed_refs->lock);
1889
1890 /*
1891 * We may have dropped the spin lock to get the head mutex lock, and
1892 * that might have given someone else time to free the head. If that's
1893 * true, it has been removed from our list and we can move on.
1894 */
1895 if (ret == -EAGAIN)
1896 head = ERR_PTR(-EAGAIN);
1897
1898 return head;
1899}
1900
Nikolay Borisove7261382018-08-15 10:39:55 +03001901static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1902 struct btrfs_delayed_ref_head *locked_ref,
1903 unsigned long *run_refs)
1904{
1905 struct btrfs_fs_info *fs_info = trans->fs_info;
1906 struct btrfs_delayed_ref_root *delayed_refs;
1907 struct btrfs_delayed_extent_op *extent_op;
1908 struct btrfs_delayed_ref_node *ref;
1909 int must_insert_reserved = 0;
1910 int ret;
1911
1912 delayed_refs = &trans->transaction->delayed_refs;
1913
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001914 lockdep_assert_held(&locked_ref->mutex);
1915 lockdep_assert_held(&locked_ref->lock);
1916
Nikolay Borisove7261382018-08-15 10:39:55 +03001917 while ((ref = select_delayed_ref(locked_ref))) {
1918 if (ref->seq &&
1919 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1920 spin_unlock(&locked_ref->lock);
1921 unselect_delayed_ref_head(delayed_refs, locked_ref);
1922 return -EAGAIN;
1923 }
1924
1925 (*run_refs)++;
1926 ref->in_tree = 0;
1927 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1928 RB_CLEAR_NODE(&ref->ref_node);
1929 if (!list_empty(&ref->add_list))
1930 list_del(&ref->add_list);
1931 /*
1932 * When we play the delayed ref, also correct the ref_mod on
1933 * head
1934 */
1935 switch (ref->action) {
1936 case BTRFS_ADD_DELAYED_REF:
1937 case BTRFS_ADD_DELAYED_EXTENT:
1938 locked_ref->ref_mod -= ref->ref_mod;
1939 break;
1940 case BTRFS_DROP_DELAYED_REF:
1941 locked_ref->ref_mod += ref->ref_mod;
1942 break;
1943 default:
1944 WARN_ON(1);
1945 }
1946 atomic_dec(&delayed_refs->num_entries);
1947
1948 /*
1949 * Record the must_insert_reserved flag before we drop the
1950 * spin lock.
1951 */
1952 must_insert_reserved = locked_ref->must_insert_reserved;
1953 locked_ref->must_insert_reserved = 0;
1954
1955 extent_op = locked_ref->extent_op;
1956 locked_ref->extent_op = NULL;
1957 spin_unlock(&locked_ref->lock);
1958
1959 ret = run_one_delayed_ref(trans, ref, extent_op,
1960 must_insert_reserved);
1961
1962 btrfs_free_delayed_extent_op(extent_op);
1963 if (ret) {
1964 unselect_delayed_ref_head(delayed_refs, locked_ref);
1965 btrfs_put_delayed_ref(ref);
1966 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1967 ret);
1968 return ret;
1969 }
1970
1971 btrfs_put_delayed_ref(ref);
1972 cond_resched();
1973
1974 spin_lock(&locked_ref->lock);
1975 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1976 }
1977
1978 return 0;
1979}
1980
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001981/*
1982 * Returns 0 on success or if called with an already aborted transaction.
1983 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
1984 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05001985static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05001986 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04001987{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02001988 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04001989 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001990 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05001991 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04001992 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001993 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05001994 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001995
1996 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001997 do {
Chris Mason56bec292009-03-13 10:10:06 -04001998 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001999 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002000 if (IS_ERR_OR_NULL(locked_ref)) {
2001 if (PTR_ERR(locked_ref) == -EAGAIN) {
2002 continue;
2003 } else {
2004 break;
2005 }
Chris Mason56bec292009-03-13 10:10:06 -04002006 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002007 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002008 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002009 /*
2010 * We need to try and merge add/drops of the same ref since we
2011 * can run into issues with relocate dropping the implicit ref
2012 * and then it being added back again before the drop can
2013 * finish. If we merged anything we need to re-loop so we can
2014 * get a good ref.
2015 * Or we can get node references of the same type that weren't
2016 * merged when created due to bumps in the tree mod seq, and
2017 * we need to merge them to prevent adding an inline extent
2018 * backref before dropping it (triggering a BUG_ON at
2019 * insert_inline_extent_backref()).
2020 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002021 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002022 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002023
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002024 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2025 &actual_count);
2026 if (ret < 0 && ret != -EAGAIN) {
2027 /*
2028 * Error, btrfs_run_delayed_refs_for_head already
2029 * unlocked everything so just bail out
2030 */
2031 return ret;
2032 } else if (!ret) {
2033 /*
2034 * Success, perform the usual cleanup of a processed
2035 * head
2036 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002037 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002038 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002039 /* We dropped our lock, we need to loop. */
2040 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002041 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002042 } else if (ret) {
2043 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002045 }
Chris Mason56bec292009-03-13 10:10:06 -04002046
Josef Bacikb00e6252017-09-29 15:43:53 -04002047 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002048 * Either success case or btrfs_run_delayed_refs_for_head
2049 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002050 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002051
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002052 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002053 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002054 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002055
2056 /*
2057 * We don't want to include ref heads since we can have empty ref heads
2058 * and those will drastically skew our runtime down since we just do
2059 * accounting, no actual extent tree updates.
2060 */
2061 if (actual_count > 0) {
2062 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2063 u64 avg;
2064
2065 /*
2066 * We weigh the current average higher than our current runtime
2067 * to avoid large swings in the average.
2068 */
2069 spin_lock(&delayed_refs->lock);
2070 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002071 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002072 spin_unlock(&delayed_refs->lock);
2073 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002074 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002075}
2076
Arne Jansen709c0482011-09-12 12:22:57 +02002077#ifdef SCRAMBLE_DELAYED_REFS
2078/*
2079 * Normally delayed refs get processed in ascending bytenr order. This
2080 * correlates in most cases to the order added. To expose dependencies on this
2081 * order, we start to process the tree in the middle instead of the beginning
2082 */
2083static u64 find_middle(struct rb_root *root)
2084{
2085 struct rb_node *n = root->rb_node;
2086 struct btrfs_delayed_ref_node *entry;
2087 int alt = 1;
2088 u64 middle;
2089 u64 first = 0, last = 0;
2090
2091 n = rb_first(root);
2092 if (n) {
2093 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2094 first = entry->bytenr;
2095 }
2096 n = rb_last(root);
2097 if (n) {
2098 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2099 last = entry->bytenr;
2100 }
2101 n = root->rb_node;
2102
2103 while (n) {
2104 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2105 WARN_ON(!entry->in_tree);
2106
2107 middle = entry->bytenr;
2108
2109 if (alt)
2110 n = n->rb_left;
2111 else
2112 n = n->rb_right;
2113
2114 alt = 1 - alt;
2115 }
2116 return middle;
2117}
2118#endif
2119
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002120static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002121{
2122 u64 num_bytes;
2123
2124 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2125 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002126 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002127 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2128
2129 /*
2130 * We don't ever fill up leaves all the way so multiply by 2 just to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04002131 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002132 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002133 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002134}
2135
Josef Bacik12621332015-02-03 07:50:16 -08002136/*
2137 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2138 * would require to store the csums for that many bytes.
2139 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002140u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002141{
2142 u64 csum_size;
2143 u64 num_csums_per_leaf;
2144 u64 num_csums;
2145
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002146 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002147 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002148 (u64)btrfs_super_csum_size(fs_info->super_copy));
2149 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002150 num_csums += num_csums_per_leaf - 1;
2151 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2152 return num_csums;
2153}
2154
Chris Masonc3e69d52009-03-13 10:17:05 -04002155/*
2156 * this starts processing the delayed reference count updates and
2157 * extent insertions we have queued up so far. count can be
2158 * 0, which means to process everything in the tree at the start
2159 * of the run (but not newly added entries), or it can be some target
2160 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002161 *
2162 * Returns 0 on success or if called with an aborted transaction
2163 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002164 */
2165int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002166 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002167{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002168 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002169 struct rb_node *node;
2170 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002171 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002172 int ret;
2173 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002174
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002175 /* We'll clean this up in btrfs_cleanup_transaction */
David Sterbabf31f872020-02-05 17:34:34 +01002176 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002177 return 0;
2178
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002179 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002180 return 0;
2181
Chris Masonc3e69d52009-03-13 10:17:05 -04002182 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002183 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002184 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002185
Chris Masonc3e69d52009-03-13 10:17:05 -04002186again:
Arne Jansen709c0482011-09-12 12:22:57 +02002187#ifdef SCRAMBLE_DELAYED_REFS
2188 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2189#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002190 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002191 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002192 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002193 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002194 }
2195
Chris Mason56bec292009-03-13 10:10:06 -04002196 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002197 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002198
Josef Bacikd7df2c72014-01-23 09:21:38 -05002199 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002200 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002201 if (!node) {
2202 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002203 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002204 }
Josef Bacikd2788502017-09-29 15:43:57 -04002205 head = rb_entry(node, struct btrfs_delayed_ref_head,
2206 href_node);
2207 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002208 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002209
2210 /* Mutex was contended, block until it's released and retry. */
2211 mutex_lock(&head->mutex);
2212 mutex_unlock(&head->mutex);
2213
2214 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002215 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002216 goto again;
2217 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002218out:
Chris Masona28ec192007-03-06 20:08:01 -05002219 return 0;
2220}
2221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
David Sterba42c9d0b2019-03-20 11:54:13 +01002223 struct extent_buffer *eb, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002224 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002225{
2226 struct btrfs_delayed_extent_op *extent_op;
2227 int ret;
2228
Miao Xie78a61842012-11-21 02:21:28 +00002229 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002230 if (!extent_op)
2231 return -ENOMEM;
2232
2233 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002234 extent_op->update_flags = true;
2235 extent_op->update_key = false;
2236 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002237 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238
David Sterba42c9d0b2019-03-20 11:54:13 +01002239 ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002241 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 return ret;
2243}
2244
Liu Boe4c3b2d2017-01-30 12:25:28 -08002245static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 struct btrfs_path *path,
2247 u64 objectid, u64 offset, u64 bytenr)
2248{
2249 struct btrfs_delayed_ref_head *head;
2250 struct btrfs_delayed_ref_node *ref;
2251 struct btrfs_delayed_data_ref *data_ref;
2252 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002253 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002254 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255 int ret = 0;
2256
ethanwu998ac6d2018-04-29 15:59:42 +08002257 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002258 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002259 if (cur_trans)
2260 refcount_inc(&cur_trans->use_count);
2261 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002262 if (!cur_trans)
2263 return 0;
2264
2265 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002266 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002267 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002268 if (!head) {
2269 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002270 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002271 return 0;
2272 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273
2274 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002275 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276 spin_unlock(&delayed_refs->lock);
2277
David Sterbab3b4aa72011-04-21 01:20:15 +02002278 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279
David Sterba8cc33e52011-05-02 15:29:25 +02002280 /*
2281 * Mutex was contended, block until it's released and let
2282 * caller try again
2283 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002284 mutex_lock(&head->mutex);
2285 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002286 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002287 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288 return -EAGAIN;
2289 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002291
2292 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002293 /*
2294 * XXX: We should replace this with a proper search function in the
2295 * future.
2296 */
Liu Boe3d03962018-08-23 03:51:50 +08002297 for (node = rb_first_cached(&head->ref_tree); node;
2298 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002299 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002300 /* If it's a shared ref we know a cross reference exists */
2301 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2302 ret = 1;
2303 break;
2304 }
2305
2306 data_ref = btrfs_delayed_node_to_data_ref(ref);
2307
2308 /*
2309 * If our ref doesn't match the one we're currently looking at
2310 * then we have a cross reference.
2311 */
2312 if (data_ref->root != root->root_key.objectid ||
2313 data_ref->objectid != objectid ||
2314 data_ref->offset != offset) {
2315 ret = 1;
2316 break;
2317 }
2318 }
2319 spin_unlock(&head->lock);
2320 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002321 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002322 return ret;
2323}
2324
Liu Boe4c3b2d2017-01-30 12:25:28 -08002325static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326 struct btrfs_path *path,
2327 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002329 struct btrfs_fs_info *fs_info = root->fs_info;
2330 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002331 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332 struct btrfs_extent_data_ref *ref;
2333 struct btrfs_extent_inline_ref *iref;
2334 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002335 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002336 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002337 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002338 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002339
Chris Masonbe20aa92007-12-17 20:14:01 -05002340 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002341 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002342 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002343
Chris Masonbe20aa92007-12-17 20:14:01 -05002344 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2345 if (ret < 0)
2346 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002347 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002348
2349 ret = -ENOENT;
2350 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002351 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002352
Zheng Yan31840ae2008-09-23 13:14:14 -04002353 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002354 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002355 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002356
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002357 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002358 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002359
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002360 ret = 1;
2361 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002362 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2363
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002364 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 if (item_size != sizeof(*ei) +
2366 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2367 goto out;
2368
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002369 /* If extent created before last snapshot => it's definitely shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370 if (btrfs_extent_generation(leaf, ei) <=
2371 btrfs_root_last_snapshot(&root->root_item))
2372 goto out;
2373
2374 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002375
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002376 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002377 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2378 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002379 goto out;
2380
2381 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2382 if (btrfs_extent_refs(leaf, ei) !=
2383 btrfs_extent_data_ref_count(leaf, ref) ||
2384 btrfs_extent_data_ref_root(leaf, ref) !=
2385 root->root_key.objectid ||
2386 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2387 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2388 goto out;
2389
Yan Zhengf321e492008-07-30 09:26:11 -04002390 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002391out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002392 return ret;
2393}
2394
Liu Boe4c3b2d2017-01-30 12:25:28 -08002395int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2396 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002397{
2398 struct btrfs_path *path;
2399 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002400
2401 path = btrfs_alloc_path();
2402 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002403 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404
2405 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002406 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407 offset, bytenr);
2408 if (ret && ret != -ENOENT)
2409 goto out;
2410
Misono Tomohiro380fd062018-08-30 10:59:16 +09002411 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2412 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002413
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002414out:
Yan Zhengf321e492008-07-30 09:26:11 -04002415 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002416 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2417 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002418 return ret;
2419}
2420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002422 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002423 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002424 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002426 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002427 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428 u64 num_bytes;
2429 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002430 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002431 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 struct btrfs_key key;
2433 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002434 struct btrfs_ref generic_ref = { 0 };
2435 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002436 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002437 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002438 int level;
2439 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002440
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002442 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002443
Zheng Yan31840ae2008-09-23 13:14:14 -04002444 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 nritems = btrfs_header_nritems(buf);
2446 level = btrfs_header_level(buf);
2447
Miao Xie27cdeb72014-04-02 19:51:05 +08002448 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002450
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002451 if (full_backref)
2452 parent = buf->start;
2453 else
2454 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002455 if (inc)
2456 action = BTRFS_ADD_DELAYED_REF;
2457 else
2458 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002461 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002462 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002463 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002464 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002465 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002466 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002467 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002468 BTRFS_FILE_EXTENT_INLINE)
2469 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002470 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2471 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002472 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2475 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002476 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2477 num_bytes, parent);
2478 generic_ref.real_root = root->root_key.objectid;
2479 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2480 key.offset);
2481 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002482 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002483 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002484 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002485 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 if (ret)
2487 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002488 } else {
2489 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002491 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2492 num_bytes, parent);
2493 generic_ref.real_root = root->root_key.objectid;
2494 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2495 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002496 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002497 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002498 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002499 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002500 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002501 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002502 }
2503 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002504 return 0;
2505fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002506 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002507}
2508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002510 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002511{
Josef Bacike339a6b2014-07-02 10:54:25 -07002512 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513}
Zheng Yan31840ae2008-09-23 13:14:14 -04002514
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002515int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002516 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002517{
Josef Bacike339a6b2014-07-02 10:54:25 -07002518 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002519}
2520
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002521int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002522{
David Sterba32da53862019-10-29 19:20:18 +01002523 struct btrfs_block_group *block_group;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002524 int readonly = 0;
2525
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002526 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002527 if (!block_group || block_group->ro)
2528 readonly = 1;
2529 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002530 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002531 return readonly;
2532}
2533
Jeff Mahoney1b868262017-05-17 11:38:35 -04002534static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002535{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002536 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002537 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002538 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002539
Yan, Zhengb742bb822010-05-16 10:46:24 -04002540 if (data)
2541 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002542 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002543 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2544 else
2545 flags = BTRFS_BLOCK_GROUP_METADATA;
2546
Josef Bacik878d7b62019-06-20 15:38:05 -04002547 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002548 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002549}
2550
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002551static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002552{
David Sterba32da53862019-10-29 19:20:18 +01002553 struct btrfs_block_group *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002554 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002555
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002556 spin_lock(&fs_info->block_group_cache_lock);
2557 bytenr = fs_info->first_logical_byte;
2558 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002559
2560 if (bytenr < (u64)-1)
2561 return bytenr;
2562
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002563 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002564 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002565 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002566
David Sterbab3470b52019-10-23 18:48:22 +02002567 bytenr = cache->start;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002568 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002569
2570 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002571}
2572
Nikolay Borisov6690d072020-01-20 16:09:14 +02002573static int pin_down_extent(struct btrfs_trans_handle *trans,
2574 struct btrfs_block_group *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002575 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002576{
David Sterbafdf08602019-03-20 12:12:32 +01002577 struct btrfs_fs_info *fs_info = cache->fs_info;
2578
Yan Zheng11833d62009-09-11 16:11:19 -04002579 spin_lock(&cache->space_info->lock);
2580 spin_lock(&cache->lock);
2581 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002582 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2583 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002584 if (reserved) {
2585 cache->reserved -= num_bytes;
2586 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002587 }
Yan Zheng11833d62009-09-11 16:11:19 -04002588 spin_unlock(&cache->lock);
2589 spin_unlock(&cache->space_info->lock);
2590
Ethan Liendec59fa2018-07-13 16:50:42 +08002591 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2592 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002593 set_extent_dirty(&trans->transaction->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002594 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002595 return 0;
2596}
Chris Mason9078a3e2007-04-26 16:46:15 -04002597
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002598int btrfs_pin_extent(struct btrfs_trans_handle *trans,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002599 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002600{
David Sterba32da53862019-10-29 19:20:18 +01002601 struct btrfs_block_group *cache;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002602
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002603 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002604 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002605
Nikolay Borisov6690d072020-01-20 16:09:14 +02002606 pin_down_extent(trans, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002607
2608 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002609 return 0;
2610}
Zheng Yane8569812008-09-26 10:05:48 -04002611
Yan, Zhengf0486c62010-05-16 10:46:25 -04002612/*
Chris Masone688b7252011-10-31 20:52:39 -04002613 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002614 */
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002615int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
Chris Masone688b7252011-10-31 20:52:39 -04002616 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002617{
David Sterba32da53862019-10-29 19:20:18 +01002618 struct btrfs_block_group *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002619 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002620
Nikolay Borisovf2fb7292020-01-20 16:09:16 +02002621 btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes);
2622
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002623 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002624 if (!cache)
2625 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002626
2627 /*
2628 * pull in the free space cache (if any) so that our pin
2629 * removes the free space from the cache. We have load_only set
2630 * to one because the slow code to read in the free extents does check
2631 * the pinned extents.
2632 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002633 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002634
Nikolay Borisov6690d072020-01-20 16:09:14 +02002635 pin_down_extent(trans, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002636
2637 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002638 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002639 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002640 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002641}
2642
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002643static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2644 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002645{
2646 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002647 struct btrfs_block_group *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002648 struct btrfs_caching_control *caching_ctl;
2649
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002651 if (!block_group)
2652 return -EINVAL;
2653
Josef Bacik676f1f72019-06-20 15:37:48 -04002654 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002655 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002656
2657 if (!caching_ctl) {
2658 /* Logic error */
David Sterba32da53862019-10-29 19:20:18 +01002659 BUG_ON(!btrfs_block_group_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002660 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2661 } else {
2662 mutex_lock(&caching_ctl->mutex);
2663
2664 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002665 ret = btrfs_add_excluded_extent(fs_info, start,
2666 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002667 } else if (start + num_bytes <= caching_ctl->progress) {
2668 ret = btrfs_remove_free_space(block_group,
2669 start, num_bytes);
2670 } else {
2671 num_bytes = caching_ctl->progress - start;
2672 ret = btrfs_remove_free_space(block_group,
2673 start, num_bytes);
2674 if (ret)
2675 goto out_lock;
2676
2677 num_bytes = (start + num_bytes) -
2678 caching_ctl->progress;
2679 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002680 ret = btrfs_add_excluded_extent(fs_info, start,
2681 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002682 }
2683out_lock:
2684 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002685 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002686 }
2687 btrfs_put_block_group(block_group);
2688 return ret;
2689}
2690
David Sterbabcdc4282019-03-20 12:14:33 +01002691int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002692{
David Sterbabcdc4282019-03-20 12:14:33 +01002693 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002694 struct btrfs_file_extent_item *item;
2695 struct btrfs_key key;
2696 int found_type;
2697 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002698 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002699
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002700 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002701 return 0;
2702
2703 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2704 btrfs_item_key_to_cpu(eb, &key, i);
2705 if (key.type != BTRFS_EXTENT_DATA_KEY)
2706 continue;
2707 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2708 found_type = btrfs_file_extent_type(eb, item);
2709 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2710 continue;
2711 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2712 continue;
2713 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2714 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002715 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2716 if (ret)
2717 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002718 }
2719
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002720 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002721}
2722
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002723static void
David Sterba32da53862019-10-29 19:20:18 +01002724btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002725{
2726 atomic_inc(&bg->reservations);
2727}
2728
David Sterba8b74c032017-02-10 19:20:56 +01002729void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04002730{
Yan Zheng11833d62009-09-11 16:11:19 -04002731 struct btrfs_caching_control *next;
2732 struct btrfs_caching_control *caching_ctl;
David Sterba32da53862019-10-29 19:20:18 +01002733 struct btrfs_block_group *cache;
Yan Zheng11833d62009-09-11 16:11:19 -04002734
Josef Bacik9e351cc2014-03-13 15:42:13 -04002735 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04002736
2737 list_for_each_entry_safe(caching_ctl, next,
2738 &fs_info->caching_block_groups, list) {
2739 cache = caching_ctl->block_group;
David Sterba32da53862019-10-29 19:20:18 +01002740 if (btrfs_block_group_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04002741 cache->last_byte_to_unpin = (u64)-1;
2742 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04002743 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04002744 } else {
2745 cache->last_byte_to_unpin = caching_ctl->progress;
2746 }
2747 }
2748
Josef Bacik9e351cc2014-03-13 15:42:13 -04002749 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002750
Josef Bacik67f9c222019-06-19 13:47:23 -04002751 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04002752}
2753
Josef Bacikc759c4e2015-10-02 15:25:10 -04002754/*
2755 * Returns the free cluster for the given space info and sets empty_cluster to
2756 * what it should be based on the mount options.
2757 */
2758static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002759fetch_cluster_info(struct btrfs_fs_info *fs_info,
2760 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002761{
2762 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002763
2764 *empty_cluster = 0;
2765 if (btrfs_mixed_space_info(space_info))
2766 return ret;
2767
Josef Bacikc759c4e2015-10-02 15:25:10 -04002768 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002769 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002770 if (btrfs_test_opt(fs_info, SSD))
2771 *empty_cluster = SZ_2M;
2772 else
Byongho Leeee221842015-12-15 01:42:10 +09002773 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002774 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2775 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2776 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002777 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002778 }
2779
2780 return ret;
2781}
2782
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002783static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2784 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002785 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002786{
David Sterba32da53862019-10-29 19:20:18 +01002787 struct btrfs_block_group *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002788 struct btrfs_space_info *space_info;
2789 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002790 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002791 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002792 u64 total_unpinned = 0;
2793 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002794 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002795
2796 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002797 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002798 if (!cache ||
David Sterbab3470b52019-10-23 18:48:22 +02002799 start >= cache->start + cache->length) {
Yan Zheng11833d62009-09-11 16:11:19 -04002800 if (cache)
2801 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002802 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002803 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002804 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002805
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002806 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002807 cache->space_info,
2808 &empty_cluster);
2809 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002810 }
2811
David Sterbab3470b52019-10-23 18:48:22 +02002812 len = cache->start + cache->length - start;
Yan Zheng11833d62009-09-11 16:11:19 -04002813 len = min(len, end + 1 - start);
2814
2815 if (start < cache->last_byte_to_unpin) {
2816 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00002817 if (return_free_space)
2818 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04002819 }
Josef Bacik25179202008-10-29 14:49:05 -04002820
Yan, Zhengf0486c62010-05-16 10:46:25 -04002821 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002822 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002823 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002824
Josef Bacikc759c4e2015-10-02 15:25:10 -04002825 /*
2826 * If this space cluster has been marked as fragmented and we've
2827 * unpinned enough in this block group to potentially allow a
2828 * cluster to be created inside of it go ahead and clear the
2829 * fragmented check.
2830 */
2831 if (cluster && cluster->fragmented &&
2832 total_unpinned > empty_cluster) {
2833 spin_lock(&cluster->lock);
2834 cluster->fragmented = 0;
2835 spin_unlock(&cluster->lock);
2836 }
2837
Josef Bacik7b398f82012-10-22 15:52:28 -04002838 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002839 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002840 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002841 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacik4f4db212015-09-29 11:40:47 -04002842 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08002843 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2844 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04002845 if (cache->ro) {
2846 space_info->bytes_readonly += len;
2847 readonly = true;
2848 }
Josef Bacik25179202008-10-29 14:49:05 -04002849 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002850 if (!readonly && return_free_space &&
2851 global_rsv->space_info == space_info) {
2852 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002853
Josef Bacik7b398f82012-10-22 15:52:28 -04002854 spin_lock(&global_rsv->lock);
2855 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002856 to_add = min(len, global_rsv->size -
2857 global_rsv->reserved);
2858 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002859 btrfs_space_info_update_bytes_may_use(fs_info,
2860 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002861 if (global_rsv->reserved >= global_rsv->size)
2862 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002863 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002864 }
2865 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002866 /* Add to any tickets we may have */
2867 if (len)
Josef Bacik18fa2282019-08-22 15:10:58 -04002868 btrfs_try_granting_tickets(fs_info,
2869 space_info);
Josef Bacik7b398f82012-10-22 15:52:28 -04002870 }
2871 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002872 }
2873
2874 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002875 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002876 return 0;
2877}
2878
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002879int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002880{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002881 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002882 struct btrfs_block_group *block_group, *tmp;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002883 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002884 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002885 u64 start;
2886 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002887 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002888
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002889 unpin = &trans->transaction->pinned_extents;
Yan Zheng11833d62009-09-11 16:11:19 -04002890
David Sterbabf31f872020-02-05 17:34:34 +01002891 while (!TRANS_ABORTED(trans)) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002892 struct extent_state *cached_state = NULL;
2893
Filipe Mananad4b450c2015-01-29 19:18:25 +00002894 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002895 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002896 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002897 if (ret) {
2898 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002899 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002900 }
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002901 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
2902 clear_extent_bits(&fs_info->excluded_extents, start,
Nikolay Borisovf2fb7292020-01-20 16:09:16 +02002903 end, EXTENT_UPTODATE);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002904
Dennis Zhou46b27f52019-12-13 16:22:11 -08002905 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002906 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002907 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002908
Filipe Manana0e6ec382018-11-16 13:04:44 +00002909 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002910 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002911 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002912 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002913 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002914 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002915
Dennis Zhoua2309302020-01-02 16:26:35 -05002916 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
2917 btrfs_discard_calc_delay(&fs_info->discard_ctl);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002918 btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -05002919 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002920
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002921 /*
2922 * Transaction is finished. We don't need the lock anymore. We
2923 * do need to clean up the block groups in case of a transaction
2924 * abort.
2925 */
2926 deleted_bgs = &trans->transaction->deleted_bgs;
2927 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2928 u64 trimmed = 0;
2929
2930 ret = -EROFS;
David Sterbabf31f872020-02-05 17:34:34 +01002931 if (!TRANS_ABORTED(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002932 ret = btrfs_discard_extent(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02002933 block_group->start,
2934 block_group->length,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002935 &trimmed);
2936
2937 list_del_init(&block_group->bg_list);
2938 btrfs_put_block_group_trimming(block_group);
2939 btrfs_put_block_group(block_group);
2940
2941 if (ret) {
2942 const char *errstr = btrfs_decode_error(ret);
2943 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002944 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002945 ret, errstr);
2946 }
2947 }
2948
Chris Masone20d96d2007-03-22 12:13:20 -04002949 return 0;
2950}
2951
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002952static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002953 struct btrfs_delayed_ref_node *node, u64 parent,
2954 u64 root_objectid, u64 owner_objectid,
2955 u64 owner_offset, int refs_to_drop,
2956 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002957{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002958 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002959 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002960 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002961 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002962 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963 struct btrfs_extent_item *ei;
2964 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002965 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002967 int extent_slot = 0;
2968 int found_extent = 0;
2969 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970 u32 item_size;
2971 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002972 u64 bytenr = node->bytenr;
2973 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002974 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002975 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002976
Chris Mason5caf2a02007-04-02 11:20:42 -04002977 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002978 if (!path)
2979 return -ENOMEM;
2980
David Sterbae4058b52015-11-27 16:31:35 +01002981 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002982 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983
2984 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2985 BUG_ON(!is_data && refs_to_drop != 1);
2986
Josef Bacik3173a182013-03-07 14:22:04 -05002987 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02002988 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05002989
Nikolay Borisovfbe48012018-06-20 15:48:52 +03002990 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
2991 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002992 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05002993 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002994 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002995 while (extent_slot >= 0) {
2996 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05002997 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002998 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05002999 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3001 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003002 found_extent = 1;
3003 break;
3004 }
Josef Bacik3173a182013-03-07 14:22:04 -05003005 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3006 key.offset == owner_objectid) {
3007 found_extent = 1;
3008 break;
3009 }
Chris Mason952fcca2008-02-18 16:33:44 -05003010 if (path->slots[0] - extent_slot > 5)
3011 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003013 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003014
Zheng Yan31840ae2008-09-23 13:14:14 -04003015 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003016 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003017 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003018 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003019 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003020 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003021 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003022 goto out;
3023 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003024 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003025 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026
3027 key.objectid = bytenr;
3028 key.type = BTRFS_EXTENT_ITEM_KEY;
3029 key.offset = num_bytes;
3030
Josef Bacik3173a182013-03-07 14:22:04 -05003031 if (!is_data && skinny_metadata) {
3032 key.type = BTRFS_METADATA_ITEM_KEY;
3033 key.offset = owner_objectid;
3034 }
3035
Zheng Yan31840ae2008-09-23 13:14:14 -04003036 ret = btrfs_search_slot(trans, extent_root,
3037 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003038 if (ret > 0 && skinny_metadata && path->slots[0]) {
3039 /*
3040 * Couldn't find our skinny metadata item,
3041 * see if we have ye olde extent item.
3042 */
3043 path->slots[0]--;
3044 btrfs_item_key_to_cpu(path->nodes[0], &key,
3045 path->slots[0]);
3046 if (key.objectid == bytenr &&
3047 key.type == BTRFS_EXTENT_ITEM_KEY &&
3048 key.offset == num_bytes)
3049 ret = 0;
3050 }
3051
3052 if (ret > 0 && skinny_metadata) {
3053 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003054 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003055 key.type = BTRFS_EXTENT_ITEM_KEY;
3056 key.offset = num_bytes;
3057 btrfs_release_path(path);
3058 ret = btrfs_search_slot(trans, extent_root,
3059 &key, path, -1, 1);
3060 }
3061
Josef Bacikf3465ca2008-11-12 14:19:50 -05003062 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003063 btrfs_err(info,
3064 "umm, got %d back from search, was looking for %llu",
3065 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003066 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003067 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003068 }
David Sterba005d6422012-09-18 07:52:32 -06003069 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003070 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003071 goto out;
3072 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003073 extent_slot = path->slots[0];
3074 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303075 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003076 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003077 btrfs_err(info,
3078 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003079 bytenr, parent, root_objectid, owner_objectid,
3080 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003081 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003082 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003084 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003085 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003086 }
Chris Mason5f39d392007-10-15 16:14:19 -04003087
3088 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003089 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003090 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003091 ret = -EINVAL;
3092 btrfs_print_v0_err(info);
3093 btrfs_abort_transaction(trans, ret);
3094 goto out;
3095 }
Chris Mason952fcca2008-02-18 16:33:44 -05003096 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003097 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003098 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3099 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003100 struct btrfs_tree_block_info *bi;
3101 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3102 bi = (struct btrfs_tree_block_info *)(ei + 1);
3103 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003104 }
3105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003106 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003107 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003108 btrfs_err(info,
3109 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
3110 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04003111 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04003112 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04003113 goto out;
3114 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 refs -= refs_to_drop;
3116
3117 if (refs > 0) {
3118 if (extent_op)
3119 __run_delayed_extent_op(extent_op, leaf, ei);
3120 /*
3121 * In the case of inline back ref, reference count will
3122 * be updated by remove_extent_backref
3123 */
3124 if (iref) {
3125 BUG_ON(!found_extent);
3126 } else {
3127 btrfs_set_extent_refs(leaf, ei, refs);
3128 btrfs_mark_buffer_dirty(leaf);
3129 }
3130 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003131 ret = remove_extent_backref(trans, path, iref,
3132 refs_to_drop, is_data,
3133 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003134 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003135 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003136 goto out;
3137 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003138 }
3139 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003140 if (found_extent) {
3141 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08003142 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003143 if (iref) {
3144 BUG_ON(path->slots[0] != extent_slot);
3145 } else {
3146 BUG_ON(path->slots[0] != extent_slot + 1);
3147 path->slots[0] = extent_slot;
3148 num_to_del = 2;
3149 }
Chris Mason78fae272007-03-25 11:35:08 -04003150 }
Chris Masonb9473432009-03-13 11:00:37 -04003151
Josef Bacikfcebe452014-05-13 17:30:47 -07003152 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003153 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3154 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003155 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003156 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003157 goto out;
3158 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003159 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003160
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003161 if (is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00003162 ret = btrfs_del_csums(trans, info->csum_root, bytenr,
3163 num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003164 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003165 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003166 goto out;
3167 }
Chris Mason459931e2008-12-10 09:10:46 -05003168 }
3169
Nikolay Borisove7355e52018-05-10 15:44:55 +03003170 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003171 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003172 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003173 goto out;
3174 }
3175
Josef Bacikade4b512019-06-20 15:38:01 -04003176 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003177 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003178 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003179 goto out;
3180 }
Chris Masona28ec192007-03-06 20:08:01 -05003181 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003182 btrfs_release_path(path);
3183
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003185 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003186 return ret;
3187}
3188
3189/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003190 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003191 * delayed ref for that extent as well. This searches the delayed ref tree for
3192 * a given extent, and if there are no other delayed refs to be processed, it
3193 * removes it from the tree.
3194 */
3195static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003196 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003197{
3198 struct btrfs_delayed_ref_head *head;
3199 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003200 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003201
3202 delayed_refs = &trans->transaction->delayed_refs;
3203 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003204 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003205 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003206 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003207
Josef Bacikd7df2c72014-01-23 09:21:38 -05003208 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003209 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003210 goto out;
3211
Josef Bacikbedc66172018-12-03 10:20:31 -05003212 if (cleanup_extent_op(head) != NULL)
3213 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003214
Chris Mason1887be62009-03-13 10:11:24 -04003215 /*
3216 * waiting for the lock here would deadlock. If someone else has it
3217 * locked they are already in the process of dropping it anyway
3218 */
3219 if (!mutex_trylock(&head->mutex))
3220 goto out;
3221
Josef Bacikd7baffd2018-12-03 10:20:29 -05003222 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003223 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003224
Josef Bacikd7df2c72014-01-23 09:21:38 -05003225 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003226 spin_unlock(&delayed_refs->lock);
3227
Yan, Zhengf0486c62010-05-16 10:46:25 -04003228 BUG_ON(head->extent_op);
3229 if (head->must_insert_reserved)
3230 ret = 1;
3231
Josef Bacik31890da2018-11-21 14:05:41 -05003232 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003233 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003234 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003235 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003236out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003237 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003238
3239out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003240 spin_unlock(&delayed_refs->lock);
3241 return 0;
3242}
3243
Yan, Zhengf0486c62010-05-16 10:46:25 -04003244void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3245 struct btrfs_root *root,
3246 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003247 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003248{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003249 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003250 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04003251 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003252 int ret;
3253
Qu Wenruoed4f2552019-04-04 14:45:31 +08003254 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3255 buf->start, buf->len, parent);
3256 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3257 root->root_key.objectid);
3258
Yan, Zhengf0486c62010-05-16 10:46:25 -04003259 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07003260 int old_ref_mod, new_ref_mod;
3261
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003262 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08003263 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003264 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003265 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07003266 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003267 }
3268
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003269 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
David Sterba32da53862019-10-29 19:20:18 +01003270 struct btrfs_block_group *cache;
Filipe Manana62198722015-01-06 20:18:45 +00003271
Yan, Zhengf0486c62010-05-16 10:46:25 -04003272 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003273 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003274 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003275 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003276 }
3277
Omar Sandoval4da8b762017-06-06 16:45:28 -07003278 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003279 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003280
Yan, Zhengf0486c62010-05-16 10:46:25 -04003281 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Nikolay Borisov6690d072020-01-20 16:09:14 +02003282 pin_down_extent(trans, cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003283 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003284 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003285 }
3286
3287 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3288
3289 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003290 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003291 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003292 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003293 }
3294out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04003295 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08003296 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003297
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003298 if (last_ref) {
3299 /*
3300 * Deleting the buffer, clear the corrupt flag since it doesn't
3301 * matter anymore.
3302 */
3303 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3304 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003305}
3306
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003307/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003308int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003309{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003310 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07003311 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04003312 int ret;
3313
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003314 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003315 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003316
Chris Mason56bec292009-03-13 10:10:06 -04003317 /*
3318 * tree log blocks never actually go into the extent allocation
3319 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003320 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003321 if ((ref->type == BTRFS_REF_METADATA &&
3322 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3323 (ref->type == BTRFS_REF_DATA &&
3324 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003325 /* unlocks the pinned mutex */
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02003326 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07003327 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04003328 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003329 } else if (ref->type == BTRFS_REF_METADATA) {
3330 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003331 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003332 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003333 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07003334 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04003335 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003336
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003337 if (!((ref->type == BTRFS_REF_METADATA &&
3338 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3339 (ref->type == BTRFS_REF_DATA &&
3340 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3341 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003342
Qu Wenruoddf30cf2019-04-04 14:45:34 +08003343 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08003344 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07003345
Chris Mason925baed2008-06-25 16:01:30 -04003346 return ret;
3347}
3348
Josef Bacik817d52f2009-07-13 21:29:25 -04003349enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003350 LOOP_CACHING_NOWAIT,
3351 LOOP_CACHING_WAIT,
3352 LOOP_ALLOC_CHUNK,
3353 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003354};
3355
Miao Xiee570fd22014-06-19 10:42:50 +08003356static inline void
David Sterba32da53862019-10-29 19:20:18 +01003357btrfs_lock_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003358 int delalloc)
3359{
3360 if (delalloc)
3361 down_read(&cache->data_rwsem);
3362}
3363
David Sterba32da53862019-10-29 19:20:18 +01003364static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003365 int delalloc)
3366{
3367 btrfs_get_block_group(cache);
3368 if (delalloc)
3369 down_read(&cache->data_rwsem);
3370}
3371
David Sterba32da53862019-10-29 19:20:18 +01003372static struct btrfs_block_group *btrfs_lock_cluster(
3373 struct btrfs_block_group *block_group,
Miao Xiee570fd22014-06-19 10:42:50 +08003374 struct btrfs_free_cluster *cluster,
3375 int delalloc)
3376{
David Sterba32da53862019-10-29 19:20:18 +01003377 struct btrfs_block_group *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003378
Miao Xiee570fd22014-06-19 10:42:50 +08003379 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003380 while (1) {
3381 used_bg = cluster->block_group;
3382 if (!used_bg)
3383 return NULL;
3384
3385 if (used_bg == block_group)
3386 return used_bg;
3387
3388 btrfs_get_block_group(used_bg);
3389
3390 if (!delalloc)
3391 return used_bg;
3392
3393 if (down_read_trylock(&used_bg->data_rwsem))
3394 return used_bg;
3395
3396 spin_unlock(&cluster->refill_lock);
3397
Liu Boe321f8a2016-11-30 16:11:04 -08003398 /* We should only have one-level nested. */
3399 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003400
3401 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003402 if (used_bg == cluster->block_group)
3403 return used_bg;
3404
3405 up_read(&used_bg->data_rwsem);
3406 btrfs_put_block_group(used_bg);
3407 }
Miao Xiee570fd22014-06-19 10:42:50 +08003408}
3409
3410static inline void
David Sterba32da53862019-10-29 19:20:18 +01003411btrfs_release_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003412 int delalloc)
3413{
3414 if (delalloc)
3415 up_read(&cache->data_rwsem);
3416 btrfs_put_block_group(cache);
3417}
3418
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003419enum btrfs_extent_allocation_policy {
3420 BTRFS_EXTENT_ALLOC_CLUSTERED,
3421};
3422
Josef Bacik817d52f2009-07-13 21:29:25 -04003423/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08003424 * Structure used internally for find_free_extent() function. Wraps needed
3425 * parameters.
3426 */
3427struct find_free_extent_ctl {
3428 /* Basic allocation info */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003429 u64 num_bytes;
3430 u64 empty_size;
3431 u64 flags;
3432 int delalloc;
3433
3434 /* Where to start the search inside the bg */
3435 u64 search_start;
3436
3437 /* For clustered allocation */
3438 u64 empty_cluster;
Naohiro Aotac10859b2020-02-25 12:56:18 +09003439 struct btrfs_free_cluster *last_ptr;
3440 bool use_cluster;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003441
3442 bool have_caching_bg;
3443 bool orig_have_caching_bg;
3444
3445 /* RAID index, converted from flags */
3446 int index;
3447
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003448 /*
3449 * Current loop number, check find_free_extent_update_loop() for details
3450 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003451 int loop;
3452
3453 /*
3454 * Whether we're refilling a cluster, if true we need to re-search
3455 * current block group but don't try to refill the cluster again.
3456 */
3457 bool retry_clustered;
3458
3459 /*
3460 * Whether we're updating free space cache, if true we need to re-search
3461 * current block group but don't try updating free space cache again.
3462 */
3463 bool retry_unclustered;
3464
3465 /* If current block group is cached */
3466 int cached;
3467
3468 /* Max contiguous hole found */
3469 u64 max_extent_size;
3470
3471 /* Total free space from free space cache, not always contiguous */
3472 u64 total_free_space;
3473
3474 /* Found result */
3475 u64 found_offset;
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003476
Naohiro Aotaea544142020-02-25 12:56:17 +09003477 /* Hint where to start looking for an empty space */
3478 u64 hint_byte;
3479
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003480 /* Allocation policy */
3481 enum btrfs_extent_allocation_policy policy;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003482};
3483
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003484
3485/*
3486 * Helper function for find_free_extent().
3487 *
3488 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3489 * Return -EAGAIN to inform caller that we need to re-search this block group
3490 * Return >0 to inform caller that we find nothing
3491 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3492 */
David Sterba32da53862019-10-29 19:20:18 +01003493static int find_free_extent_clustered(struct btrfs_block_group *bg,
Naohiro Aota897cae72020-02-25 12:56:20 +09003494 struct find_free_extent_ctl *ffe_ctl,
3495 struct btrfs_block_group **cluster_bg_ret)
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003496{
David Sterba32da53862019-10-29 19:20:18 +01003497 struct btrfs_block_group *cluster_bg;
Naohiro Aota897cae72020-02-25 12:56:20 +09003498 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003499 u64 aligned_cluster;
3500 u64 offset;
3501 int ret;
3502
3503 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3504 if (!cluster_bg)
3505 goto refill_cluster;
3506 if (cluster_bg != bg && (cluster_bg->ro ||
3507 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3508 goto release_cluster;
3509
3510 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
David Sterbab3470b52019-10-23 18:48:22 +02003511 ffe_ctl->num_bytes, cluster_bg->start,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003512 &ffe_ctl->max_extent_size);
3513 if (offset) {
3514 /* We have a block, we're done */
3515 spin_unlock(&last_ptr->refill_lock);
3516 trace_btrfs_reserve_extent_cluster(cluster_bg,
3517 ffe_ctl->search_start, ffe_ctl->num_bytes);
3518 *cluster_bg_ret = cluster_bg;
3519 ffe_ctl->found_offset = offset;
3520 return 0;
3521 }
3522 WARN_ON(last_ptr->block_group != cluster_bg);
3523
3524release_cluster:
3525 /*
3526 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3527 * lets just skip it and let the allocator find whatever block it can
3528 * find. If we reach this point, we will have tried the cluster
3529 * allocator plenty of times and not have found anything, so we are
3530 * likely way too fragmented for the clustering stuff to find anything.
3531 *
3532 * However, if the cluster is taken from the current block group,
3533 * release the cluster first, so that we stand a better chance of
3534 * succeeding in the unclustered allocation.
3535 */
3536 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3537 spin_unlock(&last_ptr->refill_lock);
3538 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3539 return -ENOENT;
3540 }
3541
3542 /* This cluster didn't work out, free it and start over */
3543 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3544
3545 if (cluster_bg != bg)
3546 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3547
3548refill_cluster:
3549 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3550 spin_unlock(&last_ptr->refill_lock);
3551 return -ENOENT;
3552 }
3553
3554 aligned_cluster = max_t(u64,
3555 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3556 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003557 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3558 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003559 if (ret == 0) {
3560 /* Now pull our allocation out of this cluster */
3561 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3562 ffe_ctl->num_bytes, ffe_ctl->search_start,
3563 &ffe_ctl->max_extent_size);
3564 if (offset) {
3565 /* We found one, proceed */
3566 spin_unlock(&last_ptr->refill_lock);
3567 trace_btrfs_reserve_extent_cluster(bg,
3568 ffe_ctl->search_start,
3569 ffe_ctl->num_bytes);
3570 ffe_ctl->found_offset = offset;
3571 return 0;
3572 }
3573 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3574 !ffe_ctl->retry_clustered) {
3575 spin_unlock(&last_ptr->refill_lock);
3576
3577 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003578 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003579 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3580 return -EAGAIN;
3581 }
3582 /*
3583 * At this point we either didn't find a cluster or we weren't able to
3584 * allocate a block from our cluster. Free the cluster we've been
3585 * trying to use, and go to the next block group.
3586 */
3587 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3588 spin_unlock(&last_ptr->refill_lock);
3589 return 1;
3590}
3591
Qu Wenruob4bd7452018-11-02 09:39:47 +08003592/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003593 * Return >0 to inform caller that we find nothing
3594 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3595 * Return -EAGAIN to inform caller that we need to re-search this block group
3596 */
David Sterba32da53862019-10-29 19:20:18 +01003597static int find_free_extent_unclustered(struct btrfs_block_group *bg,
Naohiro Aota897cae72020-02-25 12:56:20 +09003598 struct find_free_extent_ctl *ffe_ctl)
Qu Wenruoe1a41842018-11-02 09:39:49 +08003599{
Naohiro Aota897cae72020-02-25 12:56:20 +09003600 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003601 u64 offset;
3602
3603 /*
3604 * We are doing an unclustered allocation, set the fragmented flag so
3605 * we don't bother trying to setup a cluster again until we get more
3606 * space.
3607 */
3608 if (unlikely(last_ptr)) {
3609 spin_lock(&last_ptr->lock);
3610 last_ptr->fragmented = 1;
3611 spin_unlock(&last_ptr->lock);
3612 }
3613 if (ffe_ctl->cached) {
3614 struct btrfs_free_space_ctl *free_space_ctl;
3615
3616 free_space_ctl = bg->free_space_ctl;
3617 spin_lock(&free_space_ctl->tree_lock);
3618 if (free_space_ctl->free_space <
3619 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3620 ffe_ctl->empty_size) {
3621 ffe_ctl->total_free_space = max_t(u64,
3622 ffe_ctl->total_free_space,
3623 free_space_ctl->free_space);
3624 spin_unlock(&free_space_ctl->tree_lock);
3625 return 1;
3626 }
3627 spin_unlock(&free_space_ctl->tree_lock);
3628 }
3629
3630 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3631 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3632 &ffe_ctl->max_extent_size);
3633
3634 /*
3635 * If we didn't find a chunk, and we haven't failed on this block group
3636 * before, and this block group is in the middle of caching and we are
3637 * ok with waiting, then go ahead and wait for progress to be made, and
3638 * set @retry_unclustered to true.
3639 *
3640 * If @retry_unclustered is true then we've already waited on this
3641 * block group once and should move on to the next block group.
3642 */
3643 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3644 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003645 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3646 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003647 ffe_ctl->retry_unclustered = true;
3648 return -EAGAIN;
3649 } else if (!offset) {
3650 return 1;
3651 }
3652 ffe_ctl->found_offset = offset;
3653 return 0;
3654}
3655
Naohiro Aotac6686902020-02-25 12:56:19 +09003656static int do_allocation_clustered(struct btrfs_block_group *block_group,
3657 struct find_free_extent_ctl *ffe_ctl,
3658 struct btrfs_block_group **bg_ret)
3659{
3660 int ret;
3661
3662 /* We want to try and use the cluster allocator, so lets look there */
3663 if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) {
Naohiro Aota897cae72020-02-25 12:56:20 +09003664 ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret);
Naohiro Aotac6686902020-02-25 12:56:19 +09003665 if (ret >= 0 || ret == -EAGAIN)
3666 return ret;
3667 /* ret == -ENOENT case falls through */
3668 }
3669
Naohiro Aota897cae72020-02-25 12:56:20 +09003670 return find_free_extent_unclustered(block_group, ffe_ctl);
Naohiro Aotac6686902020-02-25 12:56:19 +09003671}
3672
3673static int do_allocation(struct btrfs_block_group *block_group,
3674 struct find_free_extent_ctl *ffe_ctl,
3675 struct btrfs_block_group **bg_ret)
3676{
3677 switch (ffe_ctl->policy) {
3678 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3679 return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
3680 default:
3681 BUG();
3682 }
3683}
3684
Naohiro Aotababa5062020-02-25 12:56:21 +09003685static void release_block_group(struct btrfs_block_group *block_group,
3686 struct find_free_extent_ctl *ffe_ctl,
3687 int delalloc)
3688{
3689 switch (ffe_ctl->policy) {
3690 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3691 ffe_ctl->retry_clustered = false;
3692 ffe_ctl->retry_unclustered = false;
3693 break;
3694 default:
3695 BUG();
3696 }
3697
3698 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
3699 ffe_ctl->index);
3700 btrfs_release_block_group(block_group, delalloc);
3701}
3702
Naohiro Aota0ab97242020-02-25 12:56:22 +09003703static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl,
3704 struct btrfs_key *ins)
3705{
3706 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3707
3708 if (!ffe_ctl->use_cluster && last_ptr) {
3709 spin_lock(&last_ptr->lock);
3710 last_ptr->window_start = ins->objectid;
3711 spin_unlock(&last_ptr->lock);
3712 }
3713}
3714
3715static void found_extent(struct find_free_extent_ctl *ffe_ctl,
3716 struct btrfs_key *ins)
3717{
3718 switch (ffe_ctl->policy) {
3719 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3720 found_extent_clustered(ffe_ctl, ins);
3721 break;
3722 default:
3723 BUG();
3724 }
3725}
3726
Naohiro Aotac70e2132020-02-25 12:56:24 +09003727static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl)
3728{
3729 switch (ffe_ctl->policy) {
3730 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3731 /*
3732 * If we can't allocate a new chunk we've already looped through
3733 * at least once, move on to the NO_EMPTY_SIZE case.
3734 */
3735 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3736 return 0;
3737 default:
3738 BUG();
3739 }
3740}
3741
Qu Wenruoe1a41842018-11-02 09:39:49 +08003742/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003743 * Return >0 means caller needs to re-search for free extent
3744 * Return 0 means we have the needed free extent.
3745 * Return <0 means we failed to locate any free extent.
3746 */
3747static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003748 struct btrfs_key *ins,
3749 struct find_free_extent_ctl *ffe_ctl,
Naohiro Aota15b7ee62020-02-25 12:56:23 +09003750 bool full_search)
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003751{
3752 struct btrfs_root *root = fs_info->extent_root;
3753 int ret;
3754
3755 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3756 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3757 ffe_ctl->orig_have_caching_bg = true;
3758
3759 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3760 ffe_ctl->have_caching_bg)
3761 return 1;
3762
3763 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3764 return 1;
3765
3766 if (ins->objectid) {
Naohiro Aota0ab97242020-02-25 12:56:22 +09003767 found_extent(ffe_ctl, ins);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003768 return 0;
3769 }
3770
3771 /*
3772 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3773 * caching kthreads as we move along
3774 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3775 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3776 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3777 * again
3778 */
3779 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3780 ffe_ctl->index = 0;
3781 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3782 /*
3783 * We want to skip the LOOP_CACHING_WAIT step if we
3784 * don't have any uncached bgs and we've already done a
3785 * full search through.
3786 */
3787 if (ffe_ctl->orig_have_caching_bg || !full_search)
3788 ffe_ctl->loop = LOOP_CACHING_WAIT;
3789 else
3790 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3791 } else {
3792 ffe_ctl->loop++;
3793 }
3794
3795 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3796 struct btrfs_trans_handle *trans;
3797 int exist = 0;
3798
3799 trans = current->journal_info;
3800 if (trans)
3801 exist = 1;
3802 else
3803 trans = btrfs_join_transaction(root);
3804
3805 if (IS_ERR(trans)) {
3806 ret = PTR_ERR(trans);
3807 return ret;
3808 }
3809
Josef Bacikfc471cb2019-06-18 16:09:17 -04003810 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3811 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003812
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003813 /* Do not bail out on ENOSPC since we can do more. */
Naohiro Aotac70e2132020-02-25 12:56:24 +09003814 if (ret == -ENOSPC)
3815 ret = chunk_allocation_failed(ffe_ctl);
3816 else if (ret < 0)
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003817 btrfs_abort_transaction(trans, ret);
3818 else
3819 ret = 0;
3820 if (!exist)
3821 btrfs_end_transaction(trans);
3822 if (ret)
3823 return ret;
3824 }
3825
3826 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
Naohiro Aota45d8e032020-02-25 12:56:25 +09003827 if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED)
3828 return -ENOSPC;
3829
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003830 /*
3831 * Don't loop again if we already have no empty_size and
3832 * no empty_cluster.
3833 */
3834 if (ffe_ctl->empty_size == 0 &&
3835 ffe_ctl->empty_cluster == 0)
3836 return -ENOSPC;
3837 ffe_ctl->empty_size = 0;
3838 ffe_ctl->empty_cluster = 0;
3839 }
3840 return 1;
3841 }
3842 return -ENOSPC;
3843}
3844
Naohiro Aota7e895402020-02-25 12:56:26 +09003845static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info,
3846 struct find_free_extent_ctl *ffe_ctl,
3847 struct btrfs_space_info *space_info,
3848 struct btrfs_key *ins)
3849{
3850 /*
3851 * If our free space is heavily fragmented we may not be able to make
3852 * big contiguous allocations, so instead of doing the expensive search
3853 * for free space, simply return ENOSPC with our max_extent_size so we
3854 * can go ahead and search for a more manageable chunk.
3855 *
3856 * If our max_extent_size is large enough for our allocation simply
3857 * disable clustering since we will likely not be able to find enough
3858 * space to create a cluster and induce latency trying.
3859 */
3860 if (space_info->max_extent_size) {
3861 spin_lock(&space_info->lock);
3862 if (space_info->max_extent_size &&
3863 ffe_ctl->num_bytes > space_info->max_extent_size) {
3864 ins->offset = space_info->max_extent_size;
3865 spin_unlock(&space_info->lock);
3866 return -ENOSPC;
3867 } else if (space_info->max_extent_size) {
3868 ffe_ctl->use_cluster = false;
3869 }
3870 spin_unlock(&space_info->lock);
3871 }
3872
3873 ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info,
3874 &ffe_ctl->empty_cluster);
3875 if (ffe_ctl->last_ptr) {
3876 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3877
3878 spin_lock(&last_ptr->lock);
3879 if (last_ptr->block_group)
3880 ffe_ctl->hint_byte = last_ptr->window_start;
3881 if (last_ptr->fragmented) {
3882 /*
3883 * We still set window_start so we can keep track of the
3884 * last place we found an allocation to try and save
3885 * some time.
3886 */
3887 ffe_ctl->hint_byte = last_ptr->window_start;
3888 ffe_ctl->use_cluster = false;
3889 }
3890 spin_unlock(&last_ptr->lock);
3891 }
3892
3893 return 0;
3894}
3895
3896static int prepare_allocation(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 switch (ffe_ctl->policy) {
3902 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3903 return prepare_allocation_clustered(fs_info, ffe_ctl,
3904 space_info, ins);
3905 default:
3906 BUG();
3907 }
3908}
3909
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003910/*
Chris Masonfec577f2007-02-26 10:40:21 -05003911 * walks the btree of allocated extents and find a hole of a given size.
3912 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003913 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003914 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003915 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003916 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003917 *
3918 * If there is no suitable free space, we will record the max size of
3919 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003920 *
3921 * The overall logic and call chain:
3922 *
3923 * find_free_extent()
3924 * |- Iterate through all block groups
3925 * | |- Get a valid block group
3926 * | |- Try to do clustered allocation in that block group
3927 * | |- Try to do unclustered allocation in that block group
3928 * | |- Check if the result is valid
3929 * | | |- If valid, then exit
3930 * | |- Jump to next block group
3931 * |
3932 * |- Push harder to find free extents
3933 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003934 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003935static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003936 u64 ram_bytes, u64 num_bytes, u64 empty_size,
Naohiro Aotaea544142020-02-25 12:56:17 +09003937 u64 hint_byte_orig, struct btrfs_key *ins,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003938 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003939{
Josef Bacik80eb2342008-10-29 14:49:05 -04003940 int ret = 0;
Josef Bacikdb8fe642019-11-19 13:59:00 -05003941 int cache_block_group_error = 0;
David Sterba32da53862019-10-29 19:20:18 +01003942 struct btrfs_block_group *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003943 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003944 struct btrfs_space_info *space_info;
Josef Bacika5e681d2015-10-01 14:54:10 -04003945 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003946
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003947 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003948
Qu Wenruob4bd7452018-11-02 09:39:47 +08003949 ffe_ctl.num_bytes = num_bytes;
3950 ffe_ctl.empty_size = empty_size;
3951 ffe_ctl.flags = flags;
3952 ffe_ctl.search_start = 0;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003953 ffe_ctl.delalloc = delalloc;
3954 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
3955 ffe_ctl.have_caching_bg = false;
3956 ffe_ctl.orig_have_caching_bg = false;
3957 ffe_ctl.found_offset = 0;
Naohiro Aotaea544142020-02-25 12:56:17 +09003958 ffe_ctl.hint_byte = hint_byte_orig;
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003959 ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003960
Naohiro Aotac10859b2020-02-25 12:56:18 +09003961 /* For clustered allocation */
3962 ffe_ctl.retry_clustered = false;
3963 ffe_ctl.retry_unclustered = false;
3964 ffe_ctl.last_ptr = NULL;
3965 ffe_ctl.use_cluster = true;
3966
David Sterba962a2982014-06-04 18:41:45 +02003967 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04003968 ins->objectid = 0;
3969 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003970
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003971 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05003972
Josef Bacik280c29082019-06-18 16:09:19 -04003973 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003974 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003975 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003976 return -ENOSPC;
3977 }
Josef Bacik2552d172009-04-03 10:14:19 -04003978
Naohiro Aota7e895402020-02-25 12:56:26 +09003979 ret = prepare_allocation(fs_info, &ffe_ctl, space_info, ins);
3980 if (ret < 0)
3981 return ret;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003982
Qu Wenruob4bd7452018-11-02 09:39:47 +08003983 ffe_ctl.search_start = max(ffe_ctl.search_start,
3984 first_logical_byte(fs_info, 0));
Naohiro Aotaea544142020-02-25 12:56:17 +09003985 ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte);
3986 if (ffe_ctl.search_start == ffe_ctl.hint_byte) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003987 block_group = btrfs_lookup_block_group(fs_info,
3988 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003989 /*
3990 * we don't want to use the block group if it doesn't match our
3991 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05003992 *
3993 * However if we are re-searching with an ideal block group
3994 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04003995 */
David Sterbab6919a52013-04-29 13:39:40 +00003996 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05003997 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04003998 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003999 if (list_empty(&block_group->list) ||
4000 block_group->ro) {
4001 /*
4002 * someone is removing this block group,
4003 * we can't jump into the have_block_group
4004 * target because our list pointers are not
4005 * valid
4006 */
4007 btrfs_put_block_group(block_group);
4008 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004009 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004010 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004011 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08004012 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04004013 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004014 }
Josef Bacik2552d172009-04-03 10:14:19 -04004015 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004016 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004017 }
Chris Mason42e70e72008-11-07 18:17:11 -05004018 }
Josef Bacik2552d172009-04-03 10:14:19 -04004019search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004020 ffe_ctl.have_caching_bg = false;
4021 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
4022 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04004023 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04004024 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004025 list_for_each_entry(block_group,
4026 &space_info->block_groups[ffe_ctl.index], list) {
Naohiro Aotac6686902020-02-25 12:56:19 +09004027 struct btrfs_block_group *bg_ret;
4028
Jeff Mahoney14443932017-07-19 23:25:51 -04004029 /* If the block group is read-only, we can skip it entirely. */
4030 if (unlikely(block_group->ro))
4031 continue;
4032
Miao Xiee570fd22014-06-19 10:42:50 +08004033 btrfs_grab_block_group(block_group, delalloc);
David Sterbab3470b52019-10-23 18:48:22 +02004034 ffe_ctl.search_start = block_group->start;
Chris Mason42e70e72008-11-07 18:17:11 -05004035
Chris Mason83a50de2010-12-13 15:06:46 -05004036 /*
4037 * this can happen if we end up cycling through all the
4038 * raid types, but we want to make sure we only allocate
4039 * for the proper type.
4040 */
David Sterbab6919a52013-04-29 13:39:40 +00004041 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07004042 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02004043 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02004044 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05004045 BTRFS_BLOCK_GROUP_RAID10;
4046
4047 /*
4048 * if they asked for extra copies and this block group
4049 * doesn't provide them, bail. This does allow us to
4050 * fill raid0 from raid1.
4051 */
David Sterbab6919a52013-04-29 13:39:40 +00004052 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05004053 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08004054
4055 /*
4056 * This block group has different flags than we want.
4057 * It's possible that we have MIXED_GROUP flag but no
4058 * block group is mixed. Just skip such block group.
4059 */
4060 btrfs_release_block_group(block_group, delalloc);
4061 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05004062 }
4063
Josef Bacik2552d172009-04-03 10:14:19 -04004064have_block_group:
David Sterba32da53862019-10-29 19:20:18 +01004065 ffe_ctl.cached = btrfs_block_group_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004066 if (unlikely(!ffe_ctl.cached)) {
4067 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04004068 ret = btrfs_cache_block_group(block_group, 0);
Josef Bacikdb8fe642019-11-19 13:59:00 -05004069
4070 /*
4071 * If we get ENOMEM here or something else we want to
4072 * try other block groups, because it may not be fatal.
4073 * However if we can't find anything else we need to
4074 * save our return here so that we return the actual
4075 * error that caused problems, not ENOSPC.
4076 */
4077 if (ret < 0) {
4078 if (!cache_block_group_error)
4079 cache_block_group_error = ret;
4080 ret = 0;
4081 goto loop;
4082 }
Chris Mason1d4284b2012-03-28 20:31:37 -04004083 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05004084 }
4085
Josef Bacik36cce922013-08-05 11:15:21 -04004086 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
4087 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004088
Naohiro Aotac6686902020-02-25 12:56:19 +09004089 bg_ret = NULL;
4090 ret = do_allocation(block_group, &ffe_ctl, &bg_ret);
4091 if (ret == 0) {
4092 if (bg_ret && bg_ret != block_group) {
4093 btrfs_release_block_group(block_group, delalloc);
4094 block_group = bg_ret;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004095 }
Naohiro Aotac6686902020-02-25 12:56:19 +09004096 } else if (ret == -EAGAIN) {
4097 goto have_block_group;
4098 } else if (ret > 0) {
4099 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004100 }
4101
Naohiro Aotac6686902020-02-25 12:56:19 +09004102 /* Checks */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004103 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4104 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04004105
Josef Bacik2552d172009-04-03 10:14:19 -04004106 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004107 if (ffe_ctl.search_start + num_bytes >
David Sterbab3470b52019-10-23 18:48:22 +02004108 block_group->start + block_group->length) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004109 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4110 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004111 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004112 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004113
Qu Wenruob4bd7452018-11-02 09:39:47 +08004114 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4115 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4116 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004117
Wang Xiaoguang18513092016-07-25 15:51:40 +08004118 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4119 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004120 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004121 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4122 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004123 goto loop;
4124 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004125 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004126
Josef Bacik2552d172009-04-03 10:14:19 -04004127 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004128 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004129 ins->offset = num_bytes;
4130
Qu Wenruob4bd7452018-11-02 09:39:47 +08004131 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4132 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004133 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004134 break;
4135loop:
Naohiro Aotababa5062020-02-25 12:56:21 +09004136 release_block_group(block_group, &ffe_ctl, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004137 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004138 }
4139 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004140
Naohiro Aota15b7ee62020-02-25 12:56:23 +09004141 ret = find_free_extent_update_loop(fs_info, ins, &ffe_ctl, full_search);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004142 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004143 goto search;
4144
Josef Bacikdb8fe642019-11-19 13:59:00 -05004145 if (ret == -ENOSPC && !cache_block_group_error) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004146 /*
4147 * Use ffe_ctl->total_free_space as fallback if we can't find
4148 * any contiguous hole.
4149 */
4150 if (!ffe_ctl.max_extent_size)
4151 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004152 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004153 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004154 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004155 ins->offset = ffe_ctl.max_extent_size;
Josef Bacikdb8fe642019-11-19 13:59:00 -05004156 } else if (ret == -ENOSPC) {
4157 ret = cache_block_group_error;
Josef Bacik4f4db212015-09-29 11:40:47 -04004158 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004159 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004160}
Chris Masonec44a352008-04-28 15:29:52 -04004161
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004162/*
4163 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4164 * hole that is at least as big as @num_bytes.
4165 *
4166 * @root - The root that will contain this extent
4167 *
4168 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4169 * is used for accounting purposes. This value differs
4170 * from @num_bytes only in the case of compressed extents.
4171 *
4172 * @num_bytes - Number of bytes to allocate on-disk.
4173 *
4174 * @min_alloc_size - Indicates the minimum amount of space that the
4175 * allocator should try to satisfy. In some cases
4176 * @num_bytes may be larger than what is required and if
4177 * the filesystem is fragmented then allocation fails.
4178 * However, the presence of @min_alloc_size gives a
4179 * chance to try and satisfy the smaller allocation.
4180 *
4181 * @empty_size - A hint that you plan on doing more COW. This is the
4182 * size in bytes the allocator should try to find free
4183 * next to the block it returns. This is just a hint and
4184 * may be ignored by the allocator.
4185 *
4186 * @hint_byte - Hint to the allocator to start searching above the byte
4187 * address passed. It might be ignored.
4188 *
4189 * @ins - This key is modified to record the found hole. It will
4190 * have the following values:
4191 * ins->objectid == start position
4192 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4193 * ins->offset == the size of the hole.
4194 *
4195 * @is_data - Boolean flag indicating whether an extent is
4196 * allocated for data (true) or metadata (false)
4197 *
4198 * @delalloc - Boolean flag indicating whether this allocation is for
4199 * delalloc or not. If 'true' data_rwsem of block groups
4200 * is going to be acquired.
4201 *
4202 *
4203 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4204 * case -ENOSPC is returned then @ins->offset will contain the size of the
4205 * largest available hole the allocator managed to find.
4206 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004207int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004208 u64 num_bytes, u64 min_alloc_size,
4209 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004210 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004211{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004212 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004213 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004214 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004215 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004216
Jeff Mahoney1b868262017-05-17 11:38:35 -04004217 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004218again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004219 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004220 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004221 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004222 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004223 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004224 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004225 if (!final_tried && ins->offset) {
4226 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004227 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004228 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004229 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004230 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004231 if (num_bytes == min_alloc_size)
4232 final_tried = true;
4233 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004234 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004235 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004236
Josef Bacik280c29082019-06-18 16:09:19 -04004237 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004238 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004239 "allocation failed flags %llu, wanted %llu",
4240 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004241 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004242 btrfs_dump_space_info(fs_info, sinfo,
4243 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004244 }
Chris Mason925baed2008-06-25 16:01:30 -04004245 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004246
4247 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248}
4249
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004250int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004251 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004252{
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004253 struct btrfs_block_group *cache;
4254
4255 cache = btrfs_lookup_block_group(fs_info, start);
4256 if (!cache) {
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004257 btrfs_err(fs_info, "Unable to find block group for %llu",
4258 start);
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004259 return -ENOSPC;
4260 }
4261
4262 btrfs_add_free_space(cache, start, len);
4263 btrfs_free_reserved_bytes(cache, len, delalloc);
4264 trace_btrfs_reserved_extent_free(fs_info, start, len);
4265
4266 btrfs_put_block_group(cache);
4267 return 0;
Chris Masone688b7252011-10-31 20:52:39 -04004268}
4269
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004270int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
4271 u64 len)
Chris Masone688b7252011-10-31 20:52:39 -04004272{
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004273 struct btrfs_block_group *cache;
4274 int ret = 0;
4275
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004276 cache = btrfs_lookup_block_group(trans->fs_info, start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004277 if (!cache) {
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004278 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4279 start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004280 return -ENOSPC;
4281 }
4282
Nikolay Borisov6690d072020-01-20 16:09:14 +02004283 ret = pin_down_extent(trans, cache, start, len, 1);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004284 btrfs_put_block_group(cache);
4285 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04004286}
4287
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004288static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004289 u64 parent, u64 root_objectid,
4290 u64 flags, u64 owner, u64 offset,
4291 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004292{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004293 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004294 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004295 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004296 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004297 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004298 struct extent_buffer *leaf;
4299 int type;
4300 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004301
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004302 if (parent > 0)
4303 type = BTRFS_SHARED_DATA_REF_KEY;
4304 else
4305 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004306
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004307 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004308
4309 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004310 if (!path)
4311 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004312
Chris Masonb9473432009-03-13 11:00:37 -04004313 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004314 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4315 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004316 if (ret) {
4317 btrfs_free_path(path);
4318 return ret;
4319 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004320
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004321 leaf = path->nodes[0];
4322 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004323 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004324 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4325 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4326 btrfs_set_extent_flags(leaf, extent_item,
4327 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004329 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4330 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4331 if (parent > 0) {
4332 struct btrfs_shared_data_ref *ref;
4333 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4334 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4335 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4336 } else {
4337 struct btrfs_extent_data_ref *ref;
4338 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4339 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4340 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4341 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4342 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4343 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004344
4345 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004346 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004347
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004348 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004349 if (ret)
4350 return ret;
4351
Josef Bacikade4b512019-06-20 15:38:01 -04004352 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004353 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004354 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004355 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004356 BUG();
4357 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004358 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 return ret;
4360}
4361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004362static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004363 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004364 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004365{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004366 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004367 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004368 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004369 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004370 struct btrfs_tree_block_info *block_info;
4371 struct btrfs_extent_inline_ref *iref;
4372 struct btrfs_path *path;
4373 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004374 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004375 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004376 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004377 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004378 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004379
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004380 ref = btrfs_delayed_node_to_tree_ref(node);
4381
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004382 extent_key.objectid = node->bytenr;
4383 if (skinny_metadata) {
4384 extent_key.offset = ref->level;
4385 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4386 num_bytes = fs_info->nodesize;
4387 } else {
4388 extent_key.offset = node->num_bytes;
4389 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004390 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004391 num_bytes = node->num_bytes;
4392 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393
4394 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004395 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004396 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004397
4398 path->leave_spinning = 1;
4399 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004400 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004401 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004402 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004403 return ret;
4404 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004405
4406 leaf = path->nodes[0];
4407 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4408 struct btrfs_extent_item);
4409 btrfs_set_extent_refs(leaf, extent_item, 1);
4410 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4411 btrfs_set_extent_flags(leaf, extent_item,
4412 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004413
Josef Bacik3173a182013-03-07 14:22:04 -05004414 if (skinny_metadata) {
4415 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4416 } else {
4417 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004418 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004419 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004420 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4421 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004422
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004423 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004424 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4425 btrfs_set_extent_inline_ref_type(leaf, iref,
4426 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004427 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428 } else {
4429 btrfs_set_extent_inline_ref_type(leaf, iref,
4430 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004431 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004432 }
4433
4434 btrfs_mark_buffer_dirty(leaf);
4435 btrfs_free_path(path);
4436
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004437 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4438 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004439 if (ret)
4440 return ret;
4441
Josef Bacikade4b512019-06-20 15:38:01 -04004442 ret = btrfs_update_block_group(trans, extent_key.objectid,
4443 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004444 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004445 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004446 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004447 BUG();
4448 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004449
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004450 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004451 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004452 return ret;
4453}
4454
4455int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004456 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004457 u64 offset, u64 ram_bytes,
4458 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004459{
Qu Wenruo76675592019-04-04 14:45:32 +08004460 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04004461 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004462
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004463 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004464
Qu Wenruo76675592019-04-04 14:45:32 +08004465 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4466 ins->objectid, ins->offset, 0);
4467 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004468 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08004469 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4470 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004471 return ret;
4472}
Chris Masone02119d2008-09-05 16:13:11 -04004473
4474/*
4475 * this is used by the tree logging recovery code. It records that
4476 * an extent has been allocated and makes sure to clear the free
4477 * space cache bits as well
4478 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004479int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004480 u64 root_objectid, u64 owner, u64 offset,
4481 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004482{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004483 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004484 int ret;
David Sterba32da53862019-10-29 19:20:18 +01004485 struct btrfs_block_group *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004486 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004487
4488 /*
4489 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004490 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004491 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004492 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004493 ret = __exclude_logged_extent(fs_info, ins->objectid,
4494 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004495 if (ret)
4496 return ret;
4497 }
Chris Masone02119d2008-09-05 16:13:11 -04004498
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004499 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004500 if (!block_group)
4501 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004502
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004503 space_info = block_group->space_info;
4504 spin_lock(&space_info->lock);
4505 spin_lock(&block_group->lock);
4506 space_info->bytes_reserved += ins->offset;
4507 block_group->reserved += ins->offset;
4508 spin_unlock(&block_group->lock);
4509 spin_unlock(&space_info->lock);
4510
Nikolay Borisovef89b822018-06-20 15:48:58 +03004511 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4512 offset, ins, 1);
Josef Bacikbd727172020-02-13 10:47:30 -05004513 if (ret)
Josef Bacikab9b2c72020-02-13 10:47:30 -05004514 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004515 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004516 return ret;
4517}
4518
Eric Sandeen48a3b632013-04-25 20:41:01 +00004519static struct extent_buffer *
4520btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004521 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04004522{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004523 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004524 struct extent_buffer *buf;
4525
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004526 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004527 if (IS_ERR(buf))
4528 return buf;
4529
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004530 /*
4531 * Extra safety check in case the extent tree is corrupted and extent
4532 * allocator chooses to use a tree block which is already used and
4533 * locked.
4534 */
4535 if (buf->lock_owner == current->pid) {
4536 btrfs_err_rl(fs_info,
4537"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4538 buf->start, btrfs_header_owner(buf), current->pid);
4539 free_extent_buffer(buf);
4540 return ERR_PTR(-EUCLEAN);
4541 }
4542
Chris Mason85d4e462011-07-26 16:11:19 -04004543 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004544 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01004545 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004546 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004547
David Sterba8bead252018-04-04 02:03:48 +02004548 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004549 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004550
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004551 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4552 btrfs_set_header_level(buf, level);
4553 btrfs_set_header_bytenr(buf, buf->start);
4554 btrfs_set_header_generation(buf, trans->transid);
4555 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4556 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004557 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004558 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004559 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004560 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004561 /*
4562 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004563 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004564 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004565 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004566 set_extent_dirty(&root->dirty_log_pages, buf->start,
4567 buf->start + buf->len - 1, GFP_NOFS);
4568 else
4569 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004570 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004571 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004572 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004573 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4574 buf->start + buf->len - 1, GFP_NOFS);
4575 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004576 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004577 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004578 return buf;
4579}
4580
Chris Masonfec577f2007-02-26 10:40:21 -05004581/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004582 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004583 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004584 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004585struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004586 struct btrfs_root *root,
4587 u64 parent, u64 root_objectid,
4588 const struct btrfs_disk_key *key,
4589 int level, u64 hint,
4590 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004591{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004592 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004593 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004594 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004595 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004596 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004597 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004598 u64 flags = 0;
4599 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004600 u32 blocksize = fs_info->nodesize;
4601 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004602
David Sterba05653ef2016-07-15 15:23:37 +02004603#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004604 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004605 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004606 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004607 if (!IS_ERR(buf))
4608 root->alloc_bytenr += blocksize;
4609 return buf;
4610 }
David Sterba05653ef2016-07-15 15:23:37 +02004611#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004612
Josef Bacik67f9c222019-06-19 13:47:23 -04004613 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004614 if (IS_ERR(block_rsv))
4615 return ERR_CAST(block_rsv);
4616
Wang Xiaoguang18513092016-07-25 15:51:40 +08004617 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004618 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004619 if (ret)
4620 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004621
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004622 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4623 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08004624 if (IS_ERR(buf)) {
4625 ret = PTR_ERR(buf);
4626 goto out_free_reserved;
4627 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004628
4629 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4630 if (parent == 0)
4631 parent = ins.objectid;
4632 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4633 } else
4634 BUG_ON(parent > 0);
4635
4636 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004637 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004638 if (!extent_op) {
4639 ret = -ENOMEM;
4640 goto out_free_buf;
4641 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004642 if (key)
4643 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4644 else
4645 memset(&extent_op->key, 0, sizeof(extent_op->key));
4646 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004647 extent_op->update_key = skinny_metadata ? false : true;
4648 extent_op->update_flags = true;
4649 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004650 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004651
Qu Wenruoed4f2552019-04-04 14:45:31 +08004652 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4653 ins.objectid, ins.offset, parent);
4654 generic_ref.real_root = root->root_key.objectid;
4655 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004656 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004657 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07004658 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08004659 if (ret)
4660 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004661 }
Chris Masonfec577f2007-02-26 10:40:21 -05004662 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004663
4664out_free_delayed:
4665 btrfs_free_delayed_extent_op(extent_op);
4666out_free_buf:
4667 free_extent_buffer(buf);
4668out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004669 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004670out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004671 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004672 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004673}
Chris Masona28ec192007-03-06 20:08:01 -05004674
Yan Zheng2c47e6052009-06-27 21:07:35 -04004675struct walk_control {
4676 u64 refs[BTRFS_MAX_LEVEL];
4677 u64 flags[BTRFS_MAX_LEVEL];
4678 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004679 struct btrfs_key drop_progress;
4680 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004681 int stage;
4682 int level;
4683 int shared_level;
4684 int update_ref;
4685 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004686 int reada_slot;
4687 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004688 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004689};
4690
4691#define DROP_REFERENCE 1
4692#define UPDATE_BACKREF 2
4693
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004694static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4695 struct btrfs_root *root,
4696 struct walk_control *wc,
4697 struct btrfs_path *path)
4698{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004699 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004700 u64 bytenr;
4701 u64 generation;
4702 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004703 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004704 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004705 struct btrfs_key key;
4706 struct extent_buffer *eb;
4707 int ret;
4708 int slot;
4709 int nread = 0;
4710
4711 if (path->slots[wc->level] < wc->reada_slot) {
4712 wc->reada_count = wc->reada_count * 2 / 3;
4713 wc->reada_count = max(wc->reada_count, 2);
4714 } else {
4715 wc->reada_count = wc->reada_count * 3 / 2;
4716 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004717 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004718 }
4719
4720 eb = path->nodes[wc->level];
4721 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004722
4723 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4724 if (nread >= wc->reada_count)
4725 break;
4726
4727 cond_resched();
4728 bytenr = btrfs_node_blockptr(eb, slot);
4729 generation = btrfs_node_ptr_generation(eb, slot);
4730
4731 if (slot == path->slots[wc->level])
4732 goto reada;
4733
4734 if (wc->stage == UPDATE_BACKREF &&
4735 generation <= root->root_key.offset)
4736 continue;
4737
Yan, Zheng94fcca92009-10-09 09:25:16 -04004738 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004739 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004740 wc->level - 1, 1, &refs,
4741 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004742 /* We don't care about errors in readahead. */
4743 if (ret < 0)
4744 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004745 BUG_ON(refs == 0);
4746
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004747 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004748 if (refs == 1)
4749 goto reada;
4750
Yan, Zheng94fcca92009-10-09 09:25:16 -04004751 if (wc->level == 1 &&
4752 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4753 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004754 if (!wc->update_ref ||
4755 generation <= root->root_key.offset)
4756 continue;
4757 btrfs_node_key_to_cpu(eb, &key, slot);
4758 ret = btrfs_comp_cpu_keys(&key,
4759 &wc->update_progress);
4760 if (ret < 0)
4761 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004762 } else {
4763 if (wc->level == 1 &&
4764 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4765 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004766 }
4767reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004768 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004769 nread++;
4770 }
4771 wc->reada_slot = slot;
4772}
4773
Chris Mason9aca1d52007-03-13 11:09:37 -04004774/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004775 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004776 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004777 * when wc->stage == UPDATE_BACKREF, this function updates
4778 * back refs for pointers in the block.
4779 *
4780 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004781 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004782static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4783 struct btrfs_root *root,
4784 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004785 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004786{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004787 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004788 int level = wc->level;
4789 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004790 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4791 int ret;
4792
4793 if (wc->stage == UPDATE_BACKREF &&
4794 btrfs_header_owner(eb) != root->root_key.objectid)
4795 return 1;
4796
4797 /*
4798 * when reference count of tree block is 1, it won't increase
4799 * again. once full backref flag is set, we never clear it.
4800 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004801 if (lookup_info &&
4802 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4803 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004804 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004805 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004806 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004807 &wc->refs[level],
4808 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004809 BUG_ON(ret == -ENOMEM);
4810 if (ret)
4811 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004812 BUG_ON(wc->refs[level] == 0);
4813 }
4814
Yan Zheng2c47e6052009-06-27 21:07:35 -04004815 if (wc->stage == DROP_REFERENCE) {
4816 if (wc->refs[level] > 1)
4817 return 1;
4818
4819 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004820 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004821 path->locks[level] = 0;
4822 }
4823 return 0;
4824 }
4825
4826 /* wc->stage == UPDATE_BACKREF */
4827 if (!(wc->flags[level] & flag)) {
4828 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004829 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004830 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004831 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004832 BUG_ON(ret); /* -ENOMEM */
David Sterba42c9d0b2019-03-20 11:54:13 +01004833 ret = btrfs_set_disk_extent_flags(trans, eb, flag,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004834 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004835 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004836 wc->flags[level] |= flag;
4837 }
4838
4839 /*
4840 * the block is shared by multiple trees, so it's not good to
4841 * keep the tree lock
4842 */
4843 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04004844 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004845 path->locks[level] = 0;
4846 }
4847 return 0;
4848}
4849
4850/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004851 * This is used to verify a ref exists for this root to deal with a bug where we
4852 * would have a drop_progress key that hadn't been updated properly.
4853 */
4854static int check_ref_exists(struct btrfs_trans_handle *trans,
4855 struct btrfs_root *root, u64 bytenr, u64 parent,
4856 int level)
4857{
4858 struct btrfs_path *path;
4859 struct btrfs_extent_inline_ref *iref;
4860 int ret;
4861
4862 path = btrfs_alloc_path();
4863 if (!path)
4864 return -ENOMEM;
4865
4866 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4867 root->fs_info->nodesize, parent,
4868 root->root_key.objectid, level, 0);
4869 btrfs_free_path(path);
4870 if (ret == -ENOENT)
4871 return 0;
4872 if (ret < 0)
4873 return ret;
4874 return 1;
4875}
4876
4877/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004878 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004879 *
4880 * when wc->stage == DROP_REFERENCE, this function checks
4881 * reference count of the block pointed to. if the block
4882 * is shared and we need update back refs for the subtree
4883 * rooted at the block, this function changes wc->stage to
4884 * UPDATE_BACKREF. if the block is shared and there is no
4885 * need to update back, this function drops the reference
4886 * to the block.
4887 *
4888 * NOTE: return value 1 means we should stop walking down.
4889 */
4890static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4891 struct btrfs_root *root,
4892 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004893 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004894{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004895 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004896 u64 bytenr;
4897 u64 generation;
4898 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004899 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004900 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004901 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004902 struct extent_buffer *next;
4903 int level = wc->level;
4904 int reada = 0;
4905 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004906 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004907
4908 generation = btrfs_node_ptr_generation(path->nodes[level],
4909 path->slots[level]);
4910 /*
4911 * if the lower level block was created before the snapshot
4912 * was created, we know there is no need to update back refs
4913 * for the subtree
4914 */
4915 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004916 generation <= root->root_key.offset) {
4917 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004918 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004919 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004920
4921 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004922 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4923 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004924
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004925 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004926 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004927 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004928 if (IS_ERR(next))
4929 return PTR_ERR(next);
4930
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004931 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4932 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004933 reada = 1;
4934 }
4935 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004936 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004937
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004938 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004939 &wc->refs[level - 1],
4940 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004941 if (ret < 0)
4942 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004943
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004944 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004945 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004946 ret = -EIO;
4947 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004948 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004949 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004950
Yan, Zheng94fcca92009-10-09 09:25:16 -04004951 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004952 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07004953 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004954 if (level == 1 &&
4955 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4956 goto skip;
4957
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004958 if (!wc->update_ref ||
4959 generation <= root->root_key.offset)
4960 goto skip;
4961
4962 btrfs_node_key_to_cpu(path->nodes[level], &key,
4963 path->slots[level]);
4964 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4965 if (ret < 0)
4966 goto skip;
4967
4968 wc->stage = UPDATE_BACKREF;
4969 wc->shared_level = level - 1;
4970 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004971 } else {
4972 if (level == 1 &&
4973 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4974 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004975 }
4976
Chris Masonb9fab912012-05-06 07:23:47 -04004977 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004978 btrfs_tree_unlock(next);
4979 free_extent_buffer(next);
4980 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004981 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004982 }
4983
4984 if (!next) {
4985 if (reada && level == 1)
4986 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08004987 next = read_tree_block(fs_info, bytenr, generation, level - 1,
4988 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08004989 if (IS_ERR(next)) {
4990 return PTR_ERR(next);
4991 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04004992 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004993 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04004994 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004995 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004996 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004997 }
4998
4999 level--;
Josef Bacik48672682016-09-23 13:23:28 +02005000 ASSERT(level == btrfs_header_level(next));
5001 if (level != btrfs_header_level(next)) {
5002 btrfs_err(root->fs_info, "mismatched level");
5003 ret = -EIO;
5004 goto out_unlock;
5005 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005006 path->nodes[level] = next;
5007 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005008 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005009 wc->level = level;
5010 if (wc->level == 1)
5011 wc->reada_slot = 0;
5012 return 0;
5013skip:
5014 wc->refs[level - 1] = 0;
5015 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005016 if (wc->stage == DROP_REFERENCE) {
5017 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5018 parent = path->nodes[level]->start;
5019 } else {
Josef Bacik48672682016-09-23 13:23:28 +02005020 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04005021 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02005022 if (root->root_key.objectid !=
5023 btrfs_header_owner(path->nodes[level])) {
5024 btrfs_err(root->fs_info,
5025 "mismatched block owner");
5026 ret = -EIO;
5027 goto out_unlock;
5028 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005029 parent = 0;
5030 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005031
Qu Wenruo2cd86d32018-09-27 14:42:33 +08005032 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05005033 * If we had a drop_progress we need to verify the refs are set
5034 * as expected. If we find our ref then we know that from here
5035 * on out everything should be correct, and we can clear the
5036 * ->restarted flag.
5037 */
5038 if (wc->restarted) {
5039 ret = check_ref_exists(trans, root, bytenr, parent,
5040 level - 1);
5041 if (ret < 0)
5042 goto out_unlock;
5043 if (ret == 0)
5044 goto no_delete;
5045 ret = 0;
5046 wc->restarted = 0;
5047 }
5048
5049 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08005050 * Reloc tree doesn't contribute to qgroup numbers, and we have
5051 * already accounted them at merge time (replace_path),
5052 * thus we could skip expensive subtree trace here.
5053 */
5054 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
5055 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08005056 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08005057 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07005058 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005059 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005060 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
5061 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07005062 }
5063 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005064
5065 /*
5066 * We need to update the next key in our walk control so we can
5067 * update the drop_progress key accordingly. We don't care if
5068 * find_next_key doesn't find a key because that means we're at
5069 * the end and are going to clean up now.
5070 */
5071 wc->drop_level = level;
5072 find_next_key(path, level, &wc->drop_progress);
5073
Qu Wenruoffd4bb22019-04-04 14:45:36 +08005074 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
5075 fs_info->nodesize, parent);
5076 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
5077 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02005078 if (ret)
5079 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005080 }
Josef Bacik78c52d92019-02-06 15:46:14 -05005081no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02005082 *lookup_info = 1;
5083 ret = 1;
5084
5085out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005086 btrfs_tree_unlock(next);
5087 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02005088
5089 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005090}
5091
5092/*
Liu Bo2c016dc2012-12-26 15:32:17 +08005093 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04005094 *
5095 * when wc->stage == DROP_REFERENCE, this function drops
5096 * reference count on the block.
5097 *
5098 * when wc->stage == UPDATE_BACKREF, this function changes
5099 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5100 * to UPDATE_BACKREF previously while processing the block.
5101 *
5102 * NOTE: return value 1 means we should stop walking up.
5103 */
5104static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5105 struct btrfs_root *root,
5106 struct btrfs_path *path,
5107 struct walk_control *wc)
5108{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005109 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005110 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005111 int level = wc->level;
5112 struct extent_buffer *eb = path->nodes[level];
5113 u64 parent = 0;
5114
5115 if (wc->stage == UPDATE_BACKREF) {
5116 BUG_ON(wc->shared_level < level);
5117 if (level < wc->shared_level)
5118 goto out;
5119
Yan Zheng2c47e6052009-06-27 21:07:35 -04005120 ret = find_next_key(path, level + 1, &wc->update_progress);
5121 if (ret > 0)
5122 wc->update_ref = 0;
5123
5124 wc->stage = DROP_REFERENCE;
5125 wc->shared_level = -1;
5126 path->slots[level] = 0;
5127
5128 /*
5129 * check reference count again if the block isn't locked.
5130 * we should start walking down the tree again if reference
5131 * count is one.
5132 */
5133 if (!path->locks[level]) {
5134 BUG_ON(level == 0);
5135 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005136 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005137 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005138
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005139 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005140 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005141 &wc->refs[level],
5142 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005143 if (ret < 0) {
5144 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005145 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005146 return ret;
5147 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005148 BUG_ON(wc->refs[level] == 0);
5149 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005150 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005151 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005152 return 1;
5153 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005154 }
5155 }
5156
5157 /* wc->stage == DROP_REFERENCE */
5158 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5159
5160 if (wc->refs[level] == 1) {
5161 if (level == 0) {
5162 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005163 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005164 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005165 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005166 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005167 if (is_fstree(root->root_key.objectid)) {
5168 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5169 if (ret) {
5170 btrfs_err_rl(fs_info,
5171 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005172 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005173 }
Mark Fasheh11526512014-07-17 12:39:01 -07005174 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005175 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005176 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005177 if (!path->locks[level] &&
5178 btrfs_header_generation(eb) == trans->transid) {
5179 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005180 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005181 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005182 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005183 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005184 }
5185
5186 if (eb == root->node) {
5187 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5188 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005189 else if (root->root_key.objectid != btrfs_header_owner(eb))
5190 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005191 } else {
5192 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5193 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005194 else if (root->root_key.objectid !=
5195 btrfs_header_owner(path->nodes[level + 1]))
5196 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005197 }
5198
Jan Schmidt5581a512012-05-16 17:04:52 +02005199 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005200out:
5201 wc->refs[level] = 0;
5202 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005203 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005204
5205owner_mismatch:
5206 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5207 btrfs_header_owner(eb), root->root_key.objectid);
5208 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005209}
5210
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005211static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5212 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005213 struct btrfs_path *path,
5214 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005215{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005216 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005217 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005218 int ret;
5219
Yan Zheng2c47e6052009-06-27 21:07:35 -04005220 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005221 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005222 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005223 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005224
Yan Zheng2c47e6052009-06-27 21:07:35 -04005225 if (level == 0)
5226 break;
5227
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005228 if (path->slots[level] >=
5229 btrfs_header_nritems(path->nodes[level]))
5230 break;
5231
Yan, Zheng94fcca92009-10-09 09:25:16 -04005232 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005233 if (ret > 0) {
5234 path->slots[level]++;
5235 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005236 } else if (ret < 0)
5237 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005238 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005239 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005240 return 0;
5241}
5242
Chris Masond3977122009-01-05 21:25:51 -05005243static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005244 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005245 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005246 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005247{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005248 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005249 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005250
Yan Zheng2c47e6052009-06-27 21:07:35 -04005251 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5252 while (level < max_level && path->nodes[level]) {
5253 wc->level = level;
5254 if (path->slots[level] + 1 <
5255 btrfs_header_nritems(path->nodes[level])) {
5256 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005257 return 0;
5258 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005259 ret = walk_up_proc(trans, root, path, wc);
5260 if (ret > 0)
5261 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005262 if (ret < 0)
5263 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005264
Yan Zheng2c47e6052009-06-27 21:07:35 -04005265 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005266 btrfs_tree_unlock_rw(path->nodes[level],
5267 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005268 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005269 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005270 free_extent_buffer(path->nodes[level]);
5271 path->nodes[level] = NULL;
5272 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005273 }
5274 }
5275 return 1;
5276}
5277
Chris Mason9aca1d52007-03-13 11:09:37 -04005278/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005279 * drop a subvolume tree.
5280 *
5281 * this function traverses the tree freeing any blocks that only
5282 * referenced by the tree.
5283 *
5284 * when a shared tree block is found. this function decreases its
5285 * reference count by one. if update_ref is true, this function
5286 * also make sure backrefs for the shared block and all lower level
5287 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005288 *
5289 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005290 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04005291int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005292 struct btrfs_block_rsv *block_rsv, int update_ref,
5293 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005294{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005295 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005296 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005297 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005298 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005299 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005300 struct walk_control *wc;
5301 struct btrfs_key key;
5302 int err = 0;
5303 int ret;
5304 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005305 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005306
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005307 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005308
Chris Mason5caf2a02007-04-02 11:20:42 -04005309 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005310 if (!path) {
5311 err = -ENOMEM;
5312 goto out;
5313 }
Chris Mason20524f02007-03-10 06:35:47 -05005314
Yan Zheng2c47e6052009-06-27 21:07:35 -04005315 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005316 if (!wc) {
5317 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005318 err = -ENOMEM;
5319 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005320 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005321
Yan, Zhenga22285a2010-05-16 10:48:46 -04005322 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005323 if (IS_ERR(trans)) {
5324 err = PTR_ERR(trans);
5325 goto out_free;
5326 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005327
Josef Bacik0568e822018-11-30 11:52:14 -05005328 err = btrfs_run_delayed_items(trans);
5329 if (err)
5330 goto out_end_trans;
5331
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005332 if (block_rsv)
5333 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005334
Josef Bacik83354f02018-11-30 11:52:13 -05005335 /*
5336 * This will help us catch people modifying the fs tree while we're
5337 * dropping it. It is unsafe to mess with the fs tree while it's being
5338 * dropped as we unlock the root node and parent nodes as we walk down
5339 * the tree, assuming nothing will change. If something does change
5340 * then we'll have stale information and drop references to blocks we've
5341 * already dropped.
5342 */
5343 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005344 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005345 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005346 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005347 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005348 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005349 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005350 memset(&wc->update_progress, 0,
5351 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005352 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005353 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005354 memcpy(&wc->update_progress, &key,
5355 sizeof(wc->update_progress));
5356
Chris Mason6702ed42007-08-07 16:15:09 -04005357 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005358 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005359 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005360 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5361 path->lowest_level = 0;
5362 if (ret < 0) {
5363 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005364 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005365 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005366 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005367
Chris Mason7d9eb122008-07-08 14:19:17 -04005368 /*
5369 * unlock our path, this is safe because only this
5370 * function is allowed to delete this snapshot
5371 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005372 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005373
Yan Zheng2c47e6052009-06-27 21:07:35 -04005374 level = btrfs_header_level(root->node);
5375 while (1) {
5376 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005377 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005378 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005379
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005380 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005381 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005382 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005383 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005384 if (ret < 0) {
5385 err = ret;
5386 goto out_end_trans;
5387 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005388 BUG_ON(wc->refs[level] == 0);
5389
5390 if (level == root_item->drop_level)
5391 break;
5392
5393 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005394 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005395 WARN_ON(wc->refs[level] != 1);
5396 level--;
5397 }
5398 }
5399
Josef Bacik78c52d92019-02-06 15:46:14 -05005400 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005401 wc->level = level;
5402 wc->shared_level = -1;
5403 wc->stage = DROP_REFERENCE;
5404 wc->update_ref = update_ref;
5405 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005407
5408 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005409
Yan Zheng2c47e6052009-06-27 21:07:35 -04005410 ret = walk_down_tree(trans, root, path, wc);
5411 if (ret < 0) {
5412 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005413 break;
5414 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005415
5416 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5417 if (ret < 0) {
5418 err = ret;
5419 break;
5420 }
5421
5422 if (ret > 0) {
5423 BUG_ON(wc->stage != DROP_REFERENCE);
5424 break;
5425 }
5426
5427 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005428 wc->drop_level = wc->level;
5429 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5430 &wc->drop_progress,
5431 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005432 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005433 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5434 &wc->drop_progress);
5435 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005436
5437 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005438 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005439 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005440 ret = btrfs_update_root(trans, tree_root,
5441 &root->root_key,
5442 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005443 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005444 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005445 err = ret;
5446 goto out_end_trans;
5447 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005448
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005449 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005451 btrfs_debug(fs_info,
5452 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005453 err = -EAGAIN;
5454 goto out_free;
5455 }
5456
Yan, Zhenga22285a2010-05-16 10:48:46 -04005457 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005458 if (IS_ERR(trans)) {
5459 err = PTR_ERR(trans);
5460 goto out_free;
5461 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005462 if (block_rsv)
5463 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04005464 }
Chris Mason20524f02007-03-10 06:35:47 -05005465 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005466 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005467 if (err)
5468 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005469
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005470 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005471 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005472 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005473 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005474 goto out_end_trans;
5475 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005476
Yan, Zheng76dda932009-09-21 16:00:26 -04005477 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005478 ret = btrfs_find_root(tree_root, &root->root_key, path,
5479 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005480 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005481 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005482 err = ret;
5483 goto out_end_trans;
5484 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005485 /* if we fail to delete the orphan item this time
5486 * around, it'll get picked up the next time.
5487 *
5488 * The most common failure here is just -ENOENT.
5489 */
5490 btrfs_del_orphan_item(trans, tree_root,
5491 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005492 }
5493 }
5494
Miao Xie27cdeb72014-04-02 19:51:05 +08005495 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005496 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005497 } else {
5498 free_extent_buffer(root->node);
5499 free_extent_buffer(root->commit_root);
Josef Bacik00246522020-01-24 09:33:01 -05005500 btrfs_put_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005501 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04005502 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005503out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005504 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005505out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005506 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005507 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005508out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005509 /*
5510 * So if we need to stop dropping the snapshot for whatever reason we
5511 * need to make sure to add it back to the dead root list so that we
5512 * keep trying to do the work later. This also cleans up roots if we
5513 * don't have it in the radix (like when we recover after a power fail
5514 * or unmount) so we don't leak memory.
5515 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005516 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005517 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08005518 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005519 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005520 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005521}
Chris Mason9078a3e2007-04-26 16:46:15 -04005522
Yan Zheng2c47e6052009-06-27 21:07:35 -04005523/*
5524 * drop subtree rooted at tree block 'node'.
5525 *
5526 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005527 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005528 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005529int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5530 struct btrfs_root *root,
5531 struct extent_buffer *node,
5532 struct extent_buffer *parent)
5533{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005534 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005535 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005536 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005537 int level;
5538 int parent_level;
5539 int ret = 0;
5540 int wret;
5541
Yan Zheng2c47e6052009-06-27 21:07:35 -04005542 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5543
Yan Zhengf82d02d2008-10-29 14:49:05 -04005544 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005545 if (!path)
5546 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005547
Yan Zheng2c47e6052009-06-27 21:07:35 -04005548 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005549 if (!wc) {
5550 btrfs_free_path(path);
5551 return -ENOMEM;
5552 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005553
Chris Masonb9447ef82009-03-09 11:45:38 -04005554 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005555 parent_level = btrfs_header_level(parent);
David Sterba67439da2019-10-08 13:28:47 +02005556 atomic_inc(&parent->refs);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005557 path->nodes[parent_level] = parent;
5558 path->slots[parent_level] = btrfs_header_nritems(parent);
5559
Chris Masonb9447ef82009-03-09 11:45:38 -04005560 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005561 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005562 path->nodes[level] = node;
5563 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005564 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005565
5566 wc->refs[parent_level] = 1;
5567 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5568 wc->level = level;
5569 wc->shared_level = -1;
5570 wc->stage = DROP_REFERENCE;
5571 wc->update_ref = 0;
5572 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005573 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005574
5575 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005576 wret = walk_down_tree(trans, root, path, wc);
5577 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005578 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005579 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005580 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005581
Yan Zheng2c47e6052009-06-27 21:07:35 -04005582 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005583 if (wret < 0)
5584 ret = wret;
5585 if (wret != 0)
5586 break;
5587 }
5588
Yan Zheng2c47e6052009-06-27 21:07:35 -04005589 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005590 btrfs_free_path(path);
5591 return ret;
5592}
5593
Miao Xie6d07bce2011-01-05 10:07:31 +00005594/*
5595 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005596 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005597 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005598u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005599{
David Sterba32da53862019-10-29 19:20:18 +01005600 struct btrfs_block_group *block_group;
Miao Xie6d07bce2011-01-05 10:07:31 +00005601 u64 free_bytes = 0;
5602 int factor;
5603
Nicholas D Steeves01327612016-05-19 21:18:45 -04005604 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005605 if (list_empty(&sinfo->ro_bgs))
5606 return 0;
5607
5608 spin_lock(&sinfo->lock);
5609 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005610 spin_lock(&block_group->lock);
5611
5612 if (!block_group->ro) {
5613 spin_unlock(&block_group->lock);
5614 continue;
5615 }
5616
David Sterba46df06b2018-07-13 20:46:30 +02005617 factor = btrfs_bg_type_to_factor(block_group->flags);
David Sterbab3470b52019-10-23 18:48:22 +02005618 free_bytes += (block_group->length -
David Sterbabf38be62019-10-23 18:48:11 +02005619 block_group->used) * factor;
Miao Xie6d07bce2011-01-05 10:07:31 +00005620
5621 spin_unlock(&block_group->lock);
5622 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005623 spin_unlock(&sinfo->lock);
5624
5625 return free_bytes;
5626}
5627
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005628int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5629 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005630{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005631 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005632}
5633
Jeff Mahoney499f3772015-06-15 09:41:17 -04005634/*
5635 * It used to be that old block groups would be left around forever.
5636 * Iterating over them would be enough to trim unused space. Since we
5637 * now automatically remove them, we also need to iterate over unallocated
5638 * space.
5639 *
5640 * We don't want a transaction for this since the discard may take a
5641 * substantial amount of time. We don't require that a transaction be
5642 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005643 * to ensure that we're not discarding chunks that were released or
5644 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005645 *
5646 * Holding the chunks lock will prevent other threads from allocating
5647 * or releasing chunks, but it won't prevent a running transaction
5648 * from committing and releasing the memory that the pending chunks
5649 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005650 * transaction and hold the commit root sem. We only need to hold
5651 * it while performing the free space search since we have already
5652 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005653 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005654static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005655{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005656 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005657 int ret;
5658
5659 *trimmed = 0;
5660
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005661 /* Discard not supported = nothing to do. */
5662 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5663 return 0;
5664
Andrea Gelmini52042d82018-11-28 12:05:13 +01005665 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005666 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005667 return 0;
5668
5669 /* No free space = nothing to do. */
5670 if (device->total_bytes <= device->bytes_used)
5671 return 0;
5672
5673 ret = 0;
5674
5675 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005676 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005677 u64 bytes;
5678
5679 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5680 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005681 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005682
Nikolay Borisov929be172019-03-27 14:24:18 +02005683 find_first_clear_extent_bit(&device->alloc_state, start,
5684 &start, &end,
5685 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005686
5687 /* Ensure we skip the reserved area in the first 1M */
5688 start = max_t(u64, start, SZ_1M);
5689
Nikolay Borisov929be172019-03-27 14:24:18 +02005690 /*
5691 * If find_first_clear_extent_bit find a range that spans the
5692 * end of the device it will set end to -1, in this case it's up
5693 * to the caller to trim the value to the size of the device.
5694 */
5695 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005696
Nikolay Borisov929be172019-03-27 14:24:18 +02005697 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005698
Nikolay Borisov929be172019-03-27 14:24:18 +02005699 /* We didn't find any extents */
5700 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005701 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005702 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005703 break;
5704 }
5705
Nikolay Borisov929be172019-03-27 14:24:18 +02005706 ret = btrfs_issue_discard(device->bdev, start, len,
5707 &bytes);
5708 if (!ret)
5709 set_extent_bits(&device->alloc_state, start,
5710 start + bytes - 1,
5711 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005712 mutex_unlock(&fs_info->chunk_mutex);
5713
5714 if (ret)
5715 break;
5716
5717 start += len;
5718 *trimmed += bytes;
5719
5720 if (fatal_signal_pending(current)) {
5721 ret = -ERESTARTSYS;
5722 break;
5723 }
5724
5725 cond_resched();
5726 }
5727
5728 return ret;
5729}
5730
Qu Wenruo93bba242018-09-07 14:16:23 +08005731/*
5732 * Trim the whole filesystem by:
5733 * 1) trimming the free space in each block group
5734 * 2) trimming the unallocated space on each device
5735 *
5736 * This will also continue trimming even if a block group or device encounters
5737 * an error. The return value will be the last error, or 0 if nothing bad
5738 * happens.
5739 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005740int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005741{
David Sterba32da53862019-10-29 19:20:18 +01005742 struct btrfs_block_group *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005743 struct btrfs_device *device;
5744 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005745 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005746 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005747 u64 start;
5748 u64 end;
5749 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005750 u64 bg_failed = 0;
5751 u64 dev_failed = 0;
5752 int bg_ret = 0;
5753 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005754 int ret = 0;
5755
Qu Wenruo07301df2019-05-28 16:21:54 +08005756 /*
5757 * Check range overflow if range->len is set.
5758 * The default range->len is U64_MAX.
5759 */
5760 if (range->len != U64_MAX &&
5761 check_add_overflow(range->start, range->len, &range_end))
5762 return -EINVAL;
5763
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005764 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005765 for (; cache; cache = btrfs_next_block_group(cache)) {
David Sterbab3470b52019-10-23 18:48:22 +02005766 if (cache->start >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005767 btrfs_put_block_group(cache);
5768 break;
5769 }
5770
David Sterbab3470b52019-10-23 18:48:22 +02005771 start = max(range->start, cache->start);
5772 end = min(range_end, cache->start + cache->length);
Li Dongyangf7039b12011-03-24 10:24:28 +00005773
5774 if (end - start >= range->minlen) {
David Sterba32da53862019-10-29 19:20:18 +01005775 if (!btrfs_block_group_done(cache)) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005776 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005777 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005778 bg_failed++;
5779 bg_ret = ret;
5780 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005781 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005782 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005783 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005784 bg_failed++;
5785 bg_ret = ret;
5786 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005787 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005788 }
5789 ret = btrfs_trim_block_group(cache,
5790 &group_trimmed,
5791 start,
5792 end,
5793 range->minlen);
5794
5795 trimmed += group_trimmed;
5796 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005797 bg_failed++;
5798 bg_ret = ret;
5799 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005800 }
5801 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005802 }
5803
Qu Wenruo93bba242018-09-07 14:16:23 +08005804 if (bg_failed)
5805 btrfs_warn(fs_info,
5806 "failed to trim %llu block group(s), last error %d",
5807 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005808 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005809 devices = &fs_info->fs_devices->devices;
5810 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03005811 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005812 if (ret) {
5813 dev_failed++;
5814 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005815 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005816 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005817
5818 trimmed += group_trimmed;
5819 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005821
Qu Wenruo93bba242018-09-07 14:16:23 +08005822 if (dev_failed)
5823 btrfs_warn(fs_info,
5824 "failed to trim %llu device(s), last error %d",
5825 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005826 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005827 if (bg_ret)
5828 return bg_ret;
5829 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005830}