blob: dfa88a795c3fe79df5999d9b1e5975cecf271de0 [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
1192static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 struct btrfs_path *path,
1194 u64 bytenr, u64 parent, u64 root_objectid,
1195 u64 owner, u64 offset, int refs_to_add)
1196{
1197 int ret;
1198 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1199 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001200 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1201 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001202 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001203 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1204 root_objectid, owner, offset,
1205 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 }
1207 return ret;
1208}
1209
1210static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001211 struct btrfs_path *path,
1212 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001213 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001215 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001216
1217 BUG_ON(!is_data && refs_to_drop != 1);
1218 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001219 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1220 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001221 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001222 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001223 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001224 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001225 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001226 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001227 }
1228 return ret;
1229}
1230
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001231static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1232 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001233{
Jeff Mahoney86557862015-06-15 09:41:16 -04001234 int j, ret = 0;
1235 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001236 u64 aligned_start = ALIGN(start, 1 << 9);
1237
1238 if (WARN_ON(start != aligned_start)) {
1239 len -= aligned_start - start;
1240 len = round_down(len, 1 << 9);
1241 start = aligned_start;
1242 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001243
1244 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001245
1246 if (!len)
1247 return 0;
1248
1249 end = start + len;
1250 bytes_left = len;
1251
1252 /* Skip any superblocks on this device. */
1253 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1254 u64 sb_start = btrfs_sb_offset(j);
1255 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1256 u64 size = sb_start - start;
1257
1258 if (!in_range(sb_start, start, bytes_left) &&
1259 !in_range(sb_end, start, bytes_left) &&
1260 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1261 continue;
1262
1263 /*
1264 * Superblock spans beginning of range. Adjust start and
1265 * try again.
1266 */
1267 if (sb_start <= start) {
1268 start += sb_end - start;
1269 if (start > end) {
1270 bytes_left = 0;
1271 break;
1272 }
1273 bytes_left = end - start;
1274 continue;
1275 }
1276
1277 if (size) {
1278 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1279 GFP_NOFS, 0);
1280 if (!ret)
1281 *discarded_bytes += size;
1282 else if (ret != -EOPNOTSUPP)
1283 return ret;
1284 }
1285
1286 start = sb_end;
1287 if (start > end) {
1288 bytes_left = 0;
1289 break;
1290 }
1291 bytes_left = end - start;
1292 }
1293
1294 if (bytes_left) {
1295 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001296 GFP_NOFS, 0);
1297 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001298 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001299 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001300 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001301}
Chris Mason15916de2008-11-19 21:17:22 -05001302
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001303int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001304 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001305{
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001306 int ret = 0;
Li Dongyang5378e602011-03-24 10:24:27 +00001307 u64 discarded_bytes = 0;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001308 u64 end = bytenr + num_bytes;
1309 u64 cur = bytenr;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001310 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001311
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001312
Filipe Manana29992412016-05-27 17:42:05 +01001313 /*
1314 * Avoid races with device replace and make sure our bbio has devices
1315 * associated to its stripes that don't go away while we are discarding.
1316 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001317 btrfs_bio_counter_inc_blocked(fs_info);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001318 while (cur < end) {
1319 struct btrfs_bio_stripe *stripe;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001320 int i;
1321
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001322 num_bytes = end - cur;
1323 /* Tell the block device(s) that the sectors can be discarded */
1324 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1325 &num_bytes, &bbio, 0);
1326 /*
1327 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1328 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1329 * thus we can't continue anyway.
1330 */
1331 if (ret < 0)
1332 goto out;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001333
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001334 stripe = bbio->stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001335 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001336 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001337 struct request_queue *req_q;
1338
Filipe Manana627e0872018-01-30 18:40:22 +00001339 if (!stripe->dev->bdev) {
1340 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1341 continue;
1342 }
Anand Jain38b5f682017-11-29 18:53:43 +08001343 req_q = bdev_get_queue(stripe->dev->bdev);
1344 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001345 continue;
1346
Li Dongyang5378e602011-03-24 10:24:27 +00001347 ret = btrfs_issue_discard(stripe->dev->bdev,
1348 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001349 stripe->length,
1350 &bytes);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001351 if (!ret) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001352 discarded_bytes += bytes;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001353 } else if (ret != -EOPNOTSUPP) {
1354 /*
1355 * Logic errors or -ENOMEM, or -EIO, but
1356 * unlikely to happen.
1357 *
1358 * And since there are two loops, explicitly
1359 * go to out to avoid confusion.
1360 */
1361 btrfs_put_bbio(bbio);
1362 goto out;
1363 }
Josef Bacikd5e20032011-08-04 14:52:27 +00001364
1365 /*
1366 * Just in case we get back EOPNOTSUPP for some reason,
1367 * just ignore the return value so we don't screw up
1368 * people calling discard_extent.
1369 */
1370 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001371 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001372 btrfs_put_bbio(bbio);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001373 cur += num_bytes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001374 }
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001375out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001376 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001377
1378 if (actual_bytes)
1379 *actual_bytes = discarded_bytes;
1380
Liu Hui1f3c79a2009-01-05 15:57:51 -05001381
David Woodhouse53b381b2013-01-29 18:40:14 -05001382 if (ret == -EOPNOTSUPP)
1383 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001384 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001385}
1386
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001388int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001389 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001390{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001391 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001392 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001393 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001394
Qu Wenruo82fa1132019-04-04 14:45:35 +08001395 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1396 generic_ref->action);
1397 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1398 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001399
Qu Wenruo82fa1132019-04-04 14:45:35 +08001400 if (generic_ref->type == BTRFS_REF_METADATA)
1401 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001402 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001403 else
1404 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001405 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001406
Qu Wenruo82fa1132019-04-04 14:45:35 +08001407 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001408
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001409 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001410 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001411
Zheng Yan31840ae2008-09-23 13:14:14 -04001412 return ret;
1413}
1414
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001415/*
1416 * __btrfs_inc_extent_ref - insert backreference for a given extent
1417 *
1418 * @trans: Handle of transaction
1419 *
1420 * @node: The delayed ref node used to get the bytenr/length for
1421 * extent whose references are incremented.
1422 *
1423 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1424 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1425 * bytenr of the parent block. Since new extents are always
1426 * created with indirect references, this will only be the case
1427 * when relocating a shared extent. In that case, root_objectid
1428 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1429 * be 0
1430 *
1431 * @root_objectid: The id of the root where this modification has originated,
1432 * this can be either one of the well-known metadata trees or
1433 * the subvolume id which references this extent.
1434 *
1435 * @owner: For data extents it is the inode number of the owning file.
1436 * For metadata extents this parameter holds the level in the
1437 * tree of the extent.
1438 *
1439 * @offset: For metadata extents the offset is ignored and is currently
1440 * always passed as 0. For data extents it is the fileoffset
1441 * this extent belongs to.
1442 *
1443 * @refs_to_add Number of references to add
1444 *
1445 * @extent_op Pointer to a structure, holding information necessary when
1446 * updating a tree block's flags
1447 *
1448 */
Chris Mason925baed2008-06-25 16:01:30 -04001449static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001450 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451 u64 parent, u64 root_objectid,
1452 u64 owner, u64 offset, int refs_to_add,
1453 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001454{
Chris Mason5caf2a02007-04-02 11:20:42 -04001455 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001456 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001457 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001458 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001459 u64 bytenr = node->bytenr;
1460 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001461 u64 refs;
1462 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001463
Chris Mason5caf2a02007-04-02 11:20:42 -04001464 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001465 if (!path)
1466 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001467
David Sterbae4058b52015-11-27 16:31:35 +01001468 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001469 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001470 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001471 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1472 parent, root_objectid, owner,
1473 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001474 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001475 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001476
1477 /*
1478 * Ok we had -EAGAIN which means we didn't have space to insert and
1479 * inline extent ref, so just update the reference count and add a
1480 * normal backref.
1481 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001482 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001483 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1485 refs = btrfs_extent_refs(leaf, item);
1486 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1487 if (extent_op)
1488 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001489
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001490 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001491 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001492
David Sterbae4058b52015-11-27 16:31:35 +01001493 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001494 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001495 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03001496 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
1497 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001498 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001499 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001500out:
Chris Mason74493f72007-12-11 09:25:06 -05001501 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001502 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001503}
1504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001505static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 struct btrfs_delayed_ref_node *node,
1507 struct btrfs_delayed_extent_op *extent_op,
1508 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001509{
Chris Mason56bec292009-03-13 10:10:06 -04001510 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001511 struct btrfs_delayed_data_ref *ref;
1512 struct btrfs_key ins;
1513 u64 parent = 0;
1514 u64 ref_root = 0;
1515 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001516
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001517 ins.objectid = node->bytenr;
1518 ins.offset = node->num_bytes;
1519 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001522 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1525 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001526 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527
1528 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001529 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001530 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001531 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1532 flags, ref->objectid,
1533 ref->offset, &ins,
1534 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001535 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001536 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1537 ref->objectid, ref->offset,
1538 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001540 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541 ref_root, ref->objectid,
1542 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001543 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001544 } else {
1545 BUG();
1546 }
Chris Mason56bec292009-03-13 10:10:06 -04001547 return ret;
1548}
1549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001550static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1551 struct extent_buffer *leaf,
1552 struct btrfs_extent_item *ei)
1553{
1554 u64 flags = btrfs_extent_flags(leaf, ei);
1555 if (extent_op->update_flags) {
1556 flags |= extent_op->flags_to_set;
1557 btrfs_set_extent_flags(leaf, ei, flags);
1558 }
1559
1560 if (extent_op->update_key) {
1561 struct btrfs_tree_block_info *bi;
1562 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1563 bi = (struct btrfs_tree_block_info *)(ei + 1);
1564 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1565 }
1566}
1567
1568static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001569 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001570 struct btrfs_delayed_extent_op *extent_op)
1571{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001572 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001573 struct btrfs_key key;
1574 struct btrfs_path *path;
1575 struct btrfs_extent_item *ei;
1576 struct extent_buffer *leaf;
1577 u32 item_size;
1578 int ret;
1579 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001580 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001581
David Sterbabf31f872020-02-05 17:34:34 +01001582 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001583 return 0;
1584
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001585 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001586 metadata = 0;
1587
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001588 path = btrfs_alloc_path();
1589 if (!path)
1590 return -ENOMEM;
1591
Josef Bacikd2788502017-09-29 15:43:57 -04001592 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001593
Josef Bacik3173a182013-03-07 14:22:04 -05001594 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001595 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001596 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001597 } else {
1598 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001599 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001600 }
1601
1602again:
David Sterbae4058b52015-11-27 16:31:35 +01001603 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001604 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001605 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001606 if (ret < 0) {
1607 err = ret;
1608 goto out;
1609 }
1610 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001611 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001612 if (path->slots[0] > 0) {
1613 path->slots[0]--;
1614 btrfs_item_key_to_cpu(path->nodes[0], &key,
1615 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001616 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001617 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001618 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001619 ret = 0;
1620 }
1621 if (ret > 0) {
1622 btrfs_release_path(path);
1623 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001624
Josef Bacikd2788502017-09-29 15:43:57 -04001625 key.objectid = head->bytenr;
1626 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001627 key.type = BTRFS_EXTENT_ITEM_KEY;
1628 goto again;
1629 }
1630 } else {
1631 err = -EIO;
1632 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001633 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001634 }
1635
1636 leaf = path->nodes[0];
1637 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001638
David Sterba6d8ff4e2018-06-26 16:20:59 +02001639 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001640 err = -EINVAL;
1641 btrfs_print_v0_err(fs_info);
1642 btrfs_abort_transaction(trans, err);
1643 goto out;
1644 }
1645
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1647 __run_delayed_extent_op(extent_op, leaf, ei);
1648
1649 btrfs_mark_buffer_dirty(leaf);
1650out:
1651 btrfs_free_path(path);
1652 return err;
1653}
1654
1655static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001656 struct btrfs_delayed_ref_node *node,
1657 struct btrfs_delayed_extent_op *extent_op,
1658 int insert_reserved)
1659{
1660 int ret = 0;
1661 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662 u64 parent = 0;
1663 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001664
1665 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001666 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1669 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001670 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001671
Liu Bo02794222016-09-14 19:19:05 -07001672 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001673 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001674 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1675 node->bytenr, node->ref_mod, node->action, ref_root,
1676 parent);
1677 return -EIO;
1678 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001679 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001680 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001681 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001683 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1684 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001686 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001687 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 } else {
1689 BUG();
1690 }
1691 return ret;
1692}
1693
Chris Mason56bec292009-03-13 10:10:06 -04001694/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001695static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 struct btrfs_delayed_ref_node *node,
1697 struct btrfs_delayed_extent_op *extent_op,
1698 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001699{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001700 int ret = 0;
1701
David Sterbabf31f872020-02-05 17:34:34 +01001702 if (TRANS_ABORTED(trans)) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04001703 if (insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001704 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001705 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001706 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001707
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1709 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001710 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001711 insert_reserved);
1712 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1713 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001714 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 insert_reserved);
1716 else
1717 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001718 if (ret && insert_reserved)
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001719 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001721}
1722
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001723static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001724select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001725{
Filipe Mananacffc3372015-07-09 13:13:44 +01001726 struct btrfs_delayed_ref_node *ref;
1727
Liu Boe3d03962018-08-23 03:51:50 +08001728 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001729 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001730
Filipe Mananacffc3372015-07-09 13:13:44 +01001731 /*
1732 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1733 * This is to prevent a ref count from going down to zero, which deletes
1734 * the extent item from the extent tree, when there still are references
1735 * to add, which would fail because they would not find the extent item.
1736 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001737 if (!list_empty(&head->ref_add_list))
1738 return list_first_entry(&head->ref_add_list,
1739 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001740
Liu Boe3d03962018-08-23 03:51:50 +08001741 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001742 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001743 ASSERT(list_empty(&ref->add_list));
1744 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001745}
1746
Josef Bacik2eadaa22017-09-29 15:43:52 -04001747static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1748 struct btrfs_delayed_ref_head *head)
1749{
1750 spin_lock(&delayed_refs->lock);
1751 head->processing = 0;
1752 delayed_refs->num_heads_ready++;
1753 spin_unlock(&delayed_refs->lock);
1754 btrfs_delayed_ref_unlock(head);
1755}
1756
Josef Bacikbedc66172018-12-03 10:20:31 -05001757static struct btrfs_delayed_extent_op *cleanup_extent_op(
1758 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001759{
1760 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001761
1762 if (!extent_op)
1763 return NULL;
1764
1765 if (head->must_insert_reserved) {
1766 head->extent_op = NULL;
1767 btrfs_free_delayed_extent_op(extent_op);
1768 return NULL;
1769 }
1770 return extent_op;
1771}
1772
1773static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1774 struct btrfs_delayed_ref_head *head)
1775{
1776 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001777 int ret;
1778
Josef Bacikbedc66172018-12-03 10:20:31 -05001779 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001780 if (!extent_op)
1781 return 0;
1782 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001783 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001784 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001785 btrfs_free_delayed_extent_op(extent_op);
1786 return ret ? ret : 1;
1787}
1788
Josef Bacik31890da2018-11-21 14:05:41 -05001789void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1790 struct btrfs_delayed_ref_root *delayed_refs,
1791 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001792{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001793 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001794
1795 if (head->total_ref_mod < 0) {
1796 struct btrfs_space_info *space_info;
1797 u64 flags;
1798
1799 if (head->is_data)
1800 flags = BTRFS_BLOCK_GROUP_DATA;
1801 else if (head->is_system)
1802 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1803 else
1804 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacik280c29082019-06-18 16:09:19 -04001805 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05001806 ASSERT(space_info);
1807 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1808 -head->num_bytes,
1809 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1810
Josef Bacikba2c4d42018-12-03 10:20:33 -05001811 /*
1812 * We had csum deletions accounted for in our delayed refs rsv,
1813 * we need to drop the csum leaves for this update from our
1814 * delayed_refs_rsv.
1815 */
Josef Bacik07c47772018-12-03 10:20:30 -05001816 if (head->is_data) {
1817 spin_lock(&delayed_refs->lock);
1818 delayed_refs->pending_csums -= head->num_bytes;
1819 spin_unlock(&delayed_refs->lock);
Josef Bacikba2c4d42018-12-03 10:20:33 -05001820 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1821 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001822 }
1823 }
1824
Josef Bacikba2c4d42018-12-03 10:20:33 -05001825 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001826}
1827
Josef Bacik194ab0b2017-09-29 15:43:54 -04001828static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001829 struct btrfs_delayed_ref_head *head)
1830{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001831
1832 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001833 struct btrfs_delayed_ref_root *delayed_refs;
1834 int ret;
1835
1836 delayed_refs = &trans->transaction->delayed_refs;
1837
Josef Bacikbedc66172018-12-03 10:20:31 -05001838 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001839 if (ret < 0) {
1840 unselect_delayed_ref_head(delayed_refs, head);
1841 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1842 return ret;
1843 } else if (ret) {
1844 return ret;
1845 }
1846
1847 /*
1848 * Need to drop our head ref lock and re-acquire the delayed ref lock
1849 * and then re-check to make sure nobody got added.
1850 */
1851 spin_unlock(&head->lock);
1852 spin_lock(&delayed_refs->lock);
1853 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001854 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001855 spin_unlock(&head->lock);
1856 spin_unlock(&delayed_refs->lock);
1857 return 1;
1858 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001859 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001860 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001861 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001862
Josef Bacikc1103f72017-09-29 15:43:56 -04001863 if (head->must_insert_reserved) {
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02001864 btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001865 if (head->is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00001866 ret = btrfs_del_csums(trans, fs_info->csum_root,
1867 head->bytenr, head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001868 }
1869 }
1870
Josef Bacik31890da2018-11-21 14:05:41 -05001871 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001872
1873 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001874 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001875 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001876 return 0;
1877}
1878
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001879static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1880 struct btrfs_trans_handle *trans)
1881{
1882 struct btrfs_delayed_ref_root *delayed_refs =
1883 &trans->transaction->delayed_refs;
1884 struct btrfs_delayed_ref_head *head = NULL;
1885 int ret;
1886
1887 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001888 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001889 if (!head) {
1890 spin_unlock(&delayed_refs->lock);
1891 return head;
1892 }
1893
1894 /*
1895 * Grab the lock that says we are going to process all the refs for
1896 * this head
1897 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001898 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001899 spin_unlock(&delayed_refs->lock);
1900
1901 /*
1902 * We may have dropped the spin lock to get the head mutex lock, and
1903 * that might have given someone else time to free the head. If that's
1904 * true, it has been removed from our list and we can move on.
1905 */
1906 if (ret == -EAGAIN)
1907 head = ERR_PTR(-EAGAIN);
1908
1909 return head;
1910}
1911
Nikolay Borisove7261382018-08-15 10:39:55 +03001912static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1913 struct btrfs_delayed_ref_head *locked_ref,
1914 unsigned long *run_refs)
1915{
1916 struct btrfs_fs_info *fs_info = trans->fs_info;
1917 struct btrfs_delayed_ref_root *delayed_refs;
1918 struct btrfs_delayed_extent_op *extent_op;
1919 struct btrfs_delayed_ref_node *ref;
1920 int must_insert_reserved = 0;
1921 int ret;
1922
1923 delayed_refs = &trans->transaction->delayed_refs;
1924
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001925 lockdep_assert_held(&locked_ref->mutex);
1926 lockdep_assert_held(&locked_ref->lock);
1927
Nikolay Borisove7261382018-08-15 10:39:55 +03001928 while ((ref = select_delayed_ref(locked_ref))) {
1929 if (ref->seq &&
1930 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1931 spin_unlock(&locked_ref->lock);
1932 unselect_delayed_ref_head(delayed_refs, locked_ref);
1933 return -EAGAIN;
1934 }
1935
1936 (*run_refs)++;
1937 ref->in_tree = 0;
1938 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1939 RB_CLEAR_NODE(&ref->ref_node);
1940 if (!list_empty(&ref->add_list))
1941 list_del(&ref->add_list);
1942 /*
1943 * When we play the delayed ref, also correct the ref_mod on
1944 * head
1945 */
1946 switch (ref->action) {
1947 case BTRFS_ADD_DELAYED_REF:
1948 case BTRFS_ADD_DELAYED_EXTENT:
1949 locked_ref->ref_mod -= ref->ref_mod;
1950 break;
1951 case BTRFS_DROP_DELAYED_REF:
1952 locked_ref->ref_mod += ref->ref_mod;
1953 break;
1954 default:
1955 WARN_ON(1);
1956 }
1957 atomic_dec(&delayed_refs->num_entries);
1958
1959 /*
1960 * Record the must_insert_reserved flag before we drop the
1961 * spin lock.
1962 */
1963 must_insert_reserved = locked_ref->must_insert_reserved;
1964 locked_ref->must_insert_reserved = 0;
1965
1966 extent_op = locked_ref->extent_op;
1967 locked_ref->extent_op = NULL;
1968 spin_unlock(&locked_ref->lock);
1969
1970 ret = run_one_delayed_ref(trans, ref, extent_op,
1971 must_insert_reserved);
1972
1973 btrfs_free_delayed_extent_op(extent_op);
1974 if (ret) {
1975 unselect_delayed_ref_head(delayed_refs, locked_ref);
1976 btrfs_put_delayed_ref(ref);
1977 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1978 ret);
1979 return ret;
1980 }
1981
1982 btrfs_put_delayed_ref(ref);
1983 cond_resched();
1984
1985 spin_lock(&locked_ref->lock);
1986 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1987 }
1988
1989 return 0;
1990}
1991
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001992/*
1993 * Returns 0 on success or if called with an already aborted transaction.
1994 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
1995 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05001996static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05001997 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04001998{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02001999 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002000 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002001 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002002 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002003 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002004 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002005 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002006
2007 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002008 do {
Chris Mason56bec292009-03-13 10:10:06 -04002009 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002010 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002011 if (IS_ERR_OR_NULL(locked_ref)) {
2012 if (PTR_ERR(locked_ref) == -EAGAIN) {
2013 continue;
2014 } else {
2015 break;
2016 }
Chris Mason56bec292009-03-13 10:10:06 -04002017 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002018 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002019 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002020 /*
2021 * We need to try and merge add/drops of the same ref since we
2022 * can run into issues with relocate dropping the implicit ref
2023 * and then it being added back again before the drop can
2024 * finish. If we merged anything we need to re-loop so we can
2025 * get a good ref.
2026 * Or we can get node references of the same type that weren't
2027 * merged when created due to bumps in the tree mod seq, and
2028 * we need to merge them to prevent adding an inline extent
2029 * backref before dropping it (triggering a BUG_ON at
2030 * insert_inline_extent_backref()).
2031 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002032 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002033 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002034
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002035 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2036 &actual_count);
2037 if (ret < 0 && ret != -EAGAIN) {
2038 /*
2039 * Error, btrfs_run_delayed_refs_for_head already
2040 * unlocked everything so just bail out
2041 */
2042 return ret;
2043 } else if (!ret) {
2044 /*
2045 * Success, perform the usual cleanup of a processed
2046 * head
2047 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002048 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002049 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002050 /* We dropped our lock, we need to loop. */
2051 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002052 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002053 } else if (ret) {
2054 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002055 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002056 }
Chris Mason56bec292009-03-13 10:10:06 -04002057
Josef Bacikb00e6252017-09-29 15:43:53 -04002058 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002059 * Either success case or btrfs_run_delayed_refs_for_head
2060 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002061 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002062
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002063 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002064 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002065 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002066
2067 /*
2068 * We don't want to include ref heads since we can have empty ref heads
2069 * and those will drastically skew our runtime down since we just do
2070 * accounting, no actual extent tree updates.
2071 */
2072 if (actual_count > 0) {
2073 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2074 u64 avg;
2075
2076 /*
2077 * We weigh the current average higher than our current runtime
2078 * to avoid large swings in the average.
2079 */
2080 spin_lock(&delayed_refs->lock);
2081 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002082 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002083 spin_unlock(&delayed_refs->lock);
2084 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002085 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002086}
2087
Arne Jansen709c0482011-09-12 12:22:57 +02002088#ifdef SCRAMBLE_DELAYED_REFS
2089/*
2090 * Normally delayed refs get processed in ascending bytenr order. This
2091 * correlates in most cases to the order added. To expose dependencies on this
2092 * order, we start to process the tree in the middle instead of the beginning
2093 */
2094static u64 find_middle(struct rb_root *root)
2095{
2096 struct rb_node *n = root->rb_node;
2097 struct btrfs_delayed_ref_node *entry;
2098 int alt = 1;
2099 u64 middle;
2100 u64 first = 0, last = 0;
2101
2102 n = rb_first(root);
2103 if (n) {
2104 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2105 first = entry->bytenr;
2106 }
2107 n = rb_last(root);
2108 if (n) {
2109 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2110 last = entry->bytenr;
2111 }
2112 n = root->rb_node;
2113
2114 while (n) {
2115 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2116 WARN_ON(!entry->in_tree);
2117
2118 middle = entry->bytenr;
2119
2120 if (alt)
2121 n = n->rb_left;
2122 else
2123 n = n->rb_right;
2124
2125 alt = 1 - alt;
2126 }
2127 return middle;
2128}
2129#endif
2130
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002131static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002132{
2133 u64 num_bytes;
2134
2135 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2136 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002137 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002138 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2139
2140 /*
2141 * 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 -04002142 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002143 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002144 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002145}
2146
Josef Bacik12621332015-02-03 07:50:16 -08002147/*
2148 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2149 * would require to store the csums for that many bytes.
2150 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002151u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002152{
2153 u64 csum_size;
2154 u64 num_csums_per_leaf;
2155 u64 num_csums;
2156
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002157 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002158 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002159 (u64)btrfs_super_csum_size(fs_info->super_copy));
2160 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002161 num_csums += num_csums_per_leaf - 1;
2162 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2163 return num_csums;
2164}
2165
Chris Masonc3e69d52009-03-13 10:17:05 -04002166/*
2167 * this starts processing the delayed reference count updates and
2168 * extent insertions we have queued up so far. count can be
2169 * 0, which means to process everything in the tree at the start
2170 * of the run (but not newly added entries), or it can be some target
2171 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002172 *
2173 * Returns 0 on success or if called with an aborted transaction
2174 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002175 */
2176int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002177 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002178{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002179 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002180 struct rb_node *node;
2181 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002182 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002183 int ret;
2184 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002185
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002186 /* We'll clean this up in btrfs_cleanup_transaction */
David Sterbabf31f872020-02-05 17:34:34 +01002187 if (TRANS_ABORTED(trans))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002188 return 0;
2189
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002191 return 0;
2192
Chris Masonc3e69d52009-03-13 10:17:05 -04002193 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002194 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002195 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002196
Chris Masonc3e69d52009-03-13 10:17:05 -04002197again:
Arne Jansen709c0482011-09-12 12:22:57 +02002198#ifdef SCRAMBLE_DELAYED_REFS
2199 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2200#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002201 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002202 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002203 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002204 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002205 }
2206
Chris Mason56bec292009-03-13 10:10:06 -04002207 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002208 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002209
Josef Bacikd7df2c72014-01-23 09:21:38 -05002210 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002211 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002212 if (!node) {
2213 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002214 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002215 }
Josef Bacikd2788502017-09-29 15:43:57 -04002216 head = rb_entry(node, struct btrfs_delayed_ref_head,
2217 href_node);
2218 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002219 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002220
2221 /* Mutex was contended, block until it's released and retry. */
2222 mutex_lock(&head->mutex);
2223 mutex_unlock(&head->mutex);
2224
2225 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002226 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002227 goto again;
2228 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002229out:
Chris Masona28ec192007-03-06 20:08:01 -05002230 return 0;
2231}
2232
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002233int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
David Sterba42c9d0b2019-03-20 11:54:13 +01002234 struct extent_buffer *eb, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002235 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002236{
2237 struct btrfs_delayed_extent_op *extent_op;
2238 int ret;
2239
Miao Xie78a61842012-11-21 02:21:28 +00002240 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 if (!extent_op)
2242 return -ENOMEM;
2243
2244 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002245 extent_op->update_flags = true;
2246 extent_op->update_key = false;
2247 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002248 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249
David Sterba42c9d0b2019-03-20 11:54:13 +01002250 ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002251 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002252 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002253 return ret;
2254}
2255
Liu Boe4c3b2d2017-01-30 12:25:28 -08002256static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002257 struct btrfs_path *path,
2258 u64 objectid, u64 offset, u64 bytenr)
2259{
2260 struct btrfs_delayed_ref_head *head;
2261 struct btrfs_delayed_ref_node *ref;
2262 struct btrfs_delayed_data_ref *data_ref;
2263 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002264 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002265 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002266 int ret = 0;
2267
ethanwu998ac6d2018-04-29 15:59:42 +08002268 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002269 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002270 if (cur_trans)
2271 refcount_inc(&cur_trans->use_count);
2272 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002273 if (!cur_trans)
2274 return 0;
2275
2276 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002278 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002279 if (!head) {
2280 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002281 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002282 return 0;
2283 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002284
2285 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002286 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287 spin_unlock(&delayed_refs->lock);
2288
David Sterbab3b4aa72011-04-21 01:20:15 +02002289 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290
David Sterba8cc33e52011-05-02 15:29:25 +02002291 /*
2292 * Mutex was contended, block until it's released and let
2293 * caller try again
2294 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295 mutex_lock(&head->mutex);
2296 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002297 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002298 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 return -EAGAIN;
2300 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002301 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002302
2303 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002304 /*
2305 * XXX: We should replace this with a proper search function in the
2306 * future.
2307 */
Liu Boe3d03962018-08-23 03:51:50 +08002308 for (node = rb_first_cached(&head->ref_tree); node;
2309 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002310 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002311 /* If it's a shared ref we know a cross reference exists */
2312 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2313 ret = 1;
2314 break;
2315 }
2316
2317 data_ref = btrfs_delayed_node_to_data_ref(ref);
2318
2319 /*
2320 * If our ref doesn't match the one we're currently looking at
2321 * then we have a cross reference.
2322 */
2323 if (data_ref->root != root->root_key.objectid ||
2324 data_ref->objectid != objectid ||
2325 data_ref->offset != offset) {
2326 ret = 1;
2327 break;
2328 }
2329 }
2330 spin_unlock(&head->lock);
2331 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002332 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002333 return ret;
2334}
2335
Liu Boe4c3b2d2017-01-30 12:25:28 -08002336static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337 struct btrfs_path *path,
2338 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002340 struct btrfs_fs_info *fs_info = root->fs_info;
2341 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002342 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002343 struct btrfs_extent_data_ref *ref;
2344 struct btrfs_extent_inline_ref *iref;
2345 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002346 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002348 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002349 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002350
Chris Masonbe20aa92007-12-17 20:14:01 -05002351 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002352 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002353 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002354
Chris Masonbe20aa92007-12-17 20:14:01 -05002355 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2356 if (ret < 0)
2357 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002358 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002359
2360 ret = -ENOENT;
2361 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002362 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002363
Zheng Yan31840ae2008-09-23 13:14:14 -04002364 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002365 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002366 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002367
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002368 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002369 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002370
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002371 ret = 1;
2372 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2374
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002375 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376 if (item_size != sizeof(*ei) +
2377 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2378 goto out;
2379
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002380 /* If extent created before last snapshot => it's definitely shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002381 if (btrfs_extent_generation(leaf, ei) <=
2382 btrfs_root_last_snapshot(&root->root_item))
2383 goto out;
2384
2385 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002386
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002387 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002388 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2389 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002390 goto out;
2391
2392 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2393 if (btrfs_extent_refs(leaf, ei) !=
2394 btrfs_extent_data_ref_count(leaf, ref) ||
2395 btrfs_extent_data_ref_root(leaf, ref) !=
2396 root->root_key.objectid ||
2397 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2398 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2399 goto out;
2400
Yan Zhengf321e492008-07-30 09:26:11 -04002401 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002402out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002403 return ret;
2404}
2405
Liu Boe4c3b2d2017-01-30 12:25:28 -08002406int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2407 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002408{
2409 struct btrfs_path *path;
2410 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002411
2412 path = btrfs_alloc_path();
2413 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002414 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415
2416 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002417 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418 offset, bytenr);
2419 if (ret && ret != -ENOENT)
2420 goto out;
2421
Misono Tomohiro380fd062018-08-30 10:59:16 +09002422 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2423 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002424
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425out:
Yan Zhengf321e492008-07-30 09:26:11 -04002426 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002427 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2428 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002429 return ret;
2430}
2431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002432static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002433 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002434 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002435 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002437 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002438 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002439 u64 num_bytes;
2440 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002441 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002442 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002443 struct btrfs_key key;
2444 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002445 struct btrfs_ref generic_ref = { 0 };
2446 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002447 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002448 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002449 int level;
2450 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002451
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002452 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002453 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002454
Zheng Yan31840ae2008-09-23 13:14:14 -04002455 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002456 nritems = btrfs_header_nritems(buf);
2457 level = btrfs_header_level(buf);
2458
Miao Xie27cdeb72014-04-02 19:51:05 +08002459 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002460 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002461
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 if (full_backref)
2463 parent = buf->start;
2464 else
2465 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002466 if (inc)
2467 action = BTRFS_ADD_DELAYED_REF;
2468 else
2469 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470
Zheng Yan31840ae2008-09-23 13:14:14 -04002471 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002472 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002473 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002474 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002475 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002476 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002477 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002478 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002479 BTRFS_FILE_EXTENT_INLINE)
2480 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002481 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2482 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002483 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002484
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002485 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2486 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002487 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2488 num_bytes, parent);
2489 generic_ref.real_root = root->root_key.objectid;
2490 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2491 key.offset);
2492 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002493 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002494 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002495 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002496 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002497 if (ret)
2498 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002499 } else {
2500 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002501 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002502 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2503 num_bytes, parent);
2504 generic_ref.real_root = root->root_key.objectid;
2505 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2506 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002507 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002508 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002509 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002510 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002511 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002512 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002513 }
2514 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002515 return 0;
2516fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002517 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002518}
2519
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002520int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002521 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002522{
Josef Bacike339a6b2014-07-02 10:54:25 -07002523 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002524}
Zheng Yan31840ae2008-09-23 13:14:14 -04002525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002527 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002528{
Josef Bacike339a6b2014-07-02 10:54:25 -07002529 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002530}
2531
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002532int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002533{
David Sterba32da53862019-10-29 19:20:18 +01002534 struct btrfs_block_group *block_group;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002535 int readonly = 0;
2536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002537 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002538 if (!block_group || block_group->ro)
2539 readonly = 1;
2540 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002541 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002542 return readonly;
2543}
2544
Jeff Mahoney1b868262017-05-17 11:38:35 -04002545static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002546{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002547 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002548 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002549 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002550
Yan, Zhengb742bb822010-05-16 10:46:24 -04002551 if (data)
2552 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002553 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002554 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2555 else
2556 flags = BTRFS_BLOCK_GROUP_METADATA;
2557
Josef Bacik878d7b62019-06-20 15:38:05 -04002558 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002559 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002560}
2561
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002562static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002563{
David Sterba32da53862019-10-29 19:20:18 +01002564 struct btrfs_block_group *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002565 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002566
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002567 spin_lock(&fs_info->block_group_cache_lock);
2568 bytenr = fs_info->first_logical_byte;
2569 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002570
2571 if (bytenr < (u64)-1)
2572 return bytenr;
2573
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002574 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002575 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002576 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002577
David Sterbab3470b52019-10-23 18:48:22 +02002578 bytenr = cache->start;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002579 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002580
2581 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002582}
2583
Nikolay Borisov6690d072020-01-20 16:09:14 +02002584static int pin_down_extent(struct btrfs_trans_handle *trans,
2585 struct btrfs_block_group *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002586 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002587{
David Sterbafdf08602019-03-20 12:12:32 +01002588 struct btrfs_fs_info *fs_info = cache->fs_info;
2589
Yan Zheng11833d62009-09-11 16:11:19 -04002590 spin_lock(&cache->space_info->lock);
2591 spin_lock(&cache->lock);
2592 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002593 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2594 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002595 if (reserved) {
2596 cache->reserved -= num_bytes;
2597 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002598 }
Yan Zheng11833d62009-09-11 16:11:19 -04002599 spin_unlock(&cache->lock);
2600 spin_unlock(&cache->space_info->lock);
2601
Ethan Liendec59fa2018-07-13 16:50:42 +08002602 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2603 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002604 set_extent_dirty(&trans->transaction->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002605 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002606 return 0;
2607}
Chris Mason9078a3e2007-04-26 16:46:15 -04002608
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002609int btrfs_pin_extent(struct btrfs_trans_handle *trans,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002610 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002611{
David Sterba32da53862019-10-29 19:20:18 +01002612 struct btrfs_block_group *cache;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002613
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02002614 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002615 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002616
Nikolay Borisov6690d072020-01-20 16:09:14 +02002617 pin_down_extent(trans, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002618
2619 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002620 return 0;
2621}
Zheng Yane8569812008-09-26 10:05:48 -04002622
Yan, Zhengf0486c62010-05-16 10:46:25 -04002623/*
Chris Masone688b7252011-10-31 20:52:39 -04002624 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002625 */
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002626int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
Chris Masone688b7252011-10-31 20:52:39 -04002627 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002628{
David Sterba32da53862019-10-29 19:20:18 +01002629 struct btrfs_block_group *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002630 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002631
Nikolay Borisovf2fb7292020-01-20 16:09:16 +02002632 btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes);
2633
Nikolay Borisov9fce5702020-01-20 16:09:13 +02002634 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002635 if (!cache)
2636 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002637
2638 /*
2639 * pull in the free space cache (if any) so that our pin
2640 * removes the free space from the cache. We have load_only set
2641 * to one because the slow code to read in the free extents does check
2642 * the pinned extents.
2643 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002644 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002645
Nikolay Borisov6690d072020-01-20 16:09:14 +02002646 pin_down_extent(trans, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002647
2648 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002649 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002650 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002651 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002652}
2653
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002654static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2655 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002656{
2657 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002658 struct btrfs_block_group *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002659 struct btrfs_caching_control *caching_ctl;
2660
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002661 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002662 if (!block_group)
2663 return -EINVAL;
2664
Josef Bacik676f1f72019-06-20 15:37:48 -04002665 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002666 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002667
2668 if (!caching_ctl) {
2669 /* Logic error */
David Sterba32da53862019-10-29 19:20:18 +01002670 BUG_ON(!btrfs_block_group_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002671 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2672 } else {
2673 mutex_lock(&caching_ctl->mutex);
2674
2675 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002676 ret = btrfs_add_excluded_extent(fs_info, start,
2677 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002678 } else if (start + num_bytes <= caching_ctl->progress) {
2679 ret = btrfs_remove_free_space(block_group,
2680 start, num_bytes);
2681 } else {
2682 num_bytes = caching_ctl->progress - start;
2683 ret = btrfs_remove_free_space(block_group,
2684 start, num_bytes);
2685 if (ret)
2686 goto out_lock;
2687
2688 num_bytes = (start + num_bytes) -
2689 caching_ctl->progress;
2690 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002691 ret = btrfs_add_excluded_extent(fs_info, start,
2692 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002693 }
2694out_lock:
2695 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002696 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002697 }
2698 btrfs_put_block_group(block_group);
2699 return ret;
2700}
2701
David Sterbabcdc4282019-03-20 12:14:33 +01002702int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002703{
David Sterbabcdc4282019-03-20 12:14:33 +01002704 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002705 struct btrfs_file_extent_item *item;
2706 struct btrfs_key key;
2707 int found_type;
2708 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002709 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002710
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002711 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002712 return 0;
2713
2714 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2715 btrfs_item_key_to_cpu(eb, &key, i);
2716 if (key.type != BTRFS_EXTENT_DATA_KEY)
2717 continue;
2718 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2719 found_type = btrfs_file_extent_type(eb, item);
2720 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2721 continue;
2722 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2723 continue;
2724 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2725 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002726 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2727 if (ret)
2728 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002729 }
2730
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002731 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002732}
2733
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002734static void
David Sterba32da53862019-10-29 19:20:18 +01002735btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002736{
2737 atomic_inc(&bg->reservations);
2738}
2739
David Sterba8b74c032017-02-10 19:20:56 +01002740void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04002741{
Yan Zheng11833d62009-09-11 16:11:19 -04002742 struct btrfs_caching_control *next;
2743 struct btrfs_caching_control *caching_ctl;
David Sterba32da53862019-10-29 19:20:18 +01002744 struct btrfs_block_group *cache;
Yan Zheng11833d62009-09-11 16:11:19 -04002745
Josef Bacik9e351cc2014-03-13 15:42:13 -04002746 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04002747
2748 list_for_each_entry_safe(caching_ctl, next,
2749 &fs_info->caching_block_groups, list) {
2750 cache = caching_ctl->block_group;
David Sterba32da53862019-10-29 19:20:18 +01002751 if (btrfs_block_group_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04002752 cache->last_byte_to_unpin = (u64)-1;
2753 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04002754 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04002755 } else {
2756 cache->last_byte_to_unpin = caching_ctl->progress;
2757 }
2758 }
2759
Josef Bacik9e351cc2014-03-13 15:42:13 -04002760 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002761
Josef Bacik67f9c222019-06-19 13:47:23 -04002762 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04002763}
2764
Josef Bacikc759c4e2015-10-02 15:25:10 -04002765/*
2766 * Returns the free cluster for the given space info and sets empty_cluster to
2767 * what it should be based on the mount options.
2768 */
2769static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002770fetch_cluster_info(struct btrfs_fs_info *fs_info,
2771 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002772{
2773 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002774
2775 *empty_cluster = 0;
2776 if (btrfs_mixed_space_info(space_info))
2777 return ret;
2778
Josef Bacikc759c4e2015-10-02 15:25:10 -04002779 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002780 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002781 if (btrfs_test_opt(fs_info, SSD))
2782 *empty_cluster = SZ_2M;
2783 else
Byongho Leeee221842015-12-15 01:42:10 +09002784 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002785 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2786 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2787 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002789 }
2790
2791 return ret;
2792}
2793
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002794static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2795 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002796 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002797{
David Sterba32da53862019-10-29 19:20:18 +01002798 struct btrfs_block_group *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002799 struct btrfs_space_info *space_info;
2800 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002801 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002802 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002803 u64 total_unpinned = 0;
2804 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002805 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002806
2807 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002808 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002809 if (!cache ||
David Sterbab3470b52019-10-23 18:48:22 +02002810 start >= cache->start + cache->length) {
Yan Zheng11833d62009-09-11 16:11:19 -04002811 if (cache)
2812 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002813 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002814 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002815 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002816
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002817 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002818 cache->space_info,
2819 &empty_cluster);
2820 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002821 }
2822
David Sterbab3470b52019-10-23 18:48:22 +02002823 len = cache->start + cache->length - start;
Yan Zheng11833d62009-09-11 16:11:19 -04002824 len = min(len, end + 1 - start);
2825
2826 if (start < cache->last_byte_to_unpin) {
2827 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00002828 if (return_free_space)
2829 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04002830 }
Josef Bacik25179202008-10-29 14:49:05 -04002831
Yan, Zhengf0486c62010-05-16 10:46:25 -04002832 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002833 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002834 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002835
Josef Bacikc759c4e2015-10-02 15:25:10 -04002836 /*
2837 * If this space cluster has been marked as fragmented and we've
2838 * unpinned enough in this block group to potentially allow a
2839 * cluster to be created inside of it go ahead and clear the
2840 * fragmented check.
2841 */
2842 if (cluster && cluster->fragmented &&
2843 total_unpinned > empty_cluster) {
2844 spin_lock(&cluster->lock);
2845 cluster->fragmented = 0;
2846 spin_unlock(&cluster->lock);
2847 }
2848
Josef Bacik7b398f82012-10-22 15:52:28 -04002849 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002850 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002851 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002852 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacik4f4db212015-09-29 11:40:47 -04002853 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08002854 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2855 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04002856 if (cache->ro) {
2857 space_info->bytes_readonly += len;
2858 readonly = true;
2859 }
Josef Bacik25179202008-10-29 14:49:05 -04002860 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002861 if (!readonly && return_free_space &&
2862 global_rsv->space_info == space_info) {
2863 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002864
Josef Bacik7b398f82012-10-22 15:52:28 -04002865 spin_lock(&global_rsv->lock);
2866 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002867 to_add = min(len, global_rsv->size -
2868 global_rsv->reserved);
2869 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002870 btrfs_space_info_update_bytes_may_use(fs_info,
2871 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002872 if (global_rsv->reserved >= global_rsv->size)
2873 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002874 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002875 }
2876 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002877 /* Add to any tickets we may have */
2878 if (len)
Josef Bacik18fa2282019-08-22 15:10:58 -04002879 btrfs_try_granting_tickets(fs_info,
2880 space_info);
Josef Bacik7b398f82012-10-22 15:52:28 -04002881 }
2882 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002883 }
2884
2885 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002886 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002887 return 0;
2888}
2889
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002890int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002891{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002892 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002893 struct btrfs_block_group *block_group, *tmp;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002894 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002895 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002896 u64 start;
2897 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002898 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002899
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002900 unpin = &trans->transaction->pinned_extents;
Yan Zheng11833d62009-09-11 16:11:19 -04002901
David Sterbabf31f872020-02-05 17:34:34 +01002902 while (!TRANS_ABORTED(trans)) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002903 struct extent_state *cached_state = NULL;
2904
Filipe Mananad4b450c2015-01-29 19:18:25 +00002905 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002906 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002907 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002908 if (ret) {
2909 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002910 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002911 }
Nikolay Borisovfe119a62020-01-20 16:09:18 +02002912 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
2913 clear_extent_bits(&fs_info->excluded_extents, start,
Nikolay Borisovf2fb7292020-01-20 16:09:16 +02002914 end, EXTENT_UPTODATE);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002915
Dennis Zhou46b27f52019-12-13 16:22:11 -08002916 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002917 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002918 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002919
Filipe Manana0e6ec382018-11-16 13:04:44 +00002920 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002921 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002922 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002923 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002924 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002925 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002926
Dennis Zhoua2309302020-01-02 16:26:35 -05002927 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
2928 btrfs_discard_calc_delay(&fs_info->discard_ctl);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002929 btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -05002930 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002931
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002932 /*
2933 * Transaction is finished. We don't need the lock anymore. We
2934 * do need to clean up the block groups in case of a transaction
2935 * abort.
2936 */
2937 deleted_bgs = &trans->transaction->deleted_bgs;
2938 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2939 u64 trimmed = 0;
2940
2941 ret = -EROFS;
David Sterbabf31f872020-02-05 17:34:34 +01002942 if (!TRANS_ABORTED(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002943 ret = btrfs_discard_extent(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02002944 block_group->start,
2945 block_group->length,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002946 &trimmed);
2947
2948 list_del_init(&block_group->bg_list);
2949 btrfs_put_block_group_trimming(block_group);
2950 btrfs_put_block_group(block_group);
2951
2952 if (ret) {
2953 const char *errstr = btrfs_decode_error(ret);
2954 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002955 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002956 ret, errstr);
2957 }
2958 }
2959
Chris Masone20d96d2007-03-22 12:13:20 -04002960 return 0;
2961}
2962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002964 struct btrfs_delayed_ref_node *node, u64 parent,
2965 u64 root_objectid, u64 owner_objectid,
2966 u64 owner_offset, int refs_to_drop,
2967 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002968{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002969 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002970 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002971 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002972 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002973 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974 struct btrfs_extent_item *ei;
2975 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002976 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002977 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002978 int extent_slot = 0;
2979 int found_extent = 0;
2980 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002981 u32 item_size;
2982 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002983 u64 bytenr = node->bytenr;
2984 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002985 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002986 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002987
Chris Mason5caf2a02007-04-02 11:20:42 -04002988 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002989 if (!path)
2990 return -ENOMEM;
2991
David Sterbae4058b52015-11-27 16:31:35 +01002992 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002993 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002994
2995 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2996 BUG_ON(!is_data && refs_to_drop != 1);
2997
Josef Bacik3173a182013-03-07 14:22:04 -05002998 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02002999 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05003000
Nikolay Borisovfbe48012018-06-20 15:48:52 +03003001 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
3002 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003003 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003004 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003005 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 while (extent_slot >= 0) {
3007 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003008 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003009 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003010 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003011 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3012 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003013 found_extent = 1;
3014 break;
3015 }
Josef Bacik3173a182013-03-07 14:22:04 -05003016 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3017 key.offset == owner_objectid) {
3018 found_extent = 1;
3019 break;
3020 }
Chris Mason952fcca2008-02-18 16:33:44 -05003021 if (path->slots[0] - extent_slot > 5)
3022 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003023 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003024 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003025
Zheng Yan31840ae2008-09-23 13:14:14 -04003026 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003027 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003028 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003029 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003030 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003031 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003032 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003033 goto out;
3034 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003035 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003036 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037
3038 key.objectid = bytenr;
3039 key.type = BTRFS_EXTENT_ITEM_KEY;
3040 key.offset = num_bytes;
3041
Josef Bacik3173a182013-03-07 14:22:04 -05003042 if (!is_data && skinny_metadata) {
3043 key.type = BTRFS_METADATA_ITEM_KEY;
3044 key.offset = owner_objectid;
3045 }
3046
Zheng Yan31840ae2008-09-23 13:14:14 -04003047 ret = btrfs_search_slot(trans, extent_root,
3048 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003049 if (ret > 0 && skinny_metadata && path->slots[0]) {
3050 /*
3051 * Couldn't find our skinny metadata item,
3052 * see if we have ye olde extent item.
3053 */
3054 path->slots[0]--;
3055 btrfs_item_key_to_cpu(path->nodes[0], &key,
3056 path->slots[0]);
3057 if (key.objectid == bytenr &&
3058 key.type == BTRFS_EXTENT_ITEM_KEY &&
3059 key.offset == num_bytes)
3060 ret = 0;
3061 }
3062
3063 if (ret > 0 && skinny_metadata) {
3064 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003065 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003066 key.type = BTRFS_EXTENT_ITEM_KEY;
3067 key.offset = num_bytes;
3068 btrfs_release_path(path);
3069 ret = btrfs_search_slot(trans, extent_root,
3070 &key, path, -1, 1);
3071 }
3072
Josef Bacikf3465ca2008-11-12 14:19:50 -05003073 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003074 btrfs_err(info,
3075 "umm, got %d back from search, was looking for %llu",
3076 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003077 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003078 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003079 }
David Sterba005d6422012-09-18 07:52:32 -06003080 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003081 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003082 goto out;
3083 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003084 extent_slot = path->slots[0];
3085 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303086 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003087 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003088 btrfs_err(info,
3089 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003090 bytenr, parent, root_objectid, owner_objectid,
3091 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003092 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003093 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003094 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003095 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003096 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003097 }
Chris Mason5f39d392007-10-15 16:14:19 -04003098
3099 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003100 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003101 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003102 ret = -EINVAL;
3103 btrfs_print_v0_err(info);
3104 btrfs_abort_transaction(trans, ret);
3105 goto out;
3106 }
Chris Mason952fcca2008-02-18 16:33:44 -05003107 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003108 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003109 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3110 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 struct btrfs_tree_block_info *bi;
3112 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3113 bi = (struct btrfs_tree_block_info *)(ei + 1);
3114 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003115 }
3116
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003117 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003118 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003119 btrfs_err(info,
3120 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
3121 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04003122 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04003123 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04003124 goto out;
3125 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003126 refs -= refs_to_drop;
3127
3128 if (refs > 0) {
3129 if (extent_op)
3130 __run_delayed_extent_op(extent_op, leaf, ei);
3131 /*
3132 * In the case of inline back ref, reference count will
3133 * be updated by remove_extent_backref
3134 */
3135 if (iref) {
3136 BUG_ON(!found_extent);
3137 } else {
3138 btrfs_set_extent_refs(leaf, ei, refs);
3139 btrfs_mark_buffer_dirty(leaf);
3140 }
3141 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003142 ret = remove_extent_backref(trans, path, iref,
3143 refs_to_drop, is_data,
3144 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003145 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003146 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003147 goto out;
3148 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003149 }
3150 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003151 if (found_extent) {
3152 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08003153 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003154 if (iref) {
3155 BUG_ON(path->slots[0] != extent_slot);
3156 } else {
3157 BUG_ON(path->slots[0] != extent_slot + 1);
3158 path->slots[0] = extent_slot;
3159 num_to_del = 2;
3160 }
Chris Mason78fae272007-03-25 11:35:08 -04003161 }
Chris Masonb9473432009-03-13 11:00:37 -04003162
Josef Bacikfcebe452014-05-13 17:30:47 -07003163 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003164 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3165 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003166 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003167 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003168 goto out;
3169 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003170 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003171
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003172 if (is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00003173 ret = btrfs_del_csums(trans, info->csum_root, bytenr,
3174 num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003175 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003176 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003177 goto out;
3178 }
Chris Mason459931e2008-12-10 09:10:46 -05003179 }
3180
Nikolay Borisove7355e52018-05-10 15:44:55 +03003181 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003182 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003183 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003184 goto out;
3185 }
3186
Josef Bacikade4b512019-06-20 15:38:01 -04003187 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003188 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003189 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003190 goto out;
3191 }
Chris Masona28ec192007-03-06 20:08:01 -05003192 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003193 btrfs_release_path(path);
3194
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003195out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003196 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003197 return ret;
3198}
3199
3200/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003201 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003202 * delayed ref for that extent as well. This searches the delayed ref tree for
3203 * a given extent, and if there are no other delayed refs to be processed, it
3204 * removes it from the tree.
3205 */
3206static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003207 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003208{
3209 struct btrfs_delayed_ref_head *head;
3210 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003211 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003212
3213 delayed_refs = &trans->transaction->delayed_refs;
3214 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003215 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003216 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003217 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003218
Josef Bacikd7df2c72014-01-23 09:21:38 -05003219 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003220 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003221 goto out;
3222
Josef Bacikbedc66172018-12-03 10:20:31 -05003223 if (cleanup_extent_op(head) != NULL)
3224 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003225
Chris Mason1887be62009-03-13 10:11:24 -04003226 /*
3227 * waiting for the lock here would deadlock. If someone else has it
3228 * locked they are already in the process of dropping it anyway
3229 */
3230 if (!mutex_trylock(&head->mutex))
3231 goto out;
3232
Josef Bacikd7baffd2018-12-03 10:20:29 -05003233 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003234 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003235
Josef Bacikd7df2c72014-01-23 09:21:38 -05003236 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003237 spin_unlock(&delayed_refs->lock);
3238
Yan, Zhengf0486c62010-05-16 10:46:25 -04003239 BUG_ON(head->extent_op);
3240 if (head->must_insert_reserved)
3241 ret = 1;
3242
Josef Bacik31890da2018-11-21 14:05:41 -05003243 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003244 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003245 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003246 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003247out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003248 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003249
3250out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003251 spin_unlock(&delayed_refs->lock);
3252 return 0;
3253}
3254
Yan, Zhengf0486c62010-05-16 10:46:25 -04003255void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3256 struct btrfs_root *root,
3257 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003258 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003260 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003261 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04003262 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003263 int ret;
3264
Qu Wenruoed4f2552019-04-04 14:45:31 +08003265 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3266 buf->start, buf->len, parent);
3267 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3268 root->root_key.objectid);
3269
Yan, Zhengf0486c62010-05-16 10:46:25 -04003270 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07003271 int old_ref_mod, new_ref_mod;
3272
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003273 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08003274 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003275 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003276 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07003277 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003278 }
3279
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003280 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
David Sterba32da53862019-10-29 19:20:18 +01003281 struct btrfs_block_group *cache;
Filipe Manana62198722015-01-06 20:18:45 +00003282
Yan, Zhengf0486c62010-05-16 10:46:25 -04003283 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003284 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003285 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003286 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003287 }
3288
Omar Sandoval4da8b762017-06-06 16:45:28 -07003289 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003290 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003291
Yan, Zhengf0486c62010-05-16 10:46:25 -04003292 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Nikolay Borisov6690d072020-01-20 16:09:14 +02003293 pin_down_extent(trans, cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003294 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003295 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003296 }
3297
3298 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3299
3300 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003301 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003302 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003303 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003304 }
3305out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04003306 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08003307 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003308
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003309 if (last_ref) {
3310 /*
3311 * Deleting the buffer, clear the corrupt flag since it doesn't
3312 * matter anymore.
3313 */
3314 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3315 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003316}
3317
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003318/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003319int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003320{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003321 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07003322 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04003323 int ret;
3324
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003325 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003326 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003327
Chris Mason56bec292009-03-13 10:10:06 -04003328 /*
3329 * tree log blocks never actually go into the extent allocation
3330 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003331 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003332 if ((ref->type == BTRFS_REF_METADATA &&
3333 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3334 (ref->type == BTRFS_REF_DATA &&
3335 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003336 /* unlocks the pinned mutex */
Nikolay Borisovb25c36f2020-01-20 16:09:09 +02003337 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07003338 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04003339 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003340 } else if (ref->type == BTRFS_REF_METADATA) {
3341 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003342 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003343 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003344 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07003345 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04003346 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003347
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003348 if (!((ref->type == BTRFS_REF_METADATA &&
3349 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3350 (ref->type == BTRFS_REF_DATA &&
3351 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3352 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003353
Qu Wenruoddf30cf2019-04-04 14:45:34 +08003354 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08003355 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07003356
Chris Mason925baed2008-06-25 16:01:30 -04003357 return ret;
3358}
3359
Josef Bacik817d52f2009-07-13 21:29:25 -04003360enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003361 LOOP_CACHING_NOWAIT,
3362 LOOP_CACHING_WAIT,
3363 LOOP_ALLOC_CHUNK,
3364 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003365};
3366
Miao Xiee570fd22014-06-19 10:42:50 +08003367static inline void
David Sterba32da53862019-10-29 19:20:18 +01003368btrfs_lock_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003369 int delalloc)
3370{
3371 if (delalloc)
3372 down_read(&cache->data_rwsem);
3373}
3374
David Sterba32da53862019-10-29 19:20:18 +01003375static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003376 int delalloc)
3377{
3378 btrfs_get_block_group(cache);
3379 if (delalloc)
3380 down_read(&cache->data_rwsem);
3381}
3382
David Sterba32da53862019-10-29 19:20:18 +01003383static struct btrfs_block_group *btrfs_lock_cluster(
3384 struct btrfs_block_group *block_group,
Miao Xiee570fd22014-06-19 10:42:50 +08003385 struct btrfs_free_cluster *cluster,
3386 int delalloc)
3387{
David Sterba32da53862019-10-29 19:20:18 +01003388 struct btrfs_block_group *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003389
Miao Xiee570fd22014-06-19 10:42:50 +08003390 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003391 while (1) {
3392 used_bg = cluster->block_group;
3393 if (!used_bg)
3394 return NULL;
3395
3396 if (used_bg == block_group)
3397 return used_bg;
3398
3399 btrfs_get_block_group(used_bg);
3400
3401 if (!delalloc)
3402 return used_bg;
3403
3404 if (down_read_trylock(&used_bg->data_rwsem))
3405 return used_bg;
3406
3407 spin_unlock(&cluster->refill_lock);
3408
Liu Boe321f8a2016-11-30 16:11:04 -08003409 /* We should only have one-level nested. */
3410 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003411
3412 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003413 if (used_bg == cluster->block_group)
3414 return used_bg;
3415
3416 up_read(&used_bg->data_rwsem);
3417 btrfs_put_block_group(used_bg);
3418 }
Miao Xiee570fd22014-06-19 10:42:50 +08003419}
3420
3421static inline void
David Sterba32da53862019-10-29 19:20:18 +01003422btrfs_release_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003423 int delalloc)
3424{
3425 if (delalloc)
3426 up_read(&cache->data_rwsem);
3427 btrfs_put_block_group(cache);
3428}
3429
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003430enum btrfs_extent_allocation_policy {
3431 BTRFS_EXTENT_ALLOC_CLUSTERED,
3432};
3433
Josef Bacik817d52f2009-07-13 21:29:25 -04003434/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08003435 * Structure used internally for find_free_extent() function. Wraps needed
3436 * parameters.
3437 */
3438struct find_free_extent_ctl {
3439 /* Basic allocation info */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003440 u64 num_bytes;
3441 u64 empty_size;
3442 u64 flags;
3443 int delalloc;
3444
3445 /* Where to start the search inside the bg */
3446 u64 search_start;
3447
3448 /* For clustered allocation */
3449 u64 empty_cluster;
Naohiro Aotac10859b2020-02-25 12:56:18 +09003450 struct btrfs_free_cluster *last_ptr;
3451 bool use_cluster;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003452
3453 bool have_caching_bg;
3454 bool orig_have_caching_bg;
3455
3456 /* RAID index, converted from flags */
3457 int index;
3458
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003459 /*
3460 * Current loop number, check find_free_extent_update_loop() for details
3461 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003462 int loop;
3463
3464 /*
3465 * Whether we're refilling a cluster, if true we need to re-search
3466 * current block group but don't try to refill the cluster again.
3467 */
3468 bool retry_clustered;
3469
3470 /*
3471 * Whether we're updating free space cache, if true we need to re-search
3472 * current block group but don't try updating free space cache again.
3473 */
3474 bool retry_unclustered;
3475
3476 /* If current block group is cached */
3477 int cached;
3478
3479 /* Max contiguous hole found */
3480 u64 max_extent_size;
3481
3482 /* Total free space from free space cache, not always contiguous */
3483 u64 total_free_space;
3484
3485 /* Found result */
3486 u64 found_offset;
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003487
Naohiro Aotaea544142020-02-25 12:56:17 +09003488 /* Hint where to start looking for an empty space */
3489 u64 hint_byte;
3490
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003491 /* Allocation policy */
3492 enum btrfs_extent_allocation_policy policy;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003493};
3494
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003495
3496/*
3497 * Helper function for find_free_extent().
3498 *
3499 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3500 * Return -EAGAIN to inform caller that we need to re-search this block group
3501 * Return >0 to inform caller that we find nothing
3502 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3503 */
David Sterba32da53862019-10-29 19:20:18 +01003504static int find_free_extent_clustered(struct btrfs_block_group *bg,
Naohiro Aota897cae72020-02-25 12:56:20 +09003505 struct find_free_extent_ctl *ffe_ctl,
3506 struct btrfs_block_group **cluster_bg_ret)
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003507{
David Sterba32da53862019-10-29 19:20:18 +01003508 struct btrfs_block_group *cluster_bg;
Naohiro Aota897cae72020-02-25 12:56:20 +09003509 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003510 u64 aligned_cluster;
3511 u64 offset;
3512 int ret;
3513
3514 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3515 if (!cluster_bg)
3516 goto refill_cluster;
3517 if (cluster_bg != bg && (cluster_bg->ro ||
3518 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3519 goto release_cluster;
3520
3521 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
David Sterbab3470b52019-10-23 18:48:22 +02003522 ffe_ctl->num_bytes, cluster_bg->start,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003523 &ffe_ctl->max_extent_size);
3524 if (offset) {
3525 /* We have a block, we're done */
3526 spin_unlock(&last_ptr->refill_lock);
3527 trace_btrfs_reserve_extent_cluster(cluster_bg,
3528 ffe_ctl->search_start, ffe_ctl->num_bytes);
3529 *cluster_bg_ret = cluster_bg;
3530 ffe_ctl->found_offset = offset;
3531 return 0;
3532 }
3533 WARN_ON(last_ptr->block_group != cluster_bg);
3534
3535release_cluster:
3536 /*
3537 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3538 * lets just skip it and let the allocator find whatever block it can
3539 * find. If we reach this point, we will have tried the cluster
3540 * allocator plenty of times and not have found anything, so we are
3541 * likely way too fragmented for the clustering stuff to find anything.
3542 *
3543 * However, if the cluster is taken from the current block group,
3544 * release the cluster first, so that we stand a better chance of
3545 * succeeding in the unclustered allocation.
3546 */
3547 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3548 spin_unlock(&last_ptr->refill_lock);
3549 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3550 return -ENOENT;
3551 }
3552
3553 /* This cluster didn't work out, free it and start over */
3554 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3555
3556 if (cluster_bg != bg)
3557 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3558
3559refill_cluster:
3560 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3561 spin_unlock(&last_ptr->refill_lock);
3562 return -ENOENT;
3563 }
3564
3565 aligned_cluster = max_t(u64,
3566 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3567 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003568 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3569 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003570 if (ret == 0) {
3571 /* Now pull our allocation out of this cluster */
3572 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3573 ffe_ctl->num_bytes, ffe_ctl->search_start,
3574 &ffe_ctl->max_extent_size);
3575 if (offset) {
3576 /* We found one, proceed */
3577 spin_unlock(&last_ptr->refill_lock);
3578 trace_btrfs_reserve_extent_cluster(bg,
3579 ffe_ctl->search_start,
3580 ffe_ctl->num_bytes);
3581 ffe_ctl->found_offset = offset;
3582 return 0;
3583 }
3584 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3585 !ffe_ctl->retry_clustered) {
3586 spin_unlock(&last_ptr->refill_lock);
3587
3588 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003589 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003590 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3591 return -EAGAIN;
3592 }
3593 /*
3594 * At this point we either didn't find a cluster or we weren't able to
3595 * allocate a block from our cluster. Free the cluster we've been
3596 * trying to use, and go to the next block group.
3597 */
3598 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3599 spin_unlock(&last_ptr->refill_lock);
3600 return 1;
3601}
3602
Qu Wenruob4bd7452018-11-02 09:39:47 +08003603/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003604 * Return >0 to inform caller that we find nothing
3605 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3606 * Return -EAGAIN to inform caller that we need to re-search this block group
3607 */
David Sterba32da53862019-10-29 19:20:18 +01003608static int find_free_extent_unclustered(struct btrfs_block_group *bg,
Naohiro Aota897cae72020-02-25 12:56:20 +09003609 struct find_free_extent_ctl *ffe_ctl)
Qu Wenruoe1a41842018-11-02 09:39:49 +08003610{
Naohiro Aota897cae72020-02-25 12:56:20 +09003611 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003612 u64 offset;
3613
3614 /*
3615 * We are doing an unclustered allocation, set the fragmented flag so
3616 * we don't bother trying to setup a cluster again until we get more
3617 * space.
3618 */
3619 if (unlikely(last_ptr)) {
3620 spin_lock(&last_ptr->lock);
3621 last_ptr->fragmented = 1;
3622 spin_unlock(&last_ptr->lock);
3623 }
3624 if (ffe_ctl->cached) {
3625 struct btrfs_free_space_ctl *free_space_ctl;
3626
3627 free_space_ctl = bg->free_space_ctl;
3628 spin_lock(&free_space_ctl->tree_lock);
3629 if (free_space_ctl->free_space <
3630 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3631 ffe_ctl->empty_size) {
3632 ffe_ctl->total_free_space = max_t(u64,
3633 ffe_ctl->total_free_space,
3634 free_space_ctl->free_space);
3635 spin_unlock(&free_space_ctl->tree_lock);
3636 return 1;
3637 }
3638 spin_unlock(&free_space_ctl->tree_lock);
3639 }
3640
3641 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3642 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3643 &ffe_ctl->max_extent_size);
3644
3645 /*
3646 * If we didn't find a chunk, and we haven't failed on this block group
3647 * before, and this block group is in the middle of caching and we are
3648 * ok with waiting, then go ahead and wait for progress to be made, and
3649 * set @retry_unclustered to true.
3650 *
3651 * If @retry_unclustered is true then we've already waited on this
3652 * block group once and should move on to the next block group.
3653 */
3654 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3655 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003656 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3657 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003658 ffe_ctl->retry_unclustered = true;
3659 return -EAGAIN;
3660 } else if (!offset) {
3661 return 1;
3662 }
3663 ffe_ctl->found_offset = offset;
3664 return 0;
3665}
3666
Naohiro Aotac6686902020-02-25 12:56:19 +09003667static int do_allocation_clustered(struct btrfs_block_group *block_group,
3668 struct find_free_extent_ctl *ffe_ctl,
3669 struct btrfs_block_group **bg_ret)
3670{
3671 int ret;
3672
3673 /* We want to try and use the cluster allocator, so lets look there */
3674 if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) {
Naohiro Aota897cae72020-02-25 12:56:20 +09003675 ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret);
Naohiro Aotac6686902020-02-25 12:56:19 +09003676 if (ret >= 0 || ret == -EAGAIN)
3677 return ret;
3678 /* ret == -ENOENT case falls through */
3679 }
3680
Naohiro Aota897cae72020-02-25 12:56:20 +09003681 return find_free_extent_unclustered(block_group, ffe_ctl);
Naohiro Aotac6686902020-02-25 12:56:19 +09003682}
3683
3684static int do_allocation(struct btrfs_block_group *block_group,
3685 struct find_free_extent_ctl *ffe_ctl,
3686 struct btrfs_block_group **bg_ret)
3687{
3688 switch (ffe_ctl->policy) {
3689 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3690 return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
3691 default:
3692 BUG();
3693 }
3694}
3695
Naohiro Aotababa5062020-02-25 12:56:21 +09003696static void release_block_group(struct btrfs_block_group *block_group,
3697 struct find_free_extent_ctl *ffe_ctl,
3698 int delalloc)
3699{
3700 switch (ffe_ctl->policy) {
3701 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3702 ffe_ctl->retry_clustered = false;
3703 ffe_ctl->retry_unclustered = false;
3704 break;
3705 default:
3706 BUG();
3707 }
3708
3709 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
3710 ffe_ctl->index);
3711 btrfs_release_block_group(block_group, delalloc);
3712}
3713
Naohiro Aota0ab97242020-02-25 12:56:22 +09003714static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl,
3715 struct btrfs_key *ins)
3716{
3717 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3718
3719 if (!ffe_ctl->use_cluster && last_ptr) {
3720 spin_lock(&last_ptr->lock);
3721 last_ptr->window_start = ins->objectid;
3722 spin_unlock(&last_ptr->lock);
3723 }
3724}
3725
3726static void found_extent(struct find_free_extent_ctl *ffe_ctl,
3727 struct btrfs_key *ins)
3728{
3729 switch (ffe_ctl->policy) {
3730 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3731 found_extent_clustered(ffe_ctl, ins);
3732 break;
3733 default:
3734 BUG();
3735 }
3736}
3737
Qu Wenruoe1a41842018-11-02 09:39:49 +08003738/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003739 * Return >0 means caller needs to re-search for free extent
3740 * Return 0 means we have the needed free extent.
3741 * Return <0 means we failed to locate any free extent.
3742 */
3743static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
3744 struct btrfs_free_cluster *last_ptr,
3745 struct btrfs_key *ins,
3746 struct find_free_extent_ctl *ffe_ctl,
Naohiro Aota52d40ab2020-02-25 12:56:06 +09003747 bool full_search, bool use_cluster)
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003748{
3749 struct btrfs_root *root = fs_info->extent_root;
3750 int ret;
3751
3752 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3753 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3754 ffe_ctl->orig_have_caching_bg = true;
3755
3756 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3757 ffe_ctl->have_caching_bg)
3758 return 1;
3759
3760 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3761 return 1;
3762
3763 if (ins->objectid) {
Naohiro Aota0ab97242020-02-25 12:56:22 +09003764 found_extent(ffe_ctl, ins);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003765 return 0;
3766 }
3767
3768 /*
3769 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3770 * caching kthreads as we move along
3771 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3772 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3773 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3774 * again
3775 */
3776 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3777 ffe_ctl->index = 0;
3778 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3779 /*
3780 * We want to skip the LOOP_CACHING_WAIT step if we
3781 * don't have any uncached bgs and we've already done a
3782 * full search through.
3783 */
3784 if (ffe_ctl->orig_have_caching_bg || !full_search)
3785 ffe_ctl->loop = LOOP_CACHING_WAIT;
3786 else
3787 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3788 } else {
3789 ffe_ctl->loop++;
3790 }
3791
3792 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3793 struct btrfs_trans_handle *trans;
3794 int exist = 0;
3795
3796 trans = current->journal_info;
3797 if (trans)
3798 exist = 1;
3799 else
3800 trans = btrfs_join_transaction(root);
3801
3802 if (IS_ERR(trans)) {
3803 ret = PTR_ERR(trans);
3804 return ret;
3805 }
3806
Josef Bacikfc471cb2019-06-18 16:09:17 -04003807 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3808 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003809
3810 /*
3811 * If we can't allocate a new chunk we've already looped
3812 * through at least once, move on to the NO_EMPTY_SIZE
3813 * case.
3814 */
3815 if (ret == -ENOSPC)
3816 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3817
3818 /* Do not bail out on ENOSPC since we can do more. */
3819 if (ret < 0 && ret != -ENOSPC)
3820 btrfs_abort_transaction(trans, ret);
3821 else
3822 ret = 0;
3823 if (!exist)
3824 btrfs_end_transaction(trans);
3825 if (ret)
3826 return ret;
3827 }
3828
3829 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
3830 /*
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
3845/*
Chris Masonfec577f2007-02-26 10:40:21 -05003846 * walks the btree of allocated extents and find a hole of a given size.
3847 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003848 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003849 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003850 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003851 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003852 *
3853 * If there is no suitable free space, we will record the max size of
3854 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003855 *
3856 * The overall logic and call chain:
3857 *
3858 * find_free_extent()
3859 * |- Iterate through all block groups
3860 * | |- Get a valid block group
3861 * | |- Try to do clustered allocation in that block group
3862 * | |- Try to do unclustered allocation in that block group
3863 * | |- Check if the result is valid
3864 * | | |- If valid, then exit
3865 * | |- Jump to next block group
3866 * |
3867 * |- Push harder to find free extents
3868 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003869 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003870static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003871 u64 ram_bytes, u64 num_bytes, u64 empty_size,
Naohiro Aotaea544142020-02-25 12:56:17 +09003872 u64 hint_byte_orig, struct btrfs_key *ins,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003873 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003874{
Josef Bacik80eb2342008-10-29 14:49:05 -04003875 int ret = 0;
Josef Bacikdb8fe642019-11-19 13:59:00 -05003876 int cache_block_group_error = 0;
David Sterba32da53862019-10-29 19:20:18 +01003877 struct btrfs_block_group *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003878 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003879 struct btrfs_space_info *space_info;
Josef Bacika5e681d2015-10-01 14:54:10 -04003880 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003881
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003882 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003883
Qu Wenruob4bd7452018-11-02 09:39:47 +08003884 ffe_ctl.num_bytes = num_bytes;
3885 ffe_ctl.empty_size = empty_size;
3886 ffe_ctl.flags = flags;
3887 ffe_ctl.search_start = 0;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003888 ffe_ctl.delalloc = delalloc;
3889 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
3890 ffe_ctl.have_caching_bg = false;
3891 ffe_ctl.orig_have_caching_bg = false;
3892 ffe_ctl.found_offset = 0;
Naohiro Aotaea544142020-02-25 12:56:17 +09003893 ffe_ctl.hint_byte = hint_byte_orig;
Naohiro Aotacb2f96f2020-02-25 12:56:16 +09003894 ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003895
Naohiro Aotac10859b2020-02-25 12:56:18 +09003896 /* For clustered allocation */
3897 ffe_ctl.retry_clustered = false;
3898 ffe_ctl.retry_unclustered = false;
3899 ffe_ctl.last_ptr = NULL;
3900 ffe_ctl.use_cluster = true;
3901
David Sterba962a2982014-06-04 18:41:45 +02003902 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04003903 ins->objectid = 0;
3904 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003905
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003906 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05003907
Josef Bacik280c29082019-06-18 16:09:19 -04003908 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003909 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003910 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003911 return -ENOSPC;
3912 }
Josef Bacik2552d172009-04-03 10:14:19 -04003913
Josef Bacik67377732010-09-16 16:19:09 -04003914 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04003915 * If our free space is heavily fragmented we may not be able to make
3916 * big contiguous allocations, so instead of doing the expensive search
3917 * for free space, simply return ENOSPC with our max_extent_size so we
3918 * can go ahead and search for a more manageable chunk.
3919 *
3920 * If our max_extent_size is large enough for our allocation simply
3921 * disable clustering since we will likely not be able to find enough
3922 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04003923 */
Josef Bacik4f4db212015-09-29 11:40:47 -04003924 if (unlikely(space_info->max_extent_size)) {
3925 spin_lock(&space_info->lock);
3926 if (space_info->max_extent_size &&
3927 num_bytes > space_info->max_extent_size) {
3928 ins->offset = space_info->max_extent_size;
3929 spin_unlock(&space_info->lock);
3930 return -ENOSPC;
3931 } else if (space_info->max_extent_size) {
Naohiro Aotac10859b2020-02-25 12:56:18 +09003932 ffe_ctl.use_cluster = false;
Josef Bacik4f4db212015-09-29 11:40:47 -04003933 }
3934 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003935 }
3936
Naohiro Aotac10859b2020-02-25 12:56:18 +09003937 ffe_ctl.last_ptr = fetch_cluster_info(fs_info, space_info,
3938 &ffe_ctl.empty_cluster);
3939 if (ffe_ctl.last_ptr) {
3940 struct btrfs_free_cluster *last_ptr = ffe_ctl.last_ptr;
3941
Chris Masonfa9c0d792009-04-03 09:47:43 -04003942 spin_lock(&last_ptr->lock);
3943 if (last_ptr->block_group)
Naohiro Aotaea544142020-02-25 12:56:17 +09003944 ffe_ctl.hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003945 if (last_ptr->fragmented) {
3946 /*
3947 * We still set window_start so we can keep track of the
3948 * last place we found an allocation to try and save
3949 * some time.
3950 */
Naohiro Aotaea544142020-02-25 12:56:17 +09003951 ffe_ctl.hint_byte = last_ptr->window_start;
Naohiro Aotac10859b2020-02-25 12:56:18 +09003952 ffe_ctl.use_cluster = false;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003953 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003954 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003955 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003956
Qu Wenruob4bd7452018-11-02 09:39:47 +08003957 ffe_ctl.search_start = max(ffe_ctl.search_start,
3958 first_logical_byte(fs_info, 0));
Naohiro Aotaea544142020-02-25 12:56:17 +09003959 ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte);
3960 if (ffe_ctl.search_start == ffe_ctl.hint_byte) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003961 block_group = btrfs_lookup_block_group(fs_info,
3962 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003963 /*
3964 * we don't want to use the block group if it doesn't match our
3965 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05003966 *
3967 * However if we are re-searching with an ideal block group
3968 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04003969 */
David Sterbab6919a52013-04-29 13:39:40 +00003970 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05003971 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04003972 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003973 if (list_empty(&block_group->list) ||
3974 block_group->ro) {
3975 /*
3976 * someone is removing this block group,
3977 * we can't jump into the have_block_group
3978 * target because our list pointers are not
3979 * valid
3980 */
3981 btrfs_put_block_group(block_group);
3982 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05003983 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003984 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08003985 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08003986 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04003987 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05003988 }
Josef Bacik2552d172009-04-03 10:14:19 -04003989 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003990 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003991 }
Chris Mason42e70e72008-11-07 18:17:11 -05003992 }
Josef Bacik2552d172009-04-03 10:14:19 -04003993search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08003994 ffe_ctl.have_caching_bg = false;
3995 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
3996 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04003997 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04003998 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003999 list_for_each_entry(block_group,
4000 &space_info->block_groups[ffe_ctl.index], list) {
Naohiro Aotac6686902020-02-25 12:56:19 +09004001 struct btrfs_block_group *bg_ret;
4002
Jeff Mahoney14443932017-07-19 23:25:51 -04004003 /* If the block group is read-only, we can skip it entirely. */
4004 if (unlikely(block_group->ro))
4005 continue;
4006
Miao Xiee570fd22014-06-19 10:42:50 +08004007 btrfs_grab_block_group(block_group, delalloc);
David Sterbab3470b52019-10-23 18:48:22 +02004008 ffe_ctl.search_start = block_group->start;
Chris Mason42e70e72008-11-07 18:17:11 -05004009
Chris Mason83a50de2010-12-13 15:06:46 -05004010 /*
4011 * this can happen if we end up cycling through all the
4012 * raid types, but we want to make sure we only allocate
4013 * for the proper type.
4014 */
David Sterbab6919a52013-04-29 13:39:40 +00004015 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07004016 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02004017 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02004018 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05004019 BTRFS_BLOCK_GROUP_RAID10;
4020
4021 /*
4022 * if they asked for extra copies and this block group
4023 * doesn't provide them, bail. This does allow us to
4024 * fill raid0 from raid1.
4025 */
David Sterbab6919a52013-04-29 13:39:40 +00004026 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05004027 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08004028
4029 /*
4030 * This block group has different flags than we want.
4031 * It's possible that we have MIXED_GROUP flag but no
4032 * block group is mixed. Just skip such block group.
4033 */
4034 btrfs_release_block_group(block_group, delalloc);
4035 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05004036 }
4037
Josef Bacik2552d172009-04-03 10:14:19 -04004038have_block_group:
David Sterba32da53862019-10-29 19:20:18 +01004039 ffe_ctl.cached = btrfs_block_group_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004040 if (unlikely(!ffe_ctl.cached)) {
4041 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04004042 ret = btrfs_cache_block_group(block_group, 0);
Josef Bacikdb8fe642019-11-19 13:59:00 -05004043
4044 /*
4045 * If we get ENOMEM here or something else we want to
4046 * try other block groups, because it may not be fatal.
4047 * However if we can't find anything else we need to
4048 * save our return here so that we return the actual
4049 * error that caused problems, not ENOSPC.
4050 */
4051 if (ret < 0) {
4052 if (!cache_block_group_error)
4053 cache_block_group_error = ret;
4054 ret = 0;
4055 goto loop;
4056 }
Chris Mason1d4284b2012-03-28 20:31:37 -04004057 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05004058 }
4059
Josef Bacik36cce922013-08-05 11:15:21 -04004060 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
4061 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004062
Naohiro Aotac6686902020-02-25 12:56:19 +09004063 bg_ret = NULL;
4064 ret = do_allocation(block_group, &ffe_ctl, &bg_ret);
4065 if (ret == 0) {
4066 if (bg_ret && bg_ret != block_group) {
4067 btrfs_release_block_group(block_group, delalloc);
4068 block_group = bg_ret;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004069 }
Naohiro Aotac6686902020-02-25 12:56:19 +09004070 } else if (ret == -EAGAIN) {
4071 goto have_block_group;
4072 } else if (ret > 0) {
4073 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004074 }
4075
Naohiro Aotac6686902020-02-25 12:56:19 +09004076 /* Checks */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004077 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4078 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04004079
Josef Bacik2552d172009-04-03 10:14:19 -04004080 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004081 if (ffe_ctl.search_start + num_bytes >
David Sterbab3470b52019-10-23 18:48:22 +02004082 block_group->start + block_group->length) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004083 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4084 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004085 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004086 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004087
Qu Wenruob4bd7452018-11-02 09:39:47 +08004088 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4089 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4090 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004091
Wang Xiaoguang18513092016-07-25 15:51:40 +08004092 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4093 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004094 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004095 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4096 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004097 goto loop;
4098 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004099 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004100
Josef Bacik2552d172009-04-03 10:14:19 -04004101 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004102 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004103 ins->offset = num_bytes;
4104
Qu Wenruob4bd7452018-11-02 09:39:47 +08004105 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4106 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004107 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004108 break;
4109loop:
Naohiro Aotababa5062020-02-25 12:56:21 +09004110 release_block_group(block_group, &ffe_ctl, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004111 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004112 }
4113 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004114
Naohiro Aotac10859b2020-02-25 12:56:18 +09004115 ret = find_free_extent_update_loop(fs_info, ffe_ctl.last_ptr, ins,
4116 &ffe_ctl, full_search,
4117 ffe_ctl.use_cluster);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004118 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004119 goto search;
4120
Josef Bacikdb8fe642019-11-19 13:59:00 -05004121 if (ret == -ENOSPC && !cache_block_group_error) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004122 /*
4123 * Use ffe_ctl->total_free_space as fallback if we can't find
4124 * any contiguous hole.
4125 */
4126 if (!ffe_ctl.max_extent_size)
4127 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004128 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004129 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004130 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004131 ins->offset = ffe_ctl.max_extent_size;
Josef Bacikdb8fe642019-11-19 13:59:00 -05004132 } else if (ret == -ENOSPC) {
4133 ret = cache_block_group_error;
Josef Bacik4f4db212015-09-29 11:40:47 -04004134 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004135 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004136}
Chris Masonec44a352008-04-28 15:29:52 -04004137
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004138/*
4139 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4140 * hole that is at least as big as @num_bytes.
4141 *
4142 * @root - The root that will contain this extent
4143 *
4144 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4145 * is used for accounting purposes. This value differs
4146 * from @num_bytes only in the case of compressed extents.
4147 *
4148 * @num_bytes - Number of bytes to allocate on-disk.
4149 *
4150 * @min_alloc_size - Indicates the minimum amount of space that the
4151 * allocator should try to satisfy. In some cases
4152 * @num_bytes may be larger than what is required and if
4153 * the filesystem is fragmented then allocation fails.
4154 * However, the presence of @min_alloc_size gives a
4155 * chance to try and satisfy the smaller allocation.
4156 *
4157 * @empty_size - A hint that you plan on doing more COW. This is the
4158 * size in bytes the allocator should try to find free
4159 * next to the block it returns. This is just a hint and
4160 * may be ignored by the allocator.
4161 *
4162 * @hint_byte - Hint to the allocator to start searching above the byte
4163 * address passed. It might be ignored.
4164 *
4165 * @ins - This key is modified to record the found hole. It will
4166 * have the following values:
4167 * ins->objectid == start position
4168 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4169 * ins->offset == the size of the hole.
4170 *
4171 * @is_data - Boolean flag indicating whether an extent is
4172 * allocated for data (true) or metadata (false)
4173 *
4174 * @delalloc - Boolean flag indicating whether this allocation is for
4175 * delalloc or not. If 'true' data_rwsem of block groups
4176 * is going to be acquired.
4177 *
4178 *
4179 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4180 * case -ENOSPC is returned then @ins->offset will contain the size of the
4181 * largest available hole the allocator managed to find.
4182 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004183int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004184 u64 num_bytes, u64 min_alloc_size,
4185 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004186 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004187{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004188 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004189 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004190 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004191 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004192
Jeff Mahoney1b868262017-05-17 11:38:35 -04004193 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004194again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004195 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004196 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004197 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004198 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004199 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004200 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004201 if (!final_tried && ins->offset) {
4202 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004203 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004204 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004205 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004206 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004207 if (num_bytes == min_alloc_size)
4208 final_tried = true;
4209 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004210 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004211 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004212
Josef Bacik280c29082019-06-18 16:09:19 -04004213 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004214 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004215 "allocation failed flags %llu, wanted %llu",
4216 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004217 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004218 btrfs_dump_space_info(fs_info, sinfo,
4219 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004220 }
Chris Mason925baed2008-06-25 16:01:30 -04004221 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004222
4223 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004224}
4225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004226int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004227 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004228{
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004229 struct btrfs_block_group *cache;
4230
4231 cache = btrfs_lookup_block_group(fs_info, start);
4232 if (!cache) {
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004233 btrfs_err(fs_info, "Unable to find block group for %llu",
4234 start);
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004235 return -ENOSPC;
4236 }
4237
4238 btrfs_add_free_space(cache, start, len);
4239 btrfs_free_reserved_bytes(cache, len, delalloc);
4240 trace_btrfs_reserved_extent_free(fs_info, start, len);
4241
4242 btrfs_put_block_group(cache);
4243 return 0;
Chris Masone688b7252011-10-31 20:52:39 -04004244}
4245
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004246int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
4247 u64 len)
Chris Masone688b7252011-10-31 20:52:39 -04004248{
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004249 struct btrfs_block_group *cache;
4250 int ret = 0;
4251
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004252 cache = btrfs_lookup_block_group(trans->fs_info, start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004253 if (!cache) {
Nikolay Borisov7bfc1002020-01-20 16:09:12 +02004254 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4255 start);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004256 return -ENOSPC;
4257 }
4258
Nikolay Borisov6690d072020-01-20 16:09:14 +02004259 ret = pin_down_extent(trans, cache, start, len, 1);
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004260 btrfs_put_block_group(cache);
4261 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04004262}
4263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004264static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004265 u64 parent, u64 root_objectid,
4266 u64 flags, u64 owner, u64 offset,
4267 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004268{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004269 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004270 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004271 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004272 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004274 struct extent_buffer *leaf;
4275 int type;
4276 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004278 if (parent > 0)
4279 type = BTRFS_SHARED_DATA_REF_KEY;
4280 else
4281 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004283 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004284
4285 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004286 if (!path)
4287 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004288
Chris Masonb9473432009-03-13 11:00:37 -04004289 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4291 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004292 if (ret) {
4293 btrfs_free_path(path);
4294 return ret;
4295 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004296
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 leaf = path->nodes[0];
4298 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004299 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4301 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4302 btrfs_set_extent_flags(leaf, extent_item,
4303 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004305 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4306 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4307 if (parent > 0) {
4308 struct btrfs_shared_data_ref *ref;
4309 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4310 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4311 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4312 } else {
4313 struct btrfs_extent_data_ref *ref;
4314 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4315 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4316 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4317 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4318 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4319 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004320
4321 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004322 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004323
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004324 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004325 if (ret)
4326 return ret;
4327
Josef Bacikade4b512019-06-20 15:38:01 -04004328 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004330 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004331 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004332 BUG();
4333 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004334 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004335 return ret;
4336}
4337
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004338static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004339 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004340 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004342 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004343 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004344 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004345 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004346 struct btrfs_tree_block_info *block_info;
4347 struct btrfs_extent_inline_ref *iref;
4348 struct btrfs_path *path;
4349 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004350 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004351 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004352 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004353 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004355
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004356 ref = btrfs_delayed_node_to_tree_ref(node);
4357
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004358 extent_key.objectid = node->bytenr;
4359 if (skinny_metadata) {
4360 extent_key.offset = ref->level;
4361 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4362 num_bytes = fs_info->nodesize;
4363 } else {
4364 extent_key.offset = node->num_bytes;
4365 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004366 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004367 num_bytes = node->num_bytes;
4368 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004369
4370 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004371 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004372 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004373
4374 path->leave_spinning = 1;
4375 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004376 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004377 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004378 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004379 return ret;
4380 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004381
4382 leaf = path->nodes[0];
4383 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4384 struct btrfs_extent_item);
4385 btrfs_set_extent_refs(leaf, extent_item, 1);
4386 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4387 btrfs_set_extent_flags(leaf, extent_item,
4388 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004389
Josef Bacik3173a182013-03-07 14:22:04 -05004390 if (skinny_metadata) {
4391 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4392 } else {
4393 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004394 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004395 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004396 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4397 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004398
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004399 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004400 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4401 btrfs_set_extent_inline_ref_type(leaf, iref,
4402 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004403 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004404 } else {
4405 btrfs_set_extent_inline_ref_type(leaf, iref,
4406 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004407 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004408 }
4409
4410 btrfs_mark_buffer_dirty(leaf);
4411 btrfs_free_path(path);
4412
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004413 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4414 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004415 if (ret)
4416 return ret;
4417
Josef Bacikade4b512019-06-20 15:38:01 -04004418 ret = btrfs_update_block_group(trans, extent_key.objectid,
4419 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004420 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004421 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004422 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004423 BUG();
4424 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004425
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004426 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004427 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428 return ret;
4429}
4430
4431int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004432 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004433 u64 offset, u64 ram_bytes,
4434 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004435{
Qu Wenruo76675592019-04-04 14:45:32 +08004436 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04004437 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004438
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004439 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004440
Qu Wenruo76675592019-04-04 14:45:32 +08004441 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4442 ins->objectid, ins->offset, 0);
4443 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004444 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08004445 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4446 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004447 return ret;
4448}
Chris Masone02119d2008-09-05 16:13:11 -04004449
4450/*
4451 * this is used by the tree logging recovery code. It records that
4452 * an extent has been allocated and makes sure to clear the free
4453 * space cache bits as well
4454 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004455int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004456 u64 root_objectid, u64 owner, u64 offset,
4457 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004458{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004459 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004460 int ret;
David Sterba32da53862019-10-29 19:20:18 +01004461 struct btrfs_block_group *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004462 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004463
4464 /*
4465 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004466 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004467 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004468 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004469 ret = __exclude_logged_extent(fs_info, ins->objectid,
4470 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004471 if (ret)
4472 return ret;
4473 }
Chris Masone02119d2008-09-05 16:13:11 -04004474
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004475 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004476 if (!block_group)
4477 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004478
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004479 space_info = block_group->space_info;
4480 spin_lock(&space_info->lock);
4481 spin_lock(&block_group->lock);
4482 space_info->bytes_reserved += ins->offset;
4483 block_group->reserved += ins->offset;
4484 spin_unlock(&block_group->lock);
4485 spin_unlock(&space_info->lock);
4486
Nikolay Borisovef89b822018-06-20 15:48:58 +03004487 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4488 offset, ins, 1);
Josef Bacikbd727172020-02-13 10:47:30 -05004489 if (ret)
Josef Bacikab9b2c72020-02-13 10:47:30 -05004490 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004491 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004492 return ret;
4493}
4494
Eric Sandeen48a3b632013-04-25 20:41:01 +00004495static struct extent_buffer *
4496btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004497 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04004498{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004499 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004500 struct extent_buffer *buf;
4501
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004502 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004503 if (IS_ERR(buf))
4504 return buf;
4505
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004506 /*
4507 * Extra safety check in case the extent tree is corrupted and extent
4508 * allocator chooses to use a tree block which is already used and
4509 * locked.
4510 */
4511 if (buf->lock_owner == current->pid) {
4512 btrfs_err_rl(fs_info,
4513"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4514 buf->start, btrfs_header_owner(buf), current->pid);
4515 free_extent_buffer(buf);
4516 return ERR_PTR(-EUCLEAN);
4517 }
4518
Chris Mason85d4e462011-07-26 16:11:19 -04004519 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004520 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01004521 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004522 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004523
David Sterba8bead252018-04-04 02:03:48 +02004524 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004525 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004526
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004527 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4528 btrfs_set_header_level(buf, level);
4529 btrfs_set_header_bytenr(buf, buf->start);
4530 btrfs_set_header_generation(buf, trans->transid);
4531 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4532 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004533 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004534 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004535 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004536 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004537 /*
4538 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004539 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004540 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004541 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004542 set_extent_dirty(&root->dirty_log_pages, buf->start,
4543 buf->start + buf->len - 1, GFP_NOFS);
4544 else
4545 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004546 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004547 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004548 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004549 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4550 buf->start + buf->len - 1, GFP_NOFS);
4551 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004552 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004553 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004554 return buf;
4555}
4556
Chris Masonfec577f2007-02-26 10:40:21 -05004557/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004558 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004559 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004560 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004561struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004562 struct btrfs_root *root,
4563 u64 parent, u64 root_objectid,
4564 const struct btrfs_disk_key *key,
4565 int level, u64 hint,
4566 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004567{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004568 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004569 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004570 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004571 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004572 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004573 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004574 u64 flags = 0;
4575 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004576 u32 blocksize = fs_info->nodesize;
4577 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004578
David Sterba05653ef2016-07-15 15:23:37 +02004579#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004580 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004581 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004582 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004583 if (!IS_ERR(buf))
4584 root->alloc_bytenr += blocksize;
4585 return buf;
4586 }
David Sterba05653ef2016-07-15 15:23:37 +02004587#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004588
Josef Bacik67f9c222019-06-19 13:47:23 -04004589 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004590 if (IS_ERR(block_rsv))
4591 return ERR_CAST(block_rsv);
4592
Wang Xiaoguang18513092016-07-25 15:51:40 +08004593 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004594 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004595 if (ret)
4596 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004597
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004598 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4599 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08004600 if (IS_ERR(buf)) {
4601 ret = PTR_ERR(buf);
4602 goto out_free_reserved;
4603 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004604
4605 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4606 if (parent == 0)
4607 parent = ins.objectid;
4608 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4609 } else
4610 BUG_ON(parent > 0);
4611
4612 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004613 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004614 if (!extent_op) {
4615 ret = -ENOMEM;
4616 goto out_free_buf;
4617 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004618 if (key)
4619 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4620 else
4621 memset(&extent_op->key, 0, sizeof(extent_op->key));
4622 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004623 extent_op->update_key = skinny_metadata ? false : true;
4624 extent_op->update_flags = true;
4625 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004626 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004627
Qu Wenruoed4f2552019-04-04 14:45:31 +08004628 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4629 ins.objectid, ins.offset, parent);
4630 generic_ref.real_root = root->root_key.objectid;
4631 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004632 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004633 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07004634 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08004635 if (ret)
4636 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004637 }
Chris Masonfec577f2007-02-26 10:40:21 -05004638 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004639
4640out_free_delayed:
4641 btrfs_free_delayed_extent_op(extent_op);
4642out_free_buf:
4643 free_extent_buffer(buf);
4644out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004645 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004646out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004647 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004648 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004649}
Chris Masona28ec192007-03-06 20:08:01 -05004650
Yan Zheng2c47e6052009-06-27 21:07:35 -04004651struct walk_control {
4652 u64 refs[BTRFS_MAX_LEVEL];
4653 u64 flags[BTRFS_MAX_LEVEL];
4654 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004655 struct btrfs_key drop_progress;
4656 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004657 int stage;
4658 int level;
4659 int shared_level;
4660 int update_ref;
4661 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004662 int reada_slot;
4663 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004664 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004665};
4666
4667#define DROP_REFERENCE 1
4668#define UPDATE_BACKREF 2
4669
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004670static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4671 struct btrfs_root *root,
4672 struct walk_control *wc,
4673 struct btrfs_path *path)
4674{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004675 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004676 u64 bytenr;
4677 u64 generation;
4678 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004679 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004680 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004681 struct btrfs_key key;
4682 struct extent_buffer *eb;
4683 int ret;
4684 int slot;
4685 int nread = 0;
4686
4687 if (path->slots[wc->level] < wc->reada_slot) {
4688 wc->reada_count = wc->reada_count * 2 / 3;
4689 wc->reada_count = max(wc->reada_count, 2);
4690 } else {
4691 wc->reada_count = wc->reada_count * 3 / 2;
4692 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004693 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004694 }
4695
4696 eb = path->nodes[wc->level];
4697 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004698
4699 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4700 if (nread >= wc->reada_count)
4701 break;
4702
4703 cond_resched();
4704 bytenr = btrfs_node_blockptr(eb, slot);
4705 generation = btrfs_node_ptr_generation(eb, slot);
4706
4707 if (slot == path->slots[wc->level])
4708 goto reada;
4709
4710 if (wc->stage == UPDATE_BACKREF &&
4711 generation <= root->root_key.offset)
4712 continue;
4713
Yan, Zheng94fcca92009-10-09 09:25:16 -04004714 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004715 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004716 wc->level - 1, 1, &refs,
4717 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004718 /* We don't care about errors in readahead. */
4719 if (ret < 0)
4720 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004721 BUG_ON(refs == 0);
4722
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004723 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004724 if (refs == 1)
4725 goto reada;
4726
Yan, Zheng94fcca92009-10-09 09:25:16 -04004727 if (wc->level == 1 &&
4728 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4729 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004730 if (!wc->update_ref ||
4731 generation <= root->root_key.offset)
4732 continue;
4733 btrfs_node_key_to_cpu(eb, &key, slot);
4734 ret = btrfs_comp_cpu_keys(&key,
4735 &wc->update_progress);
4736 if (ret < 0)
4737 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004738 } else {
4739 if (wc->level == 1 &&
4740 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4741 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004742 }
4743reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004744 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004745 nread++;
4746 }
4747 wc->reada_slot = slot;
4748}
4749
Chris Mason9aca1d52007-03-13 11:09:37 -04004750/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004751 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004752 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004753 * when wc->stage == UPDATE_BACKREF, this function updates
4754 * back refs for pointers in the block.
4755 *
4756 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004757 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004758static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4759 struct btrfs_root *root,
4760 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004761 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004762{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004763 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004764 int level = wc->level;
4765 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004766 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4767 int ret;
4768
4769 if (wc->stage == UPDATE_BACKREF &&
4770 btrfs_header_owner(eb) != root->root_key.objectid)
4771 return 1;
4772
4773 /*
4774 * when reference count of tree block is 1, it won't increase
4775 * again. once full backref flag is set, we never clear it.
4776 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004777 if (lookup_info &&
4778 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4779 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004780 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004781 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004782 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004783 &wc->refs[level],
4784 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004785 BUG_ON(ret == -ENOMEM);
4786 if (ret)
4787 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004788 BUG_ON(wc->refs[level] == 0);
4789 }
4790
Yan Zheng2c47e6052009-06-27 21:07:35 -04004791 if (wc->stage == DROP_REFERENCE) {
4792 if (wc->refs[level] > 1)
4793 return 1;
4794
4795 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004796 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004797 path->locks[level] = 0;
4798 }
4799 return 0;
4800 }
4801
4802 /* wc->stage == UPDATE_BACKREF */
4803 if (!(wc->flags[level] & flag)) {
4804 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004805 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004806 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004807 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004808 BUG_ON(ret); /* -ENOMEM */
David Sterba42c9d0b2019-03-20 11:54:13 +01004809 ret = btrfs_set_disk_extent_flags(trans, eb, flag,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004810 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004811 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004812 wc->flags[level] |= flag;
4813 }
4814
4815 /*
4816 * the block is shared by multiple trees, so it's not good to
4817 * keep the tree lock
4818 */
4819 if (path->locks[level] && level > 0) {
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/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004827 * This is used to verify a ref exists for this root to deal with a bug where we
4828 * would have a drop_progress key that hadn't been updated properly.
4829 */
4830static int check_ref_exists(struct btrfs_trans_handle *trans,
4831 struct btrfs_root *root, u64 bytenr, u64 parent,
4832 int level)
4833{
4834 struct btrfs_path *path;
4835 struct btrfs_extent_inline_ref *iref;
4836 int ret;
4837
4838 path = btrfs_alloc_path();
4839 if (!path)
4840 return -ENOMEM;
4841
4842 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4843 root->fs_info->nodesize, parent,
4844 root->root_key.objectid, level, 0);
4845 btrfs_free_path(path);
4846 if (ret == -ENOENT)
4847 return 0;
4848 if (ret < 0)
4849 return ret;
4850 return 1;
4851}
4852
4853/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004854 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004855 *
4856 * when wc->stage == DROP_REFERENCE, this function checks
4857 * reference count of the block pointed to. if the block
4858 * is shared and we need update back refs for the subtree
4859 * rooted at the block, this function changes wc->stage to
4860 * UPDATE_BACKREF. if the block is shared and there is no
4861 * need to update back, this function drops the reference
4862 * to the block.
4863 *
4864 * NOTE: return value 1 means we should stop walking down.
4865 */
4866static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4867 struct btrfs_root *root,
4868 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004869 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004870{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004871 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004872 u64 bytenr;
4873 u64 generation;
4874 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004875 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004876 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004877 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004878 struct extent_buffer *next;
4879 int level = wc->level;
4880 int reada = 0;
4881 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004882 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004883
4884 generation = btrfs_node_ptr_generation(path->nodes[level],
4885 path->slots[level]);
4886 /*
4887 * if the lower level block was created before the snapshot
4888 * was created, we know there is no need to update back refs
4889 * for the subtree
4890 */
4891 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004892 generation <= root->root_key.offset) {
4893 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004894 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004895 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004896
4897 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004898 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4899 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004900
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004901 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004902 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004903 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004904 if (IS_ERR(next))
4905 return PTR_ERR(next);
4906
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004907 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4908 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004909 reada = 1;
4910 }
4911 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004912 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004913
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004914 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004915 &wc->refs[level - 1],
4916 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004917 if (ret < 0)
4918 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004919
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004920 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004922 ret = -EIO;
4923 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004924 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004925 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004926
Yan, Zheng94fcca92009-10-09 09:25:16 -04004927 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004928 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07004929 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004930 if (level == 1 &&
4931 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4932 goto skip;
4933
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004934 if (!wc->update_ref ||
4935 generation <= root->root_key.offset)
4936 goto skip;
4937
4938 btrfs_node_key_to_cpu(path->nodes[level], &key,
4939 path->slots[level]);
4940 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4941 if (ret < 0)
4942 goto skip;
4943
4944 wc->stage = UPDATE_BACKREF;
4945 wc->shared_level = level - 1;
4946 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004947 } else {
4948 if (level == 1 &&
4949 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4950 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004951 }
4952
Chris Masonb9fab912012-05-06 07:23:47 -04004953 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004954 btrfs_tree_unlock(next);
4955 free_extent_buffer(next);
4956 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004957 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004958 }
4959
4960 if (!next) {
4961 if (reada && level == 1)
4962 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08004963 next = read_tree_block(fs_info, bytenr, generation, level - 1,
4964 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08004965 if (IS_ERR(next)) {
4966 return PTR_ERR(next);
4967 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04004968 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004969 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04004970 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004971 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004972 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004973 }
4974
4975 level--;
Josef Bacik48672682016-09-23 13:23:28 +02004976 ASSERT(level == btrfs_header_level(next));
4977 if (level != btrfs_header_level(next)) {
4978 btrfs_err(root->fs_info, "mismatched level");
4979 ret = -EIO;
4980 goto out_unlock;
4981 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004982 path->nodes[level] = next;
4983 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004984 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004985 wc->level = level;
4986 if (wc->level == 1)
4987 wc->reada_slot = 0;
4988 return 0;
4989skip:
4990 wc->refs[level - 1] = 0;
4991 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004992 if (wc->stage == DROP_REFERENCE) {
4993 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4994 parent = path->nodes[level]->start;
4995 } else {
Josef Bacik48672682016-09-23 13:23:28 +02004996 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04004997 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02004998 if (root->root_key.objectid !=
4999 btrfs_header_owner(path->nodes[level])) {
5000 btrfs_err(root->fs_info,
5001 "mismatched block owner");
5002 ret = -EIO;
5003 goto out_unlock;
5004 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005005 parent = 0;
5006 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005007
Qu Wenruo2cd86d32018-09-27 14:42:33 +08005008 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05005009 * If we had a drop_progress we need to verify the refs are set
5010 * as expected. If we find our ref then we know that from here
5011 * on out everything should be correct, and we can clear the
5012 * ->restarted flag.
5013 */
5014 if (wc->restarted) {
5015 ret = check_ref_exists(trans, root, bytenr, parent,
5016 level - 1);
5017 if (ret < 0)
5018 goto out_unlock;
5019 if (ret == 0)
5020 goto no_delete;
5021 ret = 0;
5022 wc->restarted = 0;
5023 }
5024
5025 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08005026 * Reloc tree doesn't contribute to qgroup numbers, and we have
5027 * already accounted them at merge time (replace_path),
5028 * thus we could skip expensive subtree trace here.
5029 */
5030 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
5031 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08005032 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08005033 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07005034 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005036 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
5037 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07005038 }
5039 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005040
5041 /*
5042 * We need to update the next key in our walk control so we can
5043 * update the drop_progress key accordingly. We don't care if
5044 * find_next_key doesn't find a key because that means we're at
5045 * the end and are going to clean up now.
5046 */
5047 wc->drop_level = level;
5048 find_next_key(path, level, &wc->drop_progress);
5049
Qu Wenruoffd4bb22019-04-04 14:45:36 +08005050 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
5051 fs_info->nodesize, parent);
5052 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
5053 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02005054 if (ret)
5055 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005056 }
Josef Bacik78c52d92019-02-06 15:46:14 -05005057no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02005058 *lookup_info = 1;
5059 ret = 1;
5060
5061out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005062 btrfs_tree_unlock(next);
5063 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02005064
5065 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005066}
5067
5068/*
Liu Bo2c016dc2012-12-26 15:32:17 +08005069 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04005070 *
5071 * when wc->stage == DROP_REFERENCE, this function drops
5072 * reference count on the block.
5073 *
5074 * when wc->stage == UPDATE_BACKREF, this function changes
5075 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5076 * to UPDATE_BACKREF previously while processing the block.
5077 *
5078 * NOTE: return value 1 means we should stop walking up.
5079 */
5080static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5081 struct btrfs_root *root,
5082 struct btrfs_path *path,
5083 struct walk_control *wc)
5084{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005085 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005086 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005087 int level = wc->level;
5088 struct extent_buffer *eb = path->nodes[level];
5089 u64 parent = 0;
5090
5091 if (wc->stage == UPDATE_BACKREF) {
5092 BUG_ON(wc->shared_level < level);
5093 if (level < wc->shared_level)
5094 goto out;
5095
Yan Zheng2c47e6052009-06-27 21:07:35 -04005096 ret = find_next_key(path, level + 1, &wc->update_progress);
5097 if (ret > 0)
5098 wc->update_ref = 0;
5099
5100 wc->stage = DROP_REFERENCE;
5101 wc->shared_level = -1;
5102 path->slots[level] = 0;
5103
5104 /*
5105 * check reference count again if the block isn't locked.
5106 * we should start walking down the tree again if reference
5107 * count is one.
5108 */
5109 if (!path->locks[level]) {
5110 BUG_ON(level == 0);
5111 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005112 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005113 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005114
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005115 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005116 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005117 &wc->refs[level],
5118 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005119 if (ret < 0) {
5120 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005121 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005122 return ret;
5123 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005124 BUG_ON(wc->refs[level] == 0);
5125 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005126 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005127 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005128 return 1;
5129 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005130 }
5131 }
5132
5133 /* wc->stage == DROP_REFERENCE */
5134 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5135
5136 if (wc->refs[level] == 1) {
5137 if (level == 0) {
5138 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005139 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005140 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005141 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005142 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005143 if (is_fstree(root->root_key.objectid)) {
5144 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5145 if (ret) {
5146 btrfs_err_rl(fs_info,
5147 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005148 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005149 }
Mark Fasheh11526512014-07-17 12:39:01 -07005150 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005151 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005152 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005153 if (!path->locks[level] &&
5154 btrfs_header_generation(eb) == trans->transid) {
5155 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005156 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005157 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005158 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005159 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005160 }
5161
5162 if (eb == root->node) {
5163 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5164 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005165 else if (root->root_key.objectid != btrfs_header_owner(eb))
5166 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005167 } else {
5168 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5169 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005170 else if (root->root_key.objectid !=
5171 btrfs_header_owner(path->nodes[level + 1]))
5172 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005173 }
5174
Jan Schmidt5581a512012-05-16 17:04:52 +02005175 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005176out:
5177 wc->refs[level] = 0;
5178 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005179 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005180
5181owner_mismatch:
5182 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5183 btrfs_header_owner(eb), root->root_key.objectid);
5184 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005185}
5186
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005187static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5188 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005189 struct btrfs_path *path,
5190 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005191{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005192 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005193 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005194 int ret;
5195
Yan Zheng2c47e6052009-06-27 21:07:35 -04005196 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005197 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005198 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005199 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005200
Yan Zheng2c47e6052009-06-27 21:07:35 -04005201 if (level == 0)
5202 break;
5203
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005204 if (path->slots[level] >=
5205 btrfs_header_nritems(path->nodes[level]))
5206 break;
5207
Yan, Zheng94fcca92009-10-09 09:25:16 -04005208 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005209 if (ret > 0) {
5210 path->slots[level]++;
5211 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005212 } else if (ret < 0)
5213 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005214 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005215 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005216 return 0;
5217}
5218
Chris Masond3977122009-01-05 21:25:51 -05005219static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005220 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005221 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005222 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005223{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005224 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005225 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005226
Yan Zheng2c47e6052009-06-27 21:07:35 -04005227 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5228 while (level < max_level && path->nodes[level]) {
5229 wc->level = level;
5230 if (path->slots[level] + 1 <
5231 btrfs_header_nritems(path->nodes[level])) {
5232 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005233 return 0;
5234 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005235 ret = walk_up_proc(trans, root, path, wc);
5236 if (ret > 0)
5237 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005238 if (ret < 0)
5239 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005240
Yan Zheng2c47e6052009-06-27 21:07:35 -04005241 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005242 btrfs_tree_unlock_rw(path->nodes[level],
5243 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005244 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005245 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005246 free_extent_buffer(path->nodes[level]);
5247 path->nodes[level] = NULL;
5248 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005249 }
5250 }
5251 return 1;
5252}
5253
Chris Mason9aca1d52007-03-13 11:09:37 -04005254/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005255 * drop a subvolume tree.
5256 *
5257 * this function traverses the tree freeing any blocks that only
5258 * referenced by the tree.
5259 *
5260 * when a shared tree block is found. this function decreases its
5261 * reference count by one. if update_ref is true, this function
5262 * also make sure backrefs for the shared block and all lower level
5263 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005264 *
5265 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005266 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04005267int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005268 struct btrfs_block_rsv *block_rsv, int update_ref,
5269 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005270{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005271 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005272 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005273 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005274 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005275 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005276 struct walk_control *wc;
5277 struct btrfs_key key;
5278 int err = 0;
5279 int ret;
5280 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005281 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005282
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005283 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005284
Chris Mason5caf2a02007-04-02 11:20:42 -04005285 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005286 if (!path) {
5287 err = -ENOMEM;
5288 goto out;
5289 }
Chris Mason20524f02007-03-10 06:35:47 -05005290
Yan Zheng2c47e6052009-06-27 21:07:35 -04005291 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005292 if (!wc) {
5293 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005294 err = -ENOMEM;
5295 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005296 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005297
Yan, Zhenga22285a2010-05-16 10:48:46 -04005298 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005299 if (IS_ERR(trans)) {
5300 err = PTR_ERR(trans);
5301 goto out_free;
5302 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005303
Josef Bacik0568e822018-11-30 11:52:14 -05005304 err = btrfs_run_delayed_items(trans);
5305 if (err)
5306 goto out_end_trans;
5307
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005308 if (block_rsv)
5309 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005310
Josef Bacik83354f02018-11-30 11:52:13 -05005311 /*
5312 * This will help us catch people modifying the fs tree while we're
5313 * dropping it. It is unsafe to mess with the fs tree while it's being
5314 * dropped as we unlock the root node and parent nodes as we walk down
5315 * the tree, assuming nothing will change. If something does change
5316 * then we'll have stale information and drop references to blocks we've
5317 * already dropped.
5318 */
5319 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005320 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005321 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005322 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005323 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005324 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005325 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005326 memset(&wc->update_progress, 0,
5327 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005328 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005329 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005330 memcpy(&wc->update_progress, &key,
5331 sizeof(wc->update_progress));
5332
Chris Mason6702ed42007-08-07 16:15:09 -04005333 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005334 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005335 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005336 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5337 path->lowest_level = 0;
5338 if (ret < 0) {
5339 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005340 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005341 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005342 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005343
Chris Mason7d9eb122008-07-08 14:19:17 -04005344 /*
5345 * unlock our path, this is safe because only this
5346 * function is allowed to delete this snapshot
5347 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005348 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005349
Yan Zheng2c47e6052009-06-27 21:07:35 -04005350 level = btrfs_header_level(root->node);
5351 while (1) {
5352 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005353 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005354 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005356 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005357 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005358 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005359 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005360 if (ret < 0) {
5361 err = ret;
5362 goto out_end_trans;
5363 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005364 BUG_ON(wc->refs[level] == 0);
5365
5366 if (level == root_item->drop_level)
5367 break;
5368
5369 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005370 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005371 WARN_ON(wc->refs[level] != 1);
5372 level--;
5373 }
5374 }
5375
Josef Bacik78c52d92019-02-06 15:46:14 -05005376 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005377 wc->level = level;
5378 wc->shared_level = -1;
5379 wc->stage = DROP_REFERENCE;
5380 wc->update_ref = update_ref;
5381 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005382 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005383
5384 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005385
Yan Zheng2c47e6052009-06-27 21:07:35 -04005386 ret = walk_down_tree(trans, root, path, wc);
5387 if (ret < 0) {
5388 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005389 break;
5390 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005391
5392 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5393 if (ret < 0) {
5394 err = ret;
5395 break;
5396 }
5397
5398 if (ret > 0) {
5399 BUG_ON(wc->stage != DROP_REFERENCE);
5400 break;
5401 }
5402
5403 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005404 wc->drop_level = wc->level;
5405 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5406 &wc->drop_progress,
5407 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005408 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005409 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5410 &wc->drop_progress);
5411 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005412
5413 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005414 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005415 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005416 ret = btrfs_update_root(trans, tree_root,
5417 &root->root_key,
5418 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005419 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005420 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005421 err = ret;
5422 goto out_end_trans;
5423 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005424
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005425 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005426 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005427 btrfs_debug(fs_info,
5428 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005429 err = -EAGAIN;
5430 goto out_free;
5431 }
5432
Yan, Zhenga22285a2010-05-16 10:48:46 -04005433 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005434 if (IS_ERR(trans)) {
5435 err = PTR_ERR(trans);
5436 goto out_free;
5437 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005438 if (block_rsv)
5439 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04005440 }
Chris Mason20524f02007-03-10 06:35:47 -05005441 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005442 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005443 if (err)
5444 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005445
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005446 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005447 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005448 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005449 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005450 goto out_end_trans;
5451 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005452
Yan, Zheng76dda932009-09-21 16:00:26 -04005453 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005454 ret = btrfs_find_root(tree_root, &root->root_key, path,
5455 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005456 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005457 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005458 err = ret;
5459 goto out_end_trans;
5460 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005461 /* if we fail to delete the orphan item this time
5462 * around, it'll get picked up the next time.
5463 *
5464 * The most common failure here is just -ENOENT.
5465 */
5466 btrfs_del_orphan_item(trans, tree_root,
5467 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005468 }
5469 }
5470
Miao Xie27cdeb72014-04-02 19:51:05 +08005471 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005472 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005473 } else {
5474 free_extent_buffer(root->node);
5475 free_extent_buffer(root->commit_root);
Josef Bacik00246522020-01-24 09:33:01 -05005476 btrfs_put_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005477 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04005478 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005479out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005480 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005481out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005482 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005483 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005484out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005485 /*
5486 * So if we need to stop dropping the snapshot for whatever reason we
5487 * need to make sure to add it back to the dead root list so that we
5488 * keep trying to do the work later. This also cleans up roots if we
5489 * don't have it in the radix (like when we recover after a power fail
5490 * or unmount) so we don't leak memory.
5491 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005492 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005493 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08005494 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005495 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005496 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005497}
Chris Mason9078a3e2007-04-26 16:46:15 -04005498
Yan Zheng2c47e6052009-06-27 21:07:35 -04005499/*
5500 * drop subtree rooted at tree block 'node'.
5501 *
5502 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005503 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005504 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005505int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5506 struct btrfs_root *root,
5507 struct extent_buffer *node,
5508 struct extent_buffer *parent)
5509{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005510 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005511 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005512 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005513 int level;
5514 int parent_level;
5515 int ret = 0;
5516 int wret;
5517
Yan Zheng2c47e6052009-06-27 21:07:35 -04005518 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5519
Yan Zhengf82d02d2008-10-29 14:49:05 -04005520 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005521 if (!path)
5522 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005523
Yan Zheng2c47e6052009-06-27 21:07:35 -04005524 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005525 if (!wc) {
5526 btrfs_free_path(path);
5527 return -ENOMEM;
5528 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005529
Chris Masonb9447ef82009-03-09 11:45:38 -04005530 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005531 parent_level = btrfs_header_level(parent);
David Sterba67439da2019-10-08 13:28:47 +02005532 atomic_inc(&parent->refs);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005533 path->nodes[parent_level] = parent;
5534 path->slots[parent_level] = btrfs_header_nritems(parent);
5535
Chris Masonb9447ef82009-03-09 11:45:38 -04005536 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005537 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005538 path->nodes[level] = node;
5539 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005540 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005541
5542 wc->refs[parent_level] = 1;
5543 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5544 wc->level = level;
5545 wc->shared_level = -1;
5546 wc->stage = DROP_REFERENCE;
5547 wc->update_ref = 0;
5548 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005549 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005550
5551 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005552 wret = walk_down_tree(trans, root, path, wc);
5553 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005554 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005555 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005556 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005557
Yan Zheng2c47e6052009-06-27 21:07:35 -04005558 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005559 if (wret < 0)
5560 ret = wret;
5561 if (wret != 0)
5562 break;
5563 }
5564
Yan Zheng2c47e6052009-06-27 21:07:35 -04005565 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005566 btrfs_free_path(path);
5567 return ret;
5568}
5569
Miao Xie6d07bce2011-01-05 10:07:31 +00005570/*
5571 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005572 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005573 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005574u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005575{
David Sterba32da53862019-10-29 19:20:18 +01005576 struct btrfs_block_group *block_group;
Miao Xie6d07bce2011-01-05 10:07:31 +00005577 u64 free_bytes = 0;
5578 int factor;
5579
Nicholas D Steeves01327612016-05-19 21:18:45 -04005580 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005581 if (list_empty(&sinfo->ro_bgs))
5582 return 0;
5583
5584 spin_lock(&sinfo->lock);
5585 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005586 spin_lock(&block_group->lock);
5587
5588 if (!block_group->ro) {
5589 spin_unlock(&block_group->lock);
5590 continue;
5591 }
5592
David Sterba46df06b2018-07-13 20:46:30 +02005593 factor = btrfs_bg_type_to_factor(block_group->flags);
David Sterbab3470b52019-10-23 18:48:22 +02005594 free_bytes += (block_group->length -
David Sterbabf38be62019-10-23 18:48:11 +02005595 block_group->used) * factor;
Miao Xie6d07bce2011-01-05 10:07:31 +00005596
5597 spin_unlock(&block_group->lock);
5598 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005599 spin_unlock(&sinfo->lock);
5600
5601 return free_bytes;
5602}
5603
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005604int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5605 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005606{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005607 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005608}
5609
Jeff Mahoney499f3772015-06-15 09:41:17 -04005610/*
5611 * It used to be that old block groups would be left around forever.
5612 * Iterating over them would be enough to trim unused space. Since we
5613 * now automatically remove them, we also need to iterate over unallocated
5614 * space.
5615 *
5616 * We don't want a transaction for this since the discard may take a
5617 * substantial amount of time. We don't require that a transaction be
5618 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005619 * to ensure that we're not discarding chunks that were released or
5620 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005621 *
5622 * Holding the chunks lock will prevent other threads from allocating
5623 * or releasing chunks, but it won't prevent a running transaction
5624 * from committing and releasing the memory that the pending chunks
5625 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005626 * transaction and hold the commit root sem. We only need to hold
5627 * it while performing the free space search since we have already
5628 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005629 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005630static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005631{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005632 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005633 int ret;
5634
5635 *trimmed = 0;
5636
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005637 /* Discard not supported = nothing to do. */
5638 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5639 return 0;
5640
Andrea Gelmini52042d82018-11-28 12:05:13 +01005641 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005642 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005643 return 0;
5644
5645 /* No free space = nothing to do. */
5646 if (device->total_bytes <= device->bytes_used)
5647 return 0;
5648
5649 ret = 0;
5650
5651 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005652 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005653 u64 bytes;
5654
5655 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5656 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005657 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005658
Nikolay Borisov929be172019-03-27 14:24:18 +02005659 find_first_clear_extent_bit(&device->alloc_state, start,
5660 &start, &end,
5661 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005662
5663 /* Ensure we skip the reserved area in the first 1M */
5664 start = max_t(u64, start, SZ_1M);
5665
Nikolay Borisov929be172019-03-27 14:24:18 +02005666 /*
5667 * If find_first_clear_extent_bit find a range that spans the
5668 * end of the device it will set end to -1, in this case it's up
5669 * to the caller to trim the value to the size of the device.
5670 */
5671 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005672
Nikolay Borisov929be172019-03-27 14:24:18 +02005673 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005674
Nikolay Borisov929be172019-03-27 14:24:18 +02005675 /* We didn't find any extents */
5676 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005677 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005678 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005679 break;
5680 }
5681
Nikolay Borisov929be172019-03-27 14:24:18 +02005682 ret = btrfs_issue_discard(device->bdev, start, len,
5683 &bytes);
5684 if (!ret)
5685 set_extent_bits(&device->alloc_state, start,
5686 start + bytes - 1,
5687 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005688 mutex_unlock(&fs_info->chunk_mutex);
5689
5690 if (ret)
5691 break;
5692
5693 start += len;
5694 *trimmed += bytes;
5695
5696 if (fatal_signal_pending(current)) {
5697 ret = -ERESTARTSYS;
5698 break;
5699 }
5700
5701 cond_resched();
5702 }
5703
5704 return ret;
5705}
5706
Qu Wenruo93bba242018-09-07 14:16:23 +08005707/*
5708 * Trim the whole filesystem by:
5709 * 1) trimming the free space in each block group
5710 * 2) trimming the unallocated space on each device
5711 *
5712 * This will also continue trimming even if a block group or device encounters
5713 * an error. The return value will be the last error, or 0 if nothing bad
5714 * happens.
5715 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005716int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005717{
David Sterba32da53862019-10-29 19:20:18 +01005718 struct btrfs_block_group *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005719 struct btrfs_device *device;
5720 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005721 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005722 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005723 u64 start;
5724 u64 end;
5725 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005726 u64 bg_failed = 0;
5727 u64 dev_failed = 0;
5728 int bg_ret = 0;
5729 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005730 int ret = 0;
5731
Qu Wenruo07301df2019-05-28 16:21:54 +08005732 /*
5733 * Check range overflow if range->len is set.
5734 * The default range->len is U64_MAX.
5735 */
5736 if (range->len != U64_MAX &&
5737 check_add_overflow(range->start, range->len, &range_end))
5738 return -EINVAL;
5739
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005740 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005741 for (; cache; cache = btrfs_next_block_group(cache)) {
David Sterbab3470b52019-10-23 18:48:22 +02005742 if (cache->start >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005743 btrfs_put_block_group(cache);
5744 break;
5745 }
5746
David Sterbab3470b52019-10-23 18:48:22 +02005747 start = max(range->start, cache->start);
5748 end = min(range_end, cache->start + cache->length);
Li Dongyangf7039b12011-03-24 10:24:28 +00005749
5750 if (end - start >= range->minlen) {
David Sterba32da53862019-10-29 19:20:18 +01005751 if (!btrfs_block_group_done(cache)) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005752 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005753 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005754 bg_failed++;
5755 bg_ret = ret;
5756 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005757 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005758 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005759 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005760 bg_failed++;
5761 bg_ret = ret;
5762 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005763 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005764 }
5765 ret = btrfs_trim_block_group(cache,
5766 &group_trimmed,
5767 start,
5768 end,
5769 range->minlen);
5770
5771 trimmed += group_trimmed;
5772 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005773 bg_failed++;
5774 bg_ret = ret;
5775 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005776 }
5777 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005778 }
5779
Qu Wenruo93bba242018-09-07 14:16:23 +08005780 if (bg_failed)
5781 btrfs_warn(fs_info,
5782 "failed to trim %llu block group(s), last error %d",
5783 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005784 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005785 devices = &fs_info->fs_devices->devices;
5786 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03005787 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005788 if (ret) {
5789 dev_failed++;
5790 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005791 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005792 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005793
5794 trimmed += group_trimmed;
5795 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005796 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005797
Qu Wenruo93bba242018-09-07 14:16:23 +08005798 if (dev_failed)
5799 btrfs_warn(fs_info,
5800 "failed to trim %llu device(s), last error %d",
5801 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005802 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005803 if (bg_ret)
5804 return bg_ret;
5805 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005806}